blob: 3b63f298ce288486a47247138dfe915202a58edb [file] [log] [blame]
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001/*
2 * /proc/sys support
3 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04004#include <linux/init.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08005#include <linux/sysctl.h>
Lucas De Marchif1ecf062011-11-02 13:39:22 -07006#include <linux/poll.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08007#include <linux/proc_fs.h>
8#include <linux/security.h>
Nick Piggin34286d62011-01-07 17:49:57 +11009#include <linux/namei.h>
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080010#include <linux/module.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -080011#include "internal.h"
12
Al Virod72f71e2009-02-20 05:58:47 +000013static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080014static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080015static const struct inode_operations proc_sys_inode_operations;
Al Viro90434762008-07-15 08:54:06 -040016static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080018
Lucas De Marchif1ecf062011-11-02 13:39:22 -070019void 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
Eric W. Biedermana1945582012-01-21 17:51:48 -080028static struct ctl_table root_table[] = {
29 {
30 .procname = "",
31 .mode = S_IRUGO|S_IXUGO,
32 .child = &root_table[1],
33 },
34 { }
35};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080036static struct ctl_table_root sysctl_table_root;
37static struct ctl_table_header root_table_header = {
38 {{.count = 1,
Eric W. Biederman938aaa42012-01-09 17:24:30 -080039 .nreg = 1,
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080042 .root = &sysctl_table_root,
43 .set = &sysctl_table_root.default_set,
44};
45static struct ctl_table_root sysctl_table_root = {
46 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
47 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
48};
49
50static DEFINE_SPINLOCK(sysctl_lock);
51
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080052static int namecmp(const char *name1, int len1, const char *name2, int len2)
53{
54 int minlen;
55 int cmp;
56
57 minlen = len1;
58 if (minlen > len2)
59 minlen = len2;
60
61 cmp = memcmp(name1, name2, minlen);
62 if (cmp == 0)
63 cmp = len1 - len2;
64 return cmp;
65}
66
67static struct ctl_table *find_entry(struct ctl_table_header **phead,
68 struct ctl_table_set *set,
69 struct ctl_table_header *dir_head, struct ctl_table *dir,
70 const char *name, int namelen)
71{
72 struct ctl_table_header *head;
73 struct ctl_table *entry;
74
75 if (dir_head->set == set) {
76 for (entry = dir; entry->procname; entry++) {
77 const char *procname = entry->procname;
78 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
79 *phead = dir_head;
80 return entry;
81 }
82 }
83 }
84
85 list_for_each_entry(head, &set->list, ctl_entry) {
86 if (head->unregistering)
87 continue;
88 if (head->attached_to != dir)
89 continue;
90 for (entry = head->attached_by; entry->procname; entry++) {
91 const char *procname = entry->procname;
92 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
93 *phead = head;
94 return entry;
95 }
96 }
97 }
98 return NULL;
99}
100
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800101static void init_header(struct ctl_table_header *head,
102 struct ctl_table_root *root, struct ctl_table_set *set,
103 struct ctl_table *table)
104{
105 head->ctl_table_arg = table;
106 INIT_LIST_HEAD(&head->ctl_entry);
107 head->used = 0;
108 head->count = 1;
109 head->nreg = 1;
110 head->unregistering = NULL;
111 head->root = root;
112 head->set = set;
113 head->parent = NULL;
114}
115
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800116static void erase_header(struct ctl_table_header *head)
117{
118 list_del_init(&head->ctl_entry);
119}
120
121static void insert_header(struct ctl_table_header *header)
122{
123 header->parent->count++;
124 list_add_tail(&header->ctl_entry, &header->set->list);
125}
126
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800127/* called under sysctl_lock */
128static int use_table(struct ctl_table_header *p)
129{
130 if (unlikely(p->unregistering))
131 return 0;
132 p->used++;
133 return 1;
134}
135
136/* called under sysctl_lock */
137static void unuse_table(struct ctl_table_header *p)
138{
139 if (!--p->used)
140 if (unlikely(p->unregistering))
141 complete(p->unregistering);
142}
143
144/* called under sysctl_lock, will reacquire if has to wait */
145static void start_unregistering(struct ctl_table_header *p)
146{
147 /*
148 * if p->used is 0, nobody will ever touch that entry again;
149 * we'll eliminate all paths to it before dropping sysctl_lock
150 */
151 if (unlikely(p->used)) {
152 struct completion wait;
153 init_completion(&wait);
154 p->unregistering = &wait;
155 spin_unlock(&sysctl_lock);
156 wait_for_completion(&wait);
157 spin_lock(&sysctl_lock);
158 } else {
159 /* anything non-NULL; we'll never dereference it */
160 p->unregistering = ERR_PTR(-EINVAL);
161 }
162 /*
163 * do not remove from the list until nobody holds it; walking the
164 * list in do_sysctl() relies on that.
165 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800166 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800167}
168
169static void sysctl_head_get(struct ctl_table_header *head)
170{
171 spin_lock(&sysctl_lock);
172 head->count++;
173 spin_unlock(&sysctl_lock);
174}
175
176void sysctl_head_put(struct ctl_table_header *head)
177{
178 spin_lock(&sysctl_lock);
179 if (!--head->count)
180 kfree_rcu(head, rcu);
181 spin_unlock(&sysctl_lock);
182}
183
184static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
185{
186 if (!head)
187 BUG();
188 spin_lock(&sysctl_lock);
189 if (!use_table(head))
190 head = ERR_PTR(-ENOENT);
191 spin_unlock(&sysctl_lock);
192 return head;
193}
194
195static void sysctl_head_finish(struct ctl_table_header *head)
196{
197 if (!head)
198 return;
199 spin_lock(&sysctl_lock);
200 unuse_table(head);
201 spin_unlock(&sysctl_lock);
202}
203
204static struct ctl_table_set *
205lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
206{
207 struct ctl_table_set *set = &root->default_set;
208 if (root->lookup)
209 set = root->lookup(root, namespaces);
210 return set;
211}
212
213static struct list_head *
214lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
215{
216 struct ctl_table_set *set = lookup_header_set(root, namespaces);
217 return &set->list;
218}
219
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800220static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
221 struct ctl_table_header *dir_head,
222 struct ctl_table *dir,
223 const char *name, int namelen)
224{
225 struct ctl_table_header *head;
226 struct ctl_table *entry;
227 struct ctl_table_root *root;
228 struct ctl_table_set *set;
229
230 spin_lock(&sysctl_lock);
231 root = &sysctl_table_root;
232 do {
233 set = lookup_header_set(root, current->nsproxy);
234 entry = find_entry(&head, set, dir_head, dir, name, namelen);
235 if (entry && use_table(head))
236 *phead = head;
237 else
238 entry = NULL;
239 root = list_entry(root->root_list.next,
240 struct ctl_table_root, root_list);
241 } while (!entry && root != &sysctl_table_root);
242 spin_unlock(&sysctl_lock);
243 return entry;
244}
245
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800246static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
247 struct ctl_table_header *prev)
248{
249 struct ctl_table_root *root;
250 struct list_head *header_list;
251 struct ctl_table_header *head;
252 struct list_head *tmp;
253
254 spin_lock(&sysctl_lock);
255 if (prev) {
256 head = prev;
257 tmp = &prev->ctl_entry;
258 unuse_table(prev);
259 goto next;
260 }
261 tmp = &root_table_header.ctl_entry;
262 for (;;) {
263 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
264
265 if (!use_table(head))
266 goto next;
267 spin_unlock(&sysctl_lock);
268 return head;
269 next:
270 root = head->root;
271 tmp = tmp->next;
272 header_list = lookup_header_list(root, namespaces);
273 if (tmp != header_list)
274 continue;
275
276 do {
277 root = list_entry(root->root_list.next,
278 struct ctl_table_root, root_list);
279 if (root == &sysctl_table_root)
280 goto out;
281 header_list = lookup_header_list(root, namespaces);
282 } while (list_empty(header_list));
283 tmp = header_list->next;
284 }
285out:
286 spin_unlock(&sysctl_lock);
287 return NULL;
288}
289
290static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
291{
292 return __sysctl_head_next(current->nsproxy, prev);
293}
294
295void register_sysctl_root(struct ctl_table_root *root)
296{
297 spin_lock(&sysctl_lock);
298 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
299 spin_unlock(&sysctl_lock);
300}
301
302/*
303 * sysctl_perm does NOT grant the superuser all rights automatically, because
304 * some sysctl variables are readonly even to root.
305 */
306
307static int test_perm(int mode, int op)
308{
309 if (!current_euid())
310 mode >>= 6;
311 else if (in_egroup_p(0))
312 mode >>= 3;
313 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
314 return 0;
315 return -EACCES;
316}
317
318static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
319{
320 int mode;
321
322 if (root->permissions)
323 mode = root->permissions(root, current->nsproxy, table);
324 else
325 mode = table->mode;
326
327 return test_perm(mode, op);
328}
329
Al Viro90434762008-07-15 08:54:06 -0400330static struct inode *proc_sys_make_inode(struct super_block *sb,
331 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800332{
333 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400334 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800335
Al Viro90434762008-07-15 08:54:06 -0400336 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800337 if (!inode)
338 goto out;
339
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400340 inode->i_ino = get_next_ino();
341
Al Viro90434762008-07-15 08:54:06 -0400342 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800343 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400344 ei->sysctl = head;
345 ei->sysctl_entry = table;
346
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800347 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400348 inode->i_mode = table->mode;
349 if (!table->child) {
350 inode->i_mode |= S_IFREG;
351 inode->i_op = &proc_sys_inode_operations;
352 inode->i_fop = &proc_sys_file_operations;
353 } else {
354 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400355 inode->i_op = &proc_sys_dir_operations;
356 inode->i_fop = &proc_sys_dir_file_operations;
357 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800358out:
359 return inode;
360}
361
Adrian Bunk81324362008-10-03 00:33:54 +0400362static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800363{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800364 struct ctl_table_header *head = PROC_I(inode)->sysctl;
365 if (!head)
366 head = &root_table_header;
367 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800368}
369
370static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
371 struct nameidata *nd)
372{
Al Viro90434762008-07-15 08:54:06 -0400373 struct ctl_table_header *head = grab_header(dir);
374 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
375 struct ctl_table_header *h = NULL;
376 struct qstr *name = &dentry->d_name;
377 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800378 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400379 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800380
Al Viro90434762008-07-15 08:54:06 -0400381 if (IS_ERR(head))
382 return ERR_CAST(head);
383
384 if (table && !table->child) {
385 WARN_ON(1);
386 goto out;
387 }
388
Eric W. Biedermana1945582012-01-21 17:51:48 -0800389 table = table ? table->child : &head->ctl_table[1];
Al Viro90434762008-07-15 08:54:06 -0400390
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800391 p = lookup_entry(&h, head, table, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400392 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800393 goto out;
394
395 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400396 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
397 if (h)
398 sysctl_head_finish(h);
399
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800400 if (!inode)
401 goto out;
402
403 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100404 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800405 d_add(dentry, inode);
406
407out:
408 sysctl_head_finish(head);
409 return err;
410}
411
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700412static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
413 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800414{
Al Viro90434762008-07-15 08:54:06 -0400415 struct inode *inode = filp->f_path.dentry->d_inode;
416 struct ctl_table_header *head = grab_header(inode);
417 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100418 ssize_t error;
419 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800420
Al Viro90434762008-07-15 08:54:06 -0400421 if (IS_ERR(head))
422 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800423
424 /*
425 * At this point we know that the sysctl was not unregistered
426 * and won't be until we finish.
427 */
428 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700429 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800430 goto out;
431
Al Viro90434762008-07-15 08:54:06 -0400432 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
433 error = -EINVAL;
434 if (!table->proc_handler)
435 goto out;
436
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800437 /* careful: calling conventions are nasty here */
438 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700439 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800440 if (!error)
441 error = res;
442out:
443 sysctl_head_finish(head);
444
445 return error;
446}
447
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700448static ssize_t proc_sys_read(struct file *filp, char __user *buf,
449 size_t count, loff_t *ppos)
450{
451 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
452}
453
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800454static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
455 size_t count, loff_t *ppos)
456{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700457 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800458}
459
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700460static int proc_sys_open(struct inode *inode, struct file *filp)
461{
462 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
463
464 if (table->poll)
465 filp->private_data = proc_sys_poll_event(table->poll);
466
467 return 0;
468}
469
470static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
471{
472 struct inode *inode = filp->f_path.dentry->d_inode;
473 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
474 unsigned long event = (unsigned long)filp->private_data;
475 unsigned int ret = DEFAULT_POLLMASK;
476
477 if (!table->proc_handler)
478 goto out;
479
480 if (!table->poll)
481 goto out;
482
483 poll_wait(filp, &table->poll->wait, wait);
484
485 if (event != atomic_read(&table->poll->event)) {
486 filp->private_data = proc_sys_poll_event(table->poll);
487 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
488 }
489
490out:
491 return ret;
492}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800493
494static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400495 filldir_t filldir,
496 struct ctl_table_header *head,
497 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800498{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800499 struct dentry *child, *dir = filp->f_path.dentry;
500 struct inode *inode;
501 struct qstr qname;
502 ino_t ino = 0;
503 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800504
505 qname.name = table->procname;
506 qname.len = strlen(table->procname);
507 qname.hash = full_name_hash(qname.name, qname.len);
508
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800509 child = d_lookup(dir, &qname);
510 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400511 child = d_alloc(dir, &qname);
512 if (child) {
513 inode = proc_sys_make_inode(dir->d_sb, head, table);
514 if (!inode) {
515 dput(child);
516 return -ENOMEM;
517 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100518 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400519 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800520 }
Al Viro90434762008-07-15 08:54:06 -0400521 } else {
522 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800523 }
524 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800525 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400526 ino = inode->i_ino;
527 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800528 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400529 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
530}
531
532static int scan(struct ctl_table_header *head, ctl_table *table,
533 unsigned long *pos, struct file *file,
534 void *dirent, filldir_t filldir)
535{
536
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700537 for (; table->procname; table++, (*pos)++) {
Al Viro90434762008-07-15 08:54:06 -0400538 int res;
539
Al Viro90434762008-07-15 08:54:06 -0400540 if (*pos < file->f_pos)
541 continue;
542
543 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
544 if (res)
545 return res;
546
547 file->f_pos = *pos + 1;
548 }
549 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800550}
551
552static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
553{
Al Viro90434762008-07-15 08:54:06 -0400554 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800555 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400556 struct ctl_table_header *head = grab_header(inode);
557 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
558 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800559 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400560 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800561
Al Viro90434762008-07-15 08:54:06 -0400562 if (IS_ERR(head))
563 return PTR_ERR(head);
564
565 if (table && !table->child) {
566 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800567 goto out;
Al Viro90434762008-07-15 08:54:06 -0400568 }
569
Eric W. Biedermana1945582012-01-21 17:51:48 -0800570 table = table ? table->child : &head->ctl_table[1];
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800571
572 ret = 0;
573 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
574 if (filp->f_pos == 0) {
575 if (filldir(dirent, ".", 1, filp->f_pos,
576 inode->i_ino, DT_DIR) < 0)
577 goto out;
578 filp->f_pos++;
579 }
580 if (filp->f_pos == 1) {
581 if (filldir(dirent, "..", 2, filp->f_pos,
582 parent_ino(dentry), DT_DIR) < 0)
583 goto out;
584 filp->f_pos++;
585 }
586 pos = 2;
587
Al Viro90434762008-07-15 08:54:06 -0400588 ret = scan(head, table, &pos, filp, dirent, filldir);
589 if (ret)
590 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800591
Al Viro90434762008-07-15 08:54:06 -0400592 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
593 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800594 continue;
Al Viro90434762008-07-15 08:54:06 -0400595 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
596 if (ret) {
597 sysctl_head_finish(h);
598 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800599 }
600 }
601 ret = 1;
602out:
603 sysctl_head_finish(head);
604 return ret;
605}
606
Al Viro10556cb2011-06-20 19:28:19 -0400607static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800608{
609 /*
610 * sysctl entries that are not writeable,
611 * are _NOT_ writeable, capabilities or not.
612 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200613 struct ctl_table_header *head;
614 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800615 int error;
616
Miklos Szeredif696a362008-07-31 13:41:58 +0200617 /* Executable files are not allowed under /proc/sys/ */
618 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
619 return -EACCES;
620
621 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400622 if (IS_ERR(head))
623 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800624
Miklos Szeredif696a362008-07-31 13:41:58 +0200625 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400626 if (!table) /* global root - r-xr-xr-x */
627 error = mask & MAY_WRITE ? -EACCES : 0;
628 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400629 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800630
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800631 sysctl_head_finish(head);
632 return error;
633}
634
635static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
636{
637 struct inode *inode = dentry->d_inode;
638 int error;
639
640 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
641 return -EPERM;
642
643 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200644 if (error)
645 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800646
Christoph Hellwig10257742010-06-04 11:30:02 +0200647 if ((attr->ia_valid & ATTR_SIZE) &&
648 attr->ia_size != i_size_read(inode)) {
649 error = vmtruncate(inode, attr->ia_size);
650 if (error)
651 return error;
652 }
653
654 setattr_copy(inode, attr);
655 mark_inode_dirty(inode);
656 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800657}
658
Al Viro90434762008-07-15 08:54:06 -0400659static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
660{
661 struct inode *inode = dentry->d_inode;
662 struct ctl_table_header *head = grab_header(inode);
663 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
664
665 if (IS_ERR(head))
666 return PTR_ERR(head);
667
668 generic_fillattr(inode, stat);
669 if (table)
670 stat->mode = (stat->mode & S_IFMT) | table->mode;
671
672 sysctl_head_finish(head);
673 return 0;
674}
675
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800676static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700677 .open = proc_sys_open,
678 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800679 .read = proc_sys_read,
680 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200681 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400682};
683
684static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700685 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800686 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200687 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800688};
689
Jan Engelhardt03a44822008-02-08 04:21:19 -0800690static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400691 .permission = proc_sys_permission,
692 .setattr = proc_sys_setattr,
693 .getattr = proc_sys_getattr,
694};
695
696static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800697 .lookup = proc_sys_lookup,
698 .permission = proc_sys_permission,
699 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400700 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800701};
702
703static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
704{
Nick Piggin34286d62011-01-07 17:49:57 +1100705 if (nd->flags & LOOKUP_RCU)
706 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400707 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
708}
709
Nick Pigginfe15ce42011-01-07 17:49:23 +1100710static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400711{
712 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
713}
714
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800715static int sysctl_is_seen(struct ctl_table_header *p)
716{
717 struct ctl_table_set *set = p->set;
718 int res;
719 spin_lock(&sysctl_lock);
720 if (p->unregistering)
721 res = 0;
722 else if (!set->is_seen)
723 res = 1;
724 else
725 res = set->is_seen(set);
726 spin_unlock(&sysctl_lock);
727 return res;
728}
729
Nick Piggin621e1552011-01-07 17:49:27 +1100730static int proc_sys_compare(const struct dentry *parent,
731 const struct inode *pinode,
732 const struct dentry *dentry, const struct inode *inode,
733 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400734{
Al Virodfef6dc2011-03-08 01:25:28 -0500735 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100736 /* Although proc doesn't have negative dentries, rcu-walk means
737 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500738 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100739 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500740 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100741 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400742 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100743 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400744 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500745 head = rcu_dereference(PROC_I(inode)->sysctl);
746 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800747}
748
Al Virod72f71e2009-02-20 05:58:47 +0000749static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800750 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400751 .d_delete = proc_sys_delete,
752 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800753};
754
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800755static struct ctl_table *is_branch_in(struct ctl_table *branch,
756 struct ctl_table *table)
757{
758 struct ctl_table *p;
759 const char *s = branch->procname;
760
761 /* branch should have named subdirectory as its first element */
762 if (!s || !branch->child)
763 return NULL;
764
765 /* ... and nothing else */
766 if (branch[1].procname)
767 return NULL;
768
769 /* table should contain subdirectory with the same name */
770 for (p = table; p->procname; p++) {
771 if (!p->child)
772 continue;
773 if (p->procname && strcmp(p->procname, s) == 0)
774 return p;
775 }
776 return NULL;
777}
778
779/* see if attaching q to p would be an improvement */
780static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
781{
782 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
783 struct ctl_table *next;
784 int is_better = 0;
785 int not_in_parent = !p->attached_by;
786
787 while ((next = is_branch_in(by, to)) != NULL) {
788 if (by == q->attached_by)
789 is_better = 1;
790 if (to == p->attached_by)
791 not_in_parent = 1;
792 by = by->child;
793 to = next->child;
794 }
795
796 if (is_better && not_in_parent) {
797 q->attached_by = by;
798 q->attached_to = to;
799 q->parent = p;
800 }
801}
802
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800803static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
804 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800805{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800806 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800807 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800808
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800809 for (entry = old; entry->procname; entry++) {
810 for (test = table; test->procname; test++) {
811 if (strcmp(entry->procname, test->procname) == 0) {
812 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
813 path, test->procname);
814 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800815 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800816 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800817 }
818 return error;
819}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800820
821static int sysctl_check_dups(struct nsproxy *namespaces,
822 struct ctl_table_header *header,
823 const char *path, struct ctl_table *table)
824{
825 struct ctl_table_root *root;
826 struct ctl_table_set *set;
827 struct ctl_table_header *dir_head, *head;
828 struct ctl_table *dir_table;
829 int error = 0;
830
831 /* No dups if we are the only member of our directory */
832 if (header->attached_by != table)
833 return 0;
834
835 dir_head = header->parent;
836 dir_table = header->attached_to;
837
838 error = sysctl_check_table_dups(path, dir_table, table);
839
840 root = &sysctl_table_root;
841 do {
842 set = lookup_header_set(root, namespaces);
843
844 list_for_each_entry(head, &set->list, ctl_entry) {
845 if (head->unregistering)
846 continue;
847 if (head->attached_to != dir_table)
848 continue;
849 error = sysctl_check_table_dups(path, head->attached_by,
850 table);
851 }
852 root = list_entry(root->root_list.next,
853 struct ctl_table_root, root_list);
854 } while (root != &sysctl_table_root);
855 return error;
856}
857
858static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
859{
860 struct va_format vaf;
861 va_list args;
862
863 va_start(args, fmt);
864 vaf.fmt = fmt;
865 vaf.va = &args;
866
867 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
868 path, table->procname, &vaf);
869
870 va_end(args);
871 return -EINVAL;
872}
873
874static int sysctl_check_table(const char *path, struct ctl_table *table)
875{
876 int err = 0;
877 for (; table->procname; table++) {
878 if (table->child)
879 err = sysctl_err(path, table, "Not a file");
880
881 if ((table->proc_handler == proc_dostring) ||
882 (table->proc_handler == proc_dointvec) ||
883 (table->proc_handler == proc_dointvec_minmax) ||
884 (table->proc_handler == proc_dointvec_jiffies) ||
885 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
886 (table->proc_handler == proc_dointvec_ms_jiffies) ||
887 (table->proc_handler == proc_doulongvec_minmax) ||
888 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
889 if (!table->data)
890 err = sysctl_err(path, table, "No data");
891 if (!table->maxlen)
892 err = sysctl_err(path, table, "No maxlen");
893 }
894 if (!table->proc_handler)
895 err = sysctl_err(path, table, "No proc_handler");
896
897 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
898 err = sysctl_err(path, table, "bogus .mode 0%o",
899 table->mode);
900 }
901 return err;
902}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800903
904/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800905 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800906 * @root: List of sysctl headers to register on
907 * @namespaces: Data to compute which lists of sysctl entries are visible
908 * @path: The path to the directory the sysctl table is in.
909 * @table: the top-level table structure
910 *
911 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
912 * array. A completely 0 filled entry terminates the table.
913 *
914 * The members of the &struct ctl_table structure are used as follows:
915 *
916 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
917 * enter a sysctl file
918 *
919 * data - a pointer to data for use by proc_handler
920 *
921 * maxlen - the maximum size in bytes of the data
922 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800923 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800924 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800925 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800926 *
927 * proc_handler - the text handler routine (described below)
928 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800929 * extra1, extra2 - extra pointers usable by the proc handler routines
930 *
931 * Leaf nodes in the sysctl tree will be represented by a single file
932 * under /proc; non-leaf nodes will be represented by directories.
933 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800934 * There must be a proc_handler routine for any terminal nodes.
935 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800936 *
937 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
938 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
939 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
940 *
941 * It is the handler's job to read the input buffer from user memory
942 * and process it. The handler should return 0 on success.
943 *
944 * This routine returns %NULL on a failure to register, and a pointer
945 * to the table header on success.
946 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800947struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800948 struct ctl_table_root *root,
949 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800950 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800951{
952 struct ctl_table_header *header;
953 struct ctl_table *new, **prevp;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800954 const char *name, *nextname;
955 unsigned int npath = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800956 struct ctl_table_set *set;
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800957 size_t path_bytes = 0;
958 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800959
960 /* Count the path components */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800961 for (name = path; name; name = nextname) {
962 int namelen;
963 nextname = strchr(name, '/');
964 if (nextname) {
965 namelen = nextname - name;
966 nextname++;
967 } else {
968 namelen = strlen(name);
969 }
970 if (namelen == 0)
971 continue;
972 path_bytes += namelen + 1;
973 npath++;
974 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800975
976 /*
977 * For each path component, allocate a 2-element ctl_table array.
978 * The first array element will be filled with the sysctl entry
979 * for this, the second will be the sentinel (procname == 0).
980 *
981 * We allocate everything in one go so that we don't have to
982 * worry about freeing additional memory in unregister_sysctl_table.
983 */
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800984 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800985 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
986 if (!header)
987 return NULL;
988
989 new = (struct ctl_table *) (header + 1);
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800990 new_name = (char *)(new + (2 * npath));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800991
992 /* Now connect the dots */
993 prevp = &header->ctl_table;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800994 for (name = path; name; name = nextname) {
995 int namelen;
996 nextname = strchr(name, '/');
997 if (nextname) {
998 namelen = nextname - name;
999 nextname++;
1000 } else {
1001 namelen = strlen(name);
1002 }
1003 if (namelen == 0)
1004 continue;
1005 memcpy(new_name, name, namelen);
1006 new_name[namelen] = '\0';
1007
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -08001008 new->procname = new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001009 new->mode = 0555;
1010
1011 *prevp = new;
1012 prevp = &new->child;
1013
1014 new += 2;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001015 new_name += namelen + 1;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001016 }
1017 *prevp = table;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001018
Eric W. Biedermane0d04522012-01-09 22:36:41 -08001019 init_header(header, root, NULL, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001020 if (sysctl_check_table(path, table))
1021 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001022
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001023 spin_lock(&sysctl_lock);
1024 header->set = lookup_header_set(root, namespaces);
1025 header->attached_by = header->ctl_table;
Eric W. Biedermana1945582012-01-21 17:51:48 -08001026 header->attached_to = &root_table[1];
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001027 header->parent = &root_table_header;
Eric W. Biedermanbd295b52012-01-22 21:10:21 -08001028 set = header->set;
1029 root = header->root;
1030 for (;;) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001031 struct ctl_table_header *p;
1032 list_for_each_entry(p, &set->list, ctl_entry) {
1033 if (p->unregistering)
1034 continue;
1035 try_attach(p, header);
1036 }
Eric W. Biedermanbd295b52012-01-22 21:10:21 -08001037 if (root == &sysctl_table_root)
1038 break;
1039 root = list_entry(root->root_list.prev,
1040 struct ctl_table_root, root_list);
1041 set = lookup_header_set(root, namespaces);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001042 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001043 if (sysctl_check_dups(namespaces, header, path, table))
1044 goto fail_locked;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -08001045 insert_header(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001046 spin_unlock(&sysctl_lock);
1047
1048 return header;
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001049fail_locked:
1050 spin_unlock(&sysctl_lock);
1051fail:
1052 kfree(header);
1053 dump_stack();
1054 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001055}
1056
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001057static char *append_path(const char *path, char *pos, const char *name)
1058{
1059 int namelen;
1060 namelen = strlen(name);
1061 if (((pos - path) + namelen + 2) >= PATH_MAX)
1062 return NULL;
1063 memcpy(pos, name, namelen);
1064 pos[namelen] = '/';
1065 pos[namelen + 1] = '\0';
1066 pos += namelen + 1;
1067 return pos;
1068}
1069
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001070static int count_subheaders(struct ctl_table *table)
1071{
1072 int has_files = 0;
1073 int nr_subheaders = 0;
1074 struct ctl_table *entry;
1075
1076 /* special case: no directory and empty directory */
1077 if (!table || !table->procname)
1078 return 1;
1079
1080 for (entry = table; entry->procname; entry++) {
1081 if (entry->child)
1082 nr_subheaders += count_subheaders(entry->child);
1083 else
1084 has_files = 1;
1085 }
1086 return nr_subheaders + has_files;
1087}
1088
1089static int register_leaf_sysctl_tables(const char *path, char *pos,
1090 struct ctl_table_header ***subheader,
1091 struct ctl_table_root *root, struct nsproxy *namespaces,
1092 struct ctl_table *table)
1093{
1094 struct ctl_table *ctl_table_arg = NULL;
1095 struct ctl_table *entry, *files;
1096 int nr_files = 0;
1097 int nr_dirs = 0;
1098 int err = -ENOMEM;
1099
1100 for (entry = table; entry->procname; entry++) {
1101 if (entry->child)
1102 nr_dirs++;
1103 else
1104 nr_files++;
1105 }
1106
1107 files = table;
1108 /* If there are mixed files and directories we need a new table */
1109 if (nr_dirs && nr_files) {
1110 struct ctl_table *new;
1111 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1112 GFP_KERNEL);
1113 if (!files)
1114 goto out;
1115
1116 ctl_table_arg = files;
1117 for (new = files, entry = table; entry->procname; entry++) {
1118 if (entry->child)
1119 continue;
1120 *new = *entry;
1121 new++;
1122 }
1123 }
1124
1125 /* Register everything except a directory full of subdirectories */
1126 if (nr_files || !nr_dirs) {
1127 struct ctl_table_header *header;
1128 header = __register_sysctl_table(root, namespaces, path, files);
1129 if (!header) {
1130 kfree(ctl_table_arg);
1131 goto out;
1132 }
1133
1134 /* Remember if we need to free the file table */
1135 header->ctl_table_arg = ctl_table_arg;
1136 **subheader = header;
1137 (*subheader)++;
1138 }
1139
1140 /* Recurse into the subdirectories. */
1141 for (entry = table; entry->procname; entry++) {
1142 char *child_pos;
1143
1144 if (!entry->child)
1145 continue;
1146
1147 err = -ENAMETOOLONG;
1148 child_pos = append_path(path, pos, entry->procname);
1149 if (!child_pos)
1150 goto out;
1151
1152 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1153 root, namespaces, entry->child);
1154 pos[0] = '\0';
1155 if (err)
1156 goto out;
1157 }
1158 err = 0;
1159out:
1160 /* On failure our caller will unregister all registered subheaders */
1161 return err;
1162}
1163
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001164/**
1165 * __register_sysctl_paths - register a sysctl table hierarchy
1166 * @root: List of sysctl headers to register on
1167 * @namespaces: Data to compute which lists of sysctl entries are visible
1168 * @path: The path to the directory the sysctl table is in.
1169 * @table: the top-level table structure
1170 *
1171 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1172 * array. A completely 0 filled entry terminates the table.
1173 *
1174 * See __register_sysctl_table for more details.
1175 */
1176struct ctl_table_header *__register_sysctl_paths(
1177 struct ctl_table_root *root,
1178 struct nsproxy *namespaces,
1179 const struct ctl_path *path, struct ctl_table *table)
1180{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001181 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001182 int nr_subheaders = count_subheaders(table);
1183 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001184 const struct ctl_path *component;
1185 char *new_path, *pos;
1186
1187 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1188 if (!new_path)
1189 return NULL;
1190
1191 pos[0] = '\0';
1192 for (component = path; component->procname; component++) {
1193 pos = append_path(new_path, pos, component->procname);
1194 if (!pos)
1195 goto out;
1196 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001197 while (table->procname && table->child && !table[1].procname) {
1198 pos = append_path(new_path, pos, table->procname);
1199 if (!pos)
1200 goto out;
1201 table = table->child;
1202 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001203 if (nr_subheaders == 1) {
1204 header = __register_sysctl_table(root, namespaces, new_path, table);
1205 if (header)
1206 header->ctl_table_arg = ctl_table_arg;
1207 } else {
1208 header = kzalloc(sizeof(*header) +
1209 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1210 if (!header)
1211 goto out;
1212
1213 subheaders = (struct ctl_table_header **) (header + 1);
1214 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001215 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001216
1217 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1218 root, namespaces, table))
1219 goto err_register_leaves;
1220 }
1221
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001222out:
1223 kfree(new_path);
1224 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001225
1226err_register_leaves:
1227 while (subheader > subheaders) {
1228 struct ctl_table_header *subh = *(--subheader);
1229 struct ctl_table *table = subh->ctl_table_arg;
1230 unregister_sysctl_table(subh);
1231 kfree(table);
1232 }
1233 kfree(header);
1234 header = NULL;
1235 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001236}
1237
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001238/**
1239 * register_sysctl_table_path - register a sysctl table hierarchy
1240 * @path: The path to the directory the sysctl table is in.
1241 * @table: the top-level table structure
1242 *
1243 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1244 * array. A completely 0 filled entry terminates the table.
1245 *
1246 * See __register_sysctl_paths for more details.
1247 */
1248struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1249 struct ctl_table *table)
1250{
1251 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1252 path, table);
1253}
1254EXPORT_SYMBOL(register_sysctl_paths);
1255
1256/**
1257 * register_sysctl_table - register a sysctl table hierarchy
1258 * @table: the top-level table structure
1259 *
1260 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1261 * array. A completely 0 filled entry terminates the table.
1262 *
1263 * See register_sysctl_paths for more details.
1264 */
1265struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1266{
1267 static const struct ctl_path null_path[] = { {} };
1268
1269 return register_sysctl_paths(null_path, table);
1270}
1271EXPORT_SYMBOL(register_sysctl_table);
1272
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001273static void drop_sysctl_table(struct ctl_table_header *header)
1274{
1275 if (--header->nreg)
1276 return;
1277
1278 start_unregistering(header);
1279 if (!--header->parent->count) {
1280 WARN_ON(1);
1281 kfree_rcu(header->parent, rcu);
1282 }
1283 if (!--header->count)
1284 kfree_rcu(header, rcu);
1285}
1286
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001287/**
1288 * unregister_sysctl_table - unregister a sysctl table hierarchy
1289 * @header: the header returned from register_sysctl_table
1290 *
1291 * Unregisters the sysctl table and all children. proc entries may not
1292 * actually be removed until they are no longer used by anyone.
1293 */
1294void unregister_sysctl_table(struct ctl_table_header * header)
1295{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001296 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001297 might_sleep();
1298
1299 if (header == NULL)
1300 return;
1301
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001302 nr_subheaders = count_subheaders(header->ctl_table_arg);
1303 if (unlikely(nr_subheaders > 1)) {
1304 struct ctl_table_header **subheaders;
1305 int i;
1306
1307 subheaders = (struct ctl_table_header **)(header + 1);
1308 for (i = nr_subheaders -1; i >= 0; i--) {
1309 struct ctl_table_header *subh = subheaders[i];
1310 struct ctl_table *table = subh->ctl_table_arg;
1311 unregister_sysctl_table(subh);
1312 kfree(table);
1313 }
1314 kfree(header);
1315 return;
1316 }
1317
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001318 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001319 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001320 spin_unlock(&sysctl_lock);
1321}
1322EXPORT_SYMBOL(unregister_sysctl_table);
1323
1324void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001325 int (*is_seen)(struct ctl_table_set *))
1326{
1327 INIT_LIST_HEAD(&p->list);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001328 p->is_seen = is_seen;
1329}
1330
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001331void retire_sysctl_set(struct ctl_table_set *set)
1332{
1333 WARN_ON(!list_empty(&set->list));
1334}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001335
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001336int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001337{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001338 struct proc_dir_entry *proc_sys_root;
1339
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001340 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001341 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1342 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001343 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001344
1345 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001346}