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/module.h>
16 #include <linux/bpf-cgroup.h>
17 #include <linux/kmemleak.h>
20 static const struct dentry_operations proc_sys_dentry_operations;
21 static const struct file_operations proc_sys_file_operations;
22 static const struct inode_operations proc_sys_inode_operations;
23 static const struct file_operations proc_sys_dir_file_operations;
24 static const struct inode_operations proc_sys_dir_operations;
26 /* shared constants to be used in various sysctls */
27 const int sysctl_vals[] = { 0, 1, INT_MAX };
28 EXPORT_SYMBOL(sysctl_vals);
30 /* Support for permanently empty directories */
32 struct ctl_table sysctl_mount_point[] = {
36 static bool is_empty_dir(struct ctl_table_header *head)
38 return head->ctl_table[0].child == sysctl_mount_point;
41 static void set_empty_dir(struct ctl_dir *dir)
43 dir->header.ctl_table[0].child = sysctl_mount_point;
46 static void clear_empty_dir(struct ctl_dir *dir)
49 dir->header.ctl_table[0].child = NULL;
52 void proc_sys_poll_notify(struct ctl_table_poll *poll)
57 atomic_inc(&poll->event);
58 wake_up_interruptible(&poll->wait);
61 static struct ctl_table root_table[] = {
64 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
68 static struct ctl_table_root sysctl_table_root = {
69 .default_set.dir.header = {
72 .ctl_table = root_table }},
73 .ctl_table_arg = root_table,
74 .root = &sysctl_table_root,
75 .set = &sysctl_table_root.default_set,
79 static DEFINE_SPINLOCK(sysctl_lock);
81 static void drop_sysctl_table(struct ctl_table_header *header);
82 static int sysctl_follow_link(struct ctl_table_header **phead,
83 struct ctl_table **pentry);
84 static int insert_links(struct ctl_table_header *head);
85 static void put_links(struct ctl_table_header *header);
87 static void sysctl_print_dir(struct ctl_dir *dir)
89 if (dir->header.parent)
90 sysctl_print_dir(dir->header.parent);
91 pr_cont("%s/", dir->header.ctl_table[0].procname);
94 static int namecmp(const char *name1, int len1, const char *name2, int len2)
103 cmp = memcmp(name1, name2, minlen);
109 /* Called under sysctl_lock */
110 static struct ctl_table *find_entry(struct ctl_table_header **phead,
111 struct ctl_dir *dir, const char *name, int namelen)
113 struct ctl_table_header *head;
114 struct ctl_table *entry;
115 struct rb_node *node = dir->root.rb_node;
119 struct ctl_node *ctl_node;
120 const char *procname;
123 ctl_node = rb_entry(node, struct ctl_node, node);
124 head = ctl_node->header;
125 entry = &head->ctl_table[ctl_node - head->node];
126 procname = entry->procname;
128 cmp = namecmp(name, namelen, procname, strlen(procname));
130 node = node->rb_left;
132 node = node->rb_right;
141 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
143 struct rb_node *node = &head->node[entry - head->ctl_table].node;
144 struct rb_node **p = &head->parent->root.rb_node;
145 struct rb_node *parent = NULL;
146 const char *name = entry->procname;
147 int namelen = strlen(name);
150 struct ctl_table_header *parent_head;
151 struct ctl_table *parent_entry;
152 struct ctl_node *parent_node;
153 const char *parent_name;
157 parent_node = rb_entry(parent, struct ctl_node, node);
158 parent_head = parent_node->header;
159 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
160 parent_name = parent_entry->procname;
162 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
168 pr_err("sysctl duplicate entry: ");
169 sysctl_print_dir(head->parent);
170 pr_cont("/%s\n", entry->procname);
175 rb_link_node(node, parent, p);
176 rb_insert_color(node, &head->parent->root);
180 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
182 struct rb_node *node = &head->node[entry - head->ctl_table].node;
184 rb_erase(node, &head->parent->root);
187 static void init_header(struct ctl_table_header *head,
188 struct ctl_table_root *root, struct ctl_table_set *set,
189 struct ctl_node *node, struct ctl_table *table)
191 head->ctl_table = table;
192 head->ctl_table_arg = table;
196 head->unregistering = NULL;
201 INIT_HLIST_HEAD(&head->inodes);
203 struct ctl_table *entry;
204 for (entry = table; entry->procname; entry++, node++)
209 static void erase_header(struct ctl_table_header *head)
211 struct ctl_table *entry;
212 for (entry = head->ctl_table; entry->procname; entry++)
213 erase_entry(head, entry);
216 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
218 struct ctl_table *entry;
221 /* Is this a permanently empty directory? */
222 if (is_empty_dir(&dir->header))
225 /* Am I creating a permanently empty directory? */
226 if (header->ctl_table == sysctl_mount_point) {
227 if (!RB_EMPTY_ROOT(&dir->root))
233 header->parent = dir;
234 err = insert_links(header);
237 for (entry = header->ctl_table; entry->procname; entry++) {
238 err = insert_entry(header, entry);
244 erase_header(header);
247 if (header->ctl_table == sysctl_mount_point)
248 clear_empty_dir(dir);
249 header->parent = NULL;
250 drop_sysctl_table(&dir->header);
254 /* called under sysctl_lock */
255 static int use_table(struct ctl_table_header *p)
257 if (unlikely(p->unregistering))
263 /* called under sysctl_lock */
264 static void unuse_table(struct ctl_table_header *p)
267 if (unlikely(p->unregistering))
268 complete(p->unregistering);
271 static void proc_sys_prune_dcache(struct ctl_table_header *head)
274 struct proc_inode *ei;
275 struct hlist_node *node;
276 struct super_block *sb;
280 node = hlist_first_rcu(&head->inodes);
283 ei = hlist_entry(node, struct proc_inode, sysctl_inodes);
284 spin_lock(&sysctl_lock);
285 hlist_del_init_rcu(&ei->sysctl_inodes);
286 spin_unlock(&sysctl_lock);
288 inode = &ei->vfs_inode;
290 if (!atomic_inc_not_zero(&sb->s_active))
292 inode = igrab(inode);
294 if (unlikely(!inode)) {
295 deactivate_super(sb);
300 d_prune_aliases(inode);
302 deactivate_super(sb);
309 /* called under sysctl_lock, will reacquire if has to wait */
310 static void start_unregistering(struct ctl_table_header *p)
313 * if p->used is 0, nobody will ever touch that entry again;
314 * we'll eliminate all paths to it before dropping sysctl_lock
316 if (unlikely(p->used)) {
317 struct completion wait;
318 init_completion(&wait);
319 p->unregistering = &wait;
320 spin_unlock(&sysctl_lock);
321 wait_for_completion(&wait);
323 /* anything non-NULL; we'll never dereference it */
324 p->unregistering = ERR_PTR(-EINVAL);
325 spin_unlock(&sysctl_lock);
328 * Prune dentries for unregistered sysctls: namespaced sysctls
329 * can have duplicate names and contaminate dcache very badly.
331 proc_sys_prune_dcache(p);
333 * do not remove from the list until nobody holds it; walking the
334 * list in do_sysctl() relies on that.
336 spin_lock(&sysctl_lock);
340 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
343 spin_lock(&sysctl_lock);
344 if (!use_table(head))
345 head = ERR_PTR(-ENOENT);
346 spin_unlock(&sysctl_lock);
350 static void sysctl_head_finish(struct ctl_table_header *head)
354 spin_lock(&sysctl_lock);
356 spin_unlock(&sysctl_lock);
359 static struct ctl_table_set *
360 lookup_header_set(struct ctl_table_root *root)
362 struct ctl_table_set *set = &root->default_set;
364 set = root->lookup(root);
368 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
370 const char *name, int namelen)
372 struct ctl_table_header *head;
373 struct ctl_table *entry;
375 spin_lock(&sysctl_lock);
376 entry = find_entry(&head, dir, name, namelen);
377 if (entry && use_table(head))
381 spin_unlock(&sysctl_lock);
385 static struct ctl_node *first_usable_entry(struct rb_node *node)
387 struct ctl_node *ctl_node;
389 for (;node; node = rb_next(node)) {
390 ctl_node = rb_entry(node, struct ctl_node, node);
391 if (use_table(ctl_node->header))
397 static void first_entry(struct ctl_dir *dir,
398 struct ctl_table_header **phead, struct ctl_table **pentry)
400 struct ctl_table_header *head = NULL;
401 struct ctl_table *entry = NULL;
402 struct ctl_node *ctl_node;
404 spin_lock(&sysctl_lock);
405 ctl_node = first_usable_entry(rb_first(&dir->root));
406 spin_unlock(&sysctl_lock);
408 head = ctl_node->header;
409 entry = &head->ctl_table[ctl_node - head->node];
415 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
417 struct ctl_table_header *head = *phead;
418 struct ctl_table *entry = *pentry;
419 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
421 spin_lock(&sysctl_lock);
424 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
425 spin_unlock(&sysctl_lock);
428 head = ctl_node->header;
429 entry = &head->ctl_table[ctl_node - head->node];
436 * sysctl_perm does NOT grant the superuser all rights automatically, because
437 * some sysctl variables are readonly even to root.
440 static int test_perm(int mode, int op)
442 if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
444 else if (in_egroup_p(GLOBAL_ROOT_GID))
446 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
451 static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
453 struct ctl_table_root *root = head->root;
456 if (root->permissions)
457 mode = root->permissions(head, table);
461 return test_perm(mode, op);
464 static struct inode *proc_sys_make_inode(struct super_block *sb,
465 struct ctl_table_header *head, struct ctl_table *table)
467 struct ctl_table_root *root = head->root;
469 struct proc_inode *ei;
471 inode = new_inode(sb);
473 return ERR_PTR(-ENOMEM);
475 inode->i_ino = get_next_ino();
479 spin_lock(&sysctl_lock);
480 if (unlikely(head->unregistering)) {
481 spin_unlock(&sysctl_lock);
483 return ERR_PTR(-ENOENT);
486 ei->sysctl_entry = table;
487 hlist_add_head_rcu(&ei->sysctl_inodes, &head->inodes);
489 spin_unlock(&sysctl_lock);
491 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
492 inode->i_mode = table->mode;
493 if (!S_ISDIR(table->mode)) {
494 inode->i_mode |= S_IFREG;
495 inode->i_op = &proc_sys_inode_operations;
496 inode->i_fop = &proc_sys_file_operations;
498 inode->i_mode |= S_IFDIR;
499 inode->i_op = &proc_sys_dir_operations;
500 inode->i_fop = &proc_sys_dir_file_operations;
501 if (is_empty_dir(head))
502 make_empty_dir_inode(inode);
505 if (root->set_ownership)
506 root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
508 inode->i_uid = GLOBAL_ROOT_UID;
509 inode->i_gid = GLOBAL_ROOT_GID;
515 void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
517 spin_lock(&sysctl_lock);
518 hlist_del_init_rcu(&PROC_I(inode)->sysctl_inodes);
520 kfree_rcu(head, rcu);
521 spin_unlock(&sysctl_lock);
524 static struct ctl_table_header *grab_header(struct inode *inode)
526 struct ctl_table_header *head = PROC_I(inode)->sysctl;
528 head = &sysctl_table_root.default_set.dir.header;
529 return sysctl_head_grab(head);
532 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
535 struct ctl_table_header *head = grab_header(dir);
536 struct ctl_table_header *h = NULL;
537 const struct qstr *name = &dentry->d_name;
540 struct dentry *err = ERR_PTR(-ENOENT);
541 struct ctl_dir *ctl_dir;
545 return ERR_CAST(head);
547 ctl_dir = container_of(head, struct ctl_dir, header);
549 p = lookup_entry(&h, ctl_dir, name->name, name->len);
553 if (S_ISLNK(p->mode)) {
554 ret = sysctl_follow_link(&h, &p);
560 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
562 err = ERR_CAST(inode);
566 d_set_d_op(dentry, &proc_sys_dentry_operations);
567 err = d_splice_alias(inode, dentry);
571 sysctl_head_finish(h);
572 sysctl_head_finish(head);
576 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
577 size_t count, loff_t *ppos, int write)
579 struct inode *inode = file_inode(filp);
580 struct ctl_table_header *head = grab_header(inode);
581 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
582 void *new_buf = NULL;
586 return PTR_ERR(head);
589 * At this point we know that the sysctl was not unregistered
590 * and won't be until we finish.
593 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
596 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
598 if (!table->proc_handler)
601 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, buf, &count,
606 /* careful: calling conventions are nasty here */
612 error = table->proc_handler(table, write, (void __user *)new_buf,
617 error = table->proc_handler(table, write, buf, &count, ppos);
623 sysctl_head_finish(head);
628 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
629 size_t count, loff_t *ppos)
631 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
634 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
635 size_t count, loff_t *ppos)
637 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
640 static int proc_sys_open(struct inode *inode, struct file *filp)
642 struct ctl_table_header *head = grab_header(inode);
643 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
645 /* sysctl was unregistered */
647 return PTR_ERR(head);
650 filp->private_data = proc_sys_poll_event(table->poll);
652 sysctl_head_finish(head);
657 static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
659 struct inode *inode = file_inode(filp);
660 struct ctl_table_header *head = grab_header(inode);
661 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
662 __poll_t ret = DEFAULT_POLLMASK;
665 /* sysctl was unregistered */
667 return EPOLLERR | EPOLLHUP;
669 if (!table->proc_handler)
675 event = (unsigned long)filp->private_data;
676 poll_wait(filp, &table->poll->wait, wait);
678 if (event != atomic_read(&table->poll->event)) {
679 filp->private_data = proc_sys_poll_event(table->poll);
680 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
684 sysctl_head_finish(head);
689 static bool proc_sys_fill_cache(struct file *file,
690 struct dir_context *ctx,
691 struct ctl_table_header *head,
692 struct ctl_table *table)
694 struct dentry *child, *dir = file->f_path.dentry;
698 unsigned type = DT_UNKNOWN;
700 qname.name = table->procname;
701 qname.len = strlen(table->procname);
702 qname.hash = full_name_hash(dir, qname.name, qname.len);
704 child = d_lookup(dir, &qname);
706 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
707 child = d_alloc_parallel(dir, &qname, &wq);
710 if (d_in_lookup(child)) {
712 inode = proc_sys_make_inode(dir->d_sb, head, table);
714 d_lookup_done(child);
718 d_set_d_op(child, &proc_sys_dentry_operations);
719 res = d_splice_alias(inode, child);
720 d_lookup_done(child);
731 inode = d_inode(child);
733 type = inode->i_mode >> 12;
735 return dir_emit(ctx, qname.name, qname.len, ino, type);
738 static bool proc_sys_link_fill_cache(struct file *file,
739 struct dir_context *ctx,
740 struct ctl_table_header *head,
741 struct ctl_table *table)
745 head = sysctl_head_grab(head);
749 /* It is not an error if we can not follow the link ignore it */
750 if (sysctl_follow_link(&head, &table))
753 ret = proc_sys_fill_cache(file, ctx, head, table);
755 sysctl_head_finish(head);
759 static int scan(struct ctl_table_header *head, struct ctl_table *table,
760 unsigned long *pos, struct file *file,
761 struct dir_context *ctx)
765 if ((*pos)++ < ctx->pos)
768 if (unlikely(S_ISLNK(table->mode)))
769 res = proc_sys_link_fill_cache(file, ctx, head, table);
771 res = proc_sys_fill_cache(file, ctx, head, table);
779 static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
781 struct ctl_table_header *head = grab_header(file_inode(file));
782 struct ctl_table_header *h = NULL;
783 struct ctl_table *entry;
784 struct ctl_dir *ctl_dir;
788 return PTR_ERR(head);
790 ctl_dir = container_of(head, struct ctl_dir, header);
792 if (!dir_emit_dots(file, ctx))
797 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
798 if (!scan(h, entry, &pos, file, ctx)) {
799 sysctl_head_finish(h);
804 sysctl_head_finish(head);
808 static int proc_sys_permission(struct inode *inode, int mask)
811 * sysctl entries that are not writeable,
812 * are _NOT_ writeable, capabilities or not.
814 struct ctl_table_header *head;
815 struct ctl_table *table;
818 /* Executable files are not allowed under /proc/sys/ */
819 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
822 head = grab_header(inode);
824 return PTR_ERR(head);
826 table = PROC_I(inode)->sysctl_entry;
827 if (!table) /* global root - r-xr-xr-x */
828 error = mask & MAY_WRITE ? -EACCES : 0;
829 else /* Use the permissions on the sysctl table entry */
830 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
832 sysctl_head_finish(head);
836 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
838 struct inode *inode = d_inode(dentry);
841 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
844 error = setattr_prepare(dentry, attr);
848 setattr_copy(inode, attr);
849 mark_inode_dirty(inode);
853 static int proc_sys_getattr(const struct path *path, struct kstat *stat,
854 u32 request_mask, unsigned int query_flags)
856 struct inode *inode = d_inode(path->dentry);
857 struct ctl_table_header *head = grab_header(inode);
858 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
861 return PTR_ERR(head);
863 generic_fillattr(inode, stat);
865 stat->mode = (stat->mode & S_IFMT) | table->mode;
867 sysctl_head_finish(head);
871 static const struct file_operations proc_sys_file_operations = {
872 .open = proc_sys_open,
873 .poll = proc_sys_poll,
874 .read = proc_sys_read,
875 .write = proc_sys_write,
876 .llseek = default_llseek,
879 static const struct file_operations proc_sys_dir_file_operations = {
880 .read = generic_read_dir,
881 .iterate_shared = proc_sys_readdir,
882 .llseek = generic_file_llseek,
885 static const struct inode_operations proc_sys_inode_operations = {
886 .permission = proc_sys_permission,
887 .setattr = proc_sys_setattr,
888 .getattr = proc_sys_getattr,
891 static const struct inode_operations proc_sys_dir_operations = {
892 .lookup = proc_sys_lookup,
893 .permission = proc_sys_permission,
894 .setattr = proc_sys_setattr,
895 .getattr = proc_sys_getattr,
898 static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
900 if (flags & LOOKUP_RCU)
902 return !PROC_I(d_inode(dentry))->sysctl->unregistering;
905 static int proc_sys_delete(const struct dentry *dentry)
907 return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
910 static int sysctl_is_seen(struct ctl_table_header *p)
912 struct ctl_table_set *set = p->set;
914 spin_lock(&sysctl_lock);
915 if (p->unregistering)
917 else if (!set->is_seen)
920 res = set->is_seen(set);
921 spin_unlock(&sysctl_lock);
925 static int proc_sys_compare(const struct dentry *dentry,
926 unsigned int len, const char *str, const struct qstr *name)
928 struct ctl_table_header *head;
931 /* Although proc doesn't have negative dentries, rcu-walk means
932 * that inode here can be NULL */
933 /* AV: can it, indeed? */
934 inode = d_inode_rcu(dentry);
937 if (name->len != len)
939 if (memcmp(name->name, str, len))
941 head = rcu_dereference(PROC_I(inode)->sysctl);
942 return !head || !sysctl_is_seen(head);
945 static const struct dentry_operations proc_sys_dentry_operations = {
946 .d_revalidate = proc_sys_revalidate,
947 .d_delete = proc_sys_delete,
948 .d_compare = proc_sys_compare,
951 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
952 const char *name, int namelen)
954 struct ctl_table_header *head;
955 struct ctl_table *entry;
957 entry = find_entry(&head, dir, name, namelen);
959 return ERR_PTR(-ENOENT);
960 if (!S_ISDIR(entry->mode))
961 return ERR_PTR(-ENOTDIR);
962 return container_of(head, struct ctl_dir, header);
965 static struct ctl_dir *new_dir(struct ctl_table_set *set,
966 const char *name, int namelen)
968 struct ctl_table *table;
970 struct ctl_node *node;
973 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
974 sizeof(struct ctl_table)*2 + namelen + 1,
979 node = (struct ctl_node *)(new + 1);
980 table = (struct ctl_table *)(node + 1);
981 new_name = (char *)(table + 2);
982 memcpy(new_name, name, namelen);
983 new_name[namelen] = '\0';
984 table[0].procname = new_name;
985 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
986 init_header(&new->header, set->dir.header.root, set, node, table);
992 * get_subdir - find or create a subdir with the specified name.
993 * @dir: Directory to create the subdirectory in
994 * @name: The name of the subdirectory to find or create
995 * @namelen: The length of name
997 * Takes a directory with an elevated reference count so we know that
998 * if we drop the lock the directory will not go away. Upon success
999 * the reference is moved from @dir to the returned subdirectory.
1000 * Upon error an error code is returned and the reference on @dir is
1003 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
1004 const char *name, int namelen)
1006 struct ctl_table_set *set = dir->header.set;
1007 struct ctl_dir *subdir, *new = NULL;
1010 spin_lock(&sysctl_lock);
1011 subdir = find_subdir(dir, name, namelen);
1012 if (!IS_ERR(subdir))
1014 if (PTR_ERR(subdir) != -ENOENT)
1017 spin_unlock(&sysctl_lock);
1018 new = new_dir(set, name, namelen);
1019 spin_lock(&sysctl_lock);
1020 subdir = ERR_PTR(-ENOMEM);
1024 /* Was the subdir added while we dropped the lock? */
1025 subdir = find_subdir(dir, name, namelen);
1026 if (!IS_ERR(subdir))
1028 if (PTR_ERR(subdir) != -ENOENT)
1031 /* Nope. Use the our freshly made directory entry. */
1032 err = insert_header(dir, &new->header);
1033 subdir = ERR_PTR(err);
1038 subdir->header.nreg++;
1040 if (IS_ERR(subdir)) {
1041 pr_err("sysctl could not get directory: ");
1042 sysctl_print_dir(dir);
1043 pr_cont("/%*.*s %ld\n",
1044 namelen, namelen, name, PTR_ERR(subdir));
1046 drop_sysctl_table(&dir->header);
1048 drop_sysctl_table(&new->header);
1049 spin_unlock(&sysctl_lock);
1053 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1055 struct ctl_dir *parent;
1056 const char *procname;
1057 if (!dir->header.parent)
1059 parent = xlate_dir(set, dir->header.parent);
1062 procname = dir->header.ctl_table[0].procname;
1063 return find_subdir(parent, procname, strlen(procname));
1066 static int sysctl_follow_link(struct ctl_table_header **phead,
1067 struct ctl_table **pentry)
1069 struct ctl_table_header *head;
1070 struct ctl_table_root *root;
1071 struct ctl_table_set *set;
1072 struct ctl_table *entry;
1073 struct ctl_dir *dir;
1077 spin_lock(&sysctl_lock);
1078 root = (*pentry)->data;
1079 set = lookup_header_set(root);
1080 dir = xlate_dir(set, (*phead)->parent);
1084 const char *procname = (*pentry)->procname;
1086 entry = find_entry(&head, dir, procname, strlen(procname));
1088 if (entry && use_table(head)) {
1089 unuse_table(*phead);
1096 spin_unlock(&sysctl_lock);
1100 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1102 struct va_format vaf;
1105 va_start(args, fmt);
1109 pr_err("sysctl table check failed: %s/%s %pV\n",
1110 path, table->procname, &vaf);
1116 static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1120 if ((table->proc_handler == proc_douintvec) ||
1121 (table->proc_handler == proc_douintvec_minmax)) {
1122 if (table->maxlen != sizeof(unsigned int))
1123 err |= sysctl_err(path, table, "array not allowed");
1129 static int sysctl_check_table(const char *path, struct ctl_table *table)
1132 for (; table->procname; table++) {
1134 err |= sysctl_err(path, table, "Not a file");
1136 if ((table->proc_handler == proc_dostring) ||
1137 (table->proc_handler == proc_dointvec) ||
1138 (table->proc_handler == proc_douintvec) ||
1139 (table->proc_handler == proc_douintvec_minmax) ||
1140 (table->proc_handler == proc_dointvec_minmax) ||
1141 (table->proc_handler == proc_dointvec_jiffies) ||
1142 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1143 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1144 (table->proc_handler == proc_doulongvec_minmax) ||
1145 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1147 err |= sysctl_err(path, table, "No data");
1149 err |= sysctl_err(path, table, "No maxlen");
1151 err |= sysctl_check_table_array(path, table);
1153 if (!table->proc_handler)
1154 err |= sysctl_err(path, table, "No proc_handler");
1156 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1157 err |= sysctl_err(path, table, "bogus .mode 0%o",
1163 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1164 struct ctl_table_root *link_root)
1166 struct ctl_table *link_table, *entry, *link;
1167 struct ctl_table_header *links;
1168 struct ctl_node *node;
1170 int nr_entries, name_bytes;
1174 for (entry = table; entry->procname; entry++) {
1176 name_bytes += strlen(entry->procname) + 1;
1179 links = kzalloc(sizeof(struct ctl_table_header) +
1180 sizeof(struct ctl_node)*nr_entries +
1181 sizeof(struct ctl_table)*(nr_entries + 1) +
1188 node = (struct ctl_node *)(links + 1);
1189 link_table = (struct ctl_table *)(node + nr_entries);
1190 link_name = (char *)&link_table[nr_entries + 1];
1192 for (link = link_table, entry = table; entry->procname; link++, entry++) {
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;
1200 init_header(links, dir->header.root, dir->header.set, node, link_table);
1201 links->nreg = nr_entries;
1206 static bool get_links(struct ctl_dir *dir,
1207 struct ctl_table *table, struct ctl_table_root *link_root)
1209 struct ctl_table_header *head;
1210 struct ctl_table *entry, *link;
1212 /* Are there links available for every entry in table? */
1213 for (entry = table; entry->procname; entry++) {
1214 const char *procname = entry->procname;
1215 link = find_entry(&head, dir, procname, strlen(procname));
1218 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1220 if (S_ISLNK(link->mode) && (link->data == link_root))
1225 /* The checks passed. Increase the registration count on the links */
1226 for (entry = table; entry->procname; entry++) {
1227 const char *procname = entry->procname;
1228 link = find_entry(&head, dir, procname, strlen(procname));
1234 static int insert_links(struct ctl_table_header *head)
1236 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1237 struct ctl_dir *core_parent = NULL;
1238 struct ctl_table_header *links;
1241 if (head->set == root_set)
1244 core_parent = xlate_dir(root_set, head->parent);
1245 if (IS_ERR(core_parent))
1248 if (get_links(core_parent, head->ctl_table, head->root))
1251 core_parent->header.nreg++;
1252 spin_unlock(&sysctl_lock);
1254 links = new_links(core_parent, head->ctl_table, head->root);
1256 spin_lock(&sysctl_lock);
1262 if (get_links(core_parent, head->ctl_table, head->root)) {
1267 err = insert_header(core_parent, links);
1271 drop_sysctl_table(&core_parent->header);
1276 * __register_sysctl_table - register a leaf sysctl table
1277 * @set: Sysctl tree to register on
1278 * @path: The path to the directory the sysctl table is in.
1279 * @table: the top-level table structure
1281 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1282 * array. A completely 0 filled entry terminates the table.
1284 * The members of the &struct ctl_table structure are used as follows:
1286 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1287 * enter a sysctl file
1289 * data - a pointer to data for use by proc_handler
1291 * maxlen - the maximum size in bytes of the data
1293 * mode - the file permissions for the /proc/sys file
1295 * child - must be %NULL.
1297 * proc_handler - the text handler routine (described below)
1299 * extra1, extra2 - extra pointers usable by the proc handler routines
1301 * Leaf nodes in the sysctl tree will be represented by a single file
1302 * under /proc; non-leaf nodes will be represented by directories.
1304 * There must be a proc_handler routine for any terminal nodes.
1305 * Several default handlers are available to cover common cases -
1307 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1308 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1309 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1311 * It is the handler's job to read the input buffer from user memory
1312 * and process it. The handler should return 0 on success.
1314 * This routine returns %NULL on a failure to register, and a pointer
1315 * to the table header on success.
1317 struct ctl_table_header *__register_sysctl_table(
1318 struct ctl_table_set *set,
1319 const char *path, struct ctl_table *table)
1321 struct ctl_table_root *root = set->dir.header.root;
1322 struct ctl_table_header *header;
1323 const char *name, *nextname;
1324 struct ctl_dir *dir;
1325 struct ctl_table *entry;
1326 struct ctl_node *node;
1329 for (entry = table; entry->procname; entry++)
1332 header = kzalloc(sizeof(struct ctl_table_header) +
1333 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
1337 node = (struct ctl_node *)(header + 1);
1338 init_header(header, root, set, node, table);
1339 if (sysctl_check_table(path, table))
1342 spin_lock(&sysctl_lock);
1344 /* Reference moved down the diretory tree get_subdir */
1346 spin_unlock(&sysctl_lock);
1348 /* Find the directory for the ctl_table */
1349 for (name = path; name; name = nextname) {
1351 nextname = strchr(name, '/');
1353 namelen = nextname - name;
1356 namelen = strlen(name);
1361 dir = get_subdir(dir, name, namelen);
1366 spin_lock(&sysctl_lock);
1367 if (insert_header(dir, header))
1368 goto fail_put_dir_locked;
1370 drop_sysctl_table(&dir->header);
1371 spin_unlock(&sysctl_lock);
1375 fail_put_dir_locked:
1376 drop_sysctl_table(&dir->header);
1377 spin_unlock(&sysctl_lock);
1385 * register_sysctl - register a sysctl table
1386 * @path: The path to the directory the sysctl table is in.
1387 * @table: the table structure
1389 * Register a sysctl table. @table should be a filled in ctl_table
1390 * array. A completely 0 filled entry terminates the table.
1392 * See __register_sysctl_table for more details.
1394 struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1396 return __register_sysctl_table(&sysctl_table_root.default_set,
1399 EXPORT_SYMBOL(register_sysctl);
1402 * __register_sysctl_init() - register sysctl table to path
1403 * @path: path name for sysctl base
1404 * @table: This is the sysctl table that needs to be registered to the path
1405 * @table_name: The name of sysctl table, only used for log printing when
1406 * registration fails
1408 * The sysctl interface is used by userspace to query or modify at runtime
1409 * a predefined value set on a variable. These variables however have default
1410 * values pre-set. Code which depends on these variables will always work even
1411 * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1412 * ability to query or modify the sysctls dynamically at run time. Chances of
1413 * register_sysctl() failing on init are extremely low, and so for both reasons
1414 * this function does not return any error as it is used by initialization code.
1416 * Context: Can only be called after your respective sysctl base path has been
1417 * registered. So for instance, most base directories are registered early on
1418 * init before init levels are processed through proc_sys_init() and
1421 void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1422 const char *table_name)
1424 struct ctl_table_header *hdr = register_sysctl(path, table);
1426 if (unlikely(!hdr)) {
1427 pr_err("failed when register_sysctl %s to %s\n", table_name, path);
1430 kmemleak_not_leak(hdr);
1433 static char *append_path(const char *path, char *pos, const char *name)
1436 namelen = strlen(name);
1437 if (((pos - path) + namelen + 2) >= PATH_MAX)
1439 memcpy(pos, name, namelen);
1441 pos[namelen + 1] = '\0';
1446 static int count_subheaders(struct ctl_table *table)
1449 int nr_subheaders = 0;
1450 struct ctl_table *entry;
1452 /* special case: no directory and empty directory */
1453 if (!table || !table->procname)
1456 for (entry = table; entry->procname; entry++) {
1458 nr_subheaders += count_subheaders(entry->child);
1462 return nr_subheaders + has_files;
1465 static int register_leaf_sysctl_tables(const char *path, char *pos,
1466 struct ctl_table_header ***subheader, struct ctl_table_set *set,
1467 struct ctl_table *table)
1469 struct ctl_table *ctl_table_arg = NULL;
1470 struct ctl_table *entry, *files;
1475 for (entry = table; entry->procname; entry++) {
1483 /* If there are mixed files and directories we need a new table */
1484 if (nr_dirs && nr_files) {
1485 struct ctl_table *new;
1486 files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
1491 ctl_table_arg = files;
1492 for (new = files, entry = table; entry->procname; entry++) {
1500 /* Register everything except a directory full of subdirectories */
1501 if (nr_files || !nr_dirs) {
1502 struct ctl_table_header *header;
1503 header = __register_sysctl_table(set, path, files);
1505 kfree(ctl_table_arg);
1509 /* Remember if we need to free the file table */
1510 header->ctl_table_arg = ctl_table_arg;
1511 **subheader = header;
1515 /* Recurse into the subdirectories. */
1516 for (entry = table; entry->procname; entry++) {
1522 err = -ENAMETOOLONG;
1523 child_pos = append_path(path, pos, entry->procname);
1527 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1535 /* On failure our caller will unregister all registered subheaders */
1540 * __register_sysctl_paths - register a sysctl table hierarchy
1541 * @set: Sysctl tree to register on
1542 * @path: The path to the directory the sysctl table is in.
1543 * @table: the top-level table structure
1545 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1546 * array. A completely 0 filled entry terminates the table.
1548 * See __register_sysctl_table for more details.
1550 struct ctl_table_header *__register_sysctl_paths(
1551 struct ctl_table_set *set,
1552 const struct ctl_path *path, struct ctl_table *table)
1554 struct ctl_table *ctl_table_arg = table;
1555 int nr_subheaders = count_subheaders(table);
1556 struct ctl_table_header *header = NULL, **subheaders, **subheader;
1557 const struct ctl_path *component;
1558 char *new_path, *pos;
1560 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1565 for (component = path; component->procname; component++) {
1566 pos = append_path(new_path, pos, component->procname);
1570 while (table->procname && table->child && !table[1].procname) {
1571 pos = append_path(new_path, pos, table->procname);
1574 table = table->child;
1576 if (nr_subheaders == 1) {
1577 header = __register_sysctl_table(set, new_path, table);
1579 header->ctl_table_arg = ctl_table_arg;
1581 header = kzalloc(sizeof(*header) +
1582 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1586 subheaders = (struct ctl_table_header **) (header + 1);
1587 subheader = subheaders;
1588 header->ctl_table_arg = ctl_table_arg;
1590 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1592 goto err_register_leaves;
1599 err_register_leaves:
1600 while (subheader > subheaders) {
1601 struct ctl_table_header *subh = *(--subheader);
1602 struct ctl_table *table = subh->ctl_table_arg;
1603 unregister_sysctl_table(subh);
1612 * register_sysctl_table_path - register a sysctl table hierarchy
1613 * @path: The path to the directory the sysctl table is in.
1614 * @table: the top-level table structure
1616 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1617 * array. A completely 0 filled entry terminates the table.
1619 * See __register_sysctl_paths for more details.
1621 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1622 struct ctl_table *table)
1624 return __register_sysctl_paths(&sysctl_table_root.default_set,
1627 EXPORT_SYMBOL(register_sysctl_paths);
1630 * register_sysctl_table - register a sysctl table hierarchy
1631 * @table: the top-level table structure
1633 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1634 * array. A completely 0 filled entry terminates the table.
1636 * See register_sysctl_paths for more details.
1638 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1640 static const struct ctl_path null_path[] = { {} };
1642 return register_sysctl_paths(null_path, table);
1644 EXPORT_SYMBOL(register_sysctl_table);
1646 static void put_links(struct ctl_table_header *header)
1648 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1649 struct ctl_table_root *root = header->root;
1650 struct ctl_dir *parent = header->parent;
1651 struct ctl_dir *core_parent;
1652 struct ctl_table *entry;
1654 if (header->set == root_set)
1657 core_parent = xlate_dir(root_set, parent);
1658 if (IS_ERR(core_parent))
1661 for (entry = header->ctl_table; entry->procname; entry++) {
1662 struct ctl_table_header *link_head;
1663 struct ctl_table *link;
1664 const char *name = entry->procname;
1666 link = find_entry(&link_head, core_parent, name, strlen(name));
1668 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1669 (S_ISLNK(link->mode) && (link->data == root)))) {
1670 drop_sysctl_table(link_head);
1673 pr_err("sysctl link missing during unregister: ");
1674 sysctl_print_dir(parent);
1675 pr_cont("/%s\n", name);
1680 static void drop_sysctl_table(struct ctl_table_header *header)
1682 struct ctl_dir *parent = header->parent;
1689 start_unregistering(header);
1692 if (!--header->count)
1693 kfree_rcu(header, rcu);
1696 drop_sysctl_table(&parent->header);
1700 * unregister_sysctl_table - unregister a sysctl table hierarchy
1701 * @header: the header returned from register_sysctl_table
1703 * Unregisters the sysctl table and all children. proc entries may not
1704 * actually be removed until they are no longer used by anyone.
1706 void unregister_sysctl_table(struct ctl_table_header * header)
1714 nr_subheaders = count_subheaders(header->ctl_table_arg);
1715 if (unlikely(nr_subheaders > 1)) {
1716 struct ctl_table_header **subheaders;
1719 subheaders = (struct ctl_table_header **)(header + 1);
1720 for (i = nr_subheaders -1; i >= 0; i--) {
1721 struct ctl_table_header *subh = subheaders[i];
1722 struct ctl_table *table = subh->ctl_table_arg;
1723 unregister_sysctl_table(subh);
1730 spin_lock(&sysctl_lock);
1731 drop_sysctl_table(header);
1732 spin_unlock(&sysctl_lock);
1734 EXPORT_SYMBOL(unregister_sysctl_table);
1736 void setup_sysctl_set(struct ctl_table_set *set,
1737 struct ctl_table_root *root,
1738 int (*is_seen)(struct ctl_table_set *))
1740 memset(set, 0, sizeof(*set));
1741 set->is_seen = is_seen;
1742 init_header(&set->dir.header, root, set, NULL, root_table);
1745 void retire_sysctl_set(struct ctl_table_set *set)
1747 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1750 int __init proc_sys_init(void)
1752 struct proc_dir_entry *proc_sys_root;
1754 proc_sys_root = proc_mkdir("sys", NULL);
1755 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1756 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
1757 proc_sys_root->nlink = 0;
1759 return sysctl_init();