xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 6980128f)
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 = "",
317ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
32a194558eSEric W. Biederman 	},
33a194558eSEric W. Biederman 	{ }
34a194558eSEric W. Biederman };
351f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root;
367ec66d06SEric W. Biederman static struct ctl_dir sysctl_root_dir = {
377ec66d06SEric W. Biederman 	.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,
447ec66d06SEric W. Biederman 	},
451f87f0b5SEric W. Biederman };
461f87f0b5SEric W. Biederman static struct ctl_table_root sysctl_table_root = {
471f87f0b5SEric W. Biederman 	.root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
487ec66d06SEric W. Biederman 	.default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry),
499eb47c26SEric W. Biederman 	.default_set.root = &sysctl_table_root,
501f87f0b5SEric W. Biederman };
511f87f0b5SEric W. Biederman 
521f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
531f87f0b5SEric W. Biederman 
547ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
557ec66d06SEric W. Biederman 
566980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
576980128fSEric W. Biederman {
586980128fSEric W. Biederman 	if (dir->header.parent)
596980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
606980128fSEric W. Biederman 	printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
616980128fSEric W. Biederman }
626980128fSEric W. Biederman 
63076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
64076c3eedSEric W. Biederman {
65076c3eedSEric W. Biederman 	int minlen;
66076c3eedSEric W. Biederman 	int cmp;
67076c3eedSEric W. Biederman 
68076c3eedSEric W. Biederman 	minlen = len1;
69076c3eedSEric W. Biederman 	if (minlen > len2)
70076c3eedSEric W. Biederman 		minlen = len2;
71076c3eedSEric W. Biederman 
72076c3eedSEric W. Biederman 	cmp = memcmp(name1, name2, minlen);
73076c3eedSEric W. Biederman 	if (cmp == 0)
74076c3eedSEric W. Biederman 		cmp = len1 - len2;
75076c3eedSEric W. Biederman 	return cmp;
76076c3eedSEric W. Biederman }
77076c3eedSEric W. Biederman 
78076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
797ec66d06SEric W. Biederman 	struct ctl_table_set *set, struct ctl_dir *dir,
80076c3eedSEric W. Biederman 	const char *name, int namelen)
81076c3eedSEric W. Biederman {
82076c3eedSEric W. Biederman 	struct ctl_table_header *head;
83076c3eedSEric W. Biederman 	struct ctl_table *entry;
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;
887ec66d06SEric W. Biederman 		if (head->parent != dir)
89076c3eedSEric W. Biederman 			continue;
907ec66d06SEric W. Biederman 		for (entry = head->ctl_table; 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 {
1057ec66d06SEric W. Biederman 	head->ctl_table = table;
106e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
107e0d04529SEric W. Biederman 	INIT_LIST_HEAD(&head->ctl_entry);
108e0d04529SEric W. Biederman 	head->used = 0;
109e0d04529SEric W. Biederman 	head->count = 1;
110e0d04529SEric W. Biederman 	head->nreg = 1;
111e0d04529SEric W. Biederman 	head->unregistering = NULL;
112e0d04529SEric W. Biederman 	head->root = root;
113e0d04529SEric W. Biederman 	head->set = set;
114e0d04529SEric W. Biederman 	head->parent = NULL;
115e0d04529SEric W. Biederman }
116e0d04529SEric W. Biederman 
1178425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
1188425d6aaSEric W. Biederman {
1198425d6aaSEric W. Biederman 	list_del_init(&head->ctl_entry);
1208425d6aaSEric W. Biederman }
1218425d6aaSEric W. Biederman 
1227ec66d06SEric W. Biederman static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
1238425d6aaSEric W. Biederman {
1247ec66d06SEric W. Biederman 	header->parent = dir;
1257ec66d06SEric W. Biederman 	header->parent->header.nreg++;
1268425d6aaSEric W. Biederman 	list_add_tail(&header->ctl_entry, &header->set->list);
1278425d6aaSEric W. Biederman }
1288425d6aaSEric W. Biederman 
1291f87f0b5SEric W. Biederman /* called under sysctl_lock */
1301f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
1311f87f0b5SEric W. Biederman {
1321f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
1331f87f0b5SEric W. Biederman 		return 0;
1341f87f0b5SEric W. Biederman 	p->used++;
1351f87f0b5SEric W. Biederman 	return 1;
1361f87f0b5SEric W. Biederman }
1371f87f0b5SEric W. Biederman 
1381f87f0b5SEric W. Biederman /* called under sysctl_lock */
1391f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
1401f87f0b5SEric W. Biederman {
1411f87f0b5SEric W. Biederman 	if (!--p->used)
1421f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
1431f87f0b5SEric W. Biederman 			complete(p->unregistering);
1441f87f0b5SEric W. Biederman }
1451f87f0b5SEric W. Biederman 
1461f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
1471f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
1481f87f0b5SEric W. Biederman {
1491f87f0b5SEric W. Biederman 	/*
1501f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
1511f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
1521f87f0b5SEric W. Biederman 	 */
1531f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
1541f87f0b5SEric W. Biederman 		struct completion wait;
1551f87f0b5SEric W. Biederman 		init_completion(&wait);
1561f87f0b5SEric W. Biederman 		p->unregistering = &wait;
1571f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
1581f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
1591f87f0b5SEric W. Biederman 		spin_lock(&sysctl_lock);
1601f87f0b5SEric W. Biederman 	} else {
1611f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
1621f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
1631f87f0b5SEric W. Biederman 	}
1641f87f0b5SEric W. Biederman 	/*
1651f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
1661f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
1671f87f0b5SEric W. Biederman 	 */
1688425d6aaSEric W. Biederman 	erase_header(p);
1691f87f0b5SEric W. Biederman }
1701f87f0b5SEric W. Biederman 
1711f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head)
1721f87f0b5SEric W. Biederman {
1731f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1741f87f0b5SEric W. Biederman 	head->count++;
1751f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1761f87f0b5SEric W. Biederman }
1771f87f0b5SEric W. Biederman 
1781f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head)
1791f87f0b5SEric W. Biederman {
1801f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1811f87f0b5SEric W. Biederman 	if (!--head->count)
1821f87f0b5SEric W. Biederman 		kfree_rcu(head, rcu);
1831f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1841f87f0b5SEric W. Biederman }
1851f87f0b5SEric W. Biederman 
1861f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1871f87f0b5SEric W. Biederman {
1881f87f0b5SEric W. Biederman 	if (!head)
1891f87f0b5SEric W. Biederman 		BUG();
1901f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1911f87f0b5SEric W. Biederman 	if (!use_table(head))
1921f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
1931f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
1941f87f0b5SEric W. Biederman 	return head;
1951f87f0b5SEric W. Biederman }
1961f87f0b5SEric W. Biederman 
1971f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
1981f87f0b5SEric W. Biederman {
1991f87f0b5SEric W. Biederman 	if (!head)
2001f87f0b5SEric W. Biederman 		return;
2011f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
2021f87f0b5SEric W. Biederman 	unuse_table(head);
2031f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
2041f87f0b5SEric W. Biederman }
2051f87f0b5SEric W. Biederman 
2061f87f0b5SEric W. Biederman static struct ctl_table_set *
2071f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
2081f87f0b5SEric W. Biederman {
2091f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
2101f87f0b5SEric W. Biederman 	if (root->lookup)
2111f87f0b5SEric W. Biederman 		set = root->lookup(root, namespaces);
2121f87f0b5SEric W. Biederman 	return set;
2131f87f0b5SEric W. Biederman }
2141f87f0b5SEric W. Biederman 
2151f87f0b5SEric W. Biederman static struct list_head *
2161f87f0b5SEric W. Biederman lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
2171f87f0b5SEric W. Biederman {
2181f87f0b5SEric W. Biederman 	struct ctl_table_set *set = lookup_header_set(root, namespaces);
2191f87f0b5SEric W. Biederman 	return &set->list;
2201f87f0b5SEric W. Biederman }
2211f87f0b5SEric W. Biederman 
222076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
2237ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
224076c3eedSEric W. Biederman 				      const char *name, int namelen)
225076c3eedSEric W. Biederman {
226076c3eedSEric W. Biederman 	struct ctl_table_header *head;
227076c3eedSEric W. Biederman 	struct ctl_table *entry;
228076c3eedSEric W. Biederman 	struct ctl_table_root *root;
229076c3eedSEric W. Biederman 	struct ctl_table_set *set;
230076c3eedSEric W. Biederman 
231076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
232076c3eedSEric W. Biederman 	root = &sysctl_table_root;
233076c3eedSEric W. Biederman 	do {
234076c3eedSEric W. Biederman 		set = lookup_header_set(root, current->nsproxy);
2357ec66d06SEric W. Biederman 		entry = find_entry(&head, set, dir, name, namelen);
236076c3eedSEric W. Biederman 		if (entry && use_table(head))
237076c3eedSEric W. Biederman 			*phead = head;
238076c3eedSEric W. Biederman 		else
239076c3eedSEric W. Biederman 			entry = NULL;
240076c3eedSEric W. Biederman 		root = list_entry(root->root_list.next,
241076c3eedSEric W. Biederman 				  struct ctl_table_root, root_list);
242076c3eedSEric W. Biederman 	} while (!entry && root != &sysctl_table_root);
243076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
244076c3eedSEric W. Biederman 	return entry;
245076c3eedSEric W. Biederman }
246076c3eedSEric W. Biederman 
2477ec66d06SEric W. Biederman static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
2486a75ce16SEric W. Biederman 	struct ctl_table_root *root, struct list_head *tmp)
2491f87f0b5SEric W. Biederman {
2506a75ce16SEric W. Biederman 	struct nsproxy *namespaces = current->nsproxy;
2511f87f0b5SEric W. Biederman 	struct list_head *header_list;
2521f87f0b5SEric W. Biederman 	struct ctl_table_header *head;
2531f87f0b5SEric W. Biederman 
2541f87f0b5SEric W. Biederman 	goto next;
2551f87f0b5SEric W. Biederman 	for (;;) {
2561f87f0b5SEric W. Biederman 		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
2576a75ce16SEric W. Biederman 		root = head->root;
2581f87f0b5SEric W. Biederman 
2597ec66d06SEric W. Biederman 		if (head->parent != dir ||
2607ec66d06SEric W. Biederman 		    !head->ctl_table->procname ||
2616a75ce16SEric W. Biederman 		    !use_table(head))
2621f87f0b5SEric W. Biederman 			goto next;
2636a75ce16SEric W. Biederman 
2641f87f0b5SEric W. Biederman 		return head;
2651f87f0b5SEric W. Biederman 	next:
2661f87f0b5SEric W. Biederman 		tmp = tmp->next;
2671f87f0b5SEric W. Biederman 		header_list = lookup_header_list(root, namespaces);
2681f87f0b5SEric W. Biederman 		if (tmp != header_list)
2691f87f0b5SEric W. Biederman 			continue;
2701f87f0b5SEric W. Biederman 
2711f87f0b5SEric W. Biederman 		do {
2721f87f0b5SEric W. Biederman 			root = list_entry(root->root_list.next,
2731f87f0b5SEric W. Biederman 					struct ctl_table_root, root_list);
2741f87f0b5SEric W. Biederman 			if (root == &sysctl_table_root)
2751f87f0b5SEric W. Biederman 				goto out;
2761f87f0b5SEric W. Biederman 			header_list = lookup_header_list(root, namespaces);
2771f87f0b5SEric W. Biederman 		} while (list_empty(header_list));
2781f87f0b5SEric W. Biederman 		tmp = header_list->next;
2791f87f0b5SEric W. Biederman 	}
2801f87f0b5SEric W. Biederman out:
2811f87f0b5SEric W. Biederman 	return NULL;
2821f87f0b5SEric W. Biederman }
2831f87f0b5SEric W. Biederman 
2847ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
2856a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
2861f87f0b5SEric W. Biederman {
2877ec66d06SEric W. Biederman 	struct ctl_table_header *head;
2887ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
2896a75ce16SEric W. Biederman 
2906a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
2916a75ce16SEric W. Biederman 	head = next_usable_entry(dir, &sysctl_table_root,
2926a75ce16SEric W. Biederman 				 &sysctl_table_root.default_set.list);
2936a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
2947ec66d06SEric W. Biederman 	if (head)
2957ec66d06SEric W. Biederman 		entry = head->ctl_table;
2966a75ce16SEric W. Biederman 	*phead = head;
2976a75ce16SEric W. Biederman 	*pentry = entry;
2986a75ce16SEric W. Biederman }
2996a75ce16SEric W. Biederman 
3007ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
3016a75ce16SEric W. Biederman {
3026a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
3036a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
3046a75ce16SEric W. Biederman 
3056a75ce16SEric W. Biederman 	entry++;
3066a75ce16SEric W. Biederman 	if (!entry->procname) {
3076a75ce16SEric W. Biederman 		spin_lock(&sysctl_lock);
3086a75ce16SEric W. Biederman 		unuse_table(head);
3097ec66d06SEric W. Biederman 		head = next_usable_entry(head->parent, head->root, &head->ctl_entry);
3106a75ce16SEric W. Biederman 		spin_unlock(&sysctl_lock);
3116a75ce16SEric W. Biederman 		if (head)
3127ec66d06SEric W. Biederman 			entry = head->ctl_table;
3136a75ce16SEric W. Biederman 	}
3146a75ce16SEric W. Biederman 	*phead = head;
3156a75ce16SEric W. Biederman 	*pentry = entry;
3161f87f0b5SEric W. Biederman }
3171f87f0b5SEric W. Biederman 
3181f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root)
3191f87f0b5SEric W. Biederman {
3201f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3211f87f0b5SEric W. Biederman 	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
3221f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3231f87f0b5SEric W. Biederman }
3241f87f0b5SEric W. Biederman 
3251f87f0b5SEric W. Biederman /*
3261f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
3271f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
3281f87f0b5SEric W. Biederman  */
3291f87f0b5SEric W. Biederman 
3301f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
3311f87f0b5SEric W. Biederman {
3321f87f0b5SEric W. Biederman 	if (!current_euid())
3331f87f0b5SEric W. Biederman 		mode >>= 6;
3341f87f0b5SEric W. Biederman 	else if (in_egroup_p(0))
3351f87f0b5SEric W. Biederman 		mode >>= 3;
3361f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
3371f87f0b5SEric W. Biederman 		return 0;
3381f87f0b5SEric W. Biederman 	return -EACCES;
3391f87f0b5SEric W. Biederman }
3401f87f0b5SEric W. Biederman 
3411f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
3421f87f0b5SEric W. Biederman {
3431f87f0b5SEric W. Biederman 	int mode;
3441f87f0b5SEric W. Biederman 
3451f87f0b5SEric W. Biederman 	if (root->permissions)
3461f87f0b5SEric W. Biederman 		mode = root->permissions(root, current->nsproxy, table);
3471f87f0b5SEric W. Biederman 	else
3481f87f0b5SEric W. Biederman 		mode = table->mode;
3491f87f0b5SEric W. Biederman 
3501f87f0b5SEric W. Biederman 	return test_perm(mode, op);
3511f87f0b5SEric W. Biederman }
3521f87f0b5SEric W. Biederman 
3539043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
3549043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
35577b14db5SEric W. Biederman {
35677b14db5SEric W. Biederman 	struct inode *inode;
3579043476fSAl Viro 	struct proc_inode *ei;
35877b14db5SEric W. Biederman 
3599043476fSAl Viro 	inode = new_inode(sb);
36077b14db5SEric W. Biederman 	if (!inode)
36177b14db5SEric W. Biederman 		goto out;
36277b14db5SEric W. Biederman 
36385fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
36485fe4025SChristoph Hellwig 
3659043476fSAl Viro 	sysctl_head_get(head);
36677b14db5SEric W. Biederman 	ei = PROC_I(inode);
3679043476fSAl Viro 	ei->sysctl = head;
3689043476fSAl Viro 	ei->sysctl_entry = table;
3699043476fSAl Viro 
37077b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3719043476fSAl Viro 	inode->i_mode = table->mode;
3727ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
3739043476fSAl Viro 		inode->i_mode |= S_IFREG;
37477b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
37577b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
3769043476fSAl Viro 	} else {
3779043476fSAl Viro 		inode->i_mode |= S_IFDIR;
3789043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
3799043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
3809043476fSAl Viro 	}
38177b14db5SEric W. Biederman out:
38277b14db5SEric W. Biederman 	return inode;
38377b14db5SEric W. Biederman }
38477b14db5SEric W. Biederman 
38581324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
38677b14db5SEric W. Biederman {
3873cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
3883cc3e046SEric W. Biederman 	if (!head)
3897ec66d06SEric W. Biederman 		head = &sysctl_root_dir.header;
3903cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
39177b14db5SEric W. Biederman }
39277b14db5SEric W. Biederman 
39377b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
39477b14db5SEric W. Biederman 					struct nameidata *nd)
39577b14db5SEric W. Biederman {
3969043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
3979043476fSAl Viro 	struct ctl_table_header *h = NULL;
3989043476fSAl Viro 	struct qstr *name = &dentry->d_name;
3999043476fSAl Viro 	struct ctl_table *p;
40077b14db5SEric W. Biederman 	struct inode *inode;
4019043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
4027ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
40377b14db5SEric W. Biederman 
4049043476fSAl Viro 	if (IS_ERR(head))
4059043476fSAl Viro 		return ERR_CAST(head);
4069043476fSAl Viro 
4077ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
4089043476fSAl Viro 
4097ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
4109043476fSAl Viro 	if (!p)
41177b14db5SEric W. Biederman 		goto out;
41277b14db5SEric W. Biederman 
41377b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
4149043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
4159043476fSAl Viro 	if (h)
4169043476fSAl Viro 		sysctl_head_finish(h);
4179043476fSAl Viro 
41877b14db5SEric W. Biederman 	if (!inode)
41977b14db5SEric W. Biederman 		goto out;
42077b14db5SEric W. Biederman 
42177b14db5SEric W. Biederman 	err = NULL;
422fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
42377b14db5SEric W. Biederman 	d_add(dentry, inode);
42477b14db5SEric W. Biederman 
42577b14db5SEric W. Biederman out:
42677b14db5SEric W. Biederman 	sysctl_head_finish(head);
42777b14db5SEric W. Biederman 	return err;
42877b14db5SEric W. Biederman }
42977b14db5SEric W. Biederman 
4307708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
4317708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
43277b14db5SEric W. Biederman {
4339043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
4349043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
4359043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
4362a2da53bSDavid Howells 	ssize_t error;
4372a2da53bSDavid Howells 	size_t res;
43877b14db5SEric W. Biederman 
4399043476fSAl Viro 	if (IS_ERR(head))
4409043476fSAl Viro 		return PTR_ERR(head);
44177b14db5SEric W. Biederman 
44277b14db5SEric W. Biederman 	/*
44377b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
44477b14db5SEric W. Biederman 	 * and won't be until we finish.
44577b14db5SEric W. Biederman 	 */
44677b14db5SEric W. Biederman 	error = -EPERM;
447d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
44877b14db5SEric W. Biederman 		goto out;
44977b14db5SEric W. Biederman 
4509043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
4519043476fSAl Viro 	error = -EINVAL;
4529043476fSAl Viro 	if (!table->proc_handler)
4539043476fSAl Viro 		goto out;
4549043476fSAl Viro 
45577b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
45677b14db5SEric W. Biederman 	res = count;
4578d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
45877b14db5SEric W. Biederman 	if (!error)
45977b14db5SEric W. Biederman 		error = res;
46077b14db5SEric W. Biederman out:
46177b14db5SEric W. Biederman 	sysctl_head_finish(head);
46277b14db5SEric W. Biederman 
46377b14db5SEric W. Biederman 	return error;
46477b14db5SEric W. Biederman }
46577b14db5SEric W. Biederman 
4667708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
4677708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
4687708bfb1SPavel Emelyanov {
4697708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
4707708bfb1SPavel Emelyanov }
4717708bfb1SPavel Emelyanov 
47277b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
47377b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
47477b14db5SEric W. Biederman {
4757708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
47677b14db5SEric W. Biederman }
47777b14db5SEric W. Biederman 
478f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
479f1ecf068SLucas De Marchi {
480f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
481f1ecf068SLucas De Marchi 
482f1ecf068SLucas De Marchi 	if (table->poll)
483f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
484f1ecf068SLucas De Marchi 
485f1ecf068SLucas De Marchi 	return 0;
486f1ecf068SLucas De Marchi }
487f1ecf068SLucas De Marchi 
488f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
489f1ecf068SLucas De Marchi {
490f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
491f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
492f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
493f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
494f1ecf068SLucas De Marchi 
495f1ecf068SLucas De Marchi 	if (!table->proc_handler)
496f1ecf068SLucas De Marchi 		goto out;
497f1ecf068SLucas De Marchi 
498f1ecf068SLucas De Marchi 	if (!table->poll)
499f1ecf068SLucas De Marchi 		goto out;
500f1ecf068SLucas De Marchi 
501f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
502f1ecf068SLucas De Marchi 
503f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
504f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
505f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
506f1ecf068SLucas De Marchi 	}
507f1ecf068SLucas De Marchi 
508f1ecf068SLucas De Marchi out:
509f1ecf068SLucas De Marchi 	return ret;
510f1ecf068SLucas De Marchi }
51177b14db5SEric W. Biederman 
51277b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
5139043476fSAl Viro 				filldir_t filldir,
5149043476fSAl Viro 				struct ctl_table_header *head,
5159043476fSAl Viro 				struct ctl_table *table)
51677b14db5SEric W. Biederman {
51777b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
51877b14db5SEric W. Biederman 	struct inode *inode;
51977b14db5SEric W. Biederman 	struct qstr qname;
52077b14db5SEric W. Biederman 	ino_t ino = 0;
52177b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
52277b14db5SEric W. Biederman 
52377b14db5SEric W. Biederman 	qname.name = table->procname;
52477b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
52577b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
52677b14db5SEric W. Biederman 
52777b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
52877b14db5SEric W. Biederman 	if (!child) {
5299043476fSAl Viro 		child = d_alloc(dir, &qname);
5309043476fSAl Viro 		if (child) {
5319043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
5329043476fSAl Viro 			if (!inode) {
5339043476fSAl Viro 				dput(child);
5349043476fSAl Viro 				return -ENOMEM;
5359043476fSAl Viro 			} else {
536fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
5379043476fSAl Viro 				d_add(child, inode);
53877b14db5SEric W. Biederman 			}
5399043476fSAl Viro 		} else {
5409043476fSAl Viro 			return -ENOMEM;
54177b14db5SEric W. Biederman 		}
54277b14db5SEric W. Biederman 	}
54377b14db5SEric W. Biederman 	inode = child->d_inode;
54477b14db5SEric W. Biederman 	ino  = inode->i_ino;
54577b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
54677b14db5SEric W. Biederman 	dput(child);
5479043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
5489043476fSAl Viro }
5499043476fSAl Viro 
5509043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
5519043476fSAl Viro 		unsigned long *pos, struct file *file,
5529043476fSAl Viro 		void *dirent, filldir_t filldir)
5539043476fSAl Viro {
5549043476fSAl Viro 	int res;
5559043476fSAl Viro 
5566a75ce16SEric W. Biederman 	if ((*pos)++ < file->f_pos)
5576a75ce16SEric W. Biederman 		return 0;
5589043476fSAl Viro 
5599043476fSAl Viro 	res = proc_sys_fill_cache(file, dirent, filldir, head, table);
5609043476fSAl Viro 
5616a75ce16SEric W. Biederman 	if (res == 0)
5626a75ce16SEric W. Biederman 		file->f_pos = *pos;
5636a75ce16SEric W. Biederman 
5646a75ce16SEric W. Biederman 	return res;
56577b14db5SEric W. Biederman }
56677b14db5SEric W. Biederman 
56777b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
56877b14db5SEric W. Biederman {
5699043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
57077b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
5719043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5729043476fSAl Viro 	struct ctl_table_header *h = NULL;
5736a75ce16SEric W. Biederman 	struct ctl_table *entry;
5747ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
57577b14db5SEric W. Biederman 	unsigned long pos;
5769043476fSAl Viro 	int ret = -EINVAL;
57777b14db5SEric W. Biederman 
5789043476fSAl Viro 	if (IS_ERR(head))
5799043476fSAl Viro 		return PTR_ERR(head);
5809043476fSAl Viro 
5817ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
58277b14db5SEric W. Biederman 
58377b14db5SEric W. Biederman 	ret = 0;
58477b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
58577b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
58677b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
58777b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
58877b14db5SEric W. Biederman 			goto out;
58977b14db5SEric W. Biederman 		filp->f_pos++;
59077b14db5SEric W. Biederman 	}
59177b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
59277b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
59377b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
59477b14db5SEric W. Biederman 			goto out;
59577b14db5SEric W. Biederman 		filp->f_pos++;
59677b14db5SEric W. Biederman 	}
59777b14db5SEric W. Biederman 	pos = 2;
59877b14db5SEric W. Biederman 
5997ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
6006a75ce16SEric W. Biederman 		ret = scan(h, entry, &pos, filp, dirent, filldir);
6019043476fSAl Viro 		if (ret) {
6029043476fSAl Viro 			sysctl_head_finish(h);
6039043476fSAl Viro 			break;
60477b14db5SEric W. Biederman 		}
60577b14db5SEric W. Biederman 	}
60677b14db5SEric W. Biederman 	ret = 1;
60777b14db5SEric W. Biederman out:
60877b14db5SEric W. Biederman 	sysctl_head_finish(head);
60977b14db5SEric W. Biederman 	return ret;
61077b14db5SEric W. Biederman }
61177b14db5SEric W. Biederman 
61210556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
61377b14db5SEric W. Biederman {
61477b14db5SEric W. Biederman 	/*
61577b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
61677b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
61777b14db5SEric W. Biederman 	 */
618f696a365SMiklos Szeredi 	struct ctl_table_header *head;
619f696a365SMiklos Szeredi 	struct ctl_table *table;
62077b14db5SEric W. Biederman 	int error;
62177b14db5SEric W. Biederman 
622f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
623f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
624f696a365SMiklos Szeredi 		return -EACCES;
625f696a365SMiklos Szeredi 
626f696a365SMiklos Szeredi 	head = grab_header(inode);
6279043476fSAl Viro 	if (IS_ERR(head))
6289043476fSAl Viro 		return PTR_ERR(head);
62977b14db5SEric W. Biederman 
630f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
6319043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
6329043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
6339043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
6341fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
6359043476fSAl Viro 
63677b14db5SEric W. Biederman 	sysctl_head_finish(head);
63777b14db5SEric W. Biederman 	return error;
63877b14db5SEric W. Biederman }
63977b14db5SEric W. Biederman 
64077b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
64177b14db5SEric W. Biederman {
64277b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
64377b14db5SEric W. Biederman 	int error;
64477b14db5SEric W. Biederman 
64577b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
64677b14db5SEric W. Biederman 		return -EPERM;
64777b14db5SEric W. Biederman 
64877b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
6491025774cSChristoph Hellwig 	if (error)
65077b14db5SEric W. Biederman 		return error;
6511025774cSChristoph Hellwig 
6521025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
6531025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
6541025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
6551025774cSChristoph Hellwig 		if (error)
6561025774cSChristoph Hellwig 			return error;
6571025774cSChristoph Hellwig 	}
6581025774cSChristoph Hellwig 
6591025774cSChristoph Hellwig 	setattr_copy(inode, attr);
6601025774cSChristoph Hellwig 	mark_inode_dirty(inode);
6611025774cSChristoph Hellwig 	return 0;
66277b14db5SEric W. Biederman }
66377b14db5SEric W. Biederman 
6649043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
6659043476fSAl Viro {
6669043476fSAl Viro 	struct inode *inode = dentry->d_inode;
6679043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
6689043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
6699043476fSAl Viro 
6709043476fSAl Viro 	if (IS_ERR(head))
6719043476fSAl Viro 		return PTR_ERR(head);
6729043476fSAl Viro 
6739043476fSAl Viro 	generic_fillattr(inode, stat);
6749043476fSAl Viro 	if (table)
6759043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
6769043476fSAl Viro 
6779043476fSAl Viro 	sysctl_head_finish(head);
6789043476fSAl Viro 	return 0;
6799043476fSAl Viro }
6809043476fSAl Viro 
68177b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
682f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
683f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
68477b14db5SEric W. Biederman 	.read		= proc_sys_read,
68577b14db5SEric W. Biederman 	.write		= proc_sys_write,
6866038f373SArnd Bergmann 	.llseek		= default_llseek,
6879043476fSAl Viro };
6889043476fSAl Viro 
6899043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
690887df078SPavel Emelyanov 	.read		= generic_read_dir,
69177b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
6923222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
69377b14db5SEric W. Biederman };
69477b14db5SEric W. Biederman 
69503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
6969043476fSAl Viro 	.permission	= proc_sys_permission,
6979043476fSAl Viro 	.setattr	= proc_sys_setattr,
6989043476fSAl Viro 	.getattr	= proc_sys_getattr,
6999043476fSAl Viro };
7009043476fSAl Viro 
7019043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
70277b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
70377b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
70477b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
7059043476fSAl Viro 	.getattr	= proc_sys_getattr,
70677b14db5SEric W. Biederman };
70777b14db5SEric W. Biederman 
70877b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
70977b14db5SEric W. Biederman {
71034286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
71134286d66SNick Piggin 		return -ECHILD;
7129043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
7139043476fSAl Viro }
7149043476fSAl Viro 
715fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
7169043476fSAl Viro {
7179043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
7189043476fSAl Viro }
7199043476fSAl Viro 
7201f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
7211f87f0b5SEric W. Biederman {
7221f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
7231f87f0b5SEric W. Biederman 	int res;
7241f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
7251f87f0b5SEric W. Biederman 	if (p->unregistering)
7261f87f0b5SEric W. Biederman 		res = 0;
7271f87f0b5SEric W. Biederman 	else if (!set->is_seen)
7281f87f0b5SEric W. Biederman 		res = 1;
7291f87f0b5SEric W. Biederman 	else
7301f87f0b5SEric W. Biederman 		res = set->is_seen(set);
7311f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
7321f87f0b5SEric W. Biederman 	return res;
7331f87f0b5SEric W. Biederman }
7341f87f0b5SEric W. Biederman 
735621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
736621e155aSNick Piggin 		const struct inode *pinode,
737621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
738621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
7399043476fSAl Viro {
740dfef6dcdSAl Viro 	struct ctl_table_header *head;
74131e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
74231e6b01fSNick Piggin 	 * that inode here can be NULL */
743dfef6dcdSAl Viro 	/* AV: can it, indeed? */
74431e6b01fSNick Piggin 	if (!inode)
745dfef6dcdSAl Viro 		return 1;
746621e155aSNick Piggin 	if (name->len != len)
7479043476fSAl Viro 		return 1;
748621e155aSNick Piggin 	if (memcmp(name->name, str, len))
7499043476fSAl Viro 		return 1;
750dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
751dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
75277b14db5SEric W. Biederman }
75377b14db5SEric W. Biederman 
754d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
75577b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
7569043476fSAl Viro 	.d_delete	= proc_sys_delete,
7579043476fSAl Viro 	.d_compare	= proc_sys_compare,
75877b14db5SEric W. Biederman };
75977b14db5SEric W. Biederman 
7607ec66d06SEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir,
7617ec66d06SEric W. Biederman 	const char *name, int namelen)
7621f87f0b5SEric W. Biederman {
7637ec66d06SEric W. Biederman 	struct ctl_table_header *head;
7647ec66d06SEric W. Biederman 	struct ctl_table *entry;
7651f87f0b5SEric W. Biederman 
7667ec66d06SEric W. Biederman 	entry = find_entry(&head, set, dir, name, namelen);
7677ec66d06SEric W. Biederman 	if (!entry)
7687ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
7697ec66d06SEric W. Biederman 	if (S_ISDIR(entry->mode))
7707ec66d06SEric W. Biederman 		return container_of(head, struct ctl_dir, header);
7717ec66d06SEric W. Biederman 	return ERR_PTR(-ENOTDIR);
7721f87f0b5SEric W. Biederman }
7731f87f0b5SEric W. Biederman 
7747ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
7757ec66d06SEric W. Biederman 	const char *name, int namelen)
7761f87f0b5SEric W. Biederman {
7777ec66d06SEric W. Biederman 	struct ctl_table *table;
7787ec66d06SEric W. Biederman 	struct ctl_dir *new;
7797ec66d06SEric W. Biederman 	char *new_name;
7801f87f0b5SEric W. Biederman 
7817ec66d06SEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
7827ec66d06SEric W. Biederman 		      namelen + 1, GFP_KERNEL);
7837ec66d06SEric W. Biederman 	if (!new)
7847ec66d06SEric W. Biederman 		return NULL;
7857ec66d06SEric W. Biederman 
7867ec66d06SEric W. Biederman 	table = (struct ctl_table *)(new + 1);
7877ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
7887ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
7897ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
7907ec66d06SEric W. Biederman 	table[0].procname = new_name;
7917ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
7927ec66d06SEric W. Biederman 	init_header(&new->header, set->root, set, table);
7937ec66d06SEric W. Biederman 
7947ec66d06SEric W. Biederman 	return new;
7951f87f0b5SEric W. Biederman }
7961f87f0b5SEric W. Biederman 
7977ec66d06SEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_table_set *set,
7987ec66d06SEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
7997ec66d06SEric W. Biederman {
8007ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
8017ec66d06SEric W. Biederman 
8027ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
8037ec66d06SEric W. Biederman 	subdir = find_subdir(dir->header.set, dir, name, namelen);
8047ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
8057ec66d06SEric W. Biederman 		goto found;
8067ec66d06SEric W. Biederman 	if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set)
8077ec66d06SEric W. Biederman 		subdir = find_subdir(set, dir, name, namelen);
8087ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
8097ec66d06SEric W. Biederman 		goto found;
8107ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
8117ec66d06SEric W. Biederman 		goto failed;
8127ec66d06SEric W. Biederman 
8137ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
8147ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
8157ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
8167ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
8177ec66d06SEric W. Biederman 	if (!new)
8187ec66d06SEric W. Biederman 		goto failed;
8197ec66d06SEric W. Biederman 
8207ec66d06SEric W. Biederman 	subdir = find_subdir(set, dir, name, namelen);
8217ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
8227ec66d06SEric W. Biederman 		goto found;
8237ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
8247ec66d06SEric W. Biederman 		goto failed;
8257ec66d06SEric W. Biederman 
8267ec66d06SEric W. Biederman 	insert_header(dir, &new->header);
8277ec66d06SEric W. Biederman 	subdir = new;
8287ec66d06SEric W. Biederman found:
8297ec66d06SEric W. Biederman 	subdir->header.nreg++;
8307ec66d06SEric W. Biederman failed:
8317ec66d06SEric W. Biederman 	if (unlikely(IS_ERR(subdir))) {
8326980128fSEric W. Biederman 		printk(KERN_ERR "sysctl could not get directory: ");
8336980128fSEric W. Biederman 		sysctl_print_dir(dir);
8346980128fSEric W. Biederman 		printk(KERN_CONT "/%*.*s %ld\n",
8357ec66d06SEric W. Biederman 			namelen, namelen, name, PTR_ERR(subdir));
8361f87f0b5SEric W. Biederman 	}
8377ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
8387ec66d06SEric W. Biederman 	if (new)
8397ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
8407ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
8417ec66d06SEric W. Biederman 	return subdir;
8421f87f0b5SEric W. Biederman }
8431f87f0b5SEric W. Biederman 
8447c60c48fSEric W. Biederman static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
8451f87f0b5SEric W. Biederman 	struct ctl_table *table)
8461f87f0b5SEric W. Biederman {
8477c60c48fSEric W. Biederman 	struct ctl_table *entry, *test;
8481f87f0b5SEric W. Biederman 	int error = 0;
8491f87f0b5SEric W. Biederman 
8507c60c48fSEric W. Biederman 	for (entry = old; entry->procname; entry++) {
8517c60c48fSEric W. Biederman 		for (test = table; test->procname; test++) {
8527c60c48fSEric W. Biederman 			if (strcmp(entry->procname, test->procname) == 0) {
8537c60c48fSEric W. Biederman 				printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
8547c60c48fSEric W. Biederman 					path, test->procname);
8557c60c48fSEric W. Biederman 				error = -EEXIST;
8561f87f0b5SEric W. Biederman 			}
8577c60c48fSEric W. Biederman 		}
8587c60c48fSEric W. Biederman 	}
8597c60c48fSEric W. Biederman 	return error;
8607c60c48fSEric W. Biederman }
8617c60c48fSEric W. Biederman 
8627c60c48fSEric W. Biederman static int sysctl_check_dups(struct nsproxy *namespaces,
8637ec66d06SEric W. Biederman 	struct ctl_dir *dir,
8647c60c48fSEric W. Biederman 	const char *path, struct ctl_table *table)
8657c60c48fSEric W. Biederman {
8667c60c48fSEric W. Biederman 	struct ctl_table_root *root;
8677c60c48fSEric W. Biederman 	struct ctl_table_set *set;
8687ec66d06SEric W. Biederman 	struct ctl_table_header *head;
8697c60c48fSEric W. Biederman 	int error = 0;
8707c60c48fSEric W. Biederman 
8717c60c48fSEric W. Biederman 	root = &sysctl_table_root;
8727c60c48fSEric W. Biederman 	do {
8737c60c48fSEric W. Biederman 		set = lookup_header_set(root, namespaces);
8747c60c48fSEric W. Biederman 
8757c60c48fSEric W. Biederman 		list_for_each_entry(head, &set->list, ctl_entry) {
8767c60c48fSEric W. Biederman 			if (head->unregistering)
8777c60c48fSEric W. Biederman 				continue;
8787ec66d06SEric W. Biederman 			if (head->parent != dir)
8797c60c48fSEric W. Biederman 				continue;
8807ec66d06SEric W. Biederman 			error = sysctl_check_table_dups(path, head->ctl_table,
8817c60c48fSEric W. Biederman 							table);
8827c60c48fSEric W. Biederman 		}
8837c60c48fSEric W. Biederman 		root = list_entry(root->root_list.next,
8847c60c48fSEric W. Biederman 				  struct ctl_table_root, root_list);
8857c60c48fSEric W. Biederman 	} while (root != &sysctl_table_root);
8867c60c48fSEric W. Biederman 	return error;
8877c60c48fSEric W. Biederman }
8887c60c48fSEric W. Biederman 
8897c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
8907c60c48fSEric W. Biederman {
8917c60c48fSEric W. Biederman 	struct va_format vaf;
8927c60c48fSEric W. Biederman 	va_list args;
8937c60c48fSEric W. Biederman 
8947c60c48fSEric W. Biederman 	va_start(args, fmt);
8957c60c48fSEric W. Biederman 	vaf.fmt = fmt;
8967c60c48fSEric W. Biederman 	vaf.va = &args;
8977c60c48fSEric W. Biederman 
8987c60c48fSEric W. Biederman 	printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
8997c60c48fSEric W. Biederman 		path, table->procname, &vaf);
9007c60c48fSEric W. Biederman 
9017c60c48fSEric W. Biederman 	va_end(args);
9027c60c48fSEric W. Biederman 	return -EINVAL;
9037c60c48fSEric W. Biederman }
9047c60c48fSEric W. Biederman 
9057c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
9067c60c48fSEric W. Biederman {
9077c60c48fSEric W. Biederman 	int err = 0;
9087c60c48fSEric W. Biederman 	for (; table->procname; table++) {
9097c60c48fSEric W. Biederman 		if (table->child)
9107c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "Not a file");
9117c60c48fSEric W. Biederman 
9121f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
9131f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
9141f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
9151f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
9161f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
9171f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
9181f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
9191f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
9201f87f0b5SEric W. Biederman 			if (!table->data)
9217c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No data");
9221f87f0b5SEric W. Biederman 			if (!table->maxlen)
9237c60c48fSEric W. Biederman 				err = sysctl_err(path, table, "No maxlen");
9241f87f0b5SEric W. Biederman 		}
9251f87f0b5SEric W. Biederman 		if (!table->proc_handler)
9267c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "No proc_handler");
9277c60c48fSEric W. Biederman 
9287c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
9297c60c48fSEric W. Biederman 			err = sysctl_err(path, table, "bogus .mode 0%o",
9307c60c48fSEric W. Biederman 				table->mode);
9311f87f0b5SEric W. Biederman 	}
9327c60c48fSEric W. Biederman 	return err;
9331f87f0b5SEric W. Biederman }
9341f87f0b5SEric W. Biederman 
9351f87f0b5SEric W. Biederman /**
936f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
9371f87f0b5SEric W. Biederman  * @root: List of sysctl headers to register on
9381f87f0b5SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
9391f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
9401f87f0b5SEric W. Biederman  * @table: the top-level table structure
9411f87f0b5SEric W. Biederman  *
9421f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
9431f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
9441f87f0b5SEric W. Biederman  *
9451f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
9461f87f0b5SEric W. Biederman  *
9471f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
9481f87f0b5SEric W. Biederman  *            enter a sysctl file
9491f87f0b5SEric W. Biederman  *
9501f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
9511f87f0b5SEric W. Biederman  *
9521f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
9531f87f0b5SEric W. Biederman  *
954f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
9551f87f0b5SEric W. Biederman  *
956f728019bSEric W. Biederman  * child - must be %NULL.
9571f87f0b5SEric W. Biederman  *
9581f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
9591f87f0b5SEric W. Biederman  *
9601f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
9611f87f0b5SEric W. Biederman  *
9621f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
9631f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
9641f87f0b5SEric W. Biederman  *
965f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
966f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
9671f87f0b5SEric W. Biederman  *
9681f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
9691f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
9701f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
9711f87f0b5SEric W. Biederman  *
9721f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
9731f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
9741f87f0b5SEric W. Biederman  *
9751f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
9761f87f0b5SEric W. Biederman  * to the table header on success.
9771f87f0b5SEric W. Biederman  */
9786e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
9791f87f0b5SEric W. Biederman 	struct ctl_table_root *root,
9801f87f0b5SEric W. Biederman 	struct nsproxy *namespaces,
9816e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
9821f87f0b5SEric W. Biederman {
9831f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
9846e9d5164SEric W. Biederman 	const char *name, *nextname;
9851f87f0b5SEric W. Biederman 	struct ctl_table_set *set;
9867ec66d06SEric W. Biederman 	struct ctl_dir *dir;
9871f87f0b5SEric W. Biederman 
9887ec66d06SEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
9891f87f0b5SEric W. Biederman 	if (!header)
9901f87f0b5SEric W. Biederman 		return NULL;
9911f87f0b5SEric W. Biederman 
992e0d04529SEric W. Biederman 	init_header(header, root, NULL, table);
9937c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
9947c60c48fSEric W. Biederman 		goto fail;
9958d6ecfccSEric W. Biederman 
9961f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9977ec66d06SEric W. Biederman 	header->set = set = lookup_header_set(root, namespaces);
9987ec66d06SEric W. Biederman 	dir = &sysctl_root_dir;
9997ec66d06SEric W. Biederman 	dir->header.nreg++;
10007ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10017ec66d06SEric W. Biederman 
10027ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
10037ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
10047ec66d06SEric W. Biederman 		int namelen;
10057ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
10067ec66d06SEric W. Biederman 		if (nextname) {
10077ec66d06SEric W. Biederman 			namelen = nextname - name;
10087ec66d06SEric W. Biederman 			nextname++;
10097ec66d06SEric W. Biederman 		} else {
10107ec66d06SEric W. Biederman 			namelen = strlen(name);
10117ec66d06SEric W. Biederman 		}
10127ec66d06SEric W. Biederman 		if (namelen == 0)
10131f87f0b5SEric W. Biederman 			continue;
10147ec66d06SEric W. Biederman 
10157ec66d06SEric W. Biederman 		dir = get_subdir(set, dir, name, namelen);
10167ec66d06SEric W. Biederman 		if (IS_ERR(dir))
10177ec66d06SEric W. Biederman 			goto fail;
10181f87f0b5SEric W. Biederman 	}
10197ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10207ec66d06SEric W. Biederman 	if (sysctl_check_dups(namespaces, dir, path, table))
10217ec66d06SEric W. Biederman 		goto fail_put_dir_locked;
10227ec66d06SEric W. Biederman 	insert_header(dir, header);
10237ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10241f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
10251f87f0b5SEric W. Biederman 
10261f87f0b5SEric W. Biederman 	return header;
10277ec66d06SEric W. Biederman fail_put_dir_locked:
10287ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10297c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
10307c60c48fSEric W. Biederman fail:
10317c60c48fSEric W. Biederman 	kfree(header);
10327c60c48fSEric W. Biederman 	dump_stack();
10337c60c48fSEric W. Biederman 	return NULL;
10341f87f0b5SEric W. Biederman }
10351f87f0b5SEric W. Biederman 
10366e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
10376e9d5164SEric W. Biederman {
10386e9d5164SEric W. Biederman 	int namelen;
10396e9d5164SEric W. Biederman 	namelen = strlen(name);
10406e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
10416e9d5164SEric W. Biederman 		return NULL;
10426e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
10436e9d5164SEric W. Biederman 	pos[namelen] = '/';
10446e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
10456e9d5164SEric W. Biederman 	pos += namelen + 1;
10466e9d5164SEric W. Biederman 	return pos;
10476e9d5164SEric W. Biederman }
10486e9d5164SEric W. Biederman 
1049f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1050f728019bSEric W. Biederman {
1051f728019bSEric W. Biederman 	int has_files = 0;
1052f728019bSEric W. Biederman 	int nr_subheaders = 0;
1053f728019bSEric W. Biederman 	struct ctl_table *entry;
1054f728019bSEric W. Biederman 
1055f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1056f728019bSEric W. Biederman 	if (!table || !table->procname)
1057f728019bSEric W. Biederman 		return 1;
1058f728019bSEric W. Biederman 
1059f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1060f728019bSEric W. Biederman 		if (entry->child)
1061f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1062f728019bSEric W. Biederman 		else
1063f728019bSEric W. Biederman 			has_files = 1;
1064f728019bSEric W. Biederman 	}
1065f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1066f728019bSEric W. Biederman }
1067f728019bSEric W. Biederman 
1068f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
1069f728019bSEric W. Biederman 	struct ctl_table_header ***subheader,
1070f728019bSEric W. Biederman 	struct ctl_table_root *root, struct nsproxy *namespaces,
1071f728019bSEric W. Biederman 	struct ctl_table *table)
1072f728019bSEric W. Biederman {
1073f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1074f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1075f728019bSEric W. Biederman 	int nr_files = 0;
1076f728019bSEric W. Biederman 	int nr_dirs = 0;
1077f728019bSEric W. Biederman 	int err = -ENOMEM;
1078f728019bSEric W. Biederman 
1079f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1080f728019bSEric W. Biederman 		if (entry->child)
1081f728019bSEric W. Biederman 			nr_dirs++;
1082f728019bSEric W. Biederman 		else
1083f728019bSEric W. Biederman 			nr_files++;
1084f728019bSEric W. Biederman 	}
1085f728019bSEric W. Biederman 
1086f728019bSEric W. Biederman 	files = table;
1087f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1088f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1089f728019bSEric W. Biederman 		struct ctl_table *new;
1090f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1091f728019bSEric W. Biederman 				GFP_KERNEL);
1092f728019bSEric W. Biederman 		if (!files)
1093f728019bSEric W. Biederman 			goto out;
1094f728019bSEric W. Biederman 
1095f728019bSEric W. Biederman 		ctl_table_arg = files;
1096f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1097f728019bSEric W. Biederman 			if (entry->child)
1098f728019bSEric W. Biederman 				continue;
1099f728019bSEric W. Biederman 			*new = *entry;
1100f728019bSEric W. Biederman 			new++;
1101f728019bSEric W. Biederman 		}
1102f728019bSEric W. Biederman 	}
1103f728019bSEric W. Biederman 
1104f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1105f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1106f728019bSEric W. Biederman 		struct ctl_table_header *header;
1107f728019bSEric W. Biederman 		header = __register_sysctl_table(root, namespaces, path, files);
1108f728019bSEric W. Biederman 		if (!header) {
1109f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1110f728019bSEric W. Biederman 			goto out;
1111f728019bSEric W. Biederman 		}
1112f728019bSEric W. Biederman 
1113f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1114f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1115f728019bSEric W. Biederman 		**subheader = header;
1116f728019bSEric W. Biederman 		(*subheader)++;
1117f728019bSEric W. Biederman 	}
1118f728019bSEric W. Biederman 
1119f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1120f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1121f728019bSEric W. Biederman 		char *child_pos;
1122f728019bSEric W. Biederman 
1123f728019bSEric W. Biederman 		if (!entry->child)
1124f728019bSEric W. Biederman 			continue;
1125f728019bSEric W. Biederman 
1126f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1127f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1128f728019bSEric W. Biederman 		if (!child_pos)
1129f728019bSEric W. Biederman 			goto out;
1130f728019bSEric W. Biederman 
1131f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
1132f728019bSEric W. Biederman 						  root, namespaces, entry->child);
1133f728019bSEric W. Biederman 		pos[0] = '\0';
1134f728019bSEric W. Biederman 		if (err)
1135f728019bSEric W. Biederman 			goto out;
1136f728019bSEric W. Biederman 	}
1137f728019bSEric W. Biederman 	err = 0;
1138f728019bSEric W. Biederman out:
1139f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1140f728019bSEric W. Biederman 	return err;
1141f728019bSEric W. Biederman }
1142f728019bSEric W. Biederman 
11436e9d5164SEric W. Biederman /**
11446e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
11456e9d5164SEric W. Biederman  * @root: List of sysctl headers to register on
11466e9d5164SEric W. Biederman  * @namespaces: Data to compute which lists of sysctl entries are visible
11476e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
11486e9d5164SEric W. Biederman  * @table: the top-level table structure
11496e9d5164SEric W. Biederman  *
11506e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
11516e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
11526e9d5164SEric W. Biederman  *
11536e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
11546e9d5164SEric W. Biederman  */
11556e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
11566e9d5164SEric W. Biederman 	struct ctl_table_root *root,
11576e9d5164SEric W. Biederman 	struct nsproxy *namespaces,
11586e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
11596e9d5164SEric W. Biederman {
1160ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1161f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1162f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
11636e9d5164SEric W. Biederman 	const struct ctl_path *component;
11646e9d5164SEric W. Biederman 	char *new_path, *pos;
11656e9d5164SEric W. Biederman 
11666e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
11676e9d5164SEric W. Biederman 	if (!new_path)
11686e9d5164SEric W. Biederman 		return NULL;
11696e9d5164SEric W. Biederman 
11706e9d5164SEric W. Biederman 	pos[0] = '\0';
11716e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
11726e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
11736e9d5164SEric W. Biederman 		if (!pos)
11746e9d5164SEric W. Biederman 			goto out;
11756e9d5164SEric W. Biederman 	}
1176ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1177ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1178ec6a5266SEric W. Biederman 		if (!pos)
1179ec6a5266SEric W. Biederman 			goto out;
1180ec6a5266SEric W. Biederman 		table = table->child;
1181ec6a5266SEric W. Biederman 	}
1182f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
11836e9d5164SEric W. Biederman 		header = __register_sysctl_table(root, namespaces, new_path, table);
1184ec6a5266SEric W. Biederman 		if (header)
1185ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1186f728019bSEric W. Biederman 	} else {
1187f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1188f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1189f728019bSEric W. Biederman 		if (!header)
1190f728019bSEric W. Biederman 			goto out;
1191f728019bSEric W. Biederman 
1192f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1193f728019bSEric W. Biederman 		subheader = subheaders;
1194f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1195f728019bSEric W. Biederman 
1196f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1197f728019bSEric W. Biederman 						root, namespaces, table))
1198f728019bSEric W. Biederman 			goto err_register_leaves;
1199f728019bSEric W. Biederman 	}
1200f728019bSEric W. Biederman 
12016e9d5164SEric W. Biederman out:
12026e9d5164SEric W. Biederman 	kfree(new_path);
12036e9d5164SEric W. Biederman 	return header;
1204f728019bSEric W. Biederman 
1205f728019bSEric W. Biederman err_register_leaves:
1206f728019bSEric W. Biederman 	while (subheader > subheaders) {
1207f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1208f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1209f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1210f728019bSEric W. Biederman 		kfree(table);
1211f728019bSEric W. Biederman 	}
1212f728019bSEric W. Biederman 	kfree(header);
1213f728019bSEric W. Biederman 	header = NULL;
1214f728019bSEric W. Biederman 	goto out;
12156e9d5164SEric W. Biederman }
12166e9d5164SEric W. Biederman 
12171f87f0b5SEric W. Biederman /**
12181f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
12191f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
12201f87f0b5SEric W. Biederman  * @table: the top-level table structure
12211f87f0b5SEric W. Biederman  *
12221f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12231f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12241f87f0b5SEric W. Biederman  *
12251f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
12261f87f0b5SEric W. Biederman  */
12271f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
12281f87f0b5SEric W. Biederman 						struct ctl_table *table)
12291f87f0b5SEric W. Biederman {
12301f87f0b5SEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
12311f87f0b5SEric W. Biederman 					path, table);
12321f87f0b5SEric W. Biederman }
12331f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
12341f87f0b5SEric W. Biederman 
12351f87f0b5SEric W. Biederman /**
12361f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
12371f87f0b5SEric W. Biederman  * @table: the top-level table structure
12381f87f0b5SEric W. Biederman  *
12391f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12401f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12411f87f0b5SEric W. Biederman  *
12421f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
12431f87f0b5SEric W. Biederman  */
12441f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
12451f87f0b5SEric W. Biederman {
12461f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
12471f87f0b5SEric W. Biederman 
12481f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
12491f87f0b5SEric W. Biederman }
12501f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
12511f87f0b5SEric W. Biederman 
1252938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1253938aaa4fSEric W. Biederman {
12547ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
12557ec66d06SEric W. Biederman 
1256938aaa4fSEric W. Biederman 	if (--header->nreg)
1257938aaa4fSEric W. Biederman 		return;
1258938aaa4fSEric W. Biederman 
1259938aaa4fSEric W. Biederman 	start_unregistering(header);
1260938aaa4fSEric W. Biederman 	if (!--header->count)
1261938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
12627ec66d06SEric W. Biederman 
12637ec66d06SEric W. Biederman 	if (parent)
12647ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1265938aaa4fSEric W. Biederman }
1266938aaa4fSEric W. Biederman 
12671f87f0b5SEric W. Biederman /**
12681f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
12691f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
12701f87f0b5SEric W. Biederman  *
12711f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
12721f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
12731f87f0b5SEric W. Biederman  */
12741f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
12751f87f0b5SEric W. Biederman {
1276f728019bSEric W. Biederman 	int nr_subheaders;
12771f87f0b5SEric W. Biederman 	might_sleep();
12781f87f0b5SEric W. Biederman 
12791f87f0b5SEric W. Biederman 	if (header == NULL)
12801f87f0b5SEric W. Biederman 		return;
12811f87f0b5SEric W. Biederman 
1282f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1283f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1284f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1285f728019bSEric W. Biederman 		int i;
1286f728019bSEric W. Biederman 
1287f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1288f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1289f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1290f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1291f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1292f728019bSEric W. Biederman 			kfree(table);
1293f728019bSEric W. Biederman 		}
1294f728019bSEric W. Biederman 		kfree(header);
1295f728019bSEric W. Biederman 		return;
1296f728019bSEric W. Biederman 	}
1297f728019bSEric W. Biederman 
12981f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1299938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
13001f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13011f87f0b5SEric W. Biederman }
13021f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
13031f87f0b5SEric W. Biederman 
13041f87f0b5SEric W. Biederman void setup_sysctl_set(struct ctl_table_set *p,
13059eb47c26SEric W. Biederman 	struct ctl_table_root *root,
13061f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
13071f87f0b5SEric W. Biederman {
13081f87f0b5SEric W. Biederman 	INIT_LIST_HEAD(&p->list);
13099eb47c26SEric W. Biederman 	p->root = root;
13101f87f0b5SEric W. Biederman 	p->is_seen = is_seen;
13111f87f0b5SEric W. Biederman }
13121f87f0b5SEric W. Biederman 
131397324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
131497324cd8SEric W. Biederman {
131597324cd8SEric W. Biederman 	WARN_ON(!list_empty(&set->list));
131697324cd8SEric W. Biederman }
13171f87f0b5SEric W. Biederman 
13181e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
131977b14db5SEric W. Biederman {
1320e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1321e1675231SAlexey Dobriyan 
132277b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
13239043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
13249043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
132577b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1326de4e83bdSEric W. Biederman 
1327de4e83bdSEric W. Biederman 	return sysctl_init();
132877b14db5SEric W. Biederman }
1329