xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 3ddd9a80)
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>
19*3ddd9a80SXiaoming Ni #include <linux/kmemleak.h>
2077b14db5SEric W. Biederman #include "internal.h"
2177b14db5SEric W. Biederman 
22d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
2377b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
2403a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
259043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
269043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
2777b14db5SEric W. Biederman 
28eec4844fSMatteo Croce /* shared constants to be used in various sysctls */
29eec4844fSMatteo Croce const int sysctl_vals[] = { 0, 1, INT_MAX };
30eec4844fSMatteo Croce EXPORT_SYMBOL(sysctl_vals);
31eec4844fSMatteo Croce 
32f9bd6733SEric W. Biederman /* Support for permanently empty directories */
33f9bd6733SEric W. Biederman 
34f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = {
35f9bd6733SEric W. Biederman 	{ }
36f9bd6733SEric W. Biederman };
37f9bd6733SEric W. Biederman 
38f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head)
39f9bd6733SEric W. Biederman {
40f9bd6733SEric W. Biederman 	return head->ctl_table[0].child == sysctl_mount_point;
41f9bd6733SEric W. Biederman }
42f9bd6733SEric W. Biederman 
43f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir)
44f9bd6733SEric W. Biederman {
45f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = sysctl_mount_point;
46f9bd6733SEric W. Biederman }
47f9bd6733SEric W. Biederman 
48f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir)
49f9bd6733SEric W. Biederman 
50f9bd6733SEric W. Biederman {
51f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = NULL;
52f9bd6733SEric W. Biederman }
53f9bd6733SEric W. Biederman 
54f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
55f1ecf068SLucas De Marchi {
56f1ecf068SLucas De Marchi 	if (!poll)
57f1ecf068SLucas De Marchi 		return;
58f1ecf068SLucas De Marchi 
59f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
60f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
61f1ecf068SLucas De Marchi }
62f1ecf068SLucas De Marchi 
63a194558eSEric W. Biederman static struct ctl_table root_table[] = {
64a194558eSEric W. Biederman 	{
65a194558eSEric W. Biederman 		.procname = "",
667ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
67a194558eSEric W. Biederman 	},
68a194558eSEric W. Biederman 	{ }
69a194558eSEric W. Biederman };
700e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
710e47c99dSEric W. Biederman 	.default_set.dir.header = {
721f87f0b5SEric W. Biederman 		{{.count = 1,
73938aaa4fSEric W. Biederman 		  .nreg = 1,
74ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
750e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
761f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
771f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
787ec66d06SEric W. Biederman 	},
791f87f0b5SEric W. Biederman };
801f87f0b5SEric W. Biederman 
811f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
821f87f0b5SEric W. Biederman 
837ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
840e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
8513bcc6a2SEric W. Biederman 	struct ctl_table **pentry);
860e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
870e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
887ec66d06SEric W. Biederman 
896980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
906980128fSEric W. Biederman {
916980128fSEric W. Biederman 	if (dir->header.parent)
926980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
9387ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
946980128fSEric W. Biederman }
956980128fSEric W. Biederman 
96076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
97076c3eedSEric W. Biederman {
98076c3eedSEric W. Biederman 	int cmp;
99076c3eedSEric W. Biederman 
100d8fc9b66SMasahiro Yamada 	cmp = memcmp(name1, name2, min(len1, len2));
101076c3eedSEric W. Biederman 	if (cmp == 0)
102076c3eedSEric W. Biederman 		cmp = len1 - len2;
103076c3eedSEric W. Biederman 	return cmp;
104076c3eedSEric W. Biederman }
105076c3eedSEric W. Biederman 
10660f126d9SEric W. Biederman /* Called under sysctl_lock */
107076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
1080e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
109076c3eedSEric W. Biederman {
110076c3eedSEric W. Biederman 	struct ctl_table_header *head;
111076c3eedSEric W. Biederman 	struct ctl_table *entry;
112ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
113076c3eedSEric W. Biederman 
114ac13ac6fSEric W. Biederman 	while (node)
115ac13ac6fSEric W. Biederman 	{
116ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
117ac13ac6fSEric W. Biederman 		const char *procname;
118ac13ac6fSEric W. Biederman 		int cmp;
119ac13ac6fSEric W. Biederman 
120ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
121ac13ac6fSEric W. Biederman 		head = ctl_node->header;
122ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
123ac13ac6fSEric W. Biederman 		procname = entry->procname;
124ac13ac6fSEric W. Biederman 
125ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
126ac13ac6fSEric W. Biederman 		if (cmp < 0)
127ac13ac6fSEric W. Biederman 			node = node->rb_left;
128ac13ac6fSEric W. Biederman 		else if (cmp > 0)
129ac13ac6fSEric W. Biederman 			node = node->rb_right;
130ac13ac6fSEric W. Biederman 		else {
131076c3eedSEric W. Biederman 			*phead = head;
132076c3eedSEric W. Biederman 			return entry;
133076c3eedSEric W. Biederman 		}
134076c3eedSEric W. Biederman 	}
135076c3eedSEric W. Biederman 	return NULL;
136076c3eedSEric W. Biederman }
137076c3eedSEric W. Biederman 
138ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
139ac13ac6fSEric W. Biederman {
140ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
141ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
142ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
143ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
144ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
145ac13ac6fSEric W. Biederman 
146ac13ac6fSEric W. Biederman 	while (*p) {
147ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
148ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
149ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
150ac13ac6fSEric W. Biederman 		const char *parent_name;
151ac13ac6fSEric W. Biederman 		int cmp;
152ac13ac6fSEric W. Biederman 
153ac13ac6fSEric W. Biederman 		parent = *p;
154ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
155ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
156ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
157ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
158ac13ac6fSEric W. Biederman 
159ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
160ac13ac6fSEric W. Biederman 		if (cmp < 0)
161ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
162ac13ac6fSEric W. Biederman 		else if (cmp > 0)
163ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
164ac13ac6fSEric W. Biederman 		else {
16587ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
166ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
167153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", entry->procname);
168ac13ac6fSEric W. Biederman 			return -EEXIST;
169ac13ac6fSEric W. Biederman 		}
170ac13ac6fSEric W. Biederman 	}
171ac13ac6fSEric W. Biederman 
172ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
173ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
174ac13ac6fSEric W. Biederman 	return 0;
175ac13ac6fSEric W. Biederman }
176ac13ac6fSEric W. Biederman 
177ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
178ac13ac6fSEric W. Biederman {
179ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
180ac13ac6fSEric W. Biederman 
181ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
182ac13ac6fSEric W. Biederman }
183ac13ac6fSEric W. Biederman 
184e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
185e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
186ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
187e0d04529SEric W. Biederman {
1887ec66d06SEric W. Biederman 	head->ctl_table = table;
189e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
190e0d04529SEric W. Biederman 	head->used = 0;
191e0d04529SEric W. Biederman 	head->count = 1;
192e0d04529SEric W. Biederman 	head->nreg = 1;
193e0d04529SEric W. Biederman 	head->unregistering = NULL;
194e0d04529SEric W. Biederman 	head->root = root;
195e0d04529SEric W. Biederman 	head->set = set;
196e0d04529SEric W. Biederman 	head->parent = NULL;
197ac13ac6fSEric W. Biederman 	head->node = node;
1982fd1d2c4SEric W. Biederman 	INIT_HLIST_HEAD(&head->inodes);
199ac13ac6fSEric W. Biederman 	if (node) {
200ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
2014c199a93SMichel Lespinasse 		for (entry = table; entry->procname; entry++, node++)
202ac13ac6fSEric W. Biederman 			node->header = head;
203ac13ac6fSEric W. Biederman 	}
204ac13ac6fSEric W. Biederman }
205e0d04529SEric W. Biederman 
2068425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2078425d6aaSEric W. Biederman {
208ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
209ac13ac6fSEric W. Biederman 	for (entry = head->ctl_table; entry->procname; entry++)
210ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2118425d6aaSEric W. Biederman }
2128425d6aaSEric W. Biederman 
2130e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2148425d6aaSEric W. Biederman {
215ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
2160e47c99dSEric W. Biederman 	int err;
2170e47c99dSEric W. Biederman 
218f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
219f9bd6733SEric W. Biederman 	if (is_empty_dir(&dir->header))
220f9bd6733SEric W. Biederman 		return -EROFS;
221f9bd6733SEric W. Biederman 
222f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
223f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point) {
224f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
225f9bd6733SEric W. Biederman 			return -EINVAL;
226f9bd6733SEric W. Biederman 		set_empty_dir(dir);
227f9bd6733SEric W. Biederman 	}
228f9bd6733SEric W. Biederman 
2290e47c99dSEric W. Biederman 	dir->header.nreg++;
2307ec66d06SEric W. Biederman 	header->parent = dir;
2310e47c99dSEric W. Biederman 	err = insert_links(header);
2320e47c99dSEric W. Biederman 	if (err)
2330e47c99dSEric W. Biederman 		goto fail_links;
234ac13ac6fSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
235ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
236ac13ac6fSEric W. Biederman 		if (err)
237ac13ac6fSEric W. Biederman 			goto fail;
238ac13ac6fSEric W. Biederman 	}
2390e47c99dSEric W. Biederman 	return 0;
240ac13ac6fSEric W. Biederman fail:
241ac13ac6fSEric W. Biederman 	erase_header(header);
242ac13ac6fSEric W. Biederman 	put_links(header);
2430e47c99dSEric W. Biederman fail_links:
244f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
245f9bd6733SEric W. Biederman 		clear_empty_dir(dir);
2460e47c99dSEric W. Biederman 	header->parent = NULL;
2470e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2480e47c99dSEric W. Biederman 	return err;
2498425d6aaSEric W. Biederman }
2508425d6aaSEric W. Biederman 
2511f87f0b5SEric W. Biederman /* called under sysctl_lock */
2521f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2531f87f0b5SEric W. Biederman {
2541f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2551f87f0b5SEric W. Biederman 		return 0;
2561f87f0b5SEric W. Biederman 	p->used++;
2571f87f0b5SEric W. Biederman 	return 1;
2581f87f0b5SEric W. Biederman }
2591f87f0b5SEric W. Biederman 
2601f87f0b5SEric W. Biederman /* called under sysctl_lock */
2611f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2621f87f0b5SEric W. Biederman {
2631f87f0b5SEric W. Biederman 	if (!--p->used)
2641f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2651f87f0b5SEric W. Biederman 			complete(p->unregistering);
2661f87f0b5SEric W. Biederman }
2671f87f0b5SEric W. Biederman 
268f90f3cafSEric W. Biederman static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
269d6cffbbeSKonstantin Khlebnikov {
270f90f3cafSEric W. Biederman 	proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
271d6cffbbeSKonstantin Khlebnikov }
272d6cffbbeSKonstantin Khlebnikov 
2731f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
2741f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2751f87f0b5SEric W. Biederman {
2761f87f0b5SEric W. Biederman 	/*
2771f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
2781f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
2791f87f0b5SEric W. Biederman 	 */
2801f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
2811f87f0b5SEric W. Biederman 		struct completion wait;
2821f87f0b5SEric W. Biederman 		init_completion(&wait);
2831f87f0b5SEric W. Biederman 		p->unregistering = &wait;
2841f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
2851f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
2861f87f0b5SEric W. Biederman 	} else {
2871f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
2881f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
289ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
2901f87f0b5SEric W. Biederman 	}
2911f87f0b5SEric W. Biederman 	/*
292f90f3cafSEric W. Biederman 	 * Invalidate dentries for unregistered sysctls: namespaced sysctls
293d6cffbbeSKonstantin Khlebnikov 	 * can have duplicate names and contaminate dcache very badly.
294d6cffbbeSKonstantin Khlebnikov 	 */
295f90f3cafSEric W. Biederman 	proc_sys_invalidate_dcache(p);
296d6cffbbeSKonstantin Khlebnikov 	/*
2971f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
2981f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
2991f87f0b5SEric W. Biederman 	 */
300ace0c791SEric W. Biederman 	spin_lock(&sysctl_lock);
3018425d6aaSEric W. Biederman 	erase_header(p);
3021f87f0b5SEric W. Biederman }
3031f87f0b5SEric W. Biederman 
3041f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3051f87f0b5SEric W. Biederman {
306ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3071f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3081f87f0b5SEric W. Biederman 	if (!use_table(head))
3091f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3101f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3111f87f0b5SEric W. Biederman 	return head;
3121f87f0b5SEric W. Biederman }
3131f87f0b5SEric W. Biederman 
3141f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3151f87f0b5SEric W. Biederman {
3161f87f0b5SEric W. Biederman 	if (!head)
3171f87f0b5SEric W. Biederman 		return;
3181f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3191f87f0b5SEric W. Biederman 	unuse_table(head);
3201f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3211f87f0b5SEric W. Biederman }
3221f87f0b5SEric W. Biederman 
3231f87f0b5SEric W. Biederman static struct ctl_table_set *
32413bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root)
3251f87f0b5SEric W. Biederman {
3261f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3271f87f0b5SEric W. Biederman 	if (root->lookup)
32813bcc6a2SEric W. Biederman 		set = root->lookup(root);
3291f87f0b5SEric W. Biederman 	return set;
3301f87f0b5SEric W. Biederman }
3311f87f0b5SEric W. Biederman 
332076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3337ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
334076c3eedSEric W. Biederman 				      const char *name, int namelen)
335076c3eedSEric W. Biederman {
336076c3eedSEric W. Biederman 	struct ctl_table_header *head;
337076c3eedSEric W. Biederman 	struct ctl_table *entry;
338076c3eedSEric W. Biederman 
339076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3400e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
341076c3eedSEric W. Biederman 	if (entry && use_table(head))
342076c3eedSEric W. Biederman 		*phead = head;
343076c3eedSEric W. Biederman 	else
344076c3eedSEric W. Biederman 		entry = NULL;
345076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
346076c3eedSEric W. Biederman 	return entry;
347076c3eedSEric W. Biederman }
348076c3eedSEric W. Biederman 
349ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3501f87f0b5SEric W. Biederman {
351ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3521f87f0b5SEric W. Biederman 
353ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
354ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
355ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
356ac13ac6fSEric W. Biederman 			return ctl_node;
3571f87f0b5SEric W. Biederman 	}
3581f87f0b5SEric W. Biederman 	return NULL;
3591f87f0b5SEric W. Biederman }
3601f87f0b5SEric W. Biederman 
3617ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3626a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3631f87f0b5SEric W. Biederman {
364ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3657ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
366ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3676a75ce16SEric W. Biederman 
3686a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
369ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3706a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
371ac13ac6fSEric W. Biederman 	if (ctl_node) {
372ac13ac6fSEric W. Biederman 		head = ctl_node->header;
373ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
374ac13ac6fSEric W. Biederman 	}
3756a75ce16SEric W. Biederman 	*phead = head;
3766a75ce16SEric W. Biederman 	*pentry = entry;
3776a75ce16SEric W. Biederman }
3786a75ce16SEric W. Biederman 
3797ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
3806a75ce16SEric W. Biederman {
3816a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
3826a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
383ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
3846a75ce16SEric W. Biederman 
3856a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
3866a75ce16SEric W. Biederman 	unuse_table(head);
387ac13ac6fSEric W. Biederman 
388ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
3896a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
390ac13ac6fSEric W. Biederman 	head = NULL;
391ac13ac6fSEric W. Biederman 	if (ctl_node) {
392ac13ac6fSEric W. Biederman 		head = ctl_node->header;
393ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
3946a75ce16SEric W. Biederman 	}
3956a75ce16SEric W. Biederman 	*phead = head;
3966a75ce16SEric W. Biederman 	*pentry = entry;
3971f87f0b5SEric W. Biederman }
3981f87f0b5SEric W. Biederman 
3991f87f0b5SEric W. Biederman /*
4001f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4011f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4021f87f0b5SEric W. Biederman  */
4031f87f0b5SEric W. Biederman 
4041f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4051f87f0b5SEric W. Biederman {
406091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4071f87f0b5SEric W. Biederman 		mode >>= 6;
408091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4091f87f0b5SEric W. Biederman 		mode >>= 3;
4101f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4111f87f0b5SEric W. Biederman 		return 0;
4121f87f0b5SEric W. Biederman 	return -EACCES;
4131f87f0b5SEric W. Biederman }
4141f87f0b5SEric W. Biederman 
41573f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
4161f87f0b5SEric W. Biederman {
41773f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4181f87f0b5SEric W. Biederman 	int mode;
4191f87f0b5SEric W. Biederman 
4201f87f0b5SEric W. Biederman 	if (root->permissions)
42173f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4221f87f0b5SEric W. Biederman 	else
4231f87f0b5SEric W. Biederman 		mode = table->mode;
4241f87f0b5SEric W. Biederman 
4251f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4261f87f0b5SEric W. Biederman }
4271f87f0b5SEric W. Biederman 
4289043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
4299043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
43077b14db5SEric W. Biederman {
431e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
43277b14db5SEric W. Biederman 	struct inode *inode;
4339043476fSAl Viro 	struct proc_inode *ei;
43477b14db5SEric W. Biederman 
4359043476fSAl Viro 	inode = new_inode(sb);
43677b14db5SEric W. Biederman 	if (!inode)
437ea5751ccSIvan Delalande 		return ERR_PTR(-ENOMEM);
43877b14db5SEric W. Biederman 
43985fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
44085fe4025SChristoph Hellwig 
44177b14db5SEric W. Biederman 	ei = PROC_I(inode);
4429043476fSAl Viro 
443d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
444ace0c791SEric W. Biederman 	if (unlikely(head->unregistering)) {
445ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
446ace0c791SEric W. Biederman 		iput(inode);
447ea5751ccSIvan Delalande 		return ERR_PTR(-ENOENT);
448ace0c791SEric W. Biederman 	}
449ace0c791SEric W. Biederman 	ei->sysctl = head;
450ace0c791SEric W. Biederman 	ei->sysctl_entry = table;
4510afa5ca8SEric W. Biederman 	hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
452d6cffbbeSKonstantin Khlebnikov 	head->count++;
453d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
454d6cffbbeSKonstantin Khlebnikov 
455078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
4569043476fSAl Viro 	inode->i_mode = table->mode;
4577ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4589043476fSAl Viro 		inode->i_mode |= S_IFREG;
45977b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
46077b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4619043476fSAl Viro 	} else {
4629043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4639043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4649043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
465f9bd6733SEric W. Biederman 		if (is_empty_dir(head))
466f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4679043476fSAl Viro 	}
468e79c6a4fSDmitry Torokhov 
469e79c6a4fSDmitry Torokhov 	if (root->set_ownership)
470e79c6a4fSDmitry Torokhov 		root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
4715ec27ec7SRadoslaw Burny 	else {
4725ec27ec7SRadoslaw Burny 		inode->i_uid = GLOBAL_ROOT_UID;
4735ec27ec7SRadoslaw Burny 		inode->i_gid = GLOBAL_ROOT_GID;
4745ec27ec7SRadoslaw Burny 	}
475e79c6a4fSDmitry Torokhov 
47677b14db5SEric W. Biederman 	return inode;
47777b14db5SEric W. Biederman }
47877b14db5SEric W. Biederman 
479d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
480d6cffbbeSKonstantin Khlebnikov {
481d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
4820afa5ca8SEric W. Biederman 	hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
483d6cffbbeSKonstantin Khlebnikov 	if (!--head->count)
484d6cffbbeSKonstantin Khlebnikov 		kfree_rcu(head, rcu);
485d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
486d6cffbbeSKonstantin Khlebnikov }
487d6cffbbeSKonstantin Khlebnikov 
48881324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
48977b14db5SEric W. Biederman {
4903cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
4913cc3e046SEric W. Biederman 	if (!head)
4920e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
4933cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
49477b14db5SEric W. Biederman }
49577b14db5SEric W. Biederman 
49677b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
49700cd8dd3SAl Viro 					unsigned int flags)
49877b14db5SEric W. Biederman {
4999043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
5009043476fSAl Viro 	struct ctl_table_header *h = NULL;
501dc12e909SAl Viro 	const struct qstr *name = &dentry->d_name;
5029043476fSAl Viro 	struct ctl_table *p;
50377b14db5SEric W. Biederman 	struct inode *inode;
5049043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
5057ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
5060e47c99dSEric W. Biederman 	int ret;
50777b14db5SEric W. Biederman 
5089043476fSAl Viro 	if (IS_ERR(head))
5099043476fSAl Viro 		return ERR_CAST(head);
5109043476fSAl Viro 
5117ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
5129043476fSAl Viro 
5137ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
5149043476fSAl Viro 	if (!p)
51577b14db5SEric W. Biederman 		goto out;
51677b14db5SEric W. Biederman 
5174e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
51813bcc6a2SEric W. Biederman 		ret = sysctl_follow_link(&h, &p);
5190e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5200e47c99dSEric W. Biederman 		if (ret)
5210e47c99dSEric W. Biederman 			goto out;
5224e757320SEric W. Biederman 	}
5230e47c99dSEric W. Biederman 
5249043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
525ea5751ccSIvan Delalande 	if (IS_ERR(inode)) {
526ea5751ccSIvan Delalande 		err = ERR_CAST(inode);
52777b14db5SEric W. Biederman 		goto out;
528ea5751ccSIvan Delalande 	}
52977b14db5SEric W. Biederman 
530fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
531888e2b03SAl Viro 	err = d_splice_alias(inode, dentry);
53277b14db5SEric W. Biederman 
53377b14db5SEric W. Biederman out:
5346bf61045SFrancesco Ruggeri 	if (h)
5356bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
53677b14db5SEric W. Biederman 	sysctl_head_finish(head);
53777b14db5SEric W. Biederman 	return err;
53877b14db5SEric W. Biederman }
53977b14db5SEric W. Biederman 
5404bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
5414bd6a735SMatthew Wilcox (Oracle) 		int write)
54277b14db5SEric W. Biederman {
5434bd6a735SMatthew Wilcox (Oracle) 	struct inode *inode = file_inode(iocb->ki_filp);
5449043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5459043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5464bd6a735SMatthew Wilcox (Oracle) 	size_t count = iov_iter_count(iter);
5474bd6a735SMatthew Wilcox (Oracle) 	char *kbuf;
5482a2da53bSDavid Howells 	ssize_t error;
54977b14db5SEric W. Biederman 
5509043476fSAl Viro 	if (IS_ERR(head))
5519043476fSAl Viro 		return PTR_ERR(head);
55277b14db5SEric W. Biederman 
55377b14db5SEric W. Biederman 	/*
55477b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
55577b14db5SEric W. Biederman 	 * and won't be until we finish.
55677b14db5SEric W. Biederman 	 */
55777b14db5SEric W. Biederman 	error = -EPERM;
55873f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
55977b14db5SEric W. Biederman 		goto out;
56077b14db5SEric W. Biederman 
5619043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5629043476fSAl Viro 	error = -EINVAL;
5639043476fSAl Viro 	if (!table->proc_handler)
5649043476fSAl Viro 		goto out;
5659043476fSAl Viro 
566ef9d965bSChristoph Hellwig 	/* don't even try if the size is too large */
567d4d80e69SMatthew Wilcox (Oracle) 	error = -ENOMEM;
568d4d80e69SMatthew Wilcox (Oracle) 	if (count >= KMALLOC_MAX_SIZE)
569d4d80e69SMatthew Wilcox (Oracle) 		goto out;
57045089437SJosef Bacik 	kbuf = kvzalloc(count + 1, GFP_KERNEL);
57132927393SChristoph Hellwig 	if (!kbuf)
57232927393SChristoph Hellwig 		goto out;
5734bd6a735SMatthew Wilcox (Oracle) 
5744bd6a735SMatthew Wilcox (Oracle) 	if (write) {
5754bd6a735SMatthew Wilcox (Oracle) 		error = -EFAULT;
5764bd6a735SMatthew Wilcox (Oracle) 		if (!copy_from_iter_full(kbuf, count, iter))
5774bd6a735SMatthew Wilcox (Oracle) 			goto out_free_buf;
5784bd6a735SMatthew Wilcox (Oracle) 		kbuf[count] = '\0';
5794e63acdfSAndrey Ignatov 	}
5804e63acdfSAndrey Ignatov 
58132927393SChristoph Hellwig 	error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
5824bd6a735SMatthew Wilcox (Oracle) 					   &iocb->ki_pos);
58332927393SChristoph Hellwig 	if (error)
58432927393SChristoph Hellwig 		goto out_free_buf;
58532927393SChristoph Hellwig 
58632927393SChristoph Hellwig 	/* careful: calling conventions are nasty here */
5874bd6a735SMatthew Wilcox (Oracle) 	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
58832927393SChristoph Hellwig 	if (error)
58932927393SChristoph Hellwig 		goto out_free_buf;
59032927393SChristoph Hellwig 
59132927393SChristoph Hellwig 	if (!write) {
59232927393SChristoph Hellwig 		error = -EFAULT;
5934bd6a735SMatthew Wilcox (Oracle) 		if (copy_to_iter(kbuf, count, iter) < count)
59432927393SChristoph Hellwig 			goto out_free_buf;
59532927393SChristoph Hellwig 	}
59632927393SChristoph Hellwig 
5974e63acdfSAndrey Ignatov 	error = count;
59832927393SChristoph Hellwig out_free_buf:
59945089437SJosef Bacik 	kvfree(kbuf);
60077b14db5SEric W. Biederman out:
60177b14db5SEric W. Biederman 	sysctl_head_finish(head);
60277b14db5SEric W. Biederman 
60377b14db5SEric W. Biederman 	return error;
60477b14db5SEric W. Biederman }
60577b14db5SEric W. Biederman 
6064bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
6077708bfb1SPavel Emelyanov {
6084bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 0);
6097708bfb1SPavel Emelyanov }
6107708bfb1SPavel Emelyanov 
6114bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
61277b14db5SEric W. Biederman {
6134bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 1);
61477b14db5SEric W. Biederman }
61577b14db5SEric W. Biederman 
616f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
617f1ecf068SLucas De Marchi {
6184e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
619f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
620f1ecf068SLucas De Marchi 
6214e474a00SLucas De Marchi 	/* sysctl was unregistered */
6224e474a00SLucas De Marchi 	if (IS_ERR(head))
6234e474a00SLucas De Marchi 		return PTR_ERR(head);
6244e474a00SLucas De Marchi 
625f1ecf068SLucas De Marchi 	if (table->poll)
626f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
627f1ecf068SLucas De Marchi 
6284e474a00SLucas De Marchi 	sysctl_head_finish(head);
6294e474a00SLucas De Marchi 
630f1ecf068SLucas De Marchi 	return 0;
631f1ecf068SLucas De Marchi }
632f1ecf068SLucas De Marchi 
633076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
634f1ecf068SLucas De Marchi {
635496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
6364e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
637f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
638076ccb76SAl Viro 	__poll_t ret = DEFAULT_POLLMASK;
6394e474a00SLucas De Marchi 	unsigned long event;
6404e474a00SLucas De Marchi 
6414e474a00SLucas De Marchi 	/* sysctl was unregistered */
6424e474a00SLucas De Marchi 	if (IS_ERR(head))
643a9a08845SLinus Torvalds 		return EPOLLERR | EPOLLHUP;
644f1ecf068SLucas De Marchi 
645f1ecf068SLucas De Marchi 	if (!table->proc_handler)
646f1ecf068SLucas De Marchi 		goto out;
647f1ecf068SLucas De Marchi 
648f1ecf068SLucas De Marchi 	if (!table->poll)
649f1ecf068SLucas De Marchi 		goto out;
650f1ecf068SLucas De Marchi 
6514e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
652f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
653f1ecf068SLucas De Marchi 
654f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
655f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
656a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
657f1ecf068SLucas De Marchi 	}
658f1ecf068SLucas De Marchi 
659f1ecf068SLucas De Marchi out:
6604e474a00SLucas De Marchi 	sysctl_head_finish(head);
6614e474a00SLucas De Marchi 
662f1ecf068SLucas De Marchi 	return ret;
663f1ecf068SLucas De Marchi }
66477b14db5SEric W. Biederman 
665f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
666f0c3b509SAl Viro 				struct dir_context *ctx,
6679043476fSAl Viro 				struct ctl_table_header *head,
6689043476fSAl Viro 				struct ctl_table *table)
66977b14db5SEric W. Biederman {
670f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
67177b14db5SEric W. Biederman 	struct inode *inode;
67277b14db5SEric W. Biederman 	struct qstr qname;
67377b14db5SEric W. Biederman 	ino_t ino = 0;
67477b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
67577b14db5SEric W. Biederman 
67677b14db5SEric W. Biederman 	qname.name = table->procname;
67777b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
6788387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
67977b14db5SEric W. Biederman 
68077b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
68177b14db5SEric W. Biederman 	if (!child) {
68276aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
68376aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
68476aab3abSAl Viro 		if (IS_ERR(child))
68576aab3abSAl Viro 			return false;
68676aab3abSAl Viro 		if (d_in_lookup(child)) {
687888e2b03SAl Viro 			struct dentry *res;
6889043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
689ea5751ccSIvan Delalande 			if (IS_ERR(inode)) {
69076aab3abSAl Viro 				d_lookup_done(child);
6919043476fSAl Viro 				dput(child);
692f0c3b509SAl Viro 				return false;
69376aab3abSAl Viro 			}
694fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
695888e2b03SAl Viro 			res = d_splice_alias(inode, child);
696888e2b03SAl Viro 			d_lookup_done(child);
697888e2b03SAl Viro 			if (unlikely(res)) {
698888e2b03SAl Viro 				if (IS_ERR(res)) {
699888e2b03SAl Viro 					dput(child);
700888e2b03SAl Viro 					return false;
701888e2b03SAl Viro 				}
702888e2b03SAl Viro 				dput(child);
703888e2b03SAl Viro 				child = res;
704888e2b03SAl Viro 			}
70577b14db5SEric W. Biederman 		}
70677b14db5SEric W. Biederman 	}
7072b0143b5SDavid Howells 	inode = d_inode(child);
70877b14db5SEric W. Biederman 	ino  = inode->i_ino;
70977b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
71077b14db5SEric W. Biederman 	dput(child);
711f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
7129043476fSAl Viro }
7139043476fSAl Viro 
714f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
715f0c3b509SAl Viro 				    struct dir_context *ctx,
7160e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
7170e47c99dSEric W. Biederman 				    struct ctl_table *table)
7180e47c99dSEric W. Biederman {
719f0c3b509SAl Viro 	bool ret = true;
720a0b0d1c3SDanilo Krummrich 
7210e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
722a0b0d1c3SDanilo Krummrich 	if (IS_ERR(head))
723a0b0d1c3SDanilo Krummrich 		return false;
7240e47c99dSEric W. Biederman 
7250e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
726835b94e0SDanilo Krummrich 	if (sysctl_follow_link(&head, &table))
7270e47c99dSEric W. Biederman 		goto out;
7280e47c99dSEric W. Biederman 
729f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
7300e47c99dSEric W. Biederman out:
7310e47c99dSEric W. Biederman 	sysctl_head_finish(head);
7320e47c99dSEric W. Biederman 	return ret;
7330e47c99dSEric W. Biederman }
7340e47c99dSEric W. Biederman 
735e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table,
7369043476fSAl Viro 		unsigned long *pos, struct file *file,
737f0c3b509SAl Viro 		struct dir_context *ctx)
7389043476fSAl Viro {
739f0c3b509SAl Viro 	bool res;
7409043476fSAl Viro 
741f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
742f0c3b509SAl Viro 		return true;
7439043476fSAl Viro 
7440e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
745f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
7460e47c99dSEric W. Biederman 	else
747f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
7489043476fSAl Viro 
749f0c3b509SAl Viro 	if (res)
750f0c3b509SAl Viro 		ctx->pos = *pos;
7516a75ce16SEric W. Biederman 
7526a75ce16SEric W. Biederman 	return res;
75377b14db5SEric W. Biederman }
75477b14db5SEric W. Biederman 
755f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
75677b14db5SEric W. Biederman {
757f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7589043476fSAl Viro 	struct ctl_table_header *h = NULL;
7596a75ce16SEric W. Biederman 	struct ctl_table *entry;
7607ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
76177b14db5SEric W. Biederman 	unsigned long pos;
76277b14db5SEric W. Biederman 
7639043476fSAl Viro 	if (IS_ERR(head))
7649043476fSAl Viro 		return PTR_ERR(head);
7659043476fSAl Viro 
7667ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
76777b14db5SEric W. Biederman 
768f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
76993362fa4SZhou Chengming 		goto out;
770f0c3b509SAl Viro 
77177b14db5SEric W. Biederman 	pos = 2;
77277b14db5SEric W. Biederman 
7737ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
774f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7759043476fSAl Viro 			sysctl_head_finish(h);
7769043476fSAl Viro 			break;
77777b14db5SEric W. Biederman 		}
77877b14db5SEric W. Biederman 	}
77993362fa4SZhou Chengming out:
78077b14db5SEric W. Biederman 	sysctl_head_finish(head);
781f0c3b509SAl Viro 	return 0;
78277b14db5SEric W. Biederman }
78377b14db5SEric W. Biederman 
784549c7297SChristian Brauner static int proc_sys_permission(struct user_namespace *mnt_userns,
785549c7297SChristian Brauner 			       struct inode *inode, int mask)
78677b14db5SEric W. Biederman {
78777b14db5SEric W. Biederman 	/*
78877b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
78977b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
79077b14db5SEric W. Biederman 	 */
791f696a365SMiklos Szeredi 	struct ctl_table_header *head;
792f696a365SMiklos Szeredi 	struct ctl_table *table;
79377b14db5SEric W. Biederman 	int error;
79477b14db5SEric W. Biederman 
795f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
796f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
797f696a365SMiklos Szeredi 		return -EACCES;
798f696a365SMiklos Szeredi 
799f696a365SMiklos Szeredi 	head = grab_header(inode);
8009043476fSAl Viro 	if (IS_ERR(head))
8019043476fSAl Viro 		return PTR_ERR(head);
80277b14db5SEric W. Biederman 
803f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
8049043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
8059043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
8069043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
80773f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
8089043476fSAl Viro 
80977b14db5SEric W. Biederman 	sysctl_head_finish(head);
81077b14db5SEric W. Biederman 	return error;
81177b14db5SEric W. Biederman }
81277b14db5SEric W. Biederman 
813549c7297SChristian Brauner static int proc_sys_setattr(struct user_namespace *mnt_userns,
814549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *attr)
81577b14db5SEric W. Biederman {
8162b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
81777b14db5SEric W. Biederman 	int error;
81877b14db5SEric W. Biederman 
81977b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
82077b14db5SEric W. Biederman 		return -EPERM;
82177b14db5SEric W. Biederman 
8222f221d6fSChristian Brauner 	error = setattr_prepare(&init_user_ns, dentry, attr);
8231025774cSChristoph Hellwig 	if (error)
82477b14db5SEric W. Biederman 		return error;
8251025774cSChristoph Hellwig 
8262f221d6fSChristian Brauner 	setattr_copy(&init_user_ns, inode, attr);
8271025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8281025774cSChristoph Hellwig 	return 0;
82977b14db5SEric W. Biederman }
83077b14db5SEric W. Biederman 
831549c7297SChristian Brauner static int proc_sys_getattr(struct user_namespace *mnt_userns,
832549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
833a528d35eSDavid Howells 			    u32 request_mask, unsigned int query_flags)
8349043476fSAl Viro {
835a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
8369043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
8379043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
8389043476fSAl Viro 
8399043476fSAl Viro 	if (IS_ERR(head))
8409043476fSAl Viro 		return PTR_ERR(head);
8419043476fSAl Viro 
8420d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, inode, stat);
8439043476fSAl Viro 	if (table)
8449043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
8459043476fSAl Viro 
8469043476fSAl Viro 	sysctl_head_finish(head);
8479043476fSAl Viro 	return 0;
8489043476fSAl Viro }
8499043476fSAl Viro 
85077b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
851f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
852f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
8534bd6a735SMatthew Wilcox (Oracle) 	.read_iter	= proc_sys_read,
8544bd6a735SMatthew Wilcox (Oracle) 	.write_iter	= proc_sys_write,
8554bd6a735SMatthew Wilcox (Oracle) 	.splice_read	= generic_file_splice_read,
8564bd6a735SMatthew Wilcox (Oracle) 	.splice_write	= iter_file_splice_write,
8576038f373SArnd Bergmann 	.llseek		= default_llseek,
8589043476fSAl Viro };
8599043476fSAl Viro 
8609043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
861887df078SPavel Emelyanov 	.read		= generic_read_dir,
862f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
8633222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
86477b14db5SEric W. Biederman };
86577b14db5SEric W. Biederman 
86603a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8679043476fSAl Viro 	.permission	= proc_sys_permission,
8689043476fSAl Viro 	.setattr	= proc_sys_setattr,
8699043476fSAl Viro 	.getattr	= proc_sys_getattr,
8709043476fSAl Viro };
8719043476fSAl Viro 
8729043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
87377b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
87477b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
87577b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
8769043476fSAl Viro 	.getattr	= proc_sys_getattr,
87777b14db5SEric W. Biederman };
87877b14db5SEric W. Biederman 
8790b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
88077b14db5SEric W. Biederman {
8810b728e19SAl Viro 	if (flags & LOOKUP_RCU)
88234286d66SNick Piggin 		return -ECHILD;
8832b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
8849043476fSAl Viro }
8859043476fSAl Viro 
886fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
8879043476fSAl Viro {
8882b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
8899043476fSAl Viro }
8909043476fSAl Viro 
8911f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
8921f87f0b5SEric W. Biederman {
8931f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
8941f87f0b5SEric W. Biederman 	int res;
8951f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
8961f87f0b5SEric W. Biederman 	if (p->unregistering)
8971f87f0b5SEric W. Biederman 		res = 0;
8981f87f0b5SEric W. Biederman 	else if (!set->is_seen)
8991f87f0b5SEric W. Biederman 		res = 1;
9001f87f0b5SEric W. Biederman 	else
9011f87f0b5SEric W. Biederman 		res = set->is_seen(set);
9021f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
9031f87f0b5SEric W. Biederman 	return res;
9041f87f0b5SEric W. Biederman }
9051f87f0b5SEric W. Biederman 
9066fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry,
907621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
9089043476fSAl Viro {
909dfef6dcdSAl Viro 	struct ctl_table_header *head;
910da53be12SLinus Torvalds 	struct inode *inode;
911da53be12SLinus Torvalds 
91231e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
91331e6b01fSNick Piggin 	 * that inode here can be NULL */
914dfef6dcdSAl Viro 	/* AV: can it, indeed? */
9152b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
91631e6b01fSNick Piggin 	if (!inode)
917dfef6dcdSAl Viro 		return 1;
918621e155aSNick Piggin 	if (name->len != len)
9199043476fSAl Viro 		return 1;
920621e155aSNick Piggin 	if (memcmp(name->name, str, len))
9219043476fSAl Viro 		return 1;
922dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
923dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
92477b14db5SEric W. Biederman }
92577b14db5SEric W. Biederman 
926d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
92777b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
9289043476fSAl Viro 	.d_delete	= proc_sys_delete,
9299043476fSAl Viro 	.d_compare	= proc_sys_compare,
93077b14db5SEric W. Biederman };
93177b14db5SEric W. Biederman 
9320e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
9337ec66d06SEric W. Biederman 				   const char *name, int namelen)
9341f87f0b5SEric W. Biederman {
9357ec66d06SEric W. Biederman 	struct ctl_table_header *head;
9367ec66d06SEric W. Biederman 	struct ctl_table *entry;
9371f87f0b5SEric W. Biederman 
9380e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
9397ec66d06SEric W. Biederman 	if (!entry)
9407ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
94151f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
9427ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
94351f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
9441f87f0b5SEric W. Biederman }
9451f87f0b5SEric W. Biederman 
9467ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
9477ec66d06SEric W. Biederman 			       const char *name, int namelen)
9481f87f0b5SEric W. Biederman {
9497ec66d06SEric W. Biederman 	struct ctl_table *table;
9507ec66d06SEric W. Biederman 	struct ctl_dir *new;
951ac13ac6fSEric W. Biederman 	struct ctl_node *node;
9527ec66d06SEric W. Biederman 	char *new_name;
9531f87f0b5SEric W. Biederman 
954ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
955ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
956ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
9577ec66d06SEric W. Biederman 	if (!new)
9587ec66d06SEric W. Biederman 		return NULL;
9597ec66d06SEric W. Biederman 
960ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
961ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
9627ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
9637ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9647ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
9657ec66d06SEric W. Biederman 	table[0].procname = new_name;
9667ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
967ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
9687ec66d06SEric W. Biederman 
9697ec66d06SEric W. Biederman 	return new;
9701f87f0b5SEric W. Biederman }
9711f87f0b5SEric W. Biederman 
97260f126d9SEric W. Biederman /**
97360f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
97460f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
97560f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
97660f126d9SEric W. Biederman  * @namelen: The length of name
97760f126d9SEric W. Biederman  *
97860f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
97960f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
98060f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
98160f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
98260f126d9SEric W. Biederman  * simply dropped.
98360f126d9SEric W. Biederman  */
9840e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
9850e47c99dSEric W. Biederman 				  const char *name, int namelen)
9867ec66d06SEric W. Biederman {
9870e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
9887ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
9890eb97f38SEric W. Biederman 	int err;
9907ec66d06SEric W. Biederman 
9917ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
9920e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
9937ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
9947ec66d06SEric W. Biederman 		goto found;
9957ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
9967ec66d06SEric W. Biederman 		goto failed;
9977ec66d06SEric W. Biederman 
9987ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
9997ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
10007ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10017ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
10027ec66d06SEric W. Biederman 	if (!new)
10037ec66d06SEric W. Biederman 		goto failed;
10047ec66d06SEric W. Biederman 
100560f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
10060e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10077ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10087ec66d06SEric W. Biederman 		goto found;
10097ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10107ec66d06SEric W. Biederman 		goto failed;
10117ec66d06SEric W. Biederman 
101260f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
10130eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
10140eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
10150eb97f38SEric W. Biederman 	if (err)
10160e47c99dSEric W. Biederman 		goto failed;
10177ec66d06SEric W. Biederman 	subdir = new;
10187ec66d06SEric W. Biederman found:
10197ec66d06SEric W. Biederman 	subdir->header.nreg++;
10207ec66d06SEric W. Biederman failed:
1021a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
102287ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
10236980128fSEric W. Biederman 		sysctl_print_dir(dir);
1024153ee1c4SGeert Uytterhoeven 		pr_cont("%*.*s %ld\n", namelen, namelen, name,
1025153ee1c4SGeert Uytterhoeven 			PTR_ERR(subdir));
10261f87f0b5SEric W. Biederman 	}
10277ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10287ec66d06SEric W. Biederman 	if (new)
10297ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
10307ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10317ec66d06SEric W. Biederman 	return subdir;
10321f87f0b5SEric W. Biederman }
10331f87f0b5SEric W. Biederman 
10340e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
10350e47c99dSEric W. Biederman {
10360e47c99dSEric W. Biederman 	struct ctl_dir *parent;
10370e47c99dSEric W. Biederman 	const char *procname;
10380e47c99dSEric W. Biederman 	if (!dir->header.parent)
10390e47c99dSEric W. Biederman 		return &set->dir;
10400e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
10410e47c99dSEric W. Biederman 	if (IS_ERR(parent))
10420e47c99dSEric W. Biederman 		return parent;
10430e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
10440e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
10450e47c99dSEric W. Biederman }
10460e47c99dSEric W. Biederman 
10470e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
104813bcc6a2SEric W. Biederman 	struct ctl_table **pentry)
10490e47c99dSEric W. Biederman {
10500e47c99dSEric W. Biederman 	struct ctl_table_header *head;
10510e47c99dSEric W. Biederman 	struct ctl_table_root *root;
10520e47c99dSEric W. Biederman 	struct ctl_table_set *set;
10530e47c99dSEric W. Biederman 	struct ctl_table *entry;
10540e47c99dSEric W. Biederman 	struct ctl_dir *dir;
10550e47c99dSEric W. Biederman 	int ret;
10560e47c99dSEric W. Biederman 
10570e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
10580e47c99dSEric W. Biederman 	root = (*pentry)->data;
105913bcc6a2SEric W. Biederman 	set = lookup_header_set(root);
10600e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
10610e47c99dSEric W. Biederman 	if (IS_ERR(dir))
10620e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10630e47c99dSEric W. Biederman 	else {
10640e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10650e47c99dSEric W. Biederman 		head = NULL;
10660e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10670e47c99dSEric W. Biederman 		ret = -ENOENT;
10680e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10690e47c99dSEric W. Biederman 			unuse_table(*phead);
10700e47c99dSEric W. Biederman 			*phead = head;
10710e47c99dSEric W. Biederman 			*pentry = entry;
10720e47c99dSEric W. Biederman 			ret = 0;
10730e47c99dSEric W. Biederman 		}
10740e47c99dSEric W. Biederman 	}
10750e47c99dSEric W. Biederman 
10760e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
10770e47c99dSEric W. Biederman 	return ret;
10780e47c99dSEric W. Biederman }
10790e47c99dSEric W. Biederman 
10807c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
10817c60c48fSEric W. Biederman {
10827c60c48fSEric W. Biederman 	struct va_format vaf;
10837c60c48fSEric W. Biederman 	va_list args;
10847c60c48fSEric W. Biederman 
10857c60c48fSEric W. Biederman 	va_start(args, fmt);
10867c60c48fSEric W. Biederman 	vaf.fmt = fmt;
10877c60c48fSEric W. Biederman 	vaf.va = &args;
10887c60c48fSEric W. Biederman 
108987ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
10907c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
10917c60c48fSEric W. Biederman 
10927c60c48fSEric W. Biederman 	va_end(args);
10937c60c48fSEric W. Biederman 	return -EINVAL;
10947c60c48fSEric W. Biederman }
10957c60c48fSEric W. Biederman 
10964f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table)
10974f2fec00SLuis R. Rodriguez {
10984f2fec00SLuis R. Rodriguez 	int err = 0;
10994f2fec00SLuis R. Rodriguez 
110061d9b56aSLuis R. Rodriguez 	if ((table->proc_handler == proc_douintvec) ||
110161d9b56aSLuis R. Rodriguez 	    (table->proc_handler == proc_douintvec_minmax)) {
11024f2fec00SLuis R. Rodriguez 		if (table->maxlen != sizeof(unsigned int))
110364a11f3dSWaiman Long 			err |= sysctl_err(path, table, "array not allowed");
11044f2fec00SLuis R. Rodriguez 	}
11054f2fec00SLuis R. Rodriguez 
1106cb944413SEric Dumazet 	if (table->proc_handler == proc_dou8vec_minmax) {
1107cb944413SEric Dumazet 		if (table->maxlen != sizeof(u8))
1108cb944413SEric Dumazet 			err |= sysctl_err(path, table, "array not allowed");
1109cb944413SEric Dumazet 	}
1110cb944413SEric Dumazet 
11114f2fec00SLuis R. Rodriguez 	return err;
11124f2fec00SLuis R. Rodriguez }
11134f2fec00SLuis R. Rodriguez 
11147c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
11157c60c48fSEric W. Biederman {
11167c60c48fSEric W. Biederman 	int err = 0;
11177c60c48fSEric W. Biederman 	for (; table->procname; table++) {
11187c60c48fSEric W. Biederman 		if (table->child)
111989c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "Not a file");
11207c60c48fSEric W. Biederman 
11211f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
11221f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
11231680a386SLiping Zhang 		    (table->proc_handler == proc_douintvec) ||
112461d9b56aSLuis R. Rodriguez 		    (table->proc_handler == proc_douintvec_minmax) ||
11251f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
1126cb944413SEric Dumazet 		    (table->proc_handler == proc_dou8vec_minmax) ||
11271f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
11281f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
11291f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
11301f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
11311f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
11321f87f0b5SEric W. Biederman 			if (!table->data)
113389c5b53bSLuis R. Rodriguez 				err |= sysctl_err(path, table, "No data");
11341f87f0b5SEric W. Biederman 			if (!table->maxlen)
113589c5b53bSLuis R. Rodriguez 				err |= sysctl_err(path, table, "No maxlen");
11364f2fec00SLuis R. Rodriguez 			else
11374f2fec00SLuis R. Rodriguez 				err |= sysctl_check_table_array(path, table);
11381f87f0b5SEric W. Biederman 		}
11391f87f0b5SEric W. Biederman 		if (!table->proc_handler)
114089c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "No proc_handler");
11417c60c48fSEric W. Biederman 
11427c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
114389c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "bogus .mode 0%o",
11447c60c48fSEric W. Biederman 				table->mode);
11451f87f0b5SEric W. Biederman 	}
11467c60c48fSEric W. Biederman 	return err;
11471f87f0b5SEric W. Biederman }
11481f87f0b5SEric W. Biederman 
11490e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
11500e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
11510e47c99dSEric W. Biederman {
11520e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
11530e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1154ac13ac6fSEric W. Biederman 	struct ctl_node *node;
11550e47c99dSEric W. Biederman 	char *link_name;
11560e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
11570e47c99dSEric W. Biederman 
11580e47c99dSEric W. Biederman 	name_bytes = 0;
11590e47c99dSEric W. Biederman 	nr_entries = 0;
11600e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11610e47c99dSEric W. Biederman 		nr_entries++;
11620e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
11630e47c99dSEric W. Biederman 	}
11640e47c99dSEric W. Biederman 
11650e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1166ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
11670e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
11680e47c99dSEric W. Biederman 			name_bytes,
11690e47c99dSEric W. Biederman 			GFP_KERNEL);
11700e47c99dSEric W. Biederman 
11710e47c99dSEric W. Biederman 	if (!links)
11720e47c99dSEric W. Biederman 		return NULL;
11730e47c99dSEric W. Biederman 
1174ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1175ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
11760e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
11770e47c99dSEric W. Biederman 
11780e47c99dSEric W. Biederman 	for (link = link_table, entry = table; entry->procname; link++, entry++) {
11790e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
11800e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
11810e47c99dSEric W. Biederman 		link->procname = link_name;
11820e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
11830e47c99dSEric W. Biederman 		link->data = link_root;
11840e47c99dSEric W. Biederman 		link_name += len;
11850e47c99dSEric W. Biederman 	}
1186ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
11870e47c99dSEric W. Biederman 	links->nreg = nr_entries;
11880e47c99dSEric W. Biederman 
11890e47c99dSEric W. Biederman 	return links;
11900e47c99dSEric W. Biederman }
11910e47c99dSEric W. Biederman 
11920e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
11930e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
11940e47c99dSEric W. Biederman {
11950e47c99dSEric W. Biederman 	struct ctl_table_header *head;
11960e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
11970e47c99dSEric W. Biederman 
11980e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
11990e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
12000e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12010e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12020e47c99dSEric W. Biederman 		if (!link)
12030e47c99dSEric W. Biederman 			return false;
12040e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
12050e47c99dSEric W. Biederman 			continue;
12060e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
12070e47c99dSEric W. Biederman 			continue;
12080e47c99dSEric W. Biederman 		return false;
12090e47c99dSEric W. Biederman 	}
12100e47c99dSEric W. Biederman 
12110e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
12120e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
12130e47c99dSEric W. Biederman 		const char *procname = entry->procname;
12140e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
12150e47c99dSEric W. Biederman 		head->nreg++;
12160e47c99dSEric W. Biederman 	}
12170e47c99dSEric W. Biederman 	return true;
12180e47c99dSEric W. Biederman }
12190e47c99dSEric W. Biederman 
12200e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
12210e47c99dSEric W. Biederman {
12220e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
12230e47c99dSEric W. Biederman 	struct ctl_dir *core_parent = NULL;
12240e47c99dSEric W. Biederman 	struct ctl_table_header *links;
12250e47c99dSEric W. Biederman 	int err;
12260e47c99dSEric W. Biederman 
12270e47c99dSEric W. Biederman 	if (head->set == root_set)
12280e47c99dSEric W. Biederman 		return 0;
12290e47c99dSEric W. Biederman 
12300e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
12310e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
12320e47c99dSEric W. Biederman 		return 0;
12330e47c99dSEric W. Biederman 
12340e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
12350e47c99dSEric W. Biederman 		return 0;
12360e47c99dSEric W. Biederman 
12370e47c99dSEric W. Biederman 	core_parent->header.nreg++;
12380e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
12390e47c99dSEric W. Biederman 
12400e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
12410e47c99dSEric W. Biederman 
12420e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
12430e47c99dSEric W. Biederman 	err = -ENOMEM;
12440e47c99dSEric W. Biederman 	if (!links)
12450e47c99dSEric W. Biederman 		goto out;
12460e47c99dSEric W. Biederman 
12470e47c99dSEric W. Biederman 	err = 0;
12480e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
12490e47c99dSEric W. Biederman 		kfree(links);
12500e47c99dSEric W. Biederman 		goto out;
12510e47c99dSEric W. Biederman 	}
12520e47c99dSEric W. Biederman 
12530e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
12540e47c99dSEric W. Biederman 	if (err)
12550e47c99dSEric W. Biederman 		kfree(links);
12560e47c99dSEric W. Biederman out:
12570e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
12580e47c99dSEric W. Biederman 	return err;
12590e47c99dSEric W. Biederman }
12600e47c99dSEric W. Biederman 
12611f87f0b5SEric W. Biederman /**
1262f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
126360a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
12641f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
12651f87f0b5SEric W. Biederman  * @table: the top-level table structure
12661f87f0b5SEric W. Biederman  *
12671f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12681f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12691f87f0b5SEric W. Biederman  *
12701f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
12711f87f0b5SEric W. Biederman  *
12721f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
12731f87f0b5SEric W. Biederman  *            enter a sysctl file
12741f87f0b5SEric W. Biederman  *
12751f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
12761f87f0b5SEric W. Biederman  *
12771f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
12781f87f0b5SEric W. Biederman  *
1279f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
12801f87f0b5SEric W. Biederman  *
1281f728019bSEric W. Biederman  * child - must be %NULL.
12821f87f0b5SEric W. Biederman  *
12831f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
12841f87f0b5SEric W. Biederman  *
12851f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
12861f87f0b5SEric W. Biederman  *
12871f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
12881f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
12891f87f0b5SEric W. Biederman  *
1290f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1291f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
12921f87f0b5SEric W. Biederman  *
12931f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
12941f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
12951f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
12961f87f0b5SEric W. Biederman  *
12971f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
12981f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
12991f87f0b5SEric W. Biederman  *
13001f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
13011f87f0b5SEric W. Biederman  * to the table header on success.
13021f87f0b5SEric W. Biederman  */
13036e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
130460a47a2eSEric W. Biederman 	struct ctl_table_set *set,
13056e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
13061f87f0b5SEric W. Biederman {
130760a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
13081f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
13096e9d5164SEric W. Biederman 	const char *name, *nextname;
13107ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1311ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1312ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1313ac13ac6fSEric W. Biederman 	int nr_entries = 0;
13141f87f0b5SEric W. Biederman 
1315ac13ac6fSEric W. Biederman 	for (entry = table; entry->procname; entry++)
1316ac13ac6fSEric W. Biederman 		nr_entries++;
1317ac13ac6fSEric W. Biederman 
1318ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1319ac13ac6fSEric W. Biederman 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
13201f87f0b5SEric W. Biederman 	if (!header)
13211f87f0b5SEric W. Biederman 		return NULL;
13221f87f0b5SEric W. Biederman 
1323ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1324ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
13257c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
13267c60c48fSEric W. Biederman 		goto fail;
13278d6ecfccSEric W. Biederman 
13281f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
13290e47c99dSEric W. Biederman 	dir = &set->dir;
133060f126d9SEric W. Biederman 	/* Reference moved down the diretory tree get_subdir */
13317ec66d06SEric W. Biederman 	dir->header.nreg++;
13327ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
13337ec66d06SEric W. Biederman 
13347ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
13357ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
13367ec66d06SEric W. Biederman 		int namelen;
13377ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
13387ec66d06SEric W. Biederman 		if (nextname) {
13397ec66d06SEric W. Biederman 			namelen = nextname - name;
13407ec66d06SEric W. Biederman 			nextname++;
13417ec66d06SEric W. Biederman 		} else {
13427ec66d06SEric W. Biederman 			namelen = strlen(name);
13437ec66d06SEric W. Biederman 		}
13447ec66d06SEric W. Biederman 		if (namelen == 0)
13451f87f0b5SEric W. Biederman 			continue;
13467ec66d06SEric W. Biederman 
13470e47c99dSEric W. Biederman 		dir = get_subdir(dir, name, namelen);
13487ec66d06SEric W. Biederman 		if (IS_ERR(dir))
13497ec66d06SEric W. Biederman 			goto fail;
13501f87f0b5SEric W. Biederman 	}
13510e47c99dSEric W. Biederman 
13527ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
13530e47c99dSEric W. Biederman 	if (insert_header(dir, header))
13540e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
13550e47c99dSEric W. Biederman 
13567ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13571f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13581f87f0b5SEric W. Biederman 
13591f87f0b5SEric W. Biederman 	return header;
13600e47c99dSEric W. Biederman 
13617ec66d06SEric W. Biederman fail_put_dir_locked:
13627ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13637c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
13647c60c48fSEric W. Biederman fail:
13657c60c48fSEric W. Biederman 	kfree(header);
13667c60c48fSEric W. Biederman 	dump_stack();
13677c60c48fSEric W. Biederman 	return NULL;
13681f87f0b5SEric W. Biederman }
13691f87f0b5SEric W. Biederman 
1370fea478d4SEric W. Biederman /**
1371fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1372fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1373fea478d4SEric W. Biederman  * @table: the table structure
1374fea478d4SEric W. Biederman  *
1375fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1376fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1377fea478d4SEric W. Biederman  *
1378fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1379fea478d4SEric W. Biederman  */
1380fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1381fea478d4SEric W. Biederman {
1382fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1383fea478d4SEric W. Biederman 					path, table);
1384fea478d4SEric W. Biederman }
1385fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1386fea478d4SEric W. Biederman 
1387*3ddd9a80SXiaoming Ni /**
1388*3ddd9a80SXiaoming Ni  * __register_sysctl_init() - register sysctl table to path
1389*3ddd9a80SXiaoming Ni  * @path: path name for sysctl base
1390*3ddd9a80SXiaoming Ni  * @table: This is the sysctl table that needs to be registered to the path
1391*3ddd9a80SXiaoming Ni  * @table_name: The name of sysctl table, only used for log printing when
1392*3ddd9a80SXiaoming Ni  *              registration fails
1393*3ddd9a80SXiaoming Ni  *
1394*3ddd9a80SXiaoming Ni  * The sysctl interface is used by userspace to query or modify at runtime
1395*3ddd9a80SXiaoming Ni  * a predefined value set on a variable. These variables however have default
1396*3ddd9a80SXiaoming Ni  * values pre-set. Code which depends on these variables will always work even
1397*3ddd9a80SXiaoming Ni  * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1398*3ddd9a80SXiaoming Ni  * ability to query or modify the sysctls dynamically at run time. Chances of
1399*3ddd9a80SXiaoming Ni  * register_sysctl() failing on init are extremely low, and so for both reasons
1400*3ddd9a80SXiaoming Ni  * this function does not return any error as it is used by initialization code.
1401*3ddd9a80SXiaoming Ni  *
1402*3ddd9a80SXiaoming Ni  * Context: Can only be called after your respective sysctl base path has been
1403*3ddd9a80SXiaoming Ni  * registered. So for instance, most base directories are registered early on
1404*3ddd9a80SXiaoming Ni  * init before init levels are processed through proc_sys_init() and
1405*3ddd9a80SXiaoming Ni  * sysctl_init().
1406*3ddd9a80SXiaoming Ni  */
1407*3ddd9a80SXiaoming Ni void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1408*3ddd9a80SXiaoming Ni 				 const char *table_name)
1409*3ddd9a80SXiaoming Ni {
1410*3ddd9a80SXiaoming Ni 	struct ctl_table_header *hdr = register_sysctl(path, table);
1411*3ddd9a80SXiaoming Ni 
1412*3ddd9a80SXiaoming Ni 	if (unlikely(!hdr)) {
1413*3ddd9a80SXiaoming Ni 		pr_err("failed when register_sysctl %s to %s\n", table_name, path);
1414*3ddd9a80SXiaoming Ni 		return;
1415*3ddd9a80SXiaoming Ni 	}
1416*3ddd9a80SXiaoming Ni 	kmemleak_not_leak(hdr);
1417*3ddd9a80SXiaoming Ni }
1418*3ddd9a80SXiaoming Ni 
14196e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
14206e9d5164SEric W. Biederman {
14216e9d5164SEric W. Biederman 	int namelen;
14226e9d5164SEric W. Biederman 	namelen = strlen(name);
14236e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
14246e9d5164SEric W. Biederman 		return NULL;
14256e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
14266e9d5164SEric W. Biederman 	pos[namelen] = '/';
14276e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
14286e9d5164SEric W. Biederman 	pos += namelen + 1;
14296e9d5164SEric W. Biederman 	return pos;
14306e9d5164SEric W. Biederman }
14316e9d5164SEric W. Biederman 
1432f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1433f728019bSEric W. Biederman {
1434f728019bSEric W. Biederman 	int has_files = 0;
1435f728019bSEric W. Biederman 	int nr_subheaders = 0;
1436f728019bSEric W. Biederman 	struct ctl_table *entry;
1437f728019bSEric W. Biederman 
1438f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1439f728019bSEric W. Biederman 	if (!table || !table->procname)
1440f728019bSEric W. Biederman 		return 1;
1441f728019bSEric W. Biederman 
1442f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1443f728019bSEric W. Biederman 		if (entry->child)
1444f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1445f728019bSEric W. Biederman 		else
1446f728019bSEric W. Biederman 			has_files = 1;
1447f728019bSEric W. Biederman 	}
1448f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1449f728019bSEric W. Biederman }
1450f728019bSEric W. Biederman 
1451f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
145260a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1453f728019bSEric W. Biederman 	struct ctl_table *table)
1454f728019bSEric W. Biederman {
1455f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1456f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1457f728019bSEric W. Biederman 	int nr_files = 0;
1458f728019bSEric W. Biederman 	int nr_dirs = 0;
1459f728019bSEric W. Biederman 	int err = -ENOMEM;
1460f728019bSEric W. Biederman 
1461f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1462f728019bSEric W. Biederman 		if (entry->child)
1463f728019bSEric W. Biederman 			nr_dirs++;
1464f728019bSEric W. Biederman 		else
1465f728019bSEric W. Biederman 			nr_files++;
1466f728019bSEric W. Biederman 	}
1467f728019bSEric W. Biederman 
1468f728019bSEric W. Biederman 	files = table;
1469f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1470f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1471f728019bSEric W. Biederman 		struct ctl_table *new;
14726396bb22SKees Cook 		files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
1473f728019bSEric W. Biederman 				GFP_KERNEL);
1474f728019bSEric W. Biederman 		if (!files)
1475f728019bSEric W. Biederman 			goto out;
1476f728019bSEric W. Biederman 
1477f728019bSEric W. Biederman 		ctl_table_arg = files;
1478f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1479f728019bSEric W. Biederman 			if (entry->child)
1480f728019bSEric W. Biederman 				continue;
1481f728019bSEric W. Biederman 			*new = *entry;
1482f728019bSEric W. Biederman 			new++;
1483f728019bSEric W. Biederman 		}
1484f728019bSEric W. Biederman 	}
1485f728019bSEric W. Biederman 
1486f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1487f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1488f728019bSEric W. Biederman 		struct ctl_table_header *header;
148960a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1490f728019bSEric W. Biederman 		if (!header) {
1491f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1492f728019bSEric W. Biederman 			goto out;
1493f728019bSEric W. Biederman 		}
1494f728019bSEric W. Biederman 
1495f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1496f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1497f728019bSEric W. Biederman 		**subheader = header;
1498f728019bSEric W. Biederman 		(*subheader)++;
1499f728019bSEric W. Biederman 	}
1500f728019bSEric W. Biederman 
1501f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1502f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1503f728019bSEric W. Biederman 		char *child_pos;
1504f728019bSEric W. Biederman 
1505f728019bSEric W. Biederman 		if (!entry->child)
1506f728019bSEric W. Biederman 			continue;
1507f728019bSEric W. Biederman 
1508f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1509f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1510f728019bSEric W. Biederman 		if (!child_pos)
1511f728019bSEric W. Biederman 			goto out;
1512f728019bSEric W. Biederman 
1513f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
151460a47a2eSEric W. Biederman 						  set, entry->child);
1515f728019bSEric W. Biederman 		pos[0] = '\0';
1516f728019bSEric W. Biederman 		if (err)
1517f728019bSEric W. Biederman 			goto out;
1518f728019bSEric W. Biederman 	}
1519f728019bSEric W. Biederman 	err = 0;
1520f728019bSEric W. Biederman out:
1521f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1522f728019bSEric W. Biederman 	return err;
1523f728019bSEric W. Biederman }
1524f728019bSEric W. Biederman 
15256e9d5164SEric W. Biederman /**
15266e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
152760a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
15286e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
15296e9d5164SEric W. Biederman  * @table: the top-level table structure
15306e9d5164SEric W. Biederman  *
15316e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15326e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15336e9d5164SEric W. Biederman  *
15346e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
15356e9d5164SEric W. Biederman  */
15366e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
153760a47a2eSEric W. Biederman 	struct ctl_table_set *set,
15386e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
15396e9d5164SEric W. Biederman {
1540ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1541f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1542f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
15436e9d5164SEric W. Biederman 	const struct ctl_path *component;
15446e9d5164SEric W. Biederman 	char *new_path, *pos;
15456e9d5164SEric W. Biederman 
15466e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
15476e9d5164SEric W. Biederman 	if (!new_path)
15486e9d5164SEric W. Biederman 		return NULL;
15496e9d5164SEric W. Biederman 
15506e9d5164SEric W. Biederman 	pos[0] = '\0';
15516e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
15526e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
15536e9d5164SEric W. Biederman 		if (!pos)
15546e9d5164SEric W. Biederman 			goto out;
15556e9d5164SEric W. Biederman 	}
1556ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1557ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1558ec6a5266SEric W. Biederman 		if (!pos)
1559ec6a5266SEric W. Biederman 			goto out;
1560ec6a5266SEric W. Biederman 		table = table->child;
1561ec6a5266SEric W. Biederman 	}
1562f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
156360a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1564ec6a5266SEric W. Biederman 		if (header)
1565ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1566f728019bSEric W. Biederman 	} else {
1567f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1568f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1569f728019bSEric W. Biederman 		if (!header)
1570f728019bSEric W. Biederman 			goto out;
1571f728019bSEric W. Biederman 
1572f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1573f728019bSEric W. Biederman 		subheader = subheaders;
1574f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1575f728019bSEric W. Biederman 
1576f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
157760a47a2eSEric W. Biederman 						set, table))
1578f728019bSEric W. Biederman 			goto err_register_leaves;
1579f728019bSEric W. Biederman 	}
1580f728019bSEric W. Biederman 
15816e9d5164SEric W. Biederman out:
15826e9d5164SEric W. Biederman 	kfree(new_path);
15836e9d5164SEric W. Biederman 	return header;
1584f728019bSEric W. Biederman 
1585f728019bSEric W. Biederman err_register_leaves:
1586f728019bSEric W. Biederman 	while (subheader > subheaders) {
1587f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1588f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1589f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1590f728019bSEric W. Biederman 		kfree(table);
1591f728019bSEric W. Biederman 	}
1592f728019bSEric W. Biederman 	kfree(header);
1593f728019bSEric W. Biederman 	header = NULL;
1594f728019bSEric W. Biederman 	goto out;
15956e9d5164SEric W. Biederman }
15966e9d5164SEric W. Biederman 
15971f87f0b5SEric W. Biederman /**
15985b31a7dfSzhouchuangao  * register_sysctl_paths - register a sysctl table hierarchy
15991f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
16001f87f0b5SEric W. Biederman  * @table: the top-level table structure
16011f87f0b5SEric W. Biederman  *
16021f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16031f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16041f87f0b5SEric W. Biederman  *
16051f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
16061f87f0b5SEric W. Biederman  */
16071f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
16081f87f0b5SEric W. Biederman 						struct ctl_table *table)
16091f87f0b5SEric W. Biederman {
161060a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
16111f87f0b5SEric W. Biederman 					path, table);
16121f87f0b5SEric W. Biederman }
16131f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
16141f87f0b5SEric W. Biederman 
16151f87f0b5SEric W. Biederman /**
16161f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
16171f87f0b5SEric W. Biederman  * @table: the top-level table structure
16181f87f0b5SEric W. Biederman  *
16191f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
16201f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
16211f87f0b5SEric W. Biederman  *
16221f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
16231f87f0b5SEric W. Biederman  */
16241f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
16251f87f0b5SEric W. Biederman {
16261f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
16271f87f0b5SEric W. Biederman 
16281f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
16291f87f0b5SEric W. Biederman }
16301f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
16311f87f0b5SEric W. Biederman 
16320e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
16330e47c99dSEric W. Biederman {
16340e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
16350e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
16360e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
16370e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
16380e47c99dSEric W. Biederman 	struct ctl_table *entry;
16390e47c99dSEric W. Biederman 
16400e47c99dSEric W. Biederman 	if (header->set == root_set)
16410e47c99dSEric W. Biederman 		return;
16420e47c99dSEric W. Biederman 
16430e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
16440e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
16450e47c99dSEric W. Biederman 		return;
16460e47c99dSEric W. Biederman 
16470e47c99dSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
16480e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
16490e47c99dSEric W. Biederman 		struct ctl_table *link;
16500e47c99dSEric W. Biederman 		const char *name = entry->procname;
16510e47c99dSEric W. Biederman 
16520e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
16530e47c99dSEric W. Biederman 		if (link &&
16540e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
16550e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
16560e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
16570e47c99dSEric W. Biederman 		}
16580e47c99dSEric W. Biederman 		else {
165987ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
16600e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
1661153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", name);
16620e47c99dSEric W. Biederman 		}
16630e47c99dSEric W. Biederman 	}
16640e47c99dSEric W. Biederman }
16650e47c99dSEric W. Biederman 
1666938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1667938aaa4fSEric W. Biederman {
16687ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
16697ec66d06SEric W. Biederman 
1670938aaa4fSEric W. Biederman 	if (--header->nreg)
1671938aaa4fSEric W. Biederman 		return;
1672938aaa4fSEric W. Biederman 
167389189557SYueHaibing 	if (parent) {
16740e47c99dSEric W. Biederman 		put_links(header);
1675938aaa4fSEric W. Biederman 		start_unregistering(header);
167689189557SYueHaibing 	}
167789189557SYueHaibing 
1678938aaa4fSEric W. Biederman 	if (!--header->count)
1679938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
16807ec66d06SEric W. Biederman 
16817ec66d06SEric W. Biederman 	if (parent)
16827ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1683938aaa4fSEric W. Biederman }
1684938aaa4fSEric W. Biederman 
16851f87f0b5SEric W. Biederman /**
16861f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
16871f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
16881f87f0b5SEric W. Biederman  *
16891f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
16901f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
16911f87f0b5SEric W. Biederman  */
16921f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
16931f87f0b5SEric W. Biederman {
1694f728019bSEric W. Biederman 	int nr_subheaders;
16951f87f0b5SEric W. Biederman 	might_sleep();
16961f87f0b5SEric W. Biederman 
16971f87f0b5SEric W. Biederman 	if (header == NULL)
16981f87f0b5SEric W. Biederman 		return;
16991f87f0b5SEric W. Biederman 
1700f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1701f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1702f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1703f728019bSEric W. Biederman 		int i;
1704f728019bSEric W. Biederman 
1705f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1706f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1707f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1708f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1709f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1710f728019bSEric W. Biederman 			kfree(table);
1711f728019bSEric W. Biederman 		}
1712f728019bSEric W. Biederman 		kfree(header);
1713f728019bSEric W. Biederman 		return;
1714f728019bSEric W. Biederman 	}
1715f728019bSEric W. Biederman 
17161f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1717938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
17181f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
17191f87f0b5SEric W. Biederman }
17201f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
17211f87f0b5SEric W. Biederman 
17220e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
17239eb47c26SEric W. Biederman 	struct ctl_table_root *root,
17241f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
17251f87f0b5SEric W. Biederman {
17261347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
17270e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1728ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
17291f87f0b5SEric W. Biederman }
17301f87f0b5SEric W. Biederman 
173197324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
173297324cd8SEric W. Biederman {
1733ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
173497324cd8SEric W. Biederman }
17351f87f0b5SEric W. Biederman 
17361e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
173777b14db5SEric W. Biederman {
1738e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1739e1675231SAlexey Dobriyan 
174077b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
17419043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
1742d56c0d45SAlexey Dobriyan 	proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
174377b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1744de4e83bdSEric W. Biederman 
1745de4e83bdSEric W. Biederman 	return sysctl_init();
174677b14db5SEric W. Biederman }
17473db978d4SVlastimil Babka 
17480a477e1aSVlastimil Babka struct sysctl_alias {
17490a477e1aSVlastimil Babka 	const char *kernel_param;
17500a477e1aSVlastimil Babka 	const char *sysctl_param;
17510a477e1aSVlastimil Babka };
17520a477e1aSVlastimil Babka 
17530a477e1aSVlastimil Babka /*
17540a477e1aSVlastimil Babka  * Historically some settings had both sysctl and a command line parameter.
17550a477e1aSVlastimil Babka  * With the generic sysctl. parameter support, we can handle them at a single
17560a477e1aSVlastimil Babka  * place and only keep the historical name for compatibility. This is not meant
17570a477e1aSVlastimil Babka  * to add brand new aliases. When adding existing aliases, consider whether
17580a477e1aSVlastimil Babka  * the possibly different moment of changing the value (e.g. from early_param
17590a477e1aSVlastimil Babka  * to the moment do_sysctl_args() is called) is an issue for the specific
17600a477e1aSVlastimil Babka  * parameter.
17610a477e1aSVlastimil Babka  */
17620a477e1aSVlastimil Babka static const struct sysctl_alias sysctl_aliases[] = {
1763f117955aSGuilherme G. Piccoli 	{"hardlockup_all_cpu_backtrace",	"kernel.hardlockup_all_cpu_backtrace" },
1764b467f3efSVlastimil Babka 	{"hung_task_panic",			"kernel.hung_task_panic" },
1765f117955aSGuilherme G. Piccoli 	{"numa_zonelist_order",			"vm.numa_zonelist_order" },
1766f117955aSGuilherme G. Piccoli 	{"softlockup_all_cpu_backtrace",	"kernel.softlockup_all_cpu_backtrace" },
1767f117955aSGuilherme G. Piccoli 	{"softlockup_panic",			"kernel.softlockup_panic" },
17680a477e1aSVlastimil Babka 	{ }
17690a477e1aSVlastimil Babka };
17700a477e1aSVlastimil Babka 
17710a477e1aSVlastimil Babka static const char *sysctl_find_alias(char *param)
17720a477e1aSVlastimil Babka {
17730a477e1aSVlastimil Babka 	const struct sysctl_alias *alias;
17740a477e1aSVlastimil Babka 
17750a477e1aSVlastimil Babka 	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
17760a477e1aSVlastimil Babka 		if (strcmp(alias->kernel_param, param) == 0)
17770a477e1aSVlastimil Babka 			return alias->sysctl_param;
17780a477e1aSVlastimil Babka 	}
17790a477e1aSVlastimil Babka 
17800a477e1aSVlastimil Babka 	return NULL;
17810a477e1aSVlastimil Babka }
17820a477e1aSVlastimil Babka 
17833db978d4SVlastimil Babka /* Set sysctl value passed on kernel command line. */
17843db978d4SVlastimil Babka static int process_sysctl_arg(char *param, char *val,
17853db978d4SVlastimil Babka 			       const char *unused, void *arg)
17863db978d4SVlastimil Babka {
17873db978d4SVlastimil Babka 	char *path;
17883db978d4SVlastimil Babka 	struct vfsmount **proc_mnt = arg;
17893db978d4SVlastimil Babka 	struct file_system_type *proc_fs_type;
17903db978d4SVlastimil Babka 	struct file *file;
17913db978d4SVlastimil Babka 	int len;
17923db978d4SVlastimil Babka 	int err;
17933db978d4SVlastimil Babka 	loff_t pos = 0;
17943db978d4SVlastimil Babka 	ssize_t wret;
17953db978d4SVlastimil Babka 
17960a477e1aSVlastimil Babka 	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
17973db978d4SVlastimil Babka 		param += sizeof("sysctl") - 1;
17983db978d4SVlastimil Babka 
17993db978d4SVlastimil Babka 		if (param[0] != '/' && param[0] != '.')
18003db978d4SVlastimil Babka 			return 0;
18013db978d4SVlastimil Babka 
18023db978d4SVlastimil Babka 		param++;
18030a477e1aSVlastimil Babka 	} else {
18040a477e1aSVlastimil Babka 		param = (char *) sysctl_find_alias(param);
18050a477e1aSVlastimil Babka 		if (!param)
18060a477e1aSVlastimil Babka 			return 0;
18070a477e1aSVlastimil Babka 	}
18083db978d4SVlastimil Babka 
1809697edcb0SXiaoming Ni 	if (!val)
1810697edcb0SXiaoming Ni 		return -EINVAL;
1811697edcb0SXiaoming Ni 	len = strlen(val);
1812697edcb0SXiaoming Ni 	if (len == 0)
1813697edcb0SXiaoming Ni 		return -EINVAL;
1814697edcb0SXiaoming Ni 
18153db978d4SVlastimil Babka 	/*
18163db978d4SVlastimil Babka 	 * To set sysctl options, we use a temporary mount of proc, look up the
18173db978d4SVlastimil Babka 	 * respective sys/ file and write to it. To avoid mounting it when no
18183db978d4SVlastimil Babka 	 * options were given, we mount it only when the first sysctl option is
18193db978d4SVlastimil Babka 	 * found. Why not a persistent mount? There are problems with a
18203db978d4SVlastimil Babka 	 * persistent mount of proc in that it forces userspace not to use any
18213db978d4SVlastimil Babka 	 * proc mount options.
18223db978d4SVlastimil Babka 	 */
18233db978d4SVlastimil Babka 	if (!*proc_mnt) {
18243db978d4SVlastimil Babka 		proc_fs_type = get_fs_type("proc");
18253db978d4SVlastimil Babka 		if (!proc_fs_type) {
18263db978d4SVlastimil Babka 			pr_err("Failed to find procfs to set sysctl from command line\n");
18273db978d4SVlastimil Babka 			return 0;
18283db978d4SVlastimil Babka 		}
18293db978d4SVlastimil Babka 		*proc_mnt = kern_mount(proc_fs_type);
18303db978d4SVlastimil Babka 		put_filesystem(proc_fs_type);
18313db978d4SVlastimil Babka 		if (IS_ERR(*proc_mnt)) {
18323db978d4SVlastimil Babka 			pr_err("Failed to mount procfs to set sysctl from command line\n");
18333db978d4SVlastimil Babka 			return 0;
18343db978d4SVlastimil Babka 		}
18353db978d4SVlastimil Babka 	}
18363db978d4SVlastimil Babka 
18373db978d4SVlastimil Babka 	path = kasprintf(GFP_KERNEL, "sys/%s", param);
18383db978d4SVlastimil Babka 	if (!path)
18393db978d4SVlastimil Babka 		panic("%s: Failed to allocate path for %s\n", __func__, param);
18403db978d4SVlastimil Babka 	strreplace(path, '.', '/');
18413db978d4SVlastimil Babka 
1842ffb37ca3SAl Viro 	file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
18433db978d4SVlastimil Babka 	if (IS_ERR(file)) {
18443db978d4SVlastimil Babka 		err = PTR_ERR(file);
18453db978d4SVlastimil Babka 		if (err == -ENOENT)
18463db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
18473db978d4SVlastimil Babka 				param, val);
18483db978d4SVlastimil Babka 		else if (err == -EACCES)
18493db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
18503db978d4SVlastimil Babka 				param, val);
18513db978d4SVlastimil Babka 		else
18523db978d4SVlastimil Babka 			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
18533db978d4SVlastimil Babka 				file, param, val);
18543db978d4SVlastimil Babka 		goto out;
18553db978d4SVlastimil Babka 	}
18563db978d4SVlastimil Babka 	wret = kernel_write(file, val, len, &pos);
18573db978d4SVlastimil Babka 	if (wret < 0) {
18583db978d4SVlastimil Babka 		err = wret;
18593db978d4SVlastimil Babka 		if (err == -EINVAL)
18603db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
18613db978d4SVlastimil Babka 				param, val);
18623db978d4SVlastimil Babka 		else
18633db978d4SVlastimil Babka 			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
18643db978d4SVlastimil Babka 				ERR_PTR(err), param, val);
18653db978d4SVlastimil Babka 	} else if (wret != len) {
18663db978d4SVlastimil Babka 		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
18673db978d4SVlastimil Babka 			wret, len, path, param, val);
18683db978d4SVlastimil Babka 	}
18693db978d4SVlastimil Babka 
18703db978d4SVlastimil Babka 	err = filp_close(file, NULL);
18713db978d4SVlastimil Babka 	if (err)
18723db978d4SVlastimil Babka 		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
18733db978d4SVlastimil Babka 			ERR_PTR(err), param, val);
18743db978d4SVlastimil Babka out:
18753db978d4SVlastimil Babka 	kfree(path);
18763db978d4SVlastimil Babka 	return 0;
18773db978d4SVlastimil Babka }
18783db978d4SVlastimil Babka 
18793db978d4SVlastimil Babka void do_sysctl_args(void)
18803db978d4SVlastimil Babka {
18813db978d4SVlastimil Babka 	char *command_line;
18823db978d4SVlastimil Babka 	struct vfsmount *proc_mnt = NULL;
18833db978d4SVlastimil Babka 
18843db978d4SVlastimil Babka 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
18853db978d4SVlastimil Babka 	if (!command_line)
18863db978d4SVlastimil Babka 		panic("%s: Failed to allocate copy of command line\n", __func__);
18873db978d4SVlastimil Babka 
18883db978d4SVlastimil Babka 	parse_args("Setting sysctl args", command_line,
18893db978d4SVlastimil Babka 		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
18903db978d4SVlastimil Babka 
18913db978d4SVlastimil Babka 	if (proc_mnt)
18923db978d4SVlastimil Babka 		kern_unmount(proc_mnt);
18933db978d4SVlastimil Babka 
18943db978d4SVlastimil Babka 	kfree(command_line);
18953db978d4SVlastimil Babka }
1896