xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision ffde3af4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * /proc/sys support
4  */
5 #include <linux/init.h>
6 #include <linux/sysctl.h>
7 #include <linux/poll.h>
8 #include <linux/proc_fs.h>
9 #include <linux/printk.h>
10 #include <linux/security.h>
11 #include <linux/sched.h>
12 #include <linux/cred.h>
13 #include <linux/namei.h>
14 #include <linux/mm.h>
15 #include <linux/uio.h>
16 #include <linux/module.h>
17 #include <linux/bpf-cgroup.h>
18 #include <linux/mount.h>
19 #include <linux/kmemleak.h>
20 #include "internal.h"
21 
22 #define list_for_each_table_entry(entry, header)	\
23 	entry = header->ctl_table;			\
24 	for (size_t i = 0 ; i < header->ctl_table_size && entry->procname; ++i, entry++)
25 
26 static const struct dentry_operations proc_sys_dentry_operations;
27 static const struct file_operations proc_sys_file_operations;
28 static const struct inode_operations proc_sys_inode_operations;
29 static const struct file_operations proc_sys_dir_file_operations;
30 static const struct inode_operations proc_sys_dir_operations;
31 
32 /* Support for permanently empty directories */
33 static struct ctl_table sysctl_mount_point[] = {
34 	{.type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY }
35 };
36 
37 /**
38  * register_sysctl_mount_point() - registers a sysctl mount point
39  * @path: path for the mount point
40  *
41  * Used to create a permanently empty directory to serve as mount point.
42  * There are some subtle but important permission checks this allows in the
43  * case of unprivileged mounts.
44  */
register_sysctl_mount_point(const char * path)45 struct ctl_table_header *register_sysctl_mount_point(const char *path)
46 {
47 	return register_sysctl(path, sysctl_mount_point);
48 }
49 EXPORT_SYMBOL(register_sysctl_mount_point);
50 
51 #define sysctl_is_perm_empty_ctl_table(tptr)		\
52 	(tptr[0].type == SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
53 #define sysctl_is_perm_empty_ctl_header(hptr)		\
54 	(sysctl_is_perm_empty_ctl_table(hptr->ctl_table))
55 #define sysctl_set_perm_empty_ctl_header(hptr)		\
56 	(hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY)
57 #define sysctl_clear_perm_empty_ctl_header(hptr)	\
58 	(hptr->ctl_table[0].type = SYSCTL_TABLE_TYPE_DEFAULT)
59 
proc_sys_poll_notify(struct ctl_table_poll * poll)60 void proc_sys_poll_notify(struct ctl_table_poll *poll)
61 {
62 	if (!poll)
63 		return;
64 
65 	atomic_inc(&poll->event);
66 	wake_up_interruptible(&poll->wait);
67 }
68 
69 static struct ctl_table root_table[] = {
70 	{
71 		.procname = "",
72 		.mode = S_IFDIR|S_IRUGO|S_IXUGO,
73 	},
74 	{ }
75 };
76 static struct ctl_table_root sysctl_table_root = {
77 	.default_set.dir.header = {
78 		{{.count = 1,
79 		  .nreg = 1,
80 		  .ctl_table = root_table }},
81 		.ctl_table_arg = root_table,
82 		.root = &sysctl_table_root,
83 		.set = &sysctl_table_root.default_set,
84 	},
85 };
86 
87 static DEFINE_SPINLOCK(sysctl_lock);
88 
89 static void drop_sysctl_table(struct ctl_table_header *header);
90 static int sysctl_follow_link(struct ctl_table_header **phead,
91 	struct ctl_table **pentry);
92 static int insert_links(struct ctl_table_header *head);
93 static void put_links(struct ctl_table_header *header);
94 
sysctl_print_dir(struct ctl_dir * dir)95 static void sysctl_print_dir(struct ctl_dir *dir)
96 {
97 	if (dir->header.parent)
98 		sysctl_print_dir(dir->header.parent);
99 	pr_cont("%s/", dir->header.ctl_table[0].procname);
100 }
101 
namecmp(const char * name1,int len1,const char * name2,int len2)102 static int namecmp(const char *name1, int len1, const char *name2, int len2)
103 {
104 	int cmp;
105 
106 	cmp = memcmp(name1, name2, min(len1, len2));
107 	if (cmp == 0)
108 		cmp = len1 - len2;
109 	return cmp;
110 }
111 
112 /* Called under sysctl_lock */
find_entry(struct ctl_table_header ** phead,struct ctl_dir * dir,const char * name,int namelen)113 static struct ctl_table *find_entry(struct ctl_table_header **phead,
114 	struct ctl_dir *dir, const char *name, int namelen)
115 {
116 	struct ctl_table_header *head;
117 	struct ctl_table *entry;
118 	struct rb_node *node = dir->root.rb_node;
119 
120 	while (node)
121 	{
122 		struct ctl_node *ctl_node;
123 		const char *procname;
124 		int cmp;
125 
126 		ctl_node = rb_entry(node, struct ctl_node, node);
127 		head = ctl_node->header;
128 		entry = &head->ctl_table[ctl_node - head->node];
129 		procname = entry->procname;
130 
131 		cmp = namecmp(name, namelen, procname, strlen(procname));
132 		if (cmp < 0)
133 			node = node->rb_left;
134 		else if (cmp > 0)
135 			node = node->rb_right;
136 		else {
137 			*phead = head;
138 			return entry;
139 		}
140 	}
141 	return NULL;
142 }
143 
insert_entry(struct ctl_table_header * head,struct ctl_table * entry)144 static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
145 {
146 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
147 	struct rb_node **p = &head->parent->root.rb_node;
148 	struct rb_node *parent = NULL;
149 	const char *name = entry->procname;
150 	int namelen = strlen(name);
151 
152 	while (*p) {
153 		struct ctl_table_header *parent_head;
154 		struct ctl_table *parent_entry;
155 		struct ctl_node *parent_node;
156 		const char *parent_name;
157 		int cmp;
158 
159 		parent = *p;
160 		parent_node = rb_entry(parent, struct ctl_node, node);
161 		parent_head = parent_node->header;
162 		parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
163 		parent_name = parent_entry->procname;
164 
165 		cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
166 		if (cmp < 0)
167 			p = &(*p)->rb_left;
168 		else if (cmp > 0)
169 			p = &(*p)->rb_right;
170 		else {
171 			pr_err("sysctl duplicate entry: ");
172 			sysctl_print_dir(head->parent);
173 			pr_cont("%s\n", entry->procname);
174 			return -EEXIST;
175 		}
176 	}
177 
178 	rb_link_node(node, parent, p);
179 	rb_insert_color(node, &head->parent->root);
180 	return 0;
181 }
182 
erase_entry(struct ctl_table_header * head,struct ctl_table * entry)183 static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
184 {
185 	struct rb_node *node = &head->node[entry - head->ctl_table].node;
186 
187 	rb_erase(node, &head->parent->root);
188 }
189 
init_header(struct ctl_table_header * head,struct ctl_table_root * root,struct ctl_table_set * set,struct ctl_node * node,struct ctl_table * table,size_t table_size)190 static void init_header(struct ctl_table_header *head,
191 	struct ctl_table_root *root, struct ctl_table_set *set,
192 	struct ctl_node *node, struct ctl_table *table, size_t table_size)
193 {
194 	head->ctl_table = table;
195 	head->ctl_table_size = table_size;
196 	head->ctl_table_arg = table;
197 	head->used = 0;
198 	head->count = 1;
199 	head->nreg = 1;
200 	head->unregistering = NULL;
201 	head->root = root;
202 	head->set = set;
203 	head->parent = NULL;
204 	head->node = node;
205 	INIT_HLIST_HEAD(&head->inodes);
206 	if (node) {
207 		struct ctl_table *entry;
208 
209 		list_for_each_table_entry(entry, head) {
210 			node->header = head;
211 			node++;
212 		}
213 	}
214 }
215 
erase_header(struct ctl_table_header * head)216 static void erase_header(struct ctl_table_header *head)
217 {
218 	struct ctl_table *entry;
219 
220 	list_for_each_table_entry(entry, head)
221 		erase_entry(head, entry);
222 }
223 
insert_header(struct ctl_dir * dir,struct ctl_table_header * header)224 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
225 {
226 	struct ctl_table *entry;
227 	struct ctl_table_header *dir_h = &dir->header;
228 	int err;
229 
230 
231 	/* Is this a permanently empty directory? */
232 	if (sysctl_is_perm_empty_ctl_header(dir_h))
233 		return -EROFS;
234 
235 	/* Am I creating a permanently empty directory? */
236 	if (header->ctl_table_size > 0 &&
237 	    sysctl_is_perm_empty_ctl_table(header->ctl_table)) {
238 		if (!RB_EMPTY_ROOT(&dir->root))
239 			return -EINVAL;
240 		sysctl_set_perm_empty_ctl_header(dir_h);
241 	}
242 
243 	dir_h->nreg++;
244 	header->parent = dir;
245 	err = insert_links(header);
246 	if (err)
247 		goto fail_links;
248 	list_for_each_table_entry(entry, header) {
249 		err = insert_entry(header, entry);
250 		if (err)
251 			goto fail;
252 	}
253 	return 0;
254 fail:
255 	erase_header(header);
256 	put_links(header);
257 fail_links:
258 	if (header->ctl_table == sysctl_mount_point)
259 		sysctl_clear_perm_empty_ctl_header(dir_h);
260 	header->parent = NULL;
261 	drop_sysctl_table(dir_h);
262 	return err;
263 }
264 
265 /* called under sysctl_lock */
use_table(struct ctl_table_header * p)266 static int use_table(struct ctl_table_header *p)
267 {
268 	if (unlikely(p->unregistering))
269 		return 0;
270 	p->used++;
271 	return 1;
272 }
273 
274 /* called under sysctl_lock */
unuse_table(struct ctl_table_header * p)275 static void unuse_table(struct ctl_table_header *p)
276 {
277 	if (!--p->used)
278 		if (unlikely(p->unregistering))
279 			complete(p->unregistering);
280 }
281 
proc_sys_invalidate_dcache(struct ctl_table_header * head)282 static void proc_sys_invalidate_dcache(struct ctl_table_header *head)
283 {
284 	proc_invalidate_siblings_dcache(&head->inodes, &sysctl_lock);
285 }
286 
287 /* called under sysctl_lock, will reacquire if has to wait */
start_unregistering(struct ctl_table_header * p)288 static void start_unregistering(struct ctl_table_header *p)
289 {
290 	/*
291 	 * if p->used is 0, nobody will ever touch that entry again;
292 	 * we'll eliminate all paths to it before dropping sysctl_lock
293 	 */
294 	if (unlikely(p->used)) {
295 		struct completion wait;
296 		init_completion(&wait);
297 		p->unregistering = &wait;
298 		spin_unlock(&sysctl_lock);
299 		wait_for_completion(&wait);
300 	} else {
301 		/* anything non-NULL; we'll never dereference it */
302 		p->unregistering = ERR_PTR(-EINVAL);
303 		spin_unlock(&sysctl_lock);
304 	}
305 	/*
306 	 * Invalidate dentries for unregistered sysctls: namespaced sysctls
307 	 * can have duplicate names and contaminate dcache very badly.
308 	 */
309 	proc_sys_invalidate_dcache(p);
310 	/*
311 	 * do not remove from the list until nobody holds it; walking the
312 	 * list in do_sysctl() relies on that.
313 	 */
314 	spin_lock(&sysctl_lock);
315 	erase_header(p);
316 }
317 
sysctl_head_grab(struct ctl_table_header * head)318 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
319 {
320 	BUG_ON(!head);
321 	spin_lock(&sysctl_lock);
322 	if (!use_table(head))
323 		head = ERR_PTR(-ENOENT);
324 	spin_unlock(&sysctl_lock);
325 	return head;
326 }
327 
sysctl_head_finish(struct ctl_table_header * head)328 static void sysctl_head_finish(struct ctl_table_header *head)
329 {
330 	if (!head)
331 		return;
332 	spin_lock(&sysctl_lock);
333 	unuse_table(head);
334 	spin_unlock(&sysctl_lock);
335 }
336 
337 static struct ctl_table_set *
lookup_header_set(struct ctl_table_root * root)338 lookup_header_set(struct ctl_table_root *root)
339 {
340 	struct ctl_table_set *set = &root->default_set;
341 	if (root->lookup)
342 		set = root->lookup(root);
343 	return set;
344 }
345 
lookup_entry(struct ctl_table_header ** phead,struct ctl_dir * dir,const char * name,int namelen)346 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
347 				      struct ctl_dir *dir,
348 				      const char *name, int namelen)
349 {
350 	struct ctl_table_header *head;
351 	struct ctl_table *entry;
352 
353 	spin_lock(&sysctl_lock);
354 	entry = find_entry(&head, dir, name, namelen);
355 	if (entry && use_table(head))
356 		*phead = head;
357 	else
358 		entry = NULL;
359 	spin_unlock(&sysctl_lock);
360 	return entry;
361 }
362 
first_usable_entry(struct rb_node * node)363 static struct ctl_node *first_usable_entry(struct rb_node *node)
364 {
365 	struct ctl_node *ctl_node;
366 
367 	for (;node; node = rb_next(node)) {
368 		ctl_node = rb_entry(node, struct ctl_node, node);
369 		if (use_table(ctl_node->header))
370 			return ctl_node;
371 	}
372 	return NULL;
373 }
374 
first_entry(struct ctl_dir * dir,struct ctl_table_header ** phead,struct ctl_table ** pentry)375 static void first_entry(struct ctl_dir *dir,
376 	struct ctl_table_header **phead, struct ctl_table **pentry)
377 {
378 	struct ctl_table_header *head = NULL;
379 	struct ctl_table *entry = NULL;
380 	struct ctl_node *ctl_node;
381 
382 	spin_lock(&sysctl_lock);
383 	ctl_node = first_usable_entry(rb_first(&dir->root));
384 	spin_unlock(&sysctl_lock);
385 	if (ctl_node) {
386 		head = ctl_node->header;
387 		entry = &head->ctl_table[ctl_node - head->node];
388 	}
389 	*phead = head;
390 	*pentry = entry;
391 }
392 
next_entry(struct ctl_table_header ** phead,struct ctl_table ** pentry)393 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
394 {
395 	struct ctl_table_header *head = *phead;
396 	struct ctl_table *entry = *pentry;
397 	struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
398 
399 	spin_lock(&sysctl_lock);
400 	unuse_table(head);
401 
402 	ctl_node = first_usable_entry(rb_next(&ctl_node->node));
403 	spin_unlock(&sysctl_lock);
404 	head = NULL;
405 	if (ctl_node) {
406 		head = ctl_node->header;
407 		entry = &head->ctl_table[ctl_node - head->node];
408 	}
409 	*phead = head;
410 	*pentry = entry;
411 }
412 
413 /*
414  * sysctl_perm does NOT grant the superuser all rights automatically, because
415  * some sysctl variables are readonly even to root.
416  */
417 
test_perm(int mode,int op)418 static int test_perm(int mode, int op)
419 {
420 	if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
421 		mode >>= 6;
422 	else if (in_egroup_p(GLOBAL_ROOT_GID))
423 		mode >>= 3;
424 	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
425 		return 0;
426 	return -EACCES;
427 }
428 
sysctl_perm(struct ctl_table_header * head,struct ctl_table * table,int op)429 static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
430 {
431 	struct ctl_table_root *root = head->root;
432 	int mode;
433 
434 	if (root->permissions)
435 		mode = root->permissions(head, table);
436 	else
437 		mode = table->mode;
438 
439 	return test_perm(mode, op);
440 }
441 
proc_sys_make_inode(struct super_block * sb,struct ctl_table_header * head,struct ctl_table * table)442 static struct inode *proc_sys_make_inode(struct super_block *sb,
443 		struct ctl_table_header *head, struct ctl_table *table)
444 {
445 	struct ctl_table_root *root = head->root;
446 	struct inode *inode;
447 	struct proc_inode *ei;
448 
449 	inode = new_inode(sb);
450 	if (!inode)
451 		return ERR_PTR(-ENOMEM);
452 
453 	inode->i_ino = get_next_ino();
454 
455 	ei = PROC_I(inode);
456 
457 	spin_lock(&sysctl_lock);
458 	if (unlikely(head->unregistering)) {
459 		spin_unlock(&sysctl_lock);
460 		iput(inode);
461 		return ERR_PTR(-ENOENT);
462 	}
463 	ei->sysctl = head;
464 	ei->sysctl_entry = table;
465 	hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
466 	head->count++;
467 	spin_unlock(&sysctl_lock);
468 
469 	inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
470 	inode->i_mode = table->mode;
471 	if (!S_ISDIR(table->mode)) {
472 		inode->i_mode |= S_IFREG;
473 		inode->i_op = &proc_sys_inode_operations;
474 		inode->i_fop = &proc_sys_file_operations;
475 	} else {
476 		inode->i_mode |= S_IFDIR;
477 		inode->i_op = &proc_sys_dir_operations;
478 		inode->i_fop = &proc_sys_dir_file_operations;
479 		if (sysctl_is_perm_empty_ctl_header(head))
480 			make_empty_dir_inode(inode);
481 	}
482 
483 	inode->i_uid = GLOBAL_ROOT_UID;
484 	inode->i_gid = GLOBAL_ROOT_GID;
485 	if (root->set_ownership)
486 		root->set_ownership(head, &inode->i_uid, &inode->i_gid);
487 
488 	return inode;
489 }
490 
proc_sys_evict_inode(struct inode * inode,struct ctl_table_header * head)491 void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
492 {
493 	spin_lock(&sysctl_lock);
494 	hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
495 	if (!--head->count)
496 		kfree_rcu(head, rcu);
497 	spin_unlock(&sysctl_lock);
498 }
499 
grab_header(struct inode * inode)500 static struct ctl_table_header *grab_header(struct inode *inode)
501 {
502 	struct ctl_table_header *head = PROC_I(inode)->sysctl;
503 	if (!head)
504 		head = &sysctl_table_root.default_set.dir.header;
505 	return sysctl_head_grab(head);
506 }
507 
proc_sys_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)508 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
509 					unsigned int flags)
510 {
511 	struct ctl_table_header *head = grab_header(dir);
512 	struct ctl_table_header *h = NULL;
513 	const struct qstr *name = &dentry->d_name;
514 	struct ctl_table *p;
515 	struct inode *inode;
516 	struct dentry *err = ERR_PTR(-ENOENT);
517 	struct ctl_dir *ctl_dir;
518 	int ret;
519 
520 	if (IS_ERR(head))
521 		return ERR_CAST(head);
522 
523 	ctl_dir = container_of(head, struct ctl_dir, header);
524 
525 	p = lookup_entry(&h, ctl_dir, name->name, name->len);
526 	if (!p)
527 		goto out;
528 
529 	if (S_ISLNK(p->mode)) {
530 		ret = sysctl_follow_link(&h, &p);
531 		err = ERR_PTR(ret);
532 		if (ret)
533 			goto out;
534 	}
535 
536 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
537 	if (IS_ERR(inode)) {
538 		err = ERR_CAST(inode);
539 		goto out;
540 	}
541 
542 	d_set_d_op(dentry, &proc_sys_dentry_operations);
543 	err = d_splice_alias(inode, dentry);
544 
545 out:
546 	if (h)
547 		sysctl_head_finish(h);
548 	sysctl_head_finish(head);
549 	return err;
550 }
551 
proc_sys_call_handler(struct kiocb * iocb,struct iov_iter * iter,int write)552 static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
553 		int write)
554 {
555 	struct inode *inode = file_inode(iocb->ki_filp);
556 	struct ctl_table_header *head = grab_header(inode);
557 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
558 	size_t count = iov_iter_count(iter);
559 	char *kbuf;
560 	ssize_t error;
561 
562 	if (IS_ERR(head))
563 		return PTR_ERR(head);
564 
565 	/*
566 	 * At this point we know that the sysctl was not unregistered
567 	 * and won't be until we finish.
568 	 */
569 	error = -EPERM;
570 	if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
571 		goto out;
572 
573 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
574 	error = -EINVAL;
575 	if (!table->proc_handler)
576 		goto out;
577 
578 	/* don't even try if the size is too large */
579 	error = -ENOMEM;
580 	if (count >= KMALLOC_MAX_SIZE)
581 		goto out;
582 	kbuf = kvzalloc(count + 1, GFP_KERNEL);
583 	if (!kbuf)
584 		goto out;
585 
586 	if (write) {
587 		error = -EFAULT;
588 		if (!copy_from_iter_full(kbuf, count, iter))
589 			goto out_free_buf;
590 		kbuf[count] = '\0';
591 	}
592 
593 	error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
594 					   &iocb->ki_pos);
595 	if (error)
596 		goto out_free_buf;
597 
598 	/* careful: calling conventions are nasty here */
599 	error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
600 	if (error)
601 		goto out_free_buf;
602 
603 	if (!write) {
604 		error = -EFAULT;
605 		if (copy_to_iter(kbuf, count, iter) < count)
606 			goto out_free_buf;
607 	}
608 
609 	error = count;
610 out_free_buf:
611 	kvfree(kbuf);
612 out:
613 	sysctl_head_finish(head);
614 
615 	return error;
616 }
617 
proc_sys_read(struct kiocb * iocb,struct iov_iter * iter)618 static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
619 {
620 	return proc_sys_call_handler(iocb, iter, 0);
621 }
622 
proc_sys_write(struct kiocb * iocb,struct iov_iter * iter)623 static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
624 {
625 	return proc_sys_call_handler(iocb, iter, 1);
626 }
627 
proc_sys_open(struct inode * inode,struct file * filp)628 static int proc_sys_open(struct inode *inode, struct file *filp)
629 {
630 	struct ctl_table_header *head = grab_header(inode);
631 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
632 
633 	/* sysctl was unregistered */
634 	if (IS_ERR(head))
635 		return PTR_ERR(head);
636 
637 	if (table->poll)
638 		filp->private_data = proc_sys_poll_event(table->poll);
639 
640 	sysctl_head_finish(head);
641 
642 	return 0;
643 }
644 
proc_sys_poll(struct file * filp,poll_table * wait)645 static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
646 {
647 	struct inode *inode = file_inode(filp);
648 	struct ctl_table_header *head = grab_header(inode);
649 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
650 	__poll_t ret = DEFAULT_POLLMASK;
651 	unsigned long event;
652 
653 	/* sysctl was unregistered */
654 	if (IS_ERR(head))
655 		return EPOLLERR | EPOLLHUP;
656 
657 	if (!table->proc_handler)
658 		goto out;
659 
660 	if (!table->poll)
661 		goto out;
662 
663 	event = (unsigned long)filp->private_data;
664 	poll_wait(filp, &table->poll->wait, wait);
665 
666 	if (event != atomic_read(&table->poll->event)) {
667 		filp->private_data = proc_sys_poll_event(table->poll);
668 		ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
669 	}
670 
671 out:
672 	sysctl_head_finish(head);
673 
674 	return ret;
675 }
676 
proc_sys_fill_cache(struct file * file,struct dir_context * ctx,struct ctl_table_header * head,struct ctl_table * table)677 static bool proc_sys_fill_cache(struct file *file,
678 				struct dir_context *ctx,
679 				struct ctl_table_header *head,
680 				struct ctl_table *table)
681 {
682 	struct dentry *child, *dir = file->f_path.dentry;
683 	struct inode *inode;
684 	struct qstr qname;
685 	ino_t ino = 0;
686 	unsigned type = DT_UNKNOWN;
687 
688 	qname.name = table->procname;
689 	qname.len  = strlen(table->procname);
690 	qname.hash = full_name_hash(dir, qname.name, qname.len);
691 
692 	child = d_lookup(dir, &qname);
693 	if (!child) {
694 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
695 		child = d_alloc_parallel(dir, &qname, &wq);
696 		if (IS_ERR(child))
697 			return false;
698 		if (d_in_lookup(child)) {
699 			struct dentry *res;
700 			inode = proc_sys_make_inode(dir->d_sb, head, table);
701 			if (IS_ERR(inode)) {
702 				d_lookup_done(child);
703 				dput(child);
704 				return false;
705 			}
706 			d_set_d_op(child, &proc_sys_dentry_operations);
707 			res = d_splice_alias(inode, child);
708 			d_lookup_done(child);
709 			if (unlikely(res)) {
710 				if (IS_ERR(res)) {
711 					dput(child);
712 					return false;
713 				}
714 				dput(child);
715 				child = res;
716 			}
717 		}
718 	}
719 	inode = d_inode(child);
720 	ino  = inode->i_ino;
721 	type = inode->i_mode >> 12;
722 	dput(child);
723 	return dir_emit(ctx, qname.name, qname.len, ino, type);
724 }
725 
proc_sys_link_fill_cache(struct file * file,struct dir_context * ctx,struct ctl_table_header * head,struct ctl_table * table)726 static bool proc_sys_link_fill_cache(struct file *file,
727 				    struct dir_context *ctx,
728 				    struct ctl_table_header *head,
729 				    struct ctl_table *table)
730 {
731 	bool ret = true;
732 
733 	head = sysctl_head_grab(head);
734 	if (IS_ERR(head))
735 		return false;
736 
737 	/* It is not an error if we can not follow the link ignore it */
738 	if (sysctl_follow_link(&head, &table))
739 		goto out;
740 
741 	ret = proc_sys_fill_cache(file, ctx, head, table);
742 out:
743 	sysctl_head_finish(head);
744 	return ret;
745 }
746 
scan(struct ctl_table_header * head,struct ctl_table * table,unsigned long * pos,struct file * file,struct dir_context * ctx)747 static int scan(struct ctl_table_header *head, struct ctl_table *table,
748 		unsigned long *pos, struct file *file,
749 		struct dir_context *ctx)
750 {
751 	bool res;
752 
753 	if ((*pos)++ < ctx->pos)
754 		return true;
755 
756 	if (unlikely(S_ISLNK(table->mode)))
757 		res = proc_sys_link_fill_cache(file, ctx, head, table);
758 	else
759 		res = proc_sys_fill_cache(file, ctx, head, table);
760 
761 	if (res)
762 		ctx->pos = *pos;
763 
764 	return res;
765 }
766 
proc_sys_readdir(struct file * file,struct dir_context * ctx)767 static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
768 {
769 	struct ctl_table_header *head = grab_header(file_inode(file));
770 	struct ctl_table_header *h = NULL;
771 	struct ctl_table *entry;
772 	struct ctl_dir *ctl_dir;
773 	unsigned long pos;
774 
775 	if (IS_ERR(head))
776 		return PTR_ERR(head);
777 
778 	ctl_dir = container_of(head, struct ctl_dir, header);
779 
780 	if (!dir_emit_dots(file, ctx))
781 		goto out;
782 
783 	pos = 2;
784 
785 	for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
786 		if (!scan(h, entry, &pos, file, ctx)) {
787 			sysctl_head_finish(h);
788 			break;
789 		}
790 	}
791 out:
792 	sysctl_head_finish(head);
793 	return 0;
794 }
795 
proc_sys_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)796 static int proc_sys_permission(struct mnt_idmap *idmap,
797 			       struct inode *inode, int mask)
798 {
799 	/*
800 	 * sysctl entries that are not writeable,
801 	 * are _NOT_ writeable, capabilities or not.
802 	 */
803 	struct ctl_table_header *head;
804 	struct ctl_table *table;
805 	int error;
806 
807 	/* Executable files are not allowed under /proc/sys/ */
808 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
809 		return -EACCES;
810 
811 	head = grab_header(inode);
812 	if (IS_ERR(head))
813 		return PTR_ERR(head);
814 
815 	table = PROC_I(inode)->sysctl_entry;
816 	if (!table) /* global root - r-xr-xr-x */
817 		error = mask & MAY_WRITE ? -EACCES : 0;
818 	else /* Use the permissions on the sysctl table entry */
819 		error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
820 
821 	sysctl_head_finish(head);
822 	return error;
823 }
824 
proc_sys_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)825 static int proc_sys_setattr(struct mnt_idmap *idmap,
826 			    struct dentry *dentry, struct iattr *attr)
827 {
828 	struct inode *inode = d_inode(dentry);
829 	int error;
830 
831 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
832 		return -EPERM;
833 
834 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
835 	if (error)
836 		return error;
837 
838 	setattr_copy(&nop_mnt_idmap, inode, attr);
839 	return 0;
840 }
841 
proc_sys_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)842 static int proc_sys_getattr(struct mnt_idmap *idmap,
843 			    const struct path *path, struct kstat *stat,
844 			    u32 request_mask, unsigned int query_flags)
845 {
846 	struct inode *inode = d_inode(path->dentry);
847 	struct ctl_table_header *head = grab_header(inode);
848 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
849 
850 	if (IS_ERR(head))
851 		return PTR_ERR(head);
852 
853 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
854 	if (table)
855 		stat->mode = (stat->mode & S_IFMT) | table->mode;
856 
857 	sysctl_head_finish(head);
858 	return 0;
859 }
860 
861 static const struct file_operations proc_sys_file_operations = {
862 	.open		= proc_sys_open,
863 	.poll		= proc_sys_poll,
864 	.read_iter	= proc_sys_read,
865 	.write_iter	= proc_sys_write,
866 	.splice_read	= copy_splice_read,
867 	.splice_write	= iter_file_splice_write,
868 	.llseek		= default_llseek,
869 };
870 
871 static const struct file_operations proc_sys_dir_file_operations = {
872 	.read		= generic_read_dir,
873 	.iterate_shared	= proc_sys_readdir,
874 	.llseek		= generic_file_llseek,
875 };
876 
877 static const struct inode_operations proc_sys_inode_operations = {
878 	.permission	= proc_sys_permission,
879 	.setattr	= proc_sys_setattr,
880 	.getattr	= proc_sys_getattr,
881 };
882 
883 static const struct inode_operations proc_sys_dir_operations = {
884 	.lookup		= proc_sys_lookup,
885 	.permission	= proc_sys_permission,
886 	.setattr	= proc_sys_setattr,
887 	.getattr	= proc_sys_getattr,
888 };
889 
proc_sys_revalidate(struct dentry * dentry,unsigned int flags)890 static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
891 {
892 	if (flags & LOOKUP_RCU)
893 		return -ECHILD;
894 	return !PROC_I(d_inode(dentry))->sysctl->unregistering;
895 }
896 
proc_sys_delete(const struct dentry * dentry)897 static int proc_sys_delete(const struct dentry *dentry)
898 {
899 	return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
900 }
901 
sysctl_is_seen(struct ctl_table_header * p)902 static int sysctl_is_seen(struct ctl_table_header *p)
903 {
904 	struct ctl_table_set *set = p->set;
905 	int res;
906 	spin_lock(&sysctl_lock);
907 	if (p->unregistering)
908 		res = 0;
909 	else if (!set->is_seen)
910 		res = 1;
911 	else
912 		res = set->is_seen(set);
913 	spin_unlock(&sysctl_lock);
914 	return res;
915 }
916 
proc_sys_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)917 static int proc_sys_compare(const struct dentry *dentry,
918 		unsigned int len, const char *str, const struct qstr *name)
919 {
920 	struct ctl_table_header *head;
921 	struct inode *inode;
922 
923 	/* Although proc doesn't have negative dentries, rcu-walk means
924 	 * that inode here can be NULL */
925 	/* AV: can it, indeed? */
926 	inode = d_inode_rcu(dentry);
927 	if (!inode)
928 		return 1;
929 	if (name->len != len)
930 		return 1;
931 	if (memcmp(name->name, str, len))
932 		return 1;
933 	head = rcu_dereference(PROC_I(inode)->sysctl);
934 	return !head || !sysctl_is_seen(head);
935 }
936 
937 static const struct dentry_operations proc_sys_dentry_operations = {
938 	.d_revalidate	= proc_sys_revalidate,
939 	.d_delete	= proc_sys_delete,
940 	.d_compare	= proc_sys_compare,
941 };
942 
find_subdir(struct ctl_dir * dir,const char * name,int namelen)943 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
944 				   const char *name, int namelen)
945 {
946 	struct ctl_table_header *head;
947 	struct ctl_table *entry;
948 
949 	entry = find_entry(&head, dir, name, namelen);
950 	if (!entry)
951 		return ERR_PTR(-ENOENT);
952 	if (!S_ISDIR(entry->mode))
953 		return ERR_PTR(-ENOTDIR);
954 	return container_of(head, struct ctl_dir, header);
955 }
956 
new_dir(struct ctl_table_set * set,const char * name,int namelen)957 static struct ctl_dir *new_dir(struct ctl_table_set *set,
958 			       const char *name, int namelen)
959 {
960 	struct ctl_table *table;
961 	struct ctl_dir *new;
962 	struct ctl_node *node;
963 	char *new_name;
964 
965 	new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
966 		      sizeof(struct ctl_table)*2 +  namelen + 1,
967 		      GFP_KERNEL);
968 	if (!new)
969 		return NULL;
970 
971 	node = (struct ctl_node *)(new + 1);
972 	table = (struct ctl_table *)(node + 1);
973 	new_name = (char *)(table + 2);
974 	memcpy(new_name, name, namelen);
975 	table[0].procname = new_name;
976 	table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
977 	init_header(&new->header, set->dir.header.root, set, node, table, 1);
978 
979 	return new;
980 }
981 
982 /**
983  * get_subdir - find or create a subdir with the specified name.
984  * @dir:  Directory to create the subdirectory in
985  * @name: The name of the subdirectory to find or create
986  * @namelen: The length of name
987  *
988  * Takes a directory with an elevated reference count so we know that
989  * if we drop the lock the directory will not go away.  Upon success
990  * the reference is moved from @dir to the returned subdirectory.
991  * Upon error an error code is returned and the reference on @dir is
992  * simply dropped.
993  */
get_subdir(struct ctl_dir * dir,const char * name,int namelen)994 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
995 				  const char *name, int namelen)
996 {
997 	struct ctl_table_set *set = dir->header.set;
998 	struct ctl_dir *subdir, *new = NULL;
999 	int err;
1000 
1001 	spin_lock(&sysctl_lock);
1002 	subdir = find_subdir(dir, name, namelen);
1003 	if (!IS_ERR(subdir))
1004 		goto found;
1005 	if (PTR_ERR(subdir) != -ENOENT)
1006 		goto failed;
1007 
1008 	spin_unlock(&sysctl_lock);
1009 	new = new_dir(set, name, namelen);
1010 	spin_lock(&sysctl_lock);
1011 	subdir = ERR_PTR(-ENOMEM);
1012 	if (!new)
1013 		goto failed;
1014 
1015 	/* Was the subdir added while we dropped the lock? */
1016 	subdir = find_subdir(dir, name, namelen);
1017 	if (!IS_ERR(subdir))
1018 		goto found;
1019 	if (PTR_ERR(subdir) != -ENOENT)
1020 		goto failed;
1021 
1022 	/* Nope.  Use the our freshly made directory entry. */
1023 	err = insert_header(dir, &new->header);
1024 	subdir = ERR_PTR(err);
1025 	if (err)
1026 		goto failed;
1027 	subdir = new;
1028 found:
1029 	subdir->header.nreg++;
1030 failed:
1031 	if (IS_ERR(subdir)) {
1032 		pr_err("sysctl could not get directory: ");
1033 		sysctl_print_dir(dir);
1034 		pr_cont("%*.*s %ld\n", namelen, namelen, name,
1035 			PTR_ERR(subdir));
1036 	}
1037 	drop_sysctl_table(&dir->header);
1038 	if (new)
1039 		drop_sysctl_table(&new->header);
1040 	spin_unlock(&sysctl_lock);
1041 	return subdir;
1042 }
1043 
xlate_dir(struct ctl_table_set * set,struct ctl_dir * dir)1044 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1045 {
1046 	struct ctl_dir *parent;
1047 	const char *procname;
1048 	if (!dir->header.parent)
1049 		return &set->dir;
1050 	parent = xlate_dir(set, dir->header.parent);
1051 	if (IS_ERR(parent))
1052 		return parent;
1053 	procname = dir->header.ctl_table[0].procname;
1054 	return find_subdir(parent, procname, strlen(procname));
1055 }
1056 
sysctl_follow_link(struct ctl_table_header ** phead,struct ctl_table ** pentry)1057 static int sysctl_follow_link(struct ctl_table_header **phead,
1058 	struct ctl_table **pentry)
1059 {
1060 	struct ctl_table_header *head;
1061 	struct ctl_table_root *root;
1062 	struct ctl_table_set *set;
1063 	struct ctl_table *entry;
1064 	struct ctl_dir *dir;
1065 	int ret;
1066 
1067 	spin_lock(&sysctl_lock);
1068 	root = (*pentry)->data;
1069 	set = lookup_header_set(root);
1070 	dir = xlate_dir(set, (*phead)->parent);
1071 	if (IS_ERR(dir))
1072 		ret = PTR_ERR(dir);
1073 	else {
1074 		const char *procname = (*pentry)->procname;
1075 		head = NULL;
1076 		entry = find_entry(&head, dir, procname, strlen(procname));
1077 		ret = -ENOENT;
1078 		if (entry && use_table(head)) {
1079 			unuse_table(*phead);
1080 			*phead = head;
1081 			*pentry = entry;
1082 			ret = 0;
1083 		}
1084 	}
1085 
1086 	spin_unlock(&sysctl_lock);
1087 	return ret;
1088 }
1089 
sysctl_err(const char * path,struct ctl_table * table,char * fmt,...)1090 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1091 {
1092 	struct va_format vaf;
1093 	va_list args;
1094 
1095 	va_start(args, fmt);
1096 	vaf.fmt = fmt;
1097 	vaf.va = &args;
1098 
1099 	pr_err("sysctl table check failed: %s/%s %pV\n",
1100 	       path, table->procname, &vaf);
1101 
1102 	va_end(args);
1103 	return -EINVAL;
1104 }
1105 
sysctl_check_table_array(const char * path,struct ctl_table * table)1106 static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1107 {
1108 	int err = 0;
1109 
1110 	if ((table->proc_handler == proc_douintvec) ||
1111 	    (table->proc_handler == proc_douintvec_minmax)) {
1112 		if (table->maxlen != sizeof(unsigned int))
1113 			err |= sysctl_err(path, table, "array not allowed");
1114 	}
1115 
1116 	if (table->proc_handler == proc_dou8vec_minmax) {
1117 		if (table->maxlen != sizeof(u8))
1118 			err |= sysctl_err(path, table, "array not allowed");
1119 	}
1120 
1121 	if (table->proc_handler == proc_dobool) {
1122 		if (table->maxlen != sizeof(bool))
1123 			err |= sysctl_err(path, table, "array not allowed");
1124 	}
1125 
1126 	return err;
1127 }
1128 
sysctl_check_table(const char * path,struct ctl_table_header * header)1129 static int sysctl_check_table(const char *path, struct ctl_table_header *header)
1130 {
1131 	struct ctl_table *entry;
1132 	int err = 0;
1133 	list_for_each_table_entry(entry, header) {
1134 		if ((entry->proc_handler == proc_dostring) ||
1135 		    (entry->proc_handler == proc_dobool) ||
1136 		    (entry->proc_handler == proc_dointvec) ||
1137 		    (entry->proc_handler == proc_douintvec) ||
1138 		    (entry->proc_handler == proc_douintvec_minmax) ||
1139 		    (entry->proc_handler == proc_dointvec_minmax) ||
1140 		    (entry->proc_handler == proc_dou8vec_minmax) ||
1141 		    (entry->proc_handler == proc_dointvec_jiffies) ||
1142 		    (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1143 		    (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1144 		    (entry->proc_handler == proc_doulongvec_minmax) ||
1145 		    (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1146 			if (!entry->data)
1147 				err |= sysctl_err(path, entry, "No data");
1148 			if (!entry->maxlen)
1149 				err |= sysctl_err(path, entry, "No maxlen");
1150 			else
1151 				err |= sysctl_check_table_array(path, entry);
1152 		}
1153 		if (!entry->proc_handler)
1154 			err |= sysctl_err(path, entry, "No proc_handler");
1155 
1156 		if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1157 			err |= sysctl_err(path, entry, "bogus .mode 0%o",
1158 				entry->mode);
1159 	}
1160 	return err;
1161 }
1162 
new_links(struct ctl_dir * dir,struct ctl_table_header * head)1163 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_header *head)
1164 {
1165 	struct ctl_table *link_table, *entry, *link;
1166 	struct ctl_table_header *links;
1167 	struct ctl_node *node;
1168 	char *link_name;
1169 	int nr_entries, name_bytes;
1170 
1171 	name_bytes = 0;
1172 	nr_entries = 0;
1173 	list_for_each_table_entry(entry, head) {
1174 		nr_entries++;
1175 		name_bytes += strlen(entry->procname) + 1;
1176 	}
1177 
1178 	links = kzalloc(sizeof(struct ctl_table_header) +
1179 			sizeof(struct ctl_node)*nr_entries +
1180 			sizeof(struct ctl_table)*(nr_entries + 1) +
1181 			name_bytes,
1182 			GFP_KERNEL);
1183 
1184 	if (!links)
1185 		return NULL;
1186 
1187 	node = (struct ctl_node *)(links + 1);
1188 	link_table = (struct ctl_table *)(node + nr_entries);
1189 	link_name = (char *)&link_table[nr_entries + 1];
1190 	link = link_table;
1191 
1192 	list_for_each_table_entry(entry, head) {
1193 		int len = strlen(entry->procname) + 1;
1194 		memcpy(link_name, entry->procname, len);
1195 		link->procname = link_name;
1196 		link->mode = S_IFLNK|S_IRWXUGO;
1197 		link->data = head->root;
1198 		link_name += len;
1199 		link++;
1200 	}
1201 	init_header(links, dir->header.root, dir->header.set, node, link_table,
1202 		    head->ctl_table_size);
1203 	links->nreg = nr_entries;
1204 
1205 	return links;
1206 }
1207 
get_links(struct ctl_dir * dir,struct ctl_table_header * header,struct ctl_table_root * link_root)1208 static bool get_links(struct ctl_dir *dir,
1209 		      struct ctl_table_header *header,
1210 		      struct ctl_table_root *link_root)
1211 {
1212 	struct ctl_table_header *tmp_head;
1213 	struct ctl_table *entry, *link;
1214 
1215 	if (header->ctl_table_size == 0 ||
1216 	    sysctl_is_perm_empty_ctl_table(header->ctl_table))
1217 		return true;
1218 
1219 	/* Are there links available for every entry in table? */
1220 	list_for_each_table_entry(entry, header) {
1221 		const char *procname = entry->procname;
1222 		link = find_entry(&tmp_head, dir, procname, strlen(procname));
1223 		if (!link)
1224 			return false;
1225 		if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1226 			continue;
1227 		if (S_ISLNK(link->mode) && (link->data == link_root))
1228 			continue;
1229 		return false;
1230 	}
1231 
1232 	/* The checks passed.  Increase the registration count on the links */
1233 	list_for_each_table_entry(entry, header) {
1234 		const char *procname = entry->procname;
1235 		link = find_entry(&tmp_head, dir, procname, strlen(procname));
1236 		tmp_head->nreg++;
1237 	}
1238 	return true;
1239 }
1240 
insert_links(struct ctl_table_header * head)1241 static int insert_links(struct ctl_table_header *head)
1242 {
1243 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1244 	struct ctl_dir *core_parent;
1245 	struct ctl_table_header *links;
1246 	int err;
1247 
1248 	if (head->set == root_set)
1249 		return 0;
1250 
1251 	core_parent = xlate_dir(root_set, head->parent);
1252 	if (IS_ERR(core_parent))
1253 		return 0;
1254 
1255 	if (get_links(core_parent, head, head->root))
1256 		return 0;
1257 
1258 	core_parent->header.nreg++;
1259 	spin_unlock(&sysctl_lock);
1260 
1261 	links = new_links(core_parent, head);
1262 
1263 	spin_lock(&sysctl_lock);
1264 	err = -ENOMEM;
1265 	if (!links)
1266 		goto out;
1267 
1268 	err = 0;
1269 	if (get_links(core_parent, head, head->root)) {
1270 		kfree(links);
1271 		goto out;
1272 	}
1273 
1274 	err = insert_header(core_parent, links);
1275 	if (err)
1276 		kfree(links);
1277 out:
1278 	drop_sysctl_table(&core_parent->header);
1279 	return err;
1280 }
1281 
1282 /* Find the directory for the ctl_table. If one is not found create it. */
sysctl_mkdir_p(struct ctl_dir * dir,const char * path)1283 static struct ctl_dir *sysctl_mkdir_p(struct ctl_dir *dir, const char *path)
1284 {
1285 	const char *name, *nextname;
1286 
1287 	for (name = path; name; name = nextname) {
1288 		int namelen;
1289 		nextname = strchr(name, '/');
1290 		if (nextname) {
1291 			namelen = nextname - name;
1292 			nextname++;
1293 		} else {
1294 			namelen = strlen(name);
1295 		}
1296 		if (namelen == 0)
1297 			continue;
1298 
1299 		/*
1300 		 * namelen ensures if name is "foo/bar/yay" only foo is
1301 		 * registered first. We traverse as if using mkdir -p and
1302 		 * return a ctl_dir for the last directory entry.
1303 		 */
1304 		dir = get_subdir(dir, name, namelen);
1305 		if (IS_ERR(dir))
1306 			break;
1307 	}
1308 	return dir;
1309 }
1310 
1311 /**
1312  * __register_sysctl_table - register a leaf sysctl table
1313  * @set: Sysctl tree to register on
1314  * @path: The path to the directory the sysctl table is in.
1315  * @table: the top-level table structure without any child. This table
1316  * 	 should not be free'd after registration. So it should not be
1317  * 	 used on stack. It can either be a global or dynamically allocated
1318  * 	 by the caller and free'd later after sysctl unregistration.
1319  * @table_size : The number of elements in table
1320  *
1321  * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1322  * array. A completely 0 filled entry terminates the table.
1323  *
1324  * The members of the &struct ctl_table structure are used as follows:
1325  *
1326  * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1327  *            enter a sysctl file
1328  *
1329  * data - a pointer to data for use by proc_handler
1330  *
1331  * maxlen - the maximum size in bytes of the data
1332  *
1333  * mode - the file permissions for the /proc/sys file
1334  *
1335  * child - must be %NULL.
1336  *
1337  * proc_handler - the text handler routine (described below)
1338  *
1339  * extra1, extra2 - extra pointers usable by the proc handler routines
1340  * XXX: we should eventually modify these to use long min / max [0]
1341  * [0] https://lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org
1342  *
1343  * Leaf nodes in the sysctl tree will be represented by a single file
1344  * under /proc; non-leaf nodes (where child is not NULL) are not allowed,
1345  * sysctl_check_table() verifies this.
1346  *
1347  * There must be a proc_handler routine for any terminal nodes.
1348  * Several default handlers are available to cover common cases -
1349  *
1350  * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1351  * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1352  * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1353  *
1354  * It is the handler's job to read the input buffer from user memory
1355  * and process it. The handler should return 0 on success.
1356  *
1357  * This routine returns %NULL on a failure to register, and a pointer
1358  * to the table header on success.
1359  */
__register_sysctl_table(struct ctl_table_set * set,const char * path,struct ctl_table * table,size_t table_size)1360 struct ctl_table_header *__register_sysctl_table(
1361 	struct ctl_table_set *set,
1362 	const char *path, struct ctl_table *table, size_t table_size)
1363 {
1364 	struct ctl_table_root *root = set->dir.header.root;
1365 	struct ctl_table_header *header;
1366 	struct ctl_dir *dir;
1367 	struct ctl_node *node;
1368 
1369 	header = kzalloc(sizeof(struct ctl_table_header) +
1370 			 sizeof(struct ctl_node)*table_size, GFP_KERNEL_ACCOUNT);
1371 	if (!header)
1372 		return NULL;
1373 
1374 	node = (struct ctl_node *)(header + 1);
1375 	init_header(header, root, set, node, table, table_size);
1376 	if (sysctl_check_table(path, header))
1377 		goto fail;
1378 
1379 	spin_lock(&sysctl_lock);
1380 	dir = &set->dir;
1381 	/* Reference moved down the directory tree get_subdir */
1382 	dir->header.nreg++;
1383 	spin_unlock(&sysctl_lock);
1384 
1385 	dir = sysctl_mkdir_p(dir, path);
1386 	if (IS_ERR(dir))
1387 		goto fail;
1388 	spin_lock(&sysctl_lock);
1389 	if (insert_header(dir, header))
1390 		goto fail_put_dir_locked;
1391 
1392 	drop_sysctl_table(&dir->header);
1393 	spin_unlock(&sysctl_lock);
1394 
1395 	return header;
1396 
1397 fail_put_dir_locked:
1398 	drop_sysctl_table(&dir->header);
1399 	spin_unlock(&sysctl_lock);
1400 fail:
1401 	kfree(header);
1402 	return NULL;
1403 }
1404 
1405 /**
1406  * register_sysctl_sz - register a sysctl table
1407  * @path: The path to the directory the sysctl table is in. If the path
1408  * 	doesn't exist we will create it for you.
1409  * @table: the table structure. The calller must ensure the life of the @table
1410  * 	will be kept during the lifetime use of the syctl. It must not be freed
1411  * 	until unregister_sysctl_table() is called with the given returned table
1412  * 	with this registration. If your code is non modular then you don't need
1413  * 	to call unregister_sysctl_table() and can instead use something like
1414  * 	register_sysctl_init() which does not care for the result of the syctl
1415  * 	registration.
1416  * @table_size: The number of elements in table.
1417  *
1418  * Register a sysctl table. @table should be a filled in ctl_table
1419  * array. A completely 0 filled entry terminates the table.
1420  *
1421  * See __register_sysctl_table for more details.
1422  */
register_sysctl_sz(const char * path,struct ctl_table * table,size_t table_size)1423 struct ctl_table_header *register_sysctl_sz(const char *path, struct ctl_table *table,
1424 					    size_t table_size)
1425 {
1426 	return __register_sysctl_table(&sysctl_table_root.default_set,
1427 					path, table, table_size);
1428 }
1429 EXPORT_SYMBOL(register_sysctl_sz);
1430 
1431 /**
1432  * __register_sysctl_init() - register sysctl table to path
1433  * @path: path name for sysctl base. If that path doesn't exist we will create
1434  * 	it for you.
1435  * @table: This is the sysctl table that needs to be registered to the path.
1436  * 	The caller must ensure the life of the @table will be kept during the
1437  * 	lifetime use of the sysctl.
1438  * @table_name: The name of sysctl table, only used for log printing when
1439  *              registration fails
1440  * @table_size: The number of elements in table
1441  *
1442  * The sysctl interface is used by userspace to query or modify at runtime
1443  * a predefined value set on a variable. These variables however have default
1444  * values pre-set. Code which depends on these variables will always work even
1445  * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1446  * ability to query or modify the sysctls dynamically at run time. Chances of
1447  * register_sysctl() failing on init are extremely low, and so for both reasons
1448  * this function does not return any error as it is used by initialization code.
1449  *
1450  * Context: if your base directory does not exist it will be created for you.
1451  */
__register_sysctl_init(const char * path,struct ctl_table * table,const char * table_name,size_t table_size)1452 void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1453 				 const char *table_name, size_t table_size)
1454 {
1455 	struct ctl_table_header *hdr = register_sysctl_sz(path, table, table_size);
1456 
1457 	if (unlikely(!hdr)) {
1458 		pr_err("failed when register_sysctl_sz %s to %s\n", table_name, path);
1459 		return;
1460 	}
1461 	kmemleak_not_leak(hdr);
1462 }
1463 
put_links(struct ctl_table_header * header)1464 static void put_links(struct ctl_table_header *header)
1465 {
1466 	struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1467 	struct ctl_table_root *root = header->root;
1468 	struct ctl_dir *parent = header->parent;
1469 	struct ctl_dir *core_parent;
1470 	struct ctl_table *entry;
1471 
1472 	if (header->set == root_set)
1473 		return;
1474 
1475 	core_parent = xlate_dir(root_set, parent);
1476 	if (IS_ERR(core_parent))
1477 		return;
1478 
1479 	list_for_each_table_entry(entry, header) {
1480 		struct ctl_table_header *link_head;
1481 		struct ctl_table *link;
1482 		const char *name = entry->procname;
1483 
1484 		link = find_entry(&link_head, core_parent, name, strlen(name));
1485 		if (link &&
1486 		    ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1487 		     (S_ISLNK(link->mode) && (link->data == root)))) {
1488 			drop_sysctl_table(link_head);
1489 		}
1490 		else {
1491 			pr_err("sysctl link missing during unregister: ");
1492 			sysctl_print_dir(parent);
1493 			pr_cont("%s\n", name);
1494 		}
1495 	}
1496 }
1497 
drop_sysctl_table(struct ctl_table_header * header)1498 static void drop_sysctl_table(struct ctl_table_header *header)
1499 {
1500 	struct ctl_dir *parent = header->parent;
1501 
1502 	if (--header->nreg)
1503 		return;
1504 
1505 	if (parent) {
1506 		put_links(header);
1507 		start_unregistering(header);
1508 	}
1509 
1510 	if (!--header->count)
1511 		kfree_rcu(header, rcu);
1512 
1513 	if (parent)
1514 		drop_sysctl_table(&parent->header);
1515 }
1516 
1517 /**
1518  * unregister_sysctl_table - unregister a sysctl table hierarchy
1519  * @header: the header returned from register_sysctl or __register_sysctl_table
1520  *
1521  * Unregisters the sysctl table and all children. proc entries may not
1522  * actually be removed until they are no longer used by anyone.
1523  */
unregister_sysctl_table(struct ctl_table_header * header)1524 void unregister_sysctl_table(struct ctl_table_header * header)
1525 {
1526 	might_sleep();
1527 
1528 	if (header == NULL)
1529 		return;
1530 
1531 	spin_lock(&sysctl_lock);
1532 	drop_sysctl_table(header);
1533 	spin_unlock(&sysctl_lock);
1534 }
1535 EXPORT_SYMBOL(unregister_sysctl_table);
1536 
setup_sysctl_set(struct ctl_table_set * set,struct ctl_table_root * root,int (* is_seen)(struct ctl_table_set *))1537 void setup_sysctl_set(struct ctl_table_set *set,
1538 	struct ctl_table_root *root,
1539 	int (*is_seen)(struct ctl_table_set *))
1540 {
1541 	memset(set, 0, sizeof(*set));
1542 	set->is_seen = is_seen;
1543 	init_header(&set->dir.header, root, set, NULL, root_table, 1);
1544 }
1545 
retire_sysctl_set(struct ctl_table_set * set)1546 void retire_sysctl_set(struct ctl_table_set *set)
1547 {
1548 	WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1549 }
1550 
proc_sys_init(void)1551 int __init proc_sys_init(void)
1552 {
1553 	struct proc_dir_entry *proc_sys_root;
1554 
1555 	proc_sys_root = proc_mkdir("sys", NULL);
1556 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
1557 	proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
1558 	proc_sys_root->nlink = 0;
1559 
1560 	return sysctl_init_bases();
1561 }
1562 
1563 struct sysctl_alias {
1564 	const char *kernel_param;
1565 	const char *sysctl_param;
1566 };
1567 
1568 /*
1569  * Historically some settings had both sysctl and a command line parameter.
1570  * With the generic sysctl. parameter support, we can handle them at a single
1571  * place and only keep the historical name for compatibility. This is not meant
1572  * to add brand new aliases. When adding existing aliases, consider whether
1573  * the possibly different moment of changing the value (e.g. from early_param
1574  * to the moment do_sysctl_args() is called) is an issue for the specific
1575  * parameter.
1576  */
1577 static const struct sysctl_alias sysctl_aliases[] = {
1578 	{"hardlockup_all_cpu_backtrace",	"kernel.hardlockup_all_cpu_backtrace" },
1579 	{"hung_task_panic",			"kernel.hung_task_panic" },
1580 	{"numa_zonelist_order",			"vm.numa_zonelist_order" },
1581 	{"softlockup_all_cpu_backtrace",	"kernel.softlockup_all_cpu_backtrace" },
1582 	{ }
1583 };
1584 
sysctl_find_alias(char * param)1585 static const char *sysctl_find_alias(char *param)
1586 {
1587 	const struct sysctl_alias *alias;
1588 
1589 	for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
1590 		if (strcmp(alias->kernel_param, param) == 0)
1591 			return alias->sysctl_param;
1592 	}
1593 
1594 	return NULL;
1595 }
1596 
sysctl_is_alias(char * param)1597 bool sysctl_is_alias(char *param)
1598 {
1599 	const char *alias = sysctl_find_alias(param);
1600 
1601 	return alias != NULL;
1602 }
1603 
1604 /* Set sysctl value passed on kernel command line. */
process_sysctl_arg(char * param,char * val,const char * unused,void * arg)1605 static int process_sysctl_arg(char *param, char *val,
1606 			       const char *unused, void *arg)
1607 {
1608 	char *path;
1609 	struct vfsmount **proc_mnt = arg;
1610 	struct file_system_type *proc_fs_type;
1611 	struct file *file;
1612 	int len;
1613 	int err;
1614 	loff_t pos = 0;
1615 	ssize_t wret;
1616 
1617 	if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
1618 		param += sizeof("sysctl") - 1;
1619 
1620 		if (param[0] != '/' && param[0] != '.')
1621 			return 0;
1622 
1623 		param++;
1624 	} else {
1625 		param = (char *) sysctl_find_alias(param);
1626 		if (!param)
1627 			return 0;
1628 	}
1629 
1630 	if (!val)
1631 		return -EINVAL;
1632 	len = strlen(val);
1633 	if (len == 0)
1634 		return -EINVAL;
1635 
1636 	/*
1637 	 * To set sysctl options, we use a temporary mount of proc, look up the
1638 	 * respective sys/ file and write to it. To avoid mounting it when no
1639 	 * options were given, we mount it only when the first sysctl option is
1640 	 * found. Why not a persistent mount? There are problems with a
1641 	 * persistent mount of proc in that it forces userspace not to use any
1642 	 * proc mount options.
1643 	 */
1644 	if (!*proc_mnt) {
1645 		proc_fs_type = get_fs_type("proc");
1646 		if (!proc_fs_type) {
1647 			pr_err("Failed to find procfs to set sysctl from command line\n");
1648 			return 0;
1649 		}
1650 		*proc_mnt = kern_mount(proc_fs_type);
1651 		put_filesystem(proc_fs_type);
1652 		if (IS_ERR(*proc_mnt)) {
1653 			pr_err("Failed to mount procfs to set sysctl from command line\n");
1654 			return 0;
1655 		}
1656 	}
1657 
1658 	path = kasprintf(GFP_KERNEL, "sys/%s", param);
1659 	if (!path)
1660 		panic("%s: Failed to allocate path for %s\n", __func__, param);
1661 	strreplace(path, '.', '/');
1662 
1663 	file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
1664 	if (IS_ERR(file)) {
1665 		err = PTR_ERR(file);
1666 		if (err == -ENOENT)
1667 			pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
1668 				param, val);
1669 		else if (err == -EACCES)
1670 			pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
1671 				param, val);
1672 		else
1673 			pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
1674 				file, param, val);
1675 		goto out;
1676 	}
1677 	wret = kernel_write(file, val, len, &pos);
1678 	if (wret < 0) {
1679 		err = wret;
1680 		if (err == -EINVAL)
1681 			pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
1682 				param, val);
1683 		else
1684 			pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
1685 				ERR_PTR(err), param, val);
1686 	} else if (wret != len) {
1687 		pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
1688 			wret, len, path, param, val);
1689 	}
1690 
1691 	err = filp_close(file, NULL);
1692 	if (err)
1693 		pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
1694 			ERR_PTR(err), param, val);
1695 out:
1696 	kfree(path);
1697 	return 0;
1698 }
1699 
do_sysctl_args(void)1700 void do_sysctl_args(void)
1701 {
1702 	char *command_line;
1703 	struct vfsmount *proc_mnt = NULL;
1704 
1705 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
1706 	if (!command_line)
1707 		panic("%s: Failed to allocate copy of command line\n", __func__);
1708 
1709 	parse_args("Setting sysctl args", command_line,
1710 		   NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
1711 
1712 	if (proc_mnt)
1713 		kern_unmount(proc_mnt);
1714 
1715 	kfree(command_line);
1716 }
1717