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