xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 228b09de)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
277b14db5SEric W. Biederman /*
377b14db5SEric W. Biederman  * /proc/sys support
477b14db5SEric W. Biederman  */
51e0edd3fSAlexey Dobriyan #include <linux/init.h>
677b14db5SEric W. Biederman #include <linux/sysctl.h>
7f1ecf068SLucas De Marchi #include <linux/poll.h>
877b14db5SEric W. Biederman #include <linux/proc_fs.h>
987ebdc00SAndrew Morton #include <linux/printk.h>
1077b14db5SEric W. Biederman #include <linux/security.h>
1140401530SAl Viro #include <linux/sched.h>
125b825c3aSIngo Molnar #include <linux/cred.h>
1334286d66SNick Piggin #include <linux/namei.h>
1440401530SAl Viro #include <linux/mm.h>
154bd6a735SMatthew Wilcox (Oracle) #include <linux/uio.h>
161f87f0b5SEric W. Biederman #include <linux/module.h>
177b146cebSAndrey Ignatov #include <linux/bpf-cgroup.h>
183db978d4SVlastimil Babka #include <linux/mount.h>
193ddd9a80SXiaoming Ni #include <linux/kmemleak.h>
2077b14db5SEric W. Biederman #include "internal.h"
2177b14db5SEric W. Biederman 
22cb55f27aSMeng Tang #define list_for_each_table_entry(entry, table) \
23cb55f27aSMeng Tang 	for ((entry) = (table); (entry)->procname; (entry)++)
24cb55f27aSMeng Tang 
25d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
2677b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
2703a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
289043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
299043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
3077b14db5SEric W. Biederman 
31f9bd6733SEric W. Biederman /* Support for permanently empty directories */
32f9bd6733SEric W. Biederman 
33f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = {
34f9bd6733SEric W. Biederman 	{ }
35f9bd6733SEric W. Biederman };
36f9bd6733SEric W. Biederman 
37ee9efac4SLuis Chamberlain /**
38ee9efac4SLuis Chamberlain  * register_sysctl_mount_point() - registers a sysctl mount point
39ee9efac4SLuis Chamberlain  * @path: path for the mount point
40ee9efac4SLuis Chamberlain  *
41ee9efac4SLuis Chamberlain  * Used to create a permanently empty directory to serve as mount point.
42ee9efac4SLuis Chamberlain  * There are some subtle but important permission checks this allows in the
43ee9efac4SLuis Chamberlain  * case of unprivileged mounts.
44ee9efac4SLuis Chamberlain  */
45ee9efac4SLuis Chamberlain struct ctl_table_header *register_sysctl_mount_point(const char *path)
46ee9efac4SLuis Chamberlain {
47ee9efac4SLuis Chamberlain 	return register_sysctl(path, sysctl_mount_point);
48ee9efac4SLuis Chamberlain }
49ee9efac4SLuis Chamberlain EXPORT_SYMBOL(register_sysctl_mount_point);
50ee9efac4SLuis Chamberlain 
51f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head)
52f9bd6733SEric W. Biederman {
53f9bd6733SEric W. Biederman 	return head->ctl_table[0].child == sysctl_mount_point;
54f9bd6733SEric W. Biederman }
55f9bd6733SEric W. Biederman 
56f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir)
57f9bd6733SEric W. Biederman {
58f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = sysctl_mount_point;
59f9bd6733SEric W. Biederman }
60f9bd6733SEric W. Biederman 
61f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir)
62f9bd6733SEric W. Biederman 
63f9bd6733SEric W. Biederman {
64f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = NULL;
65f9bd6733SEric W. Biederman }
66f9bd6733SEric W. Biederman 
67f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
68f1ecf068SLucas De Marchi {
69f1ecf068SLucas De Marchi 	if (!poll)
70f1ecf068SLucas De Marchi 		return;
71f1ecf068SLucas De Marchi 
72f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
73f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
74f1ecf068SLucas De Marchi }
75f1ecf068SLucas De Marchi 
76a194558eSEric W. Biederman static struct ctl_table root_table[] = {
77a194558eSEric W. Biederman 	{
78a194558eSEric W. Biederman 		.procname = "",
797ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
80a194558eSEric W. Biederman 	},
81a194558eSEric W. Biederman 	{ }
82a194558eSEric W. Biederman };
830e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
840e47c99dSEric W. Biederman 	.default_set.dir.header = {
851f87f0b5SEric W. Biederman 		{{.count = 1,
86938aaa4fSEric W. Biederman 		  .nreg = 1,
87ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
880e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
891f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
901f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
917ec66d06SEric W. Biederman 	},
921f87f0b5SEric W. Biederman };
931f87f0b5SEric W. Biederman 
941f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
951f87f0b5SEric W. Biederman 
967ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
970e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
9813bcc6a2SEric W. Biederman 	struct ctl_table **pentry);
990e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
1000e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
1017ec66d06SEric W. Biederman 
1026980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
1036980128fSEric W. Biederman {
1046980128fSEric W. Biederman 	if (dir->header.parent)
1056980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
10687ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
1076980128fSEric W. Biederman }
1086980128fSEric W. Biederman 
109076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
110076c3eedSEric W. Biederman {
111076c3eedSEric W. Biederman 	int cmp;
112076c3eedSEric W. Biederman 
113d8fc9b66SMasahiro Yamada 	cmp = memcmp(name1, name2, min(len1, len2));
114076c3eedSEric W. Biederman 	if (cmp == 0)
115076c3eedSEric W. Biederman 		cmp = len1 - len2;
116076c3eedSEric W. Biederman 	return cmp;
117076c3eedSEric W. Biederman }
118076c3eedSEric W. Biederman 
11960f126d9SEric W. Biederman /* Called under sysctl_lock */
120076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
1210e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
122076c3eedSEric W. Biederman {
123076c3eedSEric W. Biederman 	struct ctl_table_header *head;
124076c3eedSEric W. Biederman 	struct ctl_table *entry;
125ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
126076c3eedSEric W. Biederman 
127ac13ac6fSEric W. Biederman 	while (node)
128ac13ac6fSEric W. Biederman 	{
129ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
130ac13ac6fSEric W. Biederman 		const char *procname;
131ac13ac6fSEric W. Biederman 		int cmp;
132ac13ac6fSEric W. Biederman 
133ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
134ac13ac6fSEric W. Biederman 		head = ctl_node->header;
135ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
136ac13ac6fSEric W. Biederman 		procname = entry->procname;
137ac13ac6fSEric W. Biederman 
138ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
139ac13ac6fSEric W. Biederman 		if (cmp < 0)
140ac13ac6fSEric W. Biederman 			node = node->rb_left;
141ac13ac6fSEric W. Biederman 		else if (cmp > 0)
142ac13ac6fSEric W. Biederman 			node = node->rb_right;
143ac13ac6fSEric W. Biederman 		else {
144076c3eedSEric W. Biederman 			*phead = head;
145076c3eedSEric W. Biederman 			return entry;
146076c3eedSEric W. Biederman 		}
147076c3eedSEric W. Biederman 	}
148076c3eedSEric W. Biederman 	return NULL;
149076c3eedSEric W. Biederman }
150076c3eedSEric W. Biederman 
151ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
152ac13ac6fSEric W. Biederman {
153ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
154ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
155ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
156ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
157ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
158ac13ac6fSEric W. Biederman 
159ac13ac6fSEric W. Biederman 	while (*p) {
160ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
161ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
162ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
163ac13ac6fSEric W. Biederman 		const char *parent_name;
164ac13ac6fSEric W. Biederman 		int cmp;
165ac13ac6fSEric W. Biederman 
166ac13ac6fSEric W. Biederman 		parent = *p;
167ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
168ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
169ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
170ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
171ac13ac6fSEric W. Biederman 
172ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
173ac13ac6fSEric W. Biederman 		if (cmp < 0)
174ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
175ac13ac6fSEric W. Biederman 		else if (cmp > 0)
176ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
177ac13ac6fSEric W. Biederman 		else {
17887ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
179ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
180153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", entry->procname);
181ac13ac6fSEric W. Biederman 			return -EEXIST;
182ac13ac6fSEric W. Biederman 		}
183ac13ac6fSEric W. Biederman 	}
184ac13ac6fSEric W. Biederman 
185ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
186ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
187ac13ac6fSEric W. Biederman 	return 0;
188ac13ac6fSEric W. Biederman }
189ac13ac6fSEric W. Biederman 
190ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
191ac13ac6fSEric W. Biederman {
192ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
193ac13ac6fSEric W. Biederman 
194ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
195ac13ac6fSEric W. Biederman }
196ac13ac6fSEric W. Biederman 
197e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
198e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
199ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
200e0d04529SEric W. Biederman {
2017ec66d06SEric W. Biederman 	head->ctl_table = table;
202e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
203e0d04529SEric W. Biederman 	head->used = 0;
204e0d04529SEric W. Biederman 	head->count = 1;
205e0d04529SEric W. Biederman 	head->nreg = 1;
206e0d04529SEric W. Biederman 	head->unregistering = NULL;
207e0d04529SEric W. Biederman 	head->root = root;
208e0d04529SEric W. Biederman 	head->set = set;
209e0d04529SEric W. Biederman 	head->parent = NULL;
210ac13ac6fSEric W. Biederman 	head->node = node;
2112fd1d2c4SEric W. Biederman 	INIT_HLIST_HEAD(&head->inodes);
212ac13ac6fSEric W. Biederman 	if (node) {
213ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
214cb55f27aSMeng Tang 
215cb55f27aSMeng Tang 		list_for_each_table_entry(entry, table) {
216ac13ac6fSEric W. Biederman 			node->header = head;
217cb55f27aSMeng Tang 			node++;
218cb55f27aSMeng Tang 		}
219ac13ac6fSEric W. Biederman 	}
220ac13ac6fSEric W. Biederman }
221e0d04529SEric W. Biederman 
2228425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2238425d6aaSEric W. Biederman {
224ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
225cb55f27aSMeng Tang 
226cb55f27aSMeng Tang 	list_for_each_table_entry(entry, head->ctl_table)
227ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2288425d6aaSEric W. Biederman }
2298425d6aaSEric W. Biederman 
2300e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2318425d6aaSEric W. Biederman {
232ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
2330e47c99dSEric W. Biederman 	int err;
2340e47c99dSEric W. Biederman 
235f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
236f9bd6733SEric W. Biederman 	if (is_empty_dir(&dir->header))
237f9bd6733SEric W. Biederman 		return -EROFS;
238f9bd6733SEric W. Biederman 
239f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
240f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point) {
241f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
242f9bd6733SEric W. Biederman 			return -EINVAL;
243f9bd6733SEric W. Biederman 		set_empty_dir(dir);
244f9bd6733SEric W. Biederman 	}
245f9bd6733SEric W. Biederman 
2460e47c99dSEric W. Biederman 	dir->header.nreg++;
2477ec66d06SEric W. Biederman 	header->parent = dir;
2480e47c99dSEric W. Biederman 	err = insert_links(header);
2490e47c99dSEric W. Biederman 	if (err)
2500e47c99dSEric W. Biederman 		goto fail_links;
251cb55f27aSMeng Tang 	list_for_each_table_entry(entry, header->ctl_table) {
252ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
253ac13ac6fSEric W. Biederman 		if (err)
254ac13ac6fSEric W. Biederman 			goto fail;
255ac13ac6fSEric W. Biederman 	}
2560e47c99dSEric W. Biederman 	return 0;
257ac13ac6fSEric W. Biederman fail:
258ac13ac6fSEric W. Biederman 	erase_header(header);
259ac13ac6fSEric W. Biederman 	put_links(header);
2600e47c99dSEric W. Biederman fail_links:
261f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
262f9bd6733SEric W. Biederman 		clear_empty_dir(dir);
2630e47c99dSEric W. Biederman 	header->parent = NULL;
2640e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2650e47c99dSEric W. Biederman 	return err;
2668425d6aaSEric W. Biederman }
2678425d6aaSEric W. Biederman 
2681f87f0b5SEric W. Biederman /* called under sysctl_lock */
2691f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2701f87f0b5SEric W. Biederman {
2711f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2721f87f0b5SEric W. Biederman 		return 0;
2731f87f0b5SEric W. Biederman 	p->used++;
2741f87f0b5SEric W. Biederman 	return 1;
2751f87f0b5SEric W. Biederman }
2761f87f0b5SEric W. Biederman 
2771f87f0b5SEric W. Biederman /* called under sysctl_lock */
2781f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2791f87f0b5SEric W. Biederman {
2801f87f0b5SEric W. Biederman 	if (!--p->used)
2811f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2821f87f0b5SEric W. Biederman 			complete(p->unregistering);
2831f87f0b5SEric W. Biederman }
2841f87f0b5SEric W. Biederman 
285f90f3cafSEric W. Biederman static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
286d6cffbbeSKonstantin Khlebnikov {
287f90f3cafSEric W. Biederman 	proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
288d6cffbbeSKonstantin Khlebnikov }
289d6cffbbeSKonstantin Khlebnikov 
2901f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
2911f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2921f87f0b5SEric W. Biederman {
2931f87f0b5SEric W. Biederman 	/*
2941f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
2951f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
2961f87f0b5SEric W. Biederman 	 */
2971f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
2981f87f0b5SEric W. Biederman 		struct completion wait;
2991f87f0b5SEric W. Biederman 		init_completion(&wait);
3001f87f0b5SEric W. Biederman 		p->unregistering = &wait;
3011f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
3021f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
3031f87f0b5SEric W. Biederman 	} else {
3041f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
3051f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
306ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
3071f87f0b5SEric W. Biederman 	}
3081f87f0b5SEric W. Biederman 	/*
309f90f3cafSEric W. Biederman 	 * Invalidate dentries for unregistered sysctls: namespaced sysctls
310d6cffbbeSKonstantin Khlebnikov 	 * can have duplicate names and contaminate dcache very badly.
311d6cffbbeSKonstantin Khlebnikov 	 */
312f90f3cafSEric W. Biederman 	proc_sys_invalidate_dcache(p);
313d6cffbbeSKonstantin Khlebnikov 	/*
3141f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
3151f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
3161f87f0b5SEric W. Biederman 	 */
317ace0c791SEric W. Biederman 	spin_lock(&sysctl_lock);
3188425d6aaSEric W. Biederman 	erase_header(p);
3191f87f0b5SEric W. Biederman }
3201f87f0b5SEric W. Biederman 
3211f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3221f87f0b5SEric W. Biederman {
323ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3241f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3251f87f0b5SEric W. Biederman 	if (!use_table(head))
3261f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3271f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3281f87f0b5SEric W. Biederman 	return head;
3291f87f0b5SEric W. Biederman }
3301f87f0b5SEric W. Biederman 
3311f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3321f87f0b5SEric W. Biederman {
3331f87f0b5SEric W. Biederman 	if (!head)
3341f87f0b5SEric W. Biederman 		return;
3351f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3361f87f0b5SEric W. Biederman 	unuse_table(head);
3371f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3381f87f0b5SEric W. Biederman }
3391f87f0b5SEric W. Biederman 
3401f87f0b5SEric W. Biederman static struct ctl_table_set *
34113bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root)
3421f87f0b5SEric W. Biederman {
3431f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3441f87f0b5SEric W. Biederman 	if (root->lookup)
34513bcc6a2SEric W. Biederman 		set = root->lookup(root);
3461f87f0b5SEric W. Biederman 	return set;
3471f87f0b5SEric W. Biederman }
3481f87f0b5SEric W. Biederman 
349076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3507ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
351076c3eedSEric W. Biederman 				      const char *name, int namelen)
352076c3eedSEric W. Biederman {
353076c3eedSEric W. Biederman 	struct ctl_table_header *head;
354076c3eedSEric W. Biederman 	struct ctl_table *entry;
355076c3eedSEric W. Biederman 
356076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3570e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
358076c3eedSEric W. Biederman 	if (entry && use_table(head))
359076c3eedSEric W. Biederman 		*phead = head;
360076c3eedSEric W. Biederman 	else
361076c3eedSEric W. Biederman 		entry = NULL;
362076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
363076c3eedSEric W. Biederman 	return entry;
364076c3eedSEric W. Biederman }
365076c3eedSEric W. Biederman 
366ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3671f87f0b5SEric W. Biederman {
368ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3691f87f0b5SEric W. Biederman 
370ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
371ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
372ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
373ac13ac6fSEric W. Biederman 			return ctl_node;
3741f87f0b5SEric W. Biederman 	}
3751f87f0b5SEric W. Biederman 	return NULL;
3761f87f0b5SEric W. Biederman }
3771f87f0b5SEric W. Biederman 
3787ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3796a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3801f87f0b5SEric W. Biederman {
381ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3827ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
383ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3846a75ce16SEric W. Biederman 
3856a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
386ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3876a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
388ac13ac6fSEric W. Biederman 	if (ctl_node) {
389ac13ac6fSEric W. Biederman 		head = ctl_node->header;
390ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
391ac13ac6fSEric W. Biederman 	}
3926a75ce16SEric W. Biederman 	*phead = head;
3936a75ce16SEric W. Biederman 	*pentry = entry;
3946a75ce16SEric W. Biederman }
3956a75ce16SEric W. Biederman 
3967ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
3976a75ce16SEric W. Biederman {
3986a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
3996a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
400ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
4016a75ce16SEric W. Biederman 
4026a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
4036a75ce16SEric W. Biederman 	unuse_table(head);
404ac13ac6fSEric W. Biederman 
405ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
4066a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
407ac13ac6fSEric W. Biederman 	head = NULL;
408ac13ac6fSEric W. Biederman 	if (ctl_node) {
409ac13ac6fSEric W. Biederman 		head = ctl_node->header;
410ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
4116a75ce16SEric W. Biederman 	}
4126a75ce16SEric W. Biederman 	*phead = head;
4136a75ce16SEric W. Biederman 	*pentry = entry;
4141f87f0b5SEric W. Biederman }
4151f87f0b5SEric W. Biederman 
4161f87f0b5SEric W. Biederman /*
4171f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4181f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4191f87f0b5SEric W. Biederman  */
4201f87f0b5SEric W. Biederman 
4211f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4221f87f0b5SEric W. Biederman {
423091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4241f87f0b5SEric W. Biederman 		mode >>= 6;
425091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4261f87f0b5SEric W. Biederman 		mode >>= 3;
4271f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4281f87f0b5SEric W. Biederman 		return 0;
4291f87f0b5SEric W. Biederman 	return -EACCES;
4301f87f0b5SEric W. Biederman }
4311f87f0b5SEric W. Biederman 
43273f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
4331f87f0b5SEric W. Biederman {
43473f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4351f87f0b5SEric W. Biederman 	int mode;
4361f87f0b5SEric W. Biederman 
4371f87f0b5SEric W. Biederman 	if (root->permissions)
43873f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4391f87f0b5SEric W. Biederman 	else
4401f87f0b5SEric W. Biederman 		mode = table->mode;
4411f87f0b5SEric W. Biederman 
4421f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4431f87f0b5SEric W. Biederman }
4441f87f0b5SEric W. Biederman 
4459043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
4469043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
44777b14db5SEric W. Biederman {
448e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
44977b14db5SEric W. Biederman 	struct inode *inode;
4509043476fSAl Viro 	struct proc_inode *ei;
45177b14db5SEric W. Biederman 
4529043476fSAl Viro 	inode = new_inode(sb);
45377b14db5SEric W. Biederman 	if (!inode)
454ea5751ccSIvan Delalande 		return ERR_PTR(-ENOMEM);
45577b14db5SEric W. Biederman 
45685fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
45785fe4025SChristoph Hellwig 
45877b14db5SEric W. Biederman 	ei = PROC_I(inode);
4599043476fSAl Viro 
460d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
461ace0c791SEric W. Biederman 	if (unlikely(head->unregistering)) {
462ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
463ace0c791SEric W. Biederman 		iput(inode);
464ea5751ccSIvan Delalande 		return ERR_PTR(-ENOENT);
465ace0c791SEric W. Biederman 	}
466ace0c791SEric W. Biederman 	ei->sysctl = head;
467ace0c791SEric W. Biederman 	ei->sysctl_entry = table;
4680afa5ca8SEric W. Biederman 	hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
469d6cffbbeSKonstantin Khlebnikov 	head->count++;
470d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
471d6cffbbeSKonstantin Khlebnikov 
472078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
4739043476fSAl Viro 	inode->i_mode = table->mode;
4747ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4759043476fSAl Viro 		inode->i_mode |= S_IFREG;
47677b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
47777b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4789043476fSAl Viro 	} else {
4799043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4809043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4819043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
482f9bd6733SEric W. Biederman 		if (is_empty_dir(head))
483f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4849043476fSAl Viro 	}
485e79c6a4fSDmitry Torokhov 
486e79c6a4fSDmitry Torokhov 	if (root->set_ownership)
487e79c6a4fSDmitry Torokhov 		root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
4885ec27ec7SRadoslaw Burny 	else {
4895ec27ec7SRadoslaw Burny 		inode->i_uid = GLOBAL_ROOT_UID;
4905ec27ec7SRadoslaw Burny 		inode->i_gid = GLOBAL_ROOT_GID;
4915ec27ec7SRadoslaw Burny 	}
492e79c6a4fSDmitry Torokhov 
49377b14db5SEric W. Biederman 	return inode;
49477b14db5SEric W. Biederman }
49577b14db5SEric W. Biederman 
496d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
497d6cffbbeSKonstantin Khlebnikov {
498d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
4990afa5ca8SEric W. Biederman 	hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
500d6cffbbeSKonstantin Khlebnikov 	if (!--head->count)
501d6cffbbeSKonstantin Khlebnikov 		kfree_rcu(head, rcu);
502d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
503d6cffbbeSKonstantin Khlebnikov }
504d6cffbbeSKonstantin Khlebnikov 
50581324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
50677b14db5SEric W. Biederman {
5073cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
5083cc3e046SEric W. Biederman 	if (!head)
5090e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
5103cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
51177b14db5SEric W. Biederman }
51277b14db5SEric W. Biederman 
51377b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
51400cd8dd3SAl Viro 					unsigned int flags)
51577b14db5SEric W. Biederman {
5169043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
5179043476fSAl Viro 	struct ctl_table_header *h = NULL;
518dc12e909SAl Viro 	const struct qstr *name = &dentry->d_name;
5199043476fSAl Viro 	struct ctl_table *p;
52077b14db5SEric W. Biederman 	struct inode *inode;
5219043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
5227ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
5230e47c99dSEric W. Biederman 	int ret;
52477b14db5SEric W. Biederman 
5259043476fSAl Viro 	if (IS_ERR(head))
5269043476fSAl Viro 		return ERR_CAST(head);
5279043476fSAl Viro 
5287ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
5299043476fSAl Viro 
5307ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
5319043476fSAl Viro 	if (!p)
53277b14db5SEric W. Biederman 		goto out;
53377b14db5SEric W. Biederman 
5344e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
53513bcc6a2SEric W. Biederman 		ret = sysctl_follow_link(&h, &p);
5360e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5370e47c99dSEric W. Biederman 		if (ret)
5380e47c99dSEric W. Biederman 			goto out;
5394e757320SEric W. Biederman 	}
5400e47c99dSEric W. Biederman 
5419043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
542ea5751ccSIvan Delalande 	if (IS_ERR(inode)) {
543ea5751ccSIvan Delalande 		err = ERR_CAST(inode);
54477b14db5SEric W. Biederman 		goto out;
545ea5751ccSIvan Delalande 	}
54677b14db5SEric W. Biederman 
547fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
548888e2b03SAl Viro 	err = d_splice_alias(inode, dentry);
54977b14db5SEric W. Biederman 
55077b14db5SEric W. Biederman out:
5516bf61045SFrancesco Ruggeri 	if (h)
5526bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
55377b14db5SEric W. Biederman 	sysctl_head_finish(head);
55477b14db5SEric W. Biederman 	return err;
55577b14db5SEric W. Biederman }
55677b14db5SEric W. Biederman 
5574bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
5584bd6a735SMatthew Wilcox (Oracle) 		int write)
55977b14db5SEric W. Biederman {
5604bd6a735SMatthew Wilcox (Oracle) 	struct inode *inode = file_inode(iocb->ki_filp);
5619043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5629043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5634bd6a735SMatthew Wilcox (Oracle) 	size_t count = iov_iter_count(iter);
5644bd6a735SMatthew Wilcox (Oracle) 	char *kbuf;
5652a2da53bSDavid Howells 	ssize_t error;
56677b14db5SEric W. Biederman 
5679043476fSAl Viro 	if (IS_ERR(head))
5689043476fSAl Viro 		return PTR_ERR(head);
56977b14db5SEric W. Biederman 
57077b14db5SEric W. Biederman 	/*
57177b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
57277b14db5SEric W. Biederman 	 * and won't be until we finish.
57377b14db5SEric W. Biederman 	 */
57477b14db5SEric W. Biederman 	error = -EPERM;
57573f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
57677b14db5SEric W. Biederman 		goto out;
57777b14db5SEric W. Biederman 
5789043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5799043476fSAl Viro 	error = -EINVAL;
5809043476fSAl Viro 	if (!table->proc_handler)
5819043476fSAl Viro 		goto out;
5829043476fSAl Viro 
583ef9d965bSChristoph Hellwig 	/* don't even try if the size is too large */
584d4d80e69SMatthew Wilcox (Oracle) 	error = -ENOMEM;
585d4d80e69SMatthew Wilcox (Oracle) 	if (count >= KMALLOC_MAX_SIZE)
586d4d80e69SMatthew Wilcox (Oracle) 		goto out;
58745089437SJosef Bacik 	kbuf = kvzalloc(count + 1, GFP_KERNEL);
58832927393SChristoph Hellwig 	if (!kbuf)
58932927393SChristoph Hellwig 		goto out;
5904bd6a735SMatthew Wilcox (Oracle) 
5914bd6a735SMatthew Wilcox (Oracle) 	if (write) {
5924bd6a735SMatthew Wilcox (Oracle) 		error = -EFAULT;
5934bd6a735SMatthew Wilcox (Oracle) 		if (!copy_from_iter_full(kbuf, count, iter))
5944bd6a735SMatthew Wilcox (Oracle) 			goto out_free_buf;
5954bd6a735SMatthew Wilcox (Oracle) 		kbuf[count] = '\0';
5964e63acdfSAndrey Ignatov 	}
5974e63acdfSAndrey Ignatov 
59832927393SChristoph Hellwig 	error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
5994bd6a735SMatthew Wilcox (Oracle) 					   &iocb->ki_pos);
60032927393SChristoph Hellwig 	if (error)
60132927393SChristoph Hellwig 		goto out_free_buf;
60232927393SChristoph Hellwig 
60332927393SChristoph Hellwig 	/* careful: calling conventions are nasty here */
6044bd6a735SMatthew Wilcox (Oracle) 	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
60532927393SChristoph Hellwig 	if (error)
60632927393SChristoph Hellwig 		goto out_free_buf;
60732927393SChristoph Hellwig 
60832927393SChristoph Hellwig 	if (!write) {
60932927393SChristoph Hellwig 		error = -EFAULT;
6104bd6a735SMatthew Wilcox (Oracle) 		if (copy_to_iter(kbuf, count, iter) < count)
61132927393SChristoph Hellwig 			goto out_free_buf;
61232927393SChristoph Hellwig 	}
61332927393SChristoph Hellwig 
6144e63acdfSAndrey Ignatov 	error = count;
61532927393SChristoph Hellwig out_free_buf:
61645089437SJosef Bacik 	kvfree(kbuf);
61777b14db5SEric W. Biederman out:
61877b14db5SEric W. Biederman 	sysctl_head_finish(head);
61977b14db5SEric W. Biederman 
62077b14db5SEric W. Biederman 	return error;
62177b14db5SEric W. Biederman }
62277b14db5SEric W. Biederman 
6234bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
6247708bfb1SPavel Emelyanov {
6254bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 0);
6267708bfb1SPavel Emelyanov }
6277708bfb1SPavel Emelyanov 
6284bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
62977b14db5SEric W. Biederman {
6304bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 1);
63177b14db5SEric W. Biederman }
63277b14db5SEric W. Biederman 
633f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
634f1ecf068SLucas De Marchi {
6354e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
636f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
637f1ecf068SLucas De Marchi 
6384e474a00SLucas De Marchi 	/* sysctl was unregistered */
6394e474a00SLucas De Marchi 	if (IS_ERR(head))
6404e474a00SLucas De Marchi 		return PTR_ERR(head);
6414e474a00SLucas De Marchi 
642f1ecf068SLucas De Marchi 	if (table->poll)
643f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
644f1ecf068SLucas De Marchi 
6454e474a00SLucas De Marchi 	sysctl_head_finish(head);
6464e474a00SLucas De Marchi 
647f1ecf068SLucas De Marchi 	return 0;
648f1ecf068SLucas De Marchi }
649f1ecf068SLucas De Marchi 
650076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
651f1ecf068SLucas De Marchi {
652496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
6534e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
654f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
655076ccb76SAl Viro 	__poll_t ret = DEFAULT_POLLMASK;
6564e474a00SLucas De Marchi 	unsigned long event;
6574e474a00SLucas De Marchi 
6584e474a00SLucas De Marchi 	/* sysctl was unregistered */
6594e474a00SLucas De Marchi 	if (IS_ERR(head))
660a9a08845SLinus Torvalds 		return EPOLLERR | EPOLLHUP;
661f1ecf068SLucas De Marchi 
662f1ecf068SLucas De Marchi 	if (!table->proc_handler)
663f1ecf068SLucas De Marchi 		goto out;
664f1ecf068SLucas De Marchi 
665f1ecf068SLucas De Marchi 	if (!table->poll)
666f1ecf068SLucas De Marchi 		goto out;
667f1ecf068SLucas De Marchi 
6684e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
669f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
670f1ecf068SLucas De Marchi 
671f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
672f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
673a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
674f1ecf068SLucas De Marchi 	}
675f1ecf068SLucas De Marchi 
676f1ecf068SLucas De Marchi out:
6774e474a00SLucas De Marchi 	sysctl_head_finish(head);
6784e474a00SLucas De Marchi 
679f1ecf068SLucas De Marchi 	return ret;
680f1ecf068SLucas De Marchi }
68177b14db5SEric W. Biederman 
682f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
683f0c3b509SAl Viro 				struct dir_context *ctx,
6849043476fSAl Viro 				struct ctl_table_header *head,
6859043476fSAl Viro 				struct ctl_table *table)
68677b14db5SEric W. Biederman {
687f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
68877b14db5SEric W. Biederman 	struct inode *inode;
68977b14db5SEric W. Biederman 	struct qstr qname;
69077b14db5SEric W. Biederman 	ino_t ino = 0;
69177b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
69277b14db5SEric W. Biederman 
69377b14db5SEric W. Biederman 	qname.name = table->procname;
69477b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
6958387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
69677b14db5SEric W. Biederman 
69777b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
69877b14db5SEric W. Biederman 	if (!child) {
69976aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
70076aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
70176aab3abSAl Viro 		if (IS_ERR(child))
70276aab3abSAl Viro 			return false;
70376aab3abSAl Viro 		if (d_in_lookup(child)) {
704888e2b03SAl Viro 			struct dentry *res;
7059043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
706ea5751ccSIvan Delalande 			if (IS_ERR(inode)) {
70776aab3abSAl Viro 				d_lookup_done(child);
7089043476fSAl Viro 				dput(child);
709f0c3b509SAl Viro 				return false;
71076aab3abSAl Viro 			}
711fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
712888e2b03SAl Viro 			res = d_splice_alias(inode, child);
713888e2b03SAl Viro 			d_lookup_done(child);
714888e2b03SAl Viro 			if (unlikely(res)) {
715888e2b03SAl Viro 				if (IS_ERR(res)) {
716888e2b03SAl Viro 					dput(child);
717888e2b03SAl Viro 					return false;
718888e2b03SAl Viro 				}
719888e2b03SAl Viro 				dput(child);
720888e2b03SAl Viro 				child = res;
721888e2b03SAl Viro 			}
72277b14db5SEric W. Biederman 		}
72377b14db5SEric W. Biederman 	}
7242b0143b5SDavid Howells 	inode = d_inode(child);
72577b14db5SEric W. Biederman 	ino  = inode->i_ino;
72677b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
72777b14db5SEric W. Biederman 	dput(child);
728f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
7299043476fSAl Viro }
7309043476fSAl Viro 
731f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
732f0c3b509SAl Viro 				    struct dir_context *ctx,
7330e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
7340e47c99dSEric W. Biederman 				    struct ctl_table *table)
7350e47c99dSEric W. Biederman {
736f0c3b509SAl Viro 	bool ret = true;
737a0b0d1c3SDanilo Krummrich 
7380e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
739a0b0d1c3SDanilo Krummrich 	if (IS_ERR(head))
740a0b0d1c3SDanilo Krummrich 		return false;
7410e47c99dSEric W. Biederman 
7420e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
743835b94e0SDanilo Krummrich 	if (sysctl_follow_link(&head, &table))
7440e47c99dSEric W. Biederman 		goto out;
7450e47c99dSEric W. Biederman 
746f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
7470e47c99dSEric W. Biederman out:
7480e47c99dSEric W. Biederman 	sysctl_head_finish(head);
7490e47c99dSEric W. Biederman 	return ret;
7500e47c99dSEric W. Biederman }
7510e47c99dSEric W. Biederman 
752e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table,
7539043476fSAl Viro 		unsigned long *pos, struct file *file,
754f0c3b509SAl Viro 		struct dir_context *ctx)
7559043476fSAl Viro {
756f0c3b509SAl Viro 	bool res;
7579043476fSAl Viro 
758f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
759f0c3b509SAl Viro 		return true;
7609043476fSAl Viro 
7610e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
762f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
7630e47c99dSEric W. Biederman 	else
764f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
7659043476fSAl Viro 
766f0c3b509SAl Viro 	if (res)
767f0c3b509SAl Viro 		ctx->pos = *pos;
7686a75ce16SEric W. Biederman 
7696a75ce16SEric W. Biederman 	return res;
77077b14db5SEric W. Biederman }
77177b14db5SEric W. Biederman 
772f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
77377b14db5SEric W. Biederman {
774f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7759043476fSAl Viro 	struct ctl_table_header *h = NULL;
7766a75ce16SEric W. Biederman 	struct ctl_table *entry;
7777ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
77877b14db5SEric W. Biederman 	unsigned long pos;
77977b14db5SEric W. Biederman 
7809043476fSAl Viro 	if (IS_ERR(head))
7819043476fSAl Viro 		return PTR_ERR(head);
7829043476fSAl Viro 
7837ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
78477b14db5SEric W. Biederman 
785f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
78693362fa4SZhou Chengming 		goto out;
787f0c3b509SAl Viro 
78877b14db5SEric W. Biederman 	pos = 2;
78977b14db5SEric W. Biederman 
7907ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
791f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7929043476fSAl Viro 			sysctl_head_finish(h);
7939043476fSAl Viro 			break;
79477b14db5SEric W. Biederman 		}
79577b14db5SEric W. Biederman 	}
79693362fa4SZhou Chengming out:
79777b14db5SEric W. Biederman 	sysctl_head_finish(head);
798f0c3b509SAl Viro 	return 0;
79977b14db5SEric W. Biederman }
80077b14db5SEric W. Biederman 
8014609e1f1SChristian Brauner static int proc_sys_permission(struct mnt_idmap *idmap,
802549c7297SChristian Brauner 			       struct inode *inode, int mask)
80377b14db5SEric W. Biederman {
80477b14db5SEric W. Biederman 	/*
80577b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
80677b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
80777b14db5SEric W. Biederman 	 */
808f696a365SMiklos Szeredi 	struct ctl_table_header *head;
809f696a365SMiklos Szeredi 	struct ctl_table *table;
81077b14db5SEric W. Biederman 	int error;
81177b14db5SEric W. Biederman 
812f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
813f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
814f696a365SMiklos Szeredi 		return -EACCES;
815f696a365SMiklos Szeredi 
816f696a365SMiklos Szeredi 	head = grab_header(inode);
8179043476fSAl Viro 	if (IS_ERR(head))
8189043476fSAl Viro 		return PTR_ERR(head);
81977b14db5SEric W. Biederman 
820f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
8219043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
8229043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
8239043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
82473f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
8259043476fSAl Viro 
82677b14db5SEric W. Biederman 	sysctl_head_finish(head);
82777b14db5SEric W. Biederman 	return error;
82877b14db5SEric W. Biederman }
82977b14db5SEric W. Biederman 
830c1632a0fSChristian Brauner static int proc_sys_setattr(struct mnt_idmap *idmap,
831549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *attr)
83277b14db5SEric W. Biederman {
8332b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
83477b14db5SEric W. Biederman 	int error;
83577b14db5SEric W. Biederman 
83677b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
83777b14db5SEric W. Biederman 		return -EPERM;
83877b14db5SEric W. Biederman 
839c1632a0fSChristian Brauner 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
8401025774cSChristoph Hellwig 	if (error)
84177b14db5SEric W. Biederman 		return error;
8421025774cSChristoph Hellwig 
843c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, attr);
8441025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8451025774cSChristoph Hellwig 	return 0;
84677b14db5SEric W. Biederman }
84777b14db5SEric W. Biederman 
848b74d24f7SChristian Brauner static int proc_sys_getattr(struct mnt_idmap *idmap,
849549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
850a528d35eSDavid Howells 			    u32 request_mask, unsigned int query_flags)
8519043476fSAl Viro {
852a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
8539043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
8549043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
8559043476fSAl Viro 
8569043476fSAl Viro 	if (IS_ERR(head))
8579043476fSAl Viro 		return PTR_ERR(head);
8589043476fSAl Viro 
859b74d24f7SChristian Brauner 	generic_fillattr(&nop_mnt_idmap, inode, stat);
8609043476fSAl Viro 	if (table)
8619043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
8629043476fSAl Viro 
8639043476fSAl Viro 	sysctl_head_finish(head);
8649043476fSAl Viro 	return 0;
8659043476fSAl Viro }
8669043476fSAl Viro 
86777b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
868f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
869f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
8704bd6a735SMatthew Wilcox (Oracle) 	.read_iter	= proc_sys_read,
8714bd6a735SMatthew Wilcox (Oracle) 	.write_iter	= proc_sys_write,
8724bd6a735SMatthew Wilcox (Oracle) 	.splice_read	= generic_file_splice_read,
8734bd6a735SMatthew Wilcox (Oracle) 	.splice_write	= iter_file_splice_write,
8746038f373SArnd Bergmann 	.llseek		= default_llseek,
8759043476fSAl Viro };
8769043476fSAl Viro 
8779043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
878887df078SPavel Emelyanov 	.read		= generic_read_dir,
879f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
8803222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
88177b14db5SEric W. Biederman };
88277b14db5SEric W. Biederman 
88303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8849043476fSAl Viro 	.permission	= proc_sys_permission,
8859043476fSAl Viro 	.setattr	= proc_sys_setattr,
8869043476fSAl Viro 	.getattr	= proc_sys_getattr,
8879043476fSAl Viro };
8889043476fSAl Viro 
8899043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
89077b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
89177b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
89277b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
8939043476fSAl Viro 	.getattr	= proc_sys_getattr,
89477b14db5SEric W. Biederman };
89577b14db5SEric W. Biederman 
8960b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
89777b14db5SEric W. Biederman {
8980b728e19SAl Viro 	if (flags & LOOKUP_RCU)
89934286d66SNick Piggin 		return -ECHILD;
9002b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
9019043476fSAl Viro }
9029043476fSAl Viro 
903fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
9049043476fSAl Viro {
9052b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
9069043476fSAl Viro }
9079043476fSAl Viro 
9081f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
9091f87f0b5SEric W. Biederman {
9101f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
9111f87f0b5SEric W. Biederman 	int res;
9121f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9131f87f0b5SEric W. Biederman 	if (p->unregistering)
9141f87f0b5SEric W. Biederman 		res = 0;
9151f87f0b5SEric W. Biederman 	else if (!set->is_seen)
9161f87f0b5SEric W. Biederman 		res = 1;
9171f87f0b5SEric W. Biederman 	else
9181f87f0b5SEric W. Biederman 		res = set->is_seen(set);
9191f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
9201f87f0b5SEric W. Biederman 	return res;
9211f87f0b5SEric W. Biederman }
9221f87f0b5SEric W. Biederman 
9236fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry,
924621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
9259043476fSAl Viro {
926dfef6dcdSAl Viro 	struct ctl_table_header *head;
927da53be12SLinus Torvalds 	struct inode *inode;
928da53be12SLinus Torvalds 
92931e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
93031e6b01fSNick Piggin 	 * that inode here can be NULL */
931dfef6dcdSAl Viro 	/* AV: can it, indeed? */
9322b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
93331e6b01fSNick Piggin 	if (!inode)
934dfef6dcdSAl Viro 		return 1;
935621e155aSNick Piggin 	if (name->len != len)
9369043476fSAl Viro 		return 1;
937621e155aSNick Piggin 	if (memcmp(name->name, str, len))
9389043476fSAl Viro 		return 1;
939dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
940dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
94177b14db5SEric W. Biederman }
94277b14db5SEric W. Biederman 
943d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
94477b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
9459043476fSAl Viro 	.d_delete	= proc_sys_delete,
9469043476fSAl Viro 	.d_compare	= proc_sys_compare,
94777b14db5SEric W. Biederman };
94877b14db5SEric W. Biederman 
9490e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
9507ec66d06SEric W. Biederman 				   const char *name, int namelen)
9511f87f0b5SEric W. Biederman {
9527ec66d06SEric W. Biederman 	struct ctl_table_header *head;
9537ec66d06SEric W. Biederman 	struct ctl_table *entry;
9541f87f0b5SEric W. Biederman 
9550e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
9567ec66d06SEric W. Biederman 	if (!entry)
9577ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
95851f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
9597ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
96051f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
9611f87f0b5SEric W. Biederman }
9621f87f0b5SEric W. Biederman 
9637ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
9647ec66d06SEric W. Biederman 			       const char *name, int namelen)
9651f87f0b5SEric W. Biederman {
9667ec66d06SEric W. Biederman 	struct ctl_table *table;
9677ec66d06SEric W. Biederman 	struct ctl_dir *new;
968ac13ac6fSEric W. Biederman 	struct ctl_node *node;
9697ec66d06SEric W. Biederman 	char *new_name;
9701f87f0b5SEric W. Biederman 
971ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
972ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
973ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
9747ec66d06SEric W. Biederman 	if (!new)
9757ec66d06SEric W. Biederman 		return NULL;
9767ec66d06SEric W. Biederman 
977ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
978ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
9797ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
9807ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9817ec66d06SEric W. Biederman 	table[0].procname = new_name;
9827ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
983ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
9847ec66d06SEric W. Biederman 
9857ec66d06SEric W. Biederman 	return new;
9861f87f0b5SEric W. Biederman }
9871f87f0b5SEric W. Biederman 
98860f126d9SEric W. Biederman /**
98960f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
99060f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
99160f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
99260f126d9SEric W. Biederman  * @namelen: The length of name
99360f126d9SEric W. Biederman  *
99460f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
99560f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
99660f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
99760f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
99860f126d9SEric W. Biederman  * simply dropped.
99960f126d9SEric W. Biederman  */
10000e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
10010e47c99dSEric W. Biederman 				  const char *name, int namelen)
10027ec66d06SEric W. Biederman {
10030e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
10047ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
10050eb97f38SEric W. Biederman 	int err;
10067ec66d06SEric W. Biederman 
10077ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10080e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10097ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10107ec66d06SEric W. Biederman 		goto found;
10117ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10127ec66d06SEric W. Biederman 		goto failed;
10137ec66d06SEric W. Biederman 
10147ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10157ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
10167ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10177ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
10187ec66d06SEric W. Biederman 	if (!new)
10197ec66d06SEric W. Biederman 		goto failed;
10207ec66d06SEric W. Biederman 
102160f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
10220e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10237ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10247ec66d06SEric W. Biederman 		goto found;
10257ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10267ec66d06SEric W. Biederman 		goto failed;
10277ec66d06SEric W. Biederman 
102860f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
10290eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
10300eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
10310eb97f38SEric W. Biederman 	if (err)
10320e47c99dSEric W. Biederman 		goto failed;
10337ec66d06SEric W. Biederman 	subdir = new;
10347ec66d06SEric W. Biederman found:
10357ec66d06SEric W. Biederman 	subdir->header.nreg++;
10367ec66d06SEric W. Biederman failed:
1037a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
103887ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
10396980128fSEric W. Biederman 		sysctl_print_dir(dir);
1040153ee1c4SGeert Uytterhoeven 		pr_cont("%*.*s %ld\n", namelen, namelen, name,
1041153ee1c4SGeert Uytterhoeven 			PTR_ERR(subdir));
10421f87f0b5SEric W. Biederman 	}
10437ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10447ec66d06SEric W. Biederman 	if (new)
10457ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
10467ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10477ec66d06SEric W. Biederman 	return subdir;
10481f87f0b5SEric W. Biederman }
10491f87f0b5SEric W. Biederman 
10500e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
10510e47c99dSEric W. Biederman {
10520e47c99dSEric W. Biederman 	struct ctl_dir *parent;
10530e47c99dSEric W. Biederman 	const char *procname;
10540e47c99dSEric W. Biederman 	if (!dir->header.parent)
10550e47c99dSEric W. Biederman 		return &set->dir;
10560e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
10570e47c99dSEric W. Biederman 	if (IS_ERR(parent))
10580e47c99dSEric W. Biederman 		return parent;
10590e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
10600e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
10610e47c99dSEric W. Biederman }
10620e47c99dSEric W. Biederman 
10630e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
106413bcc6a2SEric W. Biederman 	struct ctl_table **pentry)
10650e47c99dSEric W. Biederman {
10660e47c99dSEric W. Biederman 	struct ctl_table_header *head;
10670e47c99dSEric W. Biederman 	struct ctl_table_root *root;
10680e47c99dSEric W. Biederman 	struct ctl_table_set *set;
10690e47c99dSEric W. Biederman 	struct ctl_table *entry;
10700e47c99dSEric W. Biederman 	struct ctl_dir *dir;
10710e47c99dSEric W. Biederman 	int ret;
10720e47c99dSEric W. Biederman 
10730e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
10740e47c99dSEric W. Biederman 	root = (*pentry)->data;
107513bcc6a2SEric W. Biederman 	set = lookup_header_set(root);
10760e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
10770e47c99dSEric W. Biederman 	if (IS_ERR(dir))
10780e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10790e47c99dSEric W. Biederman 	else {
10800e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10810e47c99dSEric W. Biederman 		head = NULL;
10820e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10830e47c99dSEric W. Biederman 		ret = -ENOENT;
10840e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10850e47c99dSEric W. Biederman 			unuse_table(*phead);
10860e47c99dSEric W. Biederman 			*phead = head;
10870e47c99dSEric W. Biederman 			*pentry = entry;
10880e47c99dSEric W. Biederman 			ret = 0;
10890e47c99dSEric W. Biederman 		}
10900e47c99dSEric W. Biederman 	}
10910e47c99dSEric W. Biederman 
10920e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
10930e47c99dSEric W. Biederman 	return ret;
10940e47c99dSEric W. Biederman }
10950e47c99dSEric W. Biederman 
10967c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
10977c60c48fSEric W. Biederman {
10987c60c48fSEric W. Biederman 	struct va_format vaf;
10997c60c48fSEric W. Biederman 	va_list args;
11007c60c48fSEric W. Biederman 
11017c60c48fSEric W. Biederman 	va_start(args, fmt);
11027c60c48fSEric W. Biederman 	vaf.fmt = fmt;
11037c60c48fSEric W. Biederman 	vaf.va = &args;
11047c60c48fSEric W. Biederman 
110587ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
11067c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
11077c60c48fSEric W. Biederman 
11087c60c48fSEric W. Biederman 	va_end(args);
11097c60c48fSEric W. Biederman 	return -EINVAL;
11107c60c48fSEric W. Biederman }
11117c60c48fSEric W. Biederman 
11124f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table)
11134f2fec00SLuis R. Rodriguez {
11144f2fec00SLuis R. Rodriguez 	int err = 0;
11154f2fec00SLuis R. Rodriguez 
111661d9b56aSLuis R. Rodriguez 	if ((table->proc_handler == proc_douintvec) ||
111761d9b56aSLuis R. Rodriguez 	    (table->proc_handler == proc_douintvec_minmax)) {
11184f2fec00SLuis R. Rodriguez 		if (table->maxlen != sizeof(unsigned int))
111964a11f3dSWaiman Long 			err |= sysctl_err(path, table, "array not allowed");
11204f2fec00SLuis R. Rodriguez 	}
11214f2fec00SLuis R. Rodriguez 
1122cb944413SEric Dumazet 	if (table->proc_handler == proc_dou8vec_minmax) {
1123cb944413SEric Dumazet 		if (table->maxlen != sizeof(u8))
1124cb944413SEric Dumazet 			err |= sysctl_err(path, table, "array not allowed");
1125cb944413SEric Dumazet 	}
1126cb944413SEric Dumazet 
1127f1aa2eb5SOndrej Mosnacek 	if (table->proc_handler == proc_dobool) {
1128f1aa2eb5SOndrej Mosnacek 		if (table->maxlen != sizeof(bool))
1129f1aa2eb5SOndrej Mosnacek 			err |= sysctl_err(path, table, "array not allowed");
1130f1aa2eb5SOndrej Mosnacek 	}
1131f1aa2eb5SOndrej Mosnacek 
11324f2fec00SLuis R. Rodriguez 	return err;
11334f2fec00SLuis R. Rodriguez }
11344f2fec00SLuis R. Rodriguez 
11357c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
11367c60c48fSEric W. Biederman {
1137cb55f27aSMeng Tang 	struct ctl_table *entry;
11387c60c48fSEric W. Biederman 	int err = 0;
1139cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1140cb55f27aSMeng Tang 		if (entry->child)
1141cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "Not a file");
11427c60c48fSEric W. Biederman 
1143cb55f27aSMeng Tang 		if ((entry->proc_handler == proc_dostring) ||
1144f1aa2eb5SOndrej Mosnacek 		    (entry->proc_handler == proc_dobool) ||
1145cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec) ||
1146cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec) ||
1147cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec_minmax) ||
1148cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_minmax) ||
1149cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dou8vec_minmax) ||
1150cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_jiffies) ||
1151cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1152cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1153cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_minmax) ||
1154cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1155cb55f27aSMeng Tang 			if (!entry->data)
1156cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No data");
1157cb55f27aSMeng Tang 			if (!entry->maxlen)
1158cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No maxlen");
11594f2fec00SLuis R. Rodriguez 			else
1160cb55f27aSMeng Tang 				err |= sysctl_check_table_array(path, entry);
11611f87f0b5SEric W. Biederman 		}
1162cb55f27aSMeng Tang 		if (!entry->proc_handler)
1163cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "No proc_handler");
11647c60c48fSEric W. Biederman 
1165cb55f27aSMeng Tang 		if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1166cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
1167cb55f27aSMeng Tang 				entry->mode);
11681f87f0b5SEric W. Biederman 	}
11697c60c48fSEric W. Biederman 	return err;
11701f87f0b5SEric W. Biederman }
11711f87f0b5SEric W. Biederman 
11720e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
11730e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
11740e47c99dSEric W. Biederman {
11750e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
11760e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1177ac13ac6fSEric W. Biederman 	struct ctl_node *node;
11780e47c99dSEric W. Biederman 	char *link_name;
11790e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
11800e47c99dSEric W. Biederman 
11810e47c99dSEric W. Biederman 	name_bytes = 0;
11820e47c99dSEric W. Biederman 	nr_entries = 0;
1183cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
11840e47c99dSEric W. Biederman 		nr_entries++;
11850e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
11860e47c99dSEric W. Biederman 	}
11870e47c99dSEric W. Biederman 
11880e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1189ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
11900e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
11910e47c99dSEric W. Biederman 			name_bytes,
11920e47c99dSEric W. Biederman 			GFP_KERNEL);
11930e47c99dSEric W. Biederman 
11940e47c99dSEric W. Biederman 	if (!links)
11950e47c99dSEric W. Biederman 		return NULL;
11960e47c99dSEric W. Biederman 
1197ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1198ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
11990e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
1200cb55f27aSMeng Tang 	link = link_table;
12010e47c99dSEric W. Biederman 
1202cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12030e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
12040e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
12050e47c99dSEric W. Biederman 		link->procname = link_name;
12060e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
12070e47c99dSEric W. Biederman 		link->data = link_root;
12080e47c99dSEric W. Biederman 		link_name += len;
1209cb55f27aSMeng Tang 		link++;
12100e47c99dSEric W. Biederman 	}
1211ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
12120e47c99dSEric W. Biederman 	links->nreg = nr_entries;
12130e47c99dSEric W. Biederman 
12140e47c99dSEric W. Biederman 	return links;
12150e47c99dSEric W. Biederman }
12160e47c99dSEric W. Biederman 
12170e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
12180e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
12190e47c99dSEric W. Biederman {
12200e47c99dSEric W. Biederman 	struct ctl_table_header *head;
12210e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
12220e47c99dSEric W. Biederman 
12230e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
1224cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12250e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12260e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12270e47c99dSEric W. Biederman 		if (!link)
12280e47c99dSEric W. Biederman 			return false;
12290e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
12300e47c99dSEric W. Biederman 			continue;
12310e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
12320e47c99dSEric W. Biederman 			continue;
12330e47c99dSEric W. Biederman 		return false;
12340e47c99dSEric W. Biederman 	}
12350e47c99dSEric W. Biederman 
12360e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
1237cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12380e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12390e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12400e47c99dSEric W. Biederman 		head->nreg++;
12410e47c99dSEric W. Biederman 	}
12420e47c99dSEric W. Biederman 	return true;
12430e47c99dSEric W. Biederman }
12440e47c99dSEric W. Biederman 
12450e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
12460e47c99dSEric W. Biederman {
12470e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
12489a521359SLi zeming 	struct ctl_dir *core_parent;
12490e47c99dSEric W. Biederman 	struct ctl_table_header *links;
12500e47c99dSEric W. Biederman 	int err;
12510e47c99dSEric W. Biederman 
12520e47c99dSEric W. Biederman 	if (head->set == root_set)
12530e47c99dSEric W. Biederman 		return 0;
12540e47c99dSEric W. Biederman 
12550e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
12560e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
12570e47c99dSEric W. Biederman 		return 0;
12580e47c99dSEric W. Biederman 
12590e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
12600e47c99dSEric W. Biederman 		return 0;
12610e47c99dSEric W. Biederman 
12620e47c99dSEric W. Biederman 	core_parent->header.nreg++;
12630e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
12640e47c99dSEric W. Biederman 
12650e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
12660e47c99dSEric W. Biederman 
12670e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
12680e47c99dSEric W. Biederman 	err = -ENOMEM;
12690e47c99dSEric W. Biederman 	if (!links)
12700e47c99dSEric W. Biederman 		goto out;
12710e47c99dSEric W. Biederman 
12720e47c99dSEric W. Biederman 	err = 0;
12730e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
12740e47c99dSEric W. Biederman 		kfree(links);
12750e47c99dSEric W. Biederman 		goto out;
12760e47c99dSEric W. Biederman 	}
12770e47c99dSEric W. Biederman 
12780e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
12790e47c99dSEric W. Biederman 	if (err)
12800e47c99dSEric W. Biederman 		kfree(links);
12810e47c99dSEric W. Biederman out:
12820e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
12830e47c99dSEric W. Biederman 	return err;
12840e47c99dSEric W. Biederman }
12850e47c99dSEric W. Biederman 
1286b2f56e55SLuis Chamberlain /* Find the directory for the ctl_table. If one is not found create it. */
1287b2f56e55SLuis Chamberlain static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
1288b2f56e55SLuis Chamberlain {
1289b2f56e55SLuis Chamberlain 	const char *name, *nextname;
1290b2f56e55SLuis Chamberlain 
1291b2f56e55SLuis Chamberlain 	for (name = path; name; name = nextname) {
1292b2f56e55SLuis Chamberlain 		int namelen;
1293b2f56e55SLuis Chamberlain 		nextname = strchr(name, '/');
1294b2f56e55SLuis Chamberlain 		if (nextname) {
1295b2f56e55SLuis Chamberlain 			namelen = nextname - name;
1296b2f56e55SLuis Chamberlain 			nextname++;
1297b2f56e55SLuis Chamberlain 		} else {
1298b2f56e55SLuis Chamberlain 			namelen = strlen(name);
1299b2f56e55SLuis Chamberlain 		}
1300b2f56e55SLuis Chamberlain 		if (namelen == 0)
1301b2f56e55SLuis Chamberlain 			continue;
1302b2f56e55SLuis Chamberlain 
1303b2f56e55SLuis Chamberlain 		/*
1304b2f56e55SLuis Chamberlain 		 * namelen ensures if name is "foo/bar/yay" only foo is
1305b2f56e55SLuis Chamberlain 		 * registered first. We traverse as if using mkdir -p and
1306b2f56e55SLuis Chamberlain 		 * return a ctl_dir for the last directory entry.
1307b2f56e55SLuis Chamberlain 		 */
1308b2f56e55SLuis Chamberlain 		dir = get_subdir(dir, name, namelen);
1309b2f56e55SLuis Chamberlain 		if (IS_ERR(dir))
1310b2f56e55SLuis Chamberlain 			break;
1311b2f56e55SLuis Chamberlain 	}
1312b2f56e55SLuis Chamberlain 	return dir;
1313b2f56e55SLuis Chamberlain }
1314b2f56e55SLuis Chamberlain 
13151f87f0b5SEric W. Biederman /**
1316f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
131760a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
13181f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
131967ff3228SLuis Chamberlain  * @table: the top-level table structure without any child
13201f87f0b5SEric W. Biederman  *
13211f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
13221f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
13231f87f0b5SEric W. Biederman  *
13241f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
13251f87f0b5SEric W. Biederman  *
13261f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
13271f87f0b5SEric W. Biederman  *            enter a sysctl file
13281f87f0b5SEric W. Biederman  *
13291f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
13301f87f0b5SEric W. Biederman  *
13311f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
13321f87f0b5SEric W. Biederman  *
1333f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
13341f87f0b5SEric W. Biederman  *
1335f728019bSEric W. Biederman  * child - must be %NULL.
13361f87f0b5SEric W. Biederman  *
13371f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
13381f87f0b5SEric W. Biederman  *
13391f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
134067ff3228SLuis Chamberlain  * XXX: we should eventually modify these to use long min / max [0]
134167ff3228SLuis Chamberlain  * [0] https://lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org
13421f87f0b5SEric W. Biederman  *
13431f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
134467ff3228SLuis Chamberlain  * under /proc; non-leaf nodes (where child is not NULL) are not allowed,
134567ff3228SLuis Chamberlain  * sysctl_check_table() verifies this.
13461f87f0b5SEric W. Biederman  *
1347f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1348f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
13491f87f0b5SEric W. Biederman  *
13501f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
13511f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
13521f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
13531f87f0b5SEric W. Biederman  *
13541f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
13551f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
13561f87f0b5SEric W. Biederman  *
13571f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
13581f87f0b5SEric W. Biederman  * to the table header on success.
13591f87f0b5SEric W. Biederman  */
13606e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
136160a47a2eSEric W. Biederman 	struct ctl_table_set *set,
13626e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
13631f87f0b5SEric W. Biederman {
136460a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
13651f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
13667ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1367ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1368ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1369ac13ac6fSEric W. Biederman 	int nr_entries = 0;
13701f87f0b5SEric W. Biederman 
1371cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table)
1372ac13ac6fSEric W. Biederman 		nr_entries++;
1373ac13ac6fSEric W. Biederman 
1374ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1375425b9c7fSVasily Averin 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL_ACCOUNT);
13761f87f0b5SEric W. Biederman 	if (!header)
13771f87f0b5SEric W. Biederman 		return NULL;
13781f87f0b5SEric W. Biederman 
1379ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1380ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
13817c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
13827c60c48fSEric W. Biederman 		goto fail;
13838d6ecfccSEric W. Biederman 
13841f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
13850e47c99dSEric W. Biederman 	dir = &set->dir;
138667ff3228SLuis Chamberlain 	/* Reference moved down the directory tree get_subdir */
13877ec66d06SEric W. Biederman 	dir->header.nreg++;
13887ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
13897ec66d06SEric W. Biederman 
1390b2f56e55SLuis Chamberlain 	dir = sysctl_mkdir_p(dir, path);
13917ec66d06SEric W. Biederman 	if (IS_ERR(dir))
13927ec66d06SEric W. Biederman 		goto fail;
13937ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
13940e47c99dSEric W. Biederman 	if (insert_header(dir, header))
13950e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
13960e47c99dSEric W. Biederman 
13977ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13981f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13991f87f0b5SEric W. Biederman 
14001f87f0b5SEric W. Biederman 	return header;
14010e47c99dSEric W. Biederman 
14027ec66d06SEric W. Biederman fail_put_dir_locked:
14037ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
14047c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
14057c60c48fSEric W. Biederman fail:
14067c60c48fSEric W. Biederman 	kfree(header);
14077c60c48fSEric W. Biederman 	dump_stack();
14087c60c48fSEric W. Biederman 	return NULL;
14091f87f0b5SEric W. Biederman }
14101f87f0b5SEric W. Biederman 
1411fea478d4SEric W. Biederman /**
1412fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1413fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1414fea478d4SEric W. Biederman  * @table: the table structure
1415fea478d4SEric W. Biederman  *
1416fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1417fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1418fea478d4SEric W. Biederman  *
1419fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1420fea478d4SEric W. Biederman  */
1421fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1422fea478d4SEric W. Biederman {
1423fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1424fea478d4SEric W. Biederman 					path, table);
1425fea478d4SEric W. Biederman }
1426fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1427fea478d4SEric W. Biederman 
14283ddd9a80SXiaoming Ni /**
14293ddd9a80SXiaoming Ni  * __register_sysctl_init() - register sysctl table to path
14303ddd9a80SXiaoming Ni  * @path: path name for sysctl base
14313ddd9a80SXiaoming Ni  * @table: This is the sysctl table that needs to be registered to the path
14323ddd9a80SXiaoming Ni  * @table_name: The name of sysctl table, only used for log printing when
14333ddd9a80SXiaoming Ni  *              registration fails
14343ddd9a80SXiaoming Ni  *
14353ddd9a80SXiaoming Ni  * The sysctl interface is used by userspace to query or modify at runtime
14363ddd9a80SXiaoming Ni  * a predefined value set on a variable. These variables however have default
14373ddd9a80SXiaoming Ni  * values pre-set. Code which depends on these variables will always work even
14383ddd9a80SXiaoming Ni  * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
14393ddd9a80SXiaoming Ni  * ability to query or modify the sysctls dynamically at run time. Chances of
14403ddd9a80SXiaoming Ni  * register_sysctl() failing on init are extremely low, and so for both reasons
14413ddd9a80SXiaoming Ni  * this function does not return any error as it is used by initialization code.
14423ddd9a80SXiaoming Ni  *
1443*228b09deSLuis Chamberlain  * Context: if your base directory does not exist it will be created for you.
14443ddd9a80SXiaoming Ni  */
14453ddd9a80SXiaoming Ni void __init __register_sysctl_init(const char *path, struct ctl_table *table,
14463ddd9a80SXiaoming Ni 				 const char *table_name)
14473ddd9a80SXiaoming Ni {
14483ddd9a80SXiaoming Ni 	struct ctl_table_header *hdr = register_sysctl(path, table);
14493ddd9a80SXiaoming Ni 
14503ddd9a80SXiaoming Ni 	if (unlikely(!hdr)) {
14513ddd9a80SXiaoming Ni 		pr_err("failed when register_sysctl %s to %s\n", table_name, path);
14523ddd9a80SXiaoming Ni 		return;
14533ddd9a80SXiaoming Ni 	}
14543ddd9a80SXiaoming Ni 	kmemleak_not_leak(hdr);
14553ddd9a80SXiaoming Ni }
14563ddd9a80SXiaoming Ni 
14576e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
14586e9d5164SEric W. Biederman {
14596e9d5164SEric W. Biederman 	int namelen;
14606e9d5164SEric W. Biederman 	namelen = strlen(name);
14616e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
14626e9d5164SEric W. Biederman 		return NULL;
14636e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
14646e9d5164SEric W. Biederman 	pos[namelen] = '/';
14656e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
14666e9d5164SEric W. Biederman 	pos += namelen + 1;
14676e9d5164SEric W. Biederman 	return pos;
14686e9d5164SEric W. Biederman }
14696e9d5164SEric W. Biederman 
1470f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1471f728019bSEric W. Biederman {
1472f728019bSEric W. Biederman 	int has_files = 0;
1473f728019bSEric W. Biederman 	int nr_subheaders = 0;
1474f728019bSEric W. Biederman 	struct ctl_table *entry;
1475f728019bSEric W. Biederman 
1476f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1477f728019bSEric W. Biederman 	if (!table || !table->procname)
1478f728019bSEric W. Biederman 		return 1;
1479f728019bSEric W. Biederman 
1480cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1481f728019bSEric W. Biederman 		if (entry->child)
1482f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1483f728019bSEric W. Biederman 		else
1484f728019bSEric W. Biederman 			has_files = 1;
1485f728019bSEric W. Biederman 	}
1486f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1487f728019bSEric W. Biederman }
1488f728019bSEric W. Biederman 
1489f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
149060a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1491f728019bSEric W. Biederman 	struct ctl_table *table)
1492f728019bSEric W. Biederman {
1493f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1494f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1495f728019bSEric W. Biederman 	int nr_files = 0;
1496f728019bSEric W. Biederman 	int nr_dirs = 0;
1497f728019bSEric W. Biederman 	int err = -ENOMEM;
1498f728019bSEric W. Biederman 
1499cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1500f728019bSEric W. Biederman 		if (entry->child)
1501f728019bSEric W. Biederman 			nr_dirs++;
1502f728019bSEric W. Biederman 		else
1503f728019bSEric W. Biederman 			nr_files++;
1504f728019bSEric W. Biederman 	}
1505f728019bSEric W. Biederman 
1506f728019bSEric W. Biederman 	files = table;
1507f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1508f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1509f728019bSEric W. Biederman 		struct ctl_table *new;
15106396bb22SKees Cook 		files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
1511f728019bSEric W. Biederman 				GFP_KERNEL);
1512f728019bSEric W. Biederman 		if (!files)
1513f728019bSEric W. Biederman 			goto out;
1514f728019bSEric W. Biederman 
1515f728019bSEric W. Biederman 		ctl_table_arg = files;
1516cb55f27aSMeng Tang 		new = files;
1517cb55f27aSMeng Tang 
1518cb55f27aSMeng Tang 		list_for_each_table_entry(entry, table) {
1519f728019bSEric W. Biederman 			if (entry->child)
1520f728019bSEric W. Biederman 				continue;
1521f728019bSEric W. Biederman 			*new = *entry;
1522f728019bSEric W. Biederman 			new++;
1523f728019bSEric W. Biederman 		}
1524f728019bSEric W. Biederman 	}
1525f728019bSEric W. Biederman 
1526f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1527f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1528f728019bSEric W. Biederman 		struct ctl_table_header *header;
152960a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1530f728019bSEric W. Biederman 		if (!header) {
1531f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1532f728019bSEric W. Biederman 			goto out;
1533f728019bSEric W. Biederman 		}
1534f728019bSEric W. Biederman 
1535f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1536f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1537f728019bSEric W. Biederman 		**subheader = header;
1538f728019bSEric W. Biederman 		(*subheader)++;
1539f728019bSEric W. Biederman 	}
1540f728019bSEric W. Biederman 
1541f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1542cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1543f728019bSEric W. Biederman 		char *child_pos;
1544f728019bSEric W. Biederman 
1545f728019bSEric W. Biederman 		if (!entry->child)
1546f728019bSEric W. Biederman 			continue;
1547f728019bSEric W. Biederman 
1548f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1549f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1550f728019bSEric W. Biederman 		if (!child_pos)
1551f728019bSEric W. Biederman 			goto out;
1552f728019bSEric W. Biederman 
1553f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
155460a47a2eSEric W. Biederman 						  set, entry->child);
1555f728019bSEric W. Biederman 		pos[0] = '\0';
1556f728019bSEric W. Biederman 		if (err)
1557f728019bSEric W. Biederman 			goto out;
1558f728019bSEric W. Biederman 	}
1559f728019bSEric W. Biederman 	err = 0;
1560f728019bSEric W. Biederman out:
1561f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1562f728019bSEric W. Biederman 	return err;
1563f728019bSEric W. Biederman }
1564f728019bSEric W. Biederman 
15656e9d5164SEric W. Biederman /**
15666e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
156760a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
15686e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
15696e9d5164SEric W. Biederman  * @table: the top-level table structure
15706e9d5164SEric W. Biederman  *
15716e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15726e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15736e9d5164SEric W. Biederman  *
15746e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
15756e9d5164SEric W. Biederman  */
15766e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
157760a47a2eSEric W. Biederman 	struct ctl_table_set *set,
15786e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
15796e9d5164SEric W. Biederman {
1580ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1581f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1582f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
15836e9d5164SEric W. Biederman 	const struct ctl_path *component;
15846e9d5164SEric W. Biederman 	char *new_path, *pos;
15856e9d5164SEric W. Biederman 
15866e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
15876e9d5164SEric W. Biederman 	if (!new_path)
15886e9d5164SEric W. Biederman 		return NULL;
15896e9d5164SEric W. Biederman 
15906e9d5164SEric W. Biederman 	pos[0] = '\0';
15916e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
15926e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
15936e9d5164SEric W. Biederman 		if (!pos)
15946e9d5164SEric W. Biederman 			goto out;
15956e9d5164SEric W. Biederman 	}
1596ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1597ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1598ec6a5266SEric W. Biederman 		if (!pos)
1599ec6a5266SEric W. Biederman 			goto out;
1600ec6a5266SEric W. Biederman 		table = table->child;
1601ec6a5266SEric W. Biederman 	}
1602f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
160360a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1604ec6a5266SEric W. Biederman 		if (header)
1605ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1606f728019bSEric W. Biederman 	} else {
1607f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1608f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1609f728019bSEric W. Biederman 		if (!header)
1610f728019bSEric W. Biederman 			goto out;
1611f728019bSEric W. Biederman 
1612f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1613f728019bSEric W. Biederman 		subheader = subheaders;
1614f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1615f728019bSEric W. Biederman 
1616f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
161760a47a2eSEric W. Biederman 						set, table))
1618f728019bSEric W. Biederman 			goto err_register_leaves;
1619f728019bSEric W. Biederman 	}
1620f728019bSEric W. Biederman 
16216e9d5164SEric W. Biederman out:
16226e9d5164SEric W. Biederman 	kfree(new_path);
16236e9d5164SEric W. Biederman 	return header;
1624f728019bSEric W. Biederman 
1625f728019bSEric W. Biederman err_register_leaves:
1626f728019bSEric W. Biederman 	while (subheader > subheaders) {
1627f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1628f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1629f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1630f728019bSEric W. Biederman 		kfree(table);
1631f728019bSEric W. Biederman 	}
1632f728019bSEric W. Biederman 	kfree(header);
1633f728019bSEric W. Biederman 	header = NULL;
1634f728019bSEric W. Biederman 	goto out;
16356e9d5164SEric W. Biederman }
16366e9d5164SEric W. Biederman 
16371f87f0b5SEric W. Biederman /**
16385b31a7dfSzhouchuangao  * register_sysctl_paths - register a sysctl table hierarchy
16391f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
16401f87f0b5SEric W. Biederman  * @table: the top-level table structure
16411f87f0b5SEric W. Biederman  *
16421f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16431f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16441f87f0b5SEric W. Biederman  *
16451f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
16461f87f0b5SEric W. Biederman  */
16471f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
16481f87f0b5SEric W. Biederman 						struct ctl_table *table)
16491f87f0b5SEric W. Biederman {
165060a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
16511f87f0b5SEric W. Biederman 					path, table);
16521f87f0b5SEric W. Biederman }
16531f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
16541f87f0b5SEric W. Biederman 
16551f87f0b5SEric W. Biederman /**
16561f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
16571f87f0b5SEric W. Biederman  * @table: the top-level table structure
16581f87f0b5SEric W. Biederman  *
16591f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16601f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16611f87f0b5SEric W. Biederman  *
16621f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
16631f87f0b5SEric W. Biederman  */
16641f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
16651f87f0b5SEric W. Biederman {
16661f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
16671f87f0b5SEric W. Biederman 
16681f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
16691f87f0b5SEric W. Biederman }
16701f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
16711f87f0b5SEric W. Biederman 
167251cb8dfcSLuis Chamberlain int __register_sysctl_base(struct ctl_table *base_table)
167351cb8dfcSLuis Chamberlain {
167451cb8dfcSLuis Chamberlain 	struct ctl_table_header *hdr;
167551cb8dfcSLuis Chamberlain 
167651cb8dfcSLuis Chamberlain 	hdr = register_sysctl_table(base_table);
167751cb8dfcSLuis Chamberlain 	kmemleak_not_leak(hdr);
167851cb8dfcSLuis Chamberlain 	return 0;
167951cb8dfcSLuis Chamberlain }
168051cb8dfcSLuis Chamberlain 
16810e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
16820e47c99dSEric W. Biederman {
16830e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
16840e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
16850e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
16860e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
16870e47c99dSEric W. Biederman 	struct ctl_table *entry;
16880e47c99dSEric W. Biederman 
16890e47c99dSEric W. Biederman 	if (header->set == root_set)
16900e47c99dSEric W. Biederman 		return;
16910e47c99dSEric W. Biederman 
16920e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
16930e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
16940e47c99dSEric W. Biederman 		return;
16950e47c99dSEric W. Biederman 
1696cb55f27aSMeng Tang 	list_for_each_table_entry(entry, header->ctl_table) {
16970e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
16980e47c99dSEric W. Biederman 		struct ctl_table *link;
16990e47c99dSEric W. Biederman 		const char *name = entry->procname;
17000e47c99dSEric W. Biederman 
17010e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
17020e47c99dSEric W. Biederman 		if (link &&
17030e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
17040e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
17050e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
17060e47c99dSEric W. Biederman 		}
17070e47c99dSEric W. Biederman 		else {
170887ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
17090e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
1710153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", name);
17110e47c99dSEric W. Biederman 		}
17120e47c99dSEric W. Biederman 	}
17130e47c99dSEric W. Biederman }
17140e47c99dSEric W. Biederman 
1715938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1716938aaa4fSEric W. Biederman {
17177ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
17187ec66d06SEric W. Biederman 
1719938aaa4fSEric W. Biederman 	if (--header->nreg)
1720938aaa4fSEric W. Biederman 		return;
1721938aaa4fSEric W. Biederman 
172289189557SYueHaibing 	if (parent) {
17230e47c99dSEric W. Biederman 		put_links(header);
1724938aaa4fSEric W. Biederman 		start_unregistering(header);
172589189557SYueHaibing 	}
172689189557SYueHaibing 
1727938aaa4fSEric W. Biederman 	if (!--header->count)
1728938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
17297ec66d06SEric W. Biederman 
17307ec66d06SEric W. Biederman 	if (parent)
17317ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1732938aaa4fSEric W. Biederman }
1733938aaa4fSEric W. Biederman 
17341f87f0b5SEric W. Biederman /**
17351f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
17361f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
17371f87f0b5SEric W. Biederman  *
17381f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
17391f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
17401f87f0b5SEric W. Biederman  */
17411f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
17421f87f0b5SEric W. Biederman {
1743f728019bSEric W. Biederman 	int nr_subheaders;
17441f87f0b5SEric W. Biederman 	might_sleep();
17451f87f0b5SEric W. Biederman 
17461f87f0b5SEric W. Biederman 	if (header == NULL)
17471f87f0b5SEric W. Biederman 		return;
17481f87f0b5SEric W. Biederman 
1749f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1750f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1751f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1752f728019bSEric W. Biederman 		int i;
1753f728019bSEric W. Biederman 
1754f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1755f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1756f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1757f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1758f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1759f728019bSEric W. Biederman 			kfree(table);
1760f728019bSEric W. Biederman 		}
1761f728019bSEric W. Biederman 		kfree(header);
1762f728019bSEric W. Biederman 		return;
1763f728019bSEric W. Biederman 	}
1764f728019bSEric W. Biederman 
17651f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1766938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
17671f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
17681f87f0b5SEric W. Biederman }
17691f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
17701f87f0b5SEric W. Biederman 
17710e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
17729eb47c26SEric W. Biederman 	struct ctl_table_root *root,
17731f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
17741f87f0b5SEric W. Biederman {
17751347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
17760e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1777ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
17781f87f0b5SEric W. Biederman }
17791f87f0b5SEric W. Biederman 
178097324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
178197324cd8SEric W. Biederman {
1782ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
178397324cd8SEric W. Biederman }
17841f87f0b5SEric W. Biederman 
17851e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
178677b14db5SEric W. Biederman {
1787e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1788e1675231SAlexey Dobriyan 
178977b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
17909043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
1791d56c0d45SAlexey Dobriyan 	proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
179277b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1793de4e83bdSEric W. Biederman 
1794d8c0418aSLuis Chamberlain 	return sysctl_init_bases();
179577b14db5SEric W. Biederman }
17963db978d4SVlastimil Babka 
17970a477e1aSVlastimil Babka struct sysctl_alias {
17980a477e1aSVlastimil Babka 	const char *kernel_param;
17990a477e1aSVlastimil Babka 	const char *sysctl_param;
18000a477e1aSVlastimil Babka };
18010a477e1aSVlastimil Babka 
18020a477e1aSVlastimil Babka /*
18030a477e1aSVlastimil Babka  * Historically some settings had both sysctl and a command line parameter.
18040a477e1aSVlastimil Babka  * With the generic sysctl. parameter support, we can handle them at a single
18050a477e1aSVlastimil Babka  * place and only keep the historical name for compatibility. This is not meant
18060a477e1aSVlastimil Babka  * to add brand new aliases. When adding existing aliases, consider whether
18070a477e1aSVlastimil Babka  * the possibly different moment of changing the value (e.g. from early_param
18080a477e1aSVlastimil Babka  * to the moment do_sysctl_args() is called) is an issue for the specific
18090a477e1aSVlastimil Babka  * parameter.
18100a477e1aSVlastimil Babka  */
18110a477e1aSVlastimil Babka static const struct sysctl_alias sysctl_aliases[] = {
1812f117955aSGuilherme G. Piccoli 	{"hardlockup_all_cpu_backtrace",	"kernel.hardlockup_all_cpu_backtrace" },
1813b467f3efSVlastimil Babka 	{"hung_task_panic",			"kernel.hung_task_panic" },
1814f117955aSGuilherme G. Piccoli 	{"numa_zonelist_order",			"vm.numa_zonelist_order" },
1815f117955aSGuilherme G. Piccoli 	{"softlockup_all_cpu_backtrace",	"kernel.softlockup_all_cpu_backtrace" },
1816f117955aSGuilherme G. Piccoli 	{"softlockup_panic",			"kernel.softlockup_panic" },
18170a477e1aSVlastimil Babka 	{ }
18180a477e1aSVlastimil Babka };
18190a477e1aSVlastimil Babka 
18200a477e1aSVlastimil Babka static const char *sysctl_find_alias(char *param)
18210a477e1aSVlastimil Babka {
18220a477e1aSVlastimil Babka 	const struct sysctl_alias *alias;
18230a477e1aSVlastimil Babka 
18240a477e1aSVlastimil Babka 	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
18250a477e1aSVlastimil Babka 		if (strcmp(alias->kernel_param, param) == 0)
18260a477e1aSVlastimil Babka 			return alias->sysctl_param;
18270a477e1aSVlastimil Babka 	}
18280a477e1aSVlastimil Babka 
18290a477e1aSVlastimil Babka 	return NULL;
18300a477e1aSVlastimil Babka }
18310a477e1aSVlastimil Babka 
18323db978d4SVlastimil Babka /* Set sysctl value passed on kernel command line. */
18333db978d4SVlastimil Babka static int process_sysctl_arg(char *param, char *val,
18343db978d4SVlastimil Babka 			       const char *unused, void *arg)
18353db978d4SVlastimil Babka {
18363db978d4SVlastimil Babka 	char *path;
18373db978d4SVlastimil Babka 	struct vfsmount **proc_mnt = arg;
18383db978d4SVlastimil Babka 	struct file_system_type *proc_fs_type;
18393db978d4SVlastimil Babka 	struct file *file;
18403db978d4SVlastimil Babka 	int len;
18413db978d4SVlastimil Babka 	int err;
18423db978d4SVlastimil Babka 	loff_t pos = 0;
18433db978d4SVlastimil Babka 	ssize_t wret;
18443db978d4SVlastimil Babka 
18450a477e1aSVlastimil Babka 	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
18463db978d4SVlastimil Babka 		param += sizeof("sysctl") - 1;
18473db978d4SVlastimil Babka 
18483db978d4SVlastimil Babka 		if (param[0] != '/' && param[0] != '.')
18493db978d4SVlastimil Babka 			return 0;
18503db978d4SVlastimil Babka 
18513db978d4SVlastimil Babka 		param++;
18520a477e1aSVlastimil Babka 	} else {
18530a477e1aSVlastimil Babka 		param = (char *) sysctl_find_alias(param);
18540a477e1aSVlastimil Babka 		if (!param)
18550a477e1aSVlastimil Babka 			return 0;
18560a477e1aSVlastimil Babka 	}
18573db978d4SVlastimil Babka 
1858697edcb0SXiaoming Ni 	if (!val)
1859697edcb0SXiaoming Ni 		return -EINVAL;
1860697edcb0SXiaoming Ni 	len = strlen(val);
1861697edcb0SXiaoming Ni 	if (len == 0)
1862697edcb0SXiaoming Ni 		return -EINVAL;
1863697edcb0SXiaoming Ni 
18643db978d4SVlastimil Babka 	/*
18653db978d4SVlastimil Babka 	 * To set sysctl options, we use a temporary mount of proc, look up the
18663db978d4SVlastimil Babka 	 * respective sys/ file and write to it. To avoid mounting it when no
18673db978d4SVlastimil Babka 	 * options were given, we mount it only when the first sysctl option is
18683db978d4SVlastimil Babka 	 * found. Why not a persistent mount? There are problems with a
18693db978d4SVlastimil Babka 	 * persistent mount of proc in that it forces userspace not to use any
18703db978d4SVlastimil Babka 	 * proc mount options.
18713db978d4SVlastimil Babka 	 */
18723db978d4SVlastimil Babka 	if (!*proc_mnt) {
18733db978d4SVlastimil Babka 		proc_fs_type = get_fs_type("proc");
18743db978d4SVlastimil Babka 		if (!proc_fs_type) {
18753db978d4SVlastimil Babka 			pr_err("Failed to find procfs to set sysctl from command line\n");
18763db978d4SVlastimil Babka 			return 0;
18773db978d4SVlastimil Babka 		}
18783db978d4SVlastimil Babka 		*proc_mnt = kern_mount(proc_fs_type);
18793db978d4SVlastimil Babka 		put_filesystem(proc_fs_type);
18803db978d4SVlastimil Babka 		if (IS_ERR(*proc_mnt)) {
18813db978d4SVlastimil Babka 			pr_err("Failed to mount procfs to set sysctl from command line\n");
18823db978d4SVlastimil Babka 			return 0;
18833db978d4SVlastimil Babka 		}
18843db978d4SVlastimil Babka 	}
18853db978d4SVlastimil Babka 
18863db978d4SVlastimil Babka 	path = kasprintf(GFP_KERNEL, "sys/%s", param);
18873db978d4SVlastimil Babka 	if (!path)
18883db978d4SVlastimil Babka 		panic("%s: Failed to allocate path for %s\n", __func__, param);
18893db978d4SVlastimil Babka 	strreplace(path, '.', '/');
18903db978d4SVlastimil Babka 
1891ffb37ca3SAl Viro 	file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
18923db978d4SVlastimil Babka 	if (IS_ERR(file)) {
18933db978d4SVlastimil Babka 		err = PTR_ERR(file);
18943db978d4SVlastimil Babka 		if (err == -ENOENT)
18953db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
18963db978d4SVlastimil Babka 				param, val);
18973db978d4SVlastimil Babka 		else if (err == -EACCES)
18983db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
18993db978d4SVlastimil Babka 				param, val);
19003db978d4SVlastimil Babka 		else
19013db978d4SVlastimil Babka 			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
19023db978d4SVlastimil Babka 				file, param, val);
19033db978d4SVlastimil Babka 		goto out;
19043db978d4SVlastimil Babka 	}
19053db978d4SVlastimil Babka 	wret = kernel_write(file, val, len, &pos);
19063db978d4SVlastimil Babka 	if (wret < 0) {
19073db978d4SVlastimil Babka 		err = wret;
19083db978d4SVlastimil Babka 		if (err == -EINVAL)
19093db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
19103db978d4SVlastimil Babka 				param, val);
19113db978d4SVlastimil Babka 		else
19123db978d4SVlastimil Babka 			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
19133db978d4SVlastimil Babka 				ERR_PTR(err), param, val);
19143db978d4SVlastimil Babka 	} else if (wret != len) {
19153db978d4SVlastimil Babka 		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
19163db978d4SVlastimil Babka 			wret, len, path, param, val);
19173db978d4SVlastimil Babka 	}
19183db978d4SVlastimil Babka 
19193db978d4SVlastimil Babka 	err = filp_close(file, NULL);
19203db978d4SVlastimil Babka 	if (err)
19213db978d4SVlastimil Babka 		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
19223db978d4SVlastimil Babka 			ERR_PTR(err), param, val);
19233db978d4SVlastimil Babka out:
19243db978d4SVlastimil Babka 	kfree(path);
19253db978d4SVlastimil Babka 	return 0;
19263db978d4SVlastimil Babka }
19273db978d4SVlastimil Babka 
19283db978d4SVlastimil Babka void do_sysctl_args(void)
19293db978d4SVlastimil Babka {
19303db978d4SVlastimil Babka 	char *command_line;
19313db978d4SVlastimil Babka 	struct vfsmount *proc_mnt = NULL;
19323db978d4SVlastimil Babka 
19333db978d4SVlastimil Babka 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
19343db978d4SVlastimil Babka 	if (!command_line)
19353db978d4SVlastimil Babka 		panic("%s: Failed to allocate copy of command line\n", __func__);
19363db978d4SVlastimil Babka 
19373db978d4SVlastimil Babka 	parse_args("Setting sysctl args", command_line,
19383db978d4SVlastimil Babka 		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
19393db978d4SVlastimil Babka 
19403db978d4SVlastimil Babka 	if (proc_mnt)
19413db978d4SVlastimil Babka 		kern_unmount(proc_mnt);
19423db978d4SVlastimil Babka 
19433db978d4SVlastimil Babka 	kfree(command_line);
19443db978d4SVlastimil Babka }
1945