xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 1f87f0b5)
177b14db5SEric W. Biederman /*
277b14db5SEric W. Biederman  * /proc/sys support
377b14db5SEric W. Biederman  */
41e0edd3fSAlexey Dobriyan #include <linux/init.h>
577b14db5SEric W. Biederman #include <linux/sysctl.h>
6f1ecf068SLucas De Marchi #include <linux/poll.h>
777b14db5SEric W. Biederman #include <linux/proc_fs.h>
877b14db5SEric W. Biederman #include <linux/security.h>
934286d66SNick Piggin #include <linux/namei.h>
101f87f0b5SEric W. Biederman #include <linux/module.h>
1177b14db5SEric W. Biederman #include "internal.h"
1277b14db5SEric W. Biederman 
13d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
169043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
179043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1877b14db5SEric W. Biederman 
19f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
20f1ecf068SLucas De Marchi {
21f1ecf068SLucas De Marchi 	if (!poll)
22f1ecf068SLucas De Marchi 		return;
23f1ecf068SLucas De Marchi 
24f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
25f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
26f1ecf068SLucas De Marchi }
27f1ecf068SLucas De Marchi 
281f87f0b5SEric W. Biederman static struct ctl_table root_table[1];
291f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root;
301f87f0b5SEric W. Biederman static struct ctl_table_header root_table_header = {
311f87f0b5SEric W. Biederman 	{{.count = 1,
321f87f0b5SEric W. Biederman 	.ctl_table = root_table,
331f87f0b5SEric W. Biederman 	.ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
341f87f0b5SEric W. Biederman 	.root = &sysctl_table_root,
351f87f0b5SEric W. Biederman 	.set = &sysctl_table_root.default_set,
361f87f0b5SEric W. Biederman };
371f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root = {
381f87f0b5SEric W. Biederman 	.root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
391f87f0b5SEric W. Biederman 	.default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
401f87f0b5SEric W. Biederman };
411f87f0b5SEric W. Biederman 
421f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
431f87f0b5SEric W. Biederman 
441f87f0b5SEric W. Biederman /* called under sysctl_lock */
451f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
461f87f0b5SEric W. Biederman {
471f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
481f87f0b5SEric W. Biederman 		return 0;
491f87f0b5SEric W. Biederman 	p->used++;
501f87f0b5SEric W. Biederman 	return 1;
511f87f0b5SEric W. Biederman }
521f87f0b5SEric W. Biederman 
531f87f0b5SEric W. Biederman /* called under sysctl_lock */
541f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
551f87f0b5SEric W. Biederman {
561f87f0b5SEric W. Biederman 	if (!--p->used)
571f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
581f87f0b5SEric W. Biederman 			complete(p->unregistering);
591f87f0b5SEric W. Biederman }
601f87f0b5SEric W. Biederman 
611f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
621f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
631f87f0b5SEric W. Biederman {
641f87f0b5SEric W. Biederman 	/*
651f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
661f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
671f87f0b5SEric W. Biederman 	 */
681f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
691f87f0b5SEric W. Biederman 		struct completion wait;
701f87f0b5SEric W. Biederman 		init_completion(&wait);
711f87f0b5SEric W. Biederman 		p->unregistering = &wait;
721f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
731f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
741f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
751f87f0b5SEric W. Biederman 	} else {
761f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
771f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
781f87f0b5SEric W. Biederman 	}
791f87f0b5SEric W. Biederman 	/*
801f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
811f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
821f87f0b5SEric W. Biederman 	 */
831f87f0b5SEric W. Biederman 	list_del_init(&p->ctl_entry);
841f87f0b5SEric W. Biederman }
851f87f0b5SEric W. Biederman 
861f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
871f87f0b5SEric W. Biederman {
881f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
891f87f0b5SEric W. Biederman 	head->count++;
901f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
911f87f0b5SEric W. Biederman }
921f87f0b5SEric W. Biederman 
931f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
941f87f0b5SEric W. Biederman {
951f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
961f87f0b5SEric W. Biederman 	if (!--head->count)
971f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
981f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
991f87f0b5SEric W. Biederman }
1001f87f0b5SEric W. Biederman 
1011f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1021f87f0b5SEric W. Biederman {
1031f87f0b5SEric W. Biederman 	if (!head)
1041f87f0b5SEric W. Biederman 		BUG();
1051f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1061f87f0b5SEric W. Biederman 	if (!use_table(head))
1071f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
1081f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1091f87f0b5SEric W. Biederman 	return head;
1101f87f0b5SEric W. Biederman }
1111f87f0b5SEric W. Biederman 
1121f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
1131f87f0b5SEric W. Biederman {
1141f87f0b5SEric W. Biederman 	if (!head)
1151f87f0b5SEric W. Biederman 		return;
1161f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1171f87f0b5SEric W. Biederman 	unuse_table(head);
1181f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1191f87f0b5SEric W. Biederman }
1201f87f0b5SEric W. Biederman 
1211f87f0b5SEric W. Biederman static struct ctl_table_set *
1221f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1231f87f0b5SEric W. Biederman {
1241f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
1251f87f0b5SEric W. Biederman 	if (root->lookup)
1261f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
1271f87f0b5SEric W. Biederman 	return set;
1281f87f0b5SEric W. Biederman }
1291f87f0b5SEric W. Biederman 
1301f87f0b5SEric W. Biederman static struct list_head *
1311f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1321f87f0b5SEric W. Biederman {
1331f87f0b5SEric W. Biederman 	struct ctl_table_set *set = lookup_header_set(root, namespaces);
1341f87f0b5SEric W. Biederman 	return &set->list;
1351f87f0b5SEric W. Biederman }
1361f87f0b5SEric W. Biederman 
1371f87f0b5SEric W. Biederman static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1381f87f0b5SEric W. Biederman 						struct ctl_table_header *prev)
1391f87f0b5SEric W. Biederman {
1401f87f0b5SEric W. Biederman 	struct ctl_table_root *root;
1411f87f0b5SEric W. Biederman 	struct list_head *header_list;
1421f87f0b5SEric W. Biederman 	struct ctl_table_header *head;
1431f87f0b5SEric W. Biederman 	struct list_head *tmp;
1441f87f0b5SEric W. Biederman 
1451f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1461f87f0b5SEric W. Biederman 	if (prev) {
1471f87f0b5SEric W. Biederman 		head = prev;
1481f87f0b5SEric W. Biederman 		tmp = &prev->ctl_entry;
1491f87f0b5SEric W. Biederman 		unuse_table(prev);
1501f87f0b5SEric W. Biederman 		goto next;
1511f87f0b5SEric W. Biederman 	}
1521f87f0b5SEric W. Biederman 	tmp = &root_table_header.ctl_entry;
1531f87f0b5SEric W. Biederman 	for (;;) {
1541f87f0b5SEric W. Biederman 		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1551f87f0b5SEric W. Biederman 
1561f87f0b5SEric W. Biederman 		if (!use_table(head))
1571f87f0b5SEric W. Biederman 			goto next;
1581f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
1591f87f0b5SEric W. Biederman 		return head;
1601f87f0b5SEric W. Biederman 	next:
1611f87f0b5SEric W. Biederman 		root = head->root;
1621f87f0b5SEric W. Biederman 		tmp = tmp->next;
1631f87f0b5SEric W. Biederman 		header_list = lookup_header_list(root, namespaces);
1641f87f0b5SEric W. Biederman 		if (tmp != header_list)
1651f87f0b5SEric W. Biederman 			continue;
1661f87f0b5SEric W. Biederman 
1671f87f0b5SEric W. Biederman 		do {
1681f87f0b5SEric W. Biederman 			root = list_entry(root->root_list.next,
1691f87f0b5SEric W. Biederman 					struct ctl_table_root, root_list);
1701f87f0b5SEric W. Biederman 			if (root == &sysctl_table_root)
1711f87f0b5SEric W. Biederman 				goto out;
1721f87f0b5SEric W. Biederman 			header_list = lookup_header_list(root, namespaces);
1731f87f0b5SEric W. Biederman 		} while (list_empty(header_list));
1741f87f0b5SEric W. Biederman 		tmp = header_list->next;
1751f87f0b5SEric W. Biederman 	}
1761f87f0b5SEric W. Biederman out:
1771f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1781f87f0b5SEric W. Biederman 	return NULL;
1791f87f0b5SEric W. Biederman }
1801f87f0b5SEric W. Biederman 
1811f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1821f87f0b5SEric W. Biederman {
1831f87f0b5SEric W. Biederman 	return __sysctl_head_next(current->nsproxy, prev);
1841f87f0b5SEric W. Biederman }
1851f87f0b5SEric W. Biederman 
1861f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
1871f87f0b5SEric W. Biederman {
1881f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1891f87f0b5SEric W. Biederman 	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
1901f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1911f87f0b5SEric W. Biederman }
1921f87f0b5SEric W. Biederman 
1931f87f0b5SEric W. Biederman /*
1941f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
1951f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
1961f87f0b5SEric W. Biederman  */
1971f87f0b5SEric W. Biederman 
1981f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
1991f87f0b5SEric W. Biederman {
2001f87f0b5SEric W. Biederman 	if (!current_euid())
2011f87f0b5SEric W. Biederman 		mode >>= 6;
2021f87f0b5SEric W. Biederman 	else if (in_egroup_p(0))
2031f87f0b5SEric W. Biederman 		mode >>= 3;
2041f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
2051f87f0b5SEric W. Biederman 		return 0;
2061f87f0b5SEric W. Biederman 	return -EACCES;
2071f87f0b5SEric W. Biederman }
2081f87f0b5SEric W. Biederman 
2091f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
2101f87f0b5SEric W. Biederman {
2111f87f0b5SEric W. Biederman 	int mode;
2121f87f0b5SEric W. Biederman 
2131f87f0b5SEric W. Biederman 	if (root->permissions)
2141f87f0b5SEric W. Biederman 		mode = root->permissions(root, current->nsproxy, table);
2151f87f0b5SEric W. Biederman 	else
2161f87f0b5SEric W. Biederman 		mode = table->mode;
2171f87f0b5SEric W. Biederman 
2181f87f0b5SEric W. Biederman 	return test_perm(mode, op);
2191f87f0b5SEric W. Biederman }
2201f87f0b5SEric W. Biederman 
2211f87f0b5SEric W. Biederman static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
2221f87f0b5SEric W. Biederman {
2231f87f0b5SEric W. Biederman 	for (; table->procname; table++) {
2241f87f0b5SEric W. Biederman 		table->parent = parent;
2251f87f0b5SEric W. Biederman 		if (table->child)
2261f87f0b5SEric W. Biederman 			sysctl_set_parent(table, table->child);
2271f87f0b5SEric W. Biederman 	}
2281f87f0b5SEric W. Biederman }
2291f87f0b5SEric W. Biederman 
2301f87f0b5SEric W. Biederman 
2319043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
2329043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
23377b14db5SEric W. Biederman {
23477b14db5SEric W. Biederman 	struct inode *inode;
2359043476fSAl Viro 	struct proc_inode *ei;
23677b14db5SEric W. Biederman 
2379043476fSAl Viro 	inode = new_inode(sb);
23877b14db5SEric W. Biederman 	if (!inode)
23977b14db5SEric W. Biederman 		goto out;
24077b14db5SEric W. Biederman 
24185fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
24285fe4025SChristoph Hellwig 
2439043476fSAl Viro 	sysctl_head_get(head);
24477b14db5SEric W. Biederman 	ei = PROC_I(inode);
2459043476fSAl Viro 	ei->sysctl = head;
2469043476fSAl Viro 	ei->sysctl_entry = table;
2479043476fSAl Viro 
24877b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2499043476fSAl Viro 	inode->i_mode = table->mode;
2509043476fSAl Viro 	if (!table->child) {
2519043476fSAl Viro 		inode->i_mode |= S_IFREG;
25277b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
25377b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
2549043476fSAl Viro 	} else {
2559043476fSAl Viro 		inode->i_mode |= S_IFDIR;
2566d6b77f1SMiklos Szeredi 		clear_nlink(inode);
2579043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
2589043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
2599043476fSAl Viro 	}
26077b14db5SEric W. Biederman out:
26177b14db5SEric W. Biederman 	return inode;
26277b14db5SEric W. Biederman }
26377b14db5SEric W. Biederman 
2649043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
26577b14db5SEric W. Biederman {
2662315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
26736885d7bSLucas De Marchi 		if (strlen(p->procname) != name->len)
26877b14db5SEric W. Biederman 			continue;
26977b14db5SEric W. Biederman 
27036885d7bSLucas De Marchi 		if (memcmp(p->procname, name->name, name->len) != 0)
27177b14db5SEric W. Biederman 			continue;
27277b14db5SEric W. Biederman 
27377b14db5SEric W. Biederman 		/* I have a match */
2749043476fSAl Viro 		return p;
27577b14db5SEric W. Biederman 	}
27677b14db5SEric W. Biederman 	return NULL;
27777b14db5SEric W. Biederman }
27877b14db5SEric W. Biederman 
27981324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
28077b14db5SEric W. Biederman {
2819043476fSAl Viro 	if (PROC_I(inode)->sysctl)
2829043476fSAl Viro 		return sysctl_head_grab(PROC_I(inode)->sysctl);
2839043476fSAl Viro 	else
2849043476fSAl Viro 		return sysctl_head_next(NULL);
28577b14db5SEric W. Biederman }
28677b14db5SEric W. Biederman 
28777b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
28877b14db5SEric W. Biederman 					struct nameidata *nd)
28977b14db5SEric W. Biederman {
2909043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
2919043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
2929043476fSAl Viro 	struct ctl_table_header *h = NULL;
2939043476fSAl Viro 	struct qstr *name = &dentry->d_name;
2949043476fSAl Viro 	struct ctl_table *p;
29577b14db5SEric W. Biederman 	struct inode *inode;
2969043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
29777b14db5SEric W. Biederman 
2989043476fSAl Viro 	if (IS_ERR(head))
2999043476fSAl Viro 		return ERR_CAST(head);
3009043476fSAl Viro 
3019043476fSAl Viro 	if (table && !table->child) {
3029043476fSAl Viro 		WARN_ON(1);
3039043476fSAl Viro 		goto out;
3049043476fSAl Viro 	}
3059043476fSAl Viro 
3069043476fSAl Viro 	table = table ? table->child : head->ctl_table;
3079043476fSAl Viro 
3089043476fSAl Viro 	p = find_in_table(table, name);
3099043476fSAl Viro 	if (!p) {
3109043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
3119043476fSAl Viro 			if (h->attached_to != table)
3129043476fSAl Viro 				continue;
3139043476fSAl Viro 			p = find_in_table(h->attached_by, name);
3149043476fSAl Viro 			if (p)
3159043476fSAl Viro 				break;
3169043476fSAl Viro 		}
3179043476fSAl Viro 	}
3189043476fSAl Viro 
3199043476fSAl Viro 	if (!p)
32077b14db5SEric W. Biederman 		goto out;
32177b14db5SEric W. Biederman 
32277b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
3239043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
3249043476fSAl Viro 	if (h)
3259043476fSAl Viro 		sysctl_head_finish(h);
3269043476fSAl Viro 
32777b14db5SEric W. Biederman 	if (!inode)
32877b14db5SEric W. Biederman 		goto out;
32977b14db5SEric W. Biederman 
33077b14db5SEric W. Biederman 	err = NULL;
331fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
33277b14db5SEric W. Biederman 	d_add(dentry, inode);
33377b14db5SEric W. Biederman 
33477b14db5SEric W. Biederman out:
33577b14db5SEric W. Biederman 	sysctl_head_finish(head);
33677b14db5SEric W. Biederman 	return err;
33777b14db5SEric W. Biederman }
33877b14db5SEric W. Biederman 
3397708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
3407708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
34177b14db5SEric W. Biederman {
3429043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
3439043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3449043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3452a2da53bSDavid Howells 	ssize_t error;
3462a2da53bSDavid Howells 	size_t res;
34777b14db5SEric W. Biederman 
3489043476fSAl Viro 	if (IS_ERR(head))
3499043476fSAl Viro 		return PTR_ERR(head);
35077b14db5SEric W. Biederman 
35177b14db5SEric W. Biederman 	/*
35277b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
35377b14db5SEric W. Biederman 	 * and won't be until we finish.
35477b14db5SEric W. Biederman 	 */
35577b14db5SEric W. Biederman 	error = -EPERM;
356d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
35777b14db5SEric W. Biederman 		goto out;
35877b14db5SEric W. Biederman 
3599043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
3609043476fSAl Viro 	error = -EINVAL;
3619043476fSAl Viro 	if (!table->proc_handler)
3629043476fSAl Viro 		goto out;
3639043476fSAl Viro 
36477b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
36577b14db5SEric W. Biederman 	res = count;
3668d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
36777b14db5SEric W. Biederman 	if (!error)
36877b14db5SEric W. Biederman 		error = res;
36977b14db5SEric W. Biederman out:
37077b14db5SEric W. Biederman 	sysctl_head_finish(head);
37177b14db5SEric W. Biederman 
37277b14db5SEric W. Biederman 	return error;
37377b14db5SEric W. Biederman }
37477b14db5SEric W. Biederman 
3757708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
3767708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
3777708bfb1SPavel Emelyanov {
3787708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
3797708bfb1SPavel Emelyanov }
3807708bfb1SPavel Emelyanov 
38177b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
38277b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
38377b14db5SEric W. Biederman {
3847708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
38577b14db5SEric W. Biederman }
38677b14db5SEric W. Biederman 
387f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
388f1ecf068SLucas De Marchi {
389f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
390f1ecf068SLucas De Marchi 
391f1ecf068SLucas De Marchi 	if (table->poll)
392f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
393f1ecf068SLucas De Marchi 
394f1ecf068SLucas De Marchi 	return 0;
395f1ecf068SLucas De Marchi }
396f1ecf068SLucas De Marchi 
397f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
398f1ecf068SLucas De Marchi {
399f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
400f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
401f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
402f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
403f1ecf068SLucas De Marchi 
404f1ecf068SLucas De Marchi 	if (!table->proc_handler)
405f1ecf068SLucas De Marchi 		goto out;
406f1ecf068SLucas De Marchi 
407f1ecf068SLucas De Marchi 	if (!table->poll)
408f1ecf068SLucas De Marchi 		goto out;
409f1ecf068SLucas De Marchi 
410f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
411f1ecf068SLucas De Marchi 
412f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
413f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
414f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
415f1ecf068SLucas De Marchi 	}
416f1ecf068SLucas De Marchi 
417f1ecf068SLucas De Marchi out:
418f1ecf068SLucas De Marchi 	return ret;
419f1ecf068SLucas De Marchi }
42077b14db5SEric W. Biederman 
42177b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
4229043476fSAl Viro 				filldir_t filldir,
4239043476fSAl Viro 				struct ctl_table_header *head,
4249043476fSAl Viro 				struct ctl_table *table)
42577b14db5SEric W. Biederman {
42677b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
42777b14db5SEric W. Biederman 	struct inode *inode;
42877b14db5SEric W. Biederman 	struct qstr qname;
42977b14db5SEric W. Biederman 	ino_t ino = 0;
43077b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
43177b14db5SEric W. Biederman 
43277b14db5SEric W. Biederman 	qname.name = table->procname;
43377b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
43477b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
43577b14db5SEric W. Biederman 
43677b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
43777b14db5SEric W. Biederman 	if (!child) {
4389043476fSAl Viro 		child = d_alloc(dir, &qname);
4399043476fSAl Viro 		if (child) {
4409043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
4419043476fSAl Viro 			if (!inode) {
4429043476fSAl Viro 				dput(child);
4439043476fSAl Viro 				return -ENOMEM;
4449043476fSAl Viro 			} else {
445fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
4469043476fSAl Viro 				d_add(child, inode);
44777b14db5SEric W. Biederman 			}
4489043476fSAl Viro 		} else {
4499043476fSAl Viro 			return -ENOMEM;
45077b14db5SEric W. Biederman 		}
45177b14db5SEric W. Biederman 	}
45277b14db5SEric W. Biederman 	inode = child->d_inode;
45377b14db5SEric W. Biederman 	ino  = inode->i_ino;
45477b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
45577b14db5SEric W. Biederman 	dput(child);
4569043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
4579043476fSAl Viro }
4589043476fSAl Viro 
4599043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
4609043476fSAl Viro 		unsigned long *pos, struct file *file,
4619043476fSAl Viro 		void *dirent, filldir_t filldir)
4629043476fSAl Viro {
4639043476fSAl Viro 
4642315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
4659043476fSAl Viro 		int res;
4669043476fSAl Viro 
4679043476fSAl Viro 		if (*pos < file->f_pos)
4689043476fSAl Viro 			continue;
4699043476fSAl Viro 
4709043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
4719043476fSAl Viro 		if (res)
4729043476fSAl Viro 			return res;
4739043476fSAl Viro 
4749043476fSAl Viro 		file->f_pos = *pos + 1;
4759043476fSAl Viro 	}
4769043476fSAl Viro 	return 0;
47777b14db5SEric W. Biederman }
47877b14db5SEric W. Biederman 
47977b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
48077b14db5SEric W. Biederman {
4819043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
48277b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
4839043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
4849043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
4859043476fSAl Viro 	struct ctl_table_header *h = NULL;
48677b14db5SEric W. Biederman 	unsigned long pos;
4879043476fSAl Viro 	int ret = -EINVAL;
48877b14db5SEric W. Biederman 
4899043476fSAl Viro 	if (IS_ERR(head))
4909043476fSAl Viro 		return PTR_ERR(head);
4919043476fSAl Viro 
4929043476fSAl Viro 	if (table && !table->child) {
4939043476fSAl Viro 		WARN_ON(1);
49477b14db5SEric W. Biederman 		goto out;
4959043476fSAl Viro 	}
4969043476fSAl Viro 
4979043476fSAl Viro 	table = table ? table->child : head->ctl_table;
49877b14db5SEric W. Biederman 
49977b14db5SEric W. Biederman 	ret = 0;
50077b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
50177b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
50277b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
50377b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
50477b14db5SEric W. Biederman 			goto out;
50577b14db5SEric W. Biederman 		filp->f_pos++;
50677b14db5SEric W. Biederman 	}
50777b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
50877b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
50977b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
51077b14db5SEric W. Biederman 			goto out;
51177b14db5SEric W. Biederman 		filp->f_pos++;
51277b14db5SEric W. Biederman 	}
51377b14db5SEric W. Biederman 	pos = 2;
51477b14db5SEric W. Biederman 
5159043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
5169043476fSAl Viro 	if (ret)
51777b14db5SEric W. Biederman 		goto out;
5189043476fSAl Viro 
5199043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
5209043476fSAl Viro 		if (h->attached_to != table)
5219043476fSAl Viro 			continue;
5229043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
5239043476fSAl Viro 		if (ret) {
5249043476fSAl Viro 			sysctl_head_finish(h);
5259043476fSAl Viro 			break;
52677b14db5SEric W. Biederman 		}
52777b14db5SEric W. Biederman 	}
52877b14db5SEric W. Biederman 	ret = 1;
52977b14db5SEric W. Biederman out:
53077b14db5SEric W. Biederman 	sysctl_head_finish(head);
53177b14db5SEric W. Biederman 	return ret;
53277b14db5SEric W. Biederman }
53377b14db5SEric W. Biederman 
53410556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
53577b14db5SEric W. Biederman {
53677b14db5SEric W. Biederman 	/*
53777b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
53877b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
53977b14db5SEric W. Biederman 	 */
540f696a365SMiklos Szeredi 	struct ctl_table_header *head;
541f696a365SMiklos Szeredi 	struct ctl_table *table;
54277b14db5SEric W. Biederman 	int error;
54377b14db5SEric W. Biederman 
544f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
545f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
546f696a365SMiklos Szeredi 		return -EACCES;
547f696a365SMiklos Szeredi 
548f696a365SMiklos Szeredi 	head = grab_header(inode);
5499043476fSAl Viro 	if (IS_ERR(head))
5509043476fSAl Viro 		return PTR_ERR(head);
55177b14db5SEric W. Biederman 
552f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
5539043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
5549043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
5559043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
5561fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
5579043476fSAl Viro 
55877b14db5SEric W. Biederman 	sysctl_head_finish(head);
55977b14db5SEric W. Biederman 	return error;
56077b14db5SEric W. Biederman }
56177b14db5SEric W. Biederman 
56277b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
56377b14db5SEric W. Biederman {
56477b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
56577b14db5SEric W. Biederman 	int error;
56677b14db5SEric W. Biederman 
56777b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
56877b14db5SEric W. Biederman 		return -EPERM;
56977b14db5SEric W. Biederman 
57077b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
5711025774cSChristoph Hellwig 	if (error)
57277b14db5SEric W. Biederman 		return error;
5731025774cSChristoph Hellwig 
5741025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5751025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5761025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
5771025774cSChristoph Hellwig 		if (error)
5781025774cSChristoph Hellwig 			return error;
5791025774cSChristoph Hellwig 	}
5801025774cSChristoph Hellwig 
5811025774cSChristoph Hellwig 	setattr_copy(inode, attr);
5821025774cSChristoph Hellwig 	mark_inode_dirty(inode);
5831025774cSChristoph Hellwig 	return 0;
58477b14db5SEric W. Biederman }
58577b14db5SEric W. Biederman 
5869043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
5879043476fSAl Viro {
5889043476fSAl Viro 	struct inode *inode = dentry->d_inode;
5899043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5909043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5919043476fSAl Viro 
5929043476fSAl Viro 	if (IS_ERR(head))
5939043476fSAl Viro 		return PTR_ERR(head);
5949043476fSAl Viro 
5959043476fSAl Viro 	generic_fillattr(inode, stat);
5969043476fSAl Viro 	if (table)
5979043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
5989043476fSAl Viro 
5999043476fSAl Viro 	sysctl_head_finish(head);
6009043476fSAl Viro 	return 0;
6019043476fSAl Viro }
6029043476fSAl Viro 
60377b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
604f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
605f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
60677b14db5SEric W. Biederman 	.read		= proc_sys_read,
60777b14db5SEric W. Biederman 	.write		= proc_sys_write,
6086038f373SArnd Bergmann 	.llseek		= default_llseek,
6099043476fSAl Viro };
6109043476fSAl Viro 
6119043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
612887df078SPavel Emelyanov 	.read		= generic_read_dir,
61377b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
6143222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
61577b14db5SEric W. Biederman };
61677b14db5SEric W. Biederman 
61703a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
6189043476fSAl Viro 	.permission	= proc_sys_permission,
6199043476fSAl Viro 	.setattr	= proc_sys_setattr,
6209043476fSAl Viro 	.getattr	= proc_sys_getattr,
6219043476fSAl Viro };
6229043476fSAl Viro 
6239043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
62477b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
62577b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
62677b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
6279043476fSAl Viro 	.getattr	= proc_sys_getattr,
62877b14db5SEric W. Biederman };
62977b14db5SEric W. Biederman 
63077b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
63177b14db5SEric W. Biederman {
63234286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
63334286d66SNick Piggin 		return -ECHILD;
6349043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
6359043476fSAl Viro }
6369043476fSAl Viro 
637fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
6389043476fSAl Viro {
6399043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
6409043476fSAl Viro }
6419043476fSAl Viro 
6421f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
6431f87f0b5SEric W. Biederman {
6441f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
6451f87f0b5SEric W. Biederman 	int res;
6461f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
6471f87f0b5SEric W. Biederman 	if (p->unregistering)
6481f87f0b5SEric W. Biederman 		res = 0;
6491f87f0b5SEric W. Biederman 	else if (!set->is_seen)
6501f87f0b5SEric W. Biederman 		res = 1;
6511f87f0b5SEric W. Biederman 	else
6521f87f0b5SEric W. Biederman 		res = set->is_seen(set);
6531f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
6541f87f0b5SEric W. Biederman 	return res;
6551f87f0b5SEric W. Biederman }
6561f87f0b5SEric W. Biederman 
657621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
658621e155aSNick Piggin 		const struct inode *pinode,
659621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
660621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
6619043476fSAl Viro {
662dfef6dcdSAl Viro 	struct ctl_table_header *head;
66331e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
66431e6b01fSNick Piggin 	 * that inode here can be NULL */
665dfef6dcdSAl Viro 	/* AV: can it, indeed? */
66631e6b01fSNick Piggin 	if (!inode)
667dfef6dcdSAl Viro 		return 1;
668621e155aSNick Piggin 	if (name->len != len)
6699043476fSAl Viro 		return 1;
670621e155aSNick Piggin 	if (memcmp(name->name, str, len))
6719043476fSAl Viro 		return 1;
672dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
673dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
67477b14db5SEric W. Biederman }
67577b14db5SEric W. Biederman 
676d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
67777b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
6789043476fSAl Viro 	.d_delete	= proc_sys_delete,
6799043476fSAl Viro 	.d_compare	= proc_sys_compare,
68077b14db5SEric W. Biederman };
68177b14db5SEric W. Biederman 
6821f87f0b5SEric W. Biederman static struct ctl_table *is_branch_in(struct ctl_table *branch,
6831f87f0b5SEric W. Biederman 				      struct ctl_table *table)
6841f87f0b5SEric W. Biederman {
6851f87f0b5SEric W. Biederman 	struct ctl_table *p;
6861f87f0b5SEric W. Biederman 	const char *s = branch->procname;
6871f87f0b5SEric W. Biederman 
6881f87f0b5SEric W. Biederman 	/* branch should have named subdirectory as its first element */
6891f87f0b5SEric W. Biederman 	if (!s || !branch->child)
6901f87f0b5SEric W. Biederman 		return NULL;
6911f87f0b5SEric W. Biederman 
6921f87f0b5SEric W. Biederman 	/* ... and nothing else */
6931f87f0b5SEric W. Biederman 	if (branch[1].procname)
6941f87f0b5SEric W. Biederman 		return NULL;
6951f87f0b5SEric W. Biederman 
6961f87f0b5SEric W. Biederman 	/* table should contain subdirectory with the same name */
6971f87f0b5SEric W. Biederman 	for (p = table; p->procname; p++) {
6981f87f0b5SEric W. Biederman 		if (!p->child)
6991f87f0b5SEric W. Biederman 			continue;
7001f87f0b5SEric W. Biederman 		if (p->procname && strcmp(p->procname, s) == 0)
7011f87f0b5SEric W. Biederman 			return p;
7021f87f0b5SEric W. Biederman 	}
7031f87f0b5SEric W. Biederman 	return NULL;
7041f87f0b5SEric W. Biederman }
7051f87f0b5SEric W. Biederman 
7061f87f0b5SEric W. Biederman /* see if attaching q to p would be an improvement */
7071f87f0b5SEric W. Biederman static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
7081f87f0b5SEric W. Biederman {
7091f87f0b5SEric W. Biederman 	struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
7101f87f0b5SEric W. Biederman 	struct ctl_table *next;
7111f87f0b5SEric W. Biederman 	int is_better = 0;
7121f87f0b5SEric W. Biederman 	int not_in_parent = !p->attached_by;
7131f87f0b5SEric W. Biederman 
7141f87f0b5SEric W. Biederman 	while ((next = is_branch_in(by, to)) != NULL) {
7151f87f0b5SEric W. Biederman 		if (by == q->attached_by)
7161f87f0b5SEric W. Biederman 			is_better = 1;
7171f87f0b5SEric W. Biederman 		if (to == p->attached_by)
7181f87f0b5SEric W. Biederman 			not_in_parent = 1;
7191f87f0b5SEric W. Biederman 		by = by->child;
7201f87f0b5SEric W. Biederman 		to = next->child;
7211f87f0b5SEric W. Biederman 	}
7221f87f0b5SEric W. Biederman 
7231f87f0b5SEric W. Biederman 	if (is_better && not_in_parent) {
7241f87f0b5SEric W. Biederman 		q->attached_by = by;
7251f87f0b5SEric W. Biederman 		q->attached_to = to;
7261f87f0b5SEric W. Biederman 		q->parent = p;
7271f87f0b5SEric W. Biederman 	}
7281f87f0b5SEric W. Biederman }
7291f87f0b5SEric W. Biederman 
7301f87f0b5SEric W. Biederman #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
7311f87f0b5SEric W. Biederman static int sysctl_depth(struct ctl_table *table)
7321f87f0b5SEric W. Biederman {
7331f87f0b5SEric W. Biederman 	struct ctl_table *tmp;
7341f87f0b5SEric W. Biederman 	int depth;
7351f87f0b5SEric W. Biederman 
7361f87f0b5SEric W. Biederman 	depth = 0;
7371f87f0b5SEric W. Biederman 	for (tmp = table; tmp->parent; tmp = tmp->parent)
7381f87f0b5SEric W. Biederman 		depth++;
7391f87f0b5SEric W. Biederman 
7401f87f0b5SEric W. Biederman 	return depth;
7411f87f0b5SEric W. Biederman }
7421f87f0b5SEric W. Biederman 
7431f87f0b5SEric W. Biederman static struct ctl_table *sysctl_parent(struct ctl_table *table, int n)
7441f87f0b5SEric W. Biederman {
7451f87f0b5SEric W. Biederman 	int i;
7461f87f0b5SEric W. Biederman 
7471f87f0b5SEric W. Biederman 	for (i = 0; table && i < n; i++)
7481f87f0b5SEric W. Biederman 		table = table->parent;
7491f87f0b5SEric W. Biederman 
7501f87f0b5SEric W. Biederman 	return table;
7511f87f0b5SEric W. Biederman }
7521f87f0b5SEric W. Biederman 
7531f87f0b5SEric W. Biederman 
7541f87f0b5SEric W. Biederman static void sysctl_print_path(struct ctl_table *table)
7551f87f0b5SEric W. Biederman {
7561f87f0b5SEric W. Biederman 	struct ctl_table *tmp;
7571f87f0b5SEric W. Biederman 	int depth, i;
7581f87f0b5SEric W. Biederman 	depth = sysctl_depth(table);
7591f87f0b5SEric W. Biederman 	if (table->procname) {
7601f87f0b5SEric W. Biederman 		for (i = depth; i >= 0; i--) {
7611f87f0b5SEric W. Biederman 			tmp = sysctl_parent(table, i);
7621f87f0b5SEric W. Biederman 			printk("/%s", tmp->procname?tmp->procname:"");
7631f87f0b5SEric W. Biederman 		}
7641f87f0b5SEric W. Biederman 	}
7651f87f0b5SEric W. Biederman 	printk(" ");
7661f87f0b5SEric W. Biederman }
7671f87f0b5SEric W. Biederman 
7681f87f0b5SEric W. Biederman static struct ctl_table *sysctl_check_lookup(struct nsproxy *namespaces,
7691f87f0b5SEric W. Biederman 						struct ctl_table *table)
7701f87f0b5SEric W. Biederman {
7711f87f0b5SEric W. Biederman 	struct ctl_table_header *head;
7721f87f0b5SEric W. Biederman 	struct ctl_table *ref, *test;
7731f87f0b5SEric W. Biederman 	int depth, cur_depth;
7741f87f0b5SEric W. Biederman 
7751f87f0b5SEric W. Biederman 	depth = sysctl_depth(table);
7761f87f0b5SEric W. Biederman 
7771f87f0b5SEric W. Biederman 	for (head = __sysctl_head_next(namespaces, NULL); head;
7781f87f0b5SEric W. Biederman 	     head = __sysctl_head_next(namespaces, head)) {
7791f87f0b5SEric W. Biederman 		cur_depth = depth;
7801f87f0b5SEric W. Biederman 		ref = head->ctl_table;
7811f87f0b5SEric W. Biederman repeat:
7821f87f0b5SEric W. Biederman 		test = sysctl_parent(table, cur_depth);
7831f87f0b5SEric W. Biederman 		for (; ref->procname; ref++) {
7841f87f0b5SEric W. Biederman 			int match = 0;
7851f87f0b5SEric W. Biederman 			if (cur_depth && !ref->child)
7861f87f0b5SEric W. Biederman 				continue;
7871f87f0b5SEric W. Biederman 
7881f87f0b5SEric W. Biederman 			if (test->procname && ref->procname &&
7891f87f0b5SEric W. Biederman 			    (strcmp(test->procname, ref->procname) == 0))
7901f87f0b5SEric W. Biederman 					match++;
7911f87f0b5SEric W. Biederman 
7921f87f0b5SEric W. Biederman 			if (match) {
7931f87f0b5SEric W. Biederman 				if (cur_depth != 0) {
7941f87f0b5SEric W. Biederman 					cur_depth--;
7951f87f0b5SEric W. Biederman 					ref = ref->child;
7961f87f0b5SEric W. Biederman 					goto repeat;
7971f87f0b5SEric W. Biederman 				}
7981f87f0b5SEric W. Biederman 				goto out;
7991f87f0b5SEric W. Biederman 			}
8001f87f0b5SEric W. Biederman 		}
8011f87f0b5SEric W. Biederman 	}
8021f87f0b5SEric W. Biederman 	ref = NULL;
8031f87f0b5SEric W. Biederman out:
8041f87f0b5SEric W. Biederman 	sysctl_head_finish(head);
8051f87f0b5SEric W. Biederman 	return ref;
8061f87f0b5SEric W. Biederman }
8071f87f0b5SEric W. Biederman 
8081f87f0b5SEric W. Biederman static void set_fail(const char **fail, struct ctl_table *table, const char *str)
8091f87f0b5SEric W. Biederman {
8101f87f0b5SEric W. Biederman 	if (*fail) {
8111f87f0b5SEric W. Biederman 		printk(KERN_ERR "sysctl table check failed: ");
8121f87f0b5SEric W. Biederman 		sysctl_print_path(table);
8131f87f0b5SEric W. Biederman 		printk(" %s\n", *fail);
8141f87f0b5SEric W. Biederman 		dump_stack();
8151f87f0b5SEric W. Biederman 	}
8161f87f0b5SEric W. Biederman 	*fail = str;
8171f87f0b5SEric W. Biederman }
8181f87f0b5SEric W. Biederman 
8191f87f0b5SEric W. Biederman static void sysctl_check_leaf(struct nsproxy *namespaces,
8201f87f0b5SEric W. Biederman 				struct ctl_table *table, const char **fail)
8211f87f0b5SEric W. Biederman {
8221f87f0b5SEric W. Biederman 	struct ctl_table *ref;
8231f87f0b5SEric W. Biederman 
8241f87f0b5SEric W. Biederman 	ref = sysctl_check_lookup(namespaces, table);
8251f87f0b5SEric W. Biederman 	if (ref && (ref != table))
8261f87f0b5SEric W. Biederman 		set_fail(fail, table, "Sysctl already exists");
8271f87f0b5SEric W. Biederman }
8281f87f0b5SEric W. Biederman 
8291f87f0b5SEric W. Biederman static int sysctl_check_table(struct nsproxy *namespaces, struct ctl_table *table)
8301f87f0b5SEric W. Biederman {
8311f87f0b5SEric W. Biederman 	int error = 0;
8321f87f0b5SEric W. Biederman 	for (; table->procname; table++) {
8331f87f0b5SEric W. Biederman 		const char *fail = NULL;
8341f87f0b5SEric W. Biederman 
8351f87f0b5SEric W. Biederman 		if (table->parent) {
8361f87f0b5SEric W. Biederman 			if (!table->parent->procname)
8371f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Parent without procname");
8381f87f0b5SEric W. Biederman 		}
8391f87f0b5SEric W. Biederman 		if (table->child) {
8401f87f0b5SEric W. Biederman 			if (table->data)
8411f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Directory with data?");
8421f87f0b5SEric W. Biederman 			if (table->maxlen)
8431f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Directory with maxlen?");
8441f87f0b5SEric W. Biederman 			if ((table->mode & (S_IRUGO|S_IXUGO)) != table->mode)
8451f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Writable sysctl directory");
8461f87f0b5SEric W. Biederman 			if (table->proc_handler)
8471f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Directory with proc_handler");
8481f87f0b5SEric W. Biederman 			if (table->extra1)
8491f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Directory with extra1");
8501f87f0b5SEric W. Biederman 			if (table->extra2)
8511f87f0b5SEric W. Biederman 				set_fail(&fail, table, "Directory with extra2");
8521f87f0b5SEric W. Biederman 		} else {
8531f87f0b5SEric W. Biederman 			if ((table->proc_handler == proc_dostring) ||
8541f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_dointvec) ||
8551f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_dointvec_minmax) ||
8561f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_dointvec_jiffies) ||
8571f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
8581f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_dointvec_ms_jiffies) ||
8591f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_doulongvec_minmax) ||
8601f87f0b5SEric W. Biederman 			    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
8611f87f0b5SEric W. Biederman 				if (!table->data)
8621f87f0b5SEric W. Biederman 					set_fail(&fail, table, "No data");
8631f87f0b5SEric W. Biederman 				if (!table->maxlen)
8641f87f0b5SEric W. Biederman 					set_fail(&fail, table, "No maxlen");
8651f87f0b5SEric W. Biederman 			}
8661f87f0b5SEric W. Biederman #ifdef CONFIG_PROC_SYSCTL
8671f87f0b5SEric W. Biederman 			if (!table->proc_handler)
8681f87f0b5SEric W. Biederman 				set_fail(&fail, table, "No proc_handler");
8691f87f0b5SEric W. Biederman #endif
8701f87f0b5SEric W. Biederman 			sysctl_check_leaf(namespaces, table, &fail);
8711f87f0b5SEric W. Biederman 		}
8721f87f0b5SEric W. Biederman 		if (table->mode > 0777)
8731f87f0b5SEric W. Biederman 			set_fail(&fail, table, "bogus .mode");
8741f87f0b5SEric W. Biederman 		if (fail) {
8751f87f0b5SEric W. Biederman 			set_fail(&fail, table, NULL);
8761f87f0b5SEric W. Biederman 			error = -EINVAL;
8771f87f0b5SEric W. Biederman 		}
8781f87f0b5SEric W. Biederman 		if (table->child)
8791f87f0b5SEric W. Biederman 			error |= sysctl_check_table(namespaces, table->child);
8801f87f0b5SEric W. Biederman 	}
8811f87f0b5SEric W. Biederman 	return error;
8821f87f0b5SEric W. Biederman }
8831f87f0b5SEric W. Biederman #endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
8841f87f0b5SEric W. Biederman 
8851f87f0b5SEric W. Biederman /**
8861f87f0b5SEric W. Biederman  * __register_sysctl_paths - register a sysctl hierarchy
8871f87f0b5SEric W. Biederman  * @root: List of sysctl headers to register on
8881f87f0b5SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
8891f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
8901f87f0b5SEric W. Biederman  * @table: the top-level table structure
8911f87f0b5SEric W. Biederman  *
8921f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
8931f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
8941f87f0b5SEric W. Biederman  *
8951f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
8961f87f0b5SEric W. Biederman  *
8971f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
8981f87f0b5SEric W. Biederman  *            enter a sysctl file
8991f87f0b5SEric W. Biederman  *
9001f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
9011f87f0b5SEric W. Biederman  *
9021f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
9031f87f0b5SEric W. Biederman  *
9041f87f0b5SEric W. Biederman  * mode - the file permissions for the /proc/sys file, and for sysctl(2)
9051f87f0b5SEric W. Biederman  *
9061f87f0b5SEric W. Biederman  * child - a pointer to the child sysctl table if this entry is a directory, or
9071f87f0b5SEric W. Biederman  *         %NULL.
9081f87f0b5SEric W. Biederman  *
9091f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
9101f87f0b5SEric W. Biederman  *
9111f87f0b5SEric W. Biederman  * de - for internal use by the sysctl routines
9121f87f0b5SEric W. Biederman  *
9131f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
9141f87f0b5SEric W. Biederman  *
9151f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
9161f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
9171f87f0b5SEric W. Biederman  *
9181f87f0b5SEric W. Biederman  * sysctl(2) can automatically manage read and write requests through
9191f87f0b5SEric W. Biederman  * the sysctl table.  The data and maxlen fields of the ctl_table
9201f87f0b5SEric W. Biederman  * struct enable minimal validation of the values being written to be
9211f87f0b5SEric W. Biederman  * performed, and the mode field allows minimal authentication.
9221f87f0b5SEric W. Biederman  *
9231f87f0b5SEric W. Biederman  * There must be a proc_handler routine for any terminal nodes
9241f87f0b5SEric W. Biederman  * mirrored under /proc/sys (non-terminals are handled by a built-in
9251f87f0b5SEric W. Biederman  * directory handler).  Several default handlers are available to
9261f87f0b5SEric W. Biederman  * cover common cases -
9271f87f0b5SEric W. Biederman  *
9281f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
9291f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
9301f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
9311f87f0b5SEric W. Biederman  *
9321f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
9331f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
9341f87f0b5SEric W. Biederman  *
9351f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
9361f87f0b5SEric W. Biederman  * to the table header on success.
9371f87f0b5SEric W. Biederman  */
9381f87f0b5SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
9391f87f0b5SEric W. Biederman 	struct ctl_table_root *root,
9401f87f0b5SEric W. Biederman 	struct nsproxy *namespaces,
9411f87f0b5SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
9421f87f0b5SEric W. Biederman {
9431f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
9441f87f0b5SEric W. Biederman 	struct ctl_table *new, **prevp;
9451f87f0b5SEric W. Biederman 	unsigned int n, npath;
9461f87f0b5SEric W. Biederman 	struct ctl_table_set *set;
9471f87f0b5SEric W. Biederman 
9481f87f0b5SEric W. Biederman 	/* Count the path components */
9491f87f0b5SEric W. Biederman 	for (npath = 0; path[npath].procname; ++npath)
9501f87f0b5SEric W. Biederman 		;
9511f87f0b5SEric W. Biederman 
9521f87f0b5SEric W. Biederman 	/*
9531f87f0b5SEric W. Biederman 	 * For each path component, allocate a 2-element ctl_table array.
9541f87f0b5SEric W. Biederman 	 * The first array element will be filled with the sysctl entry
9551f87f0b5SEric W. Biederman 	 * for this, the second will be the sentinel (procname == 0).
9561f87f0b5SEric W. Biederman 	 *
9571f87f0b5SEric W. Biederman 	 * We allocate everything in one go so that we don't have to
9581f87f0b5SEric W. Biederman 	 * worry about freeing additional memory in unregister_sysctl_table.
9591f87f0b5SEric W. Biederman 	 */
9601f87f0b5SEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
9611f87f0b5SEric W. Biederman 			 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
9621f87f0b5SEric W. Biederman 	if (!header)
9631f87f0b5SEric W. Biederman 		return NULL;
9641f87f0b5SEric W. Biederman 
9651f87f0b5SEric W. Biederman 	new = (struct ctl_table *) (header + 1);
9661f87f0b5SEric W. Biederman 
9671f87f0b5SEric W. Biederman 	/* Now connect the dots */
9681f87f0b5SEric W. Biederman 	prevp = &header->ctl_table;
9691f87f0b5SEric W. Biederman 	for (n = 0; n < npath; ++n, ++path) {
9701f87f0b5SEric W. Biederman 		/* Copy the procname */
9711f87f0b5SEric W. Biederman 		new->procname = path->procname;
9721f87f0b5SEric W. Biederman 		new->mode     = 0555;
9731f87f0b5SEric W. Biederman 
9741f87f0b5SEric W. Biederman 		*prevp = new;
9751f87f0b5SEric W. Biederman 		prevp = &new->child;
9761f87f0b5SEric W. Biederman 
9771f87f0b5SEric W. Biederman 		new += 2;
9781f87f0b5SEric W. Biederman 	}
9791f87f0b5SEric W. Biederman 	*prevp = table;
9801f87f0b5SEric W. Biederman 	header->ctl_table_arg = table;
9811f87f0b5SEric W. Biederman 
9821f87f0b5SEric W. Biederman 	INIT_LIST_HEAD(&header->ctl_entry);
9831f87f0b5SEric W. Biederman 	header->used = 0;
9841f87f0b5SEric W. Biederman 	header->unregistering = NULL;
9851f87f0b5SEric W. Biederman 	header->root = root;
9861f87f0b5SEric W. Biederman 	sysctl_set_parent(NULL, header->ctl_table);
9871f87f0b5SEric W. Biederman 	header->count = 1;
9881f87f0b5SEric W. Biederman #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
9891f87f0b5SEric W. Biederman 	if (sysctl_check_table(namespaces, header->ctl_table)) {
9901f87f0b5SEric W. Biederman 		kfree(header);
9911f87f0b5SEric W. Biederman 		return NULL;
9921f87f0b5SEric W. Biederman 	}
9931f87f0b5SEric W. Biederman #endif
9941f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9951f87f0b5SEric W. Biederman 	header->set = lookup_header_set(root, namespaces);
9961f87f0b5SEric W. Biederman 	header->attached_by = header->ctl_table;
9971f87f0b5SEric W. Biederman 	header->attached_to = root_table;
9981f87f0b5SEric W. Biederman 	header->parent = &root_table_header;
9991f87f0b5SEric W. Biederman 	for (set = header->set; set; set = set->parent) {
10001f87f0b5SEric W. Biederman 		struct ctl_table_header *p;
10011f87f0b5SEric W. Biederman 		list_for_each_entry(p, &set->list, ctl_entry) {
10021f87f0b5SEric W. Biederman 			if (p->unregistering)
10031f87f0b5SEric W. Biederman 				continue;
10041f87f0b5SEric W. Biederman 			try_attach(p, header);
10051f87f0b5SEric W. Biederman 		}
10061f87f0b5SEric W. Biederman 	}
10071f87f0b5SEric W. Biederman 	header->parent->count++;
10081f87f0b5SEric W. Biederman 	list_add_tail(&header->ctl_entry, &header->set->list);
10091f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
10101f87f0b5SEric W. Biederman 
10111f87f0b5SEric W. Biederman 	return header;
10121f87f0b5SEric W. Biederman }
10131f87f0b5SEric W. Biederman 
10141f87f0b5SEric W. Biederman /**
10151f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
10161f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
10171f87f0b5SEric W. Biederman  * @table: the top-level table structure
10181f87f0b5SEric W. Biederman  *
10191f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
10201f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
10211f87f0b5SEric W. Biederman  *
10221f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
10231f87f0b5SEric W. Biederman  */
10241f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
10251f87f0b5SEric W. Biederman 						struct ctl_table *table)
10261f87f0b5SEric W. Biederman {
10271f87f0b5SEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
10281f87f0b5SEric W. Biederman 					path, table);
10291f87f0b5SEric W. Biederman }
10301f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
10311f87f0b5SEric W. Biederman 
10321f87f0b5SEric W. Biederman /**
10331f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
10341f87f0b5SEric W. Biederman  * @table: the top-level table structure
10351f87f0b5SEric W. Biederman  *
10361f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
10371f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
10381f87f0b5SEric W. Biederman  *
10391f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
10401f87f0b5SEric W. Biederman  */
10411f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
10421f87f0b5SEric W. Biederman {
10431f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
10441f87f0b5SEric W. Biederman 
10451f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
10461f87f0b5SEric W. Biederman }
10471f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
10481f87f0b5SEric W. Biederman 
10491f87f0b5SEric W. Biederman /**
10501f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
10511f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
10521f87f0b5SEric W. Biederman  *
10531f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
10541f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
10551f87f0b5SEric W. Biederman  */
10561f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
10571f87f0b5SEric W. Biederman {
10581f87f0b5SEric W. Biederman 	might_sleep();
10591f87f0b5SEric W. Biederman 
10601f87f0b5SEric W. Biederman 	if (header == NULL)
10611f87f0b5SEric W. Biederman 		return;
10621f87f0b5SEric W. Biederman 
10631f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
10641f87f0b5SEric W. Biederman 	start_unregistering(header);
10651f87f0b5SEric W. Biederman 	if (!--header->parent->count) {
10661f87f0b5SEric W. Biederman 		WARN_ON(1);
10671f87f0b5SEric W. Biederman 		kfree_rcu(header->parent, rcu);
10681f87f0b5SEric W. Biederman 	}
10691f87f0b5SEric W. Biederman 	if (!--header->count)
10701f87f0b5SEric W. Biederman 		kfree_rcu(header, rcu);
10711f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
10721f87f0b5SEric W. Biederman }
10731f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
10741f87f0b5SEric W. Biederman 
10751f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p,
10761f87f0b5SEric W. Biederman 	struct ctl_table_set *parent,
10771f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
10781f87f0b5SEric W. Biederman {
10791f87f0b5SEric W. Biederman 	INIT_LIST_HEAD(&p->list);
10801f87f0b5SEric W. Biederman 	p->parent = parent ? parent : &sysctl_table_root.default_set;
10811f87f0b5SEric W. Biederman 	p->is_seen = is_seen;
10821f87f0b5SEric W. Biederman }
10831f87f0b5SEric W. Biederman 
10841f87f0b5SEric W. Biederman 
10851e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
108677b14db5SEric W. Biederman {
1087e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1088e1675231SAlexey Dobriyan 
108977b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
10909043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
10919043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
109277b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1093de4e83bdSEric W. Biederman 
1094de4e83bdSEric W. Biederman 	return sysctl_init();
109577b14db5SEric W. Biederman }
1096