Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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; ++i, entry++)
25
26static const struct dentry_operations proc_sys_dentry_operations;
27static const struct file_operations proc_sys_file_operations;
28static const struct inode_operations proc_sys_inode_operations;
29static const struct file_operations proc_sys_dir_file_operations;
30static const struct inode_operations proc_sys_dir_operations;
31
32/* Support for permanently empty directories */
33static 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 */
43struct ctl_table_header *register_sysctl_mount_point(const char *path)
44{
45 return register_sysctl(path, sysctl_mount_point);
46}
47EXPORT_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
56void 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
65static struct ctl_table root_table[] = {
66 {
67 .procname = "",
68 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
69 },
70};
71static 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
82static DEFINE_SPINLOCK(sysctl_lock);
83
84static void drop_sysctl_table(struct ctl_table_header *header);
85static int sysctl_follow_link(struct ctl_table_header **phead,
86 struct ctl_table **pentry);
87static int insert_links(struct ctl_table_header *head);
88static void put_links(struct ctl_table_header *header);
89
90static 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
97static 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 */
108static 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
139static 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
178static 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
185static 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
213static 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
221static 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;
250fail:
251 erase_header(header);
252 put_links(header);
253fail_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 */
262static 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 */
271static void unuse_table(struct ctl_table_header *p)
272{
273 if (!--p->used)
274 if (unlikely(p->unregistering))
275 complete(p->unregistering);
276}
277
278static 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 */
284static 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
314static 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
324static 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
333static struct ctl_table_set *
334lookup_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
342static 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
359static 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
371static 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
389static 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
414static 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
425static 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
438static 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 inode->i_uid = GLOBAL_ROOT_UID;
480 inode->i_gid = GLOBAL_ROOT_GID;
481 if (root->set_ownership)
482 root->set_ownership(head, &inode->i_uid, &inode->i_gid);
483
484 return inode;
485}
486
487void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
488{
489 spin_lock(&sysctl_lock);
490 hlist_del_init_rcu(&PROC_I(inode)->sibling_inodes);
491 if (!--head->count)
492 kfree_rcu(head, rcu);
493 spin_unlock(&sysctl_lock);
494}
495
496static struct ctl_table_header *grab_header(struct inode *inode)
497{
498 struct ctl_table_header *head = PROC_I(inode)->sysctl;
499 if (!head)
500 head = &sysctl_table_root.default_set.dir.header;
501 return sysctl_head_grab(head);
502}
503
504static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
505 unsigned int flags)
506{
507 struct ctl_table_header *head = grab_header(dir);
508 struct ctl_table_header *h = NULL;
509 const struct qstr *name = &dentry->d_name;
510 struct ctl_table *p;
511 struct inode *inode;
512 struct dentry *err = ERR_PTR(-ENOENT);
513 struct ctl_dir *ctl_dir;
514 int ret;
515
516 if (IS_ERR(head))
517 return ERR_CAST(head);
518
519 ctl_dir = container_of(head, struct ctl_dir, header);
520
521 p = lookup_entry(&h, ctl_dir, name->name, name->len);
522 if (!p)
523 goto out;
524
525 if (S_ISLNK(p->mode)) {
526 ret = sysctl_follow_link(&h, &p);
527 err = ERR_PTR(ret);
528 if (ret)
529 goto out;
530 }
531
532 d_set_d_op(dentry, &proc_sys_dentry_operations);
533 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
534 err = d_splice_alias(inode, dentry);
535
536out:
537 if (h)
538 sysctl_head_finish(h);
539 sysctl_head_finish(head);
540 return err;
541}
542
543static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
544 int write)
545{
546 struct inode *inode = file_inode(iocb->ki_filp);
547 struct ctl_table_header *head = grab_header(inode);
548 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
549 size_t count = iov_iter_count(iter);
550 char *kbuf;
551 ssize_t error;
552
553 if (IS_ERR(head))
554 return PTR_ERR(head);
555
556 /*
557 * At this point we know that the sysctl was not unregistered
558 * and won't be until we finish.
559 */
560 error = -EPERM;
561 if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
562 goto out;
563
564 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
565 error = -EINVAL;
566 if (!table->proc_handler)
567 goto out;
568
569 /* don't even try if the size is too large */
570 error = -ENOMEM;
571 if (count >= KMALLOC_MAX_SIZE)
572 goto out;
573 kbuf = kvzalloc(count + 1, GFP_KERNEL);
574 if (!kbuf)
575 goto out;
576
577 if (write) {
578 error = -EFAULT;
579 if (!copy_from_iter_full(kbuf, count, iter))
580 goto out_free_buf;
581 kbuf[count] = '\0';
582 }
583
584 error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
585 &iocb->ki_pos);
586 if (error)
587 goto out_free_buf;
588
589 /* careful: calling conventions are nasty here */
590 error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
591 if (error)
592 goto out_free_buf;
593
594 if (!write) {
595 error = -EFAULT;
596 if (copy_to_iter(kbuf, count, iter) < count)
597 goto out_free_buf;
598 }
599
600 error = count;
601out_free_buf:
602 kvfree(kbuf);
603out:
604 sysctl_head_finish(head);
605
606 return error;
607}
608
609static ssize_t proc_sys_read(struct kiocb *iocb, struct iov_iter *iter)
610{
611 return proc_sys_call_handler(iocb, iter, 0);
612}
613
614static ssize_t proc_sys_write(struct kiocb *iocb, struct iov_iter *iter)
615{
616 return proc_sys_call_handler(iocb, iter, 1);
617}
618
619static int proc_sys_open(struct inode *inode, struct file *filp)
620{
621 struct ctl_table_header *head = grab_header(inode);
622 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
623
624 /* sysctl was unregistered */
625 if (IS_ERR(head))
626 return PTR_ERR(head);
627
628 if (table->poll)
629 filp->private_data = proc_sys_poll_event(table->poll);
630
631 sysctl_head_finish(head);
632
633 return 0;
634}
635
636static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
637{
638 struct inode *inode = file_inode(filp);
639 struct ctl_table_header *head = grab_header(inode);
640 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
641 __poll_t ret = DEFAULT_POLLMASK;
642 unsigned long event;
643
644 /* sysctl was unregistered */
645 if (IS_ERR(head))
646 return EPOLLERR | EPOLLHUP;
647
648 if (!table->proc_handler)
649 goto out;
650
651 if (!table->poll)
652 goto out;
653
654 event = (unsigned long)filp->private_data;
655 poll_wait(filp, &table->poll->wait, wait);
656
657 if (event != atomic_read(&table->poll->event)) {
658 filp->private_data = proc_sys_poll_event(table->poll);
659 ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
660 }
661
662out:
663 sysctl_head_finish(head);
664
665 return ret;
666}
667
668static bool proc_sys_fill_cache(struct file *file,
669 struct dir_context *ctx,
670 struct ctl_table_header *head,
671 struct ctl_table *table)
672{
673 struct dentry *child, *dir = file->f_path.dentry;
674 struct inode *inode;
675 struct qstr qname;
676 ino_t ino = 0;
677 unsigned type = DT_UNKNOWN;
678
679 qname.name = table->procname;
680 qname.len = strlen(table->procname);
681 qname.hash = full_name_hash(dir, qname.name, qname.len);
682
683 child = d_lookup(dir, &qname);
684 if (!child) {
685 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
686 child = d_alloc_parallel(dir, &qname, &wq);
687 if (IS_ERR(child))
688 return false;
689 if (d_in_lookup(child)) {
690 struct dentry *res;
691 d_set_d_op(child, &proc_sys_dentry_operations);
692 inode = proc_sys_make_inode(dir->d_sb, head, table);
693 res = d_splice_alias(inode, child);
694 d_lookup_done(child);
695 if (unlikely(res)) {
696 if (IS_ERR(res)) {
697 dput(child);
698 return false;
699 }
700 dput(child);
701 child = res;
702 }
703 }
704 }
705 inode = d_inode(child);
706 ino = inode->i_ino;
707 type = inode->i_mode >> 12;
708 dput(child);
709 return dir_emit(ctx, qname.name, qname.len, ino, type);
710}
711
712static bool proc_sys_link_fill_cache(struct file *file,
713 struct dir_context *ctx,
714 struct ctl_table_header *head,
715 struct ctl_table *table)
716{
717 bool ret = true;
718
719 head = sysctl_head_grab(head);
720 if (IS_ERR(head))
721 return false;
722
723 /* It is not an error if we can not follow the link ignore it */
724 if (sysctl_follow_link(&head, &table))
725 goto out;
726
727 ret = proc_sys_fill_cache(file, ctx, head, table);
728out:
729 sysctl_head_finish(head);
730 return ret;
731}
732
733static int scan(struct ctl_table_header *head, struct ctl_table *table,
734 unsigned long *pos, struct file *file,
735 struct dir_context *ctx)
736{
737 bool res;
738
739 if ((*pos)++ < ctx->pos)
740 return true;
741
742 if (unlikely(S_ISLNK(table->mode)))
743 res = proc_sys_link_fill_cache(file, ctx, head, table);
744 else
745 res = proc_sys_fill_cache(file, ctx, head, table);
746
747 if (res)
748 ctx->pos = *pos;
749
750 return res;
751}
752
753static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
754{
755 struct ctl_table_header *head = grab_header(file_inode(file));
756 struct ctl_table_header *h = NULL;
757 struct ctl_table *entry;
758 struct ctl_dir *ctl_dir;
759 unsigned long pos;
760
761 if (IS_ERR(head))
762 return PTR_ERR(head);
763
764 ctl_dir = container_of(head, struct ctl_dir, header);
765
766 if (!dir_emit_dots(file, ctx))
767 goto out;
768
769 pos = 2;
770
771 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
772 if (!scan(h, entry, &pos, file, ctx)) {
773 sysctl_head_finish(h);
774 break;
775 }
776 }
777out:
778 sysctl_head_finish(head);
779 return 0;
780}
781
782static int proc_sys_permission(struct mnt_idmap *idmap,
783 struct inode *inode, int mask)
784{
785 /*
786 * sysctl entries that are not writeable,
787 * are _NOT_ writeable, capabilities or not.
788 */
789 struct ctl_table_header *head;
790 struct ctl_table *table;
791 int error;
792
793 /* Executable files are not allowed under /proc/sys/ */
794 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
795 return -EACCES;
796
797 head = grab_header(inode);
798 if (IS_ERR(head))
799 return PTR_ERR(head);
800
801 table = PROC_I(inode)->sysctl_entry;
802 if (!table) /* global root - r-xr-xr-x */
803 error = mask & MAY_WRITE ? -EACCES : 0;
804 else /* Use the permissions on the sysctl table entry */
805 error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
806
807 sysctl_head_finish(head);
808 return error;
809}
810
811static int proc_sys_setattr(struct mnt_idmap *idmap,
812 struct dentry *dentry, struct iattr *attr)
813{
814 struct inode *inode = d_inode(dentry);
815 int error;
816
817 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
818 return -EPERM;
819
820 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
821 if (error)
822 return error;
823
824 setattr_copy(&nop_mnt_idmap, inode, attr);
825 return 0;
826}
827
828static int proc_sys_getattr(struct mnt_idmap *idmap,
829 const struct path *path, struct kstat *stat,
830 u32 request_mask, unsigned int query_flags)
831{
832 struct inode *inode = d_inode(path->dentry);
833 struct ctl_table_header *head = grab_header(inode);
834 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
835
836 if (IS_ERR(head))
837 return PTR_ERR(head);
838
839 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
840 if (table)
841 stat->mode = (stat->mode & S_IFMT) | table->mode;
842
843 sysctl_head_finish(head);
844 return 0;
845}
846
847static const struct file_operations proc_sys_file_operations = {
848 .open = proc_sys_open,
849 .poll = proc_sys_poll,
850 .read_iter = proc_sys_read,
851 .write_iter = proc_sys_write,
852 .splice_read = copy_splice_read,
853 .splice_write = iter_file_splice_write,
854 .llseek = default_llseek,
855};
856
857static const struct file_operations proc_sys_dir_file_operations = {
858 .read = generic_read_dir,
859 .iterate_shared = proc_sys_readdir,
860 .llseek = generic_file_llseek,
861};
862
863static const struct inode_operations proc_sys_inode_operations = {
864 .permission = proc_sys_permission,
865 .setattr = proc_sys_setattr,
866 .getattr = proc_sys_getattr,
867};
868
869static const struct inode_operations proc_sys_dir_operations = {
870 .lookup = proc_sys_lookup,
871 .permission = proc_sys_permission,
872 .setattr = proc_sys_setattr,
873 .getattr = proc_sys_getattr,
874};
875
876static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
877{
878 if (flags & LOOKUP_RCU)
879 return -ECHILD;
880 return !PROC_I(d_inode(dentry))->sysctl->unregistering;
881}
882
883static int proc_sys_delete(const struct dentry *dentry)
884{
885 return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
886}
887
888static int sysctl_is_seen(struct ctl_table_header *p)
889{
890 struct ctl_table_set *set = p->set;
891 int res;
892 spin_lock(&sysctl_lock);
893 if (p->unregistering)
894 res = 0;
895 else if (!set->is_seen)
896 res = 1;
897 else
898 res = set->is_seen(set);
899 spin_unlock(&sysctl_lock);
900 return res;
901}
902
903static int proc_sys_compare(const struct dentry *dentry,
904 unsigned int len, const char *str, const struct qstr *name)
905{
906 struct ctl_table_header *head;
907 struct inode *inode;
908
909 /* Although proc doesn't have negative dentries, rcu-walk means
910 * that inode here can be NULL */
911 /* AV: can it, indeed? */
912 inode = d_inode_rcu(dentry);
913 if (!inode)
914 return 1;
915 if (name->len != len)
916 return 1;
917 if (memcmp(name->name, str, len))
918 return 1;
919 head = rcu_dereference(PROC_I(inode)->sysctl);
920 return !head || !sysctl_is_seen(head);
921}
922
923static const struct dentry_operations proc_sys_dentry_operations = {
924 .d_revalidate = proc_sys_revalidate,
925 .d_delete = proc_sys_delete,
926 .d_compare = proc_sys_compare,
927};
928
929static struct ctl_dir *find_subdir(struct ctl_dir *dir,
930 const char *name, int namelen)
931{
932 struct ctl_table_header *head;
933 struct ctl_table *entry;
934
935 entry = find_entry(&head, dir, name, namelen);
936 if (!entry)
937 return ERR_PTR(-ENOENT);
938 if (!S_ISDIR(entry->mode))
939 return ERR_PTR(-ENOTDIR);
940 return container_of(head, struct ctl_dir, header);
941}
942
943static struct ctl_dir *new_dir(struct ctl_table_set *set,
944 const char *name, int namelen)
945{
946 struct ctl_table *table;
947 struct ctl_dir *new;
948 struct ctl_node *node;
949 char *new_name;
950
951 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
952 sizeof(struct ctl_table) + namelen + 1,
953 GFP_KERNEL);
954 if (!new)
955 return NULL;
956
957 node = (struct ctl_node *)(new + 1);
958 table = (struct ctl_table *)(node + 1);
959 new_name = (char *)(table + 1);
960 memcpy(new_name, name, namelen);
961 table[0].procname = new_name;
962 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
963 init_header(&new->header, set->dir.header.root, set, node, table, 1);
964
965 return new;
966}
967
968/**
969 * get_subdir - find or create a subdir with the specified name.
970 * @dir: Directory to create the subdirectory in
971 * @name: The name of the subdirectory to find or create
972 * @namelen: The length of name
973 *
974 * Takes a directory with an elevated reference count so we know that
975 * if we drop the lock the directory will not go away. Upon success
976 * the reference is moved from @dir to the returned subdirectory.
977 * Upon error an error code is returned and the reference on @dir is
978 * simply dropped.
979 */
980static struct ctl_dir *get_subdir(struct ctl_dir *dir,
981 const char *name, int namelen)
982{
983 struct ctl_table_set *set = dir->header.set;
984 struct ctl_dir *subdir, *new = NULL;
985 int err;
986
987 spin_lock(&sysctl_lock);
988 subdir = find_subdir(dir, name, namelen);
989 if (!IS_ERR(subdir))
990 goto found;
991 if (PTR_ERR(subdir) != -ENOENT)
992 goto failed;
993
994 spin_unlock(&sysctl_lock);
995 new = new_dir(set, name, namelen);
996 spin_lock(&sysctl_lock);
997 subdir = ERR_PTR(-ENOMEM);
998 if (!new)
999 goto failed;
1000
1001 /* Was the subdir added while we dropped the 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 /* Nope. Use the our freshly made directory entry. */
1009 err = insert_header(dir, &new->header);
1010 subdir = ERR_PTR(err);
1011 if (err)
1012 goto failed;
1013 subdir = new;
1014found:
1015 subdir->header.nreg++;
1016failed:
1017 if (IS_ERR(subdir)) {
1018 pr_err("sysctl could not get directory: ");
1019 sysctl_print_dir(dir);
1020 pr_cont("%*.*s %ld\n", namelen, namelen, name,
1021 PTR_ERR(subdir));
1022 }
1023 drop_sysctl_table(&dir->header);
1024 if (new)
1025 drop_sysctl_table(&new->header);
1026 spin_unlock(&sysctl_lock);
1027 return subdir;
1028}
1029
1030static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
1031{
1032 struct ctl_dir *parent;
1033 const char *procname;
1034 if (!dir->header.parent)
1035 return &set->dir;
1036 parent = xlate_dir(set, dir->header.parent);
1037 if (IS_ERR(parent))
1038 return parent;
1039 procname = dir->header.ctl_table[0].procname;
1040 return find_subdir(parent, procname, strlen(procname));
1041}
1042
1043static int sysctl_follow_link(struct ctl_table_header **phead,
1044 struct ctl_table **pentry)
1045{
1046 struct ctl_table_header *head;
1047 struct ctl_table_root *root;
1048 struct ctl_table_set *set;
1049 struct ctl_table *entry;
1050 struct ctl_dir *dir;
1051 int ret;
1052
1053 spin_lock(&sysctl_lock);
1054 root = (*pentry)->data;
1055 set = lookup_header_set(root);
1056 dir = xlate_dir(set, (*phead)->parent);
1057 if (IS_ERR(dir))
1058 ret = PTR_ERR(dir);
1059 else {
1060 const char *procname = (*pentry)->procname;
1061 head = NULL;
1062 entry = find_entry(&head, dir, procname, strlen(procname));
1063 ret = -ENOENT;
1064 if (entry && use_table(head)) {
1065 unuse_table(*phead);
1066 *phead = head;
1067 *pentry = entry;
1068 ret = 0;
1069 }
1070 }
1071
1072 spin_unlock(&sysctl_lock);
1073 return ret;
1074}
1075
1076static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1077{
1078 struct va_format vaf;
1079 va_list args;
1080
1081 va_start(args, fmt);
1082 vaf.fmt = fmt;
1083 vaf.va = &args;
1084
1085 pr_err("sysctl table check failed: %s/%s %pV\n",
1086 path, table->procname, &vaf);
1087
1088 va_end(args);
1089 return -EINVAL;
1090}
1091
1092static int sysctl_check_table_array(const char *path, struct ctl_table *table)
1093{
1094 unsigned int extra;
1095 int err = 0;
1096
1097 if ((table->proc_handler == proc_douintvec) ||
1098 (table->proc_handler == proc_douintvec_minmax)) {
1099 if (table->maxlen != sizeof(unsigned int))
1100 err |= sysctl_err(path, table, "array not allowed");
1101 }
1102
1103 if (table->proc_handler == proc_dou8vec_minmax) {
1104 if (table->maxlen != sizeof(u8))
1105 err |= sysctl_err(path, table, "array not allowed");
1106
1107 if (table->extra1) {
1108 extra = *(unsigned int *) table->extra1;
1109 if (extra > 255U)
1110 err |= sysctl_err(path, table,
1111 "range value too large for proc_dou8vec_minmax");
1112 }
1113 if (table->extra2) {
1114 extra = *(unsigned int *) table->extra2;
1115 if (extra > 255U)
1116 err |= sysctl_err(path, table,
1117 "range value too large for proc_dou8vec_minmax");
1118 }
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
1129static 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->procname)
1135 err |= sysctl_err(path, entry, "procname is null");
1136 if ((entry->proc_handler == proc_dostring) ||
1137 (entry->proc_handler == proc_dobool) ||
1138 (entry->proc_handler == proc_dointvec) ||
1139 (entry->proc_handler == proc_douintvec) ||
1140 (entry->proc_handler == proc_douintvec_minmax) ||
1141 (entry->proc_handler == proc_dointvec_minmax) ||
1142 (entry->proc_handler == proc_dou8vec_minmax) ||
1143 (entry->proc_handler == proc_dointvec_jiffies) ||
1144 (entry->proc_handler == proc_dointvec_userhz_jiffies) ||
1145 (entry->proc_handler == proc_dointvec_ms_jiffies) ||
1146 (entry->proc_handler == proc_doulongvec_minmax) ||
1147 (entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1148 if (!entry->data)
1149 err |= sysctl_err(path, entry, "No data");
1150 if (!entry->maxlen)
1151 err |= sysctl_err(path, entry, "No maxlen");
1152 else
1153 err |= sysctl_check_table_array(path, entry);
1154 }
1155 if (!entry->proc_handler)
1156 err |= sysctl_err(path, entry, "No proc_handler");
1157
1158 if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
1159 err |= sysctl_err(path, entry, "bogus .mode 0%o",
1160 entry->mode);
1161 }
1162 return err;
1163}
1164
1165static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table_header *head)
1166{
1167 struct ctl_table *link_table, *entry, *link;
1168 struct ctl_table_header *links;
1169 struct ctl_node *node;
1170 char *link_name;
1171 int name_bytes;
1172
1173 name_bytes = 0;
1174 list_for_each_table_entry(entry, head) {
1175 name_bytes += strlen(entry->procname) + 1;
1176 }
1177
1178 links = kzalloc(sizeof(struct ctl_table_header) +
1179 sizeof(struct ctl_node)*head->ctl_table_size +
1180 sizeof(struct ctl_table)*head->ctl_table_size +
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 + head->ctl_table_size);
1189 link_name = (char *)(link_table + head->ctl_table_size);
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 = head->ctl_table_size;
1204
1205 return links;
1206}
1207
1208static 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_header(header))
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
1241static 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);
1277out:
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. */
1283static 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 *
1316 * @table: the top-level table structure. This table should not be free'd
1317 * after registration. So it should not be used on stack. It can either
1318 * be a global or dynamically allocated by the caller and free'd later
1319 * after sysctl unregistration.
1320 * @table_size : The number of elements in table
1321 *
1322 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1323 * array.
1324 *
1325 * The members of the &struct ctl_table structure are used as follows:
1326 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1327 * enter a sysctl file
1328 * data - a pointer to data for use by proc_handler
1329 * maxlen - the maximum size in bytes of the data
1330 * mode - the file permissions for the /proc/sys file
1331 * type - Defines the target type (described in struct definition)
1332 * proc_handler - the text handler routine (described below)
1333 *
1334 * extra1, extra2 - extra pointers usable by the proc handler routines
1335 * XXX: we should eventually modify these to use long min / max [0]
1336 * [0] https://lkml.kernel.org/87zgpte9o4.fsf@email.froward.int.ebiederm.org
1337 *
1338 * Leaf nodes in the sysctl tree will be represented by a single file
1339 * under /proc; non-leaf nodes are not allowed.
1340 *
1341 * There must be a proc_handler routine for any terminal nodes.
1342 * Several default handlers are available to cover common cases -
1343 *
1344 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1345 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1346 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1347 *
1348 * It is the handler's job to read the input buffer from user memory
1349 * and process it. The handler should return 0 on success.
1350 *
1351 * This routine returns %NULL on a failure to register, and a pointer
1352 * to the table header on success.
1353 */
1354struct ctl_table_header *__register_sysctl_table(
1355 struct ctl_table_set *set,
1356 const char *path, struct ctl_table *table, size_t table_size)
1357{
1358 struct ctl_table_root *root = set->dir.header.root;
1359 struct ctl_table_header *header;
1360 struct ctl_dir *dir;
1361 struct ctl_node *node;
1362
1363 header = kzalloc(sizeof(struct ctl_table_header) +
1364 sizeof(struct ctl_node)*table_size, GFP_KERNEL_ACCOUNT);
1365 if (!header)
1366 return NULL;
1367
1368 node = (struct ctl_node *)(header + 1);
1369 init_header(header, root, set, node, table, table_size);
1370 if (sysctl_check_table(path, header))
1371 goto fail;
1372
1373 spin_lock(&sysctl_lock);
1374 dir = &set->dir;
1375 /* Reference moved down the directory tree get_subdir */
1376 dir->header.nreg++;
1377 spin_unlock(&sysctl_lock);
1378
1379 dir = sysctl_mkdir_p(dir, path);
1380 if (IS_ERR(dir))
1381 goto fail;
1382 spin_lock(&sysctl_lock);
1383 if (insert_header(dir, header))
1384 goto fail_put_dir_locked;
1385
1386 drop_sysctl_table(&dir->header);
1387 spin_unlock(&sysctl_lock);
1388
1389 return header;
1390
1391fail_put_dir_locked:
1392 drop_sysctl_table(&dir->header);
1393 spin_unlock(&sysctl_lock);
1394fail:
1395 kfree(header);
1396 return NULL;
1397}
1398
1399/**
1400 * register_sysctl_sz - register a sysctl table
1401 * @path: The path to the directory the sysctl table is in. If the path
1402 * doesn't exist we will create it for you.
1403 * @table: the table structure. The calller must ensure the life of the @table
1404 * will be kept during the lifetime use of the syctl. It must not be freed
1405 * until unregister_sysctl_table() is called with the given returned table
1406 * with this registration. If your code is non modular then you don't need
1407 * to call unregister_sysctl_table() and can instead use something like
1408 * register_sysctl_init() which does not care for the result of the syctl
1409 * registration.
1410 * @table_size: The number of elements in table.
1411 *
1412 * Register a sysctl table. @table should be a filled in ctl_table
1413 * array. A completely 0 filled entry terminates the table.
1414 *
1415 * See __register_sysctl_table for more details.
1416 */
1417struct ctl_table_header *register_sysctl_sz(const char *path, struct ctl_table *table,
1418 size_t table_size)
1419{
1420 return __register_sysctl_table(&sysctl_table_root.default_set,
1421 path, table, table_size);
1422}
1423EXPORT_SYMBOL(register_sysctl_sz);
1424
1425/**
1426 * __register_sysctl_init() - register sysctl table to path
1427 * @path: path name for sysctl base. If that path doesn't exist we will create
1428 * it for you.
1429 * @table: This is the sysctl table that needs to be registered to the path.
1430 * The caller must ensure the life of the @table will be kept during the
1431 * lifetime use of the sysctl.
1432 * @table_name: The name of sysctl table, only used for log printing when
1433 * registration fails
1434 * @table_size: The number of elements in table
1435 *
1436 * The sysctl interface is used by userspace to query or modify at runtime
1437 * a predefined value set on a variable. These variables however have default
1438 * values pre-set. Code which depends on these variables will always work even
1439 * if register_sysctl() fails. If register_sysctl() fails you'd just loose the
1440 * ability to query or modify the sysctls dynamically at run time. Chances of
1441 * register_sysctl() failing on init are extremely low, and so for both reasons
1442 * this function does not return any error as it is used by initialization code.
1443 *
1444 * Context: if your base directory does not exist it will be created for you.
1445 */
1446void __init __register_sysctl_init(const char *path, struct ctl_table *table,
1447 const char *table_name, size_t table_size)
1448{
1449 struct ctl_table_header *hdr = register_sysctl_sz(path, table, table_size);
1450
1451 if (unlikely(!hdr)) {
1452 pr_err("failed when register_sysctl_sz %s to %s\n", table_name, path);
1453 return;
1454 }
1455 kmemleak_not_leak(hdr);
1456}
1457
1458static void put_links(struct ctl_table_header *header)
1459{
1460 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1461 struct ctl_table_root *root = header->root;
1462 struct ctl_dir *parent = header->parent;
1463 struct ctl_dir *core_parent;
1464 struct ctl_table *entry;
1465
1466 if (header->set == root_set)
1467 return;
1468
1469 core_parent = xlate_dir(root_set, parent);
1470 if (IS_ERR(core_parent))
1471 return;
1472
1473 list_for_each_table_entry(entry, header) {
1474 struct ctl_table_header *link_head;
1475 struct ctl_table *link;
1476 const char *name = entry->procname;
1477
1478 link = find_entry(&link_head, core_parent, name, strlen(name));
1479 if (link &&
1480 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1481 (S_ISLNK(link->mode) && (link->data == root)))) {
1482 drop_sysctl_table(link_head);
1483 }
1484 else {
1485 pr_err("sysctl link missing during unregister: ");
1486 sysctl_print_dir(parent);
1487 pr_cont("%s\n", name);
1488 }
1489 }
1490}
1491
1492static void drop_sysctl_table(struct ctl_table_header *header)
1493{
1494 struct ctl_dir *parent = header->parent;
1495
1496 if (--header->nreg)
1497 return;
1498
1499 if (parent) {
1500 put_links(header);
1501 start_unregistering(header);
1502 }
1503
1504 if (!--header->count)
1505 kfree_rcu(header, rcu);
1506
1507 if (parent)
1508 drop_sysctl_table(&parent->header);
1509}
1510
1511/**
1512 * unregister_sysctl_table - unregister a sysctl table hierarchy
1513 * @header: the header returned from register_sysctl or __register_sysctl_table
1514 *
1515 * Unregisters the sysctl table and all children. proc entries may not
1516 * actually be removed until they are no longer used by anyone.
1517 */
1518void unregister_sysctl_table(struct ctl_table_header * header)
1519{
1520 might_sleep();
1521
1522 if (header == NULL)
1523 return;
1524
1525 spin_lock(&sysctl_lock);
1526 drop_sysctl_table(header);
1527 spin_unlock(&sysctl_lock);
1528}
1529EXPORT_SYMBOL(unregister_sysctl_table);
1530
1531void setup_sysctl_set(struct ctl_table_set *set,
1532 struct ctl_table_root *root,
1533 int (*is_seen)(struct ctl_table_set *))
1534{
1535 memset(set, 0, sizeof(*set));
1536 set->is_seen = is_seen;
1537 init_header(&set->dir.header, root, set, NULL, root_table, 1);
1538}
1539
1540void retire_sysctl_set(struct ctl_table_set *set)
1541{
1542 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
1543}
1544
1545int __init proc_sys_init(void)
1546{
1547 struct proc_dir_entry *proc_sys_root;
1548
1549 proc_sys_root = proc_mkdir("sys", NULL);
1550 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1551 proc_sys_root->proc_dir_ops = &proc_sys_dir_file_operations;
1552 proc_sys_root->nlink = 0;
1553
1554 return sysctl_init_bases();
1555}
1556
1557struct sysctl_alias {
1558 const char *kernel_param;
1559 const char *sysctl_param;
1560};
1561
1562/*
1563 * Historically some settings had both sysctl and a command line parameter.
1564 * With the generic sysctl. parameter support, we can handle them at a single
1565 * place and only keep the historical name for compatibility. This is not meant
1566 * to add brand new aliases. When adding existing aliases, consider whether
1567 * the possibly different moment of changing the value (e.g. from early_param
1568 * to the moment do_sysctl_args() is called) is an issue for the specific
1569 * parameter.
1570 */
1571static const struct sysctl_alias sysctl_aliases[] = {
1572 {"hardlockup_all_cpu_backtrace", "kernel.hardlockup_all_cpu_backtrace" },
1573 {"hung_task_panic", "kernel.hung_task_panic" },
1574 {"numa_zonelist_order", "vm.numa_zonelist_order" },
1575 {"softlockup_all_cpu_backtrace", "kernel.softlockup_all_cpu_backtrace" },
1576 { }
1577};
1578
1579static const char *sysctl_find_alias(char *param)
1580{
1581 const struct sysctl_alias *alias;
1582
1583 for (alias = &sysctl_aliases[0]; alias->kernel_param != NULL; alias++) {
1584 if (strcmp(alias->kernel_param, param) == 0)
1585 return alias->sysctl_param;
1586 }
1587
1588 return NULL;
1589}
1590
1591bool sysctl_is_alias(char *param)
1592{
1593 const char *alias = sysctl_find_alias(param);
1594
1595 return alias != NULL;
1596}
1597
1598/* Set sysctl value passed on kernel command line. */
1599static int process_sysctl_arg(char *param, char *val,
1600 const char *unused, void *arg)
1601{
1602 char *path;
1603 struct vfsmount **proc_mnt = arg;
1604 struct file_system_type *proc_fs_type;
1605 struct file *file;
1606 int len;
1607 int err;
1608 loff_t pos = 0;
1609 ssize_t wret;
1610
1611 if (strncmp(param, "sysctl", sizeof("sysctl") - 1) == 0) {
1612 param += sizeof("sysctl") - 1;
1613
1614 if (param[0] != '/' && param[0] != '.')
1615 return 0;
1616
1617 param++;
1618 } else {
1619 param = (char *) sysctl_find_alias(param);
1620 if (!param)
1621 return 0;
1622 }
1623
1624 if (!val)
1625 return -EINVAL;
1626 len = strlen(val);
1627 if (len == 0)
1628 return -EINVAL;
1629
1630 /*
1631 * To set sysctl options, we use a temporary mount of proc, look up the
1632 * respective sys/ file and write to it. To avoid mounting it when no
1633 * options were given, we mount it only when the first sysctl option is
1634 * found. Why not a persistent mount? There are problems with a
1635 * persistent mount of proc in that it forces userspace not to use any
1636 * proc mount options.
1637 */
1638 if (!*proc_mnt) {
1639 proc_fs_type = get_fs_type("proc");
1640 if (!proc_fs_type) {
1641 pr_err("Failed to find procfs to set sysctl from command line\n");
1642 return 0;
1643 }
1644 *proc_mnt = kern_mount(proc_fs_type);
1645 put_filesystem(proc_fs_type);
1646 if (IS_ERR(*proc_mnt)) {
1647 pr_err("Failed to mount procfs to set sysctl from command line\n");
1648 return 0;
1649 }
1650 }
1651
1652 path = kasprintf(GFP_KERNEL, "sys/%s", param);
1653 if (!path)
1654 panic("%s: Failed to allocate path for %s\n", __func__, param);
1655 strreplace(path, '.', '/');
1656
1657 file = file_open_root_mnt(*proc_mnt, path, O_WRONLY, 0);
1658 if (IS_ERR(file)) {
1659 err = PTR_ERR(file);
1660 if (err == -ENOENT)
1661 pr_err("Failed to set sysctl parameter '%s=%s': parameter not found\n",
1662 param, val);
1663 else if (err == -EACCES)
1664 pr_err("Failed to set sysctl parameter '%s=%s': permission denied (read-only?)\n",
1665 param, val);
1666 else
1667 pr_err("Error %pe opening proc file to set sysctl parameter '%s=%s'\n",
1668 file, param, val);
1669 goto out;
1670 }
1671 wret = kernel_write(file, val, len, &pos);
1672 if (wret < 0) {
1673 err = wret;
1674 if (err == -EINVAL)
1675 pr_err("Failed to set sysctl parameter '%s=%s': invalid value\n",
1676 param, val);
1677 else
1678 pr_err("Error %pe writing to proc file to set sysctl parameter '%s=%s'\n",
1679 ERR_PTR(err), param, val);
1680 } else if (wret != len) {
1681 pr_err("Wrote only %zd bytes of %d writing to proc file %s to set sysctl parameter '%s=%s\n",
1682 wret, len, path, param, val);
1683 }
1684
1685 err = filp_close(file, NULL);
1686 if (err)
1687 pr_err("Error %pe closing proc file to set sysctl parameter '%s=%s\n",
1688 ERR_PTR(err), param, val);
1689out:
1690 kfree(path);
1691 return 0;
1692}
1693
1694void do_sysctl_args(void)
1695{
1696 char *command_line;
1697 struct vfsmount *proc_mnt = NULL;
1698
1699 command_line = kstrdup(saved_command_line, GFP_KERNEL);
1700 if (!command_line)
1701 panic("%s: Failed to allocate copy of command line\n", __func__);
1702
1703 parse_args("Setting sysctl args", command_line,
1704 NULL, 0, -1, -1, &proc_mnt, process_sysctl_arg);
1705
1706 if (proc_mnt)
1707 kern_unmount(proc_mnt);
1708
1709 kfree(command_line);
1710}