1 // SPDX-License-Identifier: GPL-2.0
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>
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>
22 #define list_for_each_table_entry(entry, table) \
23 for ((entry) = (table); (entry)->procname; (entry)++)
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;
31 /* Support for permanently empty directories */
32 static struct ctl_table sysctl_mount_point[] = {
33 {.type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY }
37 * register_sysctl_mount_point() - registers a sysctl mount point
38 * @path: path for the mount point
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.
44 struct ctl_table_header *register_sysctl_mount_point(const char *path)
46 return register_sysctl(path, sysctl_mount_point);
48 EXPORT_SYMBOL(register_sysctl_mount_point);
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)
59 void proc_sys_poll_notify(struct ctl_table_poll *poll)
64 atomic_inc(&poll->event);
65 wake_up_interruptible(&poll->wait);
68 static struct ctl_table root_table[] = {
71 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
75 static struct ctl_table_root sysctl_table_root = {
76 .default_set.dir.header = {
79 .ctl_table = root_table }},
80 .ctl_table_arg = root_table,
81 .root = &sysctl_table_root,
82 .set = &sysctl_table_root.default_set,
86 static DEFINE_SPINLOCK(sysctl_lock);
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);
94 static void sysctl_print_dir(struct ctl_dir *dir)
96 if (dir->header.parent)
97 sysctl_print_dir(dir->header.parent);
98 pr_cont("%s/", dir->header.ctl_table[0].procname);
101 static int namecmp(const char *name1, int len1, const char *name2, int len2)
105 cmp = memcmp(name1, name2, min(len1, len2));
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)
115 struct ctl_table_header *head;
116 struct ctl_table *entry;
117 struct rb_node *node = dir->root.rb_node;
121 struct ctl_node *ctl_node;
122 const char *procname;
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;
130 cmp = namecmp(name, namelen, procname, strlen(procname));
132 node = node->rb_left;
134 node = node->rb_right;
143 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
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);
152 struct ctl_table_header *parent_head;
153 struct ctl_table *parent_entry;
154 struct ctl_node *parent_node;
155 const char *parent_name;
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;
164 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
170 pr_err("sysctl duplicate entry: ");
171 sysctl_print_dir(head->parent);
172 pr_cont("%s\n", entry->procname);
177 rb_link_node(node, parent, p);
178 rb_insert_color(node, &head->parent->root);
182 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
184 struct rb_node *node = &head->node[entry - head->ctl_table].node;
186 rb_erase(node, &head->parent->root);
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)
193 head->ctl_table = table;
194 head->ctl_table_arg = table;
198 head->unregistering = NULL;
203 INIT_HLIST_HEAD(&head->inodes);
205 struct ctl_table *entry;
207 list_for_each_table_entry(entry, table) {
214 static void erase_header(struct ctl_table_header *head)
216 struct ctl_table *entry;
218 list_for_each_table_entry(entry, head->ctl_table)
219 erase_entry(head, entry);
222 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
224 struct ctl_table *entry;
225 struct ctl_table_header *dir_h = &dir->header;
229 /* Is this a permanently empty directory? */
230 if (sysctl_is_perm_empty_ctl_header(dir_h))
233 /* Am I creating a permanently empty directory? */
234 if (sysctl_is_perm_empty_ctl_table(header->ctl_table)) {
235 if (!RB_EMPTY_ROOT(&dir->root))
237 sysctl_set_perm_empty_ctl_header(dir_h);
241 header->parent = dir;
242 err = insert_links(header);
245 list_for_each_table_entry(entry, header->ctl_table) {
246 err = insert_entry(header, entry);
252 erase_header(header);
255 if (header->ctl_table == sysctl_mount_point)
256 sysctl_clear_perm_empty_ctl_header(dir_h);
257 header->parent = NULL;
258 drop_sysctl_table(dir_h);
262 /* called under sysctl_lock */
263 static int use_table(struct ctl_table_header *p)
265 if (unlikely(p->unregistering))
271 /* called under sysctl_lock */
272 static void unuse_table(struct ctl_table_header *p)
275 if (unlikely(p->unregistering))
276 complete(p->unregistering);
279 static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
281 proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
284 /* called under sysctl_lock, will reacquire if has to wait */
285 static void start_unregistering(struct ctl_table_header *p)
288 * if p->used is 0, nobody will ever touch that entry again;
289 * we'll eliminate all paths to it before dropping sysctl_lock
291 if (unlikely(p->used)) {
292 struct completion wait;
293 init_completion(&wait);
294 p->unregistering = &wait;
295 spin_unlock(&sysctl_lock);
296 wait_for_completion(&wait);
298 /* anything non-NULL; we'll never dereference it */
299 p->unregistering = ERR_PTR(-EINVAL);
300 spin_unlock(&sysctl_lock);
303 * Invalidate dentries for unregistered sysctls: namespaced sysctls
304 * can have duplicate names and contaminate dcache very badly.
306 proc_sys_invalidate_dcache(p);
308 * do not remove from the list until nobody holds it; walking the
309 * list in do_sysctl() relies on that.
311 spin_lock(&sysctl_lock);
315 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
318 spin_lock(&sysctl_lock);
319 if (!use_table(head))
320 head = ERR_PTR(-ENOENT);
321 spin_unlock(&sysctl_lock);
325 static void sysctl_head_finish(struct ctl_table_header *head)
329 spin_lock(&sysctl_lock);
331 spin_unlock(&sysctl_lock);
334 static struct ctl_table_set *
335 lookup_header_set(struct ctl_table_root *root)
337 struct ctl_table_set *set = &root->default_set;
339 set = root->lookup(root);
343 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
345 const char *name, int namelen)
347 struct ctl_table_header *head;
348 struct ctl_table *entry;
350 spin_lock(&sysctl_lock);
351 entry = find_entry(&head, dir, name, namelen);
352 if (entry && use_table(head))
356 spin_unlock(&sysctl_lock);
360 static struct ctl_node *first_usable_entry(struct rb_node *node)
362 struct ctl_node *ctl_node;
364 for (;node; node = rb_next(node)) {
365 ctl_node = rb_entry(node, struct ctl_node, node);
366 if (use_table(ctl_node->header))
372 static void first_entry(struct ctl_dir *dir,
373 struct ctl_table_header **phead, struct ctl_table **pentry)
375 struct ctl_table_header *head = NULL;
376 struct ctl_table *entry = NULL;
377 struct ctl_node *ctl_node;
379 spin_lock(&sysctl_lock);
380 ctl_node = first_usable_entry(rb_first(&dir->root));
381 spin_unlock(&sysctl_lock);
383 head = ctl_node->header;
384 entry = &head->ctl_table[ctl_node - head->node];
390 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
392 struct ctl_table_header *head = *phead;
393 struct ctl_table *entry = *pentry;
394 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
396 spin_lock(&sysctl_lock);
399 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
400 spin_unlock(&sysctl_lock);
403 head = ctl_node->header;
404 entry = &head->ctl_table[ctl_node - head->node];
411 * sysctl_perm does NOT grant the superuser all rights automatically, because
412 * some sysctl variables are readonly even to root.
415 static int test_perm(int mode, int op)
417 if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
419 else if (in_egroup_p(GLOBAL_ROOT_GID))
421 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
426 static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
428 struct ctl_table_root *root = head->root;
431 if (root->permissions)
432 mode = root->permissions(head, table);
436 return test_perm(mode, op);
439 static struct inode *proc_sys_make_inode(struct super_block *sb,
440 struct ctl_table_header *head, struct ctl_table *table)
442 struct ctl_table_root *root = head->root;
444 struct proc_inode *ei;
446 inode = new_inode(sb);
448 return ERR_PTR(-ENOMEM);
450 inode->i_ino = get_next_ino();
454 spin_lock(&sysctl_lock);
455 if (unlikely(head->unregistering)) {
456 spin_unlock(&sysctl_lock);
458 return ERR_PTR(-ENOENT);
461 ei->sysctl_entry = table;
462 hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
464 spin_unlock(&sysctl_lock);
466 inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
467 inode->i_mode = table->mode;
468 if (!S_ISDIR(table->mode)) {
469 inode->i_mode |= S_IFREG;
470 inode->i_op = &proc_sys_inode_operations;
471 inode->i_fop = &proc_sys_file_operations;
473 inode->i_mode |= S_IFDIR;
474 inode->i_op = &proc_sys_dir_operations;
475 inode->i_fop = &proc_sys_dir_file_operations;
476 if (sysctl_is_perm_empty_ctl_header(head))
477 make_empty_dir_inode(inode);
480 if (root->set_ownership)
481 root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
483 inode->i_uid = GLOBAL_ROOT_UID;
484 inode->i_gid = GLOBAL_ROOT_GID;
490 void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
492 spin_lock(&sysctl_lock);
493 hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
495 kfree_rcu(head, rcu);
496 spin_unlock(&sysctl_lock);
499 static struct ctl_table_header *grab_header(struct inode *inode)
501 struct ctl_table_header *head = PROC_I(inode)->sysctl;
503 head = &sysctl_table_root.default_set.dir.header;
504 return sysctl_head_grab(head);
507 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
510 struct ctl_table_header *head = grab_header(dir);
511 struct ctl_table_header *h = NULL;
512 const struct qstr *name = &dentry->d_name;
515 struct dentry *err = ERR_PTR(-ENOENT);
516 struct ctl_dir *ctl_dir;
520 return ERR_CAST(head);
522 ctl_dir = container_of(head, struct ctl_dir, header);
524 p = lookup_entry(&h, ctl_dir, name->name, name->len);
528 if (S_ISLNK(p->mode)) {
529 ret = sysctl_follow_link(&h, &p);
535 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
537 err = ERR_CAST(inode);
541 d_set_d_op(dentry, &proc_sys_dentry_operations);
542 err = d_splice_alias(inode, dentry);
546 sysctl_head_finish(h);
547 sysctl_head_finish(head);
551 static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
554 struct inode *inode = file_inode(iocb->ki_filp);
555 struct ctl_table_header *head = grab_header(inode);
556 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
557 size_t count = iov_iter_count(iter);
562 return PTR_ERR(head);
565 * At this point we know that the sysctl was not unregistered
566 * and won't be until we finish.
569 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
572 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
574 if (!table->proc_handler)
577 /* don't even try if the size is too large */
579 if (count >= KMALLOC_MAX_SIZE)
581 kbuf = kvzalloc(count + 1, GFP_KERNEL);
587 if (!copy_from_iter_full(kbuf, count, iter))
592 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
597 /* careful: calling conventions are nasty here */
598 error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
604 if (copy_to_iter(kbuf, count, iter) < count)
612 sysctl_head_finish(head);
617 static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
619 return proc_sys_call_handler(iocb, iter, 0);
622 static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
624 return proc_sys_call_handler(iocb, iter, 1);
627 static int proc_sys_open(struct inode *inode, struct file *filp)
629 struct ctl_table_header *head = grab_header(inode);
630 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
632 /* sysctl was unregistered */
634 return PTR_ERR(head);
637 filp->private_data = proc_sys_poll_event(table->poll);
639 sysctl_head_finish(head);
644 static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
646 struct inode *inode = file_inode(filp);
647 struct ctl_table_header *head = grab_header(inode);
648 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
649 __poll_t ret = DEFAULT_POLLMASK;
652 /* sysctl was unregistered */
654 return EPOLLERR | EPOLLHUP;
656 if (!table->proc_handler)
662 event = (unsigned long)filp->private_data;
663 poll_wait(filp, &table->poll->wait, wait);
665 if (event != atomic_read(&table->poll->event)) {
666 filp->private_data = proc_sys_poll_event(table->poll);
667 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
671 sysctl_head_finish(head);
676 static bool proc_sys_fill_cache(struct file *file,
677 struct dir_context *ctx,
678 struct ctl_table_header *head,
679 struct ctl_table *table)
681 struct dentry *child, *dir = file->f_path.dentry;
685 unsigned type = DT_UNKNOWN;
687 qname.name = table->procname;
688 qname.len = strlen(table->procname);
689 qname.hash = full_name_hash(dir, qname.name, qname.len);
691 child = d_lookup(dir, &qname);
693 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
694 child = d_alloc_parallel(dir, &qname, &wq);
697 if (d_in_lookup(child)) {
699 inode = proc_sys_make_inode(dir->d_sb, head, table);
701 d_lookup_done(child);
705 d_set_d_op(child, &proc_sys_dentry_operations);
706 res = d_splice_alias(inode, child);
707 d_lookup_done(child);
718 inode = d_inode(child);
720 type = inode->i_mode >> 12;
722 return dir_emit(ctx, qname.name, qname.len, ino, type);
725 static bool proc_sys_link_fill_cache(struct file *file,
726 struct dir_context *ctx,
727 struct ctl_table_header *head,
728 struct ctl_table *table)
732 head = sysctl_head_grab(head);
736 /* It is not an error if we can not follow the link ignore it */
737 if (sysctl_follow_link(&head, &table))
740 ret = proc_sys_fill_cache(file, ctx, head, table);
742 sysctl_head_finish(head);
746 static int scan(struct ctl_table_header *head, struct ctl_table *table,
747 unsigned long *pos, struct file *file,
748 struct dir_context *ctx)
752 if ((*pos)++ < ctx->pos)
755 if (unlikely(S_ISLNK(table->mode)))
756 res = proc_sys_link_fill_cache(file, ctx, head, table);
758 res = proc_sys_fill_cache(file, ctx, head, table);
766 static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
768 struct ctl_table_header *head = grab_header(file_inode(file));
769 struct ctl_table_header *h = NULL;
770 struct ctl_table *entry;
771 struct ctl_dir *ctl_dir;
775 return PTR_ERR(head);
777 ctl_dir = container_of(head, struct ctl_dir, header);
779 if (!dir_emit_dots(file, ctx))
784 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
785 if (!scan(h, entry, &pos, file, ctx)) {
786 sysctl_head_finish(h);
791 sysctl_head_finish(head);
795 static int proc_sys_permission(struct mnt_idmap *idmap,
796 struct inode *inode, int mask)
799 * sysctl entries that are not writeable,
800 * are _NOT_ writeable, capabilities or not.
802 struct ctl_table_header *head;
803 struct ctl_table *table;
806 /* Executable files are not allowed under /proc/sys/ */
807 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
810 head = grab_header(inode);
812 return PTR_ERR(head);
814 table = PROC_I(inode)->sysctl_entry;
815 if (!table) /* global root - r-xr-xr-x */
816 error = mask & MAY_WRITE ? -EACCES : 0;
817 else /* Use the permissions on the sysctl table entry */
818 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
820 sysctl_head_finish(head);
824 static int proc_sys_setattr(struct mnt_idmap *idmap,
825 struct dentry *dentry, struct iattr *attr)
827 struct inode *inode = d_inode(dentry);
830 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
833 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
837 setattr_copy(&nop_mnt_idmap, inode, attr);
841 static int proc_sys_getattr(struct mnt_idmap *idmap,
842 const struct path *path, struct kstat *stat,
843 u32 request_mask, unsigned int query_flags)
845 struct inode *inode = d_inode(path->dentry);
846 struct ctl_table_header *head = grab_header(inode);
847 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
850 return PTR_ERR(head);
852 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
854 stat->mode = (stat->mode & S_IFMT) | table->mode;
856 sysctl_head_finish(head);
860 static const struct file_operations proc_sys_file_operations = {
861 .open = proc_sys_open,
862 .poll = proc_sys_poll,
863 .read_iter = proc_sys_read,
864 .write_iter = proc_sys_write,
865 .splice_read = copy_splice_read,
866 .splice_write = iter_file_splice_write,
867 .llseek = default_llseek,
870 static const struct file_operations proc_sys_dir_file_operations = {
871 .read = generic_read_dir,
872 .iterate_shared = proc_sys_readdir,
873 .llseek = generic_file_llseek,
876 static const struct inode_operations proc_sys_inode_operations = {
877 .permission = proc_sys_permission,
878 .setattr = proc_sys_setattr,
879 .getattr = proc_sys_getattr,
882 static const struct inode_operations proc_sys_dir_operations = {
883 .lookup = proc_sys_lookup,
884 .permission = proc_sys_permission,
885 .setattr = proc_sys_setattr,
886 .getattr = proc_sys_getattr,
889 static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
891 if (flags & LOOKUP_RCU)
893 return !PROC_I(d_inode(dentry))->sysctl->unregistering;
896 static int proc_sys_delete(const struct dentry *dentry)
898 return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
901 static int sysctl_is_seen(struct ctl_table_header *p)
903 struct ctl_table_set *set = p->set;
905 spin_lock(&sysctl_lock);
906 if (p->unregistering)
908 else if (!set->is_seen)
911 res = set->is_seen(set);
912 spin_unlock(&sysctl_lock);
916 static int proc_sys_compare(const struct dentry *dentry,
917 unsigned int len, const char *str, const struct qstr *name)
919 struct ctl_table_header *head;
922 /* Although proc doesn't have negative dentries, rcu-walk means
923 * that inode here can be NULL */
924 /* AV: can it, indeed? */
925 inode = d_inode_rcu(dentry);
928 if (name->len != len)
930 if (memcmp(name->name, str, len))
932 head = rcu_dereference(PROC_I(inode)->sysctl);
933 return !head || !sysctl_is_seen(head);
936 static const struct dentry_operations proc_sys_dentry_operations = {
937 .d_revalidate = proc_sys_revalidate,
938 .d_delete = proc_sys_delete,
939 .d_compare = proc_sys_compare,
942 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
943 const char *name, int namelen)
945 struct ctl_table_header *head;
946 struct ctl_table *entry;
948 entry = find_entry(&head, dir, name, namelen);
950 return ERR_PTR(-ENOENT);
951 if (!S_ISDIR(entry->mode))
952 return ERR_PTR(-ENOTDIR);
953 return container_of(head, struct ctl_dir, header);
956 static struct ctl_dir *new_dir(struct ctl_table_set *set,
957 const char *name, int namelen)
959 struct ctl_table *table;
961 struct ctl_node *node;
964 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
965 sizeof(struct ctl_table)*2 + namelen + 1,
970 node = (struct ctl_node *)(new + 1);
971 table = (struct ctl_table *)(node + 1);
972 new_name = (char *)(table + 2);
973 memcpy(new_name, name, namelen);
974 table[0].procname = new_name;
975 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
976 init_header(&new->header, set->dir.header.root, set, node, table);
982 * get_subdir - find or create a subdir with the specified name.
983 * @dir: Directory to create the subdirectory in
984 * @name: The name of the subdirectory to find or create
985 * @namelen: The length of name
987 * Takes a directory with an elevated reference count so we know that
988 * if we drop the lock the directory will not go away. Upon success
989 * the reference is moved from @dir to the returned subdirectory.
990 * Upon error an error code is returned and the reference on @dir is
993 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
994 const char *name, int namelen)
996 struct ctl_table_set *set = dir->header.set;
997 struct ctl_dir *subdir, *new = NULL;
1000 spin_lock(&sysctl_lock);
1001 subdir = find_subdir(dir, name, namelen);
1002 if (!IS_ERR(subdir))
1004 if (PTR_ERR(subdir) != -ENOENT)
1007 spin_unlock(&sysctl_lock);
1008 new = new_dir(set, name, namelen);
1009 spin_lock(&sysctl_lock);
1010 subdir = ERR_PTR(-ENOMEM);
1014 /* Was the subdir added while we dropped the lock? */
1015 subdir = find_subdir(dir, name, namelen);
1016 if (!IS_ERR(subdir))
1018 if (PTR_ERR(subdir) != -ENOENT)
1021 /* Nope. Use the our freshly made directory entry. */
1022 err = insert_header(dir, &new->header);
1023 subdir = ERR_PTR(err);
1028 subdir->header.nreg++;
1030 if (IS_ERR(subdir)) {
1031 pr_err("sysctl could not get directory: ");
1032 sysctl_print_dir(dir);
1033 pr_cont("%*.*s %ld\n", namelen, namelen, name,
1036 drop_sysctl_table(&dir->header);
1038 drop_sysctl_table(&new->header);
1039 spin_unlock(&sysctl_lock);
1043 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1045 struct ctl_dir *parent;
1046 const char *procname;
1047 if (!dir->header.parent)
1049 parent = xlate_dir(set, dir->header.parent);
1052 procname = dir->header.ctl_table[0].procname;
1053 return find_subdir(parent, procname, strlen(procname));
1056 static int sysctl_follow_link(struct ctl_table_header **phead,
1057 struct ctl_table **pentry)
1059 struct ctl_table_header *head;
1060 struct ctl_table_root *root;
1061 struct ctl_table_set *set;
1062 struct ctl_table *entry;
1063 struct ctl_dir *dir;
1066 spin_lock(&sysctl_lock);
1067 root = (*pentry)->data;
1068 set = lookup_header_set(root);
1069 dir = xlate_dir(set, (*phead)->parent);
1073 const char *procname = (*pentry)->procname;
1075 entry = find_entry(&head, dir, procname, strlen(procname));
1077 if (entry && use_table(head)) {
1078 unuse_table(*phead);
1085 spin_unlock(&sysctl_lock);
1089 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1091 struct va_format vaf;
1094 va_start(args, fmt);
1098 pr_err("sysctl table check failed: %s/%s %pV\n",
1099 path, table->procname, &vaf);
1105 static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1109 if ((table->proc_handler == proc_douintvec) ||
1110 (table->proc_handler == proc_douintvec_minmax)) {
1111 if (table->maxlen != sizeof(unsigned int))
1112 err |= sysctl_err(path, table, "array not allowed");
1115 if (table->proc_handler == proc_dou8vec_minmax) {
1116 if (table->maxlen != sizeof(u8))
1117 err |= sysctl_err(path, table, "array not allowed");
1120 if (table->proc_handler == proc_dobool) {
1121 if (table->maxlen != sizeof(bool))
1122 err |= sysctl_err(path, table, "array not allowed");
1128 static int sysctl_check_table(const char *path, struct ctl_table *table)
1130 struct ctl_table *entry;
1132 list_for_each_table_entry(entry, table) {
1133 if ((entry->proc_handler == proc_dostring) ||
1134 (entry->proc_handler == proc_dobool) ||
1135 (entry->proc_handler == proc_dointvec) ||
1136 (entry->proc_handler == proc_douintvec) ||
1137 (entry->proc_handler == proc_douintvec_minmax) ||
1138 (entry->proc_handler == proc_dointvec_minmax) ||
1139 (entry->proc_handler == proc_dou8vec_minmax) ||
1140 (entry->proc_handler == proc_dointvec_jiffies) ||
1141 (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1142 (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1143 (entry->proc_handler == proc_doulongvec_minmax) ||
1144 (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1146 err |= sysctl_err(path, entry, "No data");
1148 err |= sysctl_err(path, entry, "No maxlen");
1150 err |= sysctl_check_table_array(path, entry);
1152 if (!entry->proc_handler)
1153 err |= sysctl_err(path, entry, "No proc_handler");
1155 if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1156 err |= sysctl_err(path, entry, "bogus .mode 0%o",
1162 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1163 struct ctl_table_root *link_root)
1165 struct ctl_table *link_table, *entry, *link;
1166 struct ctl_table_header *links;
1167 struct ctl_node *node;
1169 int nr_entries, name_bytes;
1173 list_for_each_table_entry(entry, table) {
1175 name_bytes += strlen(entry->procname) + 1;
1178 links = kzalloc(sizeof(struct ctl_table_header) +
1179 sizeof(struct ctl_node)*nr_entries +
1180 sizeof(struct ctl_table)*(nr_entries + 1) +
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];
1192 list_for_each_table_entry(entry, table) {
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 = link_root;
1201 init_header(links, dir->header.root, dir->header.set, node, link_table);
1202 links->nreg = nr_entries;
1207 static bool get_links(struct ctl_dir *dir,
1208 struct ctl_table *table, struct ctl_table_root *link_root)
1210 struct ctl_table_header *head;
1211 struct ctl_table *entry, *link;
1213 /* Are there links available for every entry in table? */
1214 list_for_each_table_entry(entry, table) {
1215 const char *procname = entry->procname;
1216 link = find_entry(&head, dir, procname, strlen(procname));
1219 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1221 if (S_ISLNK(link->mode) && (link->data == link_root))
1226 /* The checks passed. Increase the registration count on the links */
1227 list_for_each_table_entry(entry, table) {
1228 const char *procname = entry->procname;
1229 link = find_entry(&head, dir, procname, strlen(procname));
1235 static int insert_links(struct ctl_table_header *head)
1237 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1238 struct ctl_dir *core_parent;
1239 struct ctl_table_header *links;
1242 if (head->set == root_set)
1245 core_parent = xlate_dir(root_set, head->parent);
1246 if (IS_ERR(core_parent))
1249 if (get_links(core_parent, head->ctl_table, head->root))
1252 core_parent->header.nreg++;
1253 spin_unlock(&sysctl_lock);
1255 links = new_links(core_parent, head->ctl_table, head->root);
1257 spin_lock(&sysctl_lock);
1263 if (get_links(core_parent, head->ctl_table, head->root)) {
1268 err = insert_header(core_parent, links);
1272 drop_sysctl_table(&core_parent->header);
1276 /* Find the directory for the ctl_table. If one is not found create it. */
1277 static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
1279 const char *name, *nextname;
1281 for (name = path; name; name = nextname) {
1283 nextname = strchr(name, '/');
1285 namelen = nextname - name;
1288 namelen = strlen(name);
1294 * namelen ensures if name is "foo/bar/yay" only foo is
1295 * registered first. We traverse as if using mkdir -p and
1296 * return a ctl_dir for the last directory entry.
1298 dir = get_subdir(dir, name, namelen);
1306 * __register_sysctl_table - register a leaf sysctl table
1307 * @set: Sysctl tree to register on
1308 * @path: The path to the directory the sysctl table is in.
1309 * @table: the top-level table structure without any child. This table
1310 * should not be free'd after registration. So it should not be
1311 * used on stack. It can either be a global or dynamically allocated
1312 * by the caller and free'd later after sysctl unregistration.
1314 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1315 * array. A completely 0 filled entry terminates the table.
1317 * The members of the &struct ctl_table structure are used as follows:
1319 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1320 * enter a sysctl file
1322 * data - a pointer to data for use by proc_handler
1324 * maxlen - the maximum size in bytes of the data
1326 * mode - the file permissions for the /proc/sys file
1328 * child - must be %NULL.
1330 * proc_handler - the text handler routine (described below)
1332 * extra1, extra2 - extra pointers usable by the proc handler routines
1333 * XXX: we should eventually modify these to use long min / max [0]
1336 * Leaf nodes in the sysctl tree will be represented by a single file
1337 * under /proc; non-leaf nodes (where child is not NULL) are not allowed,
1338 * sysctl_check_table() verifies this.
1340 * There must be a proc_handler routine for any terminal nodes.
1341 * Several default handlers are available to cover common cases -
1343 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1344 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1345 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1347 * It is the handler's job to read the input buffer from user memory
1348 * and process it. The handler should return 0 on success.
1350 * This routine returns %NULL on a failure to register, and a pointer
1351 * to the table header on success.
1353 struct ctl_table_header *__register_sysctl_table(
1354 struct ctl_table_set *set,
1355 const char *path, struct ctl_table *table)
1357 struct ctl_table_root *root = set->dir.header.root;
1358 struct ctl_table_header *header;
1359 struct ctl_dir *dir;
1360 struct ctl_table *entry;
1361 struct ctl_node *node;
1364 list_for_each_table_entry(entry, table)
1367 header = kzalloc(sizeof(struct ctl_table_header) +
1368 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL_ACCOUNT);
1372 node = (struct ctl_node *)(header + 1);
1373 init_header(header, root, set, node, table);
1374 if (sysctl_check_table(path, table))
1377 spin_lock(&sysctl_lock);
1379 /* Reference moved down the directory tree get_subdir */
1381 spin_unlock(&sysctl_lock);
1383 dir = sysctl_mkdir_p(dir, path);
1386 spin_lock(&sysctl_lock);
1387 if (insert_header(dir, header))
1388 goto fail_put_dir_locked;
1390 drop_sysctl_table(&dir->header);
1391 spin_unlock(&sysctl_lock);
1395 fail_put_dir_locked:
1396 drop_sysctl_table(&dir->header);
1397 spin_unlock(&sysctl_lock);
1404 * register_sysctl - register a sysctl table
1405 * @path: The path to the directory the sysctl table is in. If the path
1406 * doesn't exist we will create it for you.
1407 * @table: the table structure. The calller must ensure the life of the @table
1408 * will be kept during the lifetime use of the syctl. It must not be freed
1409 * until unregister_sysctl_table() is called with the given returned table
1410 * with this registration. If your code is non modular then you don't need
1411 * to call unregister_sysctl_table() and can instead use something like
1412 * register_sysctl_init() which does not care for the result of the syctl
1415 * Register a sysctl table. @table should be a filled in ctl_table
1416 * array. A completely 0 filled entry terminates the table.
1418 * See __register_sysctl_table for more details.
1420 struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1422 return __register_sysctl_table(&sysctl_table_root.default_set,
1425 EXPORT_SYMBOL(register_sysctl);
1428 * __register_sysctl_init() - register sysctl table to path
1429 * @path: path name for sysctl base. If that path doesn't exist we will create
1431 * @table: This is the sysctl table that needs to be registered to the path.
1432 * The caller must ensure the life of the @table will be kept during the
1433 * lifetime use of the sysctl.
1434 * @table_name: The name of sysctl table, only used for log printing when
1435 * registration fails
1437 * The sysctl interface is used by userspace to query or modify at runtime
1438 * a predefined value set on a variable. These variables however have default
1439 * values pre-set. Code which depends on these variables will always work even
1440 * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1441 * ability to query or modify the sysctls dynamically at run time. Chances of
1442 * register_sysctl() failing on init are extremely low, and so for both reasons
1443 * this function does not return any error as it is used by initialization code.
1445 * Context: if your base directory does not exist it will be created for you.
1447 void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1448 const char *table_name)
1450 struct ctl_table_header *hdr = register_sysctl(path, table);
1452 if (unlikely(!hdr)) {
1453 pr_err("failed when register_sysctl %s to %s\n", table_name, path);
1456 kmemleak_not_leak(hdr);
1459 static void put_links(struct ctl_table_header *header)
1461 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1462 struct ctl_table_root *root = header->root;
1463 struct ctl_dir *parent = header->parent;
1464 struct ctl_dir *core_parent;
1465 struct ctl_table *entry;
1467 if (header->set == root_set)
1470 core_parent = xlate_dir(root_set, parent);
1471 if (IS_ERR(core_parent))
1474 list_for_each_table_entry(entry, header->ctl_table) {
1475 struct ctl_table_header *link_head;
1476 struct ctl_table *link;
1477 const char *name = entry->procname;
1479 link = find_entry(&link_head, core_parent, name, strlen(name));
1481 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1482 (S_ISLNK(link->mode) && (link->data == root)))) {
1483 drop_sysctl_table(link_head);
1486 pr_err("sysctl link missing during unregister: ");
1487 sysctl_print_dir(parent);
1488 pr_cont("%s\n", name);
1493 static void drop_sysctl_table(struct ctl_table_header *header)
1495 struct ctl_dir *parent = header->parent;
1502 start_unregistering(header);
1505 if (!--header->count)
1506 kfree_rcu(header, rcu);
1509 drop_sysctl_table(&parent->header);
1513 * unregister_sysctl_table - unregister a sysctl table hierarchy
1514 * @header: the header returned from register_sysctl or __register_sysctl_table
1516 * Unregisters the sysctl table and all children. proc entries may not
1517 * actually be removed until they are no longer used by anyone.
1519 void unregister_sysctl_table(struct ctl_table_header * header)
1526 spin_lock(&sysctl_lock);
1527 drop_sysctl_table(header);
1528 spin_unlock(&sysctl_lock);
1530 EXPORT_SYMBOL(unregister_sysctl_table);
1532 void setup_sysctl_set(struct ctl_table_set *set,
1533 struct ctl_table_root *root,
1534 int (*is_seen)(struct ctl_table_set *))
1536 memset(set, 0, sizeof(*set));
1537 set->is_seen = is_seen;
1538 init_header(&set->dir.header, root, set, NULL, root_table);
1541 void retire_sysctl_set(struct ctl_table_set *set)
1543 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1546 int __init proc_sys_init(void)
1548 struct proc_dir_entry *proc_sys_root;
1550 proc_sys_root = proc_mkdir("sys", NULL);
1551 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1552 proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
1553 proc_sys_root->nlink = 0;
1555 return sysctl_init_bases();
1558 struct sysctl_alias {
1559 const char *kernel_param;
1560 const char *sysctl_param;
1564 * Historically some settings had both sysctl and a command line parameter.
1565 * With the generic sysctl. parameter support, we can handle them at a single
1566 * place and only keep the historical name for compatibility. This is not meant
1567 * to add brand new aliases. When adding existing aliases, consider whether
1568 * the possibly different moment of changing the value (e.g. from early_param
1569 * to the moment do_sysctl_args() is called) is an issue for the specific
1572 static const struct sysctl_alias sysctl_aliases[] = {
1573 {"hardlockup_all_cpu_backtrace", "kernel.hardlockup_all_cpu_backtrace" },
1574 {"hung_task_panic", "kernel.hung_task_panic" },
1575 {"numa_zonelist_order", "vm.numa_zonelist_order" },
1576 {"softlockup_all_cpu_backtrace", "kernel.softlockup_all_cpu_backtrace" },
1577 {"softlockup_panic", "kernel.softlockup_panic" },
1581 static const char *sysctl_find_alias(char *param)
1583 const struct sysctl_alias *alias;
1585 for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
1586 if (strcmp(alias->kernel_param, param) == 0)
1587 return alias->sysctl_param;
1593 /* Set sysctl value passed on kernel command line. */
1594 static int process_sysctl_arg(char *param, char *val,
1595 const char *unused, void *arg)
1598 struct vfsmount **proc_mnt = arg;
1599 struct file_system_type *proc_fs_type;
1606 if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
1607 param += sizeof("sysctl") - 1;
1609 if (param[0] != '/' && param[0] != '.')
1614 param = (char *) sysctl_find_alias(param);
1626 * To set sysctl options, we use a temporary mount of proc, look up the
1627 * respective sys/ file and write to it. To avoid mounting it when no
1628 * options were given, we mount it only when the first sysctl option is
1629 * found. Why not a persistent mount? There are problems with a
1630 * persistent mount of proc in that it forces userspace not to use any
1631 * proc mount options.
1634 proc_fs_type = get_fs_type("proc");
1635 if (!proc_fs_type) {
1636 pr_err("Failed to find procfs to set sysctl from command line\n");
1639 *proc_mnt = kern_mount(proc_fs_type);
1640 put_filesystem(proc_fs_type);
1641 if (IS_ERR(*proc_mnt)) {
1642 pr_err("Failed to mount procfs to set sysctl from command line\n");
1647 path = kasprintf(GFP_KERNEL, "sys/%s", param);
1649 panic("%s: Failed to allocate path for %s\n", __func__, param);
1650 strreplace(path, '.', '/');
1652 file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
1654 err = PTR_ERR(file);
1656 pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
1658 else if (err == -EACCES)
1659 pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
1662 pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
1666 wret = kernel_write(file, val, len, &pos);
1670 pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
1673 pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
1674 ERR_PTR(err), param, val);
1675 } else if (wret != len) {
1676 pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
1677 wret, len, path, param, val);
1680 err = filp_close(file, NULL);
1682 pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
1683 ERR_PTR(err), param, val);
1689 void do_sysctl_args(void)
1692 struct vfsmount *proc_mnt = NULL;
1694 command_line = kstrdup(saved_command_line, GFP_KERNEL);
1696 panic("%s: Failed to allocate copy of command line\n", __func__);
1698 parse_args("Setting sysctl args", command_line,
1699 NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
1702 kern_unmount(proc_mnt);
1704 kfree(command_line);