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>
19 static const struct dentry_operations proc_sys_dentry_operations;
20 static const struct file_operations proc_sys_file_operations;
21 static const struct inode_operations proc_sys_inode_operations;
22 static const struct file_operations proc_sys_dir_file_operations;
23 static const struct inode_operations proc_sys_dir_operations;
25 /* shared constants to be used in various sysctls */
26 const int sysctl_vals[] = { 0, 1, INT_MAX };
27 EXPORT_SYMBOL(sysctl_vals);
29 /* Support for permanently empty directories */
31 struct ctl_table sysctl_mount_point[] = {
35 static bool is_empty_dir(struct ctl_table_header *head)
37 return head->ctl_table[0].child == sysctl_mount_point;
40 static void set_empty_dir(struct ctl_dir *dir)
42 dir->header.ctl_table[0].child = sysctl_mount_point;
45 static void clear_empty_dir(struct ctl_dir *dir)
48 dir->header.ctl_table[0].child = NULL;
51 void proc_sys_poll_notify(struct ctl_table_poll *poll)
56 atomic_inc(&poll->event);
57 wake_up_interruptible(&poll->wait);
60 static struct ctl_table root_table[] = {
63 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
67 static struct ctl_table_root sysctl_table_root = {
68 .default_set.dir.header = {
71 .ctl_table = root_table }},
72 .ctl_table_arg = root_table,
73 .root = &sysctl_table_root,
74 .set = &sysctl_table_root.default_set,
78 static DEFINE_SPINLOCK(sysctl_lock);
80 static void drop_sysctl_table(struct ctl_table_header *header);
81 static int sysctl_follow_link(struct ctl_table_header **phead,
82 struct ctl_table **pentry);
83 static int insert_links(struct ctl_table_header *head);
84 static void put_links(struct ctl_table_header *header);
86 static void sysctl_print_dir(struct ctl_dir *dir)
88 if (dir->header.parent)
89 sysctl_print_dir(dir->header.parent);
90 pr_cont("%s/", dir->header.ctl_table[0].procname);
93 static int namecmp(const char *name1, int len1, const char *name2, int len2)
102 cmp = memcmp(name1, name2, minlen);
108 /* Called under sysctl_lock */
109 static struct ctl_table *find_entry(struct ctl_table_header **phead,
110 struct ctl_dir *dir, const char *name, int namelen)
112 struct ctl_table_header *head;
113 struct ctl_table *entry;
114 struct rb_node *node = dir->root.rb_node;
118 struct ctl_node *ctl_node;
119 const char *procname;
122 ctl_node = rb_entry(node, struct ctl_node, node);
123 head = ctl_node->header;
124 entry = &head->ctl_table[ctl_node - head->node];
125 procname = entry->procname;
127 cmp = namecmp(name, namelen, procname, strlen(procname));
129 node = node->rb_left;
131 node = node->rb_right;
140 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
142 struct rb_node *node = &head->node[entry - head->ctl_table].node;
143 struct rb_node **p = &head->parent->root.rb_node;
144 struct rb_node *parent = NULL;
145 const char *name = entry->procname;
146 int namelen = strlen(name);
149 struct ctl_table_header *parent_head;
150 struct ctl_table *parent_entry;
151 struct ctl_node *parent_node;
152 const char *parent_name;
156 parent_node = rb_entry(parent, struct ctl_node, node);
157 parent_head = parent_node->header;
158 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
159 parent_name = parent_entry->procname;
161 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
167 pr_err("sysctl duplicate entry: ");
168 sysctl_print_dir(head->parent);
169 pr_cont("/%s\n", entry->procname);
174 rb_link_node(node, parent, p);
175 rb_insert_color(node, &head->parent->root);
179 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
181 struct rb_node *node = &head->node[entry - head->ctl_table].node;
183 rb_erase(node, &head->parent->root);
186 static void init_header(struct ctl_table_header *head,
187 struct ctl_table_root *root, struct ctl_table_set *set,
188 struct ctl_node *node, struct ctl_table *table)
190 head->ctl_table = table;
191 head->ctl_table_arg = table;
195 head->unregistering = NULL;
200 INIT_HLIST_HEAD(&head->inodes);
202 struct ctl_table *entry;
203 for (entry = table; entry->procname; entry++, node++)
208 static void erase_header(struct ctl_table_header *head)
210 struct ctl_table *entry;
211 for (entry = head->ctl_table; entry->procname; entry++)
212 erase_entry(head, entry);
215 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
217 struct ctl_table *entry;
220 /* Is this a permanently empty directory? */
221 if (is_empty_dir(&dir->header))
224 /* Am I creating a permanently empty directory? */
225 if (header->ctl_table == sysctl_mount_point) {
226 if (!RB_EMPTY_ROOT(&dir->root))
232 header->parent = dir;
233 err = insert_links(header);
236 for (entry = header->ctl_table; entry->procname; entry++) {
237 err = insert_entry(header, entry);
243 erase_header(header);
246 if (header->ctl_table == sysctl_mount_point)
247 clear_empty_dir(dir);
248 header->parent = NULL;
249 drop_sysctl_table(&dir->header);
253 /* called under sysctl_lock */
254 static int use_table(struct ctl_table_header *p)
256 if (unlikely(p->unregistering))
262 /* called under sysctl_lock */
263 static void unuse_table(struct ctl_table_header *p)
266 if (unlikely(p->unregistering))
267 complete(p->unregistering);
270 static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
272 proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
275 /* called under sysctl_lock, will reacquire if has to wait */
276 static void start_unregistering(struct ctl_table_header *p)
279 * if p->used is 0, nobody will ever touch that entry again;
280 * we'll eliminate all paths to it before dropping sysctl_lock
282 if (unlikely(p->used)) {
283 struct completion wait;
284 init_completion(&wait);
285 p->unregistering = &wait;
286 spin_unlock(&sysctl_lock);
287 wait_for_completion(&wait);
289 /* anything non-NULL; we'll never dereference it */
290 p->unregistering = ERR_PTR(-EINVAL);
291 spin_unlock(&sysctl_lock);
294 * Invalidate dentries for unregistered sysctls: namespaced sysctls
295 * can have duplicate names and contaminate dcache very badly.
297 proc_sys_invalidate_dcache(p);
299 * do not remove from the list until nobody holds it; walking the
300 * list in do_sysctl() relies on that.
302 spin_lock(&sysctl_lock);
306 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
309 spin_lock(&sysctl_lock);
310 if (!use_table(head))
311 head = ERR_PTR(-ENOENT);
312 spin_unlock(&sysctl_lock);
316 static void sysctl_head_finish(struct ctl_table_header *head)
320 spin_lock(&sysctl_lock);
322 spin_unlock(&sysctl_lock);
325 static struct ctl_table_set *
326 lookup_header_set(struct ctl_table_root *root)
328 struct ctl_table_set *set = &root->default_set;
330 set = root->lookup(root);
334 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
336 const char *name, int namelen)
338 struct ctl_table_header *head;
339 struct ctl_table *entry;
341 spin_lock(&sysctl_lock);
342 entry = find_entry(&head, dir, name, namelen);
343 if (entry && use_table(head))
347 spin_unlock(&sysctl_lock);
351 static struct ctl_node *first_usable_entry(struct rb_node *node)
353 struct ctl_node *ctl_node;
355 for (;node; node = rb_next(node)) {
356 ctl_node = rb_entry(node, struct ctl_node, node);
357 if (use_table(ctl_node->header))
363 static void first_entry(struct ctl_dir *dir,
364 struct ctl_table_header **phead, struct ctl_table **pentry)
366 struct ctl_table_header *head = NULL;
367 struct ctl_table *entry = NULL;
368 struct ctl_node *ctl_node;
370 spin_lock(&sysctl_lock);
371 ctl_node = first_usable_entry(rb_first(&dir->root));
372 spin_unlock(&sysctl_lock);
374 head = ctl_node->header;
375 entry = &head->ctl_table[ctl_node - head->node];
381 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
383 struct ctl_table_header *head = *phead;
384 struct ctl_table *entry = *pentry;
385 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
387 spin_lock(&sysctl_lock);
390 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
391 spin_unlock(&sysctl_lock);
394 head = ctl_node->header;
395 entry = &head->ctl_table[ctl_node - head->node];
402 * sysctl_perm does NOT grant the superuser all rights automatically, because
403 * some sysctl variables are readonly even to root.
406 static int test_perm(int mode, int op)
408 if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
410 else if (in_egroup_p(GLOBAL_ROOT_GID))
412 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
417 static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
419 struct ctl_table_root *root = head->root;
422 if (root->permissions)
423 mode = root->permissions(head, table);
427 return test_perm(mode, op);
430 static struct inode *proc_sys_make_inode(struct super_block *sb,
431 struct ctl_table_header *head, struct ctl_table *table)
433 struct ctl_table_root *root = head->root;
435 struct proc_inode *ei;
437 inode = new_inode(sb);
439 return ERR_PTR(-ENOMEM);
441 inode->i_ino = get_next_ino();
445 spin_lock(&sysctl_lock);
446 if (unlikely(head->unregistering)) {
447 spin_unlock(&sysctl_lock);
449 return ERR_PTR(-ENOENT);
452 ei->sysctl_entry = table;
453 hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
455 spin_unlock(&sysctl_lock);
457 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
458 inode->i_mode = table->mode;
459 if (!S_ISDIR(table->mode)) {
460 inode->i_mode |= S_IFREG;
461 inode->i_op = &proc_sys_inode_operations;
462 inode->i_fop = &proc_sys_file_operations;
464 inode->i_mode |= S_IFDIR;
465 inode->i_op = &proc_sys_dir_operations;
466 inode->i_fop = &proc_sys_dir_file_operations;
467 if (is_empty_dir(head))
468 make_empty_dir_inode(inode);
471 if (root->set_ownership)
472 root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
474 inode->i_uid = GLOBAL_ROOT_UID;
475 inode->i_gid = GLOBAL_ROOT_GID;
481 void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
483 spin_lock(&sysctl_lock);
484 hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
486 kfree_rcu(head, rcu);
487 spin_unlock(&sysctl_lock);
490 static struct ctl_table_header *grab_header(struct inode *inode)
492 struct ctl_table_header *head = PROC_I(inode)->sysctl;
494 head = &sysctl_table_root.default_set.dir.header;
495 return sysctl_head_grab(head);
498 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
501 struct ctl_table_header *head = grab_header(dir);
502 struct ctl_table_header *h = NULL;
503 const struct qstr *name = &dentry->d_name;
506 struct dentry *err = ERR_PTR(-ENOENT);
507 struct ctl_dir *ctl_dir;
511 return ERR_CAST(head);
513 ctl_dir = container_of(head, struct ctl_dir, header);
515 p = lookup_entry(&h, ctl_dir, name->name, name->len);
519 if (S_ISLNK(p->mode)) {
520 ret = sysctl_follow_link(&h, &p);
526 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
528 err = ERR_CAST(inode);
532 d_set_d_op(dentry, &proc_sys_dentry_operations);
533 err = d_splice_alias(inode, dentry);
537 sysctl_head_finish(h);
538 sysctl_head_finish(head);
542 static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf,
543 size_t count, loff_t *ppos, int write)
545 struct inode *inode = file_inode(filp);
546 struct ctl_table_header *head = grab_header(inode);
547 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
552 return PTR_ERR(head);
555 * At this point we know that the sysctl was not unregistered
556 * and won't be until we finish.
559 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
562 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
564 if (!table->proc_handler)
568 kbuf = memdup_user_nul(ubuf, count);
570 error = PTR_ERR(kbuf);
575 kbuf = kzalloc(count, GFP_KERNEL);
580 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
585 /* careful: calling conventions are nasty here */
586 error = table->proc_handler(table, write, kbuf, &count, ppos);
592 if (copy_to_user(ubuf, kbuf, count))
600 sysctl_head_finish(head);
605 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
606 size_t count, loff_t *ppos)
608 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
611 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
612 size_t count, loff_t *ppos)
614 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
617 static int proc_sys_open(struct inode *inode, struct file *filp)
619 struct ctl_table_header *head = grab_header(inode);
620 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
622 /* sysctl was unregistered */
624 return PTR_ERR(head);
627 filp->private_data = proc_sys_poll_event(table->poll);
629 sysctl_head_finish(head);
634 static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
636 struct inode *inode = file_inode(filp);
637 struct ctl_table_header *head = grab_header(inode);
638 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
639 __poll_t ret = DEFAULT_POLLMASK;
642 /* sysctl was unregistered */
644 return EPOLLERR | EPOLLHUP;
646 if (!table->proc_handler)
652 event = (unsigned long)filp->private_data;
653 poll_wait(filp, &table->poll->wait, wait);
655 if (event != atomic_read(&table->poll->event)) {
656 filp->private_data = proc_sys_poll_event(table->poll);
657 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
661 sysctl_head_finish(head);
666 static bool proc_sys_fill_cache(struct file *file,
667 struct dir_context *ctx,
668 struct ctl_table_header *head,
669 struct ctl_table *table)
671 struct dentry *child, *dir = file->f_path.dentry;
675 unsigned type = DT_UNKNOWN;
677 qname.name = table->procname;
678 qname.len = strlen(table->procname);
679 qname.hash = full_name_hash(dir, qname.name, qname.len);
681 child = d_lookup(dir, &qname);
683 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
684 child = d_alloc_parallel(dir, &qname, &wq);
687 if (d_in_lookup(child)) {
689 inode = proc_sys_make_inode(dir->d_sb, head, table);
691 d_lookup_done(child);
695 d_set_d_op(child, &proc_sys_dentry_operations);
696 res = d_splice_alias(inode, child);
697 d_lookup_done(child);
708 inode = d_inode(child);
710 type = inode->i_mode >> 12;
712 return dir_emit(ctx, qname.name, qname.len, ino, type);
715 static bool proc_sys_link_fill_cache(struct file *file,
716 struct dir_context *ctx,
717 struct ctl_table_header *head,
718 struct ctl_table *table)
722 head = sysctl_head_grab(head);
726 /* It is not an error if we can not follow the link ignore it */
727 if (sysctl_follow_link(&head, &table))
730 ret = proc_sys_fill_cache(file, ctx, head, table);
732 sysctl_head_finish(head);
736 static int scan(struct ctl_table_header *head, struct ctl_table *table,
737 unsigned long *pos, struct file *file,
738 struct dir_context *ctx)
742 if ((*pos)++ < ctx->pos)
745 if (unlikely(S_ISLNK(table->mode)))
746 res = proc_sys_link_fill_cache(file, ctx, head, table);
748 res = proc_sys_fill_cache(file, ctx, head, table);
756 static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
758 struct ctl_table_header *head = grab_header(file_inode(file));
759 struct ctl_table_header *h = NULL;
760 struct ctl_table *entry;
761 struct ctl_dir *ctl_dir;
765 return PTR_ERR(head);
767 ctl_dir = container_of(head, struct ctl_dir, header);
769 if (!dir_emit_dots(file, ctx))
774 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
775 if (!scan(h, entry, &pos, file, ctx)) {
776 sysctl_head_finish(h);
781 sysctl_head_finish(head);
785 static int proc_sys_permission(struct inode *inode, int mask)
788 * sysctl entries that are not writeable,
789 * are _NOT_ writeable, capabilities or not.
791 struct ctl_table_header *head;
792 struct ctl_table *table;
795 /* Executable files are not allowed under /proc/sys/ */
796 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
799 head = grab_header(inode);
801 return PTR_ERR(head);
803 table = PROC_I(inode)->sysctl_entry;
804 if (!table) /* global root - r-xr-xr-x */
805 error = mask & MAY_WRITE ? -EACCES : 0;
806 else /* Use the permissions on the sysctl table entry */
807 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
809 sysctl_head_finish(head);
813 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
815 struct inode *inode = d_inode(dentry);
818 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
821 error = setattr_prepare(dentry, attr);
825 setattr_copy(inode, attr);
826 mark_inode_dirty(inode);
830 static int proc_sys_getattr(const struct path *path, struct kstat *stat,
831 u32 request_mask, unsigned int query_flags)
833 struct inode *inode = d_inode(path->dentry);
834 struct ctl_table_header *head = grab_header(inode);
835 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
838 return PTR_ERR(head);
840 generic_fillattr(inode, stat);
842 stat->mode = (stat->mode & S_IFMT) | table->mode;
844 sysctl_head_finish(head);
848 static const struct file_operations proc_sys_file_operations = {
849 .open = proc_sys_open,
850 .poll = proc_sys_poll,
851 .read = proc_sys_read,
852 .write = proc_sys_write,
853 .llseek = default_llseek,
856 static const struct file_operations proc_sys_dir_file_operations = {
857 .read = generic_read_dir,
858 .iterate_shared = proc_sys_readdir,
859 .llseek = generic_file_llseek,
862 static const struct inode_operations proc_sys_inode_operations = {
863 .permission = proc_sys_permission,
864 .setattr = proc_sys_setattr,
865 .getattr = proc_sys_getattr,
868 static const struct inode_operations proc_sys_dir_operations = {
869 .lookup = proc_sys_lookup,
870 .permission = proc_sys_permission,
871 .setattr = proc_sys_setattr,
872 .getattr = proc_sys_getattr,
875 static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
877 if (flags & LOOKUP_RCU)
879 return !PROC_I(d_inode(dentry))->sysctl->unregistering;
882 static int proc_sys_delete(const struct dentry *dentry)
884 return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
887 static int sysctl_is_seen(struct ctl_table_header *p)
889 struct ctl_table_set *set = p->set;
891 spin_lock(&sysctl_lock);
892 if (p->unregistering)
894 else if (!set->is_seen)
897 res = set->is_seen(set);
898 spin_unlock(&sysctl_lock);
902 static int proc_sys_compare(const struct dentry *dentry,
903 unsigned int len, const char *str, const struct qstr *name)
905 struct ctl_table_header *head;
908 /* Although proc doesn't have negative dentries, rcu-walk means
909 * that inode here can be NULL */
910 /* AV: can it, indeed? */
911 inode = d_inode_rcu(dentry);
914 if (name->len != len)
916 if (memcmp(name->name, str, len))
918 head = rcu_dereference(PROC_I(inode)->sysctl);
919 return !head || !sysctl_is_seen(head);
922 static const struct dentry_operations proc_sys_dentry_operations = {
923 .d_revalidate = proc_sys_revalidate,
924 .d_delete = proc_sys_delete,
925 .d_compare = proc_sys_compare,
928 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
929 const char *name, int namelen)
931 struct ctl_table_header *head;
932 struct ctl_table *entry;
934 entry = find_entry(&head, dir, name, namelen);
936 return ERR_PTR(-ENOENT);
937 if (!S_ISDIR(entry->mode))
938 return ERR_PTR(-ENOTDIR);
939 return container_of(head, struct ctl_dir, header);
942 static struct ctl_dir *new_dir(struct ctl_table_set *set,
943 const char *name, int namelen)
945 struct ctl_table *table;
947 struct ctl_node *node;
950 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
951 sizeof(struct ctl_table)*2 + namelen + 1,
956 node = (struct ctl_node *)(new + 1);
957 table = (struct ctl_table *)(node + 1);
958 new_name = (char *)(table + 2);
959 memcpy(new_name, name, namelen);
960 new_name[namelen] = '\0';
961 table[0].procname = new_name;
962 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
963 init_header(&new->header, set->dir.header.root, set, node, table);
969 * get_subdir - find or create a subdir with the specified name.
970 * @dir: Directory to create the subdirectory in
971 * @name: The name of the subdirectory to find or create
972 * @namelen: The length of name
974 * Takes a directory with an elevated reference count so we know that
975 * if we drop the lock the directory will not go away. Upon success
976 * the reference is moved from @dir to the returned subdirectory.
977 * Upon error an error code is returned and the reference on @dir is
980 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
981 const char *name, int namelen)
983 struct ctl_table_set *set = dir->header.set;
984 struct ctl_dir *subdir, *new = NULL;
987 spin_lock(&sysctl_lock);
988 subdir = find_subdir(dir, name, namelen);
991 if (PTR_ERR(subdir) != -ENOENT)
994 spin_unlock(&sysctl_lock);
995 new = new_dir(set, name, namelen);
996 spin_lock(&sysctl_lock);
997 subdir = ERR_PTR(-ENOMEM);
1001 /* Was the subdir added while we dropped the lock? */
1002 subdir = find_subdir(dir, name, namelen);
1003 if (!IS_ERR(subdir))
1005 if (PTR_ERR(subdir) != -ENOENT)
1008 /* Nope. Use the our freshly made directory entry. */
1009 err = insert_header(dir, &new->header);
1010 subdir = ERR_PTR(err);
1015 subdir->header.nreg++;
1017 if (IS_ERR(subdir)) {
1018 pr_err("sysctl could not get directory: ");
1019 sysctl_print_dir(dir);
1020 pr_cont("/%*.*s %ld\n",
1021 namelen, namelen, name, PTR_ERR(subdir));
1023 drop_sysctl_table(&dir->header);
1025 drop_sysctl_table(&new->header);
1026 spin_unlock(&sysctl_lock);
1030 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1032 struct ctl_dir *parent;
1033 const char *procname;
1034 if (!dir->header.parent)
1036 parent = xlate_dir(set, dir->header.parent);
1039 procname = dir->header.ctl_table[0].procname;
1040 return find_subdir(parent, procname, strlen(procname));
1043 static int sysctl_follow_link(struct ctl_table_header **phead,
1044 struct ctl_table **pentry)
1046 struct ctl_table_header *head;
1047 struct ctl_table_root *root;
1048 struct ctl_table_set *set;
1049 struct ctl_table *entry;
1050 struct ctl_dir *dir;
1054 spin_lock(&sysctl_lock);
1055 root = (*pentry)->data;
1056 set = lookup_header_set(root);
1057 dir = xlate_dir(set, (*phead)->parent);
1061 const char *procname = (*pentry)->procname;
1063 entry = find_entry(&head, dir, procname, strlen(procname));
1065 if (entry && use_table(head)) {
1066 unuse_table(*phead);
1073 spin_unlock(&sysctl_lock);
1077 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1079 struct va_format vaf;
1082 va_start(args, fmt);
1086 pr_err("sysctl table check failed: %s/%s %pV\n",
1087 path, table->procname, &vaf);
1093 static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1097 if ((table->proc_handler == proc_douintvec) ||
1098 (table->proc_handler == proc_douintvec_minmax)) {
1099 if (table->maxlen != sizeof(unsigned int))
1100 err |= sysctl_err(path, table, "array not allowed");
1106 static int sysctl_check_table(const char *path, struct ctl_table *table)
1109 for (; table->procname; table++) {
1111 err |= sysctl_err(path, table, "Not a file");
1113 if ((table->proc_handler == proc_dostring) ||
1114 (table->proc_handler == proc_dointvec) ||
1115 (table->proc_handler == proc_douintvec) ||
1116 (table->proc_handler == proc_douintvec_minmax) ||
1117 (table->proc_handler == proc_dointvec_minmax) ||
1118 (table->proc_handler == proc_dointvec_jiffies) ||
1119 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1120 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1121 (table->proc_handler == proc_doulongvec_minmax) ||
1122 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1124 err |= sysctl_err(path, table, "No data");
1126 err |= sysctl_err(path, table, "No maxlen");
1128 err |= sysctl_check_table_array(path, table);
1130 if (!table->proc_handler)
1131 err |= sysctl_err(path, table, "No proc_handler");
1133 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1134 err |= sysctl_err(path, table, "bogus .mode 0%o",
1140 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1141 struct ctl_table_root *link_root)
1143 struct ctl_table *link_table, *entry, *link;
1144 struct ctl_table_header *links;
1145 struct ctl_node *node;
1147 int nr_entries, name_bytes;
1151 for (entry = table; entry->procname; entry++) {
1153 name_bytes += strlen(entry->procname) + 1;
1156 links = kzalloc(sizeof(struct ctl_table_header) +
1157 sizeof(struct ctl_node)*nr_entries +
1158 sizeof(struct ctl_table)*(nr_entries + 1) +
1165 node = (struct ctl_node *)(links + 1);
1166 link_table = (struct ctl_table *)(node + nr_entries);
1167 link_name = (char *)&link_table[nr_entries + 1];
1169 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1170 int len = strlen(entry->procname) + 1;
1171 memcpy(link_name, entry->procname, len);
1172 link->procname = link_name;
1173 link->mode = S_IFLNK|S_IRWXUGO;
1174 link->data = link_root;
1177 init_header(links, dir->header.root, dir->header.set, node, link_table);
1178 links->nreg = nr_entries;
1183 static bool get_links(struct ctl_dir *dir,
1184 struct ctl_table *table, struct ctl_table_root *link_root)
1186 struct ctl_table_header *head;
1187 struct ctl_table *entry, *link;
1189 /* Are there links available for every entry in table? */
1190 for (entry = table; entry->procname; entry++) {
1191 const char *procname = entry->procname;
1192 link = find_entry(&head, dir, procname, strlen(procname));
1195 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1197 if (S_ISLNK(link->mode) && (link->data == link_root))
1202 /* The checks passed. Increase the registration count on the links */
1203 for (entry = table; entry->procname; entry++) {
1204 const char *procname = entry->procname;
1205 link = find_entry(&head, dir, procname, strlen(procname));
1211 static int insert_links(struct ctl_table_header *head)
1213 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1214 struct ctl_dir *core_parent = NULL;
1215 struct ctl_table_header *links;
1218 if (head->set == root_set)
1221 core_parent = xlate_dir(root_set, head->parent);
1222 if (IS_ERR(core_parent))
1225 if (get_links(core_parent, head->ctl_table, head->root))
1228 core_parent->header.nreg++;
1229 spin_unlock(&sysctl_lock);
1231 links = new_links(core_parent, head->ctl_table, head->root);
1233 spin_lock(&sysctl_lock);
1239 if (get_links(core_parent, head->ctl_table, head->root)) {
1244 err = insert_header(core_parent, links);
1248 drop_sysctl_table(&core_parent->header);
1253 * __register_sysctl_table - register a leaf sysctl table
1254 * @set: Sysctl tree to register on
1255 * @path: The path to the directory the sysctl table is in.
1256 * @table: the top-level table structure
1258 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1259 * array. A completely 0 filled entry terminates the table.
1261 * The members of the &struct ctl_table structure are used as follows:
1263 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1264 * enter a sysctl file
1266 * data - a pointer to data for use by proc_handler
1268 * maxlen - the maximum size in bytes of the data
1270 * mode - the file permissions for the /proc/sys file
1272 * child - must be %NULL.
1274 * proc_handler - the text handler routine (described below)
1276 * extra1, extra2 - extra pointers usable by the proc handler routines
1278 * Leaf nodes in the sysctl tree will be represented by a single file
1279 * under /proc; non-leaf nodes will be represented by directories.
1281 * There must be a proc_handler routine for any terminal nodes.
1282 * Several default handlers are available to cover common cases -
1284 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1285 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1286 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1288 * It is the handler's job to read the input buffer from user memory
1289 * and process it. The handler should return 0 on success.
1291 * This routine returns %NULL on a failure to register, and a pointer
1292 * to the table header on success.
1294 struct ctl_table_header *__register_sysctl_table(
1295 struct ctl_table_set *set,
1296 const char *path, struct ctl_table *table)
1298 struct ctl_table_root *root = set->dir.header.root;
1299 struct ctl_table_header *header;
1300 const char *name, *nextname;
1301 struct ctl_dir *dir;
1302 struct ctl_table *entry;
1303 struct ctl_node *node;
1306 for (entry = table; entry->procname; entry++)
1309 header = kzalloc(sizeof(struct ctl_table_header) +
1310 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
1314 node = (struct ctl_node *)(header + 1);
1315 init_header(header, root, set, node, table);
1316 if (sysctl_check_table(path, table))
1319 spin_lock(&sysctl_lock);
1321 /* Reference moved down the diretory tree get_subdir */
1323 spin_unlock(&sysctl_lock);
1325 /* Find the directory for the ctl_table */
1326 for (name = path; name; name = nextname) {
1328 nextname = strchr(name, '/');
1330 namelen = nextname - name;
1333 namelen = strlen(name);
1338 dir = get_subdir(dir, name, namelen);
1343 spin_lock(&sysctl_lock);
1344 if (insert_header(dir, header))
1345 goto fail_put_dir_locked;
1347 drop_sysctl_table(&dir->header);
1348 spin_unlock(&sysctl_lock);
1352 fail_put_dir_locked:
1353 drop_sysctl_table(&dir->header);
1354 spin_unlock(&sysctl_lock);
1362 * register_sysctl - register a sysctl table
1363 * @path: The path to the directory the sysctl table is in.
1364 * @table: the table structure
1366 * Register a sysctl table. @table should be a filled in ctl_table
1367 * array. A completely 0 filled entry terminates the table.
1369 * See __register_sysctl_table for more details.
1371 struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1373 return __register_sysctl_table(&sysctl_table_root.default_set,
1376 EXPORT_SYMBOL(register_sysctl);
1378 static char *append_path(const char *path, char *pos, const char *name)
1381 namelen = strlen(name);
1382 if (((pos - path) + namelen + 2) >= PATH_MAX)
1384 memcpy(pos, name, namelen);
1386 pos[namelen + 1] = '\0';
1391 static int count_subheaders(struct ctl_table *table)
1394 int nr_subheaders = 0;
1395 struct ctl_table *entry;
1397 /* special case: no directory and empty directory */
1398 if (!table || !table->procname)
1401 for (entry = table; entry->procname; entry++) {
1403 nr_subheaders += count_subheaders(entry->child);
1407 return nr_subheaders + has_files;
1410 static int register_leaf_sysctl_tables(const char *path, char *pos,
1411 struct ctl_table_header ***subheader, struct ctl_table_set *set,
1412 struct ctl_table *table)
1414 struct ctl_table *ctl_table_arg = NULL;
1415 struct ctl_table *entry, *files;
1420 for (entry = table; entry->procname; entry++) {
1428 /* If there are mixed files and directories we need a new table */
1429 if (nr_dirs && nr_files) {
1430 struct ctl_table *new;
1431 files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
1436 ctl_table_arg = files;
1437 for (new = files, entry = table; entry->procname; entry++) {
1445 /* Register everything except a directory full of subdirectories */
1446 if (nr_files || !nr_dirs) {
1447 struct ctl_table_header *header;
1448 header = __register_sysctl_table(set, path, files);
1450 kfree(ctl_table_arg);
1454 /* Remember if we need to free the file table */
1455 header->ctl_table_arg = ctl_table_arg;
1456 **subheader = header;
1460 /* Recurse into the subdirectories. */
1461 for (entry = table; entry->procname; entry++) {
1467 err = -ENAMETOOLONG;
1468 child_pos = append_path(path, pos, entry->procname);
1472 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1480 /* On failure our caller will unregister all registered subheaders */
1485 * __register_sysctl_paths - register a sysctl table hierarchy
1486 * @set: Sysctl tree to register on
1487 * @path: The path to the directory the sysctl table is in.
1488 * @table: the top-level table structure
1490 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1491 * array. A completely 0 filled entry terminates the table.
1493 * See __register_sysctl_table for more details.
1495 struct ctl_table_header *__register_sysctl_paths(
1496 struct ctl_table_set *set,
1497 const struct ctl_path *path, struct ctl_table *table)
1499 struct ctl_table *ctl_table_arg = table;
1500 int nr_subheaders = count_subheaders(table);
1501 struct ctl_table_header *header = NULL, **subheaders, **subheader;
1502 const struct ctl_path *component;
1503 char *new_path, *pos;
1505 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1510 for (component = path; component->procname; component++) {
1511 pos = append_path(new_path, pos, component->procname);
1515 while (table->procname && table->child && !table[1].procname) {
1516 pos = append_path(new_path, pos, table->procname);
1519 table = table->child;
1521 if (nr_subheaders == 1) {
1522 header = __register_sysctl_table(set, new_path, table);
1524 header->ctl_table_arg = ctl_table_arg;
1526 header = kzalloc(sizeof(*header) +
1527 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1531 subheaders = (struct ctl_table_header **) (header + 1);
1532 subheader = subheaders;
1533 header->ctl_table_arg = ctl_table_arg;
1535 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1537 goto err_register_leaves;
1544 err_register_leaves:
1545 while (subheader > subheaders) {
1546 struct ctl_table_header *subh = *(--subheader);
1547 struct ctl_table *table = subh->ctl_table_arg;
1548 unregister_sysctl_table(subh);
1557 * register_sysctl_table_path - register a sysctl table hierarchy
1558 * @path: The path to the directory the sysctl table is in.
1559 * @table: the top-level table structure
1561 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1562 * array. A completely 0 filled entry terminates the table.
1564 * See __register_sysctl_paths for more details.
1566 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1567 struct ctl_table *table)
1569 return __register_sysctl_paths(&sysctl_table_root.default_set,
1572 EXPORT_SYMBOL(register_sysctl_paths);
1575 * register_sysctl_table - register a sysctl table hierarchy
1576 * @table: the top-level table structure
1578 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1579 * array. A completely 0 filled entry terminates the table.
1581 * See register_sysctl_paths for more details.
1583 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1585 static const struct ctl_path null_path[] = { {} };
1587 return register_sysctl_paths(null_path, table);
1589 EXPORT_SYMBOL(register_sysctl_table);
1591 static void put_links(struct ctl_table_header *header)
1593 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1594 struct ctl_table_root *root = header->root;
1595 struct ctl_dir *parent = header->parent;
1596 struct ctl_dir *core_parent;
1597 struct ctl_table *entry;
1599 if (header->set == root_set)
1602 core_parent = xlate_dir(root_set, parent);
1603 if (IS_ERR(core_parent))
1606 for (entry = header->ctl_table; entry->procname; entry++) {
1607 struct ctl_table_header *link_head;
1608 struct ctl_table *link;
1609 const char *name = entry->procname;
1611 link = find_entry(&link_head, core_parent, name, strlen(name));
1613 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1614 (S_ISLNK(link->mode) && (link->data == root)))) {
1615 drop_sysctl_table(link_head);
1618 pr_err("sysctl link missing during unregister: ");
1619 sysctl_print_dir(parent);
1620 pr_cont("/%s\n", name);
1625 static void drop_sysctl_table(struct ctl_table_header *header)
1627 struct ctl_dir *parent = header->parent;
1634 start_unregistering(header);
1637 if (!--header->count)
1638 kfree_rcu(header, rcu);
1641 drop_sysctl_table(&parent->header);
1645 * unregister_sysctl_table - unregister a sysctl table hierarchy
1646 * @header: the header returned from register_sysctl_table
1648 * Unregisters the sysctl table and all children. proc entries may not
1649 * actually be removed until they are no longer used by anyone.
1651 void unregister_sysctl_table(struct ctl_table_header * header)
1659 nr_subheaders = count_subheaders(header->ctl_table_arg);
1660 if (unlikely(nr_subheaders > 1)) {
1661 struct ctl_table_header **subheaders;
1664 subheaders = (struct ctl_table_header **)(header + 1);
1665 for (i = nr_subheaders -1; i >= 0; i--) {
1666 struct ctl_table_header *subh = subheaders[i];
1667 struct ctl_table *table = subh->ctl_table_arg;
1668 unregister_sysctl_table(subh);
1675 spin_lock(&sysctl_lock);
1676 drop_sysctl_table(header);
1677 spin_unlock(&sysctl_lock);
1679 EXPORT_SYMBOL(unregister_sysctl_table);
1681 void setup_sysctl_set(struct ctl_table_set *set,
1682 struct ctl_table_root *root,
1683 int (*is_seen)(struct ctl_table_set *))
1685 memset(set, 0, sizeof(*set));
1686 set->is_seen = is_seen;
1687 init_header(&set->dir.header, root, set, NULL, root_table);
1690 void retire_sysctl_set(struct ctl_table_set *set)
1692 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1695 int __init proc_sys_init(void)
1697 struct proc_dir_entry *proc_sys_root;
1699 proc_sys_root = proc_mkdir("sys", NULL);
1700 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1701 proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
1702 proc_sys_root->nlink = 0;
1704 return sysctl_init();