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