xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 076c3eed)
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 
52076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
53076c3eedSEric W. Biederman {
54076c3eedSEric W. Biederman 	int minlen;
55076c3eedSEric W. Biederman 	int cmp;
56076c3eedSEric W. Biederman 
57076c3eedSEric W. Biederman 	minlen = len1;
58076c3eedSEric W. Biederman 	if (minlen > len2)
59076c3eedSEric W. Biederman 		minlen = len2;
60076c3eedSEric W. Biederman 
61076c3eedSEric W. Biederman 	cmp = memcmp(name1, name2, minlen);
62076c3eedSEric W. Biederman 	if (cmp == 0)
63076c3eedSEric W. Biederman 		cmp = len1 - len2;
64076c3eedSEric W. Biederman 	return cmp;
65076c3eedSEric W. Biederman }
66076c3eedSEric W. Biederman 
67076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
68076c3eedSEric W. Biederman 	struct ctl_table_set *set,
69076c3eedSEric W. Biederman 	struct ctl_table_header *dir_head, struct ctl_table *dir,
70076c3eedSEric W. Biederman 	const char *name, int namelen)
71076c3eedSEric W. Biederman {
72076c3eedSEric W. Biederman 	struct ctl_table_header *head;
73076c3eedSEric W. Biederman 	struct ctl_table *entry;
74076c3eedSEric W. Biederman 
75076c3eedSEric W. Biederman 	if (dir_head->set == set) {
76076c3eedSEric W. Biederman 		for (entry = dir; entry->procname; entry++) {
77076c3eedSEric W. Biederman 			const char *procname = entry->procname;
78076c3eedSEric W. Biederman 			if (namecmp(procname, strlen(procname), name, namelen) == 0) {
79076c3eedSEric W. Biederman 				*phead = dir_head;
80076c3eedSEric W. Biederman 				return entry;
81076c3eedSEric W. Biederman 			}
82076c3eedSEric W. Biederman 		}
83076c3eedSEric W. Biederman 	}
84076c3eedSEric W. Biederman 
85076c3eedSEric W. Biederman 	list_for_each_entry(head, &set->list, ctl_entry) {
86076c3eedSEric W. Biederman 		if (head->unregistering)
87076c3eedSEric W. Biederman 			continue;
88076c3eedSEric W. Biederman 		if (head->attached_to != dir)
89076c3eedSEric W. Biederman 			continue;
90076c3eedSEric W. Biederman 		for (entry = head->attached_by; entry->procname; entry++) {
91076c3eedSEric W. Biederman 			const char *procname = entry->procname;
92076c3eedSEric W. Biederman 			if (namecmp(procname, strlen(procname), name, namelen) == 0) {
93076c3eedSEric W. Biederman 				*phead = head;
94076c3eedSEric W. Biederman 				return entry;
95076c3eedSEric W. Biederman 			}
96076c3eedSEric W. Biederman 		}
97076c3eedSEric W. Biederman 	}
98076c3eedSEric W. Biederman 	return NULL;
99076c3eedSEric W. Biederman }
100076c3eedSEric W. Biederman 
101e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
102e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
103e0d04529SEric W. Biederman 	struct ctl_table *table)
104e0d04529SEric W. Biederman {
105e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
106e0d04529SEric W. Biederman 	INIT_LIST_HEAD(&head->ctl_entry);
107e0d04529SEric W. Biederman 	head->used = 0;
108e0d04529SEric W. Biederman 	head->count = 1;
109e0d04529SEric W. Biederman 	head->nreg = 1;
110e0d04529SEric W. Biederman 	head->unregistering = NULL;
111e0d04529SEric W. Biederman 	head->root = root;
112e0d04529SEric W. Biederman 	head->set = set;
113e0d04529SEric W. Biederman 	head->parent = NULL;
114e0d04529SEric W. Biederman }
115e0d04529SEric W. Biederman 
1168425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
1178425d6aaSEric W. Biederman {
1188425d6aaSEric W. Biederman 	list_del_init(&head->ctl_entry);
1198425d6aaSEric W. Biederman }
1208425d6aaSEric W. Biederman 
1218425d6aaSEric W. Biederman static void insert_header(struct ctl_table_header *header)
1228425d6aaSEric W. Biederman {
1238425d6aaSEric W. Biederman 	header->parent->count++;
1248425d6aaSEric W. Biederman 	list_add_tail(&header->ctl_entry, &header->set->list);
1258425d6aaSEric W. Biederman }
1268425d6aaSEric W. Biederman 
1271f87f0b5SEric W. Biederman /* called under sysctl_lock */
1281f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
1291f87f0b5SEric W. Biederman {
1301f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
1311f87f0b5SEric W. Biederman 		return 0;
1321f87f0b5SEric W. Biederman 	p->used++;
1331f87f0b5SEric W. Biederman 	return 1;
1341f87f0b5SEric W. Biederman }
1351f87f0b5SEric W. Biederman 
1361f87f0b5SEric W. Biederman /* called under sysctl_lock */
1371f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
1381f87f0b5SEric W. Biederman {
1391f87f0b5SEric W. Biederman 	if (!--p->used)
1401f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
1411f87f0b5SEric W. Biederman 			complete(p->unregistering);
1421f87f0b5SEric W. Biederman }
1431f87f0b5SEric W. Biederman 
1441f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
1451f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
1461f87f0b5SEric W. Biederman {
1471f87f0b5SEric W. Biederman 	/*
1481f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
1491f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
1501f87f0b5SEric W. Biederman 	 */
1511f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
1521f87f0b5SEric W. Biederman 		struct completion wait;
1531f87f0b5SEric W. Biederman 		init_completion(&wait);
1541f87f0b5SEric W. Biederman 		p->unregistering = &wait;
1551f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
1561f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
1571f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
1581f87f0b5SEric W. Biederman 	} else {
1591f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
1601f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
1611f87f0b5SEric W. Biederman 	}
1621f87f0b5SEric W. Biederman 	/*
1631f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
1641f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
1651f87f0b5SEric W. Biederman 	 */
1668425d6aaSEric W. Biederman 	erase_header(p);
1671f87f0b5SEric W. Biederman }
1681f87f0b5SEric W. Biederman 
1691f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
1701f87f0b5SEric W. Biederman {
1711f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1721f87f0b5SEric W. Biederman 	head->count++;
1731f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1741f87f0b5SEric W. Biederman }
1751f87f0b5SEric W. Biederman 
1761f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
1771f87f0b5SEric W. Biederman {
1781f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1791f87f0b5SEric W. Biederman 	if (!--head->count)
1801f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
1811f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1821f87f0b5SEric W. Biederman }
1831f87f0b5SEric W. Biederman 
1841f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1851f87f0b5SEric W. Biederman {
1861f87f0b5SEric W. Biederman 	if (!head)
1871f87f0b5SEric W. Biederman 		BUG();
1881f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1891f87f0b5SEric W. Biederman 	if (!use_table(head))
1901f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
1911f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1921f87f0b5SEric W. Biederman 	return head;
1931f87f0b5SEric W. Biederman }
1941f87f0b5SEric W. Biederman 
1951f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
1961f87f0b5SEric W. Biederman {
1971f87f0b5SEric W. Biederman 	if (!head)
1981f87f0b5SEric W. Biederman 		return;
1991f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2001f87f0b5SEric W. Biederman 	unuse_table(head);
2011f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2021f87f0b5SEric W. Biederman }
2031f87f0b5SEric W. Biederman 
2041f87f0b5SEric W. Biederman static struct ctl_table_set *
2051f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
2061f87f0b5SEric W. Biederman {
2071f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
2081f87f0b5SEric W. Biederman 	if (root->lookup)
2091f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
2101f87f0b5SEric W. Biederman 	return set;
2111f87f0b5SEric W. Biederman }
2121f87f0b5SEric W. Biederman 
2131f87f0b5SEric W. Biederman static struct list_head *
2141f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
2151f87f0b5SEric W. Biederman {
2161f87f0b5SEric W. Biederman 	struct ctl_table_set *set = lookup_header_set(root, namespaces);
2171f87f0b5SEric W. Biederman 	return &set->list;
2181f87f0b5SEric W. Biederman }
2191f87f0b5SEric W. Biederman 
220076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
221076c3eedSEric W. Biederman 				      struct ctl_table_header *dir_head,
222076c3eedSEric W. Biederman 				      struct ctl_table *dir,
223076c3eedSEric W. Biederman 				      const char *name, int namelen)
224076c3eedSEric W. Biederman {
225076c3eedSEric W. Biederman 	struct ctl_table_header *head;
226076c3eedSEric W. Biederman 	struct ctl_table *entry;
227076c3eedSEric W. Biederman 	struct ctl_table_root *root;
228076c3eedSEric W. Biederman 	struct ctl_table_set *set;
229076c3eedSEric W. Biederman 
230076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
231076c3eedSEric W. Biederman 	root = &sysctl_table_root;
232076c3eedSEric W. Biederman 	do {
233076c3eedSEric W. Biederman 		set = lookup_header_set(root, current->nsproxy);
234076c3eedSEric W. Biederman 		entry = find_entry(&head, set, dir_head, dir, name, namelen);
235076c3eedSEric W. Biederman 		if (entry && use_table(head))
236076c3eedSEric W. Biederman 			*phead = head;
237076c3eedSEric W. Biederman 		else
238076c3eedSEric W. Biederman 			entry = NULL;
239076c3eedSEric W. Biederman 		root = list_entry(root->root_list.next,
240076c3eedSEric W. Biederman 				  struct ctl_table_root, root_list);
241076c3eedSEric W. Biederman 	} while (!entry && root != &sysctl_table_root);
242076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
243076c3eedSEric W. Biederman 	return entry;
244076c3eedSEric W. Biederman }
245076c3eedSEric W. Biederman 
2461f87f0b5SEric W. Biederman static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
2471f87f0b5SEric W. Biederman 						struct ctl_table_header *prev)
2481f87f0b5SEric W. Biederman {
2491f87f0b5SEric W. Biederman 	struct ctl_table_root *root;
2501f87f0b5SEric W. Biederman 	struct list_head *header_list;
2511f87f0b5SEric W. Biederman 	struct ctl_table_header *head;
2521f87f0b5SEric W. Biederman 	struct list_head *tmp;
2531f87f0b5SEric W. Biederman 
2541f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2551f87f0b5SEric W. Biederman 	if (prev) {
2561f87f0b5SEric W. Biederman 		head = prev;
2571f87f0b5SEric W. Biederman 		tmp = &prev->ctl_entry;
2581f87f0b5SEric W. Biederman 		unuse_table(prev);
2591f87f0b5SEric W. Biederman 		goto next;
2601f87f0b5SEric W. Biederman 	}
2611f87f0b5SEric W. Biederman 	tmp = &root_table_header.ctl_entry;
2621f87f0b5SEric W. Biederman 	for (;;) {
2631f87f0b5SEric W. Biederman 		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
2641f87f0b5SEric W. Biederman 
2651f87f0b5SEric W. Biederman 		if (!use_table(head))
2661f87f0b5SEric W. Biederman 			goto next;
2671f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
2681f87f0b5SEric W. Biederman 		return head;
2691f87f0b5SEric W. Biederman 	next:
2701f87f0b5SEric W. Biederman 		root = head->root;
2711f87f0b5SEric W. Biederman 		tmp = tmp->next;
2721f87f0b5SEric W. Biederman 		header_list = lookup_header_list(root, namespaces);
2731f87f0b5SEric W. Biederman 		if (tmp != header_list)
2741f87f0b5SEric W. Biederman 			continue;
2751f87f0b5SEric W. Biederman 
2761f87f0b5SEric W. Biederman 		do {
2771f87f0b5SEric W. Biederman 			root = list_entry(root->root_list.next,
2781f87f0b5SEric W. Biederman 					struct ctl_table_root, root_list);
2791f87f0b5SEric W. Biederman 			if (root == &sysctl_table_root)
2801f87f0b5SEric W. Biederman 				goto out;
2811f87f0b5SEric W. Biederman 			header_list = lookup_header_list(root, namespaces);
2821f87f0b5SEric W. Biederman 		} while (list_empty(header_list));
2831f87f0b5SEric W. Biederman 		tmp = header_list->next;
2841f87f0b5SEric W. Biederman 	}
2851f87f0b5SEric W. Biederman out:
2861f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2871f87f0b5SEric W. Biederman 	return NULL;
2881f87f0b5SEric W. Biederman }
2891f87f0b5SEric W. Biederman 
2901f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
2911f87f0b5SEric W. Biederman {
2921f87f0b5SEric W. Biederman 	return __sysctl_head_next(current->nsproxy, prev);
2931f87f0b5SEric W. Biederman }
2941f87f0b5SEric W. Biederman 
2951f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
2961f87f0b5SEric W. Biederman {
2971f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2981f87f0b5SEric W. Biederman 	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
2991f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3001f87f0b5SEric W. Biederman }
3011f87f0b5SEric W. Biederman 
3021f87f0b5SEric W. Biederman /*
3031f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
3041f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
3051f87f0b5SEric W. Biederman  */
3061f87f0b5SEric W. Biederman 
3071f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
3081f87f0b5SEric W. Biederman {
3091f87f0b5SEric W. Biederman 	if (!current_euid())
3101f87f0b5SEric W. Biederman 		mode >>= 6;
3111f87f0b5SEric W. Biederman 	else if (in_egroup_p(0))
3121f87f0b5SEric W. Biederman 		mode >>= 3;
3131f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
3141f87f0b5SEric W. Biederman 		return 0;
3151f87f0b5SEric W. Biederman 	return -EACCES;
3161f87f0b5SEric W. Biederman }
3171f87f0b5SEric W. Biederman 
3181f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
3191f87f0b5SEric W. Biederman {
3201f87f0b5SEric W. Biederman 	int mode;
3211f87f0b5SEric W. Biederman 
3221f87f0b5SEric W. Biederman 	if (root->permissions)
3231f87f0b5SEric W. Biederman 		mode = root->permissions(root, current->nsproxy, table);
3241f87f0b5SEric W. Biederman 	else
3251f87f0b5SEric W. Biederman 		mode = table->mode;
3261f87f0b5SEric W. Biederman 
3271f87f0b5SEric W. Biederman 	return test_perm(mode, op);
3281f87f0b5SEric W. Biederman }
3291f87f0b5SEric W. Biederman 
3309043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
3319043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
33277b14db5SEric W. Biederman {
33377b14db5SEric W. Biederman 	struct inode *inode;
3349043476fSAl Viro 	struct proc_inode *ei;
33577b14db5SEric W. Biederman 
3369043476fSAl Viro 	inode = new_inode(sb);
33777b14db5SEric W. Biederman 	if (!inode)
33877b14db5SEric W. Biederman 		goto out;
33977b14db5SEric W. Biederman 
34085fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
34185fe4025SChristoph Hellwig 
3429043476fSAl Viro 	sysctl_head_get(head);
34377b14db5SEric W. Biederman 	ei = PROC_I(inode);
3449043476fSAl Viro 	ei->sysctl = head;
3459043476fSAl Viro 	ei->sysctl_entry = table;
3469043476fSAl Viro 
34777b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3489043476fSAl Viro 	inode->i_mode = table->mode;
3499043476fSAl Viro 	if (!table->child) {
3509043476fSAl Viro 		inode->i_mode |= S_IFREG;
35177b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
35277b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
3539043476fSAl Viro 	} else {
3549043476fSAl Viro 		inode->i_mode |= S_IFDIR;
3559043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
3569043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
3579043476fSAl Viro 	}
35877b14db5SEric W. Biederman out:
35977b14db5SEric W. Biederman 	return inode;
36077b14db5SEric W. Biederman }
36177b14db5SEric W. Biederman 
36281324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
36377b14db5SEric W. Biederman {
3643cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
3653cc3e046SEric W. Biederman 	if (!head)
3663cc3e046SEric W. Biederman 		head = &root_table_header;
3673cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
36877b14db5SEric W. Biederman }
36977b14db5SEric W. Biederman 
37077b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
37177b14db5SEric W. Biederman 					struct nameidata *nd)
37277b14db5SEric W. Biederman {
3739043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
3749043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
3759043476fSAl Viro 	struct ctl_table_header *h = NULL;
3769043476fSAl Viro 	struct qstr *name = &dentry->d_name;
3779043476fSAl Viro 	struct ctl_table *p;
37877b14db5SEric W. Biederman 	struct inode *inode;
3799043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
38077b14db5SEric W. Biederman 
3819043476fSAl Viro 	if (IS_ERR(head))
3829043476fSAl Viro 		return ERR_CAST(head);
3839043476fSAl Viro 
3849043476fSAl Viro 	if (table && !table->child) {
3859043476fSAl Viro 		WARN_ON(1);
3869043476fSAl Viro 		goto out;
3879043476fSAl Viro 	}
3889043476fSAl Viro 
389a194558eSEric W. Biederman 	table = table ? table->child : &head->ctl_table[1];
3909043476fSAl Viro 
391076c3eedSEric W. Biederman 	p = lookup_entry(&h, head, table, name->name, name->len);
3929043476fSAl Viro 	if (!p)
39377b14db5SEric W. Biederman 		goto out;
39477b14db5SEric W. Biederman 
39577b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
3969043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
3979043476fSAl Viro 	if (h)
3989043476fSAl Viro 		sysctl_head_finish(h);
3999043476fSAl Viro 
40077b14db5SEric W. Biederman 	if (!inode)
40177b14db5SEric W. Biederman 		goto out;
40277b14db5SEric W. Biederman 
40377b14db5SEric W. Biederman 	err = NULL;
404fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
40577b14db5SEric W. Biederman 	d_add(dentry, inode);
40677b14db5SEric W. Biederman 
40777b14db5SEric W. Biederman out:
40877b14db5SEric W. Biederman 	sysctl_head_finish(head);
40977b14db5SEric W. Biederman 	return err;
41077b14db5SEric W. Biederman }
41177b14db5SEric W. Biederman 
4127708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
4137708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
41477b14db5SEric W. Biederman {
4159043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
4169043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
4179043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
4182a2da53bSDavid Howells 	ssize_t error;
4192a2da53bSDavid Howells 	size_t res;
42077b14db5SEric W. Biederman 
4219043476fSAl Viro 	if (IS_ERR(head))
4229043476fSAl Viro 		return PTR_ERR(head);
42377b14db5SEric W. Biederman 
42477b14db5SEric W. Biederman 	/*
42577b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
42677b14db5SEric W. Biederman 	 * and won't be until we finish.
42777b14db5SEric W. Biederman 	 */
42877b14db5SEric W. Biederman 	error = -EPERM;
429d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
43077b14db5SEric W. Biederman 		goto out;
43177b14db5SEric W. Biederman 
4329043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
4339043476fSAl Viro 	error = -EINVAL;
4349043476fSAl Viro 	if (!table->proc_handler)
4359043476fSAl Viro 		goto out;
4369043476fSAl Viro 
43777b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
43877b14db5SEric W. Biederman 	res = count;
4398d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
44077b14db5SEric W. Biederman 	if (!error)
44177b14db5SEric W. Biederman 		error = res;
44277b14db5SEric W. Biederman out:
44377b14db5SEric W. Biederman 	sysctl_head_finish(head);
44477b14db5SEric W. Biederman 
44577b14db5SEric W. Biederman 	return error;
44677b14db5SEric W. Biederman }
44777b14db5SEric W. Biederman 
4487708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
4497708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
4507708bfb1SPavel Emelyanov {
4517708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
4527708bfb1SPavel Emelyanov }
4537708bfb1SPavel Emelyanov 
45477b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
45577b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
45677b14db5SEric W. Biederman {
4577708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
45877b14db5SEric W. Biederman }
45977b14db5SEric W. Biederman 
460f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
461f1ecf068SLucas De Marchi {
462f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
463f1ecf068SLucas De Marchi 
464f1ecf068SLucas De Marchi 	if (table->poll)
465f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
466f1ecf068SLucas De Marchi 
467f1ecf068SLucas De Marchi 	return 0;
468f1ecf068SLucas De Marchi }
469f1ecf068SLucas De Marchi 
470f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
471f1ecf068SLucas De Marchi {
472f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
473f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
474f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
475f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
476f1ecf068SLucas De Marchi 
477f1ecf068SLucas De Marchi 	if (!table->proc_handler)
478f1ecf068SLucas De Marchi 		goto out;
479f1ecf068SLucas De Marchi 
480f1ecf068SLucas De Marchi 	if (!table->poll)
481f1ecf068SLucas De Marchi 		goto out;
482f1ecf068SLucas De Marchi 
483f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
484f1ecf068SLucas De Marchi 
485f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
486f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
487f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
488f1ecf068SLucas De Marchi 	}
489f1ecf068SLucas De Marchi 
490f1ecf068SLucas De Marchi out:
491f1ecf068SLucas De Marchi 	return ret;
492f1ecf068SLucas De Marchi }
49377b14db5SEric W. Biederman 
49477b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
4959043476fSAl Viro 				filldir_t filldir,
4969043476fSAl Viro 				struct ctl_table_header *head,
4979043476fSAl Viro 				struct ctl_table *table)
49877b14db5SEric W. Biederman {
49977b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
50077b14db5SEric W. Biederman 	struct inode *inode;
50177b14db5SEric W. Biederman 	struct qstr qname;
50277b14db5SEric W. Biederman 	ino_t ino = 0;
50377b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
50477b14db5SEric W. Biederman 
50577b14db5SEric W. Biederman 	qname.name = table->procname;
50677b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
50777b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
50877b14db5SEric W. Biederman 
50977b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
51077b14db5SEric W. Biederman 	if (!child) {
5119043476fSAl Viro 		child = d_alloc(dir, &qname);
5129043476fSAl Viro 		if (child) {
5139043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
5149043476fSAl Viro 			if (!inode) {
5159043476fSAl Viro 				dput(child);
5169043476fSAl Viro 				return -ENOMEM;
5179043476fSAl Viro 			} else {
518fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
5199043476fSAl Viro 				d_add(child, inode);
52077b14db5SEric W. Biederman 			}
5219043476fSAl Viro 		} else {
5229043476fSAl Viro 			return -ENOMEM;
52377b14db5SEric W. Biederman 		}
52477b14db5SEric W. Biederman 	}
52577b14db5SEric W. Biederman 	inode = child->d_inode;
52677b14db5SEric W. Biederman 	ino  = inode->i_ino;
52777b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
52877b14db5SEric W. Biederman 	dput(child);
5299043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
5309043476fSAl Viro }
5319043476fSAl Viro 
5329043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
5339043476fSAl Viro 		unsigned long *pos, struct file *file,
5349043476fSAl Viro 		void *dirent, filldir_t filldir)
5359043476fSAl Viro {
5369043476fSAl Viro 
5372315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
5389043476fSAl Viro 		int res;
5399043476fSAl Viro 
5409043476fSAl Viro 		if (*pos < file->f_pos)
5419043476fSAl Viro 			continue;
5429043476fSAl Viro 
5439043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
5449043476fSAl Viro 		if (res)
5459043476fSAl Viro 			return res;
5469043476fSAl Viro 
5479043476fSAl Viro 		file->f_pos = *pos + 1;
5489043476fSAl Viro 	}
5499043476fSAl Viro 	return 0;
55077b14db5SEric W. Biederman }
55177b14db5SEric W. Biederman 
55277b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
55377b14db5SEric W. Biederman {
5549043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
55577b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
5569043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5579043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5589043476fSAl Viro 	struct ctl_table_header *h = NULL;
55977b14db5SEric W. Biederman 	unsigned long pos;
5609043476fSAl Viro 	int ret = -EINVAL;
56177b14db5SEric W. Biederman 
5629043476fSAl Viro 	if (IS_ERR(head))
5639043476fSAl Viro 		return PTR_ERR(head);
5649043476fSAl Viro 
5659043476fSAl Viro 	if (table && !table->child) {
5669043476fSAl Viro 		WARN_ON(1);
56777b14db5SEric W. Biederman 		goto out;
5689043476fSAl Viro 	}
5699043476fSAl Viro 
570a194558eSEric W. Biederman 	table = table ? table->child : &head->ctl_table[1];
57177b14db5SEric W. Biederman 
57277b14db5SEric W. Biederman 	ret = 0;
57377b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
57477b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
57577b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
57677b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
57777b14db5SEric W. Biederman 			goto out;
57877b14db5SEric W. Biederman 		filp->f_pos++;
57977b14db5SEric W. Biederman 	}
58077b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
58177b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
58277b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
58377b14db5SEric W. Biederman 			goto out;
58477b14db5SEric W. Biederman 		filp->f_pos++;
58577b14db5SEric W. Biederman 	}
58677b14db5SEric W. Biederman 	pos = 2;
58777b14db5SEric W. Biederman 
5889043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
5899043476fSAl Viro 	if (ret)
59077b14db5SEric W. Biederman 		goto out;
5919043476fSAl Viro 
5929043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
5939043476fSAl Viro 		if (h->attached_to != table)
5949043476fSAl Viro 			continue;
5959043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
5969043476fSAl Viro 		if (ret) {
5979043476fSAl Viro 			sysctl_head_finish(h);
5989043476fSAl Viro 			break;
59977b14db5SEric W. Biederman 		}
60077b14db5SEric W. Biederman 	}
60177b14db5SEric W. Biederman 	ret = 1;
60277b14db5SEric W. Biederman out:
60377b14db5SEric W. Biederman 	sysctl_head_finish(head);
60477b14db5SEric W. Biederman 	return ret;
60577b14db5SEric W. Biederman }
60677b14db5SEric W. Biederman 
60710556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
60877b14db5SEric W. Biederman {
60977b14db5SEric W. Biederman 	/*
61077b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
61177b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
61277b14db5SEric W. Biederman 	 */
613f696a365SMiklos Szeredi 	struct ctl_table_header *head;
614f696a365SMiklos Szeredi 	struct ctl_table *table;
61577b14db5SEric W. Biederman 	int error;
61677b14db5SEric W. Biederman 
617f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
618f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
619f696a365SMiklos Szeredi 		return -EACCES;
620f696a365SMiklos Szeredi 
621f696a365SMiklos Szeredi 	head = grab_header(inode);
6229043476fSAl Viro 	if (IS_ERR(head))
6239043476fSAl Viro 		return PTR_ERR(head);
62477b14db5SEric W. Biederman 
625f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
6269043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
6279043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
6289043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
6291fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
6309043476fSAl Viro 
63177b14db5SEric W. Biederman 	sysctl_head_finish(head);
63277b14db5SEric W. Biederman 	return error;
63377b14db5SEric W. Biederman }
63477b14db5SEric W. Biederman 
63577b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
63677b14db5SEric W. Biederman {
63777b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
63877b14db5SEric W. Biederman 	int error;
63977b14db5SEric W. Biederman 
64077b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
64177b14db5SEric W. Biederman 		return -EPERM;
64277b14db5SEric W. Biederman 
64377b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
6441025774cSChristoph Hellwig 	if (error)
64577b14db5SEric W. Biederman 		return error;
6461025774cSChristoph Hellwig 
6471025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
6481025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
6491025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
6501025774cSChristoph Hellwig 		if (error)
6511025774cSChristoph Hellwig 			return error;
6521025774cSChristoph Hellwig 	}
6531025774cSChristoph Hellwig 
6541025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6551025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6561025774cSChristoph Hellwig 	return 0;
65777b14db5SEric W. Biederman }
65877b14db5SEric W. Biederman 
6599043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
6609043476fSAl Viro {
6619043476fSAl Viro 	struct inode *inode = dentry->d_inode;
6629043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
6639043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
6649043476fSAl Viro 
6659043476fSAl Viro 	if (IS_ERR(head))
6669043476fSAl Viro 		return PTR_ERR(head);
6679043476fSAl Viro 
6689043476fSAl Viro 	generic_fillattr(inode, stat);
6699043476fSAl Viro 	if (table)
6709043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
6719043476fSAl Viro 
6729043476fSAl Viro 	sysctl_head_finish(head);
6739043476fSAl Viro 	return 0;
6749043476fSAl Viro }
6759043476fSAl Viro 
67677b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
677f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
678f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
67977b14db5SEric W. Biederman 	.read		= proc_sys_read,
68077b14db5SEric W. Biederman 	.write		= proc_sys_write,
6816038f373SArnd Bergmann 	.llseek		= default_llseek,
6829043476fSAl Viro };
6839043476fSAl Viro 
6849043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
685887df078SPavel Emelyanov 	.read		= generic_read_dir,
68677b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
6873222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
68877b14db5SEric W. Biederman };
68977b14db5SEric W. Biederman 
69003a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
6919043476fSAl Viro 	.permission	= proc_sys_permission,
6929043476fSAl Viro 	.setattr	= proc_sys_setattr,
6939043476fSAl Viro 	.getattr	= proc_sys_getattr,
6949043476fSAl Viro };
6959043476fSAl Viro 
6969043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
69777b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
69877b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
69977b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
7009043476fSAl Viro 	.getattr	= proc_sys_getattr,
70177b14db5SEric W. Biederman };
70277b14db5SEric W. Biederman 
70377b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
70477b14db5SEric W. Biederman {
70534286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
70634286d66SNick Piggin 		return -ECHILD;
7079043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
7089043476fSAl Viro }
7099043476fSAl Viro 
710fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
7119043476fSAl Viro {
7129043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
7139043476fSAl Viro }
7149043476fSAl Viro 
7151f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
7161f87f0b5SEric W. Biederman {
7171f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
7181f87f0b5SEric W. Biederman 	int res;
7191f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
7201f87f0b5SEric W. Biederman 	if (p->unregistering)
7211f87f0b5SEric W. Biederman 		res = 0;
7221f87f0b5SEric W. Biederman 	else if (!set->is_seen)
7231f87f0b5SEric W. Biederman 		res = 1;
7241f87f0b5SEric W. Biederman 	else
7251f87f0b5SEric W. Biederman 		res = set->is_seen(set);
7261f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
7271f87f0b5SEric W. Biederman 	return res;
7281f87f0b5SEric W. Biederman }
7291f87f0b5SEric W. Biederman 
730621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
731621e155aSNick Piggin 		const struct inode *pinode,
732621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
733621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
7349043476fSAl Viro {
735dfef6dcdSAl Viro 	struct ctl_table_header *head;
73631e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
73731e6b01fSNick Piggin 	 * that inode here can be NULL */
738dfef6dcdSAl Viro 	/* AV: can it, indeed? */
73931e6b01fSNick Piggin 	if (!inode)
740dfef6dcdSAl Viro 		return 1;
741621e155aSNick Piggin 	if (name->len != len)
7429043476fSAl Viro 		return 1;
743621e155aSNick Piggin 	if (memcmp(name->name, str, len))
7449043476fSAl Viro 		return 1;
745dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
746dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
74777b14db5SEric W. Biederman }
74877b14db5SEric W. Biederman 
749d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
75077b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
7519043476fSAl Viro 	.d_delete	= proc_sys_delete,
7529043476fSAl Viro 	.d_compare	= proc_sys_compare,
75377b14db5SEric W. Biederman };
75477b14db5SEric W. Biederman 
7551f87f0b5SEric W. Biederman static struct ctl_table *is_branch_in(struct ctl_table *branch,
7561f87f0b5SEric W. Biederman 				      struct ctl_table *table)
7571f87f0b5SEric W. Biederman {
7581f87f0b5SEric W. Biederman 	struct ctl_table *p;
7591f87f0b5SEric W. Biederman 	const char *s = branch->procname;
7601f87f0b5SEric W. Biederman 
7611f87f0b5SEric W. Biederman 	/* branch should have named subdirectory as its first element */
7621f87f0b5SEric W. Biederman 	if (!s || !branch->child)
7631f87f0b5SEric W. Biederman 		return NULL;
7641f87f0b5SEric W. Biederman 
7651f87f0b5SEric W. Biederman 	/* ... and nothing else */
7661f87f0b5SEric W. Biederman 	if (branch[1].procname)
7671f87f0b5SEric W. Biederman 		return NULL;
7681f87f0b5SEric W. Biederman 
7691f87f0b5SEric W. Biederman 	/* table should contain subdirectory with the same name */
7701f87f0b5SEric W. Biederman 	for (p = table; p->procname; p++) {
7711f87f0b5SEric W. Biederman 		if (!p->child)
7721f87f0b5SEric W. Biederman 			continue;
7731f87f0b5SEric W. Biederman 		if (p->procname && strcmp(p->procname, s) == 0)
7741f87f0b5SEric W. Biederman 			return p;
7751f87f0b5SEric W. Biederman 	}
7761f87f0b5SEric W. Biederman 	return NULL;
7771f87f0b5SEric W. Biederman }
7781f87f0b5SEric W. Biederman 
7791f87f0b5SEric W. Biederman /* see if attaching q to p would be an improvement */
7801f87f0b5SEric W. Biederman static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
7811f87f0b5SEric W. Biederman {
7821f87f0b5SEric W. Biederman 	struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
7831f87f0b5SEric W. Biederman 	struct ctl_table *next;
7841f87f0b5SEric W. Biederman 	int is_better = 0;
7851f87f0b5SEric W. Biederman 	int not_in_parent = !p->attached_by;
7861f87f0b5SEric W. Biederman 
7871f87f0b5SEric W. Biederman 	while ((next = is_branch_in(by, to)) != NULL) {
7881f87f0b5SEric W. Biederman 		if (by == q->attached_by)
7891f87f0b5SEric W. Biederman 			is_better = 1;
7901f87f0b5SEric W. Biederman 		if (to == p->attached_by)
7911f87f0b5SEric W. Biederman 			not_in_parent = 1;
7921f87f0b5SEric W. Biederman 		by = by->child;
7931f87f0b5SEric W. Biederman 		to = next->child;
7941f87f0b5SEric W. Biederman 	}
7951f87f0b5SEric W. Biederman 
7961f87f0b5SEric W. Biederman 	if (is_better && not_in_parent) {
7971f87f0b5SEric W. Biederman 		q->attached_by = by;
7981f87f0b5SEric W. Biederman 		q->attached_to = to;
7991f87f0b5SEric W. Biederman 		q->parent = p;
8001f87f0b5SEric W. Biederman 	}
8011f87f0b5SEric W. Biederman }
8021f87f0b5SEric W. Biederman 
8037c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
8041f87f0b5SEric W. Biederman 	struct ctl_table *table)
8051f87f0b5SEric W. Biederman {
8067c60c48fSEric W. Biederman 	struct ctl_table *entry, *test;
8071f87f0b5SEric W. Biederman 	int error = 0;
8081f87f0b5SEric W. Biederman 
8097c60c48fSEric W. Biederman 	for (entry = old; entry->procname; entry++) {
8107c60c48fSEric W. Biederman 		for (test = table; test->procname; test++) {
8117c60c48fSEric W. Biederman 			if (strcmp(entry->procname, test->procname) == 0) {
8127c60c48fSEric W. Biederman 				printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
8137c60c48fSEric W. Biederman 					path, test->procname);
8147c60c48fSEric W. Biederman 				error = -EEXIST;
8151f87f0b5SEric W. Biederman 			}
8167c60c48fSEric W. Biederman 		}
8177c60c48fSEric W. Biederman 	}
8187c60c48fSEric W. Biederman 	return error;
8197c60c48fSEric W. Biederman }
8207c60c48fSEric W. Biederman 
8217c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces,
8227c60c48fSEric W. Biederman 	struct ctl_table_header *header,
8237c60c48fSEric W. Biederman 	const char *path, struct ctl_table *table)
8247c60c48fSEric W. Biederman {
8257c60c48fSEric W. Biederman 	struct ctl_table_root *root;
8267c60c48fSEric W. Biederman 	struct ctl_table_set *set;
8277c60c48fSEric W. Biederman 	struct ctl_table_header *dir_head, *head;
8287c60c48fSEric W. Biederman 	struct ctl_table *dir_table;
8297c60c48fSEric W. Biederman 	int error = 0;
8307c60c48fSEric W. Biederman 
8317c60c48fSEric W. Biederman 	/* No dups if we are the only member of our directory */
8327c60c48fSEric W. Biederman 	if (header->attached_by != table)
8337c60c48fSEric W. Biederman 		return 0;
8347c60c48fSEric W. Biederman 
8357c60c48fSEric W. Biederman 	dir_head = header->parent;
8367c60c48fSEric W. Biederman 	dir_table = header->attached_to;
8377c60c48fSEric W. Biederman 
8387c60c48fSEric W. Biederman 	error = sysctl_check_table_dups(path, dir_table, table);
8397c60c48fSEric W. Biederman 
8407c60c48fSEric W. Biederman 	root = &sysctl_table_root;
8417c60c48fSEric W. Biederman 	do {
8427c60c48fSEric W. Biederman 		set = lookup_header_set(root, namespaces);
8437c60c48fSEric W. Biederman 
8447c60c48fSEric W. Biederman 		list_for_each_entry(head, &set->list, ctl_entry) {
8457c60c48fSEric W. Biederman 			if (head->unregistering)
8467c60c48fSEric W. Biederman 				continue;
8477c60c48fSEric W. Biederman 			if (head->attached_to != dir_table)
8487c60c48fSEric W. Biederman 				continue;
8497c60c48fSEric W. Biederman 			error = sysctl_check_table_dups(path, head->attached_by,
8507c60c48fSEric W. Biederman 							table);
8517c60c48fSEric W. Biederman 		}
8527c60c48fSEric W. Biederman 		root = list_entry(root->root_list.next,
8537c60c48fSEric W. Biederman 				  struct ctl_table_root, root_list);
8547c60c48fSEric W. Biederman 	} while (root != &sysctl_table_root);
8557c60c48fSEric W. Biederman 	return error;
8567c60c48fSEric W. Biederman }
8577c60c48fSEric W. Biederman 
8587c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
8597c60c48fSEric W. Biederman {
8607c60c48fSEric W. Biederman 	struct va_format vaf;
8617c60c48fSEric W. Biederman 	va_list args;
8627c60c48fSEric W. Biederman 
8637c60c48fSEric W. Biederman 	va_start(args, fmt);
8647c60c48fSEric W. Biederman 	vaf.fmt = fmt;
8657c60c48fSEric W. Biederman 	vaf.va = &args;
8667c60c48fSEric W. Biederman 
8677c60c48fSEric W. Biederman 	printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
8687c60c48fSEric W. Biederman 		path, table->procname, &vaf);
8697c60c48fSEric W. Biederman 
8707c60c48fSEric W. Biederman 	va_end(args);
8717c60c48fSEric W. Biederman 	return -EINVAL;
8727c60c48fSEric W. Biederman }
8737c60c48fSEric W. Biederman 
8747c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
8757c60c48fSEric W. Biederman {
8767c60c48fSEric W. Biederman 	int err = 0;
8777c60c48fSEric W. Biederman 	for (; table->procname; table++) {
8787c60c48fSEric W. Biederman 		if (table->child)
8797c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "Not a file");
8807c60c48fSEric W. Biederman 
8811f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
8821f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
8831f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
8841f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
8851f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
8861f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
8871f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
8881f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
8891f87f0b5SEric W. Biederman 			if (!table->data)
8907c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No data");
8911f87f0b5SEric W. Biederman 			if (!table->maxlen)
8927c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No maxlen");
8931f87f0b5SEric W. Biederman 		}
8941f87f0b5SEric W. Biederman 		if (!table->proc_handler)
8957c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "No proc_handler");
8967c60c48fSEric W. Biederman 
8977c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
8987c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "bogus .mode 0%o",
8997c60c48fSEric W. Biederman 				table->mode);
9001f87f0b5SEric W. Biederman 	}
9017c60c48fSEric W. Biederman 	return err;
9021f87f0b5SEric W. Biederman }
9031f87f0b5SEric W. Biederman 
9041f87f0b5SEric W. Biederman /**
905f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
9061f87f0b5SEric W. Biederman  * @root: List of sysctl headers to register on
9071f87f0b5SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
9081f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
9091f87f0b5SEric W. Biederman  * @table: the top-level table structure
9101f87f0b5SEric W. Biederman  *
9111f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
9121f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
9131f87f0b5SEric W. Biederman  *
9141f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
9151f87f0b5SEric W. Biederman  *
9161f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
9171f87f0b5SEric W. Biederman  *            enter a sysctl file
9181f87f0b5SEric W. Biederman  *
9191f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
9201f87f0b5SEric W. Biederman  *
9211f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
9221f87f0b5SEric W. Biederman  *
923f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
9241f87f0b5SEric W. Biederman  *
925f728019bSEric W. Biederman  * child - must be %NULL.
9261f87f0b5SEric W. Biederman  *
9271f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
9281f87f0b5SEric W. Biederman  *
9291f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
9301f87f0b5SEric W. Biederman  *
9311f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
9321f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
9331f87f0b5SEric W. Biederman  *
934f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
935f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
9361f87f0b5SEric W. Biederman  *
9371f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
9381f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
9391f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
9401f87f0b5SEric W. Biederman  *
9411f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
9421f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
9431f87f0b5SEric W. Biederman  *
9441f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
9451f87f0b5SEric W. Biederman  * to the table header on success.
9461f87f0b5SEric W. Biederman  */
9476e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
9481f87f0b5SEric W. Biederman 	struct ctl_table_root *root,
9491f87f0b5SEric W. Biederman 	struct nsproxy *namespaces,
9506e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
9511f87f0b5SEric W. Biederman {
9521f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
9531f87f0b5SEric W. Biederman 	struct ctl_table *new, **prevp;
9546e9d5164SEric W. Biederman 	const char *name, *nextname;
9556e9d5164SEric W. Biederman 	unsigned int npath = 0;
9561f87f0b5SEric W. Biederman 	struct ctl_table_set *set;
957f05e53a7SEric W. Biederman 	size_t path_bytes = 0;
958f05e53a7SEric W. Biederman 	char *new_name;
9591f87f0b5SEric W. Biederman 
9601f87f0b5SEric W. Biederman 	/* Count the path components */
9616e9d5164SEric W. Biederman 	for (name = path; name; name = nextname) {
9626e9d5164SEric W. Biederman 		int namelen;
9636e9d5164SEric W. Biederman 		nextname = strchr(name, '/');
9646e9d5164SEric W. Biederman 		if (nextname) {
9656e9d5164SEric W. Biederman 			namelen = nextname - name;
9666e9d5164SEric W. Biederman 			nextname++;
9676e9d5164SEric W. Biederman 		} else {
9686e9d5164SEric W. Biederman 			namelen = strlen(name);
9696e9d5164SEric W. Biederman 		}
9706e9d5164SEric W. Biederman 		if (namelen == 0)
9716e9d5164SEric W. Biederman 			continue;
9726e9d5164SEric W. Biederman 		path_bytes += namelen + 1;
9736e9d5164SEric W. Biederman 		npath++;
9746e9d5164SEric W. Biederman 	}
9751f87f0b5SEric W. Biederman 
9761f87f0b5SEric W. Biederman 	/*
9771f87f0b5SEric W. Biederman 	 * For each path component, allocate a 2-element ctl_table array.
9781f87f0b5SEric W. Biederman 	 * The first array element will be filled with the sysctl entry
9791f87f0b5SEric W. Biederman 	 * for this, the second will be the sentinel (procname == 0).
9801f87f0b5SEric W. Biederman 	 *
9811f87f0b5SEric W. Biederman 	 * We allocate everything in one go so that we don't have to
9821f87f0b5SEric W. Biederman 	 * worry about freeing additional memory in unregister_sysctl_table.
9831f87f0b5SEric W. Biederman 	 */
984f05e53a7SEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
9851f87f0b5SEric W. Biederman 			 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
9861f87f0b5SEric W. Biederman 	if (!header)
9871f87f0b5SEric W. Biederman 		return NULL;
9881f87f0b5SEric W. Biederman 
9891f87f0b5SEric W. Biederman 	new = (struct ctl_table *) (header + 1);
990f05e53a7SEric W. Biederman 	new_name = (char *)(new + (2 * npath));
9911f87f0b5SEric W. Biederman 
9921f87f0b5SEric W. Biederman 	/* Now connect the dots */
9931f87f0b5SEric W. Biederman 	prevp = &header->ctl_table;
9946e9d5164SEric W. Biederman 	for (name = path; name; name = nextname) {
9956e9d5164SEric W. Biederman 		int namelen;
9966e9d5164SEric W. Biederman 		nextname = strchr(name, '/');
9976e9d5164SEric W. Biederman 		if (nextname) {
9986e9d5164SEric W. Biederman 			namelen = nextname - name;
9996e9d5164SEric W. Biederman 			nextname++;
10006e9d5164SEric W. Biederman 		} else {
10016e9d5164SEric W. Biederman 			namelen = strlen(name);
10026e9d5164SEric W. Biederman 		}
10036e9d5164SEric W. Biederman 		if (namelen == 0)
10046e9d5164SEric W. Biederman 			continue;
10056e9d5164SEric W. Biederman 		memcpy(new_name, name, namelen);
10066e9d5164SEric W. Biederman 		new_name[namelen] = '\0';
10076e9d5164SEric W. Biederman 
1008f05e53a7SEric W. Biederman 		new->procname = new_name;
10091f87f0b5SEric W. Biederman 		new->mode     = 0555;
10101f87f0b5SEric W. Biederman 
10111f87f0b5SEric W. Biederman 		*prevp = new;
10121f87f0b5SEric W. Biederman 		prevp = &new->child;
10131f87f0b5SEric W. Biederman 
10141f87f0b5SEric W. Biederman 		new += 2;
10156e9d5164SEric W. Biederman 		new_name += namelen + 1;
10161f87f0b5SEric W. Biederman 	}
10171f87f0b5SEric W. Biederman 	*prevp = table;
10181f87f0b5SEric W. Biederman 
1019e0d04529SEric W. Biederman 	init_header(header, root, NULL, table);
10207c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
10217c60c48fSEric W. Biederman 		goto fail;
10228d6ecfccSEric W. Biederman 
10231f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
10241f87f0b5SEric W. Biederman 	header->set = lookup_header_set(root, namespaces);
10251f87f0b5SEric W. Biederman 	header->attached_by = header->ctl_table;
1026a194558eSEric W. Biederman 	header->attached_to = &root_table[1];
10271f87f0b5SEric W. Biederman 	header->parent = &root_table_header;
1028bd295b56SEric W. Biederman 	set = header->set;
1029bd295b56SEric W. Biederman 	root = header->root;
1030bd295b56SEric W. Biederman 	for (;;) {
10311f87f0b5SEric W. Biederman 		struct ctl_table_header *p;
10321f87f0b5SEric W. Biederman 		list_for_each_entry(p, &set->list, ctl_entry) {
10331f87f0b5SEric W. Biederman 			if (p->unregistering)
10341f87f0b5SEric W. Biederman 				continue;
10351f87f0b5SEric W. Biederman 			try_attach(p, header);
10361f87f0b5SEric W. Biederman 		}
1037bd295b56SEric W. Biederman 		if (root == &sysctl_table_root)
1038bd295b56SEric W. Biederman 			break;
1039bd295b56SEric W. Biederman 		root = list_entry(root->root_list.prev,
1040bd295b56SEric W. Biederman 				  struct ctl_table_root, root_list);
1041bd295b56SEric W. Biederman 		set = lookup_header_set(root, namespaces);
10421f87f0b5SEric W. Biederman 	}
10437c60c48fSEric W. Biederman 	if (sysctl_check_dups(namespaces, header, path, table))
10447c60c48fSEric W. Biederman 		goto fail_locked;
10458425d6aaSEric W. Biederman 	insert_header(header);
10461f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
10471f87f0b5SEric W. Biederman 
10481f87f0b5SEric W. Biederman 	return header;
10497c60c48fSEric W. Biederman fail_locked:
10507c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
10517c60c48fSEric W. Biederman fail:
10527c60c48fSEric W. Biederman 	kfree(header);
10537c60c48fSEric W. Biederman 	dump_stack();
10547c60c48fSEric W. Biederman 	return NULL;
10551f87f0b5SEric W. Biederman }
10561f87f0b5SEric W. Biederman 
10576e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
10586e9d5164SEric W. Biederman {
10596e9d5164SEric W. Biederman 	int namelen;
10606e9d5164SEric W. Biederman 	namelen = strlen(name);
10616e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
10626e9d5164SEric W. Biederman 		return NULL;
10636e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
10646e9d5164SEric W. Biederman 	pos[namelen] = '/';
10656e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
10666e9d5164SEric W. Biederman 	pos += namelen + 1;
10676e9d5164SEric W. Biederman 	return pos;
10686e9d5164SEric W. Biederman }
10696e9d5164SEric W. Biederman 
1070f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1071f728019bSEric W. Biederman {
1072f728019bSEric W. Biederman 	int has_files = 0;
1073f728019bSEric W. Biederman 	int nr_subheaders = 0;
1074f728019bSEric W. Biederman 	struct ctl_table *entry;
1075f728019bSEric W. Biederman 
1076f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1077f728019bSEric W. Biederman 	if (!table || !table->procname)
1078f728019bSEric W. Biederman 		return 1;
1079f728019bSEric W. Biederman 
1080f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1081f728019bSEric W. Biederman 		if (entry->child)
1082f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1083f728019bSEric W. Biederman 		else
1084f728019bSEric W. Biederman 			has_files = 1;
1085f728019bSEric W. Biederman 	}
1086f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1087f728019bSEric W. Biederman }
1088f728019bSEric W. Biederman 
1089f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
1090f728019bSEric W. Biederman 	struct ctl_table_header ***subheader,
1091f728019bSEric W. Biederman 	struct ctl_table_root *root, struct nsproxy *namespaces,
1092f728019bSEric W. Biederman 	struct ctl_table *table)
1093f728019bSEric W. Biederman {
1094f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1095f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1096f728019bSEric W. Biederman 	int nr_files = 0;
1097f728019bSEric W. Biederman 	int nr_dirs = 0;
1098f728019bSEric W. Biederman 	int err = -ENOMEM;
1099f728019bSEric W. Biederman 
1100f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1101f728019bSEric W. Biederman 		if (entry->child)
1102f728019bSEric W. Biederman 			nr_dirs++;
1103f728019bSEric W. Biederman 		else
1104f728019bSEric W. Biederman 			nr_files++;
1105f728019bSEric W. Biederman 	}
1106f728019bSEric W. Biederman 
1107f728019bSEric W. Biederman 	files = table;
1108f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1109f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1110f728019bSEric W. Biederman 		struct ctl_table *new;
1111f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1112f728019bSEric W. Biederman 				GFP_KERNEL);
1113f728019bSEric W. Biederman 		if (!files)
1114f728019bSEric W. Biederman 			goto out;
1115f728019bSEric W. Biederman 
1116f728019bSEric W. Biederman 		ctl_table_arg = files;
1117f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1118f728019bSEric W. Biederman 			if (entry->child)
1119f728019bSEric W. Biederman 				continue;
1120f728019bSEric W. Biederman 			*new = *entry;
1121f728019bSEric W. Biederman 			new++;
1122f728019bSEric W. Biederman 		}
1123f728019bSEric W. Biederman 	}
1124f728019bSEric W. Biederman 
1125f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1126f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1127f728019bSEric W. Biederman 		struct ctl_table_header *header;
1128f728019bSEric W. Biederman 		header = __register_sysctl_table(root, namespaces, path, files);
1129f728019bSEric W. Biederman 		if (!header) {
1130f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1131f728019bSEric W. Biederman 			goto out;
1132f728019bSEric W. Biederman 		}
1133f728019bSEric W. Biederman 
1134f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1135f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1136f728019bSEric W. Biederman 		**subheader = header;
1137f728019bSEric W. Biederman 		(*subheader)++;
1138f728019bSEric W. Biederman 	}
1139f728019bSEric W. Biederman 
1140f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1141f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1142f728019bSEric W. Biederman 		char *child_pos;
1143f728019bSEric W. Biederman 
1144f728019bSEric W. Biederman 		if (!entry->child)
1145f728019bSEric W. Biederman 			continue;
1146f728019bSEric W. Biederman 
1147f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1148f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1149f728019bSEric W. Biederman 		if (!child_pos)
1150f728019bSEric W. Biederman 			goto out;
1151f728019bSEric W. Biederman 
1152f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
1153f728019bSEric W. Biederman 						  root, namespaces, entry->child);
1154f728019bSEric W. Biederman 		pos[0] = '\0';
1155f728019bSEric W. Biederman 		if (err)
1156f728019bSEric W. Biederman 			goto out;
1157f728019bSEric W. Biederman 	}
1158f728019bSEric W. Biederman 	err = 0;
1159f728019bSEric W. Biederman out:
1160f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1161f728019bSEric W. Biederman 	return err;
1162f728019bSEric W. Biederman }
1163f728019bSEric W. Biederman 
11646e9d5164SEric W. Biederman /**
11656e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
11666e9d5164SEric W. Biederman  * @root: List of sysctl headers to register on
11676e9d5164SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
11686e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11696e9d5164SEric W. Biederman  * @table: the top-level table structure
11706e9d5164SEric W. Biederman  *
11716e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11726e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11736e9d5164SEric W. Biederman  *
11746e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
11756e9d5164SEric W. Biederman  */
11766e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
11776e9d5164SEric W. Biederman 	struct ctl_table_root *root,
11786e9d5164SEric W. Biederman 	struct nsproxy *namespaces,
11796e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
11806e9d5164SEric W. Biederman {
1181ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1182f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1183f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
11846e9d5164SEric W. Biederman 	const struct ctl_path *component;
11856e9d5164SEric W. Biederman 	char *new_path, *pos;
11866e9d5164SEric W. Biederman 
11876e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
11886e9d5164SEric W. Biederman 	if (!new_path)
11896e9d5164SEric W. Biederman 		return NULL;
11906e9d5164SEric W. Biederman 
11916e9d5164SEric W. Biederman 	pos[0] = '\0';
11926e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
11936e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
11946e9d5164SEric W. Biederman 		if (!pos)
11956e9d5164SEric W. Biederman 			goto out;
11966e9d5164SEric W. Biederman 	}
1197ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1198ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1199ec6a5266SEric W. Biederman 		if (!pos)
1200ec6a5266SEric W. Biederman 			goto out;
1201ec6a5266SEric W. Biederman 		table = table->child;
1202ec6a5266SEric W. Biederman 	}
1203f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
12046e9d5164SEric W. Biederman 		header = __register_sysctl_table(root, namespaces, new_path, table);
1205ec6a5266SEric W. Biederman 		if (header)
1206ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1207f728019bSEric W. Biederman 	} else {
1208f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1209f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1210f728019bSEric W. Biederman 		if (!header)
1211f728019bSEric W. Biederman 			goto out;
1212f728019bSEric W. Biederman 
1213f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1214f728019bSEric W. Biederman 		subheader = subheaders;
1215f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1216f728019bSEric W. Biederman 
1217f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1218f728019bSEric W. Biederman 						root, namespaces, table))
1219f728019bSEric W. Biederman 			goto err_register_leaves;
1220f728019bSEric W. Biederman 	}
1221f728019bSEric W. Biederman 
12226e9d5164SEric W. Biederman out:
12236e9d5164SEric W. Biederman 	kfree(new_path);
12246e9d5164SEric W. Biederman 	return header;
1225f728019bSEric W. Biederman 
1226f728019bSEric W. Biederman err_register_leaves:
1227f728019bSEric W. Biederman 	while (subheader > subheaders) {
1228f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1229f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1230f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1231f728019bSEric W. Biederman 		kfree(table);
1232f728019bSEric W. Biederman 	}
1233f728019bSEric W. Biederman 	kfree(header);
1234f728019bSEric W. Biederman 	header = NULL;
1235f728019bSEric W. Biederman 	goto out;
12366e9d5164SEric W. Biederman }
12376e9d5164SEric W. Biederman 
12381f87f0b5SEric W. Biederman /**
12391f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
12401f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
12411f87f0b5SEric W. Biederman  * @table: the top-level table structure
12421f87f0b5SEric W. Biederman  *
12431f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12441f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12451f87f0b5SEric W. Biederman  *
12461f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
12471f87f0b5SEric W. Biederman  */
12481f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
12491f87f0b5SEric W. Biederman 						struct ctl_table *table)
12501f87f0b5SEric W. Biederman {
12511f87f0b5SEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
12521f87f0b5SEric W. Biederman 					path, table);
12531f87f0b5SEric W. Biederman }
12541f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
12551f87f0b5SEric W. Biederman 
12561f87f0b5SEric W. Biederman /**
12571f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
12581f87f0b5SEric W. Biederman  * @table: the top-level table structure
12591f87f0b5SEric W. Biederman  *
12601f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12611f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12621f87f0b5SEric W. Biederman  *
12631f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
12641f87f0b5SEric W. Biederman  */
12651f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
12661f87f0b5SEric W. Biederman {
12671f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
12681f87f0b5SEric W. Biederman 
12691f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
12701f87f0b5SEric W. Biederman }
12711f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
12721f87f0b5SEric W. Biederman 
1273938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1274938aaa4fSEric W. Biederman {
1275938aaa4fSEric W. Biederman 	if (--header->nreg)
1276938aaa4fSEric W. Biederman 		return;
1277938aaa4fSEric W. Biederman 
1278938aaa4fSEric W. Biederman 	start_unregistering(header);
1279938aaa4fSEric W. Biederman 	if (!--header->parent->count) {
1280938aaa4fSEric W. Biederman 		WARN_ON(1);
1281938aaa4fSEric W. Biederman 		kfree_rcu(header->parent, rcu);
1282938aaa4fSEric W. Biederman 	}
1283938aaa4fSEric W. Biederman 	if (!--header->count)
1284938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
1285938aaa4fSEric W. Biederman }
1286938aaa4fSEric W. Biederman 
12871f87f0b5SEric W. Biederman /**
12881f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
12891f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
12901f87f0b5SEric W. Biederman  *
12911f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
12921f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
12931f87f0b5SEric W. Biederman  */
12941f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
12951f87f0b5SEric W. Biederman {
1296f728019bSEric W. Biederman 	int nr_subheaders;
12971f87f0b5SEric W. Biederman 	might_sleep();
12981f87f0b5SEric W. Biederman 
12991f87f0b5SEric W. Biederman 	if (header == NULL)
13001f87f0b5SEric W. Biederman 		return;
13011f87f0b5SEric W. Biederman 
1302f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1303f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1304f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1305f728019bSEric W. Biederman 		int i;
1306f728019bSEric W. Biederman 
1307f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1308f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1309f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1310f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1311f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1312f728019bSEric W. Biederman 			kfree(table);
1313f728019bSEric W. Biederman 		}
1314f728019bSEric W. Biederman 		kfree(header);
1315f728019bSEric W. Biederman 		return;
1316f728019bSEric W. Biederman 	}
1317f728019bSEric W. Biederman 
13181f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1319938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
13201f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13211f87f0b5SEric W. Biederman }
13221f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
13231f87f0b5SEric W. Biederman 
13241f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p,
13251f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
13261f87f0b5SEric W. Biederman {
13271f87f0b5SEric W. Biederman 	INIT_LIST_HEAD(&p->list);
13281f87f0b5SEric W. Biederman 	p->is_seen = is_seen;
13291f87f0b5SEric W. Biederman }
13301f87f0b5SEric W. Biederman 
133197324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
133297324cd8SEric W. Biederman {
133397324cd8SEric W. Biederman 	WARN_ON(!list_empty(&set->list));
133497324cd8SEric W. Biederman }
13351f87f0b5SEric W. Biederman 
13361e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
133777b14db5SEric W. Biederman {
1338e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1339e1675231SAlexey Dobriyan 
134077b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
13419043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
13429043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
134377b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1344de4e83bdSEric W. Biederman 
1345de4e83bdSEric W. Biederman 	return sysctl_init();
134677b14db5SEric W. Biederman }
1347