xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision cb55f27a)
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 
22*cb55f27aSMeng Tang #define list_for_each_table_entry(entry, table) \
23*cb55f27aSMeng Tang 	for ((entry) = (table); (entry)->procname; (entry)++)
24*cb55f27aSMeng 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 
31eec4844fSMatteo Croce /* shared constants to be used in various sysctls */
3254771613SLuis Chamberlain const int sysctl_vals[] = { -1, 0, 1, 2, 4, 100, 200, 1000, 3000, INT_MAX, 65535 };
33eec4844fSMatteo Croce EXPORT_SYMBOL(sysctl_vals);
34eec4844fSMatteo Croce 
35b1f2aff8SLuis Chamberlain const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
36b1f2aff8SLuis Chamberlain EXPORT_SYMBOL_GPL(sysctl_long_vals);
37b1f2aff8SLuis Chamberlain 
38f9bd6733SEric W. Biederman /* Support for permanently empty directories */
39f9bd6733SEric W. Biederman 
40f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = {
41f9bd6733SEric W. Biederman 	{ }
42f9bd6733SEric W. Biederman };
43f9bd6733SEric W. Biederman 
44ee9efac4SLuis Chamberlain /**
45ee9efac4SLuis Chamberlain  * register_sysctl_mount_point() - registers a sysctl mount point
46ee9efac4SLuis Chamberlain  * @path: path for the mount point
47ee9efac4SLuis Chamberlain  *
48ee9efac4SLuis Chamberlain  * Used to create a permanently empty directory to serve as mount point.
49ee9efac4SLuis Chamberlain  * There are some subtle but important permission checks this allows in the
50ee9efac4SLuis Chamberlain  * case of unprivileged mounts.
51ee9efac4SLuis Chamberlain  */
52ee9efac4SLuis Chamberlain struct ctl_table_header *register_sysctl_mount_point(const char *path)
53ee9efac4SLuis Chamberlain {
54ee9efac4SLuis Chamberlain 	return register_sysctl(path, sysctl_mount_point);
55ee9efac4SLuis Chamberlain }
56ee9efac4SLuis Chamberlain EXPORT_SYMBOL(register_sysctl_mount_point);
57ee9efac4SLuis Chamberlain 
58f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head)
59f9bd6733SEric W. Biederman {
60f9bd6733SEric W. Biederman 	return head->ctl_table[0].child == sysctl_mount_point;
61f9bd6733SEric W. Biederman }
62f9bd6733SEric W. Biederman 
63f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir)
64f9bd6733SEric W. Biederman {
65f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = sysctl_mount_point;
66f9bd6733SEric W. Biederman }
67f9bd6733SEric W. Biederman 
68f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir)
69f9bd6733SEric W. Biederman 
70f9bd6733SEric W. Biederman {
71f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = NULL;
72f9bd6733SEric W. Biederman }
73f9bd6733SEric W. Biederman 
74f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
75f1ecf068SLucas De Marchi {
76f1ecf068SLucas De Marchi 	if (!poll)
77f1ecf068SLucas De Marchi 		return;
78f1ecf068SLucas De Marchi 
79f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
80f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
81f1ecf068SLucas De Marchi }
82f1ecf068SLucas De Marchi 
83a194558eSEric W. Biederman static struct ctl_table root_table[] = {
84a194558eSEric W. Biederman 	{
85a194558eSEric W. Biederman 		.procname = "",
867ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
87a194558eSEric W. Biederman 	},
88a194558eSEric W. Biederman 	{ }
89a194558eSEric W. Biederman };
900e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
910e47c99dSEric W. Biederman 	.default_set.dir.header = {
921f87f0b5SEric W. Biederman 		{{.count = 1,
93938aaa4fSEric W. Biederman 		  .nreg = 1,
94ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
950e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
961f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
971f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
987ec66d06SEric W. Biederman 	},
991f87f0b5SEric W. Biederman };
1001f87f0b5SEric W. Biederman 
1011f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
1021f87f0b5SEric W. Biederman 
1037ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
1040e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
10513bcc6a2SEric W. Biederman 	struct ctl_table **pentry);
1060e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
1070e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
1087ec66d06SEric W. Biederman 
1096980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
1106980128fSEric W. Biederman {
1116980128fSEric W. Biederman 	if (dir->header.parent)
1126980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
11387ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
1146980128fSEric W. Biederman }
1156980128fSEric W. Biederman 
116076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
117076c3eedSEric W. Biederman {
118076c3eedSEric W. Biederman 	int cmp;
119076c3eedSEric W. Biederman 
120d8fc9b66SMasahiro Yamada 	cmp = memcmp(name1, name2, min(len1, len2));
121076c3eedSEric W. Biederman 	if (cmp == 0)
122076c3eedSEric W. Biederman 		cmp = len1 - len2;
123076c3eedSEric W. Biederman 	return cmp;
124076c3eedSEric W. Biederman }
125076c3eedSEric W. Biederman 
12660f126d9SEric W. Biederman /* Called under sysctl_lock */
127076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
1280e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
129076c3eedSEric W. Biederman {
130076c3eedSEric W. Biederman 	struct ctl_table_header *head;
131076c3eedSEric W. Biederman 	struct ctl_table *entry;
132ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
133076c3eedSEric W. Biederman 
134ac13ac6fSEric W. Biederman 	while (node)
135ac13ac6fSEric W. Biederman 	{
136ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
137ac13ac6fSEric W. Biederman 		const char *procname;
138ac13ac6fSEric W. Biederman 		int cmp;
139ac13ac6fSEric W. Biederman 
140ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
141ac13ac6fSEric W. Biederman 		head = ctl_node->header;
142ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
143ac13ac6fSEric W. Biederman 		procname = entry->procname;
144ac13ac6fSEric W. Biederman 
145ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
146ac13ac6fSEric W. Biederman 		if (cmp < 0)
147ac13ac6fSEric W. Biederman 			node = node->rb_left;
148ac13ac6fSEric W. Biederman 		else if (cmp > 0)
149ac13ac6fSEric W. Biederman 			node = node->rb_right;
150ac13ac6fSEric W. Biederman 		else {
151076c3eedSEric W. Biederman 			*phead = head;
152076c3eedSEric W. Biederman 			return entry;
153076c3eedSEric W. Biederman 		}
154076c3eedSEric W. Biederman 	}
155076c3eedSEric W. Biederman 	return NULL;
156076c3eedSEric W. Biederman }
157076c3eedSEric W. Biederman 
158ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
159ac13ac6fSEric W. Biederman {
160ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
161ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
162ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
163ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
164ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
165ac13ac6fSEric W. Biederman 
166ac13ac6fSEric W. Biederman 	while (*p) {
167ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
168ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
169ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
170ac13ac6fSEric W. Biederman 		const char *parent_name;
171ac13ac6fSEric W. Biederman 		int cmp;
172ac13ac6fSEric W. Biederman 
173ac13ac6fSEric W. Biederman 		parent = *p;
174ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
175ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
176ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
177ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
178ac13ac6fSEric W. Biederman 
179ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
180ac13ac6fSEric W. Biederman 		if (cmp < 0)
181ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
182ac13ac6fSEric W. Biederman 		else if (cmp > 0)
183ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
184ac13ac6fSEric W. Biederman 		else {
18587ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
186ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
187153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", entry->procname);
188ac13ac6fSEric W. Biederman 			return -EEXIST;
189ac13ac6fSEric W. Biederman 		}
190ac13ac6fSEric W. Biederman 	}
191ac13ac6fSEric W. Biederman 
192ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
193ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
194ac13ac6fSEric W. Biederman 	return 0;
195ac13ac6fSEric W. Biederman }
196ac13ac6fSEric W. Biederman 
197ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
198ac13ac6fSEric W. Biederman {
199ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
200ac13ac6fSEric W. Biederman 
201ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
202ac13ac6fSEric W. Biederman }
203ac13ac6fSEric W. Biederman 
204e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
205e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
206ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
207e0d04529SEric W. Biederman {
2087ec66d06SEric W. Biederman 	head->ctl_table = table;
209e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
210e0d04529SEric W. Biederman 	head->used = 0;
211e0d04529SEric W. Biederman 	head->count = 1;
212e0d04529SEric W. Biederman 	head->nreg = 1;
213e0d04529SEric W. Biederman 	head->unregistering = NULL;
214e0d04529SEric W. Biederman 	head->root = root;
215e0d04529SEric W. Biederman 	head->set = set;
216e0d04529SEric W. Biederman 	head->parent = NULL;
217ac13ac6fSEric W. Biederman 	head->node = node;
2182fd1d2c4SEric W. Biederman 	INIT_HLIST_HEAD(&head->inodes);
219ac13ac6fSEric W. Biederman 	if (node) {
220ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
221*cb55f27aSMeng Tang 
222*cb55f27aSMeng Tang 		list_for_each_table_entry(entry, table) {
223ac13ac6fSEric W. Biederman 			node->header = head;
224*cb55f27aSMeng Tang 			node++;
225*cb55f27aSMeng Tang 		}
226ac13ac6fSEric W. Biederman 	}
227ac13ac6fSEric W. Biederman }
228e0d04529SEric W. Biederman 
2298425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2308425d6aaSEric W. Biederman {
231ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
232*cb55f27aSMeng Tang 
233*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, head->ctl_table)
234ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2358425d6aaSEric W. Biederman }
2368425d6aaSEric W. Biederman 
2370e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2388425d6aaSEric W. Biederman {
239ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
2400e47c99dSEric W. Biederman 	int err;
2410e47c99dSEric W. Biederman 
242f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
243f9bd6733SEric W. Biederman 	if (is_empty_dir(&dir->header))
244f9bd6733SEric W. Biederman 		return -EROFS;
245f9bd6733SEric W. Biederman 
246f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
247f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point) {
248f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
249f9bd6733SEric W. Biederman 			return -EINVAL;
250f9bd6733SEric W. Biederman 		set_empty_dir(dir);
251f9bd6733SEric W. Biederman 	}
252f9bd6733SEric W. Biederman 
2530e47c99dSEric W. Biederman 	dir->header.nreg++;
2547ec66d06SEric W. Biederman 	header->parent = dir;
2550e47c99dSEric W. Biederman 	err = insert_links(header);
2560e47c99dSEric W. Biederman 	if (err)
2570e47c99dSEric W. Biederman 		goto fail_links;
258*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, header->ctl_table) {
259ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
260ac13ac6fSEric W. Biederman 		if (err)
261ac13ac6fSEric W. Biederman 			goto fail;
262ac13ac6fSEric W. Biederman 	}
2630e47c99dSEric W. Biederman 	return 0;
264ac13ac6fSEric W. Biederman fail:
265ac13ac6fSEric W. Biederman 	erase_header(header);
266ac13ac6fSEric W. Biederman 	put_links(header);
2670e47c99dSEric W. Biederman fail_links:
268f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
269f9bd6733SEric W. Biederman 		clear_empty_dir(dir);
2700e47c99dSEric W. Biederman 	header->parent = NULL;
2710e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2720e47c99dSEric W. Biederman 	return err;
2738425d6aaSEric W. Biederman }
2748425d6aaSEric W. Biederman 
2751f87f0b5SEric W. Biederman /* called under sysctl_lock */
2761f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2771f87f0b5SEric W. Biederman {
2781f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2791f87f0b5SEric W. Biederman 		return 0;
2801f87f0b5SEric W. Biederman 	p->used++;
2811f87f0b5SEric W. Biederman 	return 1;
2821f87f0b5SEric W. Biederman }
2831f87f0b5SEric W. Biederman 
2841f87f0b5SEric W. Biederman /* called under sysctl_lock */
2851f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2861f87f0b5SEric W. Biederman {
2871f87f0b5SEric W. Biederman 	if (!--p->used)
2881f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2891f87f0b5SEric W. Biederman 			complete(p->unregistering);
2901f87f0b5SEric W. Biederman }
2911f87f0b5SEric W. Biederman 
292f90f3cafSEric W. Biederman static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
293d6cffbbeSKonstantin Khlebnikov {
294f90f3cafSEric W. Biederman 	proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
295d6cffbbeSKonstantin Khlebnikov }
296d6cffbbeSKonstantin Khlebnikov 
2971f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
2981f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2991f87f0b5SEric W. Biederman {
3001f87f0b5SEric W. Biederman 	/*
3011f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
3021f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
3031f87f0b5SEric W. Biederman 	 */
3041f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
3051f87f0b5SEric W. Biederman 		struct completion wait;
3061f87f0b5SEric W. Biederman 		init_completion(&wait);
3071f87f0b5SEric W. Biederman 		p->unregistering = &wait;
3081f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
3091f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
3101f87f0b5SEric W. Biederman 	} else {
3111f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
3121f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
313ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
3141f87f0b5SEric W. Biederman 	}
3151f87f0b5SEric W. Biederman 	/*
316f90f3cafSEric W. Biederman 	 * Invalidate dentries for unregistered sysctls: namespaced sysctls
317d6cffbbeSKonstantin Khlebnikov 	 * can have duplicate names and contaminate dcache very badly.
318d6cffbbeSKonstantin Khlebnikov 	 */
319f90f3cafSEric W. Biederman 	proc_sys_invalidate_dcache(p);
320d6cffbbeSKonstantin Khlebnikov 	/*
3211f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
3221f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
3231f87f0b5SEric W. Biederman 	 */
324ace0c791SEric W. Biederman 	spin_lock(&sysctl_lock);
3258425d6aaSEric W. Biederman 	erase_header(p);
3261f87f0b5SEric W. Biederman }
3271f87f0b5SEric W. Biederman 
3281f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3291f87f0b5SEric W. Biederman {
330ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3311f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3321f87f0b5SEric W. Biederman 	if (!use_table(head))
3331f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3341f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3351f87f0b5SEric W. Biederman 	return head;
3361f87f0b5SEric W. Biederman }
3371f87f0b5SEric W. Biederman 
3381f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3391f87f0b5SEric W. Biederman {
3401f87f0b5SEric W. Biederman 	if (!head)
3411f87f0b5SEric W. Biederman 		return;
3421f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3431f87f0b5SEric W. Biederman 	unuse_table(head);
3441f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3451f87f0b5SEric W. Biederman }
3461f87f0b5SEric W. Biederman 
3471f87f0b5SEric W. Biederman static struct ctl_table_set *
34813bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root)
3491f87f0b5SEric W. Biederman {
3501f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3511f87f0b5SEric W. Biederman 	if (root->lookup)
35213bcc6a2SEric W. Biederman 		set = root->lookup(root);
3531f87f0b5SEric W. Biederman 	return set;
3541f87f0b5SEric W. Biederman }
3551f87f0b5SEric W. Biederman 
356076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3577ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
358076c3eedSEric W. Biederman 				      const char *name, int namelen)
359076c3eedSEric W. Biederman {
360076c3eedSEric W. Biederman 	struct ctl_table_header *head;
361076c3eedSEric W. Biederman 	struct ctl_table *entry;
362076c3eedSEric W. Biederman 
363076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3640e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
365076c3eedSEric W. Biederman 	if (entry && use_table(head))
366076c3eedSEric W. Biederman 		*phead = head;
367076c3eedSEric W. Biederman 	else
368076c3eedSEric W. Biederman 		entry = NULL;
369076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
370076c3eedSEric W. Biederman 	return entry;
371076c3eedSEric W. Biederman }
372076c3eedSEric W. Biederman 
373ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3741f87f0b5SEric W. Biederman {
375ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3761f87f0b5SEric W. Biederman 
377ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
378ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
379ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
380ac13ac6fSEric W. Biederman 			return ctl_node;
3811f87f0b5SEric W. Biederman 	}
3821f87f0b5SEric W. Biederman 	return NULL;
3831f87f0b5SEric W. Biederman }
3841f87f0b5SEric W. Biederman 
3857ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3866a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3871f87f0b5SEric W. Biederman {
388ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3897ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
390ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3916a75ce16SEric W. Biederman 
3926a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
393ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3946a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
395ac13ac6fSEric W. Biederman 	if (ctl_node) {
396ac13ac6fSEric W. Biederman 		head = ctl_node->header;
397ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
398ac13ac6fSEric W. Biederman 	}
3996a75ce16SEric W. Biederman 	*phead = head;
4006a75ce16SEric W. Biederman 	*pentry = entry;
4016a75ce16SEric W. Biederman }
4026a75ce16SEric W. Biederman 
4037ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
4046a75ce16SEric W. Biederman {
4056a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
4066a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
407ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
4086a75ce16SEric W. Biederman 
4096a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
4106a75ce16SEric W. Biederman 	unuse_table(head);
411ac13ac6fSEric W. Biederman 
412ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
4136a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
414ac13ac6fSEric W. Biederman 	head = NULL;
415ac13ac6fSEric W. Biederman 	if (ctl_node) {
416ac13ac6fSEric W. Biederman 		head = ctl_node->header;
417ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
4186a75ce16SEric W. Biederman 	}
4196a75ce16SEric W. Biederman 	*phead = head;
4206a75ce16SEric W. Biederman 	*pentry = entry;
4211f87f0b5SEric W. Biederman }
4221f87f0b5SEric W. Biederman 
4231f87f0b5SEric W. Biederman /*
4241f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4251f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4261f87f0b5SEric W. Biederman  */
4271f87f0b5SEric W. Biederman 
4281f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4291f87f0b5SEric W. Biederman {
430091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4311f87f0b5SEric W. Biederman 		mode >>= 6;
432091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4331f87f0b5SEric W. Biederman 		mode >>= 3;
4341f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4351f87f0b5SEric W. Biederman 		return 0;
4361f87f0b5SEric W. Biederman 	return -EACCES;
4371f87f0b5SEric W. Biederman }
4381f87f0b5SEric W. Biederman 
43973f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
4401f87f0b5SEric W. Biederman {
44173f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4421f87f0b5SEric W. Biederman 	int mode;
4431f87f0b5SEric W. Biederman 
4441f87f0b5SEric W. Biederman 	if (root->permissions)
44573f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4461f87f0b5SEric W. Biederman 	else
4471f87f0b5SEric W. Biederman 		mode = table->mode;
4481f87f0b5SEric W. Biederman 
4491f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4501f87f0b5SEric W. Biederman }
4511f87f0b5SEric W. Biederman 
4529043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
4539043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
45477b14db5SEric W. Biederman {
455e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
45677b14db5SEric W. Biederman 	struct inode *inode;
4579043476fSAl Viro 	struct proc_inode *ei;
45877b14db5SEric W. Biederman 
4599043476fSAl Viro 	inode = new_inode(sb);
46077b14db5SEric W. Biederman 	if (!inode)
461ea5751ccSIvan Delalande 		return ERR_PTR(-ENOMEM);
46277b14db5SEric W. Biederman 
46385fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
46485fe4025SChristoph Hellwig 
46577b14db5SEric W. Biederman 	ei = PROC_I(inode);
4669043476fSAl Viro 
467d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
468ace0c791SEric W. Biederman 	if (unlikely(head->unregistering)) {
469ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
470ace0c791SEric W. Biederman 		iput(inode);
471ea5751ccSIvan Delalande 		return ERR_PTR(-ENOENT);
472ace0c791SEric W. Biederman 	}
473ace0c791SEric W. Biederman 	ei->sysctl = head;
474ace0c791SEric W. Biederman 	ei->sysctl_entry = table;
4750afa5ca8SEric W. Biederman 	hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
476d6cffbbeSKonstantin Khlebnikov 	head->count++;
477d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
478d6cffbbeSKonstantin Khlebnikov 
479078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
4809043476fSAl Viro 	inode->i_mode = table->mode;
4817ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4829043476fSAl Viro 		inode->i_mode |= S_IFREG;
48377b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
48477b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4859043476fSAl Viro 	} else {
4869043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4879043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4889043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
489f9bd6733SEric W. Biederman 		if (is_empty_dir(head))
490f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4919043476fSAl Viro 	}
492e79c6a4fSDmitry Torokhov 
493e79c6a4fSDmitry Torokhov 	if (root->set_ownership)
494e79c6a4fSDmitry Torokhov 		root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
4955ec27ec7SRadoslaw Burny 	else {
4965ec27ec7SRadoslaw Burny 		inode->i_uid = GLOBAL_ROOT_UID;
4975ec27ec7SRadoslaw Burny 		inode->i_gid = GLOBAL_ROOT_GID;
4985ec27ec7SRadoslaw Burny 	}
499e79c6a4fSDmitry Torokhov 
50077b14db5SEric W. Biederman 	return inode;
50177b14db5SEric W. Biederman }
50277b14db5SEric W. Biederman 
503d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
504d6cffbbeSKonstantin Khlebnikov {
505d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
5060afa5ca8SEric W. Biederman 	hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
507d6cffbbeSKonstantin Khlebnikov 	if (!--head->count)
508d6cffbbeSKonstantin Khlebnikov 		kfree_rcu(head, rcu);
509d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
510d6cffbbeSKonstantin Khlebnikov }
511d6cffbbeSKonstantin Khlebnikov 
51281324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
51377b14db5SEric W. Biederman {
5143cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
5153cc3e046SEric W. Biederman 	if (!head)
5160e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
5173cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
51877b14db5SEric W. Biederman }
51977b14db5SEric W. Biederman 
52077b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
52100cd8dd3SAl Viro 					unsigned int flags)
52277b14db5SEric W. Biederman {
5239043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
5249043476fSAl Viro 	struct ctl_table_header *h = NULL;
525dc12e909SAl Viro 	const struct qstr *name = &dentry->d_name;
5269043476fSAl Viro 	struct ctl_table *p;
52777b14db5SEric W. Biederman 	struct inode *inode;
5289043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
5297ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
5300e47c99dSEric W. Biederman 	int ret;
53177b14db5SEric W. Biederman 
5329043476fSAl Viro 	if (IS_ERR(head))
5339043476fSAl Viro 		return ERR_CAST(head);
5349043476fSAl Viro 
5357ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
5369043476fSAl Viro 
5377ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
5389043476fSAl Viro 	if (!p)
53977b14db5SEric W. Biederman 		goto out;
54077b14db5SEric W. Biederman 
5414e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
54213bcc6a2SEric W. Biederman 		ret = sysctl_follow_link(&h, &p);
5430e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5440e47c99dSEric W. Biederman 		if (ret)
5450e47c99dSEric W. Biederman 			goto out;
5464e757320SEric W. Biederman 	}
5470e47c99dSEric W. Biederman 
5489043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
549ea5751ccSIvan Delalande 	if (IS_ERR(inode)) {
550ea5751ccSIvan Delalande 		err = ERR_CAST(inode);
55177b14db5SEric W. Biederman 		goto out;
552ea5751ccSIvan Delalande 	}
55377b14db5SEric W. Biederman 
554fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
555888e2b03SAl Viro 	err = d_splice_alias(inode, dentry);
55677b14db5SEric W. Biederman 
55777b14db5SEric W. Biederman out:
5586bf61045SFrancesco Ruggeri 	if (h)
5596bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
56077b14db5SEric W. Biederman 	sysctl_head_finish(head);
56177b14db5SEric W. Biederman 	return err;
56277b14db5SEric W. Biederman }
56377b14db5SEric W. Biederman 
5644bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
5654bd6a735SMatthew Wilcox (Oracle) 		int write)
56677b14db5SEric W. Biederman {
5674bd6a735SMatthew Wilcox (Oracle) 	struct inode *inode = file_inode(iocb->ki_filp);
5689043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5699043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5704bd6a735SMatthew Wilcox (Oracle) 	size_t count = iov_iter_count(iter);
5714bd6a735SMatthew Wilcox (Oracle) 	char *kbuf;
5722a2da53bSDavid Howells 	ssize_t error;
57377b14db5SEric W. Biederman 
5749043476fSAl Viro 	if (IS_ERR(head))
5759043476fSAl Viro 		return PTR_ERR(head);
57677b14db5SEric W. Biederman 
57777b14db5SEric W. Biederman 	/*
57877b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
57977b14db5SEric W. Biederman 	 * and won't be until we finish.
58077b14db5SEric W. Biederman 	 */
58177b14db5SEric W. Biederman 	error = -EPERM;
58273f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
58377b14db5SEric W. Biederman 		goto out;
58477b14db5SEric W. Biederman 
5859043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5869043476fSAl Viro 	error = -EINVAL;
5879043476fSAl Viro 	if (!table->proc_handler)
5889043476fSAl Viro 		goto out;
5899043476fSAl Viro 
590ef9d965bSChristoph Hellwig 	/* don't even try if the size is too large */
591d4d80e69SMatthew Wilcox (Oracle) 	error = -ENOMEM;
592d4d80e69SMatthew Wilcox (Oracle) 	if (count >= KMALLOC_MAX_SIZE)
593d4d80e69SMatthew Wilcox (Oracle) 		goto out;
59445089437SJosef Bacik 	kbuf = kvzalloc(count + 1, GFP_KERNEL);
59532927393SChristoph Hellwig 	if (!kbuf)
59632927393SChristoph Hellwig 		goto out;
5974bd6a735SMatthew Wilcox (Oracle) 
5984bd6a735SMatthew Wilcox (Oracle) 	if (write) {
5994bd6a735SMatthew Wilcox (Oracle) 		error = -EFAULT;
6004bd6a735SMatthew Wilcox (Oracle) 		if (!copy_from_iter_full(kbuf, count, iter))
6014bd6a735SMatthew Wilcox (Oracle) 			goto out_free_buf;
6024bd6a735SMatthew Wilcox (Oracle) 		kbuf[count] = '\0';
6034e63acdfSAndrey Ignatov 	}
6044e63acdfSAndrey Ignatov 
60532927393SChristoph Hellwig 	error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
6064bd6a735SMatthew Wilcox (Oracle) 					   &iocb->ki_pos);
60732927393SChristoph Hellwig 	if (error)
60832927393SChristoph Hellwig 		goto out_free_buf;
60932927393SChristoph Hellwig 
61032927393SChristoph Hellwig 	/* careful: calling conventions are nasty here */
6114bd6a735SMatthew Wilcox (Oracle) 	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
61232927393SChristoph Hellwig 	if (error)
61332927393SChristoph Hellwig 		goto out_free_buf;
61432927393SChristoph Hellwig 
61532927393SChristoph Hellwig 	if (!write) {
61632927393SChristoph Hellwig 		error = -EFAULT;
6174bd6a735SMatthew Wilcox (Oracle) 		if (copy_to_iter(kbuf, count, iter) < count)
61832927393SChristoph Hellwig 			goto out_free_buf;
61932927393SChristoph Hellwig 	}
62032927393SChristoph Hellwig 
6214e63acdfSAndrey Ignatov 	error = count;
62232927393SChristoph Hellwig out_free_buf:
62345089437SJosef Bacik 	kvfree(kbuf);
62477b14db5SEric W. Biederman out:
62577b14db5SEric W. Biederman 	sysctl_head_finish(head);
62677b14db5SEric W. Biederman 
62777b14db5SEric W. Biederman 	return error;
62877b14db5SEric W. Biederman }
62977b14db5SEric W. Biederman 
6304bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
6317708bfb1SPavel Emelyanov {
6324bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 0);
6337708bfb1SPavel Emelyanov }
6347708bfb1SPavel Emelyanov 
6354bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
63677b14db5SEric W. Biederman {
6374bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 1);
63877b14db5SEric W. Biederman }
63977b14db5SEric W. Biederman 
640f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
641f1ecf068SLucas De Marchi {
6424e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
643f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
644f1ecf068SLucas De Marchi 
6454e474a00SLucas De Marchi 	/* sysctl was unregistered */
6464e474a00SLucas De Marchi 	if (IS_ERR(head))
6474e474a00SLucas De Marchi 		return PTR_ERR(head);
6484e474a00SLucas De Marchi 
649f1ecf068SLucas De Marchi 	if (table->poll)
650f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
651f1ecf068SLucas De Marchi 
6524e474a00SLucas De Marchi 	sysctl_head_finish(head);
6534e474a00SLucas De Marchi 
654f1ecf068SLucas De Marchi 	return 0;
655f1ecf068SLucas De Marchi }
656f1ecf068SLucas De Marchi 
657076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
658f1ecf068SLucas De Marchi {
659496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
6604e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
661f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
662076ccb76SAl Viro 	__poll_t ret = DEFAULT_POLLMASK;
6634e474a00SLucas De Marchi 	unsigned long event;
6644e474a00SLucas De Marchi 
6654e474a00SLucas De Marchi 	/* sysctl was unregistered */
6664e474a00SLucas De Marchi 	if (IS_ERR(head))
667a9a08845SLinus Torvalds 		return EPOLLERR | EPOLLHUP;
668f1ecf068SLucas De Marchi 
669f1ecf068SLucas De Marchi 	if (!table->proc_handler)
670f1ecf068SLucas De Marchi 		goto out;
671f1ecf068SLucas De Marchi 
672f1ecf068SLucas De Marchi 	if (!table->poll)
673f1ecf068SLucas De Marchi 		goto out;
674f1ecf068SLucas De Marchi 
6754e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
676f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
677f1ecf068SLucas De Marchi 
678f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
679f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
680a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
681f1ecf068SLucas De Marchi 	}
682f1ecf068SLucas De Marchi 
683f1ecf068SLucas De Marchi out:
6844e474a00SLucas De Marchi 	sysctl_head_finish(head);
6854e474a00SLucas De Marchi 
686f1ecf068SLucas De Marchi 	return ret;
687f1ecf068SLucas De Marchi }
68877b14db5SEric W. Biederman 
689f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
690f0c3b509SAl Viro 				struct dir_context *ctx,
6919043476fSAl Viro 				struct ctl_table_header *head,
6929043476fSAl Viro 				struct ctl_table *table)
69377b14db5SEric W. Biederman {
694f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
69577b14db5SEric W. Biederman 	struct inode *inode;
69677b14db5SEric W. Biederman 	struct qstr qname;
69777b14db5SEric W. Biederman 	ino_t ino = 0;
69877b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
69977b14db5SEric W. Biederman 
70077b14db5SEric W. Biederman 	qname.name = table->procname;
70177b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
7028387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
70377b14db5SEric W. Biederman 
70477b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
70577b14db5SEric W. Biederman 	if (!child) {
70676aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
70776aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
70876aab3abSAl Viro 		if (IS_ERR(child))
70976aab3abSAl Viro 			return false;
71076aab3abSAl Viro 		if (d_in_lookup(child)) {
711888e2b03SAl Viro 			struct dentry *res;
7129043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
713ea5751ccSIvan Delalande 			if (IS_ERR(inode)) {
71476aab3abSAl Viro 				d_lookup_done(child);
7159043476fSAl Viro 				dput(child);
716f0c3b509SAl Viro 				return false;
71776aab3abSAl Viro 			}
718fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
719888e2b03SAl Viro 			res = d_splice_alias(inode, child);
720888e2b03SAl Viro 			d_lookup_done(child);
721888e2b03SAl Viro 			if (unlikely(res)) {
722888e2b03SAl Viro 				if (IS_ERR(res)) {
723888e2b03SAl Viro 					dput(child);
724888e2b03SAl Viro 					return false;
725888e2b03SAl Viro 				}
726888e2b03SAl Viro 				dput(child);
727888e2b03SAl Viro 				child = res;
728888e2b03SAl Viro 			}
72977b14db5SEric W. Biederman 		}
73077b14db5SEric W. Biederman 	}
7312b0143b5SDavid Howells 	inode = d_inode(child);
73277b14db5SEric W. Biederman 	ino  = inode->i_ino;
73377b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
73477b14db5SEric W. Biederman 	dput(child);
735f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
7369043476fSAl Viro }
7379043476fSAl Viro 
738f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
739f0c3b509SAl Viro 				    struct dir_context *ctx,
7400e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
7410e47c99dSEric W. Biederman 				    struct ctl_table *table)
7420e47c99dSEric W. Biederman {
743f0c3b509SAl Viro 	bool ret = true;
744a0b0d1c3SDanilo Krummrich 
7450e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
746a0b0d1c3SDanilo Krummrich 	if (IS_ERR(head))
747a0b0d1c3SDanilo Krummrich 		return false;
7480e47c99dSEric W. Biederman 
7490e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
750835b94e0SDanilo Krummrich 	if (sysctl_follow_link(&head, &table))
7510e47c99dSEric W. Biederman 		goto out;
7520e47c99dSEric W. Biederman 
753f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
7540e47c99dSEric W. Biederman out:
7550e47c99dSEric W. Biederman 	sysctl_head_finish(head);
7560e47c99dSEric W. Biederman 	return ret;
7570e47c99dSEric W. Biederman }
7580e47c99dSEric W. Biederman 
759e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table,
7609043476fSAl Viro 		unsigned long *pos, struct file *file,
761f0c3b509SAl Viro 		struct dir_context *ctx)
7629043476fSAl Viro {
763f0c3b509SAl Viro 	bool res;
7649043476fSAl Viro 
765f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
766f0c3b509SAl Viro 		return true;
7679043476fSAl Viro 
7680e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
769f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
7700e47c99dSEric W. Biederman 	else
771f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
7729043476fSAl Viro 
773f0c3b509SAl Viro 	if (res)
774f0c3b509SAl Viro 		ctx->pos = *pos;
7756a75ce16SEric W. Biederman 
7766a75ce16SEric W. Biederman 	return res;
77777b14db5SEric W. Biederman }
77877b14db5SEric W. Biederman 
779f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
78077b14db5SEric W. Biederman {
781f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7829043476fSAl Viro 	struct ctl_table_header *h = NULL;
7836a75ce16SEric W. Biederman 	struct ctl_table *entry;
7847ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
78577b14db5SEric W. Biederman 	unsigned long pos;
78677b14db5SEric W. Biederman 
7879043476fSAl Viro 	if (IS_ERR(head))
7889043476fSAl Viro 		return PTR_ERR(head);
7899043476fSAl Viro 
7907ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
79177b14db5SEric W. Biederman 
792f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
79393362fa4SZhou Chengming 		goto out;
794f0c3b509SAl Viro 
79577b14db5SEric W. Biederman 	pos = 2;
79677b14db5SEric W. Biederman 
7977ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
798f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7999043476fSAl Viro 			sysctl_head_finish(h);
8009043476fSAl Viro 			break;
80177b14db5SEric W. Biederman 		}
80277b14db5SEric W. Biederman 	}
80393362fa4SZhou Chengming out:
80477b14db5SEric W. Biederman 	sysctl_head_finish(head);
805f0c3b509SAl Viro 	return 0;
80677b14db5SEric W. Biederman }
80777b14db5SEric W. Biederman 
808549c7297SChristian Brauner static int proc_sys_permission(struct user_namespace *mnt_userns,
809549c7297SChristian Brauner 			       struct inode *inode, int mask)
81077b14db5SEric W. Biederman {
81177b14db5SEric W. Biederman 	/*
81277b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
81377b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
81477b14db5SEric W. Biederman 	 */
815f696a365SMiklos Szeredi 	struct ctl_table_header *head;
816f696a365SMiklos Szeredi 	struct ctl_table *table;
81777b14db5SEric W. Biederman 	int error;
81877b14db5SEric W. Biederman 
819f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
820f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
821f696a365SMiklos Szeredi 		return -EACCES;
822f696a365SMiklos Szeredi 
823f696a365SMiklos Szeredi 	head = grab_header(inode);
8249043476fSAl Viro 	if (IS_ERR(head))
8259043476fSAl Viro 		return PTR_ERR(head);
82677b14db5SEric W. Biederman 
827f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
8289043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
8299043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
8309043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
83173f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
8329043476fSAl Viro 
83377b14db5SEric W. Biederman 	sysctl_head_finish(head);
83477b14db5SEric W. Biederman 	return error;
83577b14db5SEric W. Biederman }
83677b14db5SEric W. Biederman 
837549c7297SChristian Brauner static int proc_sys_setattr(struct user_namespace *mnt_userns,
838549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *attr)
83977b14db5SEric W. Biederman {
8402b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
84177b14db5SEric W. Biederman 	int error;
84277b14db5SEric W. Biederman 
84377b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
84477b14db5SEric W. Biederman 		return -EPERM;
84577b14db5SEric W. Biederman 
8462f221d6fSChristian Brauner 	error = setattr_prepare(&init_user_ns, dentry, attr);
8471025774cSChristoph Hellwig 	if (error)
84877b14db5SEric W. Biederman 		return error;
8491025774cSChristoph Hellwig 
8502f221d6fSChristian Brauner 	setattr_copy(&init_user_ns, inode, attr);
8511025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8521025774cSChristoph Hellwig 	return 0;
85377b14db5SEric W. Biederman }
85477b14db5SEric W. Biederman 
855549c7297SChristian Brauner static int proc_sys_getattr(struct user_namespace *mnt_userns,
856549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
857a528d35eSDavid Howells 			    u32 request_mask, unsigned int query_flags)
8589043476fSAl Viro {
859a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
8609043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
8619043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
8629043476fSAl Viro 
8639043476fSAl Viro 	if (IS_ERR(head))
8649043476fSAl Viro 		return PTR_ERR(head);
8659043476fSAl Viro 
8660d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, inode, stat);
8679043476fSAl Viro 	if (table)
8689043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
8699043476fSAl Viro 
8709043476fSAl Viro 	sysctl_head_finish(head);
8719043476fSAl Viro 	return 0;
8729043476fSAl Viro }
8739043476fSAl Viro 
87477b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
875f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
876f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
8774bd6a735SMatthew Wilcox (Oracle) 	.read_iter	= proc_sys_read,
8784bd6a735SMatthew Wilcox (Oracle) 	.write_iter	= proc_sys_write,
8794bd6a735SMatthew Wilcox (Oracle) 	.splice_read	= generic_file_splice_read,
8804bd6a735SMatthew Wilcox (Oracle) 	.splice_write	= iter_file_splice_write,
8816038f373SArnd Bergmann 	.llseek		= default_llseek,
8829043476fSAl Viro };
8839043476fSAl Viro 
8849043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
885887df078SPavel Emelyanov 	.read		= generic_read_dir,
886f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
8873222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
88877b14db5SEric W. Biederman };
88977b14db5SEric W. Biederman 
89003a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8919043476fSAl Viro 	.permission	= proc_sys_permission,
8929043476fSAl Viro 	.setattr	= proc_sys_setattr,
8939043476fSAl Viro 	.getattr	= proc_sys_getattr,
8949043476fSAl Viro };
8959043476fSAl Viro 
8969043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
89777b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
89877b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
89977b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
9009043476fSAl Viro 	.getattr	= proc_sys_getattr,
90177b14db5SEric W. Biederman };
90277b14db5SEric W. Biederman 
9030b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
90477b14db5SEric W. Biederman {
9050b728e19SAl Viro 	if (flags & LOOKUP_RCU)
90634286d66SNick Piggin 		return -ECHILD;
9072b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
9089043476fSAl Viro }
9099043476fSAl Viro 
910fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
9119043476fSAl Viro {
9122b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
9139043476fSAl Viro }
9149043476fSAl Viro 
9151f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
9161f87f0b5SEric W. Biederman {
9171f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
9181f87f0b5SEric W. Biederman 	int res;
9191f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9201f87f0b5SEric W. Biederman 	if (p->unregistering)
9211f87f0b5SEric W. Biederman 		res = 0;
9221f87f0b5SEric W. Biederman 	else if (!set->is_seen)
9231f87f0b5SEric W. Biederman 		res = 1;
9241f87f0b5SEric W. Biederman 	else
9251f87f0b5SEric W. Biederman 		res = set->is_seen(set);
9261f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
9271f87f0b5SEric W. Biederman 	return res;
9281f87f0b5SEric W. Biederman }
9291f87f0b5SEric W. Biederman 
9306fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry,
931621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
9329043476fSAl Viro {
933dfef6dcdSAl Viro 	struct ctl_table_header *head;
934da53be12SLinus Torvalds 	struct inode *inode;
935da53be12SLinus Torvalds 
93631e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
93731e6b01fSNick Piggin 	 * that inode here can be NULL */
938dfef6dcdSAl Viro 	/* AV: can it, indeed? */
9392b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
94031e6b01fSNick Piggin 	if (!inode)
941dfef6dcdSAl Viro 		return 1;
942621e155aSNick Piggin 	if (name->len != len)
9439043476fSAl Viro 		return 1;
944621e155aSNick Piggin 	if (memcmp(name->name, str, len))
9459043476fSAl Viro 		return 1;
946dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
947dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
94877b14db5SEric W. Biederman }
94977b14db5SEric W. Biederman 
950d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
95177b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
9529043476fSAl Viro 	.d_delete	= proc_sys_delete,
9539043476fSAl Viro 	.d_compare	= proc_sys_compare,
95477b14db5SEric W. Biederman };
95577b14db5SEric W. Biederman 
9560e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
9577ec66d06SEric W. Biederman 				   const char *name, int namelen)
9581f87f0b5SEric W. Biederman {
9597ec66d06SEric W. Biederman 	struct ctl_table_header *head;
9607ec66d06SEric W. Biederman 	struct ctl_table *entry;
9611f87f0b5SEric W. Biederman 
9620e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
9637ec66d06SEric W. Biederman 	if (!entry)
9647ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
96551f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
9667ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
96751f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
9681f87f0b5SEric W. Biederman }
9691f87f0b5SEric W. Biederman 
9707ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
9717ec66d06SEric W. Biederman 			       const char *name, int namelen)
9721f87f0b5SEric W. Biederman {
9737ec66d06SEric W. Biederman 	struct ctl_table *table;
9747ec66d06SEric W. Biederman 	struct ctl_dir *new;
975ac13ac6fSEric W. Biederman 	struct ctl_node *node;
9767ec66d06SEric W. Biederman 	char *new_name;
9771f87f0b5SEric W. Biederman 
978ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
979ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
980ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
9817ec66d06SEric W. Biederman 	if (!new)
9827ec66d06SEric W. Biederman 		return NULL;
9837ec66d06SEric W. Biederman 
984ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
985ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
9867ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
9877ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9887ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
9897ec66d06SEric W. Biederman 	table[0].procname = new_name;
9907ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
991ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
9927ec66d06SEric W. Biederman 
9937ec66d06SEric W. Biederman 	return new;
9941f87f0b5SEric W. Biederman }
9951f87f0b5SEric W. Biederman 
99660f126d9SEric W. Biederman /**
99760f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
99860f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
99960f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
100060f126d9SEric W. Biederman  * @namelen: The length of name
100160f126d9SEric W. Biederman  *
100260f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
100360f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
100460f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
100560f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
100660f126d9SEric W. Biederman  * simply dropped.
100760f126d9SEric W. Biederman  */
10080e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
10090e47c99dSEric W. Biederman 				  const char *name, int namelen)
10107ec66d06SEric W. Biederman {
10110e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
10127ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
10130eb97f38SEric W. Biederman 	int err;
10147ec66d06SEric W. Biederman 
10157ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10160e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10177ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10187ec66d06SEric W. Biederman 		goto found;
10197ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10207ec66d06SEric W. Biederman 		goto failed;
10217ec66d06SEric W. Biederman 
10227ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10237ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
10247ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10257ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
10267ec66d06SEric W. Biederman 	if (!new)
10277ec66d06SEric W. Biederman 		goto failed;
10287ec66d06SEric W. Biederman 
102960f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
10300e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10317ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10327ec66d06SEric W. Biederman 		goto found;
10337ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10347ec66d06SEric W. Biederman 		goto failed;
10357ec66d06SEric W. Biederman 
103660f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
10370eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
10380eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
10390eb97f38SEric W. Biederman 	if (err)
10400e47c99dSEric W. Biederman 		goto failed;
10417ec66d06SEric W. Biederman 	subdir = new;
10427ec66d06SEric W. Biederman found:
10437ec66d06SEric W. Biederman 	subdir->header.nreg++;
10447ec66d06SEric W. Biederman failed:
1045a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
104687ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
10476980128fSEric W. Biederman 		sysctl_print_dir(dir);
1048153ee1c4SGeert Uytterhoeven 		pr_cont("%*.*s %ld\n", namelen, namelen, name,
1049153ee1c4SGeert Uytterhoeven 			PTR_ERR(subdir));
10501f87f0b5SEric W. Biederman 	}
10517ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10527ec66d06SEric W. Biederman 	if (new)
10537ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
10547ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10557ec66d06SEric W. Biederman 	return subdir;
10561f87f0b5SEric W. Biederman }
10571f87f0b5SEric W. Biederman 
10580e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
10590e47c99dSEric W. Biederman {
10600e47c99dSEric W. Biederman 	struct ctl_dir *parent;
10610e47c99dSEric W. Biederman 	const char *procname;
10620e47c99dSEric W. Biederman 	if (!dir->header.parent)
10630e47c99dSEric W. Biederman 		return &set->dir;
10640e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
10650e47c99dSEric W. Biederman 	if (IS_ERR(parent))
10660e47c99dSEric W. Biederman 		return parent;
10670e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
10680e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
10690e47c99dSEric W. Biederman }
10700e47c99dSEric W. Biederman 
10710e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
107213bcc6a2SEric W. Biederman 	struct ctl_table **pentry)
10730e47c99dSEric W. Biederman {
10740e47c99dSEric W. Biederman 	struct ctl_table_header *head;
10750e47c99dSEric W. Biederman 	struct ctl_table_root *root;
10760e47c99dSEric W. Biederman 	struct ctl_table_set *set;
10770e47c99dSEric W. Biederman 	struct ctl_table *entry;
10780e47c99dSEric W. Biederman 	struct ctl_dir *dir;
10790e47c99dSEric W. Biederman 	int ret;
10800e47c99dSEric W. Biederman 
10810e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
10820e47c99dSEric W. Biederman 	root = (*pentry)->data;
108313bcc6a2SEric W. Biederman 	set = lookup_header_set(root);
10840e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
10850e47c99dSEric W. Biederman 	if (IS_ERR(dir))
10860e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10870e47c99dSEric W. Biederman 	else {
10880e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10890e47c99dSEric W. Biederman 		head = NULL;
10900e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10910e47c99dSEric W. Biederman 		ret = -ENOENT;
10920e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10930e47c99dSEric W. Biederman 			unuse_table(*phead);
10940e47c99dSEric W. Biederman 			*phead = head;
10950e47c99dSEric W. Biederman 			*pentry = entry;
10960e47c99dSEric W. Biederman 			ret = 0;
10970e47c99dSEric W. Biederman 		}
10980e47c99dSEric W. Biederman 	}
10990e47c99dSEric W. Biederman 
11000e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
11010e47c99dSEric W. Biederman 	return ret;
11020e47c99dSEric W. Biederman }
11030e47c99dSEric W. Biederman 
11047c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
11057c60c48fSEric W. Biederman {
11067c60c48fSEric W. Biederman 	struct va_format vaf;
11077c60c48fSEric W. Biederman 	va_list args;
11087c60c48fSEric W. Biederman 
11097c60c48fSEric W. Biederman 	va_start(args, fmt);
11107c60c48fSEric W. Biederman 	vaf.fmt = fmt;
11117c60c48fSEric W. Biederman 	vaf.va = &args;
11127c60c48fSEric W. Biederman 
111387ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
11147c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
11157c60c48fSEric W. Biederman 
11167c60c48fSEric W. Biederman 	va_end(args);
11177c60c48fSEric W. Biederman 	return -EINVAL;
11187c60c48fSEric W. Biederman }
11197c60c48fSEric W. Biederman 
11204f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table)
11214f2fec00SLuis R. Rodriguez {
11224f2fec00SLuis R. Rodriguez 	int err = 0;
11234f2fec00SLuis R. Rodriguez 
112461d9b56aSLuis R. Rodriguez 	if ((table->proc_handler == proc_douintvec) ||
112561d9b56aSLuis R. Rodriguez 	    (table->proc_handler == proc_douintvec_minmax)) {
11264f2fec00SLuis R. Rodriguez 		if (table->maxlen != sizeof(unsigned int))
112764a11f3dSWaiman Long 			err |= sysctl_err(path, table, "array not allowed");
11284f2fec00SLuis R. Rodriguez 	}
11294f2fec00SLuis R. Rodriguez 
1130cb944413SEric Dumazet 	if (table->proc_handler == proc_dou8vec_minmax) {
1131cb944413SEric Dumazet 		if (table->maxlen != sizeof(u8))
1132cb944413SEric Dumazet 			err |= sysctl_err(path, table, "array not allowed");
1133cb944413SEric Dumazet 	}
1134cb944413SEric Dumazet 
11354f2fec00SLuis R. Rodriguez 	return err;
11364f2fec00SLuis R. Rodriguez }
11374f2fec00SLuis R. Rodriguez 
11387c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
11397c60c48fSEric W. Biederman {
1140*cb55f27aSMeng Tang 	struct ctl_table *entry;
11417c60c48fSEric W. Biederman 	int err = 0;
1142*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1143*cb55f27aSMeng Tang 		if (entry->child)
1144*cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "Not a file");
11457c60c48fSEric W. Biederman 
1146*cb55f27aSMeng Tang 		if ((entry->proc_handler == proc_dostring) ||
1147*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec) ||
1148*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec) ||
1149*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec_minmax) ||
1150*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_minmax) ||
1151*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dou8vec_minmax) ||
1152*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_jiffies) ||
1153*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1154*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1155*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_minmax) ||
1156*cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1157*cb55f27aSMeng Tang 			if (!entry->data)
1158*cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No data");
1159*cb55f27aSMeng Tang 			if (!entry->maxlen)
1160*cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No maxlen");
11614f2fec00SLuis R. Rodriguez 			else
1162*cb55f27aSMeng Tang 				err |= sysctl_check_table_array(path, entry);
11631f87f0b5SEric W. Biederman 		}
1164*cb55f27aSMeng Tang 		if (!entry->proc_handler)
1165*cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "No proc_handler");
11667c60c48fSEric W. Biederman 
1167*cb55f27aSMeng Tang 		if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1168*cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
1169*cb55f27aSMeng Tang 				entry->mode);
11701f87f0b5SEric W. Biederman 	}
11717c60c48fSEric W. Biederman 	return err;
11721f87f0b5SEric W. Biederman }
11731f87f0b5SEric W. Biederman 
11740e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
11750e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
11760e47c99dSEric W. Biederman {
11770e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
11780e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1179ac13ac6fSEric W. Biederman 	struct ctl_node *node;
11800e47c99dSEric W. Biederman 	char *link_name;
11810e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
11820e47c99dSEric W. Biederman 
11830e47c99dSEric W. Biederman 	name_bytes = 0;
11840e47c99dSEric W. Biederman 	nr_entries = 0;
1185*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
11860e47c99dSEric W. Biederman 		nr_entries++;
11870e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
11880e47c99dSEric W. Biederman 	}
11890e47c99dSEric W. Biederman 
11900e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1191ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
11920e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
11930e47c99dSEric W. Biederman 			name_bytes,
11940e47c99dSEric W. Biederman 			GFP_KERNEL);
11950e47c99dSEric W. Biederman 
11960e47c99dSEric W. Biederman 	if (!links)
11970e47c99dSEric W. Biederman 		return NULL;
11980e47c99dSEric W. Biederman 
1199ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1200ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
12010e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
1202*cb55f27aSMeng Tang 	link = link_table;
12030e47c99dSEric W. Biederman 
1204*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12050e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
12060e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
12070e47c99dSEric W. Biederman 		link->procname = link_name;
12080e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
12090e47c99dSEric W. Biederman 		link->data = link_root;
12100e47c99dSEric W. Biederman 		link_name += len;
1211*cb55f27aSMeng Tang 		link++;
12120e47c99dSEric W. Biederman 	}
1213ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
12140e47c99dSEric W. Biederman 	links->nreg = nr_entries;
12150e47c99dSEric W. Biederman 
12160e47c99dSEric W. Biederman 	return links;
12170e47c99dSEric W. Biederman }
12180e47c99dSEric W. Biederman 
12190e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
12200e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
12210e47c99dSEric W. Biederman {
12220e47c99dSEric W. Biederman 	struct ctl_table_header *head;
12230e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
12240e47c99dSEric W. Biederman 
12250e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
1226*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12270e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12280e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12290e47c99dSEric W. Biederman 		if (!link)
12300e47c99dSEric W. Biederman 			return false;
12310e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
12320e47c99dSEric W. Biederman 			continue;
12330e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
12340e47c99dSEric W. Biederman 			continue;
12350e47c99dSEric W. Biederman 		return false;
12360e47c99dSEric W. Biederman 	}
12370e47c99dSEric W. Biederman 
12380e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
1239*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
12400e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12410e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12420e47c99dSEric W. Biederman 		head->nreg++;
12430e47c99dSEric W. Biederman 	}
12440e47c99dSEric W. Biederman 	return true;
12450e47c99dSEric W. Biederman }
12460e47c99dSEric W. Biederman 
12470e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
12480e47c99dSEric W. Biederman {
12490e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
12500e47c99dSEric W. Biederman 	struct ctl_dir *core_parent = NULL;
12510e47c99dSEric W. Biederman 	struct ctl_table_header *links;
12520e47c99dSEric W. Biederman 	int err;
12530e47c99dSEric W. Biederman 
12540e47c99dSEric W. Biederman 	if (head->set == root_set)
12550e47c99dSEric W. Biederman 		return 0;
12560e47c99dSEric W. Biederman 
12570e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
12580e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
12590e47c99dSEric W. Biederman 		return 0;
12600e47c99dSEric W. Biederman 
12610e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
12620e47c99dSEric W. Biederman 		return 0;
12630e47c99dSEric W. Biederman 
12640e47c99dSEric W. Biederman 	core_parent->header.nreg++;
12650e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
12660e47c99dSEric W. Biederman 
12670e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
12680e47c99dSEric W. Biederman 
12690e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
12700e47c99dSEric W. Biederman 	err = -ENOMEM;
12710e47c99dSEric W. Biederman 	if (!links)
12720e47c99dSEric W. Biederman 		goto out;
12730e47c99dSEric W. Biederman 
12740e47c99dSEric W. Biederman 	err = 0;
12750e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
12760e47c99dSEric W. Biederman 		kfree(links);
12770e47c99dSEric W. Biederman 		goto out;
12780e47c99dSEric W. Biederman 	}
12790e47c99dSEric W. Biederman 
12800e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
12810e47c99dSEric W. Biederman 	if (err)
12820e47c99dSEric W. Biederman 		kfree(links);
12830e47c99dSEric W. Biederman out:
12840e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
12850e47c99dSEric W. Biederman 	return err;
12860e47c99dSEric W. Biederman }
12870e47c99dSEric W. Biederman 
12881f87f0b5SEric W. Biederman /**
1289f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
129060a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
12911f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
12921f87f0b5SEric W. Biederman  * @table: the top-level table structure
12931f87f0b5SEric W. Biederman  *
12941f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12951f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12961f87f0b5SEric W. Biederman  *
12971f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
12981f87f0b5SEric W. Biederman  *
12991f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
13001f87f0b5SEric W. Biederman  *            enter a sysctl file
13011f87f0b5SEric W. Biederman  *
13021f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
13031f87f0b5SEric W. Biederman  *
13041f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
13051f87f0b5SEric W. Biederman  *
1306f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
13071f87f0b5SEric W. Biederman  *
1308f728019bSEric W. Biederman  * child - must be %NULL.
13091f87f0b5SEric W. Biederman  *
13101f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
13111f87f0b5SEric W. Biederman  *
13121f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
13131f87f0b5SEric W. Biederman  *
13141f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
13151f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
13161f87f0b5SEric W. Biederman  *
1317f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1318f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
13191f87f0b5SEric W. Biederman  *
13201f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
13211f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
13221f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
13231f87f0b5SEric W. Biederman  *
13241f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
13251f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
13261f87f0b5SEric W. Biederman  *
13271f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
13281f87f0b5SEric W. Biederman  * to the table header on success.
13291f87f0b5SEric W. Biederman  */
13306e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
133160a47a2eSEric W. Biederman 	struct ctl_table_set *set,
13326e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
13331f87f0b5SEric W. Biederman {
133460a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
13351f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
13366e9d5164SEric W. Biederman 	const char *name, *nextname;
13377ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1338ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1339ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1340ac13ac6fSEric W. Biederman 	int nr_entries = 0;
13411f87f0b5SEric W. Biederman 
1342*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table)
1343ac13ac6fSEric W. Biederman 		nr_entries++;
1344ac13ac6fSEric W. Biederman 
1345ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1346ac13ac6fSEric W. Biederman 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
13471f87f0b5SEric W. Biederman 	if (!header)
13481f87f0b5SEric W. Biederman 		return NULL;
13491f87f0b5SEric W. Biederman 
1350ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1351ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
13527c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
13537c60c48fSEric W. Biederman 		goto fail;
13548d6ecfccSEric W. Biederman 
13551f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
13560e47c99dSEric W. Biederman 	dir = &set->dir;
135760f126d9SEric W. Biederman 	/* Reference moved down the diretory tree get_subdir */
13587ec66d06SEric W. Biederman 	dir->header.nreg++;
13597ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
13607ec66d06SEric W. Biederman 
13617ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
13627ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
13637ec66d06SEric W. Biederman 		int namelen;
13647ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
13657ec66d06SEric W. Biederman 		if (nextname) {
13667ec66d06SEric W. Biederman 			namelen = nextname - name;
13677ec66d06SEric W. Biederman 			nextname++;
13687ec66d06SEric W. Biederman 		} else {
13697ec66d06SEric W. Biederman 			namelen = strlen(name);
13707ec66d06SEric W. Biederman 		}
13717ec66d06SEric W. Biederman 		if (namelen == 0)
13721f87f0b5SEric W. Biederman 			continue;
13737ec66d06SEric W. Biederman 
13740e47c99dSEric W. Biederman 		dir = get_subdir(dir, name, namelen);
13757ec66d06SEric W. Biederman 		if (IS_ERR(dir))
13767ec66d06SEric W. Biederman 			goto fail;
13771f87f0b5SEric W. Biederman 	}
13780e47c99dSEric W. Biederman 
13797ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
13800e47c99dSEric W. Biederman 	if (insert_header(dir, header))
13810e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
13820e47c99dSEric W. Biederman 
13837ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13841f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13851f87f0b5SEric W. Biederman 
13861f87f0b5SEric W. Biederman 	return header;
13870e47c99dSEric W. Biederman 
13887ec66d06SEric W. Biederman fail_put_dir_locked:
13897ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13907c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
13917c60c48fSEric W. Biederman fail:
13927c60c48fSEric W. Biederman 	kfree(header);
13937c60c48fSEric W. Biederman 	dump_stack();
13947c60c48fSEric W. Biederman 	return NULL;
13951f87f0b5SEric W. Biederman }
13961f87f0b5SEric W. Biederman 
1397fea478d4SEric W. Biederman /**
1398fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1399fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1400fea478d4SEric W. Biederman  * @table: the table structure
1401fea478d4SEric W. Biederman  *
1402fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1403fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1404fea478d4SEric W. Biederman  *
1405fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1406fea478d4SEric W. Biederman  */
1407fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1408fea478d4SEric W. Biederman {
1409fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1410fea478d4SEric W. Biederman 					path, table);
1411fea478d4SEric W. Biederman }
1412fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1413fea478d4SEric W. Biederman 
14143ddd9a80SXiaoming Ni /**
14153ddd9a80SXiaoming Ni  * __register_sysctl_init() - register sysctl table to path
14163ddd9a80SXiaoming Ni  * @path: path name for sysctl base
14173ddd9a80SXiaoming Ni  * @table: This is the sysctl table that needs to be registered to the path
14183ddd9a80SXiaoming Ni  * @table_name: The name of sysctl table, only used for log printing when
14193ddd9a80SXiaoming Ni  *              registration fails
14203ddd9a80SXiaoming Ni  *
14213ddd9a80SXiaoming Ni  * The sysctl interface is used by userspace to query or modify at runtime
14223ddd9a80SXiaoming Ni  * a predefined value set on a variable. These variables however have default
14233ddd9a80SXiaoming Ni  * values pre-set. Code which depends on these variables will always work even
14243ddd9a80SXiaoming Ni  * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
14253ddd9a80SXiaoming Ni  * ability to query or modify the sysctls dynamically at run time. Chances of
14263ddd9a80SXiaoming Ni  * register_sysctl() failing on init are extremely low, and so for both reasons
14273ddd9a80SXiaoming Ni  * this function does not return any error as it is used by initialization code.
14283ddd9a80SXiaoming Ni  *
14293ddd9a80SXiaoming Ni  * Context: Can only be called after your respective sysctl base path has been
14303ddd9a80SXiaoming Ni  * registered. So for instance, most base directories are registered early on
14313ddd9a80SXiaoming Ni  * init before init levels are processed through proc_sys_init() and
1432d8c0418aSLuis Chamberlain  * sysctl_init_bases().
14333ddd9a80SXiaoming Ni  */
14343ddd9a80SXiaoming Ni void __init __register_sysctl_init(const char *path, struct ctl_table *table,
14353ddd9a80SXiaoming Ni 				 const char *table_name)
14363ddd9a80SXiaoming Ni {
14373ddd9a80SXiaoming Ni 	struct ctl_table_header *hdr = register_sysctl(path, table);
14383ddd9a80SXiaoming Ni 
14393ddd9a80SXiaoming Ni 	if (unlikely(!hdr)) {
14403ddd9a80SXiaoming Ni 		pr_err("failed when register_sysctl %s to %s\n", table_name, path);
14413ddd9a80SXiaoming Ni 		return;
14423ddd9a80SXiaoming Ni 	}
14433ddd9a80SXiaoming Ni 	kmemleak_not_leak(hdr);
14443ddd9a80SXiaoming Ni }
14453ddd9a80SXiaoming Ni 
14466e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
14476e9d5164SEric W. Biederman {
14486e9d5164SEric W. Biederman 	int namelen;
14496e9d5164SEric W. Biederman 	namelen = strlen(name);
14506e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
14516e9d5164SEric W. Biederman 		return NULL;
14526e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
14536e9d5164SEric W. Biederman 	pos[namelen] = '/';
14546e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
14556e9d5164SEric W. Biederman 	pos += namelen + 1;
14566e9d5164SEric W. Biederman 	return pos;
14576e9d5164SEric W. Biederman }
14586e9d5164SEric W. Biederman 
1459f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1460f728019bSEric W. Biederman {
1461f728019bSEric W. Biederman 	int has_files = 0;
1462f728019bSEric W. Biederman 	int nr_subheaders = 0;
1463f728019bSEric W. Biederman 	struct ctl_table *entry;
1464f728019bSEric W. Biederman 
1465f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1466f728019bSEric W. Biederman 	if (!table || !table->procname)
1467f728019bSEric W. Biederman 		return 1;
1468f728019bSEric W. Biederman 
1469*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1470f728019bSEric W. Biederman 		if (entry->child)
1471f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1472f728019bSEric W. Biederman 		else
1473f728019bSEric W. Biederman 			has_files = 1;
1474f728019bSEric W. Biederman 	}
1475f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1476f728019bSEric W. Biederman }
1477f728019bSEric W. Biederman 
1478f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
147960a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1480f728019bSEric W. Biederman 	struct ctl_table *table)
1481f728019bSEric W. Biederman {
1482f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1483f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1484f728019bSEric W. Biederman 	int nr_files = 0;
1485f728019bSEric W. Biederman 	int nr_dirs = 0;
1486f728019bSEric W. Biederman 	int err = -ENOMEM;
1487f728019bSEric W. Biederman 
1488*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1489f728019bSEric W. Biederman 		if (entry->child)
1490f728019bSEric W. Biederman 			nr_dirs++;
1491f728019bSEric W. Biederman 		else
1492f728019bSEric W. Biederman 			nr_files++;
1493f728019bSEric W. Biederman 	}
1494f728019bSEric W. Biederman 
1495f728019bSEric W. Biederman 	files = table;
1496f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1497f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1498f728019bSEric W. Biederman 		struct ctl_table *new;
14996396bb22SKees Cook 		files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
1500f728019bSEric W. Biederman 				GFP_KERNEL);
1501f728019bSEric W. Biederman 		if (!files)
1502f728019bSEric W. Biederman 			goto out;
1503f728019bSEric W. Biederman 
1504f728019bSEric W. Biederman 		ctl_table_arg = files;
1505*cb55f27aSMeng Tang 		new = files;
1506*cb55f27aSMeng Tang 
1507*cb55f27aSMeng Tang 		list_for_each_table_entry(entry, table) {
1508f728019bSEric W. Biederman 			if (entry->child)
1509f728019bSEric W. Biederman 				continue;
1510f728019bSEric W. Biederman 			*new = *entry;
1511f728019bSEric W. Biederman 			new++;
1512f728019bSEric W. Biederman 		}
1513f728019bSEric W. Biederman 	}
1514f728019bSEric W. Biederman 
1515f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1516f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1517f728019bSEric W. Biederman 		struct ctl_table_header *header;
151860a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1519f728019bSEric W. Biederman 		if (!header) {
1520f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1521f728019bSEric W. Biederman 			goto out;
1522f728019bSEric W. Biederman 		}
1523f728019bSEric W. Biederman 
1524f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1525f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1526f728019bSEric W. Biederman 		**subheader = header;
1527f728019bSEric W. Biederman 		(*subheader)++;
1528f728019bSEric W. Biederman 	}
1529f728019bSEric W. Biederman 
1530f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1531*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, table) {
1532f728019bSEric W. Biederman 		char *child_pos;
1533f728019bSEric W. Biederman 
1534f728019bSEric W. Biederman 		if (!entry->child)
1535f728019bSEric W. Biederman 			continue;
1536f728019bSEric W. Biederman 
1537f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1538f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1539f728019bSEric W. Biederman 		if (!child_pos)
1540f728019bSEric W. Biederman 			goto out;
1541f728019bSEric W. Biederman 
1542f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
154360a47a2eSEric W. Biederman 						  set, entry->child);
1544f728019bSEric W. Biederman 		pos[0] = '\0';
1545f728019bSEric W. Biederman 		if (err)
1546f728019bSEric W. Biederman 			goto out;
1547f728019bSEric W. Biederman 	}
1548f728019bSEric W. Biederman 	err = 0;
1549f728019bSEric W. Biederman out:
1550f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1551f728019bSEric W. Biederman 	return err;
1552f728019bSEric W. Biederman }
1553f728019bSEric W. Biederman 
15546e9d5164SEric W. Biederman /**
15556e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
155660a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
15576e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
15586e9d5164SEric W. Biederman  * @table: the top-level table structure
15596e9d5164SEric W. Biederman  *
15606e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15616e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15626e9d5164SEric W. Biederman  *
15636e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
15646e9d5164SEric W. Biederman  */
15656e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
156660a47a2eSEric W. Biederman 	struct ctl_table_set *set,
15676e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
15686e9d5164SEric W. Biederman {
1569ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1570f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1571f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
15726e9d5164SEric W. Biederman 	const struct ctl_path *component;
15736e9d5164SEric W. Biederman 	char *new_path, *pos;
15746e9d5164SEric W. Biederman 
15756e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
15766e9d5164SEric W. Biederman 	if (!new_path)
15776e9d5164SEric W. Biederman 		return NULL;
15786e9d5164SEric W. Biederman 
15796e9d5164SEric W. Biederman 	pos[0] = '\0';
15806e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
15816e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
15826e9d5164SEric W. Biederman 		if (!pos)
15836e9d5164SEric W. Biederman 			goto out;
15846e9d5164SEric W. Biederman 	}
1585ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1586ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1587ec6a5266SEric W. Biederman 		if (!pos)
1588ec6a5266SEric W. Biederman 			goto out;
1589ec6a5266SEric W. Biederman 		table = table->child;
1590ec6a5266SEric W. Biederman 	}
1591f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
159260a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1593ec6a5266SEric W. Biederman 		if (header)
1594ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1595f728019bSEric W. Biederman 	} else {
1596f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1597f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1598f728019bSEric W. Biederman 		if (!header)
1599f728019bSEric W. Biederman 			goto out;
1600f728019bSEric W. Biederman 
1601f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1602f728019bSEric W. Biederman 		subheader = subheaders;
1603f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1604f728019bSEric W. Biederman 
1605f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
160660a47a2eSEric W. Biederman 						set, table))
1607f728019bSEric W. Biederman 			goto err_register_leaves;
1608f728019bSEric W. Biederman 	}
1609f728019bSEric W. Biederman 
16106e9d5164SEric W. Biederman out:
16116e9d5164SEric W. Biederman 	kfree(new_path);
16126e9d5164SEric W. Biederman 	return header;
1613f728019bSEric W. Biederman 
1614f728019bSEric W. Biederman err_register_leaves:
1615f728019bSEric W. Biederman 	while (subheader > subheaders) {
1616f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1617f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1618f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1619f728019bSEric W. Biederman 		kfree(table);
1620f728019bSEric W. Biederman 	}
1621f728019bSEric W. Biederman 	kfree(header);
1622f728019bSEric W. Biederman 	header = NULL;
1623f728019bSEric W. Biederman 	goto out;
16246e9d5164SEric W. Biederman }
16256e9d5164SEric W. Biederman 
16261f87f0b5SEric W. Biederman /**
16275b31a7dfSzhouchuangao  * register_sysctl_paths - register a sysctl table hierarchy
16281f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
16291f87f0b5SEric W. Biederman  * @table: the top-level table structure
16301f87f0b5SEric W. Biederman  *
16311f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16321f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16331f87f0b5SEric W. Biederman  *
16341f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
16351f87f0b5SEric W. Biederman  */
16361f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
16371f87f0b5SEric W. Biederman 						struct ctl_table *table)
16381f87f0b5SEric W. Biederman {
163960a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
16401f87f0b5SEric W. Biederman 					path, table);
16411f87f0b5SEric W. Biederman }
16421f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
16431f87f0b5SEric W. Biederman 
16441f87f0b5SEric W. Biederman /**
16451f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
16461f87f0b5SEric W. Biederman  * @table: the top-level table structure
16471f87f0b5SEric W. Biederman  *
16481f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16491f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16501f87f0b5SEric W. Biederman  *
16511f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
16521f87f0b5SEric W. Biederman  */
16531f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
16541f87f0b5SEric W. Biederman {
16551f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
16561f87f0b5SEric W. Biederman 
16571f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
16581f87f0b5SEric W. Biederman }
16591f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
16601f87f0b5SEric W. Biederman 
166151cb8dfcSLuis Chamberlain int __register_sysctl_base(struct ctl_table *base_table)
166251cb8dfcSLuis Chamberlain {
166351cb8dfcSLuis Chamberlain 	struct ctl_table_header *hdr;
166451cb8dfcSLuis Chamberlain 
166551cb8dfcSLuis Chamberlain 	hdr = register_sysctl_table(base_table);
166651cb8dfcSLuis Chamberlain 	kmemleak_not_leak(hdr);
166751cb8dfcSLuis Chamberlain 	return 0;
166851cb8dfcSLuis Chamberlain }
166951cb8dfcSLuis Chamberlain 
16700e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
16710e47c99dSEric W. Biederman {
16720e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
16730e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
16740e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
16750e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
16760e47c99dSEric W. Biederman 	struct ctl_table *entry;
16770e47c99dSEric W. Biederman 
16780e47c99dSEric W. Biederman 	if (header->set == root_set)
16790e47c99dSEric W. Biederman 		return;
16800e47c99dSEric W. Biederman 
16810e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
16820e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
16830e47c99dSEric W. Biederman 		return;
16840e47c99dSEric W. Biederman 
1685*cb55f27aSMeng Tang 	list_for_each_table_entry(entry, header->ctl_table) {
16860e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
16870e47c99dSEric W. Biederman 		struct ctl_table *link;
16880e47c99dSEric W. Biederman 		const char *name = entry->procname;
16890e47c99dSEric W. Biederman 
16900e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
16910e47c99dSEric W. Biederman 		if (link &&
16920e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
16930e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
16940e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
16950e47c99dSEric W. Biederman 		}
16960e47c99dSEric W. Biederman 		else {
169787ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
16980e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
1699153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", name);
17000e47c99dSEric W. Biederman 		}
17010e47c99dSEric W. Biederman 	}
17020e47c99dSEric W. Biederman }
17030e47c99dSEric W. Biederman 
1704938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1705938aaa4fSEric W. Biederman {
17067ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
17077ec66d06SEric W. Biederman 
1708938aaa4fSEric W. Biederman 	if (--header->nreg)
1709938aaa4fSEric W. Biederman 		return;
1710938aaa4fSEric W. Biederman 
171189189557SYueHaibing 	if (parent) {
17120e47c99dSEric W. Biederman 		put_links(header);
1713938aaa4fSEric W. Biederman 		start_unregistering(header);
171489189557SYueHaibing 	}
171589189557SYueHaibing 
1716938aaa4fSEric W. Biederman 	if (!--header->count)
1717938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
17187ec66d06SEric W. Biederman 
17197ec66d06SEric W. Biederman 	if (parent)
17207ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1721938aaa4fSEric W. Biederman }
1722938aaa4fSEric W. Biederman 
17231f87f0b5SEric W. Biederman /**
17241f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
17251f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
17261f87f0b5SEric W. Biederman  *
17271f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
17281f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
17291f87f0b5SEric W. Biederman  */
17301f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
17311f87f0b5SEric W. Biederman {
1732f728019bSEric W. Biederman 	int nr_subheaders;
17331f87f0b5SEric W. Biederman 	might_sleep();
17341f87f0b5SEric W. Biederman 
17351f87f0b5SEric W. Biederman 	if (header == NULL)
17361f87f0b5SEric W. Biederman 		return;
17371f87f0b5SEric W. Biederman 
1738f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1739f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1740f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1741f728019bSEric W. Biederman 		int i;
1742f728019bSEric W. Biederman 
1743f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1744f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1745f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1746f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1747f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1748f728019bSEric W. Biederman 			kfree(table);
1749f728019bSEric W. Biederman 		}
1750f728019bSEric W. Biederman 		kfree(header);
1751f728019bSEric W. Biederman 		return;
1752f728019bSEric W. Biederman 	}
1753f728019bSEric W. Biederman 
17541f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1755938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
17561f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
17571f87f0b5SEric W. Biederman }
17581f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
17591f87f0b5SEric W. Biederman 
17600e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
17619eb47c26SEric W. Biederman 	struct ctl_table_root *root,
17621f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
17631f87f0b5SEric W. Biederman {
17641347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
17650e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1766ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
17671f87f0b5SEric W. Biederman }
17681f87f0b5SEric W. Biederman 
176997324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
177097324cd8SEric W. Biederman {
1771ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
177297324cd8SEric W. Biederman }
17731f87f0b5SEric W. Biederman 
17741e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
177577b14db5SEric W. Biederman {
1776e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1777e1675231SAlexey Dobriyan 
177877b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
17799043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
1780d56c0d45SAlexey Dobriyan 	proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
178177b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1782de4e83bdSEric W. Biederman 
1783d8c0418aSLuis Chamberlain 	return sysctl_init_bases();
178477b14db5SEric W. Biederman }
17853db978d4SVlastimil Babka 
17860a477e1aSVlastimil Babka struct sysctl_alias {
17870a477e1aSVlastimil Babka 	const char *kernel_param;
17880a477e1aSVlastimil Babka 	const char *sysctl_param;
17890a477e1aSVlastimil Babka };
17900a477e1aSVlastimil Babka 
17910a477e1aSVlastimil Babka /*
17920a477e1aSVlastimil Babka  * Historically some settings had both sysctl and a command line parameter.
17930a477e1aSVlastimil Babka  * With the generic sysctl. parameter support, we can handle them at a single
17940a477e1aSVlastimil Babka  * place and only keep the historical name for compatibility. This is not meant
17950a477e1aSVlastimil Babka  * to add brand new aliases. When adding existing aliases, consider whether
17960a477e1aSVlastimil Babka  * the possibly different moment of changing the value (e.g. from early_param
17970a477e1aSVlastimil Babka  * to the moment do_sysctl_args() is called) is an issue for the specific
17980a477e1aSVlastimil Babka  * parameter.
17990a477e1aSVlastimil Babka  */
18000a477e1aSVlastimil Babka static const struct sysctl_alias sysctl_aliases[] = {
1801f117955aSGuilherme G. Piccoli 	{"hardlockup_all_cpu_backtrace",	"kernel.hardlockup_all_cpu_backtrace" },
1802b467f3efSVlastimil Babka 	{"hung_task_panic",			"kernel.hung_task_panic" },
1803f117955aSGuilherme G. Piccoli 	{"numa_zonelist_order",			"vm.numa_zonelist_order" },
1804f117955aSGuilherme G. Piccoli 	{"softlockup_all_cpu_backtrace",	"kernel.softlockup_all_cpu_backtrace" },
1805f117955aSGuilherme G. Piccoli 	{"softlockup_panic",			"kernel.softlockup_panic" },
18060a477e1aSVlastimil Babka 	{ }
18070a477e1aSVlastimil Babka };
18080a477e1aSVlastimil Babka 
18090a477e1aSVlastimil Babka static const char *sysctl_find_alias(char *param)
18100a477e1aSVlastimil Babka {
18110a477e1aSVlastimil Babka 	const struct sysctl_alias *alias;
18120a477e1aSVlastimil Babka 
18130a477e1aSVlastimil Babka 	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
18140a477e1aSVlastimil Babka 		if (strcmp(alias->kernel_param, param) == 0)
18150a477e1aSVlastimil Babka 			return alias->sysctl_param;
18160a477e1aSVlastimil Babka 	}
18170a477e1aSVlastimil Babka 
18180a477e1aSVlastimil Babka 	return NULL;
18190a477e1aSVlastimil Babka }
18200a477e1aSVlastimil Babka 
18213db978d4SVlastimil Babka /* Set sysctl value passed on kernel command line. */
18223db978d4SVlastimil Babka static int process_sysctl_arg(char *param, char *val,
18233db978d4SVlastimil Babka 			       const char *unused, void *arg)
18243db978d4SVlastimil Babka {
18253db978d4SVlastimil Babka 	char *path;
18263db978d4SVlastimil Babka 	struct vfsmount **proc_mnt = arg;
18273db978d4SVlastimil Babka 	struct file_system_type *proc_fs_type;
18283db978d4SVlastimil Babka 	struct file *file;
18293db978d4SVlastimil Babka 	int len;
18303db978d4SVlastimil Babka 	int err;
18313db978d4SVlastimil Babka 	loff_t pos = 0;
18323db978d4SVlastimil Babka 	ssize_t wret;
18333db978d4SVlastimil Babka 
18340a477e1aSVlastimil Babka 	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
18353db978d4SVlastimil Babka 		param += sizeof("sysctl") - 1;
18363db978d4SVlastimil Babka 
18373db978d4SVlastimil Babka 		if (param[0] != '/' && param[0] != '.')
18383db978d4SVlastimil Babka 			return 0;
18393db978d4SVlastimil Babka 
18403db978d4SVlastimil Babka 		param++;
18410a477e1aSVlastimil Babka 	} else {
18420a477e1aSVlastimil Babka 		param = (char *) sysctl_find_alias(param);
18430a477e1aSVlastimil Babka 		if (!param)
18440a477e1aSVlastimil Babka 			return 0;
18450a477e1aSVlastimil Babka 	}
18463db978d4SVlastimil Babka 
1847697edcb0SXiaoming Ni 	if (!val)
1848697edcb0SXiaoming Ni 		return -EINVAL;
1849697edcb0SXiaoming Ni 	len = strlen(val);
1850697edcb0SXiaoming Ni 	if (len == 0)
1851697edcb0SXiaoming Ni 		return -EINVAL;
1852697edcb0SXiaoming Ni 
18533db978d4SVlastimil Babka 	/*
18543db978d4SVlastimil Babka 	 * To set sysctl options, we use a temporary mount of proc, look up the
18553db978d4SVlastimil Babka 	 * respective sys/ file and write to it. To avoid mounting it when no
18563db978d4SVlastimil Babka 	 * options were given, we mount it only when the first sysctl option is
18573db978d4SVlastimil Babka 	 * found. Why not a persistent mount? There are problems with a
18583db978d4SVlastimil Babka 	 * persistent mount of proc in that it forces userspace not to use any
18593db978d4SVlastimil Babka 	 * proc mount options.
18603db978d4SVlastimil Babka 	 */
18613db978d4SVlastimil Babka 	if (!*proc_mnt) {
18623db978d4SVlastimil Babka 		proc_fs_type = get_fs_type("proc");
18633db978d4SVlastimil Babka 		if (!proc_fs_type) {
18643db978d4SVlastimil Babka 			pr_err("Failed to find procfs to set sysctl from command line\n");
18653db978d4SVlastimil Babka 			return 0;
18663db978d4SVlastimil Babka 		}
18673db978d4SVlastimil Babka 		*proc_mnt = kern_mount(proc_fs_type);
18683db978d4SVlastimil Babka 		put_filesystem(proc_fs_type);
18693db978d4SVlastimil Babka 		if (IS_ERR(*proc_mnt)) {
18703db978d4SVlastimil Babka 			pr_err("Failed to mount procfs to set sysctl from command line\n");
18713db978d4SVlastimil Babka 			return 0;
18723db978d4SVlastimil Babka 		}
18733db978d4SVlastimil Babka 	}
18743db978d4SVlastimil Babka 
18753db978d4SVlastimil Babka 	path = kasprintf(GFP_KERNEL, "sys/%s", param);
18763db978d4SVlastimil Babka 	if (!path)
18773db978d4SVlastimil Babka 		panic("%s: Failed to allocate path for %s\n", __func__, param);
18783db978d4SVlastimil Babka 	strreplace(path, '.', '/');
18793db978d4SVlastimil Babka 
1880ffb37ca3SAl Viro 	file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
18813db978d4SVlastimil Babka 	if (IS_ERR(file)) {
18823db978d4SVlastimil Babka 		err = PTR_ERR(file);
18833db978d4SVlastimil Babka 		if (err == -ENOENT)
18843db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
18853db978d4SVlastimil Babka 				param, val);
18863db978d4SVlastimil Babka 		else if (err == -EACCES)
18873db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
18883db978d4SVlastimil Babka 				param, val);
18893db978d4SVlastimil Babka 		else
18903db978d4SVlastimil Babka 			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
18913db978d4SVlastimil Babka 				file, param, val);
18923db978d4SVlastimil Babka 		goto out;
18933db978d4SVlastimil Babka 	}
18943db978d4SVlastimil Babka 	wret = kernel_write(file, val, len, &pos);
18953db978d4SVlastimil Babka 	if (wret < 0) {
18963db978d4SVlastimil Babka 		err = wret;
18973db978d4SVlastimil Babka 		if (err == -EINVAL)
18983db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
18993db978d4SVlastimil Babka 				param, val);
19003db978d4SVlastimil Babka 		else
19013db978d4SVlastimil Babka 			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
19023db978d4SVlastimil Babka 				ERR_PTR(err), param, val);
19033db978d4SVlastimil Babka 	} else if (wret != len) {
19043db978d4SVlastimil Babka 		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
19053db978d4SVlastimil Babka 			wret, len, path, param, val);
19063db978d4SVlastimil Babka 	}
19073db978d4SVlastimil Babka 
19083db978d4SVlastimil Babka 	err = filp_close(file, NULL);
19093db978d4SVlastimil Babka 	if (err)
19103db978d4SVlastimil Babka 		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
19113db978d4SVlastimil Babka 			ERR_PTR(err), param, val);
19123db978d4SVlastimil Babka out:
19133db978d4SVlastimil Babka 	kfree(path);
19143db978d4SVlastimil Babka 	return 0;
19153db978d4SVlastimil Babka }
19163db978d4SVlastimil Babka 
19173db978d4SVlastimil Babka void do_sysctl_args(void)
19183db978d4SVlastimil Babka {
19193db978d4SVlastimil Babka 	char *command_line;
19203db978d4SVlastimil Babka 	struct vfsmount *proc_mnt = NULL;
19213db978d4SVlastimil Babka 
19223db978d4SVlastimil Babka 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
19233db978d4SVlastimil Babka 	if (!command_line)
19243db978d4SVlastimil Babka 		panic("%s: Failed to allocate copy of command line\n", __func__);
19253db978d4SVlastimil Babka 
19263db978d4SVlastimil Babka 	parse_args("Setting sysctl args", command_line,
19273db978d4SVlastimil Babka 		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
19283db978d4SVlastimil Babka 
19293db978d4SVlastimil Babka 	if (proc_mnt)
19303db978d4SVlastimil Babka 		kern_unmount(proc_mnt);
19313db978d4SVlastimil Babka 
19323db978d4SVlastimil Babka 	kfree(command_line);
19333db978d4SVlastimil Babka }
1934