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