xref: /linux-6.15/fs/proc/proc_sysctl.c (revision 5be1fa8a)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
277b14db5SEric W. Biederman /*
377b14db5SEric W. Biederman  * /proc/sys support
477b14db5SEric W. Biederman  */
51e0edd3fSAlexey Dobriyan #include <linux/init.h>
677b14db5SEric W. Biederman #include <linux/sysctl.h>
7f1ecf068SLucas De Marchi #include <linux/poll.h>
877b14db5SEric W. Biederman #include <linux/proc_fs.h>
987ebdc00SAndrew Morton #include <linux/printk.h>
1077b14db5SEric W. Biederman #include <linux/security.h>
1140401530SAl Viro #include <linux/sched.h>
125b825c3aSIngo Molnar #include <linux/cred.h>
1334286d66SNick Piggin #include <linux/namei.h>
1440401530SAl Viro #include <linux/mm.h>
154bd6a735SMatthew Wilcox (Oracle) #include <linux/uio.h>
161f87f0b5SEric W. Biederman #include <linux/module.h>
177b146cebSAndrey Ignatov #include <linux/bpf-cgroup.h>
183db978d4SVlastimil Babka #include <linux/mount.h>
193ddd9a80SXiaoming Ni #include <linux/kmemleak.h>
208e666244SThomas Weißschuh #include <linux/lockdep.h>
2177b14db5SEric W. Biederman #include "internal.h"
2277b14db5SEric W. Biederman 
2318d4b42eSJoel Granados #define list_for_each_table_entry(entry, header)	\
2453f3811dSJoel Granados 	entry = header->ctl_table;			\
25d7a76ec8SJoel Granados 	for (size_t i = 0 ; i < header->ctl_table_size; ++i, entry++)
26cb55f27aSMeng Tang 
27d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
2877b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
2903a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
309043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
319043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
3277b14db5SEric W. Biederman 
33559d4c6aSThomas Weißschuh /*
34559d4c6aSThomas Weißschuh  * Support for permanently empty directories.
35559d4c6aSThomas Weißschuh  * Must be non-empty to avoid sharing an address with other tables.
36559d4c6aSThomas Weißschuh  */
37fbc26ee7SThomas Weißschuh static const struct ctl_table sysctl_mount_point[] = {
38559d4c6aSThomas Weißschuh 	{ }
39559d4c6aSThomas Weißschuh };
40f9bd6733SEric W. Biederman 
41ee9efac4SLuis Chamberlain /**
42ee9efac4SLuis Chamberlain  * register_sysctl_mount_point() - registers a sysctl mount point
43ee9efac4SLuis Chamberlain  * @path: path for the mount point
44ee9efac4SLuis Chamberlain  *
45ee9efac4SLuis Chamberlain  * Used to create a permanently empty directory to serve as mount point.
46ee9efac4SLuis Chamberlain  * There are some subtle but important permission checks this allows in the
47ee9efac4SLuis Chamberlain  * case of unprivileged mounts.
48ee9efac4SLuis Chamberlain  */
register_sysctl_mount_point(const char * path)49ee9efac4SLuis Chamberlain struct ctl_table_header *register_sysctl_mount_point(const char *path)
50ee9efac4SLuis Chamberlain {
51559d4c6aSThomas Weißschuh 	return register_sysctl_sz(path, sysctl_mount_point, 0);
52ee9efac4SLuis Chamberlain }
53ee9efac4SLuis Chamberlain EXPORT_SYMBOL(register_sysctl_mount_point);
54ee9efac4SLuis Chamberlain 
552f2665c1SJoel Granados #define sysctl_is_perm_empty_ctl_header(hptr)		\
564a7b29f6SThomas Weißschuh 	(hptr->type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
572f2665c1SJoel Granados #define sysctl_set_perm_empty_ctl_header(hptr)		\
584a7b29f6SThomas Weißschuh 	(hptr->type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
592f2665c1SJoel Granados #define sysctl_clear_perm_empty_ctl_header(hptr)	\
604a7b29f6SThomas Weißschuh 	(hptr->type = SYSCTL_TABLE_TYPE_DEFAULT)
61f9bd6733SEric W. Biederman 
proc_sys_poll_notify(struct ctl_table_poll * poll)62f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
63f1ecf068SLucas De Marchi {
64f1ecf068SLucas De Marchi 	if (!poll)
65f1ecf068SLucas De Marchi 		return;
66f1ecf068SLucas De Marchi 
67f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
68f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
69f1ecf068SLucas De Marchi }
70f1ecf068SLucas De Marchi 
71fbc26ee7SThomas Weißschuh static const struct ctl_table root_table[] = {
72a194558eSEric W. Biederman 	{
73a194558eSEric W. Biederman 		.procname = "",
747ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
75a194558eSEric W. Biederman 	},
76a194558eSEric W. Biederman };
770e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
780e47c99dSEric W. Biederman 	.default_set.dir.header = {
791f87f0b5SEric W. Biederman 		{{.count = 1,
80938aaa4fSEric W. Biederman 		  .nreg = 1,
81ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
820e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
831f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
841f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
857ec66d06SEric W. Biederman 	},
861f87f0b5SEric W. Biederman };
871f87f0b5SEric W. Biederman 
881f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
891f87f0b5SEric W. Biederman 
907ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
910e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
9229e1095bSThomas Weißschuh 	const struct ctl_table **pentry);
930e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
940e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
957ec66d06SEric W. Biederman 
sysctl_print_dir(struct ctl_dir * dir)966980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
976980128fSEric W. Biederman {
986980128fSEric W. Biederman 	if (dir->header.parent)
996980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
10087ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
1016980128fSEric W. Biederman }
1026980128fSEric W. Biederman 
namecmp(const char * name1,int len1,const char * name2,int len2)103076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
104076c3eedSEric W. Biederman {
105076c3eedSEric W. Biederman 	int cmp;
106076c3eedSEric W. Biederman 
107d8fc9b66SMasahiro Yamada 	cmp = memcmp(name1, name2, min(len1, len2));
108076c3eedSEric W. Biederman 	if (cmp == 0)
109076c3eedSEric W. Biederman 		cmp = len1 - len2;
110076c3eedSEric W. Biederman 	return cmp;
111076c3eedSEric W. Biederman }
112076c3eedSEric W. Biederman 
find_entry(struct ctl_table_header ** phead,struct ctl_dir * dir,const char * name,int namelen)11329e1095bSThomas Weißschuh static const struct ctl_table *find_entry(struct ctl_table_header **phead,
1140e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
115076c3eedSEric W. Biederman {
116076c3eedSEric W. Biederman 	struct ctl_table_header *head;
11729e1095bSThomas Weißschuh 	const struct ctl_table *entry;
118ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
119076c3eedSEric W. Biederman 
1208e666244SThomas Weißschuh 	lockdep_assert_held(&sysctl_lock);
1218e666244SThomas Weißschuh 
122ac13ac6fSEric W. Biederman 	while (node)
123ac13ac6fSEric W. Biederman 	{
124ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
125ac13ac6fSEric W. Biederman 		const char *procname;
126ac13ac6fSEric W. Biederman 		int cmp;
127ac13ac6fSEric W. Biederman 
128ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
129ac13ac6fSEric W. Biederman 		head = ctl_node->header;
130ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
131ac13ac6fSEric W. Biederman 		procname = entry->procname;
132ac13ac6fSEric W. Biederman 
133ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
134ac13ac6fSEric W. Biederman 		if (cmp < 0)
135ac13ac6fSEric W. Biederman 			node = node->rb_left;
136ac13ac6fSEric W. Biederman 		else if (cmp > 0)
137ac13ac6fSEric W. Biederman 			node = node->rb_right;
138ac13ac6fSEric W. Biederman 		else {
139076c3eedSEric W. Biederman 			*phead = head;
140076c3eedSEric W. Biederman 			return entry;
141076c3eedSEric W. Biederman 		}
142076c3eedSEric W. Biederman 	}
143076c3eedSEric W. Biederman 	return NULL;
144076c3eedSEric W. Biederman }
145076c3eedSEric W. Biederman 
insert_entry(struct ctl_table_header * head,const struct ctl_table * entry)14629e1095bSThomas Weißschuh static int insert_entry(struct ctl_table_header *head, const struct ctl_table *entry)
147ac13ac6fSEric W. Biederman {
148ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
149ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
150ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
151ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
152ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
153ac13ac6fSEric W. Biederman 
154ac13ac6fSEric W. Biederman 	while (*p) {
155ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
15629e1095bSThomas Weißschuh 		const struct ctl_table *parent_entry;
157ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
158ac13ac6fSEric W. Biederman 		const char *parent_name;
159ac13ac6fSEric W. Biederman 		int cmp;
160ac13ac6fSEric W. Biederman 
161ac13ac6fSEric W. Biederman 		parent = *p;
162ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
163ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
164ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
165ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
166ac13ac6fSEric W. Biederman 
167ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
168ac13ac6fSEric W. Biederman 		if (cmp < 0)
169ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
170ac13ac6fSEric W. Biederman 		else if (cmp > 0)
171ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
172ac13ac6fSEric W. Biederman 		else {
17387ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
174ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
175153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", entry->procname);
176ac13ac6fSEric W. Biederman 			return -EEXIST;
177ac13ac6fSEric W. Biederman 		}
178ac13ac6fSEric W. Biederman 	}
179ac13ac6fSEric W. Biederman 
180ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
181ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
182ac13ac6fSEric W. Biederman 	return 0;
183ac13ac6fSEric W. Biederman }
184ac13ac6fSEric W. Biederman 
erase_entry(struct ctl_table_header * head,const struct ctl_table * entry)18529e1095bSThomas Weißschuh static void erase_entry(struct ctl_table_header *head, const struct ctl_table *entry)
186ac13ac6fSEric W. Biederman {
187ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
188ac13ac6fSEric W. Biederman 
189ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
190ac13ac6fSEric W. Biederman }
191ac13ac6fSEric W. Biederman 
init_header(struct ctl_table_header * head,struct ctl_table_root * root,struct ctl_table_set * set,struct ctl_node * node,const struct ctl_table * table,size_t table_size)192e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
193e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
19429e1095bSThomas Weißschuh 	struct ctl_node *node, const struct ctl_table *table, size_t table_size)
195e0d04529SEric W. Biederman {
1967ec66d06SEric W. Biederman 	head->ctl_table = table;
197b1f01e2bSJoel Granados 	head->ctl_table_size = table_size;
198e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
199e0d04529SEric W. Biederman 	head->used = 0;
200e0d04529SEric W. Biederman 	head->count = 1;
201e0d04529SEric W. Biederman 	head->nreg = 1;
202e0d04529SEric W. Biederman 	head->unregistering = NULL;
203e0d04529SEric W. Biederman 	head->root = root;
204e0d04529SEric W. Biederman 	head->set = set;
205e0d04529SEric W. Biederman 	head->parent = NULL;
206ac13ac6fSEric W. Biederman 	head->node = node;
2072fd1d2c4SEric W. Biederman 	INIT_HLIST_HEAD(&head->inodes);
208ac13ac6fSEric W. Biederman 	if (node) {
20929e1095bSThomas Weißschuh 		const struct ctl_table *entry;
210cb55f27aSMeng Tang 
21118d4b42eSJoel Granados 		list_for_each_table_entry(entry, head) {
212ac13ac6fSEric W. Biederman 			node->header = head;
213cb55f27aSMeng Tang 			node++;
214cb55f27aSMeng Tang 		}
215ac13ac6fSEric W. Biederman 	}
2164a7b29f6SThomas Weißschuh 	if (table == sysctl_mount_point)
2174a7b29f6SThomas Weißschuh 		sysctl_set_perm_empty_ctl_header(head);
218ac13ac6fSEric W. Biederman }
219e0d04529SEric W. Biederman 
erase_header(struct ctl_table_header * head)2208425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2218425d6aaSEric W. Biederman {
22229e1095bSThomas Weißschuh 	const struct ctl_table *entry;
223cb55f27aSMeng Tang 
22418d4b42eSJoel Granados 	list_for_each_table_entry(entry, head)
225ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2268425d6aaSEric W. Biederman }
2278425d6aaSEric W. Biederman 
insert_header(struct ctl_dir * dir,struct ctl_table_header * header)2280e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2298425d6aaSEric W. Biederman {
23029e1095bSThomas Weißschuh 	const struct ctl_table *entry;
2312f2665c1SJoel Granados 	struct ctl_table_header *dir_h = &dir->header;
2320e47c99dSEric W. Biederman 	int err;
2330e47c99dSEric W. Biederman 
2342f2665c1SJoel Granados 
235f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
2362f2665c1SJoel Granados 	if (sysctl_is_perm_empty_ctl_header(dir_h))
237f9bd6733SEric W. Biederman 		return -EROFS;
238f9bd6733SEric W. Biederman 
239f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
240a35dd3a7SThomas Weißschuh 	if (sysctl_is_perm_empty_ctl_header(header)) {
241f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
242f9bd6733SEric W. Biederman 			return -EINVAL;
2432f2665c1SJoel Granados 		sysctl_set_perm_empty_ctl_header(dir_h);
244f9bd6733SEric W. Biederman 	}
245f9bd6733SEric W. Biederman 
2462f2665c1SJoel Granados 	dir_h->nreg++;
2477ec66d06SEric W. Biederman 	header->parent = dir;
2480e47c99dSEric W. Biederman 	err = insert_links(header);
2490e47c99dSEric W. Biederman 	if (err)
2500e47c99dSEric W. Biederman 		goto fail_links;
25118d4b42eSJoel Granados 	list_for_each_table_entry(entry, header) {
252ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
253ac13ac6fSEric W. Biederman 		if (err)
254ac13ac6fSEric W. Biederman 			goto fail;
255ac13ac6fSEric W. Biederman 	}
2560e47c99dSEric W. Biederman 	return 0;
257ac13ac6fSEric W. Biederman fail:
258ac13ac6fSEric W. Biederman 	erase_header(header);
259ac13ac6fSEric W. Biederman 	put_links(header);
2600e47c99dSEric W. Biederman fail_links:
261f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
2622f2665c1SJoel Granados 		sysctl_clear_perm_empty_ctl_header(dir_h);
2630e47c99dSEric W. Biederman 	header->parent = NULL;
2642f2665c1SJoel Granados 	drop_sysctl_table(dir_h);
2650e47c99dSEric W. Biederman 	return err;
2668425d6aaSEric W. Biederman }
2678425d6aaSEric W. Biederman 
use_table(struct ctl_table_header * p)2681f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2691f87f0b5SEric W. Biederman {
2708e666244SThomas Weißschuh 	lockdep_assert_held(&sysctl_lock);
2718e666244SThomas Weißschuh 
2721f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2731f87f0b5SEric W. Biederman 		return 0;
2741f87f0b5SEric W. Biederman 	p->used++;
2751f87f0b5SEric W. Biederman 	return 1;
2761f87f0b5SEric W. Biederman }
2771f87f0b5SEric W. Biederman 
unuse_table(struct ctl_table_header * p)2781f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2791f87f0b5SEric W. Biederman {
2808e666244SThomas Weißschuh 	lockdep_assert_held(&sysctl_lock);
2818e666244SThomas Weißschuh 
2821f87f0b5SEric W. Biederman 	if (!--p->used)
2831f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2841f87f0b5SEric W. Biederman 			complete(p->unregistering);
2851f87f0b5SEric W. Biederman }
2861f87f0b5SEric W. Biederman 
proc_sys_invalidate_dcache(struct ctl_table_header * head)287f90f3cafSEric W. Biederman static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
288d6cffbbeSKonstantin Khlebnikov {
289f90f3cafSEric W. Biederman 	proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
290d6cffbbeSKonstantin Khlebnikov }
291d6cffbbeSKonstantin Khlebnikov 
start_unregistering(struct ctl_table_header * p)2921f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
2931f87f0b5SEric W. Biederman {
2948e666244SThomas Weißschuh 	/* will reacquire if has to wait */
2958e666244SThomas Weißschuh 	lockdep_assert_held(&sysctl_lock);
2968e666244SThomas Weißschuh 
2971f87f0b5SEric W. Biederman 	/*
2981f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
2991f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
3001f87f0b5SEric W. Biederman 	 */
3011f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
3021f87f0b5SEric W. Biederman 		struct completion wait;
3031f87f0b5SEric W. Biederman 		init_completion(&wait);
3041f87f0b5SEric W. Biederman 		p->unregistering = &wait;
3051f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
3061f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
3071f87f0b5SEric W. Biederman 	} else {
3081f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
3091f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
310ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
3111f87f0b5SEric W. Biederman 	}
3121f87f0b5SEric W. Biederman 	/*
313f90f3cafSEric W. Biederman 	 * Invalidate dentries for unregistered sysctls: namespaced sysctls
314d6cffbbeSKonstantin Khlebnikov 	 * can have duplicate names and contaminate dcache very badly.
315d6cffbbeSKonstantin Khlebnikov 	 */
316f90f3cafSEric W. Biederman 	proc_sys_invalidate_dcache(p);
317d6cffbbeSKonstantin Khlebnikov 	/*
3181f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
3191f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
3201f87f0b5SEric W. Biederman 	 */
321ace0c791SEric W. Biederman 	spin_lock(&sysctl_lock);
3228425d6aaSEric W. Biederman 	erase_header(p);
3231f87f0b5SEric W. Biederman }
3241f87f0b5SEric W. Biederman 
sysctl_head_grab(struct ctl_table_header * head)3251f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3261f87f0b5SEric W. Biederman {
327ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3281f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3291f87f0b5SEric W. Biederman 	if (!use_table(head))
3301f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3311f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3321f87f0b5SEric W. Biederman 	return head;
3331f87f0b5SEric W. Biederman }
3341f87f0b5SEric W. Biederman 
sysctl_head_finish(struct ctl_table_header * head)3351f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3361f87f0b5SEric W. Biederman {
3371f87f0b5SEric W. Biederman 	if (!head)
3381f87f0b5SEric W. Biederman 		return;
3391f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3401f87f0b5SEric W. Biederman 	unuse_table(head);
3411f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3421f87f0b5SEric W. Biederman }
3431f87f0b5SEric W. Biederman 
3441f87f0b5SEric W. Biederman static struct ctl_table_set *
lookup_header_set(struct ctl_table_root * root)34513bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root)
3461f87f0b5SEric W. Biederman {
3471f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3481f87f0b5SEric W. Biederman 	if (root->lookup)
34913bcc6a2SEric W. Biederman 		set = root->lookup(root);
3501f87f0b5SEric W. Biederman 	return set;
3511f87f0b5SEric W. Biederman }
3521f87f0b5SEric W. Biederman 
lookup_entry(struct ctl_table_header ** phead,struct ctl_dir * dir,const char * name,int namelen)35329e1095bSThomas Weißschuh static const struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3547ec66d06SEric W. Biederman 					    struct ctl_dir *dir,
355076c3eedSEric W. Biederman 					    const char *name, int namelen)
356076c3eedSEric W. Biederman {
357076c3eedSEric W. Biederman 	struct ctl_table_header *head;
35829e1095bSThomas Weißschuh 	const struct ctl_table *entry;
359076c3eedSEric W. Biederman 
360076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3610e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
362076c3eedSEric W. Biederman 	if (entry && use_table(head))
363076c3eedSEric W. Biederman 		*phead = head;
364076c3eedSEric W. Biederman 	else
365076c3eedSEric W. Biederman 		entry = NULL;
366076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
367076c3eedSEric W. Biederman 	return entry;
368076c3eedSEric W. Biederman }
369076c3eedSEric W. Biederman 
first_usable_entry(struct rb_node * node)370ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3711f87f0b5SEric W. Biederman {
372ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3731f87f0b5SEric W. Biederman 
374ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
375ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
376ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
377ac13ac6fSEric W. Biederman 			return ctl_node;
3781f87f0b5SEric W. Biederman 	}
3791f87f0b5SEric W. Biederman 	return NULL;
3801f87f0b5SEric W. Biederman }
3811f87f0b5SEric W. Biederman 
first_entry(struct ctl_dir * dir,struct ctl_table_header ** phead,const struct ctl_table ** pentry)3827ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
38329e1095bSThomas Weißschuh 	struct ctl_table_header **phead, const struct ctl_table **pentry)
3841f87f0b5SEric W. Biederman {
385ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
38629e1095bSThomas Weißschuh 	const struct ctl_table *entry = NULL;
387ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3886a75ce16SEric W. Biederman 
3896a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
390ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
3916a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
392ac13ac6fSEric W. Biederman 	if (ctl_node) {
393ac13ac6fSEric W. Biederman 		head = ctl_node->header;
394ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
395ac13ac6fSEric W. Biederman 	}
3966a75ce16SEric W. Biederman 	*phead = head;
3976a75ce16SEric W. Biederman 	*pentry = entry;
3986a75ce16SEric W. Biederman }
3996a75ce16SEric W. Biederman 
next_entry(struct ctl_table_header ** phead,const struct ctl_table ** pentry)40029e1095bSThomas Weißschuh static void next_entry(struct ctl_table_header **phead, const struct ctl_table **pentry)
4016a75ce16SEric W. Biederman {
4026a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
40329e1095bSThomas Weißschuh 	const struct ctl_table *entry = *pentry;
404ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
4056a75ce16SEric W. Biederman 
4066a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
4076a75ce16SEric W. Biederman 	unuse_table(head);
408ac13ac6fSEric W. Biederman 
409ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
4106a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
411ac13ac6fSEric W. Biederman 	head = NULL;
412ac13ac6fSEric W. Biederman 	if (ctl_node) {
413ac13ac6fSEric W. Biederman 		head = ctl_node->header;
414ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
4156a75ce16SEric W. Biederman 	}
4166a75ce16SEric W. Biederman 	*phead = head;
4176a75ce16SEric W. Biederman 	*pentry = entry;
4181f87f0b5SEric W. Biederman }
4191f87f0b5SEric W. Biederman 
4201f87f0b5SEric W. Biederman /*
4211f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4221f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4231f87f0b5SEric W. Biederman  */
4241f87f0b5SEric W. Biederman 
test_perm(int mode,int op)4251f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4261f87f0b5SEric W. Biederman {
427091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4281f87f0b5SEric W. Biederman 		mode >>= 6;
429091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4301f87f0b5SEric W. Biederman 		mode >>= 3;
4311f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4321f87f0b5SEric W. Biederman 		return 0;
4331f87f0b5SEric W. Biederman 	return -EACCES;
4341f87f0b5SEric W. Biederman }
4351f87f0b5SEric W. Biederman 
sysctl_perm(struct ctl_table_header * head,const struct ctl_table * table,int op)43629e1095bSThomas Weißschuh static int sysctl_perm(struct ctl_table_header *head, const struct ctl_table *table, int op)
4371f87f0b5SEric W. Biederman {
43873f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4391f87f0b5SEric W. Biederman 	int mode;
4401f87f0b5SEric W. Biederman 
4411f87f0b5SEric W. Biederman 	if (root->permissions)
44273f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4431f87f0b5SEric W. Biederman 	else
4441f87f0b5SEric W. Biederman 		mode = table->mode;
4451f87f0b5SEric W. Biederman 
4461f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4471f87f0b5SEric W. Biederman }
4481f87f0b5SEric W. Biederman 
proc_sys_make_inode(struct super_block * sb,struct ctl_table_header * head,const struct ctl_table * table)4499043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
45029e1095bSThomas Weißschuh 		struct ctl_table_header *head, const struct ctl_table *table)
45177b14db5SEric W. Biederman {
452e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
45377b14db5SEric W. Biederman 	struct inode *inode;
4549043476fSAl Viro 	struct proc_inode *ei;
45577b14db5SEric W. Biederman 
4569043476fSAl Viro 	inode = new_inode(sb);
45777b14db5SEric W. Biederman 	if (!inode)
458ea5751ccSIvan Delalande 		return ERR_PTR(-ENOMEM);
45977b14db5SEric W. Biederman 
46085fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
46185fe4025SChristoph Hellwig 
46277b14db5SEric W. Biederman 	ei = PROC_I(inode);
4639043476fSAl Viro 
464d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
465ace0c791SEric W. Biederman 	if (unlikely(head->unregistering)) {
466ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
467ace0c791SEric W. Biederman 		iput(inode);
468ea5751ccSIvan Delalande 		return ERR_PTR(-ENOENT);
469ace0c791SEric W. Biederman 	}
470ace0c791SEric W. Biederman 	ei->sysctl = head;
471ace0c791SEric W. Biederman 	ei->sysctl_entry = table;
4720afa5ca8SEric W. Biederman 	hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
473d6cffbbeSKonstantin Khlebnikov 	head->count++;
474d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
475d6cffbbeSKonstantin Khlebnikov 
476200d9421SJeff Layton 	simple_inode_init_ts(inode);
4779043476fSAl Viro 	inode->i_mode = table->mode;
4787ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4799043476fSAl Viro 		inode->i_mode |= S_IFREG;
48077b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
48177b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4829043476fSAl Viro 	} else {
4839043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4849043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4859043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
4862f2665c1SJoel Granados 		if (sysctl_is_perm_empty_ctl_header(head))
487f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4889043476fSAl Viro 	}
489e79c6a4fSDmitry Torokhov 
4905ec27ec7SRadoslaw Burny 	inode->i_uid = GLOBAL_ROOT_UID;
4915ec27ec7SRadoslaw Burny 	inode->i_gid = GLOBAL_ROOT_GID;
49298ca62baSThomas Weißschuh 	if (root->set_ownership)
49398ca62baSThomas Weißschuh 		root->set_ownership(head, &inode->i_uid, &inode->i_gid);
494e79c6a4fSDmitry Torokhov 
49577b14db5SEric W. Biederman 	return inode;
49677b14db5SEric W. Biederman }
49777b14db5SEric W. Biederman 
proc_sys_evict_inode(struct inode * inode,struct ctl_table_header * head)498d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
499d6cffbbeSKonstantin Khlebnikov {
500d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
5010afa5ca8SEric W. Biederman 	hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
502d6cffbbeSKonstantin Khlebnikov 	if (!--head->count)
503d6cffbbeSKonstantin Khlebnikov 		kfree_rcu(head, rcu);
504d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
505d6cffbbeSKonstantin Khlebnikov }
506d6cffbbeSKonstantin Khlebnikov 
grab_header(struct inode * inode)50781324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
50877b14db5SEric W. Biederman {
5093cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
5103cc3e046SEric W. Biederman 	if (!head)
5110e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
5123cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
51377b14db5SEric W. Biederman }
51477b14db5SEric W. Biederman 
proc_sys_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)51577b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
51600cd8dd3SAl Viro 					unsigned int flags)
51777b14db5SEric W. Biederman {
5189043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
5199043476fSAl Viro 	struct ctl_table_header *h = NULL;
520dc12e909SAl Viro 	const struct qstr *name = &dentry->d_name;
52129e1095bSThomas Weißschuh 	const struct ctl_table *p;
52277b14db5SEric W. Biederman 	struct inode *inode;
5239043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
5247ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
5250e47c99dSEric W. Biederman 	int ret;
52677b14db5SEric W. Biederman 
5279043476fSAl Viro 	if (IS_ERR(head))
5289043476fSAl Viro 		return ERR_CAST(head);
5299043476fSAl Viro 
5307ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
5319043476fSAl Viro 
5327ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
5339043476fSAl Viro 	if (!p)
53477b14db5SEric W. Biederman 		goto out;
53577b14db5SEric W. Biederman 
5364e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
53713bcc6a2SEric W. Biederman 		ret = sysctl_follow_link(&h, &p);
5380e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5390e47c99dSEric W. Biederman 		if (ret)
5400e47c99dSEric W. Biederman 			goto out;
5414e757320SEric W. Biederman 	}
5420e47c99dSEric W. Biederman 
543fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
5441eae9a47SAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
545888e2b03SAl Viro 	err = d_splice_alias(inode, dentry);
54677b14db5SEric W. Biederman 
54777b14db5SEric W. Biederman out:
5486bf61045SFrancesco Ruggeri 	if (h)
5496bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
55077b14db5SEric W. Biederman 	sysctl_head_finish(head);
55177b14db5SEric W. Biederman 	return err;
55277b14db5SEric W. Biederman }
55377b14db5SEric W. Biederman 
proc_sys_call_handler(struct kiocb * iocb,struct iov_iter * iter,int write)5544bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
5554bd6a735SMatthew Wilcox (Oracle) 		int write)
55677b14db5SEric W. Biederman {
5574bd6a735SMatthew Wilcox (Oracle) 	struct inode *inode = file_inode(iocb->ki_filp);
5589043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
55929e1095bSThomas Weißschuh 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5604bd6a735SMatthew Wilcox (Oracle) 	size_t count = iov_iter_count(iter);
5614bd6a735SMatthew Wilcox (Oracle) 	char *kbuf;
5622a2da53bSDavid Howells 	ssize_t error;
56377b14db5SEric W. Biederman 
5649043476fSAl Viro 	if (IS_ERR(head))
5659043476fSAl Viro 		return PTR_ERR(head);
56677b14db5SEric W. Biederman 
56777b14db5SEric W. Biederman 	/*
56877b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
56977b14db5SEric W. Biederman 	 * and won't be until we finish.
57077b14db5SEric W. Biederman 	 */
57177b14db5SEric W. Biederman 	error = -EPERM;
57273f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
57377b14db5SEric W. Biederman 		goto out;
57477b14db5SEric W. Biederman 
5759043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5769043476fSAl Viro 	error = -EINVAL;
5779043476fSAl Viro 	if (!table->proc_handler)
5789043476fSAl Viro 		goto out;
5799043476fSAl Viro 
580ef9d965bSChristoph Hellwig 	/* don't even try if the size is too large */
581d4d80e69SMatthew Wilcox (Oracle) 	error = -ENOMEM;
582d4d80e69SMatthew Wilcox (Oracle) 	if (count >= KMALLOC_MAX_SIZE)
583d4d80e69SMatthew Wilcox (Oracle) 		goto out;
58445089437SJosef Bacik 	kbuf = kvzalloc(count + 1, GFP_KERNEL);
58532927393SChristoph Hellwig 	if (!kbuf)
58632927393SChristoph Hellwig 		goto out;
5874bd6a735SMatthew Wilcox (Oracle) 
5884bd6a735SMatthew Wilcox (Oracle) 	if (write) {
5894bd6a735SMatthew Wilcox (Oracle) 		error = -EFAULT;
5904bd6a735SMatthew Wilcox (Oracle) 		if (!copy_from_iter_full(kbuf, count, iter))
5914bd6a735SMatthew Wilcox (Oracle) 			goto out_free_buf;
5924bd6a735SMatthew Wilcox (Oracle) 		kbuf[count] = '\0';
5934e63acdfSAndrey Ignatov 	}
5944e63acdfSAndrey Ignatov 
59532927393SChristoph Hellwig 	error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
5964bd6a735SMatthew Wilcox (Oracle) 					   &iocb->ki_pos);
59732927393SChristoph Hellwig 	if (error)
59832927393SChristoph Hellwig 		goto out_free_buf;
59932927393SChristoph Hellwig 
60032927393SChristoph Hellwig 	/* careful: calling conventions are nasty here */
6014bd6a735SMatthew Wilcox (Oracle) 	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
60232927393SChristoph Hellwig 	if (error)
60332927393SChristoph Hellwig 		goto out_free_buf;
60432927393SChristoph Hellwig 
60532927393SChristoph Hellwig 	if (!write) {
60632927393SChristoph Hellwig 		error = -EFAULT;
6074bd6a735SMatthew Wilcox (Oracle) 		if (copy_to_iter(kbuf, count, iter) < count)
60832927393SChristoph Hellwig 			goto out_free_buf;
60932927393SChristoph Hellwig 	}
61032927393SChristoph Hellwig 
6114e63acdfSAndrey Ignatov 	error = count;
61232927393SChristoph Hellwig out_free_buf:
61345089437SJosef Bacik 	kvfree(kbuf);
61477b14db5SEric W. Biederman out:
61577b14db5SEric W. Biederman 	sysctl_head_finish(head);
61677b14db5SEric W. Biederman 
61777b14db5SEric W. Biederman 	return error;
61877b14db5SEric W. Biederman }
61977b14db5SEric W. Biederman 
proc_sys_read(struct kiocb * iocb,struct iov_iter * iter)6204bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
6217708bfb1SPavel Emelyanov {
6224bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 0);
6237708bfb1SPavel Emelyanov }
6247708bfb1SPavel Emelyanov 
proc_sys_write(struct kiocb * iocb,struct iov_iter * iter)6254bd6a735SMatthew Wilcox (Oracle) static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
62677b14db5SEric W. Biederman {
6274bd6a735SMatthew Wilcox (Oracle) 	return proc_sys_call_handler(iocb, iter, 1);
62877b14db5SEric W. Biederman }
62977b14db5SEric W. Biederman 
proc_sys_open(struct inode * inode,struct file * filp)630f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
631f1ecf068SLucas De Marchi {
6324e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
63329e1095bSThomas Weißschuh 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
634f1ecf068SLucas De Marchi 
6354e474a00SLucas De Marchi 	/* sysctl was unregistered */
6364e474a00SLucas De Marchi 	if (IS_ERR(head))
6374e474a00SLucas De Marchi 		return PTR_ERR(head);
6384e474a00SLucas De Marchi 
639f1ecf068SLucas De Marchi 	if (table->poll)
640f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
641f1ecf068SLucas De Marchi 
6424e474a00SLucas De Marchi 	sysctl_head_finish(head);
6434e474a00SLucas De Marchi 
644f1ecf068SLucas De Marchi 	return 0;
645f1ecf068SLucas De Marchi }
646f1ecf068SLucas De Marchi 
proc_sys_poll(struct file * filp,poll_table * wait)647076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
648f1ecf068SLucas De Marchi {
649496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
6504e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
65129e1095bSThomas Weißschuh 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
652076ccb76SAl Viro 	__poll_t ret = DEFAULT_POLLMASK;
6534e474a00SLucas De Marchi 	unsigned long event;
6544e474a00SLucas De Marchi 
6554e474a00SLucas De Marchi 	/* sysctl was unregistered */
6564e474a00SLucas De Marchi 	if (IS_ERR(head))
657a9a08845SLinus Torvalds 		return EPOLLERR | EPOLLHUP;
658f1ecf068SLucas De Marchi 
659f1ecf068SLucas De Marchi 	if (!table->proc_handler)
660f1ecf068SLucas De Marchi 		goto out;
661f1ecf068SLucas De Marchi 
662f1ecf068SLucas De Marchi 	if (!table->poll)
663f1ecf068SLucas De Marchi 		goto out;
664f1ecf068SLucas De Marchi 
6654e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
666f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
667f1ecf068SLucas De Marchi 
668f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
669f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
670a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
671f1ecf068SLucas De Marchi 	}
672f1ecf068SLucas De Marchi 
673f1ecf068SLucas De Marchi out:
6744e474a00SLucas De Marchi 	sysctl_head_finish(head);
6754e474a00SLucas De Marchi 
676f1ecf068SLucas De Marchi 	return ret;
677f1ecf068SLucas De Marchi }
67877b14db5SEric W. Biederman 
proc_sys_fill_cache(struct file * file,struct dir_context * ctx,struct ctl_table_header * head,const struct ctl_table * table)679f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
680f0c3b509SAl Viro 				struct dir_context *ctx,
6819043476fSAl Viro 				struct ctl_table_header *head,
68229e1095bSThomas Weißschuh 				const struct ctl_table *table)
68377b14db5SEric W. Biederman {
684f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
68577b14db5SEric W. Biederman 	struct inode *inode;
68677b14db5SEric W. Biederman 	struct qstr qname;
68777b14db5SEric W. Biederman 	ino_t ino = 0;
68877b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
68977b14db5SEric W. Biederman 
69077b14db5SEric W. Biederman 	qname.name = table->procname;
69177b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
6928387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
69377b14db5SEric W. Biederman 
69477b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
69577b14db5SEric W. Biederman 	if (!child) {
69676aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
69776aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
69876aab3abSAl Viro 		if (IS_ERR(child))
69976aab3abSAl Viro 			return false;
70076aab3abSAl Viro 		if (d_in_lookup(child)) {
701888e2b03SAl Viro 			struct dentry *res;
702fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
7031eae9a47SAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
704888e2b03SAl Viro 			res = d_splice_alias(inode, child);
705888e2b03SAl Viro 			d_lookup_done(child);
706888e2b03SAl Viro 			if (unlikely(res)) {
707888e2b03SAl Viro 				dput(child);
7089c738daeSMarkus Elfring 
7099c738daeSMarkus Elfring 				if (IS_ERR(res))
710888e2b03SAl Viro 					return false;
7119c738daeSMarkus Elfring 
712888e2b03SAl Viro 				child = res;
713888e2b03SAl Viro 			}
71477b14db5SEric W. Biederman 		}
71577b14db5SEric W. Biederman 	}
7162b0143b5SDavid Howells 	inode = d_inode(child);
71777b14db5SEric W. Biederman 	ino  = inode->i_ino;
71877b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
71977b14db5SEric W. Biederman 	dput(child);
720f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
7219043476fSAl Viro }
7229043476fSAl Viro 
proc_sys_link_fill_cache(struct file * file,struct dir_context * ctx,struct ctl_table_header * head,const struct ctl_table * table)723f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
724f0c3b509SAl Viro 				    struct dir_context *ctx,
7250e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
72629e1095bSThomas Weißschuh 				    const struct ctl_table *table)
7270e47c99dSEric W. Biederman {
728f0c3b509SAl Viro 	bool ret = true;
729a0b0d1c3SDanilo Krummrich 
7300e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
731a0b0d1c3SDanilo Krummrich 	if (IS_ERR(head))
732a0b0d1c3SDanilo Krummrich 		return false;
7330e47c99dSEric W. Biederman 
7340e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
735835b94e0SDanilo Krummrich 	if (sysctl_follow_link(&head, &table))
7360e47c99dSEric W. Biederman 		goto out;
7370e47c99dSEric W. Biederman 
738f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
7390e47c99dSEric W. Biederman out:
7400e47c99dSEric W. Biederman 	sysctl_head_finish(head);
7410e47c99dSEric W. Biederman 	return ret;
7420e47c99dSEric W. Biederman }
7430e47c99dSEric W. Biederman 
scan(struct ctl_table_header * head,const struct ctl_table * table,unsigned long * pos,struct file * file,struct dir_context * ctx)74429e1095bSThomas Weißschuh static int scan(struct ctl_table_header *head, const struct ctl_table *table,
7459043476fSAl Viro 		unsigned long *pos, struct file *file,
746f0c3b509SAl Viro 		struct dir_context *ctx)
7479043476fSAl Viro {
748f0c3b509SAl Viro 	bool res;
7499043476fSAl Viro 
750f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
751f0c3b509SAl Viro 		return true;
7529043476fSAl Viro 
7530e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
754f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
7550e47c99dSEric W. Biederman 	else
756f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
7579043476fSAl Viro 
758f0c3b509SAl Viro 	if (res)
759f0c3b509SAl Viro 		ctx->pos = *pos;
7606a75ce16SEric W. Biederman 
7616a75ce16SEric W. Biederman 	return res;
76277b14db5SEric W. Biederman }
76377b14db5SEric W. Biederman 
proc_sys_readdir(struct file * file,struct dir_context * ctx)764f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
76577b14db5SEric W. Biederman {
766f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7679043476fSAl Viro 	struct ctl_table_header *h = NULL;
76829e1095bSThomas Weißschuh 	const struct ctl_table *entry;
7697ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
77077b14db5SEric W. Biederman 	unsigned long pos;
77177b14db5SEric W. Biederman 
7729043476fSAl Viro 	if (IS_ERR(head))
7739043476fSAl Viro 		return PTR_ERR(head);
7749043476fSAl Viro 
7757ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
77677b14db5SEric W. Biederman 
777f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
77893362fa4SZhou Chengming 		goto out;
779f0c3b509SAl Viro 
78077b14db5SEric W. Biederman 	pos = 2;
78177b14db5SEric W. Biederman 
7827ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
783f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7849043476fSAl Viro 			sysctl_head_finish(h);
7859043476fSAl Viro 			break;
78677b14db5SEric W. Biederman 		}
78777b14db5SEric W. Biederman 	}
78893362fa4SZhou Chengming out:
78977b14db5SEric W. Biederman 	sysctl_head_finish(head);
790f0c3b509SAl Viro 	return 0;
79177b14db5SEric W. Biederman }
79277b14db5SEric W. Biederman 
proc_sys_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)7934609e1f1SChristian Brauner static int proc_sys_permission(struct mnt_idmap *idmap,
794549c7297SChristian Brauner 			       struct inode *inode, int mask)
79577b14db5SEric W. Biederman {
79677b14db5SEric W. Biederman 	/*
79777b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
79877b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
79977b14db5SEric W. Biederman 	 */
800f696a365SMiklos Szeredi 	struct ctl_table_header *head;
80129e1095bSThomas Weißschuh 	const struct ctl_table *table;
80277b14db5SEric W. Biederman 	int error;
80377b14db5SEric W. Biederman 
804f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
805f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
806f696a365SMiklos Szeredi 		return -EACCES;
807f696a365SMiklos Szeredi 
808f696a365SMiklos Szeredi 	head = grab_header(inode);
8099043476fSAl Viro 	if (IS_ERR(head))
8109043476fSAl Viro 		return PTR_ERR(head);
81177b14db5SEric W. Biederman 
812f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
8139043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
8149043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
8159043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
81673f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
8179043476fSAl Viro 
81877b14db5SEric W. Biederman 	sysctl_head_finish(head);
81977b14db5SEric W. Biederman 	return error;
82077b14db5SEric W. Biederman }
82177b14db5SEric W. Biederman 
proc_sys_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)822c1632a0fSChristian Brauner static int proc_sys_setattr(struct mnt_idmap *idmap,
823549c7297SChristian Brauner 			    struct dentry *dentry, struct iattr *attr)
82477b14db5SEric W. Biederman {
8252b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
82677b14db5SEric W. Biederman 	int error;
82777b14db5SEric W. Biederman 
82877b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
82977b14db5SEric W. Biederman 		return -EPERM;
83077b14db5SEric W. Biederman 
831c1632a0fSChristian Brauner 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
8321025774cSChristoph Hellwig 	if (error)
83377b14db5SEric W. Biederman 		return error;
8341025774cSChristoph Hellwig 
835c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, attr);
8361025774cSChristoph Hellwig 	return 0;
83777b14db5SEric W. Biederman }
83877b14db5SEric W. Biederman 
proc_sys_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)839b74d24f7SChristian Brauner static int proc_sys_getattr(struct mnt_idmap *idmap,
840549c7297SChristian Brauner 			    const struct path *path, struct kstat *stat,
841a528d35eSDavid Howells 			    u32 request_mask, unsigned int query_flags)
8429043476fSAl Viro {
843a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
8449043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
84529e1095bSThomas Weißschuh 	const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
8469043476fSAl Viro 
8479043476fSAl Viro 	if (IS_ERR(head))
8489043476fSAl Viro 		return PTR_ERR(head);
8499043476fSAl Viro 
8500d72b928SJeff Layton 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
8519043476fSAl Viro 	if (table)
8529043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
8539043476fSAl Viro 
8549043476fSAl Viro 	sysctl_head_finish(head);
8559043476fSAl Viro 	return 0;
8569043476fSAl Viro }
8579043476fSAl Viro 
85877b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
859f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
860f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
8614bd6a735SMatthew Wilcox (Oracle) 	.read_iter	= proc_sys_read,
8624bd6a735SMatthew Wilcox (Oracle) 	.write_iter	= proc_sys_write,
863b0072734SDavid Howells 	.splice_read	= copy_splice_read,
8644bd6a735SMatthew Wilcox (Oracle) 	.splice_write	= iter_file_splice_write,
8656038f373SArnd Bergmann 	.llseek		= default_llseek,
8669043476fSAl Viro };
8679043476fSAl Viro 
8689043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
869887df078SPavel Emelyanov 	.read		= generic_read_dir,
870f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
8713222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
87277b14db5SEric W. Biederman };
87377b14db5SEric W. Biederman 
87403a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8759043476fSAl Viro 	.permission	= proc_sys_permission,
8769043476fSAl Viro 	.setattr	= proc_sys_setattr,
8779043476fSAl Viro 	.getattr	= proc_sys_getattr,
8789043476fSAl Viro };
8799043476fSAl Viro 
8809043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
88177b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
88277b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
88377b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
8849043476fSAl Viro 	.getattr	= proc_sys_getattr,
88577b14db5SEric W. Biederman };
88677b14db5SEric W. Biederman 
proc_sys_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)887*5be1fa8aSAl Viro static int proc_sys_revalidate(struct inode *dir, const struct qstr *name,
888*5be1fa8aSAl Viro 			       struct dentry *dentry, unsigned int flags)
88977b14db5SEric W. Biederman {
8900b728e19SAl Viro 	if (flags & LOOKUP_RCU)
89134286d66SNick Piggin 		return -ECHILD;
8922b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
8939043476fSAl Viro }
8949043476fSAl Viro 
proc_sys_delete(const struct dentry * dentry)895fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
8969043476fSAl Viro {
8972b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
8989043476fSAl Viro }
8999043476fSAl Viro 
sysctl_is_seen(struct ctl_table_header * p)9001f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
9011f87f0b5SEric W. Biederman {
9021f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
9031f87f0b5SEric W. Biederman 	int res;
9041f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
9051f87f0b5SEric W. Biederman 	if (p->unregistering)
9061f87f0b5SEric W. Biederman 		res = 0;
9071f87f0b5SEric W. Biederman 	else if (!set->is_seen)
9081f87f0b5SEric W. Biederman 		res = 1;
9091f87f0b5SEric W. Biederman 	else
9101f87f0b5SEric W. Biederman 		res = set->is_seen(set);
9111f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
9121f87f0b5SEric W. Biederman 	return res;
9131f87f0b5SEric W. Biederman }
9141f87f0b5SEric W. Biederman 
proc_sys_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)9156fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry,
916621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
9179043476fSAl Viro {
918dfef6dcdSAl Viro 	struct ctl_table_header *head;
919da53be12SLinus Torvalds 	struct inode *inode;
920da53be12SLinus Torvalds 
92131e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
92231e6b01fSNick Piggin 	 * that inode here can be NULL */
923dfef6dcdSAl Viro 	/* AV: can it, indeed? */
9242b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
92531e6b01fSNick Piggin 	if (!inode)
926dfef6dcdSAl Viro 		return 1;
927621e155aSNick Piggin 	if (name->len != len)
9289043476fSAl Viro 		return 1;
929621e155aSNick Piggin 	if (memcmp(name->name, str, len))
9309043476fSAl Viro 		return 1;
931dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
932dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
93377b14db5SEric W. Biederman }
93477b14db5SEric W. Biederman 
935d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
93677b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
9379043476fSAl Viro 	.d_delete	= proc_sys_delete,
9389043476fSAl Viro 	.d_compare	= proc_sys_compare,
93977b14db5SEric W. Biederman };
94077b14db5SEric W. Biederman 
find_subdir(struct ctl_dir * dir,const char * name,int namelen)9410e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
9427ec66d06SEric W. Biederman 				   const char *name, int namelen)
9431f87f0b5SEric W. Biederman {
9447ec66d06SEric W. Biederman 	struct ctl_table_header *head;
94529e1095bSThomas Weißschuh 	const struct ctl_table *entry;
9461f87f0b5SEric W. Biederman 
9470e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
9487ec66d06SEric W. Biederman 	if (!entry)
9497ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
95051f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
9517ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
95251f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
9531f87f0b5SEric W. Biederman }
9541f87f0b5SEric W. Biederman 
new_dir(struct ctl_table_set * set,const char * name,int namelen)9557ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
9567ec66d06SEric W. Biederman 			       const char *name, int namelen)
9571f87f0b5SEric W. Biederman {
9587ec66d06SEric W. Biederman 	struct ctl_table *table;
9597ec66d06SEric W. Biederman 	struct ctl_dir *new;
960ac13ac6fSEric W. Biederman 	struct ctl_node *node;
9617ec66d06SEric W. Biederman 	char *new_name;
9621f87f0b5SEric W. Biederman 
963ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
964aef9d25eSJoel Granados 		      sizeof(struct ctl_table) +  namelen + 1,
965ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
9667ec66d06SEric W. Biederman 	if (!new)
9677ec66d06SEric W. Biederman 		return NULL;
9687ec66d06SEric W. Biederman 
969ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
970ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
971aef9d25eSJoel Granados 	new_name = (char *)(table + 1);
9727ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9737ec66d06SEric W. Biederman 	table[0].procname = new_name;
9747ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
975b1f01e2bSJoel Granados 	init_header(&new->header, set->dir.header.root, set, node, table, 1);
9767ec66d06SEric W. Biederman 
9777ec66d06SEric W. Biederman 	return new;
9781f87f0b5SEric W. Biederman }
9791f87f0b5SEric W. Biederman 
98060f126d9SEric W. Biederman /**
98160f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
98260f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
98360f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
98460f126d9SEric W. Biederman  * @namelen: The length of name
98560f126d9SEric W. Biederman  *
98660f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
98760f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
98860f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
98960f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
99060f126d9SEric W. Biederman  * simply dropped.
99160f126d9SEric W. Biederman  */
get_subdir(struct ctl_dir * dir,const char * name,int namelen)9920e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
9930e47c99dSEric W. Biederman 				  const char *name, int namelen)
9947ec66d06SEric W. Biederman {
9950e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
9967ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
9970eb97f38SEric W. Biederman 	int err;
9987ec66d06SEric W. Biederman 
9997ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10000e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10017ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10027ec66d06SEric W. Biederman 		goto found;
10037ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10047ec66d06SEric W. Biederman 		goto failed;
10057ec66d06SEric W. Biederman 
10067ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10077ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
10087ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
10097ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
10107ec66d06SEric W. Biederman 	if (!new)
10117ec66d06SEric W. Biederman 		goto failed;
10127ec66d06SEric W. Biederman 
101360f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
10140e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
10157ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
10167ec66d06SEric W. Biederman 		goto found;
10177ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
10187ec66d06SEric W. Biederman 		goto failed;
10197ec66d06SEric W. Biederman 
102060f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
10210eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
10220eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
10230eb97f38SEric W. Biederman 	if (err)
10240e47c99dSEric W. Biederman 		goto failed;
10257ec66d06SEric W. Biederman 	subdir = new;
10267ec66d06SEric W. Biederman found:
10277ec66d06SEric W. Biederman 	subdir->header.nreg++;
10287ec66d06SEric W. Biederman failed:
1029a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
103087ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
10316980128fSEric W. Biederman 		sysctl_print_dir(dir);
1032153ee1c4SGeert Uytterhoeven 		pr_cont("%*.*s %ld\n", namelen, namelen, name,
1033153ee1c4SGeert Uytterhoeven 			PTR_ERR(subdir));
10341f87f0b5SEric W. Biederman 	}
10357ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10367ec66d06SEric W. Biederman 	if (new)
10377ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
10387ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10397ec66d06SEric W. Biederman 	return subdir;
10401f87f0b5SEric W. Biederman }
10411f87f0b5SEric W. Biederman 
xlate_dir(struct ctl_table_set * set,struct ctl_dir * dir)10420e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
10430e47c99dSEric W. Biederman {
10440e47c99dSEric W. Biederman 	struct ctl_dir *parent;
10450e47c99dSEric W. Biederman 	const char *procname;
10460e47c99dSEric W. Biederman 	if (!dir->header.parent)
10470e47c99dSEric W. Biederman 		return &set->dir;
10480e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
10490e47c99dSEric W. Biederman 	if (IS_ERR(parent))
10500e47c99dSEric W. Biederman 		return parent;
10510e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
10520e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
10530e47c99dSEric W. Biederman }
10540e47c99dSEric W. Biederman 
sysctl_follow_link(struct ctl_table_header ** phead,const struct ctl_table ** pentry)10550e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
105629e1095bSThomas Weißschuh 	const struct ctl_table **pentry)
10570e47c99dSEric W. Biederman {
10580e47c99dSEric W. Biederman 	struct ctl_table_header *head;
105929e1095bSThomas Weißschuh 	const struct ctl_table *entry;
10600e47c99dSEric W. Biederman 	struct ctl_table_root *root;
10610e47c99dSEric W. Biederman 	struct ctl_table_set *set;
10620e47c99dSEric W. Biederman 	struct ctl_dir *dir;
10630e47c99dSEric W. Biederman 	int ret;
10640e47c99dSEric W. Biederman 
10650e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
10660e47c99dSEric W. Biederman 	root = (*pentry)->data;
106713bcc6a2SEric W. Biederman 	set = lookup_header_set(root);
10680e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
10690e47c99dSEric W. Biederman 	if (IS_ERR(dir))
10700e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10710e47c99dSEric W. Biederman 	else {
10720e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10730e47c99dSEric W. Biederman 		head = NULL;
10740e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10750e47c99dSEric W. Biederman 		ret = -ENOENT;
10760e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10770e47c99dSEric W. Biederman 			unuse_table(*phead);
10780e47c99dSEric W. Biederman 			*phead = head;
10790e47c99dSEric W. Biederman 			*pentry = entry;
10800e47c99dSEric W. Biederman 			ret = 0;
10810e47c99dSEric W. Biederman 		}
10820e47c99dSEric W. Biederman 	}
10830e47c99dSEric W. Biederman 
10840e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
10850e47c99dSEric W. Biederman 	return ret;
10860e47c99dSEric W. Biederman }
10870e47c99dSEric W. Biederman 
sysctl_err(const char * path,const struct ctl_table * table,char * fmt,...)108829e1095bSThomas Weißschuh static int sysctl_err(const char *path, const struct ctl_table *table, char *fmt, ...)
10897c60c48fSEric W. Biederman {
10907c60c48fSEric W. Biederman 	struct va_format vaf;
10917c60c48fSEric W. Biederman 	va_list args;
10927c60c48fSEric W. Biederman 
10937c60c48fSEric W. Biederman 	va_start(args, fmt);
10947c60c48fSEric W. Biederman 	vaf.fmt = fmt;
10957c60c48fSEric W. Biederman 	vaf.va = &args;
10967c60c48fSEric W. Biederman 
109787ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
10987c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
10997c60c48fSEric W. Biederman 
11007c60c48fSEric W. Biederman 	va_end(args);
11017c60c48fSEric W. Biederman 	return -EINVAL;
11027c60c48fSEric W. Biederman }
11037c60c48fSEric W. Biederman 
sysctl_check_table_array(const char * path,const struct ctl_table * table)110429e1095bSThomas Weißschuh static int sysctl_check_table_array(const char *path, const struct ctl_table *table)
11054f2fec00SLuis R. Rodriguez {
1106b5ffbd13SWen Yang 	unsigned int extra;
11074f2fec00SLuis R. Rodriguez 	int err = 0;
11084f2fec00SLuis R. Rodriguez 
110961d9b56aSLuis R. Rodriguez 	if ((table->proc_handler == proc_douintvec) ||
111061d9b56aSLuis R. Rodriguez 	    (table->proc_handler == proc_douintvec_minmax)) {
11114f2fec00SLuis R. Rodriguez 		if (table->maxlen != sizeof(unsigned int))
111264a11f3dSWaiman Long 			err |= sysctl_err(path, table, "array not allowed");
11134f2fec00SLuis R. Rodriguez 	}
11144f2fec00SLuis R. Rodriguez 
1115cb944413SEric Dumazet 	if (table->proc_handler == proc_dou8vec_minmax) {
1116cb944413SEric Dumazet 		if (table->maxlen != sizeof(u8))
1117cb944413SEric Dumazet 			err |= sysctl_err(path, table, "array not allowed");
1118b5ffbd13SWen Yang 
1119b5ffbd13SWen Yang 		if (table->extra1) {
1120b5ffbd13SWen Yang 			extra = *(unsigned int *) table->extra1;
1121b5ffbd13SWen Yang 			if (extra > 255U)
1122b5ffbd13SWen Yang 				err |= sysctl_err(path, table,
1123b5ffbd13SWen Yang 						"range value too large for proc_dou8vec_minmax");
1124b5ffbd13SWen Yang 		}
1125b5ffbd13SWen Yang 		if (table->extra2) {
1126b5ffbd13SWen Yang 			extra = *(unsigned int *) table->extra2;
1127b5ffbd13SWen Yang 			if (extra > 255U)
1128b5ffbd13SWen Yang 				err |= sysctl_err(path, table,
1129b5ffbd13SWen Yang 						"range value too large for proc_dou8vec_minmax");
1130b5ffbd13SWen Yang 		}
1131cb944413SEric Dumazet 	}
1132cb944413SEric Dumazet 
1133f1aa2eb5SOndrej Mosnacek 	if (table->proc_handler == proc_dobool) {
1134f1aa2eb5SOndrej Mosnacek 		if (table->maxlen != sizeof(bool))
1135f1aa2eb5SOndrej Mosnacek 			err |= sysctl_err(path, table, "array not allowed");
1136f1aa2eb5SOndrej Mosnacek 	}
1137f1aa2eb5SOndrej Mosnacek 
11384f2fec00SLuis R. Rodriguez 	return err;
11394f2fec00SLuis R. Rodriguez }
11404f2fec00SLuis R. Rodriguez 
sysctl_check_table(const char * path,struct ctl_table_header * header)1141cc9f7ee0SJoel Granados static int sysctl_check_table(const char *path, struct ctl_table_header *header)
11427c60c48fSEric W. Biederman {
114329e1095bSThomas Weißschuh 	const struct ctl_table *entry;
11447c60c48fSEric W. Biederman 	int err = 0;
114518d4b42eSJoel Granados 	list_for_each_table_entry(entry, header) {
1146acc15469SJoel Granados 		if (!entry->procname)
1147acc15469SJoel Granados 			err |= sysctl_err(path, entry, "procname is null");
1148cb55f27aSMeng Tang 		if ((entry->proc_handler == proc_dostring) ||
1149f1aa2eb5SOndrej Mosnacek 		    (entry->proc_handler == proc_dobool) ||
1150cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec) ||
1151cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec) ||
1152cb55f27aSMeng Tang 		    (entry->proc_handler == proc_douintvec_minmax) ||
1153cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_minmax) ||
1154cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dou8vec_minmax) ||
1155cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_jiffies) ||
1156cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1157cb55f27aSMeng Tang 		    (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1158cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_minmax) ||
1159cb55f27aSMeng Tang 		    (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1160cb55f27aSMeng Tang 			if (!entry->data)
1161cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No data");
1162cb55f27aSMeng Tang 			if (!entry->maxlen)
1163cb55f27aSMeng Tang 				err |= sysctl_err(path, entry, "No maxlen");
11644f2fec00SLuis R. Rodriguez 			else
1165cb55f27aSMeng Tang 				err |= sysctl_check_table_array(path, entry);
11661f87f0b5SEric W. Biederman 		}
1167cb55f27aSMeng Tang 		if (!entry->proc_handler)
1168cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "No proc_handler");
11697c60c48fSEric W. Biederman 
1170cb55f27aSMeng Tang 		if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1171cb55f27aSMeng Tang 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
1172cb55f27aSMeng Tang 				entry->mode);
11731f87f0b5SEric W. Biederman 	}
11747c60c48fSEric W. Biederman 	return err;
11751f87f0b5SEric W. Biederman }
11761f87f0b5SEric W. Biederman 
new_links(struct ctl_dir * dir,struct ctl_table_header * head)1177cc9f7ee0SJoel Granados static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_header *head)
11780e47c99dSEric W. Biederman {
117929e1095bSThomas Weißschuh 	struct ctl_table *link_table, *link;
11800e47c99dSEric W. Biederman 	struct ctl_table_header *links;
118129e1095bSThomas Weißschuh 	const struct ctl_table *entry;
1182ac13ac6fSEric W. Biederman 	struct ctl_node *node;
11830e47c99dSEric W. Biederman 	char *link_name;
118455bb7eb6SJoel Granados 	int name_bytes;
11850e47c99dSEric W. Biederman 
11860e47c99dSEric W. Biederman 	name_bytes = 0;
118718d4b42eSJoel Granados 	list_for_each_table_entry(entry, head) {
11880e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
11890e47c99dSEric W. Biederman 	}
11900e47c99dSEric W. Biederman 
11910e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
119255bb7eb6SJoel Granados 			sizeof(struct ctl_node)*head->ctl_table_size +
1193aef9d25eSJoel Granados 			sizeof(struct ctl_table)*head->ctl_table_size +
11940e47c99dSEric W. Biederman 			name_bytes,
11950e47c99dSEric W. Biederman 			GFP_KERNEL);
11960e47c99dSEric W. Biederman 
11970e47c99dSEric W. Biederman 	if (!links)
11980e47c99dSEric W. Biederman 		return NULL;
11990e47c99dSEric W. Biederman 
1200ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
120155bb7eb6SJoel Granados 	link_table = (struct ctl_table *)(node + head->ctl_table_size);
1202aef9d25eSJoel Granados 	link_name = (char *)(link_table + head->ctl_table_size);
1203cb55f27aSMeng Tang 	link = link_table;
12040e47c99dSEric W. Biederman 
120518d4b42eSJoel Granados 	list_for_each_table_entry(entry, head) {
12060e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
12070e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
12080e47c99dSEric W. Biederman 		link->procname = link_name;
12090e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
1210cc9f7ee0SJoel Granados 		link->data = head->root;
12110e47c99dSEric W. Biederman 		link_name += len;
1212cb55f27aSMeng Tang 		link++;
12130e47c99dSEric W. Biederman 	}
1214b1f01e2bSJoel Granados 	init_header(links, dir->header.root, dir->header.set, node, link_table,
1215b1f01e2bSJoel Granados 		    head->ctl_table_size);
121655bb7eb6SJoel Granados 	links->nreg = head->ctl_table_size;
12170e47c99dSEric W. Biederman 
12180e47c99dSEric W. Biederman 	return links;
12190e47c99dSEric W. Biederman }
12200e47c99dSEric W. Biederman 
get_links(struct ctl_dir * dir,struct ctl_table_header * header,struct ctl_table_root * link_root)12210e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
1222cc9f7ee0SJoel Granados 		      struct ctl_table_header *header,
1223cc9f7ee0SJoel Granados 		      struct ctl_table_root *link_root)
12240e47c99dSEric W. Biederman {
1225cc9f7ee0SJoel Granados 	struct ctl_table_header *tmp_head;
122629e1095bSThomas Weißschuh 	const struct ctl_table *entry, *link;
12270e47c99dSEric W. Biederman 
122831555231SJoel Granados 	if (header->ctl_table_size == 0 ||
1229eb32d3adSThomas Weißschuh 	    sysctl_is_perm_empty_ctl_header(header))
123031555231SJoel Granados 		return true;
123131555231SJoel Granados 
12320e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
123318d4b42eSJoel Granados 	list_for_each_table_entry(entry, header) {
12340e47c99dSEric W. Biederman 		const char *procname = entry->procname;
1235cc9f7ee0SJoel Granados 		link = find_entry(&tmp_head, dir, procname, strlen(procname));
12360e47c99dSEric W. Biederman 		if (!link)
12370e47c99dSEric W. Biederman 			return false;
12380e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
12390e47c99dSEric W. Biederman 			continue;
12400e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
12410e47c99dSEric W. Biederman 			continue;
12420e47c99dSEric W. Biederman 		return false;
12430e47c99dSEric W. Biederman 	}
12440e47c99dSEric W. Biederman 
12450e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
124618d4b42eSJoel Granados 	list_for_each_table_entry(entry, header) {
12470e47c99dSEric W. Biederman 		const char *procname = entry->procname;
1248cc9f7ee0SJoel Granados 		link = find_entry(&tmp_head, dir, procname, strlen(procname));
1249cc9f7ee0SJoel Granados 		tmp_head->nreg++;
12500e47c99dSEric W. Biederman 	}
12510e47c99dSEric W. Biederman 	return true;
12520e47c99dSEric W. Biederman }
12530e47c99dSEric W. Biederman 
insert_links(struct ctl_table_header * head)12540e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
12550e47c99dSEric W. Biederman {
12560e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
12579a521359SLi zeming 	struct ctl_dir *core_parent;
12580e47c99dSEric W. Biederman 	struct ctl_table_header *links;
12590e47c99dSEric W. Biederman 	int err;
12600e47c99dSEric W. Biederman 
12610e47c99dSEric W. Biederman 	if (head->set == root_set)
12620e47c99dSEric W. Biederman 		return 0;
12630e47c99dSEric W. Biederman 
12640e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
12650e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
12660e47c99dSEric W. Biederman 		return 0;
12670e47c99dSEric W. Biederman 
1268cc9f7ee0SJoel Granados 	if (get_links(core_parent, head, head->root))
12690e47c99dSEric W. Biederman 		return 0;
12700e47c99dSEric W. Biederman 
12710e47c99dSEric W. Biederman 	core_parent->header.nreg++;
12720e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
12730e47c99dSEric W. Biederman 
1274cc9f7ee0SJoel Granados 	links = new_links(core_parent, head);
12750e47c99dSEric W. Biederman 
12760e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
12770e47c99dSEric W. Biederman 	err = -ENOMEM;
12780e47c99dSEric W. Biederman 	if (!links)
12790e47c99dSEric W. Biederman 		goto out;
12800e47c99dSEric W. Biederman 
12810e47c99dSEric W. Biederman 	err = 0;
1282cc9f7ee0SJoel Granados 	if (get_links(core_parent, head, head->root)) {
12830e47c99dSEric W. Biederman 		kfree(links);
12840e47c99dSEric W. Biederman 		goto out;
12850e47c99dSEric W. Biederman 	}
12860e47c99dSEric W. Biederman 
12870e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
12880e47c99dSEric W. Biederman 	if (err)
12890e47c99dSEric W. Biederman 		kfree(links);
12900e47c99dSEric W. Biederman out:
12910e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
12920e47c99dSEric W. Biederman 	return err;
12930e47c99dSEric W. Biederman }
12940e47c99dSEric W. Biederman 
1295b2f56e55SLuis Chamberlain /* Find the directory for the ctl_table. If one is not found create it. */
sysctl_mkdir_p(struct ctl_dir * dir,const char * path)1296b2f56e55SLuis Chamberlain static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
1297b2f56e55SLuis Chamberlain {
1298b2f56e55SLuis Chamberlain 	const char *name, *nextname;
1299b2f56e55SLuis Chamberlain 
1300b2f56e55SLuis Chamberlain 	for (name = path; name; name = nextname) {
1301b2f56e55SLuis Chamberlain 		int namelen;
1302b2f56e55SLuis Chamberlain 		nextname = strchr(name, '/');
1303b2f56e55SLuis Chamberlain 		if (nextname) {
1304b2f56e55SLuis Chamberlain 			namelen = nextname - name;
1305b2f56e55SLuis Chamberlain 			nextname++;
1306b2f56e55SLuis Chamberlain 		} else {
1307b2f56e55SLuis Chamberlain 			namelen = strlen(name);
1308b2f56e55SLuis Chamberlain 		}
1309b2f56e55SLuis Chamberlain 		if (namelen == 0)
1310b2f56e55SLuis Chamberlain 			continue;
1311b2f56e55SLuis Chamberlain 
1312b2f56e55SLuis Chamberlain 		/*
1313b2f56e55SLuis Chamberlain 		 * namelen ensures if name is "foo/bar/yay" only foo is
1314b2f56e55SLuis Chamberlain 		 * registered first. We traverse as if using mkdir -p and
1315b2f56e55SLuis Chamberlain 		 * return a ctl_dir for the last directory entry.
1316b2f56e55SLuis Chamberlain 		 */
1317b2f56e55SLuis Chamberlain 		dir = get_subdir(dir, name, namelen);
1318b2f56e55SLuis Chamberlain 		if (IS_ERR(dir))
1319b2f56e55SLuis Chamberlain 			break;
1320b2f56e55SLuis Chamberlain 	}
1321b2f56e55SLuis Chamberlain 	return dir;
1322b2f56e55SLuis Chamberlain }
1323b2f56e55SLuis Chamberlain 
13241f87f0b5SEric W. Biederman /**
1325f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
132660a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
13271f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1328a02fe70dSJoel Granados  *
1329a02fe70dSJoel Granados  * @table: the top-level table structure. This table should not be free'd
1330a02fe70dSJoel Granados  *         after registration. So it should not be used on stack. It can either
1331a02fe70dSJoel Granados  *         be a global or dynamically allocated by the caller and free'd later
1332a02fe70dSJoel Granados  *         after sysctl unregistration.
1333bff97cf1SJoel Granados  * @table_size : The number of elements in table
13341f87f0b5SEric W. Biederman  *
13351f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
133637175403SJoel Granados  * array.
13371f87f0b5SEric W. Biederman  *
13381f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
13391f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
13401f87f0b5SEric W. Biederman  *            enter a sysctl file
13411f87f0b5SEric W. Biederman  * data     - a pointer to data for use by proc_handler
13421f87f0b5SEric W. Biederman  * maxlen   - the maximum size in bytes of the data
1343f728019bSEric W. Biederman  * mode     - the file permissions for the /proc/sys file
1344a02fe70dSJoel Granados  * type     - Defines the target type (described in struct definition)
13451f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
13461f87f0b5SEric W. Biederman  *
13471f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
134867ff3228SLuis Chamberlain  * XXX: we should eventually modify these to use long min / max [0]
134967ff3228SLuis Chamberlain  * [0] https://lkml.kernel.org/[email protected]
13501f87f0b5SEric W. Biederman  *
13511f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
1352a02fe70dSJoel Granados  * under /proc; non-leaf nodes are not allowed.
13531f87f0b5SEric W. Biederman  *
1354f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1355f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
13561f87f0b5SEric W. Biederman  *
13571f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
13581f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
13591f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
13601f87f0b5SEric W. Biederman  *
13611f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
13621f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
13631f87f0b5SEric W. Biederman  *
13641f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
13651f87f0b5SEric W. Biederman  * to the table header on success.
13661f87f0b5SEric W. Biederman  */
__register_sysctl_table(struct ctl_table_set * set,const char * path,const struct ctl_table * table,size_t table_size)13676e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
136860a47a2eSEric W. Biederman 	struct ctl_table_set *set,
13697abc9b53SThomas Weißschuh 	const char *path, const struct ctl_table *table, size_t table_size)
13701f87f0b5SEric W. Biederman {
137160a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
13721f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
13737ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1374ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1375ac13ac6fSEric W. Biederman 
1376ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1377bff97cf1SJoel Granados 			 sizeof(struct ctl_node)*table_size, GFP_KERNEL_ACCOUNT);
13781f87f0b5SEric W. Biederman 	if (!header)
13791f87f0b5SEric W. Biederman 		return NULL;
13801f87f0b5SEric W. Biederman 
1381ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1382bff97cf1SJoel Granados 	init_header(header, root, set, node, table, table_size);
1383cc9f7ee0SJoel Granados 	if (sysctl_check_table(path, header))
13847c60c48fSEric W. Biederman 		goto fail;
13858d6ecfccSEric W. Biederman 
13861f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
13870e47c99dSEric W. Biederman 	dir = &set->dir;
138867ff3228SLuis Chamberlain 	/* Reference moved down the directory tree get_subdir */
13897ec66d06SEric W. Biederman 	dir->header.nreg++;
13907ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
13917ec66d06SEric W. Biederman 
1392b2f56e55SLuis Chamberlain 	dir = sysctl_mkdir_p(dir, path);
13937ec66d06SEric W. Biederman 	if (IS_ERR(dir))
13947ec66d06SEric W. Biederman 		goto fail;
13957ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
13960e47c99dSEric W. Biederman 	if (insert_header(dir, header))
13970e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
13980e47c99dSEric W. Biederman 
13997ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
14001f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
14011f87f0b5SEric W. Biederman 
14021f87f0b5SEric W. Biederman 	return header;
14030e47c99dSEric W. Biederman 
14047ec66d06SEric W. Biederman fail_put_dir_locked:
14057ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
14067c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
14077c60c48fSEric W. Biederman fail:
14087c60c48fSEric W. Biederman 	kfree(header);
14097c60c48fSEric W. Biederman 	return NULL;
14101f87f0b5SEric W. Biederman }
14111f87f0b5SEric W. Biederman 
1412fea478d4SEric W. Biederman /**
14139edbfe92SJoel Granados  * register_sysctl_sz - register a sysctl table
14141dc8689eSLuis Chamberlain  * @path: The path to the directory the sysctl table is in. If the path
14151dc8689eSLuis Chamberlain  * 	doesn't exist we will create it for you.
14161dc8689eSLuis Chamberlain  * @table: the table structure. The calller must ensure the life of the @table
14171dc8689eSLuis Chamberlain  * 	will be kept during the lifetime use of the syctl. It must not be freed
14181dc8689eSLuis Chamberlain  * 	until unregister_sysctl_table() is called with the given returned table
14191dc8689eSLuis Chamberlain  * 	with this registration. If your code is non modular then you don't need
14201dc8689eSLuis Chamberlain  * 	to call unregister_sysctl_table() and can instead use something like
14211dc8689eSLuis Chamberlain  * 	register_sysctl_init() which does not care for the result of the syctl
14221dc8689eSLuis Chamberlain  * 	registration.
14239edbfe92SJoel Granados  * @table_size: The number of elements in table.
1424fea478d4SEric W. Biederman  *
1425fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1426fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1427fea478d4SEric W. Biederman  *
1428fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1429fea478d4SEric W. Biederman  */
register_sysctl_sz(const char * path,const struct ctl_table * table,size_t table_size)14307abc9b53SThomas Weißschuh struct ctl_table_header *register_sysctl_sz(const char *path, const struct ctl_table *table,
14319edbfe92SJoel Granados 					    size_t table_size)
1432fea478d4SEric W. Biederman {
1433fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
14349edbfe92SJoel Granados 					path, table, table_size);
1435fea478d4SEric W. Biederman }
14369edbfe92SJoel Granados EXPORT_SYMBOL(register_sysctl_sz);
1437fea478d4SEric W. Biederman 
14383ddd9a80SXiaoming Ni /**
14393ddd9a80SXiaoming Ni  * __register_sysctl_init() - register sysctl table to path
14401dc8689eSLuis Chamberlain  * @path: path name for sysctl base. If that path doesn't exist we will create
14411dc8689eSLuis Chamberlain  * 	it for you.
14421dc8689eSLuis Chamberlain  * @table: This is the sysctl table that needs to be registered to the path.
14431dc8689eSLuis Chamberlain  * 	The caller must ensure the life of the @table will be kept during the
14441dc8689eSLuis Chamberlain  * 	lifetime use of the sysctl.
14453ddd9a80SXiaoming Ni  * @table_name: The name of sysctl table, only used for log printing when
14463ddd9a80SXiaoming Ni  *              registration fails
14473bc269cfSJoel Granados  * @table_size: The number of elements in table
14483ddd9a80SXiaoming Ni  *
14493ddd9a80SXiaoming Ni  * The sysctl interface is used by userspace to query or modify at runtime
14503ddd9a80SXiaoming Ni  * a predefined value set on a variable. These variables however have default
14513ddd9a80SXiaoming Ni  * values pre-set. Code which depends on these variables will always work even
14523ddd9a80SXiaoming Ni  * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
14533ddd9a80SXiaoming Ni  * ability to query or modify the sysctls dynamically at run time. Chances of
14543ddd9a80SXiaoming Ni  * register_sysctl() failing on init are extremely low, and so for both reasons
14553ddd9a80SXiaoming Ni  * this function does not return any error as it is used by initialization code.
14563ddd9a80SXiaoming Ni  *
1457228b09deSLuis Chamberlain  * Context: if your base directory does not exist it will be created for you.
14583ddd9a80SXiaoming Ni  */
__register_sysctl_init(const char * path,const struct ctl_table * table,const char * table_name,size_t table_size)14597abc9b53SThomas Weißschuh void __init __register_sysctl_init(const char *path, const struct ctl_table *table,
14603bc269cfSJoel Granados 				 const char *table_name, size_t table_size)
14613ddd9a80SXiaoming Ni {
14623bc269cfSJoel Granados 	struct ctl_table_header *hdr = register_sysctl_sz(path, table, table_size);
14633ddd9a80SXiaoming Ni 
14643ddd9a80SXiaoming Ni 	if (unlikely(!hdr)) {
14659edbfe92SJoel Granados 		pr_err("failed when register_sysctl_sz %s to %s\n", table_name, path);
14663ddd9a80SXiaoming Ni 		return;
14673ddd9a80SXiaoming Ni 	}
14683ddd9a80SXiaoming Ni 	kmemleak_not_leak(hdr);
14693ddd9a80SXiaoming Ni }
14703ddd9a80SXiaoming Ni 
put_links(struct ctl_table_header * header)14710e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
14720e47c99dSEric W. Biederman {
14730e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
14740e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
14750e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
14760e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
147729e1095bSThomas Weißschuh 	const struct ctl_table *entry;
14780e47c99dSEric W. Biederman 
14790e47c99dSEric W. Biederman 	if (header->set == root_set)
14800e47c99dSEric W. Biederman 		return;
14810e47c99dSEric W. Biederman 
14820e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
14830e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
14840e47c99dSEric W. Biederman 		return;
14850e47c99dSEric W. Biederman 
148618d4b42eSJoel Granados 	list_for_each_table_entry(entry, header) {
14870e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
148829e1095bSThomas Weißschuh 		const struct ctl_table *link;
14890e47c99dSEric W. Biederman 		const char *name = entry->procname;
14900e47c99dSEric W. Biederman 
14910e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
14920e47c99dSEric W. Biederman 		if (link &&
14930e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
14940e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
14950e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
14960e47c99dSEric W. Biederman 		}
14970e47c99dSEric W. Biederman 		else {
149887ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
14990e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
1500153ee1c4SGeert Uytterhoeven 			pr_cont("%s\n", name);
15010e47c99dSEric W. Biederman 		}
15020e47c99dSEric W. Biederman 	}
15030e47c99dSEric W. Biederman }
15040e47c99dSEric W. Biederman 
drop_sysctl_table(struct ctl_table_header * header)1505938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1506938aaa4fSEric W. Biederman {
15077ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
15087ec66d06SEric W. Biederman 
1509938aaa4fSEric W. Biederman 	if (--header->nreg)
1510938aaa4fSEric W. Biederman 		return;
1511938aaa4fSEric W. Biederman 
151289189557SYueHaibing 	if (parent) {
15130e47c99dSEric W. Biederman 		put_links(header);
1514938aaa4fSEric W. Biederman 		start_unregistering(header);
151589189557SYueHaibing 	}
151689189557SYueHaibing 
1517938aaa4fSEric W. Biederman 	if (!--header->count)
1518938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
15197ec66d06SEric W. Biederman 
15207ec66d06SEric W. Biederman 	if (parent)
15217ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1522938aaa4fSEric W. Biederman }
1523938aaa4fSEric W. Biederman 
15241f87f0b5SEric W. Biederman /**
15251f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
152619c4e618SJoel Granados  * @header: the header returned from register_sysctl or __register_sysctl_table
15271f87f0b5SEric W. Biederman  *
15281f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
15291f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
15301f87f0b5SEric W. Biederman  */
unregister_sysctl_table(struct ctl_table_header * header)15311f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
15321f87f0b5SEric W. Biederman {
15331f87f0b5SEric W. Biederman 	might_sleep();
15341f87f0b5SEric W. Biederman 
15351f87f0b5SEric W. Biederman 	if (header == NULL)
15361f87f0b5SEric W. Biederman 		return;
15371f87f0b5SEric W. Biederman 
15381f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1539938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
15401f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
15411f87f0b5SEric W. Biederman }
15421f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
15431f87f0b5SEric W. Biederman 
setup_sysctl_set(struct ctl_table_set * set,struct ctl_table_root * root,int (* is_seen)(struct ctl_table_set *))15440e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
15459eb47c26SEric W. Biederman 	struct ctl_table_root *root,
15461f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
15471f87f0b5SEric W. Biederman {
15481347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
15490e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1550b1f01e2bSJoel Granados 	init_header(&set->dir.header, root, set, NULL, root_table, 1);
15511f87f0b5SEric W. Biederman }
15521f87f0b5SEric W. Biederman 
retire_sysctl_set(struct ctl_table_set * set)155397324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
155497324cd8SEric W. Biederman {
1555ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
155697324cd8SEric W. Biederman }
15571f87f0b5SEric W. Biederman 
proc_sys_init(void)15581e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
155977b14db5SEric W. Biederman {
1560e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1561e1675231SAlexey Dobriyan 
156277b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
15639043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
1564d56c0d45SAlexey Dobriyan 	proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
156577b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1566de4e83bdSEric W. Biederman 
1567d8c0418aSLuis Chamberlain 	return sysctl_init_bases();
156877b14db5SEric W. Biederman }
15693db978d4SVlastimil Babka 
15700a477e1aSVlastimil Babka struct sysctl_alias {
15710a477e1aSVlastimil Babka 	const char *kernel_param;
15720a477e1aSVlastimil Babka 	const char *sysctl_param;
15730a477e1aSVlastimil Babka };
15740a477e1aSVlastimil Babka 
15750a477e1aSVlastimil Babka /*
15760a477e1aSVlastimil Babka  * Historically some settings had both sysctl and a command line parameter.
15770a477e1aSVlastimil Babka  * With the generic sysctl. parameter support, we can handle them at a single
15780a477e1aSVlastimil Babka  * place and only keep the historical name for compatibility. This is not meant
15790a477e1aSVlastimil Babka  * to add brand new aliases. When adding existing aliases, consider whether
15800a477e1aSVlastimil Babka  * the possibly different moment of changing the value (e.g. from early_param
15810a477e1aSVlastimil Babka  * to the moment do_sysctl_args() is called) is an issue for the specific
15820a477e1aSVlastimil Babka  * parameter.
15830a477e1aSVlastimil Babka  */
15840a477e1aSVlastimil Babka static const struct sysctl_alias sysctl_aliases[] = {
1585f117955aSGuilherme G. Piccoli 	{"hardlockup_all_cpu_backtrace",	"kernel.hardlockup_all_cpu_backtrace" },
1586b467f3efSVlastimil Babka 	{"hung_task_panic",			"kernel.hung_task_panic" },
1587f117955aSGuilherme G. Piccoli 	{"numa_zonelist_order",			"vm.numa_zonelist_order" },
1588f117955aSGuilherme G. Piccoli 	{"softlockup_all_cpu_backtrace",	"kernel.softlockup_all_cpu_backtrace" },
15890a477e1aSVlastimil Babka 	{ }
15900a477e1aSVlastimil Babka };
15910a477e1aSVlastimil Babka 
sysctl_find_alias(char * param)15920a477e1aSVlastimil Babka static const char *sysctl_find_alias(char *param)
15930a477e1aSVlastimil Babka {
15940a477e1aSVlastimil Babka 	const struct sysctl_alias *alias;
15950a477e1aSVlastimil Babka 
15960a477e1aSVlastimil Babka 	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
15970a477e1aSVlastimil Babka 		if (strcmp(alias->kernel_param, param) == 0)
15980a477e1aSVlastimil Babka 			return alias->sysctl_param;
15990a477e1aSVlastimil Babka 	}
16000a477e1aSVlastimil Babka 
16010a477e1aSVlastimil Babka 	return NULL;
16020a477e1aSVlastimil Babka }
16030a477e1aSVlastimil Babka 
sysctl_is_alias(char * param)16048001f493SKrister Johansen bool sysctl_is_alias(char *param)
16058001f493SKrister Johansen {
16068001f493SKrister Johansen 	const char *alias = sysctl_find_alias(param);
16078001f493SKrister Johansen 
16088001f493SKrister Johansen 	return alias != NULL;
16098001f493SKrister Johansen }
16108001f493SKrister Johansen 
16113db978d4SVlastimil Babka /* Set sysctl value passed on kernel command line. */
process_sysctl_arg(char * param,char * val,const char * unused,void * arg)16123db978d4SVlastimil Babka static int process_sysctl_arg(char *param, char *val,
16133db978d4SVlastimil Babka 			       const char *unused, void *arg)
16143db978d4SVlastimil Babka {
16153db978d4SVlastimil Babka 	char *path;
16163db978d4SVlastimil Babka 	struct vfsmount **proc_mnt = arg;
16173db978d4SVlastimil Babka 	struct file_system_type *proc_fs_type;
16183db978d4SVlastimil Babka 	struct file *file;
16193db978d4SVlastimil Babka 	int len;
16203db978d4SVlastimil Babka 	int err;
16213db978d4SVlastimil Babka 	loff_t pos = 0;
16223db978d4SVlastimil Babka 	ssize_t wret;
16233db978d4SVlastimil Babka 
16240a477e1aSVlastimil Babka 	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
16253db978d4SVlastimil Babka 		param += sizeof("sysctl") - 1;
16263db978d4SVlastimil Babka 
16273db978d4SVlastimil Babka 		if (param[0] != '/' && param[0] != '.')
16283db978d4SVlastimil Babka 			return 0;
16293db978d4SVlastimil Babka 
16303db978d4SVlastimil Babka 		param++;
16310a477e1aSVlastimil Babka 	} else {
16320a477e1aSVlastimil Babka 		param = (char *) sysctl_find_alias(param);
16330a477e1aSVlastimil Babka 		if (!param)
16340a477e1aSVlastimil Babka 			return 0;
16350a477e1aSVlastimil Babka 	}
16363db978d4SVlastimil Babka 
1637697edcb0SXiaoming Ni 	if (!val)
1638697edcb0SXiaoming Ni 		return -EINVAL;
1639697edcb0SXiaoming Ni 	len = strlen(val);
1640697edcb0SXiaoming Ni 	if (len == 0)
1641697edcb0SXiaoming Ni 		return -EINVAL;
1642697edcb0SXiaoming Ni 
16433db978d4SVlastimil Babka 	/*
16443db978d4SVlastimil Babka 	 * To set sysctl options, we use a temporary mount of proc, look up the
16453db978d4SVlastimil Babka 	 * respective sys/ file and write to it. To avoid mounting it when no
16463db978d4SVlastimil Babka 	 * options were given, we mount it only when the first sysctl option is
16473db978d4SVlastimil Babka 	 * found. Why not a persistent mount? There are problems with a
16483db978d4SVlastimil Babka 	 * persistent mount of proc in that it forces userspace not to use any
16493db978d4SVlastimil Babka 	 * proc mount options.
16503db978d4SVlastimil Babka 	 */
16513db978d4SVlastimil Babka 	if (!*proc_mnt) {
16523db978d4SVlastimil Babka 		proc_fs_type = get_fs_type("proc");
16533db978d4SVlastimil Babka 		if (!proc_fs_type) {
16543db978d4SVlastimil Babka 			pr_err("Failed to find procfs to set sysctl from command line\n");
16553db978d4SVlastimil Babka 			return 0;
16563db978d4SVlastimil Babka 		}
16573db978d4SVlastimil Babka 		*proc_mnt = kern_mount(proc_fs_type);
16583db978d4SVlastimil Babka 		put_filesystem(proc_fs_type);
16593db978d4SVlastimil Babka 		if (IS_ERR(*proc_mnt)) {
16603db978d4SVlastimil Babka 			pr_err("Failed to mount procfs to set sysctl from command line\n");
16613db978d4SVlastimil Babka 			return 0;
16623db978d4SVlastimil Babka 		}
16633db978d4SVlastimil Babka 	}
16643db978d4SVlastimil Babka 
16653db978d4SVlastimil Babka 	path = kasprintf(GFP_KERNEL, "sys/%s", param);
16663db978d4SVlastimil Babka 	if (!path)
16673db978d4SVlastimil Babka 		panic("%s: Failed to allocate path for %s\n", __func__, param);
16683db978d4SVlastimil Babka 	strreplace(path, '.', '/');
16693db978d4SVlastimil Babka 
1670ffb37ca3SAl Viro 	file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
16713db978d4SVlastimil Babka 	if (IS_ERR(file)) {
16723db978d4SVlastimil Babka 		err = PTR_ERR(file);
16733db978d4SVlastimil Babka 		if (err == -ENOENT)
16743db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
16753db978d4SVlastimil Babka 				param, val);
16763db978d4SVlastimil Babka 		else if (err == -EACCES)
16773db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
16783db978d4SVlastimil Babka 				param, val);
16793db978d4SVlastimil Babka 		else
16803db978d4SVlastimil Babka 			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
16813db978d4SVlastimil Babka 				file, param, val);
16823db978d4SVlastimil Babka 		goto out;
16833db978d4SVlastimil Babka 	}
16843db978d4SVlastimil Babka 	wret = kernel_write(file, val, len, &pos);
16853db978d4SVlastimil Babka 	if (wret < 0) {
16863db978d4SVlastimil Babka 		err = wret;
16873db978d4SVlastimil Babka 		if (err == -EINVAL)
16883db978d4SVlastimil Babka 			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
16893db978d4SVlastimil Babka 				param, val);
16903db978d4SVlastimil Babka 		else
16913db978d4SVlastimil Babka 			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
16923db978d4SVlastimil Babka 				ERR_PTR(err), param, val);
16933db978d4SVlastimil Babka 	} else if (wret != len) {
16943db978d4SVlastimil Babka 		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
16953db978d4SVlastimil Babka 			wret, len, path, param, val);
16963db978d4SVlastimil Babka 	}
16973db978d4SVlastimil Babka 
16983db978d4SVlastimil Babka 	err = filp_close(file, NULL);
16993db978d4SVlastimil Babka 	if (err)
17003db978d4SVlastimil Babka 		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
17013db978d4SVlastimil Babka 			ERR_PTR(err), param, val);
17023db978d4SVlastimil Babka out:
17033db978d4SVlastimil Babka 	kfree(path);
17043db978d4SVlastimil Babka 	return 0;
17053db978d4SVlastimil Babka }
17063db978d4SVlastimil Babka 
do_sysctl_args(void)17073db978d4SVlastimil Babka void do_sysctl_args(void)
17083db978d4SVlastimil Babka {
17093db978d4SVlastimil Babka 	char *command_line;
17103db978d4SVlastimil Babka 	struct vfsmount *proc_mnt = NULL;
17113db978d4SVlastimil Babka 
17123db978d4SVlastimil Babka 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
17133db978d4SVlastimil Babka 	if (!command_line)
17143db978d4SVlastimil Babka 		panic("%s: Failed to allocate copy of command line\n", __func__);
17153db978d4SVlastimil Babka 
17163db978d4SVlastimil Babka 	parse_args("Setting sysctl args", command_line,
17173db978d4SVlastimil Babka 		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
17183db978d4SVlastimil Babka 
17193db978d4SVlastimil Babka 	if (proc_mnt)
17203db978d4SVlastimil Babka 		kern_unmount(proc_mnt);
17213db978d4SVlastimil Babka 
17223db978d4SVlastimil Babka 	kfree(command_line);
17233db978d4SVlastimil Babka }
1724