xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision 835b94e0)
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>
151f87f0b5SEric W. Biederman #include <linux/module.h>
1677b14db5SEric W. Biederman #include "internal.h"
1777b14db5SEric W. Biederman 
18d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1977b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
2003a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
219043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
229043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
2377b14db5SEric W. Biederman 
24f9bd6733SEric W. Biederman /* Support for permanently empty directories */
25f9bd6733SEric W. Biederman 
26f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = {
27f9bd6733SEric W. Biederman 	{ }
28f9bd6733SEric W. Biederman };
29f9bd6733SEric W. Biederman 
30f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head)
31f9bd6733SEric W. Biederman {
32f9bd6733SEric W. Biederman 	return head->ctl_table[0].child == sysctl_mount_point;
33f9bd6733SEric W. Biederman }
34f9bd6733SEric W. Biederman 
35f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir)
36f9bd6733SEric W. Biederman {
37f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = sysctl_mount_point;
38f9bd6733SEric W. Biederman }
39f9bd6733SEric W. Biederman 
40f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir)
41f9bd6733SEric W. Biederman 
42f9bd6733SEric W. Biederman {
43f9bd6733SEric W. Biederman 	dir->header.ctl_table[0].child = NULL;
44f9bd6733SEric W. Biederman }
45f9bd6733SEric W. Biederman 
46f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
47f1ecf068SLucas De Marchi {
48f1ecf068SLucas De Marchi 	if (!poll)
49f1ecf068SLucas De Marchi 		return;
50f1ecf068SLucas De Marchi 
51f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
52f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
53f1ecf068SLucas De Marchi }
54f1ecf068SLucas De Marchi 
55a194558eSEric W. Biederman static struct ctl_table root_table[] = {
56a194558eSEric W. Biederman 	{
57a194558eSEric W. Biederman 		.procname = "",
587ec66d06SEric W. Biederman 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
59a194558eSEric W. Biederman 	},
60a194558eSEric W. Biederman 	{ }
61a194558eSEric W. Biederman };
620e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = {
630e47c99dSEric W. Biederman 	.default_set.dir.header = {
641f87f0b5SEric W. Biederman 		{{.count = 1,
65938aaa4fSEric W. Biederman 		  .nreg = 1,
66ac13ac6fSEric W. Biederman 		  .ctl_table = root_table }},
670e47c99dSEric W. Biederman 		.ctl_table_arg = root_table,
681f87f0b5SEric W. Biederman 		.root = &sysctl_table_root,
691f87f0b5SEric W. Biederman 		.set = &sysctl_table_root.default_set,
707ec66d06SEric W. Biederman 	},
711f87f0b5SEric W. Biederman };
721f87f0b5SEric W. Biederman 
731f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock);
741f87f0b5SEric W. Biederman 
757ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header);
760e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
7713bcc6a2SEric W. Biederman 	struct ctl_table **pentry);
780e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head);
790e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header);
807ec66d06SEric W. Biederman 
816980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir)
826980128fSEric W. Biederman {
836980128fSEric W. Biederman 	if (dir->header.parent)
846980128fSEric W. Biederman 		sysctl_print_dir(dir->header.parent);
8587ebdc00SAndrew Morton 	pr_cont("%s/", dir->header.ctl_table[0].procname);
866980128fSEric W. Biederman }
876980128fSEric W. Biederman 
88076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2)
89076c3eedSEric W. Biederman {
90076c3eedSEric W. Biederman 	int minlen;
91076c3eedSEric W. Biederman 	int cmp;
92076c3eedSEric W. Biederman 
93076c3eedSEric W. Biederman 	minlen = len1;
94076c3eedSEric W. Biederman 	if (minlen > len2)
95076c3eedSEric W. Biederman 		minlen = len2;
96076c3eedSEric W. Biederman 
97076c3eedSEric W. Biederman 	cmp = memcmp(name1, name2, minlen);
98076c3eedSEric W. Biederman 	if (cmp == 0)
99076c3eedSEric W. Biederman 		cmp = len1 - len2;
100076c3eedSEric W. Biederman 	return cmp;
101076c3eedSEric W. Biederman }
102076c3eedSEric W. Biederman 
10360f126d9SEric W. Biederman /* Called under sysctl_lock */
104076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead,
1050e47c99dSEric W. Biederman 	struct ctl_dir *dir, const char *name, int namelen)
106076c3eedSEric W. Biederman {
107076c3eedSEric W. Biederman 	struct ctl_table_header *head;
108076c3eedSEric W. Biederman 	struct ctl_table *entry;
109ac13ac6fSEric W. Biederman 	struct rb_node *node = dir->root.rb_node;
110076c3eedSEric W. Biederman 
111ac13ac6fSEric W. Biederman 	while (node)
112ac13ac6fSEric W. Biederman 	{
113ac13ac6fSEric W. Biederman 		struct ctl_node *ctl_node;
114ac13ac6fSEric W. Biederman 		const char *procname;
115ac13ac6fSEric W. Biederman 		int cmp;
116ac13ac6fSEric W. Biederman 
117ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
118ac13ac6fSEric W. Biederman 		head = ctl_node->header;
119ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
120ac13ac6fSEric W. Biederman 		procname = entry->procname;
121ac13ac6fSEric W. Biederman 
122ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, procname, strlen(procname));
123ac13ac6fSEric W. Biederman 		if (cmp < 0)
124ac13ac6fSEric W. Biederman 			node = node->rb_left;
125ac13ac6fSEric W. Biederman 		else if (cmp > 0)
126ac13ac6fSEric W. Biederman 			node = node->rb_right;
127ac13ac6fSEric W. Biederman 		else {
128076c3eedSEric W. Biederman 			*phead = head;
129076c3eedSEric W. Biederman 			return entry;
130076c3eedSEric W. Biederman 		}
131076c3eedSEric W. Biederman 	}
132076c3eedSEric W. Biederman 	return NULL;
133076c3eedSEric W. Biederman }
134076c3eedSEric W. Biederman 
135ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
136ac13ac6fSEric W. Biederman {
137ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
138ac13ac6fSEric W. Biederman 	struct rb_node **p = &head->parent->root.rb_node;
139ac13ac6fSEric W. Biederman 	struct rb_node *parent = NULL;
140ac13ac6fSEric W. Biederman 	const char *name = entry->procname;
141ac13ac6fSEric W. Biederman 	int namelen = strlen(name);
142ac13ac6fSEric W. Biederman 
143ac13ac6fSEric W. Biederman 	while (*p) {
144ac13ac6fSEric W. Biederman 		struct ctl_table_header *parent_head;
145ac13ac6fSEric W. Biederman 		struct ctl_table *parent_entry;
146ac13ac6fSEric W. Biederman 		struct ctl_node *parent_node;
147ac13ac6fSEric W. Biederman 		const char *parent_name;
148ac13ac6fSEric W. Biederman 		int cmp;
149ac13ac6fSEric W. Biederman 
150ac13ac6fSEric W. Biederman 		parent = *p;
151ac13ac6fSEric W. Biederman 		parent_node = rb_entry(parent, struct ctl_node, node);
152ac13ac6fSEric W. Biederman 		parent_head = parent_node->header;
153ac13ac6fSEric W. Biederman 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
154ac13ac6fSEric W. Biederman 		parent_name = parent_entry->procname;
155ac13ac6fSEric W. Biederman 
156ac13ac6fSEric W. Biederman 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
157ac13ac6fSEric W. Biederman 		if (cmp < 0)
158ac13ac6fSEric W. Biederman 			p = &(*p)->rb_left;
159ac13ac6fSEric W. Biederman 		else if (cmp > 0)
160ac13ac6fSEric W. Biederman 			p = &(*p)->rb_right;
161ac13ac6fSEric W. Biederman 		else {
16287ebdc00SAndrew Morton 			pr_err("sysctl duplicate entry: ");
163ac13ac6fSEric W. Biederman 			sysctl_print_dir(head->parent);
16487ebdc00SAndrew Morton 			pr_cont("/%s\n", entry->procname);
165ac13ac6fSEric W. Biederman 			return -EEXIST;
166ac13ac6fSEric W. Biederman 		}
167ac13ac6fSEric W. Biederman 	}
168ac13ac6fSEric W. Biederman 
169ac13ac6fSEric W. Biederman 	rb_link_node(node, parent, p);
170ea5272f5SMichel Lespinasse 	rb_insert_color(node, &head->parent->root);
171ac13ac6fSEric W. Biederman 	return 0;
172ac13ac6fSEric W. Biederman }
173ac13ac6fSEric W. Biederman 
174ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
175ac13ac6fSEric W. Biederman {
176ac13ac6fSEric W. Biederman 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
177ac13ac6fSEric W. Biederman 
178ac13ac6fSEric W. Biederman 	rb_erase(node, &head->parent->root);
179ac13ac6fSEric W. Biederman }
180ac13ac6fSEric W. Biederman 
181e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head,
182e0d04529SEric W. Biederman 	struct ctl_table_root *root, struct ctl_table_set *set,
183ac13ac6fSEric W. Biederman 	struct ctl_node *node, struct ctl_table *table)
184e0d04529SEric W. Biederman {
1857ec66d06SEric W. Biederman 	head->ctl_table = table;
186e0d04529SEric W. Biederman 	head->ctl_table_arg = table;
187e0d04529SEric W. Biederman 	head->used = 0;
188e0d04529SEric W. Biederman 	head->count = 1;
189e0d04529SEric W. Biederman 	head->nreg = 1;
190e0d04529SEric W. Biederman 	head->unregistering = NULL;
191e0d04529SEric W. Biederman 	head->root = root;
192e0d04529SEric W. Biederman 	head->set = set;
193e0d04529SEric W. Biederman 	head->parent = NULL;
194ac13ac6fSEric W. Biederman 	head->node = node;
1952fd1d2c4SEric W. Biederman 	INIT_HLIST_HEAD(&head->inodes);
196ac13ac6fSEric W. Biederman 	if (node) {
197ac13ac6fSEric W. Biederman 		struct ctl_table *entry;
1984c199a93SMichel Lespinasse 		for (entry = table; entry->procname; entry++, node++)
199ac13ac6fSEric W. Biederman 			node->header = head;
200ac13ac6fSEric W. Biederman 	}
201ac13ac6fSEric W. Biederman }
202e0d04529SEric W. Biederman 
2038425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head)
2048425d6aaSEric W. Biederman {
205ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
206ac13ac6fSEric W. Biederman 	for (entry = head->ctl_table; entry->procname; entry++)
207ac13ac6fSEric W. Biederman 		erase_entry(head, entry);
2088425d6aaSEric W. Biederman }
2098425d6aaSEric W. Biederman 
2100e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
2118425d6aaSEric W. Biederman {
212ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
2130e47c99dSEric W. Biederman 	int err;
2140e47c99dSEric W. Biederman 
215f9bd6733SEric W. Biederman 	/* Is this a permanently empty directory? */
216f9bd6733SEric W. Biederman 	if (is_empty_dir(&dir->header))
217f9bd6733SEric W. Biederman 		return -EROFS;
218f9bd6733SEric W. Biederman 
219f9bd6733SEric W. Biederman 	/* Am I creating a permanently empty directory? */
220f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point) {
221f9bd6733SEric W. Biederman 		if (!RB_EMPTY_ROOT(&dir->root))
222f9bd6733SEric W. Biederman 			return -EINVAL;
223f9bd6733SEric W. Biederman 		set_empty_dir(dir);
224f9bd6733SEric W. Biederman 	}
225f9bd6733SEric W. Biederman 
2260e47c99dSEric W. Biederman 	dir->header.nreg++;
2277ec66d06SEric W. Biederman 	header->parent = dir;
2280e47c99dSEric W. Biederman 	err = insert_links(header);
2290e47c99dSEric W. Biederman 	if (err)
2300e47c99dSEric W. Biederman 		goto fail_links;
231ac13ac6fSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
232ac13ac6fSEric W. Biederman 		err = insert_entry(header, entry);
233ac13ac6fSEric W. Biederman 		if (err)
234ac13ac6fSEric W. Biederman 			goto fail;
235ac13ac6fSEric W. Biederman 	}
2360e47c99dSEric W. Biederman 	return 0;
237ac13ac6fSEric W. Biederman fail:
238ac13ac6fSEric W. Biederman 	erase_header(header);
239ac13ac6fSEric W. Biederman 	put_links(header);
2400e47c99dSEric W. Biederman fail_links:
241f9bd6733SEric W. Biederman 	if (header->ctl_table == sysctl_mount_point)
242f9bd6733SEric W. Biederman 		clear_empty_dir(dir);
2430e47c99dSEric W. Biederman 	header->parent = NULL;
2440e47c99dSEric W. Biederman 	drop_sysctl_table(&dir->header);
2450e47c99dSEric W. Biederman 	return err;
2468425d6aaSEric W. Biederman }
2478425d6aaSEric W. Biederman 
2481f87f0b5SEric W. Biederman /* called under sysctl_lock */
2491f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p)
2501f87f0b5SEric W. Biederman {
2511f87f0b5SEric W. Biederman 	if (unlikely(p->unregistering))
2521f87f0b5SEric W. Biederman 		return 0;
2531f87f0b5SEric W. Biederman 	p->used++;
2541f87f0b5SEric W. Biederman 	return 1;
2551f87f0b5SEric W. Biederman }
2561f87f0b5SEric W. Biederman 
2571f87f0b5SEric W. Biederman /* called under sysctl_lock */
2581f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p)
2591f87f0b5SEric W. Biederman {
2601f87f0b5SEric W. Biederman 	if (!--p->used)
2611f87f0b5SEric W. Biederman 		if (unlikely(p->unregistering))
2621f87f0b5SEric W. Biederman 			complete(p->unregistering);
2631f87f0b5SEric W. Biederman }
2641f87f0b5SEric W. Biederman 
265d6cffbbeSKonstantin Khlebnikov static void proc_sys_prune_dcache(struct ctl_table_header *head)
266d6cffbbeSKonstantin Khlebnikov {
2672fd1d2c4SEric W. Biederman 	struct inode *inode;
268d6cffbbeSKonstantin Khlebnikov 	struct proc_inode *ei;
2692fd1d2c4SEric W. Biederman 	struct hlist_node *node;
2702fd1d2c4SEric W. Biederman 	struct super_block *sb;
271d6cffbbeSKonstantin Khlebnikov 
272ace0c791SEric W. Biederman 	rcu_read_lock();
2732fd1d2c4SEric W. Biederman 	for (;;) {
2742fd1d2c4SEric W. Biederman 		node = hlist_first_rcu(&head->inodes);
2752fd1d2c4SEric W. Biederman 		if (!node)
2762fd1d2c4SEric W. Biederman 			break;
2772fd1d2c4SEric W. Biederman 		ei = hlist_entry(node, struct proc_inode, sysctl_inodes);
2782fd1d2c4SEric W. Biederman 		spin_lock(&sysctl_lock);
2792fd1d2c4SEric W. Biederman 		hlist_del_init_rcu(&ei->sysctl_inodes);
2802fd1d2c4SEric W. Biederman 		spin_unlock(&sysctl_lock);
2812fd1d2c4SEric W. Biederman 
2822fd1d2c4SEric W. Biederman 		inode = &ei->vfs_inode;
2832fd1d2c4SEric W. Biederman 		sb = inode->i_sb;
2842fd1d2c4SEric W. Biederman 		if (!atomic_inc_not_zero(&sb->s_active))
2852fd1d2c4SEric W. Biederman 			continue;
2862fd1d2c4SEric W. Biederman 		inode = igrab(inode);
287ace0c791SEric W. Biederman 		rcu_read_unlock();
2882fd1d2c4SEric W. Biederman 		if (unlikely(!inode)) {
2892fd1d2c4SEric W. Biederman 			deactivate_super(sb);
2902fd1d2c4SEric W. Biederman 			rcu_read_lock();
2912fd1d2c4SEric W. Biederman 			continue;
2922fd1d2c4SEric W. Biederman 		}
2932fd1d2c4SEric W. Biederman 
294d6cffbbeSKonstantin Khlebnikov 		d_prune_aliases(inode);
2952fd1d2c4SEric W. Biederman 		iput(inode);
2962fd1d2c4SEric W. Biederman 		deactivate_super(sb);
2972fd1d2c4SEric W. Biederman 
298ace0c791SEric W. Biederman 		rcu_read_lock();
299d6cffbbeSKonstantin Khlebnikov 	}
300ace0c791SEric W. Biederman 	rcu_read_unlock();
301d6cffbbeSKonstantin Khlebnikov }
302d6cffbbeSKonstantin Khlebnikov 
3031f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */
3041f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p)
3051f87f0b5SEric W. Biederman {
3061f87f0b5SEric W. Biederman 	/*
3071f87f0b5SEric W. Biederman 	 * if p->used is 0, nobody will ever touch that entry again;
3081f87f0b5SEric W. Biederman 	 * we'll eliminate all paths to it before dropping sysctl_lock
3091f87f0b5SEric W. Biederman 	 */
3101f87f0b5SEric W. Biederman 	if (unlikely(p->used)) {
3111f87f0b5SEric W. Biederman 		struct completion wait;
3121f87f0b5SEric W. Biederman 		init_completion(&wait);
3131f87f0b5SEric W. Biederman 		p->unregistering = &wait;
3141f87f0b5SEric W. Biederman 		spin_unlock(&sysctl_lock);
3151f87f0b5SEric W. Biederman 		wait_for_completion(&wait);
3161f87f0b5SEric W. Biederman 	} else {
3171f87f0b5SEric W. Biederman 		/* anything non-NULL; we'll never dereference it */
3181f87f0b5SEric W. Biederman 		p->unregistering = ERR_PTR(-EINVAL);
319ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
3201f87f0b5SEric W. Biederman 	}
3211f87f0b5SEric W. Biederman 	/*
322d6cffbbeSKonstantin Khlebnikov 	 * Prune dentries for unregistered sysctls: namespaced sysctls
323d6cffbbeSKonstantin Khlebnikov 	 * can have duplicate names and contaminate dcache very badly.
324d6cffbbeSKonstantin Khlebnikov 	 */
325d6cffbbeSKonstantin Khlebnikov 	proc_sys_prune_dcache(p);
326d6cffbbeSKonstantin Khlebnikov 	/*
3271f87f0b5SEric W. Biederman 	 * do not remove from the list until nobody holds it; walking the
3281f87f0b5SEric W. Biederman 	 * list in do_sysctl() relies on that.
3291f87f0b5SEric W. Biederman 	 */
330ace0c791SEric W. Biederman 	spin_lock(&sysctl_lock);
3318425d6aaSEric W. Biederman 	erase_header(p);
3321f87f0b5SEric W. Biederman }
3331f87f0b5SEric W. Biederman 
3341f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
3351f87f0b5SEric W. Biederman {
336ab4a1f24SPrasad Joshi 	BUG_ON(!head);
3371f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3381f87f0b5SEric W. Biederman 	if (!use_table(head))
3391f87f0b5SEric W. Biederman 		head = ERR_PTR(-ENOENT);
3401f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3411f87f0b5SEric W. Biederman 	return head;
3421f87f0b5SEric W. Biederman }
3431f87f0b5SEric W. Biederman 
3441f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head)
3451f87f0b5SEric W. Biederman {
3461f87f0b5SEric W. Biederman 	if (!head)
3471f87f0b5SEric W. Biederman 		return;
3481f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
3491f87f0b5SEric W. Biederman 	unuse_table(head);
3501f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
3511f87f0b5SEric W. Biederman }
3521f87f0b5SEric W. Biederman 
3531f87f0b5SEric W. Biederman static struct ctl_table_set *
35413bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root)
3551f87f0b5SEric W. Biederman {
3561f87f0b5SEric W. Biederman 	struct ctl_table_set *set = &root->default_set;
3571f87f0b5SEric W. Biederman 	if (root->lookup)
35813bcc6a2SEric W. Biederman 		set = root->lookup(root);
3591f87f0b5SEric W. Biederman 	return set;
3601f87f0b5SEric W. Biederman }
3611f87f0b5SEric W. Biederman 
362076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
3637ec66d06SEric W. Biederman 				      struct ctl_dir *dir,
364076c3eedSEric W. Biederman 				      const char *name, int namelen)
365076c3eedSEric W. Biederman {
366076c3eedSEric W. Biederman 	struct ctl_table_header *head;
367076c3eedSEric W. Biederman 	struct ctl_table *entry;
368076c3eedSEric W. Biederman 
369076c3eedSEric W. Biederman 	spin_lock(&sysctl_lock);
3700e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
371076c3eedSEric W. Biederman 	if (entry && use_table(head))
372076c3eedSEric W. Biederman 		*phead = head;
373076c3eedSEric W. Biederman 	else
374076c3eedSEric W. Biederman 		entry = NULL;
375076c3eedSEric W. Biederman 	spin_unlock(&sysctl_lock);
376076c3eedSEric W. Biederman 	return entry;
377076c3eedSEric W. Biederman }
378076c3eedSEric W. Biederman 
379ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node)
3801f87f0b5SEric W. Biederman {
381ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3821f87f0b5SEric W. Biederman 
383ac13ac6fSEric W. Biederman 	for (;node; node = rb_next(node)) {
384ac13ac6fSEric W. Biederman 		ctl_node = rb_entry(node, struct ctl_node, node);
385ac13ac6fSEric W. Biederman 		if (use_table(ctl_node->header))
386ac13ac6fSEric W. Biederman 			return ctl_node;
3871f87f0b5SEric W. Biederman 	}
3881f87f0b5SEric W. Biederman 	return NULL;
3891f87f0b5SEric W. Biederman }
3901f87f0b5SEric W. Biederman 
3917ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir,
3926a75ce16SEric W. Biederman 	struct ctl_table_header **phead, struct ctl_table **pentry)
3931f87f0b5SEric W. Biederman {
394ac13ac6fSEric W. Biederman 	struct ctl_table_header *head = NULL;
3957ec66d06SEric W. Biederman 	struct ctl_table *entry = NULL;
396ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node;
3976a75ce16SEric W. Biederman 
3986a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
399ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_first(&dir->root));
4006a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
401ac13ac6fSEric W. Biederman 	if (ctl_node) {
402ac13ac6fSEric W. Biederman 		head = ctl_node->header;
403ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
404ac13ac6fSEric W. Biederman 	}
4056a75ce16SEric W. Biederman 	*phead = head;
4066a75ce16SEric W. Biederman 	*pentry = entry;
4076a75ce16SEric W. Biederman }
4086a75ce16SEric W. Biederman 
4097ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
4106a75ce16SEric W. Biederman {
4116a75ce16SEric W. Biederman 	struct ctl_table_header *head = *phead;
4126a75ce16SEric W. Biederman 	struct ctl_table *entry = *pentry;
413ac13ac6fSEric W. Biederman 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
4146a75ce16SEric W. Biederman 
4156a75ce16SEric W. Biederman 	spin_lock(&sysctl_lock);
4166a75ce16SEric W. Biederman 	unuse_table(head);
417ac13ac6fSEric W. Biederman 
418ac13ac6fSEric W. Biederman 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
4196a75ce16SEric W. Biederman 	spin_unlock(&sysctl_lock);
420ac13ac6fSEric W. Biederman 	head = NULL;
421ac13ac6fSEric W. Biederman 	if (ctl_node) {
422ac13ac6fSEric W. Biederman 		head = ctl_node->header;
423ac13ac6fSEric W. Biederman 		entry = &head->ctl_table[ctl_node - head->node];
4246a75ce16SEric W. Biederman 	}
4256a75ce16SEric W. Biederman 	*phead = head;
4266a75ce16SEric W. Biederman 	*pentry = entry;
4271f87f0b5SEric W. Biederman }
4281f87f0b5SEric W. Biederman 
4291f87f0b5SEric W. Biederman /*
4301f87f0b5SEric W. Biederman  * sysctl_perm does NOT grant the superuser all rights automatically, because
4311f87f0b5SEric W. Biederman  * some sysctl variables are readonly even to root.
4321f87f0b5SEric W. Biederman  */
4331f87f0b5SEric W. Biederman 
4341f87f0b5SEric W. Biederman static int test_perm(int mode, int op)
4351f87f0b5SEric W. Biederman {
436091bd3eaSEric W. Biederman 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
4371f87f0b5SEric W. Biederman 		mode >>= 6;
438091bd3eaSEric W. Biederman 	else if (in_egroup_p(GLOBAL_ROOT_GID))
4391f87f0b5SEric W. Biederman 		mode >>= 3;
4401f87f0b5SEric W. Biederman 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
4411f87f0b5SEric W. Biederman 		return 0;
4421f87f0b5SEric W. Biederman 	return -EACCES;
4431f87f0b5SEric W. Biederman }
4441f87f0b5SEric W. Biederman 
44573f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
4461f87f0b5SEric W. Biederman {
44773f7ef43SEric W. Biederman 	struct ctl_table_root *root = head->root;
4481f87f0b5SEric W. Biederman 	int mode;
4491f87f0b5SEric W. Biederman 
4501f87f0b5SEric W. Biederman 	if (root->permissions)
45173f7ef43SEric W. Biederman 		mode = root->permissions(head, table);
4521f87f0b5SEric W. Biederman 	else
4531f87f0b5SEric W. Biederman 		mode = table->mode;
4541f87f0b5SEric W. Biederman 
4551f87f0b5SEric W. Biederman 	return test_perm(mode, op);
4561f87f0b5SEric W. Biederman }
4571f87f0b5SEric W. Biederman 
4589043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
4599043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
46077b14db5SEric W. Biederman {
461e79c6a4fSDmitry Torokhov 	struct ctl_table_root *root = head->root;
46277b14db5SEric W. Biederman 	struct inode *inode;
4639043476fSAl Viro 	struct proc_inode *ei;
46477b14db5SEric W. Biederman 
4659043476fSAl Viro 	inode = new_inode(sb);
46677b14db5SEric W. Biederman 	if (!inode)
46777b14db5SEric W. Biederman 		goto out;
46877b14db5SEric W. Biederman 
46985fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
47085fe4025SChristoph Hellwig 
47177b14db5SEric W. Biederman 	ei = PROC_I(inode);
4729043476fSAl Viro 
473d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
474ace0c791SEric W. Biederman 	if (unlikely(head->unregistering)) {
475ace0c791SEric W. Biederman 		spin_unlock(&sysctl_lock);
476ace0c791SEric W. Biederman 		iput(inode);
477ace0c791SEric W. Biederman 		inode = NULL;
478ace0c791SEric W. Biederman 		goto out;
479ace0c791SEric W. Biederman 	}
480ace0c791SEric W. Biederman 	ei->sysctl = head;
481ace0c791SEric W. Biederman 	ei->sysctl_entry = table;
4822fd1d2c4SEric W. Biederman 	hlist_add_head_rcu(&ei->sysctl_inodes, &head->inodes);
483d6cffbbeSKonstantin Khlebnikov 	head->count++;
484d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
485d6cffbbeSKonstantin Khlebnikov 
486078cd827SDeepa Dinamani 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
4879043476fSAl Viro 	inode->i_mode = table->mode;
4887ec66d06SEric W. Biederman 	if (!S_ISDIR(table->mode)) {
4899043476fSAl Viro 		inode->i_mode |= S_IFREG;
49077b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
49177b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
4929043476fSAl Viro 	} else {
4939043476fSAl Viro 		inode->i_mode |= S_IFDIR;
4949043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
4959043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
496f9bd6733SEric W. Biederman 		if (is_empty_dir(head))
497f9bd6733SEric W. Biederman 			make_empty_dir_inode(inode);
4989043476fSAl Viro 	}
499e79c6a4fSDmitry Torokhov 
500e79c6a4fSDmitry Torokhov 	if (root->set_ownership)
501e79c6a4fSDmitry Torokhov 		root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
502e79c6a4fSDmitry Torokhov 
50377b14db5SEric W. Biederman out:
50477b14db5SEric W. Biederman 	return inode;
50577b14db5SEric W. Biederman }
50677b14db5SEric W. Biederman 
507d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
508d6cffbbeSKonstantin Khlebnikov {
509d6cffbbeSKonstantin Khlebnikov 	spin_lock(&sysctl_lock);
5102fd1d2c4SEric W. Biederman 	hlist_del_init_rcu(&PROC_I(inode)->sysctl_inodes);
511d6cffbbeSKonstantin Khlebnikov 	if (!--head->count)
512d6cffbbeSKonstantin Khlebnikov 		kfree_rcu(head, rcu);
513d6cffbbeSKonstantin Khlebnikov 	spin_unlock(&sysctl_lock);
514d6cffbbeSKonstantin Khlebnikov }
515d6cffbbeSKonstantin Khlebnikov 
51681324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
51777b14db5SEric W. Biederman {
5183cc3e046SEric W. Biederman 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
5193cc3e046SEric W. Biederman 	if (!head)
5200e47c99dSEric W. Biederman 		head = &sysctl_table_root.default_set.dir.header;
5213cc3e046SEric W. Biederman 	return sysctl_head_grab(head);
52277b14db5SEric W. Biederman }
52377b14db5SEric W. Biederman 
52477b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
52500cd8dd3SAl Viro 					unsigned int flags)
52677b14db5SEric W. Biederman {
5279043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
5289043476fSAl Viro 	struct ctl_table_header *h = NULL;
529dc12e909SAl Viro 	const struct qstr *name = &dentry->d_name;
5309043476fSAl Viro 	struct ctl_table *p;
53177b14db5SEric W. Biederman 	struct inode *inode;
5329043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
5337ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
5340e47c99dSEric W. Biederman 	int ret;
53577b14db5SEric W. Biederman 
5369043476fSAl Viro 	if (IS_ERR(head))
5379043476fSAl Viro 		return ERR_CAST(head);
5389043476fSAl Viro 
5397ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
5409043476fSAl Viro 
5417ec66d06SEric W. Biederman 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
5429043476fSAl Viro 	if (!p)
54377b14db5SEric W. Biederman 		goto out;
54477b14db5SEric W. Biederman 
5454e757320SEric W. Biederman 	if (S_ISLNK(p->mode)) {
54613bcc6a2SEric W. Biederman 		ret = sysctl_follow_link(&h, &p);
5470e47c99dSEric W. Biederman 		err = ERR_PTR(ret);
5480e47c99dSEric W. Biederman 		if (ret)
5490e47c99dSEric W. Biederman 			goto out;
5504e757320SEric W. Biederman 	}
5510e47c99dSEric W. Biederman 
55277b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
5539043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
55477b14db5SEric W. Biederman 	if (!inode)
55577b14db5SEric W. Biederman 		goto out;
55677b14db5SEric W. Biederman 
55777b14db5SEric W. Biederman 	err = NULL;
558fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
55977b14db5SEric W. Biederman 	d_add(dentry, inode);
56077b14db5SEric W. Biederman 
56177b14db5SEric W. Biederman out:
5626bf61045SFrancesco Ruggeri 	if (h)
5636bf61045SFrancesco Ruggeri 		sysctl_head_finish(h);
56477b14db5SEric W. Biederman 	sysctl_head_finish(head);
56577b14db5SEric W. Biederman 	return err;
56677b14db5SEric W. Biederman }
56777b14db5SEric W. Biederman 
5687708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
5697708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
57077b14db5SEric W. Biederman {
571496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
5729043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
5739043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
5742a2da53bSDavid Howells 	ssize_t error;
5752a2da53bSDavid Howells 	size_t res;
57677b14db5SEric W. Biederman 
5779043476fSAl Viro 	if (IS_ERR(head))
5789043476fSAl Viro 		return PTR_ERR(head);
57977b14db5SEric W. Biederman 
58077b14db5SEric W. Biederman 	/*
58177b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
58277b14db5SEric W. Biederman 	 * and won't be until we finish.
58377b14db5SEric W. Biederman 	 */
58477b14db5SEric W. Biederman 	error = -EPERM;
58573f7ef43SEric W. Biederman 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
58677b14db5SEric W. Biederman 		goto out;
58777b14db5SEric W. Biederman 
5889043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
5899043476fSAl Viro 	error = -EINVAL;
5909043476fSAl Viro 	if (!table->proc_handler)
5919043476fSAl Viro 		goto out;
5929043476fSAl Viro 
59377b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
59477b14db5SEric W. Biederman 	res = count;
5958d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
59677b14db5SEric W. Biederman 	if (!error)
59777b14db5SEric W. Biederman 		error = res;
59877b14db5SEric W. Biederman out:
59977b14db5SEric W. Biederman 	sysctl_head_finish(head);
60077b14db5SEric W. Biederman 
60177b14db5SEric W. Biederman 	return error;
60277b14db5SEric W. Biederman }
60377b14db5SEric W. Biederman 
6047708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
6057708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
6067708bfb1SPavel Emelyanov {
6077708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
6087708bfb1SPavel Emelyanov }
6097708bfb1SPavel Emelyanov 
61077b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
61177b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
61277b14db5SEric W. Biederman {
6137708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
61477b14db5SEric W. Biederman }
61577b14db5SEric W. Biederman 
616f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
617f1ecf068SLucas De Marchi {
6184e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
619f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
620f1ecf068SLucas De Marchi 
6214e474a00SLucas De Marchi 	/* sysctl was unregistered */
6224e474a00SLucas De Marchi 	if (IS_ERR(head))
6234e474a00SLucas De Marchi 		return PTR_ERR(head);
6244e474a00SLucas De Marchi 
625f1ecf068SLucas De Marchi 	if (table->poll)
626f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
627f1ecf068SLucas De Marchi 
6284e474a00SLucas De Marchi 	sysctl_head_finish(head);
6294e474a00SLucas De Marchi 
630f1ecf068SLucas De Marchi 	return 0;
631f1ecf068SLucas De Marchi }
632f1ecf068SLucas De Marchi 
633076ccb76SAl Viro static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
634f1ecf068SLucas De Marchi {
635496ad9aaSAl Viro 	struct inode *inode = file_inode(filp);
6364e474a00SLucas De Marchi 	struct ctl_table_header *head = grab_header(inode);
637f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
638076ccb76SAl Viro 	__poll_t ret = DEFAULT_POLLMASK;
6394e474a00SLucas De Marchi 	unsigned long event;
6404e474a00SLucas De Marchi 
6414e474a00SLucas De Marchi 	/* sysctl was unregistered */
6424e474a00SLucas De Marchi 	if (IS_ERR(head))
643a9a08845SLinus Torvalds 		return EPOLLERR | EPOLLHUP;
644f1ecf068SLucas De Marchi 
645f1ecf068SLucas De Marchi 	if (!table->proc_handler)
646f1ecf068SLucas De Marchi 		goto out;
647f1ecf068SLucas De Marchi 
648f1ecf068SLucas De Marchi 	if (!table->poll)
649f1ecf068SLucas De Marchi 		goto out;
650f1ecf068SLucas De Marchi 
6514e474a00SLucas De Marchi 	event = (unsigned long)filp->private_data;
652f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
653f1ecf068SLucas De Marchi 
654f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
655f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
656a9a08845SLinus Torvalds 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
657f1ecf068SLucas De Marchi 	}
658f1ecf068SLucas De Marchi 
659f1ecf068SLucas De Marchi out:
6604e474a00SLucas De Marchi 	sysctl_head_finish(head);
6614e474a00SLucas De Marchi 
662f1ecf068SLucas De Marchi 	return ret;
663f1ecf068SLucas De Marchi }
66477b14db5SEric W. Biederman 
665f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file,
666f0c3b509SAl Viro 				struct dir_context *ctx,
6679043476fSAl Viro 				struct ctl_table_header *head,
6689043476fSAl Viro 				struct ctl_table *table)
66977b14db5SEric W. Biederman {
670f0c3b509SAl Viro 	struct dentry *child, *dir = file->f_path.dentry;
67177b14db5SEric W. Biederman 	struct inode *inode;
67277b14db5SEric W. Biederman 	struct qstr qname;
67377b14db5SEric W. Biederman 	ino_t ino = 0;
67477b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
67577b14db5SEric W. Biederman 
67677b14db5SEric W. Biederman 	qname.name = table->procname;
67777b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
6788387ff25SLinus Torvalds 	qname.hash = full_name_hash(dir, qname.name, qname.len);
67977b14db5SEric W. Biederman 
68077b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
68177b14db5SEric W. Biederman 	if (!child) {
68276aab3abSAl Viro 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
68376aab3abSAl Viro 		child = d_alloc_parallel(dir, &qname, &wq);
68476aab3abSAl Viro 		if (IS_ERR(child))
68576aab3abSAl Viro 			return false;
68676aab3abSAl Viro 		if (d_in_lookup(child)) {
6879043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
6889043476fSAl Viro 			if (!inode) {
68976aab3abSAl Viro 				d_lookup_done(child);
6909043476fSAl Viro 				dput(child);
691f0c3b509SAl Viro 				return false;
69276aab3abSAl Viro 			}
693fb045adbSNick Piggin 			d_set_d_op(child, &proc_sys_dentry_operations);
6949043476fSAl Viro 			d_add(child, inode);
69577b14db5SEric W. Biederman 		}
69677b14db5SEric W. Biederman 	}
6972b0143b5SDavid Howells 	inode = d_inode(child);
69877b14db5SEric W. Biederman 	ino  = inode->i_ino;
69977b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
70077b14db5SEric W. Biederman 	dput(child);
701f0c3b509SAl Viro 	return dir_emit(ctx, qname.name, qname.len, ino, type);
7029043476fSAl Viro }
7039043476fSAl Viro 
704f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file,
705f0c3b509SAl Viro 				    struct dir_context *ctx,
7060e47c99dSEric W. Biederman 				    struct ctl_table_header *head,
7070e47c99dSEric W. Biederman 				    struct ctl_table *table)
7080e47c99dSEric W. Biederman {
709f0c3b509SAl Viro 	bool ret = true;
710a0b0d1c3SDanilo Krummrich 
7110e47c99dSEric W. Biederman 	head = sysctl_head_grab(head);
712a0b0d1c3SDanilo Krummrich 	if (IS_ERR(head))
713a0b0d1c3SDanilo Krummrich 		return false;
7140e47c99dSEric W. Biederman 
7150e47c99dSEric W. Biederman 	/* It is not an error if we can not follow the link ignore it */
716835b94e0SDanilo Krummrich 	if (sysctl_follow_link(&head, &table))
7170e47c99dSEric W. Biederman 		goto out;
7180e47c99dSEric W. Biederman 
719f0c3b509SAl Viro 	ret = proc_sys_fill_cache(file, ctx, head, table);
7200e47c99dSEric W. Biederman out:
7210e47c99dSEric W. Biederman 	sysctl_head_finish(head);
7220e47c99dSEric W. Biederman 	return ret;
7230e47c99dSEric W. Biederman }
7240e47c99dSEric W. Biederman 
725e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table,
7269043476fSAl Viro 		unsigned long *pos, struct file *file,
727f0c3b509SAl Viro 		struct dir_context *ctx)
7289043476fSAl Viro {
729f0c3b509SAl Viro 	bool res;
7309043476fSAl Viro 
731f0c3b509SAl Viro 	if ((*pos)++ < ctx->pos)
732f0c3b509SAl Viro 		return true;
7339043476fSAl Viro 
7340e47c99dSEric W. Biederman 	if (unlikely(S_ISLNK(table->mode)))
735f0c3b509SAl Viro 		res = proc_sys_link_fill_cache(file, ctx, head, table);
7360e47c99dSEric W. Biederman 	else
737f0c3b509SAl Viro 		res = proc_sys_fill_cache(file, ctx, head, table);
7389043476fSAl Viro 
739f0c3b509SAl Viro 	if (res)
740f0c3b509SAl Viro 		ctx->pos = *pos;
7416a75ce16SEric W. Biederman 
7426a75ce16SEric W. Biederman 	return res;
74377b14db5SEric W. Biederman }
74477b14db5SEric W. Biederman 
745f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
74677b14db5SEric W. Biederman {
747f0c3b509SAl Viro 	struct ctl_table_header *head = grab_header(file_inode(file));
7489043476fSAl Viro 	struct ctl_table_header *h = NULL;
7496a75ce16SEric W. Biederman 	struct ctl_table *entry;
7507ec66d06SEric W. Biederman 	struct ctl_dir *ctl_dir;
75177b14db5SEric W. Biederman 	unsigned long pos;
75277b14db5SEric W. Biederman 
7539043476fSAl Viro 	if (IS_ERR(head))
7549043476fSAl Viro 		return PTR_ERR(head);
7559043476fSAl Viro 
7567ec66d06SEric W. Biederman 	ctl_dir = container_of(head, struct ctl_dir, header);
75777b14db5SEric W. Biederman 
758f0c3b509SAl Viro 	if (!dir_emit_dots(file, ctx))
75993362fa4SZhou Chengming 		goto out;
760f0c3b509SAl Viro 
76177b14db5SEric W. Biederman 	pos = 2;
76277b14db5SEric W. Biederman 
7637ec66d06SEric W. Biederman 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
764f0c3b509SAl Viro 		if (!scan(h, entry, &pos, file, ctx)) {
7659043476fSAl Viro 			sysctl_head_finish(h);
7669043476fSAl Viro 			break;
76777b14db5SEric W. Biederman 		}
76877b14db5SEric W. Biederman 	}
76993362fa4SZhou Chengming out:
77077b14db5SEric W. Biederman 	sysctl_head_finish(head);
771f0c3b509SAl Viro 	return 0;
77277b14db5SEric W. Biederman }
77377b14db5SEric W. Biederman 
77410556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
77577b14db5SEric W. Biederman {
77677b14db5SEric W. Biederman 	/*
77777b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
77877b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
77977b14db5SEric W. Biederman 	 */
780f696a365SMiklos Szeredi 	struct ctl_table_header *head;
781f696a365SMiklos Szeredi 	struct ctl_table *table;
78277b14db5SEric W. Biederman 	int error;
78377b14db5SEric W. Biederman 
784f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
785f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
786f696a365SMiklos Szeredi 		return -EACCES;
787f696a365SMiklos Szeredi 
788f696a365SMiklos Szeredi 	head = grab_header(inode);
7899043476fSAl Viro 	if (IS_ERR(head))
7909043476fSAl Viro 		return PTR_ERR(head);
79177b14db5SEric W. Biederman 
792f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
7939043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
7949043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
7959043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
79673f7ef43SEric W. Biederman 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
7979043476fSAl Viro 
79877b14db5SEric W. Biederman 	sysctl_head_finish(head);
79977b14db5SEric W. Biederman 	return error;
80077b14db5SEric W. Biederman }
80177b14db5SEric W. Biederman 
80277b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
80377b14db5SEric W. Biederman {
8042b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
80577b14db5SEric W. Biederman 	int error;
80677b14db5SEric W. Biederman 
80777b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
80877b14db5SEric W. Biederman 		return -EPERM;
80977b14db5SEric W. Biederman 
81031051c85SJan Kara 	error = setattr_prepare(dentry, attr);
8111025774cSChristoph Hellwig 	if (error)
81277b14db5SEric W. Biederman 		return error;
8131025774cSChristoph Hellwig 
8141025774cSChristoph Hellwig 	setattr_copy(inode, attr);
8151025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8161025774cSChristoph Hellwig 	return 0;
81777b14db5SEric W. Biederman }
81877b14db5SEric W. Biederman 
819a528d35eSDavid Howells static int proc_sys_getattr(const struct path *path, struct kstat *stat,
820a528d35eSDavid Howells 			    u32 request_mask, unsigned int query_flags)
8219043476fSAl Viro {
822a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
8239043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
8249043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
8259043476fSAl Viro 
8269043476fSAl Viro 	if (IS_ERR(head))
8279043476fSAl Viro 		return PTR_ERR(head);
8289043476fSAl Viro 
8299043476fSAl Viro 	generic_fillattr(inode, stat);
8309043476fSAl Viro 	if (table)
8319043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
8329043476fSAl Viro 
8339043476fSAl Viro 	sysctl_head_finish(head);
8349043476fSAl Viro 	return 0;
8359043476fSAl Viro }
8369043476fSAl Viro 
83777b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
838f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
839f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
84077b14db5SEric W. Biederman 	.read		= proc_sys_read,
84177b14db5SEric W. Biederman 	.write		= proc_sys_write,
8426038f373SArnd Bergmann 	.llseek		= default_llseek,
8439043476fSAl Viro };
8449043476fSAl Viro 
8459043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
846887df078SPavel Emelyanov 	.read		= generic_read_dir,
847f50752eaSAl Viro 	.iterate_shared	= proc_sys_readdir,
8483222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
84977b14db5SEric W. Biederman };
85077b14db5SEric W. Biederman 
85103a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
8529043476fSAl Viro 	.permission	= proc_sys_permission,
8539043476fSAl Viro 	.setattr	= proc_sys_setattr,
8549043476fSAl Viro 	.getattr	= proc_sys_getattr,
8559043476fSAl Viro };
8569043476fSAl Viro 
8579043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
85877b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
85977b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
86077b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
8619043476fSAl Viro 	.getattr	= proc_sys_getattr,
86277b14db5SEric W. Biederman };
86377b14db5SEric W. Biederman 
8640b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
86577b14db5SEric W. Biederman {
8660b728e19SAl Viro 	if (flags & LOOKUP_RCU)
86734286d66SNick Piggin 		return -ECHILD;
8682b0143b5SDavid Howells 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
8699043476fSAl Viro }
8709043476fSAl Viro 
871fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
8729043476fSAl Viro {
8732b0143b5SDavid Howells 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
8749043476fSAl Viro }
8759043476fSAl Viro 
8761f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p)
8771f87f0b5SEric W. Biederman {
8781f87f0b5SEric W. Biederman 	struct ctl_table_set *set = p->set;
8791f87f0b5SEric W. Biederman 	int res;
8801f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
8811f87f0b5SEric W. Biederman 	if (p->unregistering)
8821f87f0b5SEric W. Biederman 		res = 0;
8831f87f0b5SEric W. Biederman 	else if (!set->is_seen)
8841f87f0b5SEric W. Biederman 		res = 1;
8851f87f0b5SEric W. Biederman 	else
8861f87f0b5SEric W. Biederman 		res = set->is_seen(set);
8871f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
8881f87f0b5SEric W. Biederman 	return res;
8891f87f0b5SEric W. Biederman }
8901f87f0b5SEric W. Biederman 
8916fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry,
892621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
8939043476fSAl Viro {
894dfef6dcdSAl Viro 	struct ctl_table_header *head;
895da53be12SLinus Torvalds 	struct inode *inode;
896da53be12SLinus Torvalds 
89731e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
89831e6b01fSNick Piggin 	 * that inode here can be NULL */
899dfef6dcdSAl Viro 	/* AV: can it, indeed? */
9002b0143b5SDavid Howells 	inode = d_inode_rcu(dentry);
90131e6b01fSNick Piggin 	if (!inode)
902dfef6dcdSAl Viro 		return 1;
903621e155aSNick Piggin 	if (name->len != len)
9049043476fSAl Viro 		return 1;
905621e155aSNick Piggin 	if (memcmp(name->name, str, len))
9069043476fSAl Viro 		return 1;
907dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
908dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
90977b14db5SEric W. Biederman }
91077b14db5SEric W. Biederman 
911d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
91277b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
9139043476fSAl Viro 	.d_delete	= proc_sys_delete,
9149043476fSAl Viro 	.d_compare	= proc_sys_compare,
91577b14db5SEric W. Biederman };
91677b14db5SEric W. Biederman 
9170e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir,
9187ec66d06SEric W. Biederman 				   const char *name, int namelen)
9191f87f0b5SEric W. Biederman {
9207ec66d06SEric W. Biederman 	struct ctl_table_header *head;
9217ec66d06SEric W. Biederman 	struct ctl_table *entry;
9221f87f0b5SEric W. Biederman 
9230e47c99dSEric W. Biederman 	entry = find_entry(&head, dir, name, namelen);
9247ec66d06SEric W. Biederman 	if (!entry)
9257ec66d06SEric W. Biederman 		return ERR_PTR(-ENOENT);
92651f72f4aSEric W. Biederman 	if (!S_ISDIR(entry->mode))
9277ec66d06SEric W. Biederman 		return ERR_PTR(-ENOTDIR);
92851f72f4aSEric W. Biederman 	return container_of(head, struct ctl_dir, header);
9291f87f0b5SEric W. Biederman }
9301f87f0b5SEric W. Biederman 
9317ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set,
9327ec66d06SEric W. Biederman 			       const char *name, int namelen)
9331f87f0b5SEric W. Biederman {
9347ec66d06SEric W. Biederman 	struct ctl_table *table;
9357ec66d06SEric W. Biederman 	struct ctl_dir *new;
936ac13ac6fSEric W. Biederman 	struct ctl_node *node;
9377ec66d06SEric W. Biederman 	char *new_name;
9381f87f0b5SEric W. Biederman 
939ac13ac6fSEric W. Biederman 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
940ac13ac6fSEric W. Biederman 		      sizeof(struct ctl_table)*2 +  namelen + 1,
941ac13ac6fSEric W. Biederman 		      GFP_KERNEL);
9427ec66d06SEric W. Biederman 	if (!new)
9437ec66d06SEric W. Biederman 		return NULL;
9447ec66d06SEric W. Biederman 
945ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(new + 1);
946ac13ac6fSEric W. Biederman 	table = (struct ctl_table *)(node + 1);
9477ec66d06SEric W. Biederman 	new_name = (char *)(table + 2);
9487ec66d06SEric W. Biederman 	memcpy(new_name, name, namelen);
9497ec66d06SEric W. Biederman 	new_name[namelen] = '\0';
9507ec66d06SEric W. Biederman 	table[0].procname = new_name;
9517ec66d06SEric W. Biederman 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
952ac13ac6fSEric W. Biederman 	init_header(&new->header, set->dir.header.root, set, node, table);
9537ec66d06SEric W. Biederman 
9547ec66d06SEric W. Biederman 	return new;
9551f87f0b5SEric W. Biederman }
9561f87f0b5SEric W. Biederman 
95760f126d9SEric W. Biederman /**
95860f126d9SEric W. Biederman  * get_subdir - find or create a subdir with the specified name.
95960f126d9SEric W. Biederman  * @dir:  Directory to create the subdirectory in
96060f126d9SEric W. Biederman  * @name: The name of the subdirectory to find or create
96160f126d9SEric W. Biederman  * @namelen: The length of name
96260f126d9SEric W. Biederman  *
96360f126d9SEric W. Biederman  * Takes a directory with an elevated reference count so we know that
96460f126d9SEric W. Biederman  * if we drop the lock the directory will not go away.  Upon success
96560f126d9SEric W. Biederman  * the reference is moved from @dir to the returned subdirectory.
96660f126d9SEric W. Biederman  * Upon error an error code is returned and the reference on @dir is
96760f126d9SEric W. Biederman  * simply dropped.
96860f126d9SEric W. Biederman  */
9690e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir,
9700e47c99dSEric W. Biederman 				  const char *name, int namelen)
9717ec66d06SEric W. Biederman {
9720e47c99dSEric W. Biederman 	struct ctl_table_set *set = dir->header.set;
9737ec66d06SEric W. Biederman 	struct ctl_dir *subdir, *new = NULL;
9740eb97f38SEric W. Biederman 	int err;
9757ec66d06SEric W. Biederman 
9767ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
9770e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
9787ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
9797ec66d06SEric W. Biederman 		goto found;
9807ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
9817ec66d06SEric W. Biederman 		goto failed;
9827ec66d06SEric W. Biederman 
9837ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
9847ec66d06SEric W. Biederman 	new = new_dir(set, name, namelen);
9857ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
9867ec66d06SEric W. Biederman 	subdir = ERR_PTR(-ENOMEM);
9877ec66d06SEric W. Biederman 	if (!new)
9887ec66d06SEric W. Biederman 		goto failed;
9897ec66d06SEric W. Biederman 
99060f126d9SEric W. Biederman 	/* Was the subdir added while we dropped the lock? */
9910e47c99dSEric W. Biederman 	subdir = find_subdir(dir, name, namelen);
9927ec66d06SEric W. Biederman 	if (!IS_ERR(subdir))
9937ec66d06SEric W. Biederman 		goto found;
9947ec66d06SEric W. Biederman 	if (PTR_ERR(subdir) != -ENOENT)
9957ec66d06SEric W. Biederman 		goto failed;
9967ec66d06SEric W. Biederman 
99760f126d9SEric W. Biederman 	/* Nope.  Use the our freshly made directory entry. */
9980eb97f38SEric W. Biederman 	err = insert_header(dir, &new->header);
9990eb97f38SEric W. Biederman 	subdir = ERR_PTR(err);
10000eb97f38SEric W. Biederman 	if (err)
10010e47c99dSEric W. Biederman 		goto failed;
10027ec66d06SEric W. Biederman 	subdir = new;
10037ec66d06SEric W. Biederman found:
10047ec66d06SEric W. Biederman 	subdir->header.nreg++;
10057ec66d06SEric W. Biederman failed:
1006a1c83681SViresh Kumar 	if (IS_ERR(subdir)) {
100787ebdc00SAndrew Morton 		pr_err("sysctl could not get directory: ");
10086980128fSEric W. Biederman 		sysctl_print_dir(dir);
100987ebdc00SAndrew Morton 		pr_cont("/%*.*s %ld\n",
10107ec66d06SEric W. Biederman 			namelen, namelen, name, PTR_ERR(subdir));
10111f87f0b5SEric W. Biederman 	}
10127ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
10137ec66d06SEric W. Biederman 	if (new)
10147ec66d06SEric W. Biederman 		drop_sysctl_table(&new->header);
10157ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
10167ec66d06SEric W. Biederman 	return subdir;
10171f87f0b5SEric W. Biederman }
10181f87f0b5SEric W. Biederman 
10190e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
10200e47c99dSEric W. Biederman {
10210e47c99dSEric W. Biederman 	struct ctl_dir *parent;
10220e47c99dSEric W. Biederman 	const char *procname;
10230e47c99dSEric W. Biederman 	if (!dir->header.parent)
10240e47c99dSEric W. Biederman 		return &set->dir;
10250e47c99dSEric W. Biederman 	parent = xlate_dir(set, dir->header.parent);
10260e47c99dSEric W. Biederman 	if (IS_ERR(parent))
10270e47c99dSEric W. Biederman 		return parent;
10280e47c99dSEric W. Biederman 	procname = dir->header.ctl_table[0].procname;
10290e47c99dSEric W. Biederman 	return find_subdir(parent, procname, strlen(procname));
10300e47c99dSEric W. Biederman }
10310e47c99dSEric W. Biederman 
10320e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead,
103313bcc6a2SEric W. Biederman 	struct ctl_table **pentry)
10340e47c99dSEric W. Biederman {
10350e47c99dSEric W. Biederman 	struct ctl_table_header *head;
10360e47c99dSEric W. Biederman 	struct ctl_table_root *root;
10370e47c99dSEric W. Biederman 	struct ctl_table_set *set;
10380e47c99dSEric W. Biederman 	struct ctl_table *entry;
10390e47c99dSEric W. Biederman 	struct ctl_dir *dir;
10400e47c99dSEric W. Biederman 	int ret;
10410e47c99dSEric W. Biederman 
10420e47c99dSEric W. Biederman 	ret = 0;
10430e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
10440e47c99dSEric W. Biederman 	root = (*pentry)->data;
104513bcc6a2SEric W. Biederman 	set = lookup_header_set(root);
10460e47c99dSEric W. Biederman 	dir = xlate_dir(set, (*phead)->parent);
10470e47c99dSEric W. Biederman 	if (IS_ERR(dir))
10480e47c99dSEric W. Biederman 		ret = PTR_ERR(dir);
10490e47c99dSEric W. Biederman 	else {
10500e47c99dSEric W. Biederman 		const char *procname = (*pentry)->procname;
10510e47c99dSEric W. Biederman 		head = NULL;
10520e47c99dSEric W. Biederman 		entry = find_entry(&head, dir, procname, strlen(procname));
10530e47c99dSEric W. Biederman 		ret = -ENOENT;
10540e47c99dSEric W. Biederman 		if (entry && use_table(head)) {
10550e47c99dSEric W. Biederman 			unuse_table(*phead);
10560e47c99dSEric W. Biederman 			*phead = head;
10570e47c99dSEric W. Biederman 			*pentry = entry;
10580e47c99dSEric W. Biederman 			ret = 0;
10590e47c99dSEric W. Biederman 		}
10600e47c99dSEric W. Biederman 	}
10610e47c99dSEric W. Biederman 
10620e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
10630e47c99dSEric W. Biederman 	return ret;
10640e47c99dSEric W. Biederman }
10650e47c99dSEric W. Biederman 
10667c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
10677c60c48fSEric W. Biederman {
10687c60c48fSEric W. Biederman 	struct va_format vaf;
10697c60c48fSEric W. Biederman 	va_list args;
10707c60c48fSEric W. Biederman 
10717c60c48fSEric W. Biederman 	va_start(args, fmt);
10727c60c48fSEric W. Biederman 	vaf.fmt = fmt;
10737c60c48fSEric W. Biederman 	vaf.va = &args;
10747c60c48fSEric W. Biederman 
107587ebdc00SAndrew Morton 	pr_err("sysctl table check failed: %s/%s %pV\n",
10767c60c48fSEric W. Biederman 	       path, table->procname, &vaf);
10777c60c48fSEric W. Biederman 
10787c60c48fSEric W. Biederman 	va_end(args);
10797c60c48fSEric W. Biederman 	return -EINVAL;
10807c60c48fSEric W. Biederman }
10817c60c48fSEric W. Biederman 
10824f2fec00SLuis R. Rodriguez static int sysctl_check_table_array(const char *path, struct ctl_table *table)
10834f2fec00SLuis R. Rodriguez {
10844f2fec00SLuis R. Rodriguez 	int err = 0;
10854f2fec00SLuis R. Rodriguez 
108661d9b56aSLuis R. Rodriguez 	if ((table->proc_handler == proc_douintvec) ||
108761d9b56aSLuis R. Rodriguez 	    (table->proc_handler == proc_douintvec_minmax)) {
10884f2fec00SLuis R. Rodriguez 		if (table->maxlen != sizeof(unsigned int))
10894f2fec00SLuis R. Rodriguez 			err |= sysctl_err(path, table, "array now allowed");
10904f2fec00SLuis R. Rodriguez 	}
10914f2fec00SLuis R. Rodriguez 
10924f2fec00SLuis R. Rodriguez 	return err;
10934f2fec00SLuis R. Rodriguez }
10944f2fec00SLuis R. Rodriguez 
10957c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table)
10967c60c48fSEric W. Biederman {
10977c60c48fSEric W. Biederman 	int err = 0;
10987c60c48fSEric W. Biederman 	for (; table->procname; table++) {
10997c60c48fSEric W. Biederman 		if (table->child)
110089c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "Not a file");
11017c60c48fSEric W. Biederman 
11021f87f0b5SEric W. Biederman 		if ((table->proc_handler == proc_dostring) ||
11031f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec) ||
11041680a386SLiping Zhang 		    (table->proc_handler == proc_douintvec) ||
110561d9b56aSLuis R. Rodriguez 		    (table->proc_handler == proc_douintvec_minmax) ||
11061f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_minmax) ||
11071f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_jiffies) ||
11081f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_userhz_jiffies) ||
11091f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_dointvec_ms_jiffies) ||
11101f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_minmax) ||
11111f87f0b5SEric W. Biederman 		    (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
11121f87f0b5SEric W. Biederman 			if (!table->data)
111389c5b53bSLuis R. Rodriguez 				err |= sysctl_err(path, table, "No data");
11141f87f0b5SEric W. Biederman 			if (!table->maxlen)
111589c5b53bSLuis R. Rodriguez 				err |= sysctl_err(path, table, "No maxlen");
11164f2fec00SLuis R. Rodriguez 			else
11174f2fec00SLuis R. Rodriguez 				err |= sysctl_check_table_array(path, table);
11181f87f0b5SEric W. Biederman 		}
11191f87f0b5SEric W. Biederman 		if (!table->proc_handler)
112089c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "No proc_handler");
11217c60c48fSEric W. Biederman 
11227c60c48fSEric W. Biederman 		if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
112389c5b53bSLuis R. Rodriguez 			err |= sysctl_err(path, table, "bogus .mode 0%o",
11247c60c48fSEric W. Biederman 				table->mode);
11251f87f0b5SEric W. Biederman 	}
11267c60c48fSEric W. Biederman 	return err;
11271f87f0b5SEric W. Biederman }
11281f87f0b5SEric W. Biederman 
11290e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
11300e47c99dSEric W. Biederman 	struct ctl_table_root *link_root)
11310e47c99dSEric W. Biederman {
11320e47c99dSEric W. Biederman 	struct ctl_table *link_table, *entry, *link;
11330e47c99dSEric W. Biederman 	struct ctl_table_header *links;
1134ac13ac6fSEric W. Biederman 	struct ctl_node *node;
11350e47c99dSEric W. Biederman 	char *link_name;
11360e47c99dSEric W. Biederman 	int nr_entries, name_bytes;
11370e47c99dSEric W. Biederman 
11380e47c99dSEric W. Biederman 	name_bytes = 0;
11390e47c99dSEric W. Biederman 	nr_entries = 0;
11400e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11410e47c99dSEric W. Biederman 		nr_entries++;
11420e47c99dSEric W. Biederman 		name_bytes += strlen(entry->procname) + 1;
11430e47c99dSEric W. Biederman 	}
11440e47c99dSEric W. Biederman 
11450e47c99dSEric W. Biederman 	links = kzalloc(sizeof(struct ctl_table_header) +
1146ac13ac6fSEric W. Biederman 			sizeof(struct ctl_node)*nr_entries +
11470e47c99dSEric W. Biederman 			sizeof(struct ctl_table)*(nr_entries + 1) +
11480e47c99dSEric W. Biederman 			name_bytes,
11490e47c99dSEric W. Biederman 			GFP_KERNEL);
11500e47c99dSEric W. Biederman 
11510e47c99dSEric W. Biederman 	if (!links)
11520e47c99dSEric W. Biederman 		return NULL;
11530e47c99dSEric W. Biederman 
1154ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(links + 1);
1155ac13ac6fSEric W. Biederman 	link_table = (struct ctl_table *)(node + nr_entries);
11560e47c99dSEric W. Biederman 	link_name = (char *)&link_table[nr_entries + 1];
11570e47c99dSEric W. Biederman 
11580e47c99dSEric W. Biederman 	for (link = link_table, entry = table; entry->procname; link++, entry++) {
11590e47c99dSEric W. Biederman 		int len = strlen(entry->procname) + 1;
11600e47c99dSEric W. Biederman 		memcpy(link_name, entry->procname, len);
11610e47c99dSEric W. Biederman 		link->procname = link_name;
11620e47c99dSEric W. Biederman 		link->mode = S_IFLNK|S_IRWXUGO;
11630e47c99dSEric W. Biederman 		link->data = link_root;
11640e47c99dSEric W. Biederman 		link_name += len;
11650e47c99dSEric W. Biederman 	}
1166ac13ac6fSEric W. Biederman 	init_header(links, dir->header.root, dir->header.set, node, link_table);
11670e47c99dSEric W. Biederman 	links->nreg = nr_entries;
11680e47c99dSEric W. Biederman 
11690e47c99dSEric W. Biederman 	return links;
11700e47c99dSEric W. Biederman }
11710e47c99dSEric W. Biederman 
11720e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir,
11730e47c99dSEric W. Biederman 	struct ctl_table *table, struct ctl_table_root *link_root)
11740e47c99dSEric W. Biederman {
11750e47c99dSEric W. Biederman 	struct ctl_table_header *head;
11760e47c99dSEric W. Biederman 	struct ctl_table *entry, *link;
11770e47c99dSEric W. Biederman 
11780e47c99dSEric W. Biederman 	/* Are there links available for every entry in table? */
11790e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11800e47c99dSEric W. Biederman 		const char *procname = entry->procname;
11810e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
11820e47c99dSEric W. Biederman 		if (!link)
11830e47c99dSEric W. Biederman 			return false;
11840e47c99dSEric W. Biederman 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
11850e47c99dSEric W. Biederman 			continue;
11860e47c99dSEric W. Biederman 		if (S_ISLNK(link->mode) && (link->data == link_root))
11870e47c99dSEric W. Biederman 			continue;
11880e47c99dSEric W. Biederman 		return false;
11890e47c99dSEric W. Biederman 	}
11900e47c99dSEric W. Biederman 
11910e47c99dSEric W. Biederman 	/* The checks passed.  Increase the registration count on the links */
11920e47c99dSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
11930e47c99dSEric W. Biederman 		const char *procname = entry->procname;
11940e47c99dSEric W. Biederman 		link = find_entry(&head, dir, procname, strlen(procname));
11950e47c99dSEric W. Biederman 		head->nreg++;
11960e47c99dSEric W. Biederman 	}
11970e47c99dSEric W. Biederman 	return true;
11980e47c99dSEric W. Biederman }
11990e47c99dSEric W. Biederman 
12000e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head)
12010e47c99dSEric W. Biederman {
12020e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
12030e47c99dSEric W. Biederman 	struct ctl_dir *core_parent = NULL;
12040e47c99dSEric W. Biederman 	struct ctl_table_header *links;
12050e47c99dSEric W. Biederman 	int err;
12060e47c99dSEric W. Biederman 
12070e47c99dSEric W. Biederman 	if (head->set == root_set)
12080e47c99dSEric W. Biederman 		return 0;
12090e47c99dSEric W. Biederman 
12100e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, head->parent);
12110e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
12120e47c99dSEric W. Biederman 		return 0;
12130e47c99dSEric W. Biederman 
12140e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root))
12150e47c99dSEric W. Biederman 		return 0;
12160e47c99dSEric W. Biederman 
12170e47c99dSEric W. Biederman 	core_parent->header.nreg++;
12180e47c99dSEric W. Biederman 	spin_unlock(&sysctl_lock);
12190e47c99dSEric W. Biederman 
12200e47c99dSEric W. Biederman 	links = new_links(core_parent, head->ctl_table, head->root);
12210e47c99dSEric W. Biederman 
12220e47c99dSEric W. Biederman 	spin_lock(&sysctl_lock);
12230e47c99dSEric W. Biederman 	err = -ENOMEM;
12240e47c99dSEric W. Biederman 	if (!links)
12250e47c99dSEric W. Biederman 		goto out;
12260e47c99dSEric W. Biederman 
12270e47c99dSEric W. Biederman 	err = 0;
12280e47c99dSEric W. Biederman 	if (get_links(core_parent, head->ctl_table, head->root)) {
12290e47c99dSEric W. Biederman 		kfree(links);
12300e47c99dSEric W. Biederman 		goto out;
12310e47c99dSEric W. Biederman 	}
12320e47c99dSEric W. Biederman 
12330e47c99dSEric W. Biederman 	err = insert_header(core_parent, links);
12340e47c99dSEric W. Biederman 	if (err)
12350e47c99dSEric W. Biederman 		kfree(links);
12360e47c99dSEric W. Biederman out:
12370e47c99dSEric W. Biederman 	drop_sysctl_table(&core_parent->header);
12380e47c99dSEric W. Biederman 	return err;
12390e47c99dSEric W. Biederman }
12400e47c99dSEric W. Biederman 
12411f87f0b5SEric W. Biederman /**
1242f728019bSEric W. Biederman  * __register_sysctl_table - register a leaf sysctl table
124360a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
12441f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
12451f87f0b5SEric W. Biederman  * @table: the top-level table structure
12461f87f0b5SEric W. Biederman  *
12471f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
12481f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
12491f87f0b5SEric W. Biederman  *
12501f87f0b5SEric W. Biederman  * The members of the &struct ctl_table structure are used as follows:
12511f87f0b5SEric W. Biederman  *
12521f87f0b5SEric W. Biederman  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
12531f87f0b5SEric W. Biederman  *            enter a sysctl file
12541f87f0b5SEric W. Biederman  *
12551f87f0b5SEric W. Biederman  * data - a pointer to data for use by proc_handler
12561f87f0b5SEric W. Biederman  *
12571f87f0b5SEric W. Biederman  * maxlen - the maximum size in bytes of the data
12581f87f0b5SEric W. Biederman  *
1259f728019bSEric W. Biederman  * mode - the file permissions for the /proc/sys file
12601f87f0b5SEric W. Biederman  *
1261f728019bSEric W. Biederman  * child - must be %NULL.
12621f87f0b5SEric W. Biederman  *
12631f87f0b5SEric W. Biederman  * proc_handler - the text handler routine (described below)
12641f87f0b5SEric W. Biederman  *
12651f87f0b5SEric W. Biederman  * extra1, extra2 - extra pointers usable by the proc handler routines
12661f87f0b5SEric W. Biederman  *
12671f87f0b5SEric W. Biederman  * Leaf nodes in the sysctl tree will be represented by a single file
12681f87f0b5SEric W. Biederman  * under /proc; non-leaf nodes will be represented by directories.
12691f87f0b5SEric W. Biederman  *
1270f728019bSEric W. Biederman  * There must be a proc_handler routine for any terminal nodes.
1271f728019bSEric W. Biederman  * Several default handlers are available to cover common cases -
12721f87f0b5SEric W. Biederman  *
12731f87f0b5SEric W. Biederman  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
12741f87f0b5SEric W. Biederman  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
12751f87f0b5SEric W. Biederman  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
12761f87f0b5SEric W. Biederman  *
12771f87f0b5SEric W. Biederman  * It is the handler's job to read the input buffer from user memory
12781f87f0b5SEric W. Biederman  * and process it. The handler should return 0 on success.
12791f87f0b5SEric W. Biederman  *
12801f87f0b5SEric W. Biederman  * This routine returns %NULL on a failure to register, and a pointer
12811f87f0b5SEric W. Biederman  * to the table header on success.
12821f87f0b5SEric W. Biederman  */
12836e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table(
128460a47a2eSEric W. Biederman 	struct ctl_table_set *set,
12856e9d5164SEric W. Biederman 	const char *path, struct ctl_table *table)
12861f87f0b5SEric W. Biederman {
128760a47a2eSEric W. Biederman 	struct ctl_table_root *root = set->dir.header.root;
12881f87f0b5SEric W. Biederman 	struct ctl_table_header *header;
12896e9d5164SEric W. Biederman 	const char *name, *nextname;
12907ec66d06SEric W. Biederman 	struct ctl_dir *dir;
1291ac13ac6fSEric W. Biederman 	struct ctl_table *entry;
1292ac13ac6fSEric W. Biederman 	struct ctl_node *node;
1293ac13ac6fSEric W. Biederman 	int nr_entries = 0;
12941f87f0b5SEric W. Biederman 
1295ac13ac6fSEric W. Biederman 	for (entry = table; entry->procname; entry++)
1296ac13ac6fSEric W. Biederman 		nr_entries++;
1297ac13ac6fSEric W. Biederman 
1298ac13ac6fSEric W. Biederman 	header = kzalloc(sizeof(struct ctl_table_header) +
1299ac13ac6fSEric W. Biederman 			 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
13001f87f0b5SEric W. Biederman 	if (!header)
13011f87f0b5SEric W. Biederman 		return NULL;
13021f87f0b5SEric W. Biederman 
1303ac13ac6fSEric W. Biederman 	node = (struct ctl_node *)(header + 1);
1304ac13ac6fSEric W. Biederman 	init_header(header, root, set, node, table);
13057c60c48fSEric W. Biederman 	if (sysctl_check_table(path, table))
13067c60c48fSEric W. Biederman 		goto fail;
13078d6ecfccSEric W. Biederman 
13081f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
13090e47c99dSEric W. Biederman 	dir = &set->dir;
131060f126d9SEric W. Biederman 	/* Reference moved down the diretory tree get_subdir */
13117ec66d06SEric W. Biederman 	dir->header.nreg++;
13127ec66d06SEric W. Biederman 	spin_unlock(&sysctl_lock);
13137ec66d06SEric W. Biederman 
13147ec66d06SEric W. Biederman 	/* Find the directory for the ctl_table */
13157ec66d06SEric W. Biederman 	for (name = path; name; name = nextname) {
13167ec66d06SEric W. Biederman 		int namelen;
13177ec66d06SEric W. Biederman 		nextname = strchr(name, '/');
13187ec66d06SEric W. Biederman 		if (nextname) {
13197ec66d06SEric W. Biederman 			namelen = nextname - name;
13207ec66d06SEric W. Biederman 			nextname++;
13217ec66d06SEric W. Biederman 		} else {
13227ec66d06SEric W. Biederman 			namelen = strlen(name);
13237ec66d06SEric W. Biederman 		}
13247ec66d06SEric W. Biederman 		if (namelen == 0)
13251f87f0b5SEric W. Biederman 			continue;
13267ec66d06SEric W. Biederman 
13270e47c99dSEric W. Biederman 		dir = get_subdir(dir, name, namelen);
13287ec66d06SEric W. Biederman 		if (IS_ERR(dir))
13297ec66d06SEric W. Biederman 			goto fail;
13301f87f0b5SEric W. Biederman 	}
13310e47c99dSEric W. Biederman 
13327ec66d06SEric W. Biederman 	spin_lock(&sysctl_lock);
13330e47c99dSEric W. Biederman 	if (insert_header(dir, header))
13340e47c99dSEric W. Biederman 		goto fail_put_dir_locked;
13350e47c99dSEric W. Biederman 
13367ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13371f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
13381f87f0b5SEric W. Biederman 
13391f87f0b5SEric W. Biederman 	return header;
13400e47c99dSEric W. Biederman 
13417ec66d06SEric W. Biederman fail_put_dir_locked:
13427ec66d06SEric W. Biederman 	drop_sysctl_table(&dir->header);
13437c60c48fSEric W. Biederman 	spin_unlock(&sysctl_lock);
13447c60c48fSEric W. Biederman fail:
13457c60c48fSEric W. Biederman 	kfree(header);
13467c60c48fSEric W. Biederman 	dump_stack();
13477c60c48fSEric W. Biederman 	return NULL;
13481f87f0b5SEric W. Biederman }
13491f87f0b5SEric W. Biederman 
1350fea478d4SEric W. Biederman /**
1351fea478d4SEric W. Biederman  * register_sysctl - register a sysctl table
1352fea478d4SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
1353fea478d4SEric W. Biederman  * @table: the table structure
1354fea478d4SEric W. Biederman  *
1355fea478d4SEric W. Biederman  * Register a sysctl table. @table should be a filled in ctl_table
1356fea478d4SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
1357fea478d4SEric W. Biederman  *
1358fea478d4SEric W. Biederman  * See __register_sysctl_table for more details.
1359fea478d4SEric W. Biederman  */
1360fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1361fea478d4SEric W. Biederman {
1362fea478d4SEric W. Biederman 	return __register_sysctl_table(&sysctl_table_root.default_set,
1363fea478d4SEric W. Biederman 					path, table);
1364fea478d4SEric W. Biederman }
1365fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl);
1366fea478d4SEric W. Biederman 
13676e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name)
13686e9d5164SEric W. Biederman {
13696e9d5164SEric W. Biederman 	int namelen;
13706e9d5164SEric W. Biederman 	namelen = strlen(name);
13716e9d5164SEric W. Biederman 	if (((pos - path) + namelen + 2) >= PATH_MAX)
13726e9d5164SEric W. Biederman 		return NULL;
13736e9d5164SEric W. Biederman 	memcpy(pos, name, namelen);
13746e9d5164SEric W. Biederman 	pos[namelen] = '/';
13756e9d5164SEric W. Biederman 	pos[namelen + 1] = '\0';
13766e9d5164SEric W. Biederman 	pos += namelen + 1;
13776e9d5164SEric W. Biederman 	return pos;
13786e9d5164SEric W. Biederman }
13796e9d5164SEric W. Biederman 
1380f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table)
1381f728019bSEric W. Biederman {
1382f728019bSEric W. Biederman 	int has_files = 0;
1383f728019bSEric W. Biederman 	int nr_subheaders = 0;
1384f728019bSEric W. Biederman 	struct ctl_table *entry;
1385f728019bSEric W. Biederman 
1386f728019bSEric W. Biederman 	/* special case: no directory and empty directory */
1387f728019bSEric W. Biederman 	if (!table || !table->procname)
1388f728019bSEric W. Biederman 		return 1;
1389f728019bSEric W. Biederman 
1390f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1391f728019bSEric W. Biederman 		if (entry->child)
1392f728019bSEric W. Biederman 			nr_subheaders += count_subheaders(entry->child);
1393f728019bSEric W. Biederman 		else
1394f728019bSEric W. Biederman 			has_files = 1;
1395f728019bSEric W. Biederman 	}
1396f728019bSEric W. Biederman 	return nr_subheaders + has_files;
1397f728019bSEric W. Biederman }
1398f728019bSEric W. Biederman 
1399f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos,
140060a47a2eSEric W. Biederman 	struct ctl_table_header ***subheader, struct ctl_table_set *set,
1401f728019bSEric W. Biederman 	struct ctl_table *table)
1402f728019bSEric W. Biederman {
1403f728019bSEric W. Biederman 	struct ctl_table *ctl_table_arg = NULL;
1404f728019bSEric W. Biederman 	struct ctl_table *entry, *files;
1405f728019bSEric W. Biederman 	int nr_files = 0;
1406f728019bSEric W. Biederman 	int nr_dirs = 0;
1407f728019bSEric W. Biederman 	int err = -ENOMEM;
1408f728019bSEric W. Biederman 
1409f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1410f728019bSEric W. Biederman 		if (entry->child)
1411f728019bSEric W. Biederman 			nr_dirs++;
1412f728019bSEric W. Biederman 		else
1413f728019bSEric W. Biederman 			nr_files++;
1414f728019bSEric W. Biederman 	}
1415f728019bSEric W. Biederman 
1416f728019bSEric W. Biederman 	files = table;
1417f728019bSEric W. Biederman 	/* If there are mixed files and directories we need a new table */
1418f728019bSEric W. Biederman 	if (nr_dirs && nr_files) {
1419f728019bSEric W. Biederman 		struct ctl_table *new;
1420f728019bSEric W. Biederman 		files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1421f728019bSEric W. Biederman 				GFP_KERNEL);
1422f728019bSEric W. Biederman 		if (!files)
1423f728019bSEric W. Biederman 			goto out;
1424f728019bSEric W. Biederman 
1425f728019bSEric W. Biederman 		ctl_table_arg = files;
1426f728019bSEric W. Biederman 		for (new = files, entry = table; entry->procname; entry++) {
1427f728019bSEric W. Biederman 			if (entry->child)
1428f728019bSEric W. Biederman 				continue;
1429f728019bSEric W. Biederman 			*new = *entry;
1430f728019bSEric W. Biederman 			new++;
1431f728019bSEric W. Biederman 		}
1432f728019bSEric W. Biederman 	}
1433f728019bSEric W. Biederman 
1434f728019bSEric W. Biederman 	/* Register everything except a directory full of subdirectories */
1435f728019bSEric W. Biederman 	if (nr_files || !nr_dirs) {
1436f728019bSEric W. Biederman 		struct ctl_table_header *header;
143760a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, path, files);
1438f728019bSEric W. Biederman 		if (!header) {
1439f728019bSEric W. Biederman 			kfree(ctl_table_arg);
1440f728019bSEric W. Biederman 			goto out;
1441f728019bSEric W. Biederman 		}
1442f728019bSEric W. Biederman 
1443f728019bSEric W. Biederman 		/* Remember if we need to free the file table */
1444f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1445f728019bSEric W. Biederman 		**subheader = header;
1446f728019bSEric W. Biederman 		(*subheader)++;
1447f728019bSEric W. Biederman 	}
1448f728019bSEric W. Biederman 
1449f728019bSEric W. Biederman 	/* Recurse into the subdirectories. */
1450f728019bSEric W. Biederman 	for (entry = table; entry->procname; entry++) {
1451f728019bSEric W. Biederman 		char *child_pos;
1452f728019bSEric W. Biederman 
1453f728019bSEric W. Biederman 		if (!entry->child)
1454f728019bSEric W. Biederman 			continue;
1455f728019bSEric W. Biederman 
1456f728019bSEric W. Biederman 		err = -ENAMETOOLONG;
1457f728019bSEric W. Biederman 		child_pos = append_path(path, pos, entry->procname);
1458f728019bSEric W. Biederman 		if (!child_pos)
1459f728019bSEric W. Biederman 			goto out;
1460f728019bSEric W. Biederman 
1461f728019bSEric W. Biederman 		err = register_leaf_sysctl_tables(path, child_pos, subheader,
146260a47a2eSEric W. Biederman 						  set, entry->child);
1463f728019bSEric W. Biederman 		pos[0] = '\0';
1464f728019bSEric W. Biederman 		if (err)
1465f728019bSEric W. Biederman 			goto out;
1466f728019bSEric W. Biederman 	}
1467f728019bSEric W. Biederman 	err = 0;
1468f728019bSEric W. Biederman out:
1469f728019bSEric W. Biederman 	/* On failure our caller will unregister all registered subheaders */
1470f728019bSEric W. Biederman 	return err;
1471f728019bSEric W. Biederman }
1472f728019bSEric W. Biederman 
14736e9d5164SEric W. Biederman /**
14746e9d5164SEric W. Biederman  * __register_sysctl_paths - register a sysctl table hierarchy
147560a47a2eSEric W. Biederman  * @set: Sysctl tree to register on
14766e9d5164SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
14776e9d5164SEric W. Biederman  * @table: the top-level table structure
14786e9d5164SEric W. Biederman  *
14796e9d5164SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
14806e9d5164SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
14816e9d5164SEric W. Biederman  *
14826e9d5164SEric W. Biederman  * See __register_sysctl_table for more details.
14836e9d5164SEric W. Biederman  */
14846e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths(
148560a47a2eSEric W. Biederman 	struct ctl_table_set *set,
14866e9d5164SEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table)
14876e9d5164SEric W. Biederman {
1488ec6a5266SEric W. Biederman 	struct ctl_table *ctl_table_arg = table;
1489f728019bSEric W. Biederman 	int nr_subheaders = count_subheaders(table);
1490f728019bSEric W. Biederman 	struct ctl_table_header *header = NULL, **subheaders, **subheader;
14916e9d5164SEric W. Biederman 	const struct ctl_path *component;
14926e9d5164SEric W. Biederman 	char *new_path, *pos;
14936e9d5164SEric W. Biederman 
14946e9d5164SEric W. Biederman 	pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
14956e9d5164SEric W. Biederman 	if (!new_path)
14966e9d5164SEric W. Biederman 		return NULL;
14976e9d5164SEric W. Biederman 
14986e9d5164SEric W. Biederman 	pos[0] = '\0';
14996e9d5164SEric W. Biederman 	for (component = path; component->procname; component++) {
15006e9d5164SEric W. Biederman 		pos = append_path(new_path, pos, component->procname);
15016e9d5164SEric W. Biederman 		if (!pos)
15026e9d5164SEric W. Biederman 			goto out;
15036e9d5164SEric W. Biederman 	}
1504ec6a5266SEric W. Biederman 	while (table->procname && table->child && !table[1].procname) {
1505ec6a5266SEric W. Biederman 		pos = append_path(new_path, pos, table->procname);
1506ec6a5266SEric W. Biederman 		if (!pos)
1507ec6a5266SEric W. Biederman 			goto out;
1508ec6a5266SEric W. Biederman 		table = table->child;
1509ec6a5266SEric W. Biederman 	}
1510f728019bSEric W. Biederman 	if (nr_subheaders == 1) {
151160a47a2eSEric W. Biederman 		header = __register_sysctl_table(set, new_path, table);
1512ec6a5266SEric W. Biederman 		if (header)
1513ec6a5266SEric W. Biederman 			header->ctl_table_arg = ctl_table_arg;
1514f728019bSEric W. Biederman 	} else {
1515f728019bSEric W. Biederman 		header = kzalloc(sizeof(*header) +
1516f728019bSEric W. Biederman 				 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1517f728019bSEric W. Biederman 		if (!header)
1518f728019bSEric W. Biederman 			goto out;
1519f728019bSEric W. Biederman 
1520f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **) (header + 1);
1521f728019bSEric W. Biederman 		subheader = subheaders;
1522f728019bSEric W. Biederman 		header->ctl_table_arg = ctl_table_arg;
1523f728019bSEric W. Biederman 
1524f728019bSEric W. Biederman 		if (register_leaf_sysctl_tables(new_path, pos, &subheader,
152560a47a2eSEric W. Biederman 						set, table))
1526f728019bSEric W. Biederman 			goto err_register_leaves;
1527f728019bSEric W. Biederman 	}
1528f728019bSEric W. Biederman 
15296e9d5164SEric W. Biederman out:
15306e9d5164SEric W. Biederman 	kfree(new_path);
15316e9d5164SEric W. Biederman 	return header;
1532f728019bSEric W. Biederman 
1533f728019bSEric W. Biederman err_register_leaves:
1534f728019bSEric W. Biederman 	while (subheader > subheaders) {
1535f728019bSEric W. Biederman 		struct ctl_table_header *subh = *(--subheader);
1536f728019bSEric W. Biederman 		struct ctl_table *table = subh->ctl_table_arg;
1537f728019bSEric W. Biederman 		unregister_sysctl_table(subh);
1538f728019bSEric W. Biederman 		kfree(table);
1539f728019bSEric W. Biederman 	}
1540f728019bSEric W. Biederman 	kfree(header);
1541f728019bSEric W. Biederman 	header = NULL;
1542f728019bSEric W. Biederman 	goto out;
15436e9d5164SEric W. Biederman }
15446e9d5164SEric W. Biederman 
15451f87f0b5SEric W. Biederman /**
15461f87f0b5SEric W. Biederman  * register_sysctl_table_path - register a sysctl table hierarchy
15471f87f0b5SEric W. Biederman  * @path: The path to the directory the sysctl table is in.
15481f87f0b5SEric W. Biederman  * @table: the top-level table structure
15491f87f0b5SEric W. Biederman  *
15501f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15511f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15521f87f0b5SEric W. Biederman  *
15531f87f0b5SEric W. Biederman  * See __register_sysctl_paths for more details.
15541f87f0b5SEric W. Biederman  */
15551f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
15561f87f0b5SEric W. Biederman 						struct ctl_table *table)
15571f87f0b5SEric W. Biederman {
155860a47a2eSEric W. Biederman 	return __register_sysctl_paths(&sysctl_table_root.default_set,
15591f87f0b5SEric W. Biederman 					path, table);
15601f87f0b5SEric W. Biederman }
15611f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths);
15621f87f0b5SEric W. Biederman 
15631f87f0b5SEric W. Biederman /**
15641f87f0b5SEric W. Biederman  * register_sysctl_table - register a sysctl table hierarchy
15651f87f0b5SEric W. Biederman  * @table: the top-level table structure
15661f87f0b5SEric W. Biederman  *
15671f87f0b5SEric W. Biederman  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
15681f87f0b5SEric W. Biederman  * array. A completely 0 filled entry terminates the table.
15691f87f0b5SEric W. Biederman  *
15701f87f0b5SEric W. Biederman  * See register_sysctl_paths for more details.
15711f87f0b5SEric W. Biederman  */
15721f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
15731f87f0b5SEric W. Biederman {
15741f87f0b5SEric W. Biederman 	static const struct ctl_path null_path[] = { {} };
15751f87f0b5SEric W. Biederman 
15761f87f0b5SEric W. Biederman 	return register_sysctl_paths(null_path, table);
15771f87f0b5SEric W. Biederman }
15781f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table);
15791f87f0b5SEric W. Biederman 
15800e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header)
15810e47c99dSEric W. Biederman {
15820e47c99dSEric W. Biederman 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
15830e47c99dSEric W. Biederman 	struct ctl_table_root *root = header->root;
15840e47c99dSEric W. Biederman 	struct ctl_dir *parent = header->parent;
15850e47c99dSEric W. Biederman 	struct ctl_dir *core_parent;
15860e47c99dSEric W. Biederman 	struct ctl_table *entry;
15870e47c99dSEric W. Biederman 
15880e47c99dSEric W. Biederman 	if (header->set == root_set)
15890e47c99dSEric W. Biederman 		return;
15900e47c99dSEric W. Biederman 
15910e47c99dSEric W. Biederman 	core_parent = xlate_dir(root_set, parent);
15920e47c99dSEric W. Biederman 	if (IS_ERR(core_parent))
15930e47c99dSEric W. Biederman 		return;
15940e47c99dSEric W. Biederman 
15950e47c99dSEric W. Biederman 	for (entry = header->ctl_table; entry->procname; entry++) {
15960e47c99dSEric W. Biederman 		struct ctl_table_header *link_head;
15970e47c99dSEric W. Biederman 		struct ctl_table *link;
15980e47c99dSEric W. Biederman 		const char *name = entry->procname;
15990e47c99dSEric W. Biederman 
16000e47c99dSEric W. Biederman 		link = find_entry(&link_head, core_parent, name, strlen(name));
16010e47c99dSEric W. Biederman 		if (link &&
16020e47c99dSEric W. Biederman 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
16030e47c99dSEric W. Biederman 		     (S_ISLNK(link->mode) && (link->data == root)))) {
16040e47c99dSEric W. Biederman 			drop_sysctl_table(link_head);
16050e47c99dSEric W. Biederman 		}
16060e47c99dSEric W. Biederman 		else {
160787ebdc00SAndrew Morton 			pr_err("sysctl link missing during unregister: ");
16080e47c99dSEric W. Biederman 			sysctl_print_dir(parent);
160987ebdc00SAndrew Morton 			pr_cont("/%s\n", name);
16100e47c99dSEric W. Biederman 		}
16110e47c99dSEric W. Biederman 	}
16120e47c99dSEric W. Biederman }
16130e47c99dSEric W. Biederman 
1614938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header)
1615938aaa4fSEric W. Biederman {
16167ec66d06SEric W. Biederman 	struct ctl_dir *parent = header->parent;
16177ec66d06SEric W. Biederman 
1618938aaa4fSEric W. Biederman 	if (--header->nreg)
1619938aaa4fSEric W. Biederman 		return;
1620938aaa4fSEric W. Biederman 
16210e47c99dSEric W. Biederman 	put_links(header);
1622938aaa4fSEric W. Biederman 	start_unregistering(header);
1623938aaa4fSEric W. Biederman 	if (!--header->count)
1624938aaa4fSEric W. Biederman 		kfree_rcu(header, rcu);
16257ec66d06SEric W. Biederman 
16267ec66d06SEric W. Biederman 	if (parent)
16277ec66d06SEric W. Biederman 		drop_sysctl_table(&parent->header);
1628938aaa4fSEric W. Biederman }
1629938aaa4fSEric W. Biederman 
16301f87f0b5SEric W. Biederman /**
16311f87f0b5SEric W. Biederman  * unregister_sysctl_table - unregister a sysctl table hierarchy
16321f87f0b5SEric W. Biederman  * @header: the header returned from register_sysctl_table
16331f87f0b5SEric W. Biederman  *
16341f87f0b5SEric W. Biederman  * Unregisters the sysctl table and all children. proc entries may not
16351f87f0b5SEric W. Biederman  * actually be removed until they are no longer used by anyone.
16361f87f0b5SEric W. Biederman  */
16371f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header)
16381f87f0b5SEric W. Biederman {
1639f728019bSEric W. Biederman 	int nr_subheaders;
16401f87f0b5SEric W. Biederman 	might_sleep();
16411f87f0b5SEric W. Biederman 
16421f87f0b5SEric W. Biederman 	if (header == NULL)
16431f87f0b5SEric W. Biederman 		return;
16441f87f0b5SEric W. Biederman 
1645f728019bSEric W. Biederman 	nr_subheaders = count_subheaders(header->ctl_table_arg);
1646f728019bSEric W. Biederman 	if (unlikely(nr_subheaders > 1)) {
1647f728019bSEric W. Biederman 		struct ctl_table_header **subheaders;
1648f728019bSEric W. Biederman 		int i;
1649f728019bSEric W. Biederman 
1650f728019bSEric W. Biederman 		subheaders = (struct ctl_table_header **)(header + 1);
1651f728019bSEric W. Biederman 		for (i = nr_subheaders -1; i >= 0; i--) {
1652f728019bSEric W. Biederman 			struct ctl_table_header *subh = subheaders[i];
1653f728019bSEric W. Biederman 			struct ctl_table *table = subh->ctl_table_arg;
1654f728019bSEric W. Biederman 			unregister_sysctl_table(subh);
1655f728019bSEric W. Biederman 			kfree(table);
1656f728019bSEric W. Biederman 		}
1657f728019bSEric W. Biederman 		kfree(header);
1658f728019bSEric W. Biederman 		return;
1659f728019bSEric W. Biederman 	}
1660f728019bSEric W. Biederman 
16611f87f0b5SEric W. Biederman 	spin_lock(&sysctl_lock);
1662938aaa4fSEric W. Biederman 	drop_sysctl_table(header);
16631f87f0b5SEric W. Biederman 	spin_unlock(&sysctl_lock);
16641f87f0b5SEric W. Biederman }
16651f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table);
16661f87f0b5SEric W. Biederman 
16670e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set,
16689eb47c26SEric W. Biederman 	struct ctl_table_root *root,
16691f87f0b5SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
16701f87f0b5SEric W. Biederman {
16711347440dSDan Carpenter 	memset(set, 0, sizeof(*set));
16720e47c99dSEric W. Biederman 	set->is_seen = is_seen;
1673ac13ac6fSEric W. Biederman 	init_header(&set->dir.header, root, set, NULL, root_table);
16741f87f0b5SEric W. Biederman }
16751f87f0b5SEric W. Biederman 
167697324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set)
167797324cd8SEric W. Biederman {
1678ac13ac6fSEric W. Biederman 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
167997324cd8SEric W. Biederman }
16801f87f0b5SEric W. Biederman 
16811e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
168277b14db5SEric W. Biederman {
1683e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
1684e1675231SAlexey Dobriyan 
168577b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
16869043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
16879043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
168877b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
1689de4e83bdSEric W. Biederman 
1690de4e83bdSEric W. Biederman 	return sysctl_init();
169177b14db5SEric W. Biederman }
1692