1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * /proc/sys support 4 */ 5 #include <linux/init.h> 6 #include <linux/sysctl.h> 7 #include <linux/poll.h> 8 #include <linux/proc_fs.h> 9 #include <linux/printk.h> 10 #include <linux/security.h> 11 #include <linux/sched.h> 12 #include <linux/cred.h> 13 #include <linux/namei.h> 14 #include <linux/mm.h> 15 #include <linux/uio.h> 16 #include <linux/module.h> 17 #include <linux/bpf-cgroup.h> 18 #include <linux/mount.h> 19 #include <linux/kmemleak.h> 20 #include "internal.h" 21 22 #define list_for_each_table_entry(entry, header) \ 23 for ((entry) = (header->ctl_table); (entry)->procname; (entry)++) 24 25 static const struct dentry_operations proc_sys_dentry_operations; 26 static const struct file_operations proc_sys_file_operations; 27 static const struct inode_operations proc_sys_inode_operations; 28 static const struct file_operations proc_sys_dir_file_operations; 29 static const struct inode_operations proc_sys_dir_operations; 30 31 /* Support for permanently empty directories */ 32 static struct ctl_table sysctl_mount_point[] = { 33 {.type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY } 34 }; 35 36 /** 37 * register_sysctl_mount_point() - registers a sysctl mount point 38 * @path: path for the mount point 39 * 40 * Used to create a permanently empty directory to serve as mount point. 41 * There are some subtle but important permission checks this allows in the 42 * case of unprivileged mounts. 43 */ 44 struct ctl_table_header *register_sysctl_mount_point(const char *path) 45 { 46 return register_sysctl(path, sysctl_mount_point); 47 } 48 EXPORT_SYMBOL(register_sysctl_mount_point); 49 50 #define sysctl_is_perm_empty_ctl_table(tptr) \ 51 (tptr[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) 52 #define sysctl_is_perm_empty_ctl_header(hptr) \ 53 (sysctl_is_perm_empty_ctl_table(hptr->ctl_table)) 54 #define sysctl_set_perm_empty_ctl_header(hptr) \ 55 (hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY) 56 #define sysctl_clear_perm_empty_ctl_header(hptr) \ 57 (hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_DEFAULT) 58 59 void proc_sys_poll_notify(struct ctl_table_poll *poll) 60 { 61 if (!poll) 62 return; 63 64 atomic_inc(&poll->event); 65 wake_up_interruptible(&poll->wait); 66 } 67 68 static struct ctl_table root_table[] = { 69 { 70 .procname = "", 71 .mode = S_IFDIR|S_IRUGO|S_IXUGO, 72 }, 73 { } 74 }; 75 static struct ctl_table_root sysctl_table_root = { 76 .default_set.dir.header = { 77 {{.count = 1, 78 .nreg = 1, 79 .ctl_table = root_table }}, 80 .ctl_table_arg = root_table, 81 .root = &sysctl_table_root, 82 .set = &sysctl_table_root.default_set, 83 }, 84 }; 85 86 static DEFINE_SPINLOCK(sysctl_lock); 87 88 static void drop_sysctl_table(struct ctl_table_header *header); 89 static int sysctl_follow_link(struct ctl_table_header **phead, 90 struct ctl_table **pentry); 91 static int insert_links(struct ctl_table_header *head); 92 static void put_links(struct ctl_table_header *header); 93 94 static void sysctl_print_dir(struct ctl_dir *dir) 95 { 96 if (dir->header.parent) 97 sysctl_print_dir(dir->header.parent); 98 pr_cont("%s/", dir->header.ctl_table[0].procname); 99 } 100 101 static int namecmp(const char *name1, int len1, const char *name2, int len2) 102 { 103 int cmp; 104 105 cmp = memcmp(name1, name2, min(len1, len2)); 106 if (cmp == 0) 107 cmp = len1 - len2; 108 return cmp; 109 } 110 111 /* Called under sysctl_lock */ 112 static struct ctl_table *find_entry(struct ctl_table_header **phead, 113 struct ctl_dir *dir, const char *name, int namelen) 114 { 115 struct ctl_table_header *head; 116 struct ctl_table *entry; 117 struct rb_node *node = dir->root.rb_node; 118 119 while (node) 120 { 121 struct ctl_node *ctl_node; 122 const char *procname; 123 int cmp; 124 125 ctl_node = rb_entry(node, struct ctl_node, node); 126 head = ctl_node->header; 127 entry = &head->ctl_table[ctl_node - head->node]; 128 procname = entry->procname; 129 130 cmp = namecmp(name, namelen, procname, strlen(procname)); 131 if (cmp < 0) 132 node = node->rb_left; 133 else if (cmp > 0) 134 node = node->rb_right; 135 else { 136 *phead = head; 137 return entry; 138 } 139 } 140 return NULL; 141 } 142 143 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) 144 { 145 struct rb_node *node = &head->node[entry - head->ctl_table].node; 146 struct rb_node **p = &head->parent->root.rb_node; 147 struct rb_node *parent = NULL; 148 const char *name = entry->procname; 149 int namelen = strlen(name); 150 151 while (*p) { 152 struct ctl_table_header *parent_head; 153 struct ctl_table *parent_entry; 154 struct ctl_node *parent_node; 155 const char *parent_name; 156 int cmp; 157 158 parent = *p; 159 parent_node = rb_entry(parent, struct ctl_node, node); 160 parent_head = parent_node->header; 161 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; 162 parent_name = parent_entry->procname; 163 164 cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); 165 if (cmp < 0) 166 p = &(*p)->rb_left; 167 else if (cmp > 0) 168 p = &(*p)->rb_right; 169 else { 170 pr_err("sysctl duplicate entry: "); 171 sysctl_print_dir(head->parent); 172 pr_cont("%s\n", entry->procname); 173 return -EEXIST; 174 } 175 } 176 177 rb_link_node(node, parent, p); 178 rb_insert_color(node, &head->parent->root); 179 return 0; 180 } 181 182 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) 183 { 184 struct rb_node *node = &head->node[entry - head->ctl_table].node; 185 186 rb_erase(node, &head->parent->root); 187 } 188 189 static void init_header(struct ctl_table_header *head, 190 struct ctl_table_root *root, struct ctl_table_set *set, 191 struct ctl_node *node, struct ctl_table *table, size_t table_size) 192 { 193 head->ctl_table = table; 194 head->ctl_table_size = table_size; 195 head->ctl_table_arg = table; 196 head->used = 0; 197 head->count = 1; 198 head->nreg = 1; 199 head->unregistering = NULL; 200 head->root = root; 201 head->set = set; 202 head->parent = NULL; 203 head->node = node; 204 INIT_HLIST_HEAD(&head->inodes); 205 if (node) { 206 struct ctl_table *entry; 207 208 list_for_each_table_entry(entry, head) { 209 node->header = head; 210 node++; 211 } 212 } 213 } 214 215 static void erase_header(struct ctl_table_header *head) 216 { 217 struct ctl_table *entry; 218 219 list_for_each_table_entry(entry, head) 220 erase_entry(head, entry); 221 } 222 223 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 224 { 225 struct ctl_table *entry; 226 struct ctl_table_header *dir_h = &dir->header; 227 int err; 228 229 230 /* Is this a permanently empty directory? */ 231 if (sysctl_is_perm_empty_ctl_header(dir_h)) 232 return -EROFS; 233 234 /* Am I creating a permanently empty directory? */ 235 if (sysctl_is_perm_empty_ctl_table(header->ctl_table)) { 236 if (!RB_EMPTY_ROOT(&dir->root)) 237 return -EINVAL; 238 sysctl_set_perm_empty_ctl_header(dir_h); 239 } 240 241 dir_h->nreg++; 242 header->parent = dir; 243 err = insert_links(header); 244 if (err) 245 goto fail_links; 246 list_for_each_table_entry(entry, header) { 247 err = insert_entry(header, entry); 248 if (err) 249 goto fail; 250 } 251 return 0; 252 fail: 253 erase_header(header); 254 put_links(header); 255 fail_links: 256 if (header->ctl_table == sysctl_mount_point) 257 sysctl_clear_perm_empty_ctl_header(dir_h); 258 header->parent = NULL; 259 drop_sysctl_table(dir_h); 260 return err; 261 } 262 263 /* called under sysctl_lock */ 264 static int use_table(struct ctl_table_header *p) 265 { 266 if (unlikely(p->unregistering)) 267 return 0; 268 p->used++; 269 return 1; 270 } 271 272 /* called under sysctl_lock */ 273 static void unuse_table(struct ctl_table_header *p) 274 { 275 if (!--p->used) 276 if (unlikely(p->unregistering)) 277 complete(p->unregistering); 278 } 279 280 static void proc_sys_invalidate_dcache(struct ctl_table_header *head) 281 { 282 proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock); 283 } 284 285 /* called under sysctl_lock, will reacquire if has to wait */ 286 static void start_unregistering(struct ctl_table_header *p) 287 { 288 /* 289 * if p->used is 0, nobody will ever touch that entry again; 290 * we'll eliminate all paths to it before dropping sysctl_lock 291 */ 292 if (unlikely(p->used)) { 293 struct completion wait; 294 init_completion(&wait); 295 p->unregistering = &wait; 296 spin_unlock(&sysctl_lock); 297 wait_for_completion(&wait); 298 } else { 299 /* anything non-NULL; we'll never dereference it */ 300 p->unregistering = ERR_PTR(-EINVAL); 301 spin_unlock(&sysctl_lock); 302 } 303 /* 304 * Invalidate dentries for unregistered sysctls: namespaced sysctls 305 * can have duplicate names and contaminate dcache very badly. 306 */ 307 proc_sys_invalidate_dcache(p); 308 /* 309 * do not remove from the list until nobody holds it; walking the 310 * list in do_sysctl() relies on that. 311 */ 312 spin_lock(&sysctl_lock); 313 erase_header(p); 314 } 315 316 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 317 { 318 BUG_ON(!head); 319 spin_lock(&sysctl_lock); 320 if (!use_table(head)) 321 head = ERR_PTR(-ENOENT); 322 spin_unlock(&sysctl_lock); 323 return head; 324 } 325 326 static void sysctl_head_finish(struct ctl_table_header *head) 327 { 328 if (!head) 329 return; 330 spin_lock(&sysctl_lock); 331 unuse_table(head); 332 spin_unlock(&sysctl_lock); 333 } 334 335 static struct ctl_table_set * 336 lookup_header_set(struct ctl_table_root *root) 337 { 338 struct ctl_table_set *set = &root->default_set; 339 if (root->lookup) 340 set = root->lookup(root); 341 return set; 342 } 343 344 static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 345 struct ctl_dir *dir, 346 const char *name, int namelen) 347 { 348 struct ctl_table_header *head; 349 struct ctl_table *entry; 350 351 spin_lock(&sysctl_lock); 352 entry = find_entry(&head, dir, name, namelen); 353 if (entry && use_table(head)) 354 *phead = head; 355 else 356 entry = NULL; 357 spin_unlock(&sysctl_lock); 358 return entry; 359 } 360 361 static struct ctl_node *first_usable_entry(struct rb_node *node) 362 { 363 struct ctl_node *ctl_node; 364 365 for (;node; node = rb_next(node)) { 366 ctl_node = rb_entry(node, struct ctl_node, node); 367 if (use_table(ctl_node->header)) 368 return ctl_node; 369 } 370 return NULL; 371 } 372 373 static void first_entry(struct ctl_dir *dir, 374 struct ctl_table_header **phead, struct ctl_table **pentry) 375 { 376 struct ctl_table_header *head = NULL; 377 struct ctl_table *entry = NULL; 378 struct ctl_node *ctl_node; 379 380 spin_lock(&sysctl_lock); 381 ctl_node = first_usable_entry(rb_first(&dir->root)); 382 spin_unlock(&sysctl_lock); 383 if (ctl_node) { 384 head = ctl_node->header; 385 entry = &head->ctl_table[ctl_node - head->node]; 386 } 387 *phead = head; 388 *pentry = entry; 389 } 390 391 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 392 { 393 struct ctl_table_header *head = *phead; 394 struct ctl_table *entry = *pentry; 395 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; 396 397 spin_lock(&sysctl_lock); 398 unuse_table(head); 399 400 ctl_node = first_usable_entry(rb_next(&ctl_node->node)); 401 spin_unlock(&sysctl_lock); 402 head = NULL; 403 if (ctl_node) { 404 head = ctl_node->header; 405 entry = &head->ctl_table[ctl_node - head->node]; 406 } 407 *phead = head; 408 *pentry = entry; 409 } 410 411 /* 412 * sysctl_perm does NOT grant the superuser all rights automatically, because 413 * some sysctl variables are readonly even to root. 414 */ 415 416 static int test_perm(int mode, int op) 417 { 418 if (uid_eq(current_euid(), GLOBAL_ROOT_UID)) 419 mode >>= 6; 420 else if (in_egroup_p(GLOBAL_ROOT_GID)) 421 mode >>= 3; 422 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 423 return 0; 424 return -EACCES; 425 } 426 427 static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op) 428 { 429 struct ctl_table_root *root = head->root; 430 int mode; 431 432 if (root->permissions) 433 mode = root->permissions(head, table); 434 else 435 mode = table->mode; 436 437 return test_perm(mode, op); 438 } 439 440 static struct inode *proc_sys_make_inode(struct super_block *sb, 441 struct ctl_table_header *head, struct ctl_table *table) 442 { 443 struct ctl_table_root *root = head->root; 444 struct inode *inode; 445 struct proc_inode *ei; 446 447 inode = new_inode(sb); 448 if (!inode) 449 return ERR_PTR(-ENOMEM); 450 451 inode->i_ino = get_next_ino(); 452 453 ei = PROC_I(inode); 454 455 spin_lock(&sysctl_lock); 456 if (unlikely(head->unregistering)) { 457 spin_unlock(&sysctl_lock); 458 iput(inode); 459 return ERR_PTR(-ENOENT); 460 } 461 ei->sysctl = head; 462 ei->sysctl_entry = table; 463 hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes); 464 head->count++; 465 spin_unlock(&sysctl_lock); 466 467 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 468 inode->i_mode = table->mode; 469 if (!S_ISDIR(table->mode)) { 470 inode->i_mode |= S_IFREG; 471 inode->i_op = &proc_sys_inode_operations; 472 inode->i_fop = &proc_sys_file_operations; 473 } else { 474 inode->i_mode |= S_IFDIR; 475 inode->i_op = &proc_sys_dir_operations; 476 inode->i_fop = &proc_sys_dir_file_operations; 477 if (sysctl_is_perm_empty_ctl_header(head)) 478 make_empty_dir_inode(inode); 479 } 480 481 if (root->set_ownership) 482 root->set_ownership(head, table, &inode->i_uid, &inode->i_gid); 483 else { 484 inode->i_uid = GLOBAL_ROOT_UID; 485 inode->i_gid = GLOBAL_ROOT_GID; 486 } 487 488 return inode; 489 } 490 491 void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head) 492 { 493 spin_lock(&sysctl_lock); 494 hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes); 495 if (!--head->count) 496 kfree_rcu(head, rcu); 497 spin_unlock(&sysctl_lock); 498 } 499 500 static struct ctl_table_header *grab_header(struct inode *inode) 501 { 502 struct ctl_table_header *head = PROC_I(inode)->sysctl; 503 if (!head) 504 head = &sysctl_table_root.default_set.dir.header; 505 return sysctl_head_grab(head); 506 } 507 508 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 509 unsigned int flags) 510 { 511 struct ctl_table_header *head = grab_header(dir); 512 struct ctl_table_header *h = NULL; 513 const struct qstr *name = &dentry->d_name; 514 struct ctl_table *p; 515 struct inode *inode; 516 struct dentry *err = ERR_PTR(-ENOENT); 517 struct ctl_dir *ctl_dir; 518 int ret; 519 520 if (IS_ERR(head)) 521 return ERR_CAST(head); 522 523 ctl_dir = container_of(head, struct ctl_dir, header); 524 525 p = lookup_entry(&h, ctl_dir, name->name, name->len); 526 if (!p) 527 goto out; 528 529 if (S_ISLNK(p->mode)) { 530 ret = sysctl_follow_link(&h, &p); 531 err = ERR_PTR(ret); 532 if (ret) 533 goto out; 534 } 535 536 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 537 if (IS_ERR(inode)) { 538 err = ERR_CAST(inode); 539 goto out; 540 } 541 542 d_set_d_op(dentry, &proc_sys_dentry_operations); 543 err = d_splice_alias(inode, dentry); 544 545 out: 546 if (h) 547 sysctl_head_finish(h); 548 sysctl_head_finish(head); 549 return err; 550 } 551 552 static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter, 553 int write) 554 { 555 struct inode *inode = file_inode(iocb->ki_filp); 556 struct ctl_table_header *head = grab_header(inode); 557 struct ctl_table *table = PROC_I(inode)->sysctl_entry; 558 size_t count = iov_iter_count(iter); 559 char *kbuf; 560 ssize_t error; 561 562 if (IS_ERR(head)) 563 return PTR_ERR(head); 564 565 /* 566 * At this point we know that the sysctl was not unregistered 567 * and won't be until we finish. 568 */ 569 error = -EPERM; 570 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ)) 571 goto out; 572 573 /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 574 error = -EINVAL; 575 if (!table->proc_handler) 576 goto out; 577 578 /* don't even try if the size is too large */ 579 error = -ENOMEM; 580 if (count >= KMALLOC_MAX_SIZE) 581 goto out; 582 kbuf = kvzalloc(count + 1, GFP_KERNEL); 583 if (!kbuf) 584 goto out; 585 586 if (write) { 587 error = -EFAULT; 588 if (!copy_from_iter_full(kbuf, count, iter)) 589 goto out_free_buf; 590 kbuf[count] = '\0'; 591 } 592 593 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count, 594 &iocb->ki_pos); 595 if (error) 596 goto out_free_buf; 597 598 /* careful: calling conventions are nasty here */ 599 error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos); 600 if (error) 601 goto out_free_buf; 602 603 if (!write) { 604 error = -EFAULT; 605 if (copy_to_iter(kbuf, count, iter) < count) 606 goto out_free_buf; 607 } 608 609 error = count; 610 out_free_buf: 611 kvfree(kbuf); 612 out: 613 sysctl_head_finish(head); 614 615 return error; 616 } 617 618 static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter) 619 { 620 return proc_sys_call_handler(iocb, iter, 0); 621 } 622 623 static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter) 624 { 625 return proc_sys_call_handler(iocb, iter, 1); 626 } 627 628 static int proc_sys_open(struct inode *inode, struct file *filp) 629 { 630 struct ctl_table_header *head = grab_header(inode); 631 struct ctl_table *table = PROC_I(inode)->sysctl_entry; 632 633 /* sysctl was unregistered */ 634 if (IS_ERR(head)) 635 return PTR_ERR(head); 636 637 if (table->poll) 638 filp->private_data = proc_sys_poll_event(table->poll); 639 640 sysctl_head_finish(head); 641 642 return 0; 643 } 644 645 static __poll_t proc_sys_poll(struct file *filp, poll_table *wait) 646 { 647 struct inode *inode = file_inode(filp); 648 struct ctl_table_header *head = grab_header(inode); 649 struct ctl_table *table = PROC_I(inode)->sysctl_entry; 650 __poll_t ret = DEFAULT_POLLMASK; 651 unsigned long event; 652 653 /* sysctl was unregistered */ 654 if (IS_ERR(head)) 655 return EPOLLERR | EPOLLHUP; 656 657 if (!table->proc_handler) 658 goto out; 659 660 if (!table->poll) 661 goto out; 662 663 event = (unsigned long)filp->private_data; 664 poll_wait(filp, &table->poll->wait, wait); 665 666 if (event != atomic_read(&table->poll->event)) { 667 filp->private_data = proc_sys_poll_event(table->poll); 668 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; 669 } 670 671 out: 672 sysctl_head_finish(head); 673 674 return ret; 675 } 676 677 static bool proc_sys_fill_cache(struct file *file, 678 struct dir_context *ctx, 679 struct ctl_table_header *head, 680 struct ctl_table *table) 681 { 682 struct dentry *child, *dir = file->f_path.dentry; 683 struct inode *inode; 684 struct qstr qname; 685 ino_t ino = 0; 686 unsigned type = DT_UNKNOWN; 687 688 qname.name = table->procname; 689 qname.len = strlen(table->procname); 690 qname.hash = full_name_hash(dir, qname.name, qname.len); 691 692 child = d_lookup(dir, &qname); 693 if (!child) { 694 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 695 child = d_alloc_parallel(dir, &qname, &wq); 696 if (IS_ERR(child)) 697 return false; 698 if (d_in_lookup(child)) { 699 struct dentry *res; 700 inode = proc_sys_make_inode(dir->d_sb, head, table); 701 if (IS_ERR(inode)) { 702 d_lookup_done(child); 703 dput(child); 704 return false; 705 } 706 d_set_d_op(child, &proc_sys_dentry_operations); 707 res = d_splice_alias(inode, child); 708 d_lookup_done(child); 709 if (unlikely(res)) { 710 if (IS_ERR(res)) { 711 dput(child); 712 return false; 713 } 714 dput(child); 715 child = res; 716 } 717 } 718 } 719 inode = d_inode(child); 720 ino = inode->i_ino; 721 type = inode->i_mode >> 12; 722 dput(child); 723 return dir_emit(ctx, qname.name, qname.len, ino, type); 724 } 725 726 static bool proc_sys_link_fill_cache(struct file *file, 727 struct dir_context *ctx, 728 struct ctl_table_header *head, 729 struct ctl_table *table) 730 { 731 bool ret = true; 732 733 head = sysctl_head_grab(head); 734 if (IS_ERR(head)) 735 return false; 736 737 /* It is not an error if we can not follow the link ignore it */ 738 if (sysctl_follow_link(&head, &table)) 739 goto out; 740 741 ret = proc_sys_fill_cache(file, ctx, head, table); 742 out: 743 sysctl_head_finish(head); 744 return ret; 745 } 746 747 static int scan(struct ctl_table_header *head, struct ctl_table *table, 748 unsigned long *pos, struct file *file, 749 struct dir_context *ctx) 750 { 751 bool res; 752 753 if ((*pos)++ < ctx->pos) 754 return true; 755 756 if (unlikely(S_ISLNK(table->mode))) 757 res = proc_sys_link_fill_cache(file, ctx, head, table); 758 else 759 res = proc_sys_fill_cache(file, ctx, head, table); 760 761 if (res) 762 ctx->pos = *pos; 763 764 return res; 765 } 766 767 static int proc_sys_readdir(struct file *file, struct dir_context *ctx) 768 { 769 struct ctl_table_header *head = grab_header(file_inode(file)); 770 struct ctl_table_header *h = NULL; 771 struct ctl_table *entry; 772 struct ctl_dir *ctl_dir; 773 unsigned long pos; 774 775 if (IS_ERR(head)) 776 return PTR_ERR(head); 777 778 ctl_dir = container_of(head, struct ctl_dir, header); 779 780 if (!dir_emit_dots(file, ctx)) 781 goto out; 782 783 pos = 2; 784 785 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 786 if (!scan(h, entry, &pos, file, ctx)) { 787 sysctl_head_finish(h); 788 break; 789 } 790 } 791 out: 792 sysctl_head_finish(head); 793 return 0; 794 } 795 796 static int proc_sys_permission(struct mnt_idmap *idmap, 797 struct inode *inode, int mask) 798 { 799 /* 800 * sysctl entries that are not writeable, 801 * are _NOT_ writeable, capabilities or not. 802 */ 803 struct ctl_table_header *head; 804 struct ctl_table *table; 805 int error; 806 807 /* Executable files are not allowed under /proc/sys/ */ 808 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 809 return -EACCES; 810 811 head = grab_header(inode); 812 if (IS_ERR(head)) 813 return PTR_ERR(head); 814 815 table = PROC_I(inode)->sysctl_entry; 816 if (!table) /* global root - r-xr-xr-x */ 817 error = mask & MAY_WRITE ? -EACCES : 0; 818 else /* Use the permissions on the sysctl table entry */ 819 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK); 820 821 sysctl_head_finish(head); 822 return error; 823 } 824 825 static int proc_sys_setattr(struct mnt_idmap *idmap, 826 struct dentry *dentry, struct iattr *attr) 827 { 828 struct inode *inode = d_inode(dentry); 829 int error; 830 831 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 832 return -EPERM; 833 834 error = setattr_prepare(&nop_mnt_idmap, dentry, attr); 835 if (error) 836 return error; 837 838 setattr_copy(&nop_mnt_idmap, inode, attr); 839 return 0; 840 } 841 842 static int proc_sys_getattr(struct mnt_idmap *idmap, 843 const struct path *path, struct kstat *stat, 844 u32 request_mask, unsigned int query_flags) 845 { 846 struct inode *inode = d_inode(path->dentry); 847 struct ctl_table_header *head = grab_header(inode); 848 struct ctl_table *table = PROC_I(inode)->sysctl_entry; 849 850 if (IS_ERR(head)) 851 return PTR_ERR(head); 852 853 generic_fillattr(&nop_mnt_idmap, inode, stat); 854 if (table) 855 stat->mode = (stat->mode & S_IFMT) | table->mode; 856 857 sysctl_head_finish(head); 858 return 0; 859 } 860 861 static const struct file_operations proc_sys_file_operations = { 862 .open = proc_sys_open, 863 .poll = proc_sys_poll, 864 .read_iter = proc_sys_read, 865 .write_iter = proc_sys_write, 866 .splice_read = copy_splice_read, 867 .splice_write = iter_file_splice_write, 868 .llseek = default_llseek, 869 }; 870 871 static const struct file_operations proc_sys_dir_file_operations = { 872 .read = generic_read_dir, 873 .iterate_shared = proc_sys_readdir, 874 .llseek = generic_file_llseek, 875 }; 876 877 static const struct inode_operations proc_sys_inode_operations = { 878 .permission = proc_sys_permission, 879 .setattr = proc_sys_setattr, 880 .getattr = proc_sys_getattr, 881 }; 882 883 static const struct inode_operations proc_sys_dir_operations = { 884 .lookup = proc_sys_lookup, 885 .permission = proc_sys_permission, 886 .setattr = proc_sys_setattr, 887 .getattr = proc_sys_getattr, 888 }; 889 890 static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags) 891 { 892 if (flags & LOOKUP_RCU) 893 return -ECHILD; 894 return !PROC_I(d_inode(dentry))->sysctl->unregistering; 895 } 896 897 static int proc_sys_delete(const struct dentry *dentry) 898 { 899 return !!PROC_I(d_inode(dentry))->sysctl->unregistering; 900 } 901 902 static int sysctl_is_seen(struct ctl_table_header *p) 903 { 904 struct ctl_table_set *set = p->set; 905 int res; 906 spin_lock(&sysctl_lock); 907 if (p->unregistering) 908 res = 0; 909 else if (!set->is_seen) 910 res = 1; 911 else 912 res = set->is_seen(set); 913 spin_unlock(&sysctl_lock); 914 return res; 915 } 916 917 static int proc_sys_compare(const struct dentry *dentry, 918 unsigned int len, const char *str, const struct qstr *name) 919 { 920 struct ctl_table_header *head; 921 struct inode *inode; 922 923 /* Although proc doesn't have negative dentries, rcu-walk means 924 * that inode here can be NULL */ 925 /* AV: can it, indeed? */ 926 inode = d_inode_rcu(dentry); 927 if (!inode) 928 return 1; 929 if (name->len != len) 930 return 1; 931 if (memcmp(name->name, str, len)) 932 return 1; 933 head = rcu_dereference(PROC_I(inode)->sysctl); 934 return !head || !sysctl_is_seen(head); 935 } 936 937 static const struct dentry_operations proc_sys_dentry_operations = { 938 .d_revalidate = proc_sys_revalidate, 939 .d_delete = proc_sys_delete, 940 .d_compare = proc_sys_compare, 941 }; 942 943 static struct ctl_dir *find_subdir(struct ctl_dir *dir, 944 const char *name, int namelen) 945 { 946 struct ctl_table_header *head; 947 struct ctl_table *entry; 948 949 entry = find_entry(&head, dir, name, namelen); 950 if (!entry) 951 return ERR_PTR(-ENOENT); 952 if (!S_ISDIR(entry->mode)) 953 return ERR_PTR(-ENOTDIR); 954 return container_of(head, struct ctl_dir, header); 955 } 956 957 static struct ctl_dir *new_dir(struct ctl_table_set *set, 958 const char *name, int namelen) 959 { 960 struct ctl_table *table; 961 struct ctl_dir *new; 962 struct ctl_node *node; 963 char *new_name; 964 965 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + 966 sizeof(struct ctl_table)*2 + namelen + 1, 967 GFP_KERNEL); 968 if (!new) 969 return NULL; 970 971 node = (struct ctl_node *)(new + 1); 972 table = (struct ctl_table *)(node + 1); 973 new_name = (char *)(table + 2); 974 memcpy(new_name, name, namelen); 975 table[0].procname = new_name; 976 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 977 init_header(&new->header, set->dir.header.root, set, node, table, 1); 978 979 return new; 980 } 981 982 /** 983 * get_subdir - find or create a subdir with the specified name. 984 * @dir: Directory to create the subdirectory in 985 * @name: The name of the subdirectory to find or create 986 * @namelen: The length of name 987 * 988 * Takes a directory with an elevated reference count so we know that 989 * if we drop the lock the directory will not go away. Upon success 990 * the reference is moved from @dir to the returned subdirectory. 991 * Upon error an error code is returned and the reference on @dir is 992 * simply dropped. 993 */ 994 static struct ctl_dir *get_subdir(struct ctl_dir *dir, 995 const char *name, int namelen) 996 { 997 struct ctl_table_set *set = dir->header.set; 998 struct ctl_dir *subdir, *new = NULL; 999 int err; 1000 1001 spin_lock(&sysctl_lock); 1002 subdir = find_subdir(dir, name, namelen); 1003 if (!IS_ERR(subdir)) 1004 goto found; 1005 if (PTR_ERR(subdir) != -ENOENT) 1006 goto failed; 1007 1008 spin_unlock(&sysctl_lock); 1009 new = new_dir(set, name, namelen); 1010 spin_lock(&sysctl_lock); 1011 subdir = ERR_PTR(-ENOMEM); 1012 if (!new) 1013 goto failed; 1014 1015 /* Was the subdir added while we dropped the lock? */ 1016 subdir = find_subdir(dir, name, namelen); 1017 if (!IS_ERR(subdir)) 1018 goto found; 1019 if (PTR_ERR(subdir) != -ENOENT) 1020 goto failed; 1021 1022 /* Nope. Use the our freshly made directory entry. */ 1023 err = insert_header(dir, &new->header); 1024 subdir = ERR_PTR(err); 1025 if (err) 1026 goto failed; 1027 subdir = new; 1028 found: 1029 subdir->header.nreg++; 1030 failed: 1031 if (IS_ERR(subdir)) { 1032 pr_err("sysctl could not get directory: "); 1033 sysctl_print_dir(dir); 1034 pr_cont("%*.*s %ld\n", namelen, namelen, name, 1035 PTR_ERR(subdir)); 1036 } 1037 drop_sysctl_table(&dir->header); 1038 if (new) 1039 drop_sysctl_table(&new->header); 1040 spin_unlock(&sysctl_lock); 1041 return subdir; 1042 } 1043 1044 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 1045 { 1046 struct ctl_dir *parent; 1047 const char *procname; 1048 if (!dir->header.parent) 1049 return &set->dir; 1050 parent = xlate_dir(set, dir->header.parent); 1051 if (IS_ERR(parent)) 1052 return parent; 1053 procname = dir->header.ctl_table[0].procname; 1054 return find_subdir(parent, procname, strlen(procname)); 1055 } 1056 1057 static int sysctl_follow_link(struct ctl_table_header **phead, 1058 struct ctl_table **pentry) 1059 { 1060 struct ctl_table_header *head; 1061 struct ctl_table_root *root; 1062 struct ctl_table_set *set; 1063 struct ctl_table *entry; 1064 struct ctl_dir *dir; 1065 int ret; 1066 1067 spin_lock(&sysctl_lock); 1068 root = (*pentry)->data; 1069 set = lookup_header_set(root); 1070 dir = xlate_dir(set, (*phead)->parent); 1071 if (IS_ERR(dir)) 1072 ret = PTR_ERR(dir); 1073 else { 1074 const char *procname = (*pentry)->procname; 1075 head = NULL; 1076 entry = find_entry(&head, dir, procname, strlen(procname)); 1077 ret = -ENOENT; 1078 if (entry && use_table(head)) { 1079 unuse_table(*phead); 1080 *phead = head; 1081 *pentry = entry; 1082 ret = 0; 1083 } 1084 } 1085 1086 spin_unlock(&sysctl_lock); 1087 return ret; 1088 } 1089 1090 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 1091 { 1092 struct va_format vaf; 1093 va_list args; 1094 1095 va_start(args, fmt); 1096 vaf.fmt = fmt; 1097 vaf.va = &args; 1098 1099 pr_err("sysctl table check failed: %s/%s %pV\n", 1100 path, table->procname, &vaf); 1101 1102 va_end(args); 1103 return -EINVAL; 1104 } 1105 1106 static int sysctl_check_table_array(const char *path, struct ctl_table *table) 1107 { 1108 int err = 0; 1109 1110 if ((table->proc_handler == proc_douintvec) || 1111 (table->proc_handler == proc_douintvec_minmax)) { 1112 if (table->maxlen != sizeof(unsigned int)) 1113 err |= sysctl_err(path, table, "array not allowed"); 1114 } 1115 1116 if (table->proc_handler == proc_dou8vec_minmax) { 1117 if (table->maxlen != sizeof(u8)) 1118 err |= sysctl_err(path, table, "array not allowed"); 1119 } 1120 1121 if (table->proc_handler == proc_dobool) { 1122 if (table->maxlen != sizeof(bool)) 1123 err |= sysctl_err(path, table, "array not allowed"); 1124 } 1125 1126 return err; 1127 } 1128 1129 static int sysctl_check_table(const char *path, struct ctl_table_header *header) 1130 { 1131 struct ctl_table *entry; 1132 int err = 0; 1133 list_for_each_table_entry(entry, header) { 1134 if ((entry->proc_handler == proc_dostring) || 1135 (entry->proc_handler == proc_dobool) || 1136 (entry->proc_handler == proc_dointvec) || 1137 (entry->proc_handler == proc_douintvec) || 1138 (entry->proc_handler == proc_douintvec_minmax) || 1139 (entry->proc_handler == proc_dointvec_minmax) || 1140 (entry->proc_handler == proc_dou8vec_minmax) || 1141 (entry->proc_handler == proc_dointvec_jiffies) || 1142 (entry->proc_handler == proc_dointvec_userhz_jiffies) || 1143 (entry->proc_handler == proc_dointvec_ms_jiffies) || 1144 (entry->proc_handler == proc_doulongvec_minmax) || 1145 (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 1146 if (!entry->data) 1147 err |= sysctl_err(path, entry, "No data"); 1148 if (!entry->maxlen) 1149 err |= sysctl_err(path, entry, "No maxlen"); 1150 else 1151 err |= sysctl_check_table_array(path, entry); 1152 } 1153 if (!entry->proc_handler) 1154 err |= sysctl_err(path, entry, "No proc_handler"); 1155 1156 if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode) 1157 err |= sysctl_err(path, entry, "bogus .mode 0%o", 1158 entry->mode); 1159 } 1160 return err; 1161 } 1162 1163 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_header *head) 1164 { 1165 struct ctl_table *link_table, *entry, *link; 1166 struct ctl_table_header *links; 1167 struct ctl_node *node; 1168 char *link_name; 1169 int nr_entries, name_bytes; 1170 1171 name_bytes = 0; 1172 nr_entries = 0; 1173 list_for_each_table_entry(entry, head) { 1174 nr_entries++; 1175 name_bytes += strlen(entry->procname) + 1; 1176 } 1177 1178 links = kzalloc(sizeof(struct ctl_table_header) + 1179 sizeof(struct ctl_node)*nr_entries + 1180 sizeof(struct ctl_table)*(nr_entries + 1) + 1181 name_bytes, 1182 GFP_KERNEL); 1183 1184 if (!links) 1185 return NULL; 1186 1187 node = (struct ctl_node *)(links + 1); 1188 link_table = (struct ctl_table *)(node + nr_entries); 1189 link_name = (char *)&link_table[nr_entries + 1]; 1190 link = link_table; 1191 1192 list_for_each_table_entry(entry, head) { 1193 int len = strlen(entry->procname) + 1; 1194 memcpy(link_name, entry->procname, len); 1195 link->procname = link_name; 1196 link->mode = S_IFLNK|S_IRWXUGO; 1197 link->data = head->root; 1198 link_name += len; 1199 link++; 1200 } 1201 init_header(links, dir->header.root, dir->header.set, node, link_table, 1202 head->ctl_table_size); 1203 links->nreg = nr_entries; 1204 1205 return links; 1206 } 1207 1208 static bool get_links(struct ctl_dir *dir, 1209 struct ctl_table_header *header, 1210 struct ctl_table_root *link_root) 1211 { 1212 struct ctl_table_header *tmp_head; 1213 struct ctl_table *entry, *link; 1214 1215 /* Are there links available for every entry in table? */ 1216 list_for_each_table_entry(entry, header) { 1217 const char *procname = entry->procname; 1218 link = find_entry(&tmp_head, dir, procname, strlen(procname)); 1219 if (!link) 1220 return false; 1221 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 1222 continue; 1223 if (S_ISLNK(link->mode) && (link->data == link_root)) 1224 continue; 1225 return false; 1226 } 1227 1228 /* The checks passed. Increase the registration count on the links */ 1229 list_for_each_table_entry(entry, header) { 1230 const char *procname = entry->procname; 1231 link = find_entry(&tmp_head, dir, procname, strlen(procname)); 1232 tmp_head->nreg++; 1233 } 1234 return true; 1235 } 1236 1237 static int insert_links(struct ctl_table_header *head) 1238 { 1239 struct ctl_table_set *root_set = &sysctl_table_root.default_set; 1240 struct ctl_dir *core_parent; 1241 struct ctl_table_header *links; 1242 int err; 1243 1244 if (head->set == root_set) 1245 return 0; 1246 1247 core_parent = xlate_dir(root_set, head->parent); 1248 if (IS_ERR(core_parent)) 1249 return 0; 1250 1251 if (get_links(core_parent, head, head->root)) 1252 return 0; 1253 1254 core_parent->header.nreg++; 1255 spin_unlock(&sysctl_lock); 1256 1257 links = new_links(core_parent, head); 1258 1259 spin_lock(&sysctl_lock); 1260 err = -ENOMEM; 1261 if (!links) 1262 goto out; 1263 1264 err = 0; 1265 if (get_links(core_parent, head, head->root)) { 1266 kfree(links); 1267 goto out; 1268 } 1269 1270 err = insert_header(core_parent, links); 1271 if (err) 1272 kfree(links); 1273 out: 1274 drop_sysctl_table(&core_parent->header); 1275 return err; 1276 } 1277 1278 /* Find the directory for the ctl_table. If one is not found create it. */ 1279 static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path) 1280 { 1281 const char *name, *nextname; 1282 1283 for (name = path; name; name = nextname) { 1284 int namelen; 1285 nextname = strchr(name, '/'); 1286 if (nextname) { 1287 namelen = nextname - name; 1288 nextname++; 1289 } else { 1290 namelen = strlen(name); 1291 } 1292 if (namelen == 0) 1293 continue; 1294 1295 /* 1296 * namelen ensures if name is "foo/bar/yay" only foo is 1297 * registered first. We traverse as if using mkdir -p and 1298 * return a ctl_dir for the last directory entry. 1299 */ 1300 dir = get_subdir(dir, name, namelen); 1301 if (IS_ERR(dir)) 1302 break; 1303 } 1304 return dir; 1305 } 1306 1307 /** 1308 * __register_sysctl_table - register a leaf sysctl table 1309 * @set: Sysctl tree to register on 1310 * @path: The path to the directory the sysctl table is in. 1311 * @table: the top-level table structure without any child. This table 1312 * should not be free'd after registration. So it should not be 1313 * used on stack. It can either be a global or dynamically allocated 1314 * by the caller and free'd later after sysctl unregistration. 1315 * @table_size : The number of elements in table 1316 * 1317 * Register a sysctl table hierarchy. @table should be a filled in ctl_table 1318 * array. A completely 0 filled entry terminates the table. 1319 * 1320 * The members of the &struct ctl_table structure are used as follows: 1321 * 1322 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 1323 * enter a sysctl file 1324 * 1325 * data - a pointer to data for use by proc_handler 1326 * 1327 * maxlen - the maximum size in bytes of the data 1328 * 1329 * mode - the file permissions for the /proc/sys file 1330 * 1331 * child - must be %NULL. 1332 * 1333 * proc_handler - the text handler routine (described below) 1334 * 1335 * extra1, extra2 - extra pointers usable by the proc handler routines 1336 * XXX: we should eventually modify these to use long min / max [0] 1337 * [0] https://lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org 1338 * 1339 * Leaf nodes in the sysctl tree will be represented by a single file 1340 * under /proc; non-leaf nodes (where child is not NULL) are not allowed, 1341 * sysctl_check_table() verifies this. 1342 * 1343 * There must be a proc_handler routine for any terminal nodes. 1344 * Several default handlers are available to cover common cases - 1345 * 1346 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 1347 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 1348 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 1349 * 1350 * It is the handler's job to read the input buffer from user memory 1351 * and process it. The handler should return 0 on success. 1352 * 1353 * This routine returns %NULL on a failure to register, and a pointer 1354 * to the table header on success. 1355 */ 1356 struct ctl_table_header *__register_sysctl_table( 1357 struct ctl_table_set *set, 1358 const char *path, struct ctl_table *table, size_t table_size) 1359 { 1360 struct ctl_table_root *root = set->dir.header.root; 1361 struct ctl_table_header *header; 1362 struct ctl_dir *dir; 1363 struct ctl_node *node; 1364 1365 header = kzalloc(sizeof(struct ctl_table_header) + 1366 sizeof(struct ctl_node)*table_size, GFP_KERNEL_ACCOUNT); 1367 if (!header) 1368 return NULL; 1369 1370 node = (struct ctl_node *)(header + 1); 1371 init_header(header, root, set, node, table, table_size); 1372 if (sysctl_check_table(path, header)) 1373 goto fail; 1374 1375 spin_lock(&sysctl_lock); 1376 dir = &set->dir; 1377 /* Reference moved down the directory tree get_subdir */ 1378 dir->header.nreg++; 1379 spin_unlock(&sysctl_lock); 1380 1381 dir = sysctl_mkdir_p(dir, path); 1382 if (IS_ERR(dir)) 1383 goto fail; 1384 spin_lock(&sysctl_lock); 1385 if (insert_header(dir, header)) 1386 goto fail_put_dir_locked; 1387 1388 drop_sysctl_table(&dir->header); 1389 spin_unlock(&sysctl_lock); 1390 1391 return header; 1392 1393 fail_put_dir_locked: 1394 drop_sysctl_table(&dir->header); 1395 spin_unlock(&sysctl_lock); 1396 fail: 1397 kfree(header); 1398 return NULL; 1399 } 1400 1401 /** 1402 * register_sysctl - register a sysctl table 1403 * @path: The path to the directory the sysctl table is in. If the path 1404 * doesn't exist we will create it for you. 1405 * @table: the table structure. The calller must ensure the life of the @table 1406 * will be kept during the lifetime use of the syctl. It must not be freed 1407 * until unregister_sysctl_table() is called with the given returned table 1408 * with this registration. If your code is non modular then you don't need 1409 * to call unregister_sysctl_table() and can instead use something like 1410 * register_sysctl_init() which does not care for the result of the syctl 1411 * registration. 1412 * 1413 * Register a sysctl table. @table should be a filled in ctl_table 1414 * array. A completely 0 filled entry terminates the table. 1415 * 1416 * See __register_sysctl_table for more details. 1417 */ 1418 struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) 1419 { 1420 int count = 0; 1421 struct ctl_table *entry; 1422 struct ctl_table_header t_hdr; 1423 1424 t_hdr.ctl_table = table; 1425 list_for_each_table_entry(entry, (&t_hdr)) 1426 count++; 1427 return __register_sysctl_table(&sysctl_table_root.default_set, 1428 path, table, count); 1429 } 1430 EXPORT_SYMBOL(register_sysctl); 1431 1432 /** 1433 * __register_sysctl_init() - register sysctl table to path 1434 * @path: path name for sysctl base. If that path doesn't exist we will create 1435 * it for you. 1436 * @table: This is the sysctl table that needs to be registered to the path. 1437 * The caller must ensure the life of the @table will be kept during the 1438 * lifetime use of the sysctl. 1439 * @table_name: The name of sysctl table, only used for log printing when 1440 * registration fails 1441 * 1442 * The sysctl interface is used by userspace to query or modify at runtime 1443 * a predefined value set on a variable. These variables however have default 1444 * values pre-set. Code which depends on these variables will always work even 1445 * if register_sysctl() fails. If register_sysctl() fails you'd just loose the 1446 * ability to query or modify the sysctls dynamically at run time. Chances of 1447 * register_sysctl() failing on init are extremely low, and so for both reasons 1448 * this function does not return any error as it is used by initialization code. 1449 * 1450 * Context: if your base directory does not exist it will be created for you. 1451 */ 1452 void __init __register_sysctl_init(const char *path, struct ctl_table *table, 1453 const char *table_name) 1454 { 1455 struct ctl_table_header *hdr = register_sysctl(path, table); 1456 1457 if (unlikely(!hdr)) { 1458 pr_err("failed when register_sysctl %s to %s\n", table_name, path); 1459 return; 1460 } 1461 kmemleak_not_leak(hdr); 1462 } 1463 1464 static void put_links(struct ctl_table_header *header) 1465 { 1466 struct ctl_table_set *root_set = &sysctl_table_root.default_set; 1467 struct ctl_table_root *root = header->root; 1468 struct ctl_dir *parent = header->parent; 1469 struct ctl_dir *core_parent; 1470 struct ctl_table *entry; 1471 1472 if (header->set == root_set) 1473 return; 1474 1475 core_parent = xlate_dir(root_set, parent); 1476 if (IS_ERR(core_parent)) 1477 return; 1478 1479 list_for_each_table_entry(entry, header) { 1480 struct ctl_table_header *link_head; 1481 struct ctl_table *link; 1482 const char *name = entry->procname; 1483 1484 link = find_entry(&link_head, core_parent, name, strlen(name)); 1485 if (link && 1486 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 1487 (S_ISLNK(link->mode) && (link->data == root)))) { 1488 drop_sysctl_table(link_head); 1489 } 1490 else { 1491 pr_err("sysctl link missing during unregister: "); 1492 sysctl_print_dir(parent); 1493 pr_cont("%s\n", name); 1494 } 1495 } 1496 } 1497 1498 static void drop_sysctl_table(struct ctl_table_header *header) 1499 { 1500 struct ctl_dir *parent = header->parent; 1501 1502 if (--header->nreg) 1503 return; 1504 1505 if (parent) { 1506 put_links(header); 1507 start_unregistering(header); 1508 } 1509 1510 if (!--header->count) 1511 kfree_rcu(header, rcu); 1512 1513 if (parent) 1514 drop_sysctl_table(&parent->header); 1515 } 1516 1517 /** 1518 * unregister_sysctl_table - unregister a sysctl table hierarchy 1519 * @header: the header returned from register_sysctl or __register_sysctl_table 1520 * 1521 * Unregisters the sysctl table and all children. proc entries may not 1522 * actually be removed until they are no longer used by anyone. 1523 */ 1524 void unregister_sysctl_table(struct ctl_table_header * header) 1525 { 1526 might_sleep(); 1527 1528 if (header == NULL) 1529 return; 1530 1531 spin_lock(&sysctl_lock); 1532 drop_sysctl_table(header); 1533 spin_unlock(&sysctl_lock); 1534 } 1535 EXPORT_SYMBOL(unregister_sysctl_table); 1536 1537 void setup_sysctl_set(struct ctl_table_set *set, 1538 struct ctl_table_root *root, 1539 int (*is_seen)(struct ctl_table_set *)) 1540 { 1541 memset(set, 0, sizeof(*set)); 1542 set->is_seen = is_seen; 1543 init_header(&set->dir.header, root, set, NULL, root_table, 1); 1544 } 1545 1546 void retire_sysctl_set(struct ctl_table_set *set) 1547 { 1548 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); 1549 } 1550 1551 int __init proc_sys_init(void) 1552 { 1553 struct proc_dir_entry *proc_sys_root; 1554 1555 proc_sys_root = proc_mkdir("sys", NULL); 1556 proc_sys_root->proc_iops = &proc_sys_dir_operations; 1557 proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations; 1558 proc_sys_root->nlink = 0; 1559 1560 return sysctl_init_bases(); 1561 } 1562 1563 struct sysctl_alias { 1564 const char *kernel_param; 1565 const char *sysctl_param; 1566 }; 1567 1568 /* 1569 * Historically some settings had both sysctl and a command line parameter. 1570 * With the generic sysctl. parameter support, we can handle them at a single 1571 * place and only keep the historical name for compatibility. This is not meant 1572 * to add brand new aliases. When adding existing aliases, consider whether 1573 * the possibly different moment of changing the value (e.g. from early_param 1574 * to the moment do_sysctl_args() is called) is an issue for the specific 1575 * parameter. 1576 */ 1577 static const struct sysctl_alias sysctl_aliases[] = { 1578 {"hardlockup_all_cpu_backtrace", "kernel.hardlockup_all_cpu_backtrace" }, 1579 {"hung_task_panic", "kernel.hung_task_panic" }, 1580 {"numa_zonelist_order", "vm.numa_zonelist_order" }, 1581 {"softlockup_all_cpu_backtrace", "kernel.softlockup_all_cpu_backtrace" }, 1582 {"softlockup_panic", "kernel.softlockup_panic" }, 1583 { } 1584 }; 1585 1586 static const char *sysctl_find_alias(char *param) 1587 { 1588 const struct sysctl_alias *alias; 1589 1590 for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) { 1591 if (strcmp(alias->kernel_param, param) == 0) 1592 return alias->sysctl_param; 1593 } 1594 1595 return NULL; 1596 } 1597 1598 /* Set sysctl value passed on kernel command line. */ 1599 static int process_sysctl_arg(char *param, char *val, 1600 const char *unused, void *arg) 1601 { 1602 char *path; 1603 struct vfsmount **proc_mnt = arg; 1604 struct file_system_type *proc_fs_type; 1605 struct file *file; 1606 int len; 1607 int err; 1608 loff_t pos = 0; 1609 ssize_t wret; 1610 1611 if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) { 1612 param += sizeof("sysctl") - 1; 1613 1614 if (param[0] != '/' && param[0] != '.') 1615 return 0; 1616 1617 param++; 1618 } else { 1619 param = (char *) sysctl_find_alias(param); 1620 if (!param) 1621 return 0; 1622 } 1623 1624 if (!val) 1625 return -EINVAL; 1626 len = strlen(val); 1627 if (len == 0) 1628 return -EINVAL; 1629 1630 /* 1631 * To set sysctl options, we use a temporary mount of proc, look up the 1632 * respective sys/ file and write to it. To avoid mounting it when no 1633 * options were given, we mount it only when the first sysctl option is 1634 * found. Why not a persistent mount? There are problems with a 1635 * persistent mount of proc in that it forces userspace not to use any 1636 * proc mount options. 1637 */ 1638 if (!*proc_mnt) { 1639 proc_fs_type = get_fs_type("proc"); 1640 if (!proc_fs_type) { 1641 pr_err("Failed to find procfs to set sysctl from command line\n"); 1642 return 0; 1643 } 1644 *proc_mnt = kern_mount(proc_fs_type); 1645 put_filesystem(proc_fs_type); 1646 if (IS_ERR(*proc_mnt)) { 1647 pr_err("Failed to mount procfs to set sysctl from command line\n"); 1648 return 0; 1649 } 1650 } 1651 1652 path = kasprintf(GFP_KERNEL, "sys/%s", param); 1653 if (!path) 1654 panic("%s: Failed to allocate path for %s\n", __func__, param); 1655 strreplace(path, '.', '/'); 1656 1657 file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0); 1658 if (IS_ERR(file)) { 1659 err = PTR_ERR(file); 1660 if (err == -ENOENT) 1661 pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n", 1662 param, val); 1663 else if (err == -EACCES) 1664 pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n", 1665 param, val); 1666 else 1667 pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n", 1668 file, param, val); 1669 goto out; 1670 } 1671 wret = kernel_write(file, val, len, &pos); 1672 if (wret < 0) { 1673 err = wret; 1674 if (err == -EINVAL) 1675 pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n", 1676 param, val); 1677 else 1678 pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n", 1679 ERR_PTR(err), param, val); 1680 } else if (wret != len) { 1681 pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n", 1682 wret, len, path, param, val); 1683 } 1684 1685 err = filp_close(file, NULL); 1686 if (err) 1687 pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n", 1688 ERR_PTR(err), param, val); 1689 out: 1690 kfree(path); 1691 return 0; 1692 } 1693 1694 void do_sysctl_args(void) 1695 { 1696 char *command_line; 1697 struct vfsmount *proc_mnt = NULL; 1698 1699 command_line = kstrdup(saved_command_line, GFP_KERNEL); 1700 if (!command_line) 1701 panic("%s: Failed to allocate copy of command line\n", __func__); 1702 1703 parse_args("Setting sysctl args", command_line, 1704 NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg); 1705 1706 if (proc_mnt) 1707 kern_unmount(proc_mnt); 1708 1709 kfree(command_line); 1710 } 1711