blob: 15444850b3e8e839ff77334e0c36d55e40a9b877 [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 Viro9043476f2008-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. Biederman1f87f0b2012-01-06 04:07:15 -080028static 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
Al Viro9043476f2008-07-15 08:54:06 -0400221static struct inode *proc_sys_make_inode(struct super_block *sb,
222 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800223{
224 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400225 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800226
Al Viro9043476f2008-07-15 08:54:06 -0400227 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800228 if (!inode)
229 goto out;
230
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400231 inode->i_ino = get_next_ino();
232
Al Viro9043476f2008-07-15 08:54:06 -0400233 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800234 ei = PROC_I(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400235 ei->sysctl = head;
236 ei->sysctl_entry = table;
237
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800238 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro9043476f2008-07-15 08:54:06 -0400239 inode->i_mode = table->mode;
240 if (!table->child) {
241 inode->i_mode |= S_IFREG;
242 inode->i_op = &proc_sys_inode_operations;
243 inode->i_fop = &proc_sys_file_operations;
244 } else {
245 inode->i_mode |= S_IFDIR;
Al Viro9043476f2008-07-15 08:54:06 -0400246 inode->i_op = &proc_sys_dir_operations;
247 inode->i_fop = &proc_sys_dir_file_operations;
248 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800249out:
250 return inode;
251}
252
Al Viro9043476f2008-07-15 08:54:06 -0400253static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800254{
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700255 for ( ; p->procname; p++) {
Lucas De Marchi36885d72011-06-10 02:36:05 -0300256 if (strlen(p->procname) != name->len)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800257 continue;
258
Lucas De Marchi36885d72011-06-10 02:36:05 -0300259 if (memcmp(p->procname, name->name, name->len) != 0)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800260 continue;
261
262 /* I have a match */
Al Viro9043476f2008-07-15 08:54:06 -0400263 return p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800264 }
265 return NULL;
266}
267
Adrian Bunk81324362008-10-03 00:33:54 +0400268static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800269{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800270 struct ctl_table_header *head = PROC_I(inode)->sysctl;
271 if (!head)
272 head = &root_table_header;
273 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800274}
275
276static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
277 struct nameidata *nd)
278{
Al Viro9043476f2008-07-15 08:54:06 -0400279 struct ctl_table_header *head = grab_header(dir);
280 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
281 struct ctl_table_header *h = NULL;
282 struct qstr *name = &dentry->d_name;
283 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800284 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400285 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800286
Al Viro9043476f2008-07-15 08:54:06 -0400287 if (IS_ERR(head))
288 return ERR_CAST(head);
289
290 if (table && !table->child) {
291 WARN_ON(1);
292 goto out;
293 }
294
295 table = table ? table->child : head->ctl_table;
296
297 p = find_in_table(table, name);
298 if (!p) {
299 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
300 if (h->attached_to != table)
301 continue;
302 p = find_in_table(h->attached_by, name);
303 if (p)
304 break;
305 }
306 }
307
308 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800309 goto out;
310
311 err = ERR_PTR(-ENOMEM);
Al Viro9043476f2008-07-15 08:54:06 -0400312 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
313 if (h)
314 sysctl_head_finish(h);
315
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800316 if (!inode)
317 goto out;
318
319 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100320 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800321 d_add(dentry, inode);
322
323out:
324 sysctl_head_finish(head);
325 return err;
326}
327
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700328static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
329 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800330{
Al Viro9043476f2008-07-15 08:54:06 -0400331 struct inode *inode = filp->f_path.dentry->d_inode;
332 struct ctl_table_header *head = grab_header(inode);
333 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100334 ssize_t error;
335 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800336
Al Viro9043476f2008-07-15 08:54:06 -0400337 if (IS_ERR(head))
338 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800339
340 /*
341 * At this point we know that the sysctl was not unregistered
342 * and won't be until we finish.
343 */
344 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700345 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800346 goto out;
347
Al Viro9043476f2008-07-15 08:54:06 -0400348 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
349 error = -EINVAL;
350 if (!table->proc_handler)
351 goto out;
352
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800353 /* careful: calling conventions are nasty here */
354 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700355 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800356 if (!error)
357 error = res;
358out:
359 sysctl_head_finish(head);
360
361 return error;
362}
363
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700364static ssize_t proc_sys_read(struct file *filp, char __user *buf,
365 size_t count, loff_t *ppos)
366{
367 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
368}
369
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800370static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
371 size_t count, loff_t *ppos)
372{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700373 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800374}
375
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700376static int proc_sys_open(struct inode *inode, struct file *filp)
377{
378 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
379
380 if (table->poll)
381 filp->private_data = proc_sys_poll_event(table->poll);
382
383 return 0;
384}
385
386static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
387{
388 struct inode *inode = filp->f_path.dentry->d_inode;
389 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
390 unsigned long event = (unsigned long)filp->private_data;
391 unsigned int ret = DEFAULT_POLLMASK;
392
393 if (!table->proc_handler)
394 goto out;
395
396 if (!table->poll)
397 goto out;
398
399 poll_wait(filp, &table->poll->wait, wait);
400
401 if (event != atomic_read(&table->poll->event)) {
402 filp->private_data = proc_sys_poll_event(table->poll);
403 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
404 }
405
406out:
407 return ret;
408}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800409
410static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro9043476f2008-07-15 08:54:06 -0400411 filldir_t filldir,
412 struct ctl_table_header *head,
413 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800414{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800415 struct dentry *child, *dir = filp->f_path.dentry;
416 struct inode *inode;
417 struct qstr qname;
418 ino_t ino = 0;
419 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800420
421 qname.name = table->procname;
422 qname.len = strlen(table->procname);
423 qname.hash = full_name_hash(qname.name, qname.len);
424
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800425 child = d_lookup(dir, &qname);
426 if (!child) {
Al Viro9043476f2008-07-15 08:54:06 -0400427 child = d_alloc(dir, &qname);
428 if (child) {
429 inode = proc_sys_make_inode(dir->d_sb, head, table);
430 if (!inode) {
431 dput(child);
432 return -ENOMEM;
433 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100434 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro9043476f2008-07-15 08:54:06 -0400435 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800436 }
Al Viro9043476f2008-07-15 08:54:06 -0400437 } else {
438 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800439 }
440 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800441 inode = child->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400442 ino = inode->i_ino;
443 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800444 dput(child);
Al Viro9043476f2008-07-15 08:54:06 -0400445 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
446}
447
448static int scan(struct ctl_table_header *head, ctl_table *table,
449 unsigned long *pos, struct file *file,
450 void *dirent, filldir_t filldir)
451{
452
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700453 for (; table->procname; table++, (*pos)++) {
Al Viro9043476f2008-07-15 08:54:06 -0400454 int res;
455
Al Viro9043476f2008-07-15 08:54:06 -0400456 if (*pos < file->f_pos)
457 continue;
458
459 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
460 if (res)
461 return res;
462
463 file->f_pos = *pos + 1;
464 }
465 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800466}
467
468static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
469{
Al Viro9043476f2008-07-15 08:54:06 -0400470 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800471 struct inode *inode = dentry->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400472 struct ctl_table_header *head = grab_header(inode);
473 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
474 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800475 unsigned long pos;
Al Viro9043476f2008-07-15 08:54:06 -0400476 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800477
Al Viro9043476f2008-07-15 08:54:06 -0400478 if (IS_ERR(head))
479 return PTR_ERR(head);
480
481 if (table && !table->child) {
482 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800483 goto out;
Al Viro9043476f2008-07-15 08:54:06 -0400484 }
485
486 table = table ? table->child : head->ctl_table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800487
488 ret = 0;
489 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
490 if (filp->f_pos == 0) {
491 if (filldir(dirent, ".", 1, filp->f_pos,
492 inode->i_ino, DT_DIR) < 0)
493 goto out;
494 filp->f_pos++;
495 }
496 if (filp->f_pos == 1) {
497 if (filldir(dirent, "..", 2, filp->f_pos,
498 parent_ino(dentry), DT_DIR) < 0)
499 goto out;
500 filp->f_pos++;
501 }
502 pos = 2;
503
Al Viro9043476f2008-07-15 08:54:06 -0400504 ret = scan(head, table, &pos, filp, dirent, filldir);
505 if (ret)
506 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800507
Al Viro9043476f2008-07-15 08:54:06 -0400508 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
509 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800510 continue;
Al Viro9043476f2008-07-15 08:54:06 -0400511 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
512 if (ret) {
513 sysctl_head_finish(h);
514 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800515 }
516 }
517 ret = 1;
518out:
519 sysctl_head_finish(head);
520 return ret;
521}
522
Al Viro10556cb2011-06-20 19:28:19 -0400523static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800524{
525 /*
526 * sysctl entries that are not writeable,
527 * are _NOT_ writeable, capabilities or not.
528 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200529 struct ctl_table_header *head;
530 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800531 int error;
532
Miklos Szeredif696a362008-07-31 13:41:58 +0200533 /* Executable files are not allowed under /proc/sys/ */
534 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
535 return -EACCES;
536
537 head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400538 if (IS_ERR(head))
539 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800540
Miklos Szeredif696a362008-07-31 13:41:58 +0200541 table = PROC_I(inode)->sysctl_entry;
Al Viro9043476f2008-07-15 08:54:06 -0400542 if (!table) /* global root - r-xr-xr-x */
543 error = mask & MAY_WRITE ? -EACCES : 0;
544 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400545 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800546
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800547 sysctl_head_finish(head);
548 return error;
549}
550
551static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
552{
553 struct inode *inode = dentry->d_inode;
554 int error;
555
556 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
557 return -EPERM;
558
559 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200560 if (error)
561 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800562
Christoph Hellwig10257742010-06-04 11:30:02 +0200563 if ((attr->ia_valid & ATTR_SIZE) &&
564 attr->ia_size != i_size_read(inode)) {
565 error = vmtruncate(inode, attr->ia_size);
566 if (error)
567 return error;
568 }
569
570 setattr_copy(inode, attr);
571 mark_inode_dirty(inode);
572 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800573}
574
Al Viro9043476f2008-07-15 08:54:06 -0400575static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
576{
577 struct inode *inode = dentry->d_inode;
578 struct ctl_table_header *head = grab_header(inode);
579 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
580
581 if (IS_ERR(head))
582 return PTR_ERR(head);
583
584 generic_fillattr(inode, stat);
585 if (table)
586 stat->mode = (stat->mode & S_IFMT) | table->mode;
587
588 sysctl_head_finish(head);
589 return 0;
590}
591
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800592static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700593 .open = proc_sys_open,
594 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800595 .read = proc_sys_read,
596 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200597 .llseek = default_llseek,
Al Viro9043476f2008-07-15 08:54:06 -0400598};
599
600static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700601 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800602 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200603 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800604};
605
Jan Engelhardt03a44822008-02-08 04:21:19 -0800606static const struct inode_operations proc_sys_inode_operations = {
Al Viro9043476f2008-07-15 08:54:06 -0400607 .permission = proc_sys_permission,
608 .setattr = proc_sys_setattr,
609 .getattr = proc_sys_getattr,
610};
611
612static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800613 .lookup = proc_sys_lookup,
614 .permission = proc_sys_permission,
615 .setattr = proc_sys_setattr,
Al Viro9043476f2008-07-15 08:54:06 -0400616 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800617};
618
619static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
620{
Nick Piggin34286d62011-01-07 17:49:57 +1100621 if (nd->flags & LOOKUP_RCU)
622 return -ECHILD;
Al Viro9043476f2008-07-15 08:54:06 -0400623 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
624}
625
Nick Pigginfe15ce42011-01-07 17:49:23 +1100626static int proc_sys_delete(const struct dentry *dentry)
Al Viro9043476f2008-07-15 08:54:06 -0400627{
628 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
629}
630
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800631static int sysctl_is_seen(struct ctl_table_header *p)
632{
633 struct ctl_table_set *set = p->set;
634 int res;
635 spin_lock(&sysctl_lock);
636 if (p->unregistering)
637 res = 0;
638 else if (!set->is_seen)
639 res = 1;
640 else
641 res = set->is_seen(set);
642 spin_unlock(&sysctl_lock);
643 return res;
644}
645
Nick Piggin621e1552011-01-07 17:49:27 +1100646static int proc_sys_compare(const struct dentry *parent,
647 const struct inode *pinode,
648 const struct dentry *dentry, const struct inode *inode,
649 unsigned int len, const char *str, const struct qstr *name)
Al Viro9043476f2008-07-15 08:54:06 -0400650{
Al Virodfef6dcd32011-03-08 01:25:28 -0500651 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100652 /* Although proc doesn't have negative dentries, rcu-walk means
653 * that inode here can be NULL */
Al Virodfef6dcd32011-03-08 01:25:28 -0500654 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100655 if (!inode)
Al Virodfef6dcd32011-03-08 01:25:28 -0500656 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100657 if (name->len != len)
Al Viro9043476f2008-07-15 08:54:06 -0400658 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100659 if (memcmp(name->name, str, len))
Al Viro9043476f2008-07-15 08:54:06 -0400660 return 1;
Al Virodfef6dcd32011-03-08 01:25:28 -0500661 head = rcu_dereference(PROC_I(inode)->sysctl);
662 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800663}
664
Al Virod72f71e2009-02-20 05:58:47 +0000665static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800666 .d_revalidate = proc_sys_revalidate,
Al Viro9043476f2008-07-15 08:54:06 -0400667 .d_delete = proc_sys_delete,
668 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800669};
670
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800671static struct ctl_table *is_branch_in(struct ctl_table *branch,
672 struct ctl_table *table)
673{
674 struct ctl_table *p;
675 const char *s = branch->procname;
676
677 /* branch should have named subdirectory as its first element */
678 if (!s || !branch->child)
679 return NULL;
680
681 /* ... and nothing else */
682 if (branch[1].procname)
683 return NULL;
684
685 /* table should contain subdirectory with the same name */
686 for (p = table; p->procname; p++) {
687 if (!p->child)
688 continue;
689 if (p->procname && strcmp(p->procname, s) == 0)
690 return p;
691 }
692 return NULL;
693}
694
695/* see if attaching q to p would be an improvement */
696static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
697{
698 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
699 struct ctl_table *next;
700 int is_better = 0;
701 int not_in_parent = !p->attached_by;
702
703 while ((next = is_branch_in(by, to)) != NULL) {
704 if (by == q->attached_by)
705 is_better = 1;
706 if (to == p->attached_by)
707 not_in_parent = 1;
708 by = by->child;
709 to = next->child;
710 }
711
712 if (is_better && not_in_parent) {
713 q->attached_by = by;
714 q->attached_to = to;
715 q->parent = p;
716 }
717}
718
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800719static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
720 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800721{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800722 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800723 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800724
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800725 for (entry = old; entry->procname; entry++) {
726 for (test = table; test->procname; test++) {
727 if (strcmp(entry->procname, test->procname) == 0) {
728 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
729 path, test->procname);
730 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800731 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800732 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800733 }
734 return error;
735}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800736
737static int sysctl_check_dups(struct nsproxy *namespaces,
738 struct ctl_table_header *header,
739 const char *path, struct ctl_table *table)
740{
741 struct ctl_table_root *root;
742 struct ctl_table_set *set;
743 struct ctl_table_header *dir_head, *head;
744 struct ctl_table *dir_table;
745 int error = 0;
746
747 /* No dups if we are the only member of our directory */
748 if (header->attached_by != table)
749 return 0;
750
751 dir_head = header->parent;
752 dir_table = header->attached_to;
753
754 error = sysctl_check_table_dups(path, dir_table, table);
755
756 root = &sysctl_table_root;
757 do {
758 set = lookup_header_set(root, namespaces);
759
760 list_for_each_entry(head, &set->list, ctl_entry) {
761 if (head->unregistering)
762 continue;
763 if (head->attached_to != dir_table)
764 continue;
765 error = sysctl_check_table_dups(path, head->attached_by,
766 table);
767 }
768 root = list_entry(root->root_list.next,
769 struct ctl_table_root, root_list);
770 } while (root != &sysctl_table_root);
771 return error;
772}
773
774static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
775{
776 struct va_format vaf;
777 va_list args;
778
779 va_start(args, fmt);
780 vaf.fmt = fmt;
781 vaf.va = &args;
782
783 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
784 path, table->procname, &vaf);
785
786 va_end(args);
787 return -EINVAL;
788}
789
790static int sysctl_check_table(const char *path, struct ctl_table *table)
791{
792 int err = 0;
793 for (; table->procname; table++) {
794 if (table->child)
795 err = sysctl_err(path, table, "Not a file");
796
797 if ((table->proc_handler == proc_dostring) ||
798 (table->proc_handler == proc_dointvec) ||
799 (table->proc_handler == proc_dointvec_minmax) ||
800 (table->proc_handler == proc_dointvec_jiffies) ||
801 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
802 (table->proc_handler == proc_dointvec_ms_jiffies) ||
803 (table->proc_handler == proc_doulongvec_minmax) ||
804 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
805 if (!table->data)
806 err = sysctl_err(path, table, "No data");
807 if (!table->maxlen)
808 err = sysctl_err(path, table, "No maxlen");
809 }
810 if (!table->proc_handler)
811 err = sysctl_err(path, table, "No proc_handler");
812
813 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
814 err = sysctl_err(path, table, "bogus .mode 0%o",
815 table->mode);
816 }
817 return err;
818}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800819
820/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800821 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800822 * @root: List of sysctl headers to register on
823 * @namespaces: Data to compute which lists of sysctl entries are visible
824 * @path: The path to the directory the sysctl table is in.
825 * @table: the top-level table structure
826 *
827 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
828 * array. A completely 0 filled entry terminates the table.
829 *
830 * The members of the &struct ctl_table structure are used as follows:
831 *
832 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
833 * enter a sysctl file
834 *
835 * data - a pointer to data for use by proc_handler
836 *
837 * maxlen - the maximum size in bytes of the data
838 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800839 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800840 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800841 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800842 *
843 * proc_handler - the text handler routine (described below)
844 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800845 * extra1, extra2 - extra pointers usable by the proc handler routines
846 *
847 * Leaf nodes in the sysctl tree will be represented by a single file
848 * under /proc; non-leaf nodes will be represented by directories.
849 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800850 * There must be a proc_handler routine for any terminal nodes.
851 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800852 *
853 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
854 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
855 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
856 *
857 * It is the handler's job to read the input buffer from user memory
858 * and process it. The handler should return 0 on success.
859 *
860 * This routine returns %NULL on a failure to register, and a pointer
861 * to the table header on success.
862 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800863struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800864 struct ctl_table_root *root,
865 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800866 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800867{
868 struct ctl_table_header *header;
869 struct ctl_table *new, **prevp;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800870 const char *name, *nextname;
871 unsigned int npath = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800872 struct ctl_table_set *set;
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800873 size_t path_bytes = 0;
874 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800875
876 /* Count the path components */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800877 for (name = path; name; name = nextname) {
878 int namelen;
879 nextname = strchr(name, '/');
880 if (nextname) {
881 namelen = nextname - name;
882 nextname++;
883 } else {
884 namelen = strlen(name);
885 }
886 if (namelen == 0)
887 continue;
888 path_bytes += namelen + 1;
889 npath++;
890 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800891
892 /*
893 * For each path component, allocate a 2-element ctl_table array.
894 * The first array element will be filled with the sysctl entry
895 * for this, the second will be the sentinel (procname == 0).
896 *
897 * We allocate everything in one go so that we don't have to
898 * worry about freeing additional memory in unregister_sysctl_table.
899 */
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800900 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800901 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
902 if (!header)
903 return NULL;
904
905 new = (struct ctl_table *) (header + 1);
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800906 new_name = (char *)(new + (2 * npath));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800907
908 /* Now connect the dots */
909 prevp = &header->ctl_table;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800910 for (name = path; name; name = nextname) {
911 int namelen;
912 nextname = strchr(name, '/');
913 if (nextname) {
914 namelen = nextname - name;
915 nextname++;
916 } else {
917 namelen = strlen(name);
918 }
919 if (namelen == 0)
920 continue;
921 memcpy(new_name, name, namelen);
922 new_name[namelen] = '\0';
923
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800924 new->procname = new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800925 new->mode = 0555;
926
927 *prevp = new;
928 prevp = &new->child;
929
930 new += 2;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800931 new_name += namelen + 1;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800932 }
933 *prevp = table;
934 header->ctl_table_arg = table;
935
936 INIT_LIST_HEAD(&header->ctl_entry);
937 header->used = 0;
938 header->unregistering = NULL;
939 header->root = root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800940 header->count = 1;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800941 if (sysctl_check_table(path, table))
942 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -0800943
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800944 spin_lock(&sysctl_lock);
945 header->set = lookup_header_set(root, namespaces);
946 header->attached_by = header->ctl_table;
947 header->attached_to = root_table;
948 header->parent = &root_table_header;
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800949 set = header->set;
950 root = header->root;
951 for (;;) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800952 struct ctl_table_header *p;
953 list_for_each_entry(p, &set->list, ctl_entry) {
954 if (p->unregistering)
955 continue;
956 try_attach(p, header);
957 }
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800958 if (root == &sysctl_table_root)
959 break;
960 root = list_entry(root->root_list.prev,
961 struct ctl_table_root, root_list);
962 set = lookup_header_set(root, namespaces);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800963 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800964 if (sysctl_check_dups(namespaces, header, path, table))
965 goto fail_locked;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800966 header->parent->count++;
967 list_add_tail(&header->ctl_entry, &header->set->list);
968 spin_unlock(&sysctl_lock);
969
970 return header;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800971fail_locked:
972 spin_unlock(&sysctl_lock);
973fail:
974 kfree(header);
975 dump_stack();
976 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800977}
978
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800979static char *append_path(const char *path, char *pos, const char *name)
980{
981 int namelen;
982 namelen = strlen(name);
983 if (((pos - path) + namelen + 2) >= PATH_MAX)
984 return NULL;
985 memcpy(pos, name, namelen);
986 pos[namelen] = '/';
987 pos[namelen + 1] = '\0';
988 pos += namelen + 1;
989 return pos;
990}
991
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800992static int count_subheaders(struct ctl_table *table)
993{
994 int has_files = 0;
995 int nr_subheaders = 0;
996 struct ctl_table *entry;
997
998 /* special case: no directory and empty directory */
999 if (!table || !table->procname)
1000 return 1;
1001
1002 for (entry = table; entry->procname; entry++) {
1003 if (entry->child)
1004 nr_subheaders += count_subheaders(entry->child);
1005 else
1006 has_files = 1;
1007 }
1008 return nr_subheaders + has_files;
1009}
1010
1011static int register_leaf_sysctl_tables(const char *path, char *pos,
1012 struct ctl_table_header ***subheader,
1013 struct ctl_table_root *root, struct nsproxy *namespaces,
1014 struct ctl_table *table)
1015{
1016 struct ctl_table *ctl_table_arg = NULL;
1017 struct ctl_table *entry, *files;
1018 int nr_files = 0;
1019 int nr_dirs = 0;
1020 int err = -ENOMEM;
1021
1022 for (entry = table; entry->procname; entry++) {
1023 if (entry->child)
1024 nr_dirs++;
1025 else
1026 nr_files++;
1027 }
1028
1029 files = table;
1030 /* If there are mixed files and directories we need a new table */
1031 if (nr_dirs && nr_files) {
1032 struct ctl_table *new;
1033 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1034 GFP_KERNEL);
1035 if (!files)
1036 goto out;
1037
1038 ctl_table_arg = files;
1039 for (new = files, entry = table; entry->procname; entry++) {
1040 if (entry->child)
1041 continue;
1042 *new = *entry;
1043 new++;
1044 }
1045 }
1046
1047 /* Register everything except a directory full of subdirectories */
1048 if (nr_files || !nr_dirs) {
1049 struct ctl_table_header *header;
1050 header = __register_sysctl_table(root, namespaces, path, files);
1051 if (!header) {
1052 kfree(ctl_table_arg);
1053 goto out;
1054 }
1055
1056 /* Remember if we need to free the file table */
1057 header->ctl_table_arg = ctl_table_arg;
1058 **subheader = header;
1059 (*subheader)++;
1060 }
1061
1062 /* Recurse into the subdirectories. */
1063 for (entry = table; entry->procname; entry++) {
1064 char *child_pos;
1065
1066 if (!entry->child)
1067 continue;
1068
1069 err = -ENAMETOOLONG;
1070 child_pos = append_path(path, pos, entry->procname);
1071 if (!child_pos)
1072 goto out;
1073
1074 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1075 root, namespaces, entry->child);
1076 pos[0] = '\0';
1077 if (err)
1078 goto out;
1079 }
1080 err = 0;
1081out:
1082 /* On failure our caller will unregister all registered subheaders */
1083 return err;
1084}
1085
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001086/**
1087 * __register_sysctl_paths - register a sysctl table hierarchy
1088 * @root: List of sysctl headers to register on
1089 * @namespaces: Data to compute which lists of sysctl entries are visible
1090 * @path: The path to the directory the sysctl table is in.
1091 * @table: the top-level table structure
1092 *
1093 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1094 * array. A completely 0 filled entry terminates the table.
1095 *
1096 * See __register_sysctl_table for more details.
1097 */
1098struct ctl_table_header *__register_sysctl_paths(
1099 struct ctl_table_root *root,
1100 struct nsproxy *namespaces,
1101 const struct ctl_path *path, struct ctl_table *table)
1102{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001103 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001104 int nr_subheaders = count_subheaders(table);
1105 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001106 const struct ctl_path *component;
1107 char *new_path, *pos;
1108
1109 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1110 if (!new_path)
1111 return NULL;
1112
1113 pos[0] = '\0';
1114 for (component = path; component->procname; component++) {
1115 pos = append_path(new_path, pos, component->procname);
1116 if (!pos)
1117 goto out;
1118 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001119 while (table->procname && table->child && !table[1].procname) {
1120 pos = append_path(new_path, pos, table->procname);
1121 if (!pos)
1122 goto out;
1123 table = table->child;
1124 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001125 if (nr_subheaders == 1) {
1126 header = __register_sysctl_table(root, namespaces, new_path, table);
1127 if (header)
1128 header->ctl_table_arg = ctl_table_arg;
1129 } else {
1130 header = kzalloc(sizeof(*header) +
1131 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1132 if (!header)
1133 goto out;
1134
1135 subheaders = (struct ctl_table_header **) (header + 1);
1136 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001137 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001138
1139 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1140 root, namespaces, table))
1141 goto err_register_leaves;
1142 }
1143
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001144out:
1145 kfree(new_path);
1146 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001147
1148err_register_leaves:
1149 while (subheader > subheaders) {
1150 struct ctl_table_header *subh = *(--subheader);
1151 struct ctl_table *table = subh->ctl_table_arg;
1152 unregister_sysctl_table(subh);
1153 kfree(table);
1154 }
1155 kfree(header);
1156 header = NULL;
1157 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001158}
1159
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001160/**
1161 * register_sysctl_table_path - register a sysctl table hierarchy
1162 * @path: The path to the directory the sysctl table is in.
1163 * @table: the top-level table structure
1164 *
1165 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1166 * array. A completely 0 filled entry terminates the table.
1167 *
1168 * See __register_sysctl_paths for more details.
1169 */
1170struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1171 struct ctl_table *table)
1172{
1173 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1174 path, table);
1175}
1176EXPORT_SYMBOL(register_sysctl_paths);
1177
1178/**
1179 * register_sysctl_table - register a sysctl table hierarchy
1180 * @table: the top-level table structure
1181 *
1182 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1183 * array. A completely 0 filled entry terminates the table.
1184 *
1185 * See register_sysctl_paths for more details.
1186 */
1187struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1188{
1189 static const struct ctl_path null_path[] = { {} };
1190
1191 return register_sysctl_paths(null_path, table);
1192}
1193EXPORT_SYMBOL(register_sysctl_table);
1194
1195/**
1196 * unregister_sysctl_table - unregister a sysctl table hierarchy
1197 * @header: the header returned from register_sysctl_table
1198 *
1199 * Unregisters the sysctl table and all children. proc entries may not
1200 * actually be removed until they are no longer used by anyone.
1201 */
1202void unregister_sysctl_table(struct ctl_table_header * header)
1203{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001204 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001205 might_sleep();
1206
1207 if (header == NULL)
1208 return;
1209
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001210 nr_subheaders = count_subheaders(header->ctl_table_arg);
1211 if (unlikely(nr_subheaders > 1)) {
1212 struct ctl_table_header **subheaders;
1213 int i;
1214
1215 subheaders = (struct ctl_table_header **)(header + 1);
1216 for (i = nr_subheaders -1; i >= 0; i--) {
1217 struct ctl_table_header *subh = subheaders[i];
1218 struct ctl_table *table = subh->ctl_table_arg;
1219 unregister_sysctl_table(subh);
1220 kfree(table);
1221 }
1222 kfree(header);
1223 return;
1224 }
1225
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001226 spin_lock(&sysctl_lock);
1227 start_unregistering(header);
1228 if (!--header->parent->count) {
1229 WARN_ON(1);
1230 kfree_rcu(header->parent, rcu);
1231 }
1232 if (!--header->count)
1233 kfree_rcu(header, rcu);
1234 spin_unlock(&sysctl_lock);
1235}
1236EXPORT_SYMBOL(unregister_sysctl_table);
1237
1238void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001239 int (*is_seen)(struct ctl_table_set *))
1240{
1241 INIT_LIST_HEAD(&p->list);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001242 p->is_seen = is_seen;
1243}
1244
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001245void retire_sysctl_set(struct ctl_table_set *set)
1246{
1247 WARN_ON(!list_empty(&set->list));
1248}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001249
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001250int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001251{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001252 struct proc_dir_entry *proc_sys_root;
1253
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001254 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro9043476f2008-07-15 08:54:06 -04001255 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1256 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001257 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001258
1259 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001260}