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