sysctl: Improve the sysctl sanity checks
[deliverable/linux.git] / fs / proc / proc_sysctl.c
CommitLineData
77b14db5
EB
1/*
2 * /proc/sys support
3 */
1e0edd3f 4#include <linux/init.h>
77b14db5 5#include <linux/sysctl.h>
f1ecf068 6#include <linux/poll.h>
77b14db5
EB
7#include <linux/proc_fs.h>
8#include <linux/security.h>
34286d66 9#include <linux/namei.h>
1f87f0b5 10#include <linux/module.h>
77b14db5
EB
11#include "internal.h"
12
d72f71eb 13static const struct dentry_operations proc_sys_dentry_operations;
77b14db5 14static const struct file_operations proc_sys_file_operations;
03a44825 15static const struct inode_operations proc_sys_inode_operations;
9043476f
AV
16static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
77b14db5 18
f1ecf068
LDM
19void proc_sys_poll_notify(struct ctl_table_poll *poll)
20{
21 if (!poll)
22 return;
23
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
26}
27
1f87f0b5
EB
28static struct ctl_table root_table[1];
29static struct ctl_table_root sysctl_table_root;
30static struct ctl_table_header root_table_header = {
31 {{.count = 1,
32 .ctl_table = root_table,
33 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
34 .root = &sysctl_table_root,
35 .set = &sysctl_table_root.default_set,
36};
37static struct ctl_table_root sysctl_table_root = {
38 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
39 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
40};
41
42static DEFINE_SPINLOCK(sysctl_lock);
43
44/* called under sysctl_lock */
45static int use_table(struct ctl_table_header *p)
46{
47 if (unlikely(p->unregistering))
48 return 0;
49 p->used++;
50 return 1;
51}
52
53/* called under sysctl_lock */
54static void unuse_table(struct ctl_table_header *p)
55{
56 if (!--p->used)
57 if (unlikely(p->unregistering))
58 complete(p->unregistering);
59}
60
61/* called under sysctl_lock, will reacquire if has to wait */
62static void start_unregistering(struct ctl_table_header *p)
63{
64 /*
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
67 */
68 if (unlikely(p->used)) {
69 struct completion wait;
70 init_completion(&wait);
71 p->unregistering = &wait;
72 spin_unlock(&sysctl_lock);
73 wait_for_completion(&wait);
74 spin_lock(&sysctl_lock);
75 } else {
76 /* anything non-NULL; we'll never dereference it */
77 p->unregistering = ERR_PTR(-EINVAL);
78 }
79 /*
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
82 */
83 list_del_init(&p->ctl_entry);
84}
85
86static void sysctl_head_get(struct ctl_table_header *head)
87{
88 spin_lock(&sysctl_lock);
89 head->count++;
90 spin_unlock(&sysctl_lock);
91}
92
93void sysctl_head_put(struct ctl_table_header *head)
94{
95 spin_lock(&sysctl_lock);
96 if (!--head->count)
97 kfree_rcu(head, rcu);
98 spin_unlock(&sysctl_lock);
99}
100
101static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
102{
103 if (!head)
104 BUG();
105 spin_lock(&sysctl_lock);
106 if (!use_table(head))
107 head = ERR_PTR(-ENOENT);
108 spin_unlock(&sysctl_lock);
109 return head;
110}
111
112static void sysctl_head_finish(struct ctl_table_header *head)
113{
114 if (!head)
115 return;
116 spin_lock(&sysctl_lock);
117 unuse_table(head);
118 spin_unlock(&sysctl_lock);
119}
120
121static struct ctl_table_set *
122lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
123{
124 struct ctl_table_set *set = &root->default_set;
125 if (root->lookup)
126 set = root->lookup(root, namespaces);
127 return set;
128}
129
130static struct list_head *
131lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
132{
133 struct ctl_table_set *set = lookup_header_set(root, namespaces);
134 return &set->list;
135}
136
137static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
138 struct ctl_table_header *prev)
139{
140 struct ctl_table_root *root;
141 struct list_head *header_list;
142 struct ctl_table_header *head;
143 struct list_head *tmp;
144
145 spin_lock(&sysctl_lock);
146 if (prev) {
147 head = prev;
148 tmp = &prev->ctl_entry;
149 unuse_table(prev);
150 goto next;
151 }
152 tmp = &root_table_header.ctl_entry;
153 for (;;) {
154 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
155
156 if (!use_table(head))
157 goto next;
158 spin_unlock(&sysctl_lock);
159 return head;
160 next:
161 root = head->root;
162 tmp = tmp->next;
163 header_list = lookup_header_list(root, namespaces);
164 if (tmp != header_list)
165 continue;
166
167 do {
168 root = list_entry(root->root_list.next,
169 struct ctl_table_root, root_list);
170 if (root == &sysctl_table_root)
171 goto out;
172 header_list = lookup_header_list(root, namespaces);
173 } while (list_empty(header_list));
174 tmp = header_list->next;
175 }
176out:
177 spin_unlock(&sysctl_lock);
178 return NULL;
179}
180
181static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
182{
183 return __sysctl_head_next(current->nsproxy, prev);
184}
185
186void register_sysctl_root(struct ctl_table_root *root)
187{
188 spin_lock(&sysctl_lock);
189 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
190 spin_unlock(&sysctl_lock);
191}
192
193/*
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
196 */
197
198static int test_perm(int mode, int op)
199{
200 if (!current_euid())
201 mode >>= 6;
202 else if (in_egroup_p(0))
203 mode >>= 3;
204 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
205 return 0;
206 return -EACCES;
207}
208
209static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
210{
211 int mode;
212
213 if (root->permissions)
214 mode = root->permissions(root, current->nsproxy, table);
215 else
216 mode = table->mode;
217
218 return test_perm(mode, op);
219}
220
221static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
222{
223 for (; table->procname; table++) {
224 table->parent = parent;
225 if (table->child)
226 sysctl_set_parent(table, table->child);
227 }
228}
229
230
9043476f
AV
231static struct inode *proc_sys_make_inode(struct super_block *sb,
232 struct ctl_table_header *head, struct ctl_table *table)
77b14db5
EB
233{
234 struct inode *inode;
9043476f 235 struct proc_inode *ei;
77b14db5 236
9043476f 237 inode = new_inode(sb);
77b14db5
EB
238 if (!inode)
239 goto out;
240
85fe4025
CH
241 inode->i_ino = get_next_ino();
242
9043476f 243 sysctl_head_get(head);
77b14db5 244 ei = PROC_I(inode);
9043476f
AV
245 ei->sysctl = head;
246 ei->sysctl_entry = table;
247
77b14db5 248 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
9043476f
AV
249 inode->i_mode = table->mode;
250 if (!table->child) {
251 inode->i_mode |= S_IFREG;
252 inode->i_op = &proc_sys_inode_operations;
253 inode->i_fop = &proc_sys_file_operations;
254 } else {
255 inode->i_mode |= S_IFDIR;
9043476f
AV
256 inode->i_op = &proc_sys_dir_operations;
257 inode->i_fop = &proc_sys_dir_file_operations;
258 }
77b14db5
EB
259out:
260 return inode;
261}
262
9043476f 263static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
77b14db5 264{
2315ffa0 265 for ( ; p->procname; p++) {
36885d7b 266 if (strlen(p->procname) != name->len)
77b14db5
EB
267 continue;
268
36885d7b 269 if (memcmp(p->procname, name->name, name->len) != 0)
77b14db5
EB
270 continue;
271
272 /* I have a match */
9043476f 273 return p;
77b14db5
EB
274 }
275 return NULL;
276}
277
81324364 278static struct ctl_table_header *grab_header(struct inode *inode)
77b14db5 279{
9043476f
AV
280 if (PROC_I(inode)->sysctl)
281 return sysctl_head_grab(PROC_I(inode)->sysctl);
282 else
283 return sysctl_head_next(NULL);
284}
77b14db5 285
9043476f
AV
286static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
287 struct nameidata *nd)
288{
289 struct ctl_table_header *head = grab_header(dir);
290 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
291 struct ctl_table_header *h = NULL;
292 struct qstr *name = &dentry->d_name;
293 struct ctl_table *p;
294 struct inode *inode;
295 struct dentry *err = ERR_PTR(-ENOENT);
77b14db5 296
9043476f
AV
297 if (IS_ERR(head))
298 return ERR_CAST(head);
77b14db5 299
9043476f
AV
300 if (table && !table->child) {
301 WARN_ON(1);
302 goto out;
77b14db5 303 }
77b14db5 304
9043476f 305 table = table ? table->child : head->ctl_table;
77b14db5 306
9043476f
AV
307 p = find_in_table(table, name);
308 if (!p) {
309 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
310 if (h->attached_to != table)
311 continue;
312 p = find_in_table(h->attached_by, name);
313 if (p)
314 break;
315 }
77b14db5 316 }
77b14db5 317
9043476f 318 if (!p)
77b14db5
EB
319 goto out;
320
321 err = ERR_PTR(-ENOMEM);
9043476f
AV
322 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
323 if (h)
324 sysctl_head_finish(h);
325
77b14db5
EB
326 if (!inode)
327 goto out;
328
329 err = NULL;
fb045adb 330 d_set_d_op(dentry, &proc_sys_dentry_operations);
77b14db5
EB
331 d_add(dentry, inode);
332
333out:
334 sysctl_head_finish(head);
335 return err;
336}
337
7708bfb1
PE
338static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
339 size_t count, loff_t *ppos, int write)
77b14db5 340{
9043476f
AV
341 struct inode *inode = filp->f_path.dentry->d_inode;
342 struct ctl_table_header *head = grab_header(inode);
343 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2a2da53b
DH
344 ssize_t error;
345 size_t res;
77b14db5 346
9043476f
AV
347 if (IS_ERR(head))
348 return PTR_ERR(head);
77b14db5
EB
349
350 /*
351 * At this point we know that the sysctl was not unregistered
352 * and won't be until we finish.
353 */
354 error = -EPERM;
d7321cd6 355 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
77b14db5
EB
356 goto out;
357
9043476f
AV
358 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
359 error = -EINVAL;
360 if (!table->proc_handler)
361 goto out;
362
77b14db5
EB
363 /* careful: calling conventions are nasty here */
364 res = count;
8d65af78 365 error = table->proc_handler(table, write, buf, &res, ppos);
77b14db5
EB
366 if (!error)
367 error = res;
368out:
369 sysctl_head_finish(head);
370
371 return error;
372}
373
7708bfb1 374static ssize_t proc_sys_read(struct file *filp, char __user *buf,
77b14db5
EB
375 size_t count, loff_t *ppos)
376{
7708bfb1
PE
377 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
378}
77b14db5 379
7708bfb1
PE
380static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
381 size_t count, loff_t *ppos)
382{
383 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
77b14db5
EB
384}
385
f1ecf068
LDM
386static int proc_sys_open(struct inode *inode, struct file *filp)
387{
388 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
389
390 if (table->poll)
391 filp->private_data = proc_sys_poll_event(table->poll);
392
393 return 0;
394}
395
396static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
397{
398 struct inode *inode = filp->f_path.dentry->d_inode;
399 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
400 unsigned long event = (unsigned long)filp->private_data;
401 unsigned int ret = DEFAULT_POLLMASK;
402
403 if (!table->proc_handler)
404 goto out;
405
406 if (!table->poll)
407 goto out;
408
409 poll_wait(filp, &table->poll->wait, wait);
410
411 if (event != atomic_read(&table->poll->event)) {
412 filp->private_data = proc_sys_poll_event(table->poll);
413 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
414 }
415
416out:
417 return ret;
418}
77b14db5
EB
419
420static int proc_sys_fill_cache(struct file *filp, void *dirent,
9043476f
AV
421 filldir_t filldir,
422 struct ctl_table_header *head,
423 struct ctl_table *table)
77b14db5 424{
77b14db5
EB
425 struct dentry *child, *dir = filp->f_path.dentry;
426 struct inode *inode;
427 struct qstr qname;
428 ino_t ino = 0;
429 unsigned type = DT_UNKNOWN;
77b14db5
EB
430
431 qname.name = table->procname;
432 qname.len = strlen(table->procname);
433 qname.hash = full_name_hash(qname.name, qname.len);
434
77b14db5
EB
435 child = d_lookup(dir, &qname);
436 if (!child) {
9043476f
AV
437 child = d_alloc(dir, &qname);
438 if (child) {
439 inode = proc_sys_make_inode(dir->d_sb, head, table);
440 if (!inode) {
441 dput(child);
442 return -ENOMEM;
443 } else {
fb045adb 444 d_set_d_op(child, &proc_sys_dentry_operations);
9043476f 445 d_add(child, inode);
77b14db5 446 }
9043476f
AV
447 } else {
448 return -ENOMEM;
77b14db5
EB
449 }
450 }
77b14db5 451 inode = child->d_inode;
9043476f
AV
452 ino = inode->i_ino;
453 type = inode->i_mode >> 12;
77b14db5 454 dput(child);
9043476f
AV
455 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
456}
457
458static int scan(struct ctl_table_header *head, ctl_table *table,
459 unsigned long *pos, struct file *file,
460 void *dirent, filldir_t filldir)
461{
462
2315ffa0 463 for (; table->procname; table++, (*pos)++) {
9043476f
AV
464 int res;
465
9043476f
AV
466 if (*pos < file->f_pos)
467 continue;
468
469 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
470 if (res)
471 return res;
472
473 file->f_pos = *pos + 1;
474 }
475 return 0;
77b14db5
EB
476}
477
478static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
479{
9043476f 480 struct dentry *dentry = filp->f_path.dentry;
77b14db5 481 struct inode *inode = dentry->d_inode;
9043476f
AV
482 struct ctl_table_header *head = grab_header(inode);
483 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
484 struct ctl_table_header *h = NULL;
77b14db5 485 unsigned long pos;
9043476f
AV
486 int ret = -EINVAL;
487
488 if (IS_ERR(head))
489 return PTR_ERR(head);
77b14db5 490
9043476f
AV
491 if (table && !table->child) {
492 WARN_ON(1);
77b14db5 493 goto out;
9043476f
AV
494 }
495
496 table = table ? table->child : head->ctl_table;
77b14db5
EB
497
498 ret = 0;
499 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
500 if (filp->f_pos == 0) {
501 if (filldir(dirent, ".", 1, filp->f_pos,
502 inode->i_ino, DT_DIR) < 0)
503 goto out;
504 filp->f_pos++;
505 }
506 if (filp->f_pos == 1) {
507 if (filldir(dirent, "..", 2, filp->f_pos,
508 parent_ino(dentry), DT_DIR) < 0)
509 goto out;
510 filp->f_pos++;
511 }
512 pos = 2;
513
9043476f
AV
514 ret = scan(head, table, &pos, filp, dirent, filldir);
515 if (ret)
516 goto out;
77b14db5 517
9043476f
AV
518 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
519 if (h->attached_to != table)
77b14db5 520 continue;
9043476f
AV
521 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
522 if (ret) {
523 sysctl_head_finish(h);
524 break;
77b14db5
EB
525 }
526 }
527 ret = 1;
528out:
529 sysctl_head_finish(head);
530 return ret;
531}
532
10556cb2 533static int proc_sys_permission(struct inode *inode, int mask)
77b14db5
EB
534{
535 /*
536 * sysctl entries that are not writeable,
537 * are _NOT_ writeable, capabilities or not.
538 */
f696a365
MS
539 struct ctl_table_header *head;
540 struct ctl_table *table;
77b14db5
EB
541 int error;
542
f696a365
MS
543 /* Executable files are not allowed under /proc/sys/ */
544 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
545 return -EACCES;
546
547 head = grab_header(inode);
9043476f
AV
548 if (IS_ERR(head))
549 return PTR_ERR(head);
77b14db5 550
f696a365 551 table = PROC_I(inode)->sysctl_entry;
9043476f
AV
552 if (!table) /* global root - r-xr-xr-x */
553 error = mask & MAY_WRITE ? -EACCES : 0;
554 else /* Use the permissions on the sysctl table entry */
1fc0f78c 555 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
77b14db5 556
77b14db5
EB
557 sysctl_head_finish(head);
558 return error;
559}
560
561static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
562{
563 struct inode *inode = dentry->d_inode;
564 int error;
565
566 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
567 return -EPERM;
568
569 error = inode_change_ok(inode, attr);
1025774c
CH
570 if (error)
571 return error;
572
573 if ((attr->ia_valid & ATTR_SIZE) &&
574 attr->ia_size != i_size_read(inode)) {
575 error = vmtruncate(inode, attr->ia_size);
576 if (error)
577 return error;
578 }
77b14db5 579
1025774c
CH
580 setattr_copy(inode, attr);
581 mark_inode_dirty(inode);
582 return 0;
77b14db5
EB
583}
584
9043476f
AV
585static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
586{
587 struct inode *inode = dentry->d_inode;
588 struct ctl_table_header *head = grab_header(inode);
589 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
590
591 if (IS_ERR(head))
592 return PTR_ERR(head);
593
594 generic_fillattr(inode, stat);
595 if (table)
596 stat->mode = (stat->mode & S_IFMT) | table->mode;
597
598 sysctl_head_finish(head);
599 return 0;
600}
601
77b14db5 602static const struct file_operations proc_sys_file_operations = {
f1ecf068
LDM
603 .open = proc_sys_open,
604 .poll = proc_sys_poll,
77b14db5
EB
605 .read = proc_sys_read,
606 .write = proc_sys_write,
6038f373 607 .llseek = default_llseek,
9043476f
AV
608};
609
610static const struct file_operations proc_sys_dir_file_operations = {
887df078 611 .read = generic_read_dir,
77b14db5 612 .readdir = proc_sys_readdir,
3222a3e5 613 .llseek = generic_file_llseek,
77b14db5
EB
614};
615
03a44825 616static const struct inode_operations proc_sys_inode_operations = {
9043476f
AV
617 .permission = proc_sys_permission,
618 .setattr = proc_sys_setattr,
619 .getattr = proc_sys_getattr,
620};
621
622static const struct inode_operations proc_sys_dir_operations = {
77b14db5
EB
623 .lookup = proc_sys_lookup,
624 .permission = proc_sys_permission,
625 .setattr = proc_sys_setattr,
9043476f 626 .getattr = proc_sys_getattr,
77b14db5
EB
627};
628
629static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
630{
34286d66
NP
631 if (nd->flags & LOOKUP_RCU)
632 return -ECHILD;
9043476f
AV
633 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
634}
635
fe15ce44 636static int proc_sys_delete(const struct dentry *dentry)
9043476f
AV
637{
638 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
639}
640
1f87f0b5
EB
641static int sysctl_is_seen(struct ctl_table_header *p)
642{
643 struct ctl_table_set *set = p->set;
644 int res;
645 spin_lock(&sysctl_lock);
646 if (p->unregistering)
647 res = 0;
648 else if (!set->is_seen)
649 res = 1;
650 else
651 res = set->is_seen(set);
652 spin_unlock(&sysctl_lock);
653 return res;
654}
655
621e155a
NP
656static int proc_sys_compare(const struct dentry *parent,
657 const struct inode *pinode,
658 const struct dentry *dentry, const struct inode *inode,
659 unsigned int len, const char *str, const struct qstr *name)
9043476f 660{
dfef6dcd 661 struct ctl_table_header *head;
31e6b01f
NP
662 /* Although proc doesn't have negative dentries, rcu-walk means
663 * that inode here can be NULL */
dfef6dcd 664 /* AV: can it, indeed? */
31e6b01f 665 if (!inode)
dfef6dcd 666 return 1;
621e155a 667 if (name->len != len)
9043476f 668 return 1;
621e155a 669 if (memcmp(name->name, str, len))
9043476f 670 return 1;
dfef6dcd
AV
671 head = rcu_dereference(PROC_I(inode)->sysctl);
672 return !head || !sysctl_is_seen(head);
77b14db5
EB
673}
674
d72f71eb 675static const struct dentry_operations proc_sys_dentry_operations = {
77b14db5 676 .d_revalidate = proc_sys_revalidate,
9043476f
AV
677 .d_delete = proc_sys_delete,
678 .d_compare = proc_sys_compare,
77b14db5
EB
679};
680
1f87f0b5
EB
681static struct ctl_table *is_branch_in(struct ctl_table *branch,
682 struct ctl_table *table)
683{
684 struct ctl_table *p;
685 const char *s = branch->procname;
686
687 /* branch should have named subdirectory as its first element */
688 if (!s || !branch->child)
689 return NULL;
690
691 /* ... and nothing else */
692 if (branch[1].procname)
693 return NULL;
694
695 /* table should contain subdirectory with the same name */
696 for (p = table; p->procname; p++) {
697 if (!p->child)
698 continue;
699 if (p->procname && strcmp(p->procname, s) == 0)
700 return p;
701 }
702 return NULL;
703}
704
705/* see if attaching q to p would be an improvement */
706static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
707{
708 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
709 struct ctl_table *next;
710 int is_better = 0;
711 int not_in_parent = !p->attached_by;
712
713 while ((next = is_branch_in(by, to)) != NULL) {
714 if (by == q->attached_by)
715 is_better = 1;
716 if (to == p->attached_by)
717 not_in_parent = 1;
718 by = by->child;
719 to = next->child;
720 }
721
722 if (is_better && not_in_parent) {
723 q->attached_by = by;
724 q->attached_to = to;
725 q->parent = p;
726 }
727}
728
7c60c48f
EB
729static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
730 struct ctl_table *table)
1f87f0b5 731{
7c60c48f
EB
732 struct ctl_table *entry, *test;
733 int error = 0;
1f87f0b5 734
7c60c48f
EB
735 for (entry = old; entry->procname; entry++) {
736 for (test = table; test->procname; test++) {
737 if (strcmp(entry->procname, test->procname) == 0) {
738 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
739 path, test->procname);
740 error = -EEXIST;
741 }
742 }
743 }
744 return error;
1f87f0b5
EB
745}
746
7c60c48f
EB
747static int sysctl_check_dups(struct nsproxy *namespaces,
748 struct ctl_table_header *header,
749 const char *path, struct ctl_table *table)
1f87f0b5 750{
7c60c48f
EB
751 struct ctl_table_root *root;
752 struct ctl_table_set *set;
753 struct ctl_table_header *dir_head, *head;
754 struct ctl_table *dir_table;
755 int error = 0;
1f87f0b5 756
7c60c48f
EB
757 /* No dups if we are the only member of our directory */
758 if (header->attached_by != table)
759 return 0;
1f87f0b5 760
7c60c48f
EB
761 dir_head = header->parent;
762 dir_table = header->attached_to;
1f87f0b5 763
7c60c48f 764 error = sysctl_check_table_dups(path, dir_table, table);
1f87f0b5 765
7c60c48f
EB
766 root = &sysctl_table_root;
767 do {
768 set = lookup_header_set(root, namespaces);
1f87f0b5 769
7c60c48f
EB
770 list_for_each_entry(head, &set->list, ctl_entry) {
771 if (head->unregistering)
1f87f0b5 772 continue;
7c60c48f
EB
773 if (head->attached_to != dir_table)
774 continue;
775 error = sysctl_check_table_dups(path, head->attached_by,
776 table);
1f87f0b5 777 }
7c60c48f
EB
778 root = list_entry(root->root_list.next,
779 struct ctl_table_root, root_list);
780 } while (root != &sysctl_table_root);
781 return error;
1f87f0b5
EB
782}
783
7c60c48f 784static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1f87f0b5 785{
7c60c48f
EB
786 struct va_format vaf;
787 va_list args;
1f87f0b5 788
7c60c48f
EB
789 va_start(args, fmt);
790 vaf.fmt = fmt;
791 vaf.va = &args;
792
793 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
794 path, table->procname, &vaf);
1f87f0b5 795
7c60c48f
EB
796 va_end(args);
797 return -EINVAL;
1f87f0b5
EB
798}
799
7c60c48f 800static int sysctl_check_table(const char *path, struct ctl_table *table)
1f87f0b5 801{
7c60c48f 802 int err = 0;
1f87f0b5 803 for (; table->procname; table++) {
1f87f0b5 804 if (table->child)
7c60c48f
EB
805 err = sysctl_err(path, table, "Not a file");
806
807 if ((table->proc_handler == proc_dostring) ||
808 (table->proc_handler == proc_dointvec) ||
809 (table->proc_handler == proc_dointvec_minmax) ||
810 (table->proc_handler == proc_dointvec_jiffies) ||
811 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
812 (table->proc_handler == proc_dointvec_ms_jiffies) ||
813 (table->proc_handler == proc_doulongvec_minmax) ||
814 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
815 if (!table->data)
816 err = sysctl_err(path, table, "No data");
817 if (!table->maxlen)
818 err = sysctl_err(path, table, "No maxlen");
819 }
820 if (!table->proc_handler)
821 err = sysctl_err(path, table, "No proc_handler");
822
823 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
824 err = sysctl_err(path, table, "bogus .mode 0%o",
825 table->mode);
1f87f0b5 826 }
7c60c48f 827 return err;
1f87f0b5 828}
1f87f0b5
EB
829
830/**
f728019b 831 * __register_sysctl_table - register a leaf sysctl table
1f87f0b5
EB
832 * @root: List of sysctl headers to register on
833 * @namespaces: Data to compute which lists of sysctl entries are visible
834 * @path: The path to the directory the sysctl table is in.
835 * @table: the top-level table structure
836 *
837 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
838 * array. A completely 0 filled entry terminates the table.
839 *
840 * The members of the &struct ctl_table structure are used as follows:
841 *
842 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
843 * enter a sysctl file
844 *
845 * data - a pointer to data for use by proc_handler
846 *
847 * maxlen - the maximum size in bytes of the data
848 *
f728019b 849 * mode - the file permissions for the /proc/sys file
1f87f0b5 850 *
f728019b 851 * child - must be %NULL.
1f87f0b5
EB
852 *
853 * proc_handler - the text handler routine (described below)
854 *
1f87f0b5
EB
855 * extra1, extra2 - extra pointers usable by the proc handler routines
856 *
857 * Leaf nodes in the sysctl tree will be represented by a single file
858 * under /proc; non-leaf nodes will be represented by directories.
859 *
f728019b
EB
860 * There must be a proc_handler routine for any terminal nodes.
861 * Several default handlers are available to cover common cases -
1f87f0b5
EB
862 *
863 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
864 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
865 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
866 *
867 * It is the handler's job to read the input buffer from user memory
868 * and process it. The handler should return 0 on success.
869 *
870 * This routine returns %NULL on a failure to register, and a pointer
871 * to the table header on success.
872 */
6e9d5164 873struct ctl_table_header *__register_sysctl_table(
1f87f0b5
EB
874 struct ctl_table_root *root,
875 struct nsproxy *namespaces,
6e9d5164 876 const char *path, struct ctl_table *table)
1f87f0b5
EB
877{
878 struct ctl_table_header *header;
879 struct ctl_table *new, **prevp;
6e9d5164
EB
880 const char *name, *nextname;
881 unsigned int npath = 0;
1f87f0b5 882 struct ctl_table_set *set;
f05e53a7
EB
883 size_t path_bytes = 0;
884 char *new_name;
1f87f0b5
EB
885
886 /* Count the path components */
6e9d5164
EB
887 for (name = path; name; name = nextname) {
888 int namelen;
889 nextname = strchr(name, '/');
890 if (nextname) {
891 namelen = nextname - name;
892 nextname++;
893 } else {
894 namelen = strlen(name);
895 }
896 if (namelen == 0)
897 continue;
898 path_bytes += namelen + 1;
899 npath++;
900 }
1f87f0b5
EB
901
902 /*
903 * For each path component, allocate a 2-element ctl_table array.
904 * The first array element will be filled with the sysctl entry
905 * for this, the second will be the sentinel (procname == 0).
906 *
907 * We allocate everything in one go so that we don't have to
908 * worry about freeing additional memory in unregister_sysctl_table.
909 */
f05e53a7 910 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
1f87f0b5
EB
911 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
912 if (!header)
913 return NULL;
914
915 new = (struct ctl_table *) (header + 1);
f05e53a7 916 new_name = (char *)(new + (2 * npath));
1f87f0b5
EB
917
918 /* Now connect the dots */
919 prevp = &header->ctl_table;
6e9d5164
EB
920 for (name = path; name; name = nextname) {
921 int namelen;
922 nextname = strchr(name, '/');
923 if (nextname) {
924 namelen = nextname - name;
925 nextname++;
926 } else {
927 namelen = strlen(name);
928 }
929 if (namelen == 0)
930 continue;
931 memcpy(new_name, name, namelen);
932 new_name[namelen] = '\0';
933
f05e53a7 934 new->procname = new_name;
1f87f0b5
EB
935 new->mode = 0555;
936
937 *prevp = new;
938 prevp = &new->child;
939
940 new += 2;
6e9d5164 941 new_name += namelen + 1;
1f87f0b5
EB
942 }
943 *prevp = table;
944 header->ctl_table_arg = table;
945
946 INIT_LIST_HEAD(&header->ctl_entry);
947 header->used = 0;
948 header->unregistering = NULL;
949 header->root = root;
950 sysctl_set_parent(NULL, header->ctl_table);
951 header->count = 1;
7c60c48f
EB
952 if (sysctl_check_table(path, table))
953 goto fail;
1f87f0b5
EB
954 spin_lock(&sysctl_lock);
955 header->set = lookup_header_set(root, namespaces);
956 header->attached_by = header->ctl_table;
957 header->attached_to = root_table;
958 header->parent = &root_table_header;
bd295b56
EB
959 set = header->set;
960 root = header->root;
961 for (;;) {
1f87f0b5
EB
962 struct ctl_table_header *p;
963 list_for_each_entry(p, &set->list, ctl_entry) {
964 if (p->unregistering)
965 continue;
966 try_attach(p, header);
967 }
bd295b56
EB
968 if (root == &sysctl_table_root)
969 break;
970 root = list_entry(root->root_list.prev,
971 struct ctl_table_root, root_list);
972 set = lookup_header_set(root, namespaces);
1f87f0b5 973 }
7c60c48f
EB
974 if (sysctl_check_dups(namespaces, header, path, table))
975 goto fail_locked;
1f87f0b5
EB
976 header->parent->count++;
977 list_add_tail(&header->ctl_entry, &header->set->list);
978 spin_unlock(&sysctl_lock);
979
980 return header;
7c60c48f
EB
981fail_locked:
982 spin_unlock(&sysctl_lock);
983fail:
984 kfree(header);
985 dump_stack();
986 return NULL;
1f87f0b5
EB
987}
988
6e9d5164
EB
989static char *append_path(const char *path, char *pos, const char *name)
990{
991 int namelen;
992 namelen = strlen(name);
993 if (((pos - path) + namelen + 2) >= PATH_MAX)
994 return NULL;
995 memcpy(pos, name, namelen);
996 pos[namelen] = '/';
997 pos[namelen + 1] = '\0';
998 pos += namelen + 1;
999 return pos;
1000}
1001
f728019b
EB
1002static int count_subheaders(struct ctl_table *table)
1003{
1004 int has_files = 0;
1005 int nr_subheaders = 0;
1006 struct ctl_table *entry;
1007
1008 /* special case: no directory and empty directory */
1009 if (!table || !table->procname)
1010 return 1;
1011
1012 for (entry = table; entry->procname; entry++) {
1013 if (entry->child)
1014 nr_subheaders += count_subheaders(entry->child);
1015 else
1016 has_files = 1;
1017 }
1018 return nr_subheaders + has_files;
1019}
1020
1021static int register_leaf_sysctl_tables(const char *path, char *pos,
1022 struct ctl_table_header ***subheader,
1023 struct ctl_table_root *root, struct nsproxy *namespaces,
1024 struct ctl_table *table)
1025{
1026 struct ctl_table *ctl_table_arg = NULL;
1027 struct ctl_table *entry, *files;
1028 int nr_files = 0;
1029 int nr_dirs = 0;
1030 int err = -ENOMEM;
1031
1032 for (entry = table; entry->procname; entry++) {
1033 if (entry->child)
1034 nr_dirs++;
1035 else
1036 nr_files++;
1037 }
1038
1039 files = table;
1040 /* If there are mixed files and directories we need a new table */
1041 if (nr_dirs && nr_files) {
1042 struct ctl_table *new;
1043 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1044 GFP_KERNEL);
1045 if (!files)
1046 goto out;
1047
1048 ctl_table_arg = files;
1049 for (new = files, entry = table; entry->procname; entry++) {
1050 if (entry->child)
1051 continue;
1052 *new = *entry;
1053 new++;
1054 }
1055 }
1056
1057 /* Register everything except a directory full of subdirectories */
1058 if (nr_files || !nr_dirs) {
1059 struct ctl_table_header *header;
1060 header = __register_sysctl_table(root, namespaces, path, files);
1061 if (!header) {
1062 kfree(ctl_table_arg);
1063 goto out;
1064 }
1065
1066 /* Remember if we need to free the file table */
1067 header->ctl_table_arg = ctl_table_arg;
1068 **subheader = header;
1069 (*subheader)++;
1070 }
1071
1072 /* Recurse into the subdirectories. */
1073 for (entry = table; entry->procname; entry++) {
1074 char *child_pos;
1075
1076 if (!entry->child)
1077 continue;
1078
1079 err = -ENAMETOOLONG;
1080 child_pos = append_path(path, pos, entry->procname);
1081 if (!child_pos)
1082 goto out;
1083
1084 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1085 root, namespaces, entry->child);
1086 pos[0] = '\0';
1087 if (err)
1088 goto out;
1089 }
1090 err = 0;
1091out:
1092 /* On failure our caller will unregister all registered subheaders */
1093 return err;
1094}
1095
6e9d5164
EB
1096/**
1097 * __register_sysctl_paths - register a sysctl table hierarchy
1098 * @root: List of sysctl headers to register on
1099 * @namespaces: Data to compute which lists of sysctl entries are visible
1100 * @path: The path to the directory the sysctl table is in.
1101 * @table: the top-level table structure
1102 *
1103 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1104 * array. A completely 0 filled entry terminates the table.
1105 *
1106 * See __register_sysctl_table for more details.
1107 */
1108struct ctl_table_header *__register_sysctl_paths(
1109 struct ctl_table_root *root,
1110 struct nsproxy *namespaces,
1111 const struct ctl_path *path, struct ctl_table *table)
1112{
ec6a5266 1113 struct ctl_table *ctl_table_arg = table;
f728019b
EB
1114 int nr_subheaders = count_subheaders(table);
1115 struct ctl_table_header *header = NULL, **subheaders, **subheader;
6e9d5164
EB
1116 const struct ctl_path *component;
1117 char *new_path, *pos;
1118
1119 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1120 if (!new_path)
1121 return NULL;
1122
1123 pos[0] = '\0';
1124 for (component = path; component->procname; component++) {
1125 pos = append_path(new_path, pos, component->procname);
1126 if (!pos)
1127 goto out;
1128 }
ec6a5266
EB
1129 while (table->procname && table->child && !table[1].procname) {
1130 pos = append_path(new_path, pos, table->procname);
1131 if (!pos)
1132 goto out;
1133 table = table->child;
1134 }
f728019b
EB
1135 if (nr_subheaders == 1) {
1136 header = __register_sysctl_table(root, namespaces, new_path, table);
1137 if (header)
1138 header->ctl_table_arg = ctl_table_arg;
1139 } else {
1140 header = kzalloc(sizeof(*header) +
1141 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1142 if (!header)
1143 goto out;
1144
1145 subheaders = (struct ctl_table_header **) (header + 1);
1146 subheader = subheaders;
ec6a5266 1147 header->ctl_table_arg = ctl_table_arg;
f728019b
EB
1148
1149 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1150 root, namespaces, table))
1151 goto err_register_leaves;
1152 }
1153
6e9d5164
EB
1154out:
1155 kfree(new_path);
1156 return header;
f728019b
EB
1157
1158err_register_leaves:
1159 while (subheader > subheaders) {
1160 struct ctl_table_header *subh = *(--subheader);
1161 struct ctl_table *table = subh->ctl_table_arg;
1162 unregister_sysctl_table(subh);
1163 kfree(table);
1164 }
1165 kfree(header);
1166 header = NULL;
1167 goto out;
6e9d5164
EB
1168}
1169
1f87f0b5
EB
1170/**
1171 * register_sysctl_table_path - register a sysctl table hierarchy
1172 * @path: The path to the directory the sysctl table is in.
1173 * @table: the top-level table structure
1174 *
1175 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1176 * array. A completely 0 filled entry terminates the table.
1177 *
1178 * See __register_sysctl_paths for more details.
1179 */
1180struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1181 struct ctl_table *table)
1182{
1183 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1184 path, table);
1185}
1186EXPORT_SYMBOL(register_sysctl_paths);
1187
1188/**
1189 * register_sysctl_table - register a sysctl table hierarchy
1190 * @table: the top-level table structure
1191 *
1192 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1193 * array. A completely 0 filled entry terminates the table.
1194 *
1195 * See register_sysctl_paths for more details.
1196 */
1197struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1198{
1199 static const struct ctl_path null_path[] = { {} };
1200
1201 return register_sysctl_paths(null_path, table);
1202}
1203EXPORT_SYMBOL(register_sysctl_table);
1204
1205/**
1206 * unregister_sysctl_table - unregister a sysctl table hierarchy
1207 * @header: the header returned from register_sysctl_table
1208 *
1209 * Unregisters the sysctl table and all children. proc entries may not
1210 * actually be removed until they are no longer used by anyone.
1211 */
1212void unregister_sysctl_table(struct ctl_table_header * header)
1213{
f728019b 1214 int nr_subheaders;
1f87f0b5
EB
1215 might_sleep();
1216
1217 if (header == NULL)
1218 return;
1219
f728019b
EB
1220 nr_subheaders = count_subheaders(header->ctl_table_arg);
1221 if (unlikely(nr_subheaders > 1)) {
1222 struct ctl_table_header **subheaders;
1223 int i;
1224
1225 subheaders = (struct ctl_table_header **)(header + 1);
1226 for (i = nr_subheaders -1; i >= 0; i--) {
1227 struct ctl_table_header *subh = subheaders[i];
1228 struct ctl_table *table = subh->ctl_table_arg;
1229 unregister_sysctl_table(subh);
1230 kfree(table);
1231 }
1232 kfree(header);
1233 return;
1234 }
1235
1f87f0b5
EB
1236 spin_lock(&sysctl_lock);
1237 start_unregistering(header);
1238 if (!--header->parent->count) {
1239 WARN_ON(1);
1240 kfree_rcu(header->parent, rcu);
1241 }
1242 if (!--header->count)
1243 kfree_rcu(header, rcu);
1244 spin_unlock(&sysctl_lock);
1245}
1246EXPORT_SYMBOL(unregister_sysctl_table);
1247
1248void setup_sysctl_set(struct ctl_table_set *p,
1f87f0b5
EB
1249 int (*is_seen)(struct ctl_table_set *))
1250{
1251 INIT_LIST_HEAD(&p->list);
1f87f0b5
EB
1252 p->is_seen = is_seen;
1253}
1254
97324cd8
EB
1255void retire_sysctl_set(struct ctl_table_set *set)
1256{
1257 WARN_ON(!list_empty(&set->list));
1258}
1f87f0b5 1259
1e0edd3f 1260int __init proc_sys_init(void)
77b14db5 1261{
e1675231
AD
1262 struct proc_dir_entry *proc_sys_root;
1263
77b14db5 1264 proc_sys_root = proc_mkdir("sys", NULL);
9043476f
AV
1265 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1266 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
77b14db5 1267 proc_sys_root->nlink = 0;
de4e83bd
EB
1268
1269 return sysctl_init();
77b14db5 1270}
This page took 0.555193 seconds and 5 git commands to generate.