xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision a194558e)
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 
28a194558eSEric W. Biederman static struct ctl_table root_table[] = {
29a194558eSEric W. Biederman 	{
30a194558eSEric W. Biederman 		.procname = "",
31a194558eSEric W. Biederman 		.mode = S_IRUGO|S_IXUGO,
32a194558eSEric W. Biederman 		.child = &root_table[1],
33a194558eSEric W. Biederman 	},
34a194558eSEric W. Biederman 	{ }
35a194558eSEric W. Biederman };
361f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root;
371f87f0b5SEric W. Biederman static struct ctl_table_header root_table_header = {
381f87f0b5SEric W. Biederman 	{{.count = 1,
39938aaa4fSEric W. Biederman 	  .nreg = 1,
401f87f0b5SEric W. Biederman 	  .ctl_table = root_table,
411f87f0b5SEric W. Biederman 	  .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
421f87f0b5SEric W. Biederman 	.root = &sysctl_table_root,
431f87f0b5SEric W. Biederman 	.set = &sysctl_table_root.default_set,
441f87f0b5SEric W. Biederman };
451f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root = {
461f87f0b5SEric W. Biederman 	.root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
471f87f0b5SEric W. Biederman 	.default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
481f87f0b5SEric W. Biederman };
491f87f0b5SEric W. Biederman 
501f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
511f87f0b5SEric W. Biederman 
52e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
53e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
54e0d04529SEric W. Biederman 	struct ctl_table *table)
55e0d04529SEric W. Biederman {
56e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
57e0d04529SEric W. Biederman 	INIT_LIST_HEAD(&head->ctl_entry);
58e0d04529SEric W. Biederman 	head->used = 0;
59e0d04529SEric W. Biederman 	head->count = 1;
60e0d04529SEric W. Biederman 	head->nreg = 1;
61e0d04529SEric W. Biederman 	head->unregistering = NULL;
62e0d04529SEric W. Biederman 	head->root = root;
63e0d04529SEric W. Biederman 	head->set = set;
64e0d04529SEric W. Biederman 	head->parent = NULL;
65e0d04529SEric W. Biederman }
66e0d04529SEric W. Biederman 
678425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
688425d6aaSEric W. Biederman {
698425d6aaSEric W. Biederman 	list_del_init(&head->ctl_entry);
708425d6aaSEric W. Biederman }
718425d6aaSEric W. Biederman 
728425d6aaSEric W. Biederman static void insert_header(struct ctl_table_header *header)
738425d6aaSEric W. Biederman {
748425d6aaSEric W. Biederman 	header->parent->count++;
758425d6aaSEric W. Biederman 	list_add_tail(&header->ctl_entry, &header->set->list);
768425d6aaSEric W. Biederman }
778425d6aaSEric W. Biederman 
781f87f0b5SEric W. Biederman /* called under sysctl_lock */
791f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
801f87f0b5SEric W. Biederman {
811f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
821f87f0b5SEric W. Biederman 		return 0;
831f87f0b5SEric W. Biederman 	p->used++;
841f87f0b5SEric W. Biederman 	return 1;
851f87f0b5SEric W. Biederman }
861f87f0b5SEric W. Biederman 
871f87f0b5SEric W. Biederman /* called under sysctl_lock */
881f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
891f87f0b5SEric W. Biederman {
901f87f0b5SEric W. Biederman 	if (!--p->used)
911f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
921f87f0b5SEric W. Biederman 			complete(p->unregistering);
931f87f0b5SEric W. Biederman }
941f87f0b5SEric W. Biederman 
951f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
961f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
971f87f0b5SEric W. Biederman {
981f87f0b5SEric W. Biederman 	/*
991f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
1001f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
1011f87f0b5SEric W. Biederman 	 */
1021f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
1031f87f0b5SEric W. Biederman 		struct completion wait;
1041f87f0b5SEric W. Biederman 		init_completion(&wait);
1051f87f0b5SEric W. Biederman 		p->unregistering = &wait;
1061f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
1071f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
1081f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
1091f87f0b5SEric W. Biederman 	} else {
1101f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
1111f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
1121f87f0b5SEric W. Biederman 	}
1131f87f0b5SEric W. Biederman 	/*
1141f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
1151f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
1161f87f0b5SEric W. Biederman 	 */
1178425d6aaSEric W. Biederman 	erase_header(p);
1181f87f0b5SEric W. Biederman }
1191f87f0b5SEric W. Biederman 
1201f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
1211f87f0b5SEric W. Biederman {
1221f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1231f87f0b5SEric W. Biederman 	head->count++;
1241f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1251f87f0b5SEric W. Biederman }
1261f87f0b5SEric W. Biederman 
1271f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
1281f87f0b5SEric W. Biederman {
1291f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1301f87f0b5SEric W. Biederman 	if (!--head->count)
1311f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
1321f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1331f87f0b5SEric W. Biederman }
1341f87f0b5SEric W. Biederman 
1351f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1361f87f0b5SEric W. Biederman {
1371f87f0b5SEric W. Biederman 	if (!head)
1381f87f0b5SEric W. Biederman 		BUG();
1391f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1401f87f0b5SEric W. Biederman 	if (!use_table(head))
1411f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
1421f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1431f87f0b5SEric W. Biederman 	return head;
1441f87f0b5SEric W. Biederman }
1451f87f0b5SEric W. Biederman 
1461f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
1471f87f0b5SEric W. Biederman {
1481f87f0b5SEric W. Biederman 	if (!head)
1491f87f0b5SEric W. Biederman 		return;
1501f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1511f87f0b5SEric W. Biederman 	unuse_table(head);
1521f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1531f87f0b5SEric W. Biederman }
1541f87f0b5SEric W. Biederman 
1551f87f0b5SEric W. Biederman static struct ctl_table_set *
1561f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1571f87f0b5SEric W. Biederman {
1581f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
1591f87f0b5SEric W. Biederman 	if (root->lookup)
1601f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
1611f87f0b5SEric W. Biederman 	return set;
1621f87f0b5SEric W. Biederman }
1631f87f0b5SEric W. Biederman 
1641f87f0b5SEric W. Biederman static struct list_head *
1651f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1661f87f0b5SEric W. Biederman {
1671f87f0b5SEric W. Biederman 	struct ctl_table_set *set = lookup_header_set(root, namespaces);
1681f87f0b5SEric W. Biederman 	return &set->list;
1691f87f0b5SEric W. Biederman }
1701f87f0b5SEric W. Biederman 
1711f87f0b5SEric W. Biederman static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1721f87f0b5SEric W. Biederman 						struct ctl_table_header *prev)
1731f87f0b5SEric W. Biederman {
1741f87f0b5SEric W. Biederman 	struct ctl_table_root *root;
1751f87f0b5SEric W. Biederman 	struct list_head *header_list;
1761f87f0b5SEric W. Biederman 	struct ctl_table_header *head;
1771f87f0b5SEric W. Biederman 	struct list_head *tmp;
1781f87f0b5SEric W. Biederman 
1791f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1801f87f0b5SEric W. Biederman 	if (prev) {
1811f87f0b5SEric W. Biederman 		head = prev;
1821f87f0b5SEric W. Biederman 		tmp = &prev->ctl_entry;
1831f87f0b5SEric W. Biederman 		unuse_table(prev);
1841f87f0b5SEric W. Biederman 		goto next;
1851f87f0b5SEric W. Biederman 	}
1861f87f0b5SEric W. Biederman 	tmp = &root_table_header.ctl_entry;
1871f87f0b5SEric W. Biederman 	for (;;) {
1881f87f0b5SEric W. Biederman 		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1891f87f0b5SEric W. Biederman 
1901f87f0b5SEric W. Biederman 		if (!use_table(head))
1911f87f0b5SEric W. Biederman 			goto next;
1921f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
1931f87f0b5SEric W. Biederman 		return head;
1941f87f0b5SEric W. Biederman 	next:
1951f87f0b5SEric W. Biederman 		root = head->root;
1961f87f0b5SEric W. Biederman 		tmp = tmp->next;
1971f87f0b5SEric W. Biederman 		header_list = lookup_header_list(root, namespaces);
1981f87f0b5SEric W. Biederman 		if (tmp != header_list)
1991f87f0b5SEric W. Biederman 			continue;
2001f87f0b5SEric W. Biederman 
2011f87f0b5SEric W. Biederman 		do {
2021f87f0b5SEric W. Biederman 			root = list_entry(root->root_list.next,
2031f87f0b5SEric W. Biederman 					struct ctl_table_root, root_list);
2041f87f0b5SEric W. Biederman 			if (root == &sysctl_table_root)
2051f87f0b5SEric W. Biederman 				goto out;
2061f87f0b5SEric W. Biederman 			header_list = lookup_header_list(root, namespaces);
2071f87f0b5SEric W. Biederman 		} while (list_empty(header_list));
2081f87f0b5SEric W. Biederman 		tmp = header_list->next;
2091f87f0b5SEric W. Biederman 	}
2101f87f0b5SEric W. Biederman out:
2111f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2121f87f0b5SEric W. Biederman 	return NULL;
2131f87f0b5SEric W. Biederman }
2141f87f0b5SEric W. Biederman 
2151f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
2161f87f0b5SEric W. Biederman {
2171f87f0b5SEric W. Biederman 	return __sysctl_head_next(current->nsproxy, prev);
2181f87f0b5SEric W. Biederman }
2191f87f0b5SEric W. Biederman 
2201f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
2211f87f0b5SEric W. Biederman {
2221f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2231f87f0b5SEric W. Biederman 	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
2241f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2251f87f0b5SEric W. Biederman }
2261f87f0b5SEric W. Biederman 
2271f87f0b5SEric W. Biederman /*
2281f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
2291f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
2301f87f0b5SEric W. Biederman  */
2311f87f0b5SEric W. Biederman 
2321f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
2331f87f0b5SEric W. Biederman {
2341f87f0b5SEric W. Biederman 	if (!current_euid())
2351f87f0b5SEric W. Biederman 		mode >>= 6;
2361f87f0b5SEric W. Biederman 	else if (in_egroup_p(0))
2371f87f0b5SEric W. Biederman 		mode >>= 3;
2381f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
2391f87f0b5SEric W. Biederman 		return 0;
2401f87f0b5SEric W. Biederman 	return -EACCES;
2411f87f0b5SEric W. Biederman }
2421f87f0b5SEric W. Biederman 
2431f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
2441f87f0b5SEric W. Biederman {
2451f87f0b5SEric W. Biederman 	int mode;
2461f87f0b5SEric W. Biederman 
2471f87f0b5SEric W. Biederman 	if (root->permissions)
2481f87f0b5SEric W. Biederman 		mode = root->permissions(root, current->nsproxy, table);
2491f87f0b5SEric W. Biederman 	else
2501f87f0b5SEric W. Biederman 		mode = table->mode;
2511f87f0b5SEric W. Biederman 
2521f87f0b5SEric W. Biederman 	return test_perm(mode, op);
2531f87f0b5SEric W. Biederman }
2541f87f0b5SEric W. Biederman 
2559043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
2569043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
25777b14db5SEric W. Biederman {
25877b14db5SEric W. Biederman 	struct inode *inode;
2599043476fSAl Viro 	struct proc_inode *ei;
26077b14db5SEric W. Biederman 
2619043476fSAl Viro 	inode = new_inode(sb);
26277b14db5SEric W. Biederman 	if (!inode)
26377b14db5SEric W. Biederman 		goto out;
26477b14db5SEric W. Biederman 
26585fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
26685fe4025SChristoph Hellwig 
2679043476fSAl Viro 	sysctl_head_get(head);
26877b14db5SEric W. Biederman 	ei = PROC_I(inode);
2699043476fSAl Viro 	ei->sysctl = head;
2709043476fSAl Viro 	ei->sysctl_entry = table;
2719043476fSAl Viro 
27277b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2739043476fSAl Viro 	inode->i_mode = table->mode;
2749043476fSAl Viro 	if (!table->child) {
2759043476fSAl Viro 		inode->i_mode |= S_IFREG;
27677b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
27777b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
2789043476fSAl Viro 	} else {
2799043476fSAl Viro 		inode->i_mode |= S_IFDIR;
2809043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
2819043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
2829043476fSAl Viro 	}
28377b14db5SEric W. Biederman out:
28477b14db5SEric W. Biederman 	return inode;
28577b14db5SEric W. Biederman }
28677b14db5SEric W. Biederman 
2879043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
28877b14db5SEric W. Biederman {
2892315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
29036885d7bSLucas De Marchi 		if (strlen(p->procname) != name->len)
29177b14db5SEric W. Biederman 			continue;
29277b14db5SEric W. Biederman 
29336885d7bSLucas De Marchi 		if (memcmp(p->procname, name->name, name->len) != 0)
29477b14db5SEric W. Biederman 			continue;
29577b14db5SEric W. Biederman 
29677b14db5SEric W. Biederman 		/* I have a match */
2979043476fSAl Viro 		return p;
29877b14db5SEric W. Biederman 	}
29977b14db5SEric W. Biederman 	return NULL;
30077b14db5SEric W. Biederman }
30177b14db5SEric W. Biederman 
30281324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
30377b14db5SEric W. Biederman {
3043cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
3053cc3e046SEric W. Biederman 	if (!head)
3063cc3e046SEric W. Biederman 		head = &root_table_header;
3073cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
30877b14db5SEric W. Biederman }
30977b14db5SEric W. Biederman 
31077b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
31177b14db5SEric W. Biederman 					struct nameidata *nd)
31277b14db5SEric W. Biederman {
3139043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
3149043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
3159043476fSAl Viro 	struct ctl_table_header *h = NULL;
3169043476fSAl Viro 	struct qstr *name = &dentry->d_name;
3179043476fSAl Viro 	struct ctl_table *p;
31877b14db5SEric W. Biederman 	struct inode *inode;
3199043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
32077b14db5SEric W. Biederman 
3219043476fSAl Viro 	if (IS_ERR(head))
3229043476fSAl Viro 		return ERR_CAST(head);
3239043476fSAl Viro 
3249043476fSAl Viro 	if (table && !table->child) {
3259043476fSAl Viro 		WARN_ON(1);
3269043476fSAl Viro 		goto out;
3279043476fSAl Viro 	}
3289043476fSAl Viro 
329a194558eSEric W. Biederman 	table = table ? table->child : &head->ctl_table[1];
3309043476fSAl Viro 
3319043476fSAl Viro 	p = find_in_table(table, name);
3329043476fSAl Viro 	if (!p) {
3339043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
3349043476fSAl Viro 			if (h->attached_to != table)
3359043476fSAl Viro 				continue;
3369043476fSAl Viro 			p = find_in_table(h->attached_by, name);
3379043476fSAl Viro 			if (p)
3389043476fSAl Viro 				break;
3399043476fSAl Viro 		}
3409043476fSAl Viro 	}
3419043476fSAl Viro 
3429043476fSAl Viro 	if (!p)
34377b14db5SEric W. Biederman 		goto out;
34477b14db5SEric W. Biederman 
34577b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
3469043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
3479043476fSAl Viro 	if (h)
3489043476fSAl Viro 		sysctl_head_finish(h);
3499043476fSAl Viro 
35077b14db5SEric W. Biederman 	if (!inode)
35177b14db5SEric W. Biederman 		goto out;
35277b14db5SEric W. Biederman 
35377b14db5SEric W. Biederman 	err = NULL;
354fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
35577b14db5SEric W. Biederman 	d_add(dentry, inode);
35677b14db5SEric W. Biederman 
35777b14db5SEric W. Biederman out:
35877b14db5SEric W. Biederman 	sysctl_head_finish(head);
35977b14db5SEric W. Biederman 	return err;
36077b14db5SEric W. Biederman }
36177b14db5SEric W. Biederman 
3627708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
3637708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
36477b14db5SEric W. Biederman {
3659043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
3669043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3679043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3682a2da53bSDavid Howells 	ssize_t error;
3692a2da53bSDavid Howells 	size_t res;
37077b14db5SEric W. Biederman 
3719043476fSAl Viro 	if (IS_ERR(head))
3729043476fSAl Viro 		return PTR_ERR(head);
37377b14db5SEric W. Biederman 
37477b14db5SEric W. Biederman 	/*
37577b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
37677b14db5SEric W. Biederman 	 * and won't be until we finish.
37777b14db5SEric W. Biederman 	 */
37877b14db5SEric W. Biederman 	error = -EPERM;
379d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
38077b14db5SEric W. Biederman 		goto out;
38177b14db5SEric W. Biederman 
3829043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
3839043476fSAl Viro 	error = -EINVAL;
3849043476fSAl Viro 	if (!table->proc_handler)
3859043476fSAl Viro 		goto out;
3869043476fSAl Viro 
38777b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
38877b14db5SEric W. Biederman 	res = count;
3898d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
39077b14db5SEric W. Biederman 	if (!error)
39177b14db5SEric W. Biederman 		error = res;
39277b14db5SEric W. Biederman out:
39377b14db5SEric W. Biederman 	sysctl_head_finish(head);
39477b14db5SEric W. Biederman 
39577b14db5SEric W. Biederman 	return error;
39677b14db5SEric W. Biederman }
39777b14db5SEric W. Biederman 
3987708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
3997708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
4007708bfb1SPavel Emelyanov {
4017708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
4027708bfb1SPavel Emelyanov }
4037708bfb1SPavel Emelyanov 
40477b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
40577b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
40677b14db5SEric W. Biederman {
4077708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
40877b14db5SEric W. Biederman }
40977b14db5SEric W. Biederman 
410f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
411f1ecf068SLucas De Marchi {
412f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
413f1ecf068SLucas De Marchi 
414f1ecf068SLucas De Marchi 	if (table->poll)
415f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
416f1ecf068SLucas De Marchi 
417f1ecf068SLucas De Marchi 	return 0;
418f1ecf068SLucas De Marchi }
419f1ecf068SLucas De Marchi 
420f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
421f1ecf068SLucas De Marchi {
422f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
423f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
424f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
425f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
426f1ecf068SLucas De Marchi 
427f1ecf068SLucas De Marchi 	if (!table->proc_handler)
428f1ecf068SLucas De Marchi 		goto out;
429f1ecf068SLucas De Marchi 
430f1ecf068SLucas De Marchi 	if (!table->poll)
431f1ecf068SLucas De Marchi 		goto out;
432f1ecf068SLucas De Marchi 
433f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
434f1ecf068SLucas De Marchi 
435f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
436f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
437f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
438f1ecf068SLucas De Marchi 	}
439f1ecf068SLucas De Marchi 
440f1ecf068SLucas De Marchi out:
441f1ecf068SLucas De Marchi 	return ret;
442f1ecf068SLucas De Marchi }
44377b14db5SEric W. Biederman 
44477b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
4459043476fSAl Viro 				filldir_t filldir,
4469043476fSAl Viro 				struct ctl_table_header *head,
4479043476fSAl Viro 				struct ctl_table *table)
44877b14db5SEric W. Biederman {
44977b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
45077b14db5SEric W. Biederman 	struct inode *inode;
45177b14db5SEric W. Biederman 	struct qstr qname;
45277b14db5SEric W. Biederman 	ino_t ino = 0;
45377b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
45477b14db5SEric W. Biederman 
45577b14db5SEric W. Biederman 	qname.name = table->procname;
45677b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
45777b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
45877b14db5SEric W. Biederman 
45977b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
46077b14db5SEric W. Biederman 	if (!child) {
4619043476fSAl Viro 		child = d_alloc(dir, &qname);
4629043476fSAl Viro 		if (child) {
4639043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
4649043476fSAl Viro 			if (!inode) {
4659043476fSAl Viro 				dput(child);
4669043476fSAl Viro 				return -ENOMEM;
4679043476fSAl Viro 			} else {
468fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
4699043476fSAl Viro 				d_add(child, inode);
47077b14db5SEric W. Biederman 			}
4719043476fSAl Viro 		} else {
4729043476fSAl Viro 			return -ENOMEM;
47377b14db5SEric W. Biederman 		}
47477b14db5SEric W. Biederman 	}
47577b14db5SEric W. Biederman 	inode = child->d_inode;
47677b14db5SEric W. Biederman 	ino  = inode->i_ino;
47777b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
47877b14db5SEric W. Biederman 	dput(child);
4799043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
4809043476fSAl Viro }
4819043476fSAl Viro 
4829043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
4839043476fSAl Viro 		unsigned long *pos, struct file *file,
4849043476fSAl Viro 		void *dirent, filldir_t filldir)
4859043476fSAl Viro {
4869043476fSAl Viro 
4872315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
4889043476fSAl Viro 		int res;
4899043476fSAl Viro 
4909043476fSAl Viro 		if (*pos < file->f_pos)
4919043476fSAl Viro 			continue;
4929043476fSAl Viro 
4939043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
4949043476fSAl Viro 		if (res)
4959043476fSAl Viro 			return res;
4969043476fSAl Viro 
4979043476fSAl Viro 		file->f_pos = *pos + 1;
4989043476fSAl Viro 	}
4999043476fSAl Viro 	return 0;
50077b14db5SEric W. Biederman }
50177b14db5SEric W. Biederman 
50277b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
50377b14db5SEric W. Biederman {
5049043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
50577b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
5069043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5079043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5089043476fSAl Viro 	struct ctl_table_header *h = NULL;
50977b14db5SEric W. Biederman 	unsigned long pos;
5109043476fSAl Viro 	int ret = -EINVAL;
51177b14db5SEric W. Biederman 
5129043476fSAl Viro 	if (IS_ERR(head))
5139043476fSAl Viro 		return PTR_ERR(head);
5149043476fSAl Viro 
5159043476fSAl Viro 	if (table && !table->child) {
5169043476fSAl Viro 		WARN_ON(1);
51777b14db5SEric W. Biederman 		goto out;
5189043476fSAl Viro 	}
5199043476fSAl Viro 
520a194558eSEric W. Biederman 	table = table ? table->child : &head->ctl_table[1];
52177b14db5SEric W. Biederman 
52277b14db5SEric W. Biederman 	ret = 0;
52377b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
52477b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
52577b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
52677b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
52777b14db5SEric W. Biederman 			goto out;
52877b14db5SEric W. Biederman 		filp->f_pos++;
52977b14db5SEric W. Biederman 	}
53077b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
53177b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
53277b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
53377b14db5SEric W. Biederman 			goto out;
53477b14db5SEric W. Biederman 		filp->f_pos++;
53577b14db5SEric W. Biederman 	}
53677b14db5SEric W. Biederman 	pos = 2;
53777b14db5SEric W. Biederman 
5389043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
5399043476fSAl Viro 	if (ret)
54077b14db5SEric W. Biederman 		goto out;
5419043476fSAl Viro 
5429043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
5439043476fSAl Viro 		if (h->attached_to != table)
5449043476fSAl Viro 			continue;
5459043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
5469043476fSAl Viro 		if (ret) {
5479043476fSAl Viro 			sysctl_head_finish(h);
5489043476fSAl Viro 			break;
54977b14db5SEric W. Biederman 		}
55077b14db5SEric W. Biederman 	}
55177b14db5SEric W. Biederman 	ret = 1;
55277b14db5SEric W. Biederman out:
55377b14db5SEric W. Biederman 	sysctl_head_finish(head);
55477b14db5SEric W. Biederman 	return ret;
55577b14db5SEric W. Biederman }
55677b14db5SEric W. Biederman 
55710556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
55877b14db5SEric W. Biederman {
55977b14db5SEric W. Biederman 	/*
56077b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
56177b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
56277b14db5SEric W. Biederman 	 */
563f696a365SMiklos Szeredi 	struct ctl_table_header *head;
564f696a365SMiklos Szeredi 	struct ctl_table *table;
56577b14db5SEric W. Biederman 	int error;
56677b14db5SEric W. Biederman 
567f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
568f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
569f696a365SMiklos Szeredi 		return -EACCES;
570f696a365SMiklos Szeredi 
571f696a365SMiklos Szeredi 	head = grab_header(inode);
5729043476fSAl Viro 	if (IS_ERR(head))
5739043476fSAl Viro 		return PTR_ERR(head);
57477b14db5SEric W. Biederman 
575f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
5769043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
5779043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
5789043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
5791fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
5809043476fSAl Viro 
58177b14db5SEric W. Biederman 	sysctl_head_finish(head);
58277b14db5SEric W. Biederman 	return error;
58377b14db5SEric W. Biederman }
58477b14db5SEric W. Biederman 
58577b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
58677b14db5SEric W. Biederman {
58777b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
58877b14db5SEric W. Biederman 	int error;
58977b14db5SEric W. Biederman 
59077b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
59177b14db5SEric W. Biederman 		return -EPERM;
59277b14db5SEric W. Biederman 
59377b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
5941025774cSChristoph Hellwig 	if (error)
59577b14db5SEric W. Biederman 		return error;
5961025774cSChristoph Hellwig 
5971025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
5981025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
5991025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
6001025774cSChristoph Hellwig 		if (error)
6011025774cSChristoph Hellwig 			return error;
6021025774cSChristoph Hellwig 	}
6031025774cSChristoph Hellwig 
6041025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6051025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6061025774cSChristoph Hellwig 	return 0;
60777b14db5SEric W. Biederman }
60877b14db5SEric W. Biederman 
6099043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
6109043476fSAl Viro {
6119043476fSAl Viro 	struct inode *inode = dentry->d_inode;
6129043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
6139043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
6149043476fSAl Viro 
6159043476fSAl Viro 	if (IS_ERR(head))
6169043476fSAl Viro 		return PTR_ERR(head);
6179043476fSAl Viro 
6189043476fSAl Viro 	generic_fillattr(inode, stat);
6199043476fSAl Viro 	if (table)
6209043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
6219043476fSAl Viro 
6229043476fSAl Viro 	sysctl_head_finish(head);
6239043476fSAl Viro 	return 0;
6249043476fSAl Viro }
6259043476fSAl Viro 
62677b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
627f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
628f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
62977b14db5SEric W. Biederman 	.read		= proc_sys_read,
63077b14db5SEric W. Biederman 	.write		= proc_sys_write,
6316038f373SArnd Bergmann 	.llseek		= default_llseek,
6329043476fSAl Viro };
6339043476fSAl Viro 
6349043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
635887df078SPavel Emelyanov 	.read		= generic_read_dir,
63677b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
6373222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
63877b14db5SEric W. Biederman };
63977b14db5SEric W. Biederman 
64003a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
6419043476fSAl Viro 	.permission	= proc_sys_permission,
6429043476fSAl Viro 	.setattr	= proc_sys_setattr,
6439043476fSAl Viro 	.getattr	= proc_sys_getattr,
6449043476fSAl Viro };
6459043476fSAl Viro 
6469043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
64777b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
64877b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
64977b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
6509043476fSAl Viro 	.getattr	= proc_sys_getattr,
65177b14db5SEric W. Biederman };
65277b14db5SEric W. Biederman 
65377b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
65477b14db5SEric W. Biederman {
65534286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
65634286d66SNick Piggin 		return -ECHILD;
6579043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
6589043476fSAl Viro }
6599043476fSAl Viro 
660fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
6619043476fSAl Viro {
6629043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
6639043476fSAl Viro }
6649043476fSAl Viro 
6651f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
6661f87f0b5SEric W. Biederman {
6671f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
6681f87f0b5SEric W. Biederman 	int res;
6691f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
6701f87f0b5SEric W. Biederman 	if (p->unregistering)
6711f87f0b5SEric W. Biederman 		res = 0;
6721f87f0b5SEric W. Biederman 	else if (!set->is_seen)
6731f87f0b5SEric W. Biederman 		res = 1;
6741f87f0b5SEric W. Biederman 	else
6751f87f0b5SEric W. Biederman 		res = set->is_seen(set);
6761f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
6771f87f0b5SEric W. Biederman 	return res;
6781f87f0b5SEric W. Biederman }
6791f87f0b5SEric W. Biederman 
680621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
681621e155aSNick Piggin 		const struct inode *pinode,
682621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
683621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
6849043476fSAl Viro {
685dfef6dcdSAl Viro 	struct ctl_table_header *head;
68631e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
68731e6b01fSNick Piggin 	 * that inode here can be NULL */
688dfef6dcdSAl Viro 	/* AV: can it, indeed? */
68931e6b01fSNick Piggin 	if (!inode)
690dfef6dcdSAl Viro 		return 1;
691621e155aSNick Piggin 	if (name->len != len)
6929043476fSAl Viro 		return 1;
693621e155aSNick Piggin 	if (memcmp(name->name, str, len))
6949043476fSAl Viro 		return 1;
695dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
696dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
69777b14db5SEric W. Biederman }
69877b14db5SEric W. Biederman 
699d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
70077b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
7019043476fSAl Viro 	.d_delete	= proc_sys_delete,
7029043476fSAl Viro 	.d_compare	= proc_sys_compare,
70377b14db5SEric W. Biederman };
70477b14db5SEric W. Biederman 
7051f87f0b5SEric W. Biederman static struct ctl_table *is_branch_in(struct ctl_table *branch,
7061f87f0b5SEric W. Biederman 				      struct ctl_table *table)
7071f87f0b5SEric W. Biederman {
7081f87f0b5SEric W. Biederman 	struct ctl_table *p;
7091f87f0b5SEric W. Biederman 	const char *s = branch->procname;
7101f87f0b5SEric W. Biederman 
7111f87f0b5SEric W. Biederman 	/* branch should have named subdirectory as its first element */
7121f87f0b5SEric W. Biederman 	if (!s || !branch->child)
7131f87f0b5SEric W. Biederman 		return NULL;
7141f87f0b5SEric W. Biederman 
7151f87f0b5SEric W. Biederman 	/* ... and nothing else */
7161f87f0b5SEric W. Biederman 	if (branch[1].procname)
7171f87f0b5SEric W. Biederman 		return NULL;
7181f87f0b5SEric W. Biederman 
7191f87f0b5SEric W. Biederman 	/* table should contain subdirectory with the same name */
7201f87f0b5SEric W. Biederman 	for (p = table; p->procname; p++) {
7211f87f0b5SEric W. Biederman 		if (!p->child)
7221f87f0b5SEric W. Biederman 			continue;
7231f87f0b5SEric W. Biederman 		if (p->procname && strcmp(p->procname, s) == 0)
7241f87f0b5SEric W. Biederman 			return p;
7251f87f0b5SEric W. Biederman 	}
7261f87f0b5SEric W. Biederman 	return NULL;
7271f87f0b5SEric W. Biederman }
7281f87f0b5SEric W. Biederman 
7291f87f0b5SEric W. Biederman /* see if attaching q to p would be an improvement */
7301f87f0b5SEric W. Biederman static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
7311f87f0b5SEric W. Biederman {
7321f87f0b5SEric W. Biederman 	struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
7331f87f0b5SEric W. Biederman 	struct ctl_table *next;
7341f87f0b5SEric W. Biederman 	int is_better = 0;
7351f87f0b5SEric W. Biederman 	int not_in_parent = !p->attached_by;
7361f87f0b5SEric W. Biederman 
7371f87f0b5SEric W. Biederman 	while ((next = is_branch_in(by, to)) != NULL) {
7381f87f0b5SEric W. Biederman 		if (by == q->attached_by)
7391f87f0b5SEric W. Biederman 			is_better = 1;
7401f87f0b5SEric W. Biederman 		if (to == p->attached_by)
7411f87f0b5SEric W. Biederman 			not_in_parent = 1;
7421f87f0b5SEric W. Biederman 		by = by->child;
7431f87f0b5SEric W. Biederman 		to = next->child;
7441f87f0b5SEric W. Biederman 	}
7451f87f0b5SEric W. Biederman 
7461f87f0b5SEric W. Biederman 	if (is_better && not_in_parent) {
7471f87f0b5SEric W. Biederman 		q->attached_by = by;
7481f87f0b5SEric W. Biederman 		q->attached_to = to;
7491f87f0b5SEric W. Biederman 		q->parent = p;
7501f87f0b5SEric W. Biederman 	}
7511f87f0b5SEric W. Biederman }
7521f87f0b5SEric W. Biederman 
7537c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
7541f87f0b5SEric W. Biederman 	struct ctl_table *table)
7551f87f0b5SEric W. Biederman {
7567c60c48fSEric W. Biederman 	struct ctl_table *entry, *test;
7571f87f0b5SEric W. Biederman 	int error = 0;
7581f87f0b5SEric W. Biederman 
7597c60c48fSEric W. Biederman 	for (entry = old; entry->procname; entry++) {
7607c60c48fSEric W. Biederman 		for (test = table; test->procname; test++) {
7617c60c48fSEric W. Biederman 			if (strcmp(entry->procname, test->procname) == 0) {
7627c60c48fSEric W. Biederman 				printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
7637c60c48fSEric W. Biederman 					path, test->procname);
7647c60c48fSEric W. Biederman 				error = -EEXIST;
7651f87f0b5SEric W. Biederman 			}
7667c60c48fSEric W. Biederman 		}
7677c60c48fSEric W. Biederman 	}
7687c60c48fSEric W. Biederman 	return error;
7697c60c48fSEric W. Biederman }
7707c60c48fSEric W. Biederman 
7717c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces,
7727c60c48fSEric W. Biederman 	struct ctl_table_header *header,
7737c60c48fSEric W. Biederman 	const char *path, struct ctl_table *table)
7747c60c48fSEric W. Biederman {
7757c60c48fSEric W. Biederman 	struct ctl_table_root *root;
7767c60c48fSEric W. Biederman 	struct ctl_table_set *set;
7777c60c48fSEric W. Biederman 	struct ctl_table_header *dir_head, *head;
7787c60c48fSEric W. Biederman 	struct ctl_table *dir_table;
7797c60c48fSEric W. Biederman 	int error = 0;
7807c60c48fSEric W. Biederman 
7817c60c48fSEric W. Biederman 	/* No dups if we are the only member of our directory */
7827c60c48fSEric W. Biederman 	if (header->attached_by != table)
7837c60c48fSEric W. Biederman 		return 0;
7847c60c48fSEric W. Biederman 
7857c60c48fSEric W. Biederman 	dir_head = header->parent;
7867c60c48fSEric W. Biederman 	dir_table = header->attached_to;
7877c60c48fSEric W. Biederman 
7887c60c48fSEric W. Biederman 	error = sysctl_check_table_dups(path, dir_table, table);
7897c60c48fSEric W. Biederman 
7907c60c48fSEric W. Biederman 	root = &sysctl_table_root;
7917c60c48fSEric W. Biederman 	do {
7927c60c48fSEric W. Biederman 		set = lookup_header_set(root, namespaces);
7937c60c48fSEric W. Biederman 
7947c60c48fSEric W. Biederman 		list_for_each_entry(head, &set->list, ctl_entry) {
7957c60c48fSEric W. Biederman 			if (head->unregistering)
7967c60c48fSEric W. Biederman 				continue;
7977c60c48fSEric W. Biederman 			if (head->attached_to != dir_table)
7987c60c48fSEric W. Biederman 				continue;
7997c60c48fSEric W. Biederman 			error = sysctl_check_table_dups(path, head->attached_by,
8007c60c48fSEric W. Biederman 							table);
8017c60c48fSEric W. Biederman 		}
8027c60c48fSEric W. Biederman 		root = list_entry(root->root_list.next,
8037c60c48fSEric W. Biederman 				  struct ctl_table_root, root_list);
8047c60c48fSEric W. Biederman 	} while (root != &sysctl_table_root);
8057c60c48fSEric W. Biederman 	return error;
8067c60c48fSEric W. Biederman }
8077c60c48fSEric W. Biederman 
8087c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
8097c60c48fSEric W. Biederman {
8107c60c48fSEric W. Biederman 	struct va_format vaf;
8117c60c48fSEric W. Biederman 	va_list args;
8127c60c48fSEric W. Biederman 
8137c60c48fSEric W. Biederman 	va_start(args, fmt);
8147c60c48fSEric W. Biederman 	vaf.fmt = fmt;
8157c60c48fSEric W. Biederman 	vaf.va = &args;
8167c60c48fSEric W. Biederman 
8177c60c48fSEric W. Biederman 	printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
8187c60c48fSEric W. Biederman 		path, table->procname, &vaf);
8197c60c48fSEric W. Biederman 
8207c60c48fSEric W. Biederman 	va_end(args);
8217c60c48fSEric W. Biederman 	return -EINVAL;
8227c60c48fSEric W. Biederman }
8237c60c48fSEric W. Biederman 
8247c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
8257c60c48fSEric W. Biederman {
8267c60c48fSEric W. Biederman 	int err = 0;
8277c60c48fSEric W. Biederman 	for (; table->procname; table++) {
8287c60c48fSEric W. Biederman 		if (table->child)
8297c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "Not a file");
8307c60c48fSEric W. Biederman 
8311f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
8321f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
8331f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
8341f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
8351f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
8361f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
8371f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
8381f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
8391f87f0b5SEric W. Biederman 			if (!table->data)
8407c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No data");
8411f87f0b5SEric W. Biederman 			if (!table->maxlen)
8427c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No maxlen");
8431f87f0b5SEric W. Biederman 		}
8441f87f0b5SEric W. Biederman 		if (!table->proc_handler)
8457c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "No proc_handler");
8467c60c48fSEric W. Biederman 
8477c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
8487c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "bogus .mode 0%o",
8497c60c48fSEric W. Biederman 				table->mode);
8501f87f0b5SEric W. Biederman 	}
8517c60c48fSEric W. Biederman 	return err;
8521f87f0b5SEric W. Biederman }
8531f87f0b5SEric W. Biederman 
8541f87f0b5SEric W. Biederman /**
855f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
8561f87f0b5SEric W. Biederman  * @root: List of sysctl headers to register on
8571f87f0b5SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
8581f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
8591f87f0b5SEric W. Biederman  * @table: the top-level table structure
8601f87f0b5SEric W. Biederman  *
8611f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
8621f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
8631f87f0b5SEric W. Biederman  *
8641f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
8651f87f0b5SEric W. Biederman  *
8661f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
8671f87f0b5SEric W. Biederman  *            enter a sysctl file
8681f87f0b5SEric W. Biederman  *
8691f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
8701f87f0b5SEric W. Biederman  *
8711f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
8721f87f0b5SEric W. Biederman  *
873f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
8741f87f0b5SEric W. Biederman  *
875f728019bSEric W. Biederman  * child - must be %NULL.
8761f87f0b5SEric W. Biederman  *
8771f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
8781f87f0b5SEric W. Biederman  *
8791f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
8801f87f0b5SEric W. Biederman  *
8811f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
8821f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
8831f87f0b5SEric W. Biederman  *
884f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
885f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
8861f87f0b5SEric W. Biederman  *
8871f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
8881f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
8891f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
8901f87f0b5SEric W. Biederman  *
8911f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
8921f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
8931f87f0b5SEric W. Biederman  *
8941f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
8951f87f0b5SEric W. Biederman  * to the table header on success.
8961f87f0b5SEric W. Biederman  */
8976e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
8981f87f0b5SEric W. Biederman 	struct ctl_table_root *root,
8991f87f0b5SEric W. Biederman 	struct nsproxy *namespaces,
9006e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
9011f87f0b5SEric W. Biederman {
9021f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
9031f87f0b5SEric W. Biederman 	struct ctl_table *new, **prevp;
9046e9d5164SEric W. Biederman 	const char *name, *nextname;
9056e9d5164SEric W. Biederman 	unsigned int npath = 0;
9061f87f0b5SEric W. Biederman 	struct ctl_table_set *set;
907f05e53a7SEric W. Biederman 	size_t path_bytes = 0;
908f05e53a7SEric W. Biederman 	char *new_name;
9091f87f0b5SEric W. Biederman 
9101f87f0b5SEric W. Biederman 	/* Count the path components */
9116e9d5164SEric W. Biederman 	for (name = path; name; name = nextname) {
9126e9d5164SEric W. Biederman 		int namelen;
9136e9d5164SEric W. Biederman 		nextname = strchr(name, '/');
9146e9d5164SEric W. Biederman 		if (nextname) {
9156e9d5164SEric W. Biederman 			namelen = nextname - name;
9166e9d5164SEric W. Biederman 			nextname++;
9176e9d5164SEric W. Biederman 		} else {
9186e9d5164SEric W. Biederman 			namelen = strlen(name);
9196e9d5164SEric W. Biederman 		}
9206e9d5164SEric W. Biederman 		if (namelen == 0)
9216e9d5164SEric W. Biederman 			continue;
9226e9d5164SEric W. Biederman 		path_bytes += namelen + 1;
9236e9d5164SEric W. Biederman 		npath++;
9246e9d5164SEric W. Biederman 	}
9251f87f0b5SEric W. Biederman 
9261f87f0b5SEric W. Biederman 	/*
9271f87f0b5SEric W. Biederman 	 * For each path component, allocate a 2-element ctl_table array.
9281f87f0b5SEric W. Biederman 	 * The first array element will be filled with the sysctl entry
9291f87f0b5SEric W. Biederman 	 * for this, the second will be the sentinel (procname == 0).
9301f87f0b5SEric W. Biederman 	 *
9311f87f0b5SEric W. Biederman 	 * We allocate everything in one go so that we don't have to
9321f87f0b5SEric W. Biederman 	 * worry about freeing additional memory in unregister_sysctl_table.
9331f87f0b5SEric W. Biederman 	 */
934f05e53a7SEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
9351f87f0b5SEric W. Biederman 			 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
9361f87f0b5SEric W. Biederman 	if (!header)
9371f87f0b5SEric W. Biederman 		return NULL;
9381f87f0b5SEric W. Biederman 
9391f87f0b5SEric W. Biederman 	new = (struct ctl_table *) (header + 1);
940f05e53a7SEric W. Biederman 	new_name = (char *)(new + (2 * npath));
9411f87f0b5SEric W. Biederman 
9421f87f0b5SEric W. Biederman 	/* Now connect the dots */
9431f87f0b5SEric W. Biederman 	prevp = &header->ctl_table;
9446e9d5164SEric W. Biederman 	for (name = path; name; name = nextname) {
9456e9d5164SEric W. Biederman 		int namelen;
9466e9d5164SEric W. Biederman 		nextname = strchr(name, '/');
9476e9d5164SEric W. Biederman 		if (nextname) {
9486e9d5164SEric W. Biederman 			namelen = nextname - name;
9496e9d5164SEric W. Biederman 			nextname++;
9506e9d5164SEric W. Biederman 		} else {
9516e9d5164SEric W. Biederman 			namelen = strlen(name);
9526e9d5164SEric W. Biederman 		}
9536e9d5164SEric W. Biederman 		if (namelen == 0)
9546e9d5164SEric W. Biederman 			continue;
9556e9d5164SEric W. Biederman 		memcpy(new_name, name, namelen);
9566e9d5164SEric W. Biederman 		new_name[namelen] = '\0';
9576e9d5164SEric W. Biederman 
958f05e53a7SEric W. Biederman 		new->procname = new_name;
9591f87f0b5SEric W. Biederman 		new->mode     = 0555;
9601f87f0b5SEric W. Biederman 
9611f87f0b5SEric W. Biederman 		*prevp = new;
9621f87f0b5SEric W. Biederman 		prevp = &new->child;
9631f87f0b5SEric W. Biederman 
9641f87f0b5SEric W. Biederman 		new += 2;
9656e9d5164SEric W. Biederman 		new_name += namelen + 1;
9661f87f0b5SEric W. Biederman 	}
9671f87f0b5SEric W. Biederman 	*prevp = table;
9681f87f0b5SEric W. Biederman 
969e0d04529SEric W. Biederman 	init_header(header, root, NULL, table);
9707c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
9717c60c48fSEric W. Biederman 		goto fail;
9728d6ecfccSEric W. Biederman 
9731f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9741f87f0b5SEric W. Biederman 	header->set = lookup_header_set(root, namespaces);
9751f87f0b5SEric W. Biederman 	header->attached_by = header->ctl_table;
976a194558eSEric W. Biederman 	header->attached_to = &root_table[1];
9771f87f0b5SEric W. Biederman 	header->parent = &root_table_header;
978bd295b56SEric W. Biederman 	set = header->set;
979bd295b56SEric W. Biederman 	root = header->root;
980bd295b56SEric W. Biederman 	for (;;) {
9811f87f0b5SEric W. Biederman 		struct ctl_table_header *p;
9821f87f0b5SEric W. Biederman 		list_for_each_entry(p, &set->list, ctl_entry) {
9831f87f0b5SEric W. Biederman 			if (p->unregistering)
9841f87f0b5SEric W. Biederman 				continue;
9851f87f0b5SEric W. Biederman 			try_attach(p, header);
9861f87f0b5SEric W. Biederman 		}
987bd295b56SEric W. Biederman 		if (root == &sysctl_table_root)
988bd295b56SEric W. Biederman 			break;
989bd295b56SEric W. Biederman 		root = list_entry(root->root_list.prev,
990bd295b56SEric W. Biederman 				  struct ctl_table_root, root_list);
991bd295b56SEric W. Biederman 		set = lookup_header_set(root, namespaces);
9921f87f0b5SEric W. Biederman 	}
9937c60c48fSEric W. Biederman 	if (sysctl_check_dups(namespaces, header, path, table))
9947c60c48fSEric W. Biederman 		goto fail_locked;
9958425d6aaSEric W. Biederman 	insert_header(header);
9961f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
9971f87f0b5SEric W. Biederman 
9981f87f0b5SEric W. Biederman 	return header;
9997c60c48fSEric W. Biederman fail_locked:
10007c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
10017c60c48fSEric W. Biederman fail:
10027c60c48fSEric W. Biederman 	kfree(header);
10037c60c48fSEric W. Biederman 	dump_stack();
10047c60c48fSEric W. Biederman 	return NULL;
10051f87f0b5SEric W. Biederman }
10061f87f0b5SEric W. Biederman 
10076e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
10086e9d5164SEric W. Biederman {
10096e9d5164SEric W. Biederman 	int namelen;
10106e9d5164SEric W. Biederman 	namelen = strlen(name);
10116e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
10126e9d5164SEric W. Biederman 		return NULL;
10136e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
10146e9d5164SEric W. Biederman 	pos[namelen] = '/';
10156e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
10166e9d5164SEric W. Biederman 	pos += namelen + 1;
10176e9d5164SEric W. Biederman 	return pos;
10186e9d5164SEric W. Biederman }
10196e9d5164SEric W. Biederman 
1020f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1021f728019bSEric W. Biederman {
1022f728019bSEric W. Biederman 	int has_files = 0;
1023f728019bSEric W. Biederman 	int nr_subheaders = 0;
1024f728019bSEric W. Biederman 	struct ctl_table *entry;
1025f728019bSEric W. Biederman 
1026f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1027f728019bSEric W. Biederman 	if (!table || !table->procname)
1028f728019bSEric W. Biederman 		return 1;
1029f728019bSEric W. Biederman 
1030f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1031f728019bSEric W. Biederman 		if (entry->child)
1032f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1033f728019bSEric W. Biederman 		else
1034f728019bSEric W. Biederman 			has_files = 1;
1035f728019bSEric W. Biederman 	}
1036f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1037f728019bSEric W. Biederman }
1038f728019bSEric W. Biederman 
1039f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
1040f728019bSEric W. Biederman 	struct ctl_table_header ***subheader,
1041f728019bSEric W. Biederman 	struct ctl_table_root *root, struct nsproxy *namespaces,
1042f728019bSEric W. Biederman 	struct ctl_table *table)
1043f728019bSEric W. Biederman {
1044f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1045f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1046f728019bSEric W. Biederman 	int nr_files = 0;
1047f728019bSEric W. Biederman 	int nr_dirs = 0;
1048f728019bSEric W. Biederman 	int err = -ENOMEM;
1049f728019bSEric W. Biederman 
1050f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1051f728019bSEric W. Biederman 		if (entry->child)
1052f728019bSEric W. Biederman 			nr_dirs++;
1053f728019bSEric W. Biederman 		else
1054f728019bSEric W. Biederman 			nr_files++;
1055f728019bSEric W. Biederman 	}
1056f728019bSEric W. Biederman 
1057f728019bSEric W. Biederman 	files = table;
1058f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1059f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1060f728019bSEric W. Biederman 		struct ctl_table *new;
1061f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1062f728019bSEric W. Biederman 				GFP_KERNEL);
1063f728019bSEric W. Biederman 		if (!files)
1064f728019bSEric W. Biederman 			goto out;
1065f728019bSEric W. Biederman 
1066f728019bSEric W. Biederman 		ctl_table_arg = files;
1067f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1068f728019bSEric W. Biederman 			if (entry->child)
1069f728019bSEric W. Biederman 				continue;
1070f728019bSEric W. Biederman 			*new = *entry;
1071f728019bSEric W. Biederman 			new++;
1072f728019bSEric W. Biederman 		}
1073f728019bSEric W. Biederman 	}
1074f728019bSEric W. Biederman 
1075f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1076f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1077f728019bSEric W. Biederman 		struct ctl_table_header *header;
1078f728019bSEric W. Biederman 		header = __register_sysctl_table(root, namespaces, path, files);
1079f728019bSEric W. Biederman 		if (!header) {
1080f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1081f728019bSEric W. Biederman 			goto out;
1082f728019bSEric W. Biederman 		}
1083f728019bSEric W. Biederman 
1084f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1085f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1086f728019bSEric W. Biederman 		**subheader = header;
1087f728019bSEric W. Biederman 		(*subheader)++;
1088f728019bSEric W. Biederman 	}
1089f728019bSEric W. Biederman 
1090f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1091f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1092f728019bSEric W. Biederman 		char *child_pos;
1093f728019bSEric W. Biederman 
1094f728019bSEric W. Biederman 		if (!entry->child)
1095f728019bSEric W. Biederman 			continue;
1096f728019bSEric W. Biederman 
1097f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1098f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1099f728019bSEric W. Biederman 		if (!child_pos)
1100f728019bSEric W. Biederman 			goto out;
1101f728019bSEric W. Biederman 
1102f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
1103f728019bSEric W. Biederman 						  root, namespaces, entry->child);
1104f728019bSEric W. Biederman 		pos[0] = '\0';
1105f728019bSEric W. Biederman 		if (err)
1106f728019bSEric W. Biederman 			goto out;
1107f728019bSEric W. Biederman 	}
1108f728019bSEric W. Biederman 	err = 0;
1109f728019bSEric W. Biederman out:
1110f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1111f728019bSEric W. Biederman 	return err;
1112f728019bSEric W. Biederman }
1113f728019bSEric W. Biederman 
11146e9d5164SEric W. Biederman /**
11156e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
11166e9d5164SEric W. Biederman  * @root: List of sysctl headers to register on
11176e9d5164SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
11186e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11196e9d5164SEric W. Biederman  * @table: the top-level table structure
11206e9d5164SEric W. Biederman  *
11216e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11226e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11236e9d5164SEric W. Biederman  *
11246e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
11256e9d5164SEric W. Biederman  */
11266e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
11276e9d5164SEric W. Biederman 	struct ctl_table_root *root,
11286e9d5164SEric W. Biederman 	struct nsproxy *namespaces,
11296e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
11306e9d5164SEric W. Biederman {
1131ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1132f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1133f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
11346e9d5164SEric W. Biederman 	const struct ctl_path *component;
11356e9d5164SEric W. Biederman 	char *new_path, *pos;
11366e9d5164SEric W. Biederman 
11376e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
11386e9d5164SEric W. Biederman 	if (!new_path)
11396e9d5164SEric W. Biederman 		return NULL;
11406e9d5164SEric W. Biederman 
11416e9d5164SEric W. Biederman 	pos[0] = '\0';
11426e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
11436e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
11446e9d5164SEric W. Biederman 		if (!pos)
11456e9d5164SEric W. Biederman 			goto out;
11466e9d5164SEric W. Biederman 	}
1147ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1148ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1149ec6a5266SEric W. Biederman 		if (!pos)
1150ec6a5266SEric W. Biederman 			goto out;
1151ec6a5266SEric W. Biederman 		table = table->child;
1152ec6a5266SEric W. Biederman 	}
1153f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
11546e9d5164SEric W. Biederman 		header = __register_sysctl_table(root, namespaces, new_path, table);
1155ec6a5266SEric W. Biederman 		if (header)
1156ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1157f728019bSEric W. Biederman 	} else {
1158f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1159f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1160f728019bSEric W. Biederman 		if (!header)
1161f728019bSEric W. Biederman 			goto out;
1162f728019bSEric W. Biederman 
1163f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1164f728019bSEric W. Biederman 		subheader = subheaders;
1165f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1166f728019bSEric W. Biederman 
1167f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1168f728019bSEric W. Biederman 						root, namespaces, table))
1169f728019bSEric W. Biederman 			goto err_register_leaves;
1170f728019bSEric W. Biederman 	}
1171f728019bSEric W. Biederman 
11726e9d5164SEric W. Biederman out:
11736e9d5164SEric W. Biederman 	kfree(new_path);
11746e9d5164SEric W. Biederman 	return header;
1175f728019bSEric W. Biederman 
1176f728019bSEric W. Biederman err_register_leaves:
1177f728019bSEric W. Biederman 	while (subheader > subheaders) {
1178f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1179f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1180f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1181f728019bSEric W. Biederman 		kfree(table);
1182f728019bSEric W. Biederman 	}
1183f728019bSEric W. Biederman 	kfree(header);
1184f728019bSEric W. Biederman 	header = NULL;
1185f728019bSEric W. Biederman 	goto out;
11866e9d5164SEric W. Biederman }
11876e9d5164SEric W. Biederman 
11881f87f0b5SEric W. Biederman /**
11891f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
11901f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11911f87f0b5SEric W. Biederman  * @table: the top-level table structure
11921f87f0b5SEric W. Biederman  *
11931f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11941f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11951f87f0b5SEric W. Biederman  *
11961f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
11971f87f0b5SEric W. Biederman  */
11981f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
11991f87f0b5SEric W. Biederman 						struct ctl_table *table)
12001f87f0b5SEric W. Biederman {
12011f87f0b5SEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
12021f87f0b5SEric W. Biederman 					path, table);
12031f87f0b5SEric W. Biederman }
12041f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
12051f87f0b5SEric W. Biederman 
12061f87f0b5SEric W. Biederman /**
12071f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
12081f87f0b5SEric W. Biederman  * @table: the top-level table structure
12091f87f0b5SEric W. Biederman  *
12101f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12111f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12121f87f0b5SEric W. Biederman  *
12131f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
12141f87f0b5SEric W. Biederman  */
12151f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
12161f87f0b5SEric W. Biederman {
12171f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
12181f87f0b5SEric W. Biederman 
12191f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
12201f87f0b5SEric W. Biederman }
12211f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
12221f87f0b5SEric W. Biederman 
1223938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1224938aaa4fSEric W. Biederman {
1225938aaa4fSEric W. Biederman 	if (--header->nreg)
1226938aaa4fSEric W. Biederman 		return;
1227938aaa4fSEric W. Biederman 
1228938aaa4fSEric W. Biederman 	start_unregistering(header);
1229938aaa4fSEric W. Biederman 	if (!--header->parent->count) {
1230938aaa4fSEric W. Biederman 		WARN_ON(1);
1231938aaa4fSEric W. Biederman 		kfree_rcu(header->parent, rcu);
1232938aaa4fSEric W. Biederman 	}
1233938aaa4fSEric W. Biederman 	if (!--header->count)
1234938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
1235938aaa4fSEric W. Biederman }
1236938aaa4fSEric W. Biederman 
12371f87f0b5SEric W. Biederman /**
12381f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
12391f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
12401f87f0b5SEric W. Biederman  *
12411f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
12421f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
12431f87f0b5SEric W. Biederman  */
12441f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
12451f87f0b5SEric W. Biederman {
1246f728019bSEric W. Biederman 	int nr_subheaders;
12471f87f0b5SEric W. Biederman 	might_sleep();
12481f87f0b5SEric W. Biederman 
12491f87f0b5SEric W. Biederman 	if (header == NULL)
12501f87f0b5SEric W. Biederman 		return;
12511f87f0b5SEric W. Biederman 
1252f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1253f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1254f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1255f728019bSEric W. Biederman 		int i;
1256f728019bSEric W. Biederman 
1257f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1258f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1259f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1260f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1261f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1262f728019bSEric W. Biederman 			kfree(table);
1263f728019bSEric W. Biederman 		}
1264f728019bSEric W. Biederman 		kfree(header);
1265f728019bSEric W. Biederman 		return;
1266f728019bSEric W. Biederman 	}
1267f728019bSEric W. Biederman 
12681f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1269938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
12701f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
12711f87f0b5SEric W. Biederman }
12721f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
12731f87f0b5SEric W. Biederman 
12741f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p,
12751f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
12761f87f0b5SEric W. Biederman {
12771f87f0b5SEric W. Biederman 	INIT_LIST_HEAD(&p->list);
12781f87f0b5SEric W. Biederman 	p->is_seen = is_seen;
12791f87f0b5SEric W. Biederman }
12801f87f0b5SEric W. Biederman 
128197324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
128297324cd8SEric W. Biederman {
128397324cd8SEric W. Biederman 	WARN_ON(!list_empty(&set->list));
128497324cd8SEric W. Biederman }
12851f87f0b5SEric W. Biederman 
12861e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
128777b14db5SEric W. Biederman {
1288e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1289e1675231SAlexey Dobriyan 
129077b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
12919043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
12929043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
129377b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1294de4e83bdSEric W. Biederman 
1295de4e83bdSEric W. Biederman 	return sysctl_init();
129677b14db5SEric W. Biederman }
1297