blob: 13faa48c467e91cc7159145ac491c908d64472ea [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. 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,
Eric W. Biederman938aaa42012-01-09 17:24:30 -080032 .nreg = 1,
33 .ctl_table = root_table,
34 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080035 .root = &sysctl_table_root,
36 .set = &sysctl_table_root.default_set,
37};
38static struct ctl_table_root sysctl_table_root = {
39 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
40 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
41};
42
43static DEFINE_SPINLOCK(sysctl_lock);
44
45/* called under sysctl_lock */
46static int use_table(struct ctl_table_header *p)
47{
48 if (unlikely(p->unregistering))
49 return 0;
50 p->used++;
51 return 1;
52}
53
54/* called under sysctl_lock */
55static void unuse_table(struct ctl_table_header *p)
56{
57 if (!--p->used)
58 if (unlikely(p->unregistering))
59 complete(p->unregistering);
60}
61
62/* called under sysctl_lock, will reacquire if has to wait */
63static void start_unregistering(struct ctl_table_header *p)
64{
65 /*
66 * if p->used is 0, nobody will ever touch that entry again;
67 * we'll eliminate all paths to it before dropping sysctl_lock
68 */
69 if (unlikely(p->used)) {
70 struct completion wait;
71 init_completion(&wait);
72 p->unregistering = &wait;
73 spin_unlock(&sysctl_lock);
74 wait_for_completion(&wait);
75 spin_lock(&sysctl_lock);
76 } else {
77 /* anything non-NULL; we'll never dereference it */
78 p->unregistering = ERR_PTR(-EINVAL);
79 }
80 /*
81 * do not remove from the list until nobody holds it; walking the
82 * list in do_sysctl() relies on that.
83 */
84 list_del_init(&p->ctl_entry);
85}
86
87static void sysctl_head_get(struct ctl_table_header *head)
88{
89 spin_lock(&sysctl_lock);
90 head->count++;
91 spin_unlock(&sysctl_lock);
92}
93
94void sysctl_head_put(struct ctl_table_header *head)
95{
96 spin_lock(&sysctl_lock);
97 if (!--head->count)
98 kfree_rcu(head, rcu);
99 spin_unlock(&sysctl_lock);
100}
101
102static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
103{
104 if (!head)
105 BUG();
106 spin_lock(&sysctl_lock);
107 if (!use_table(head))
108 head = ERR_PTR(-ENOENT);
109 spin_unlock(&sysctl_lock);
110 return head;
111}
112
113static void sysctl_head_finish(struct ctl_table_header *head)
114{
115 if (!head)
116 return;
117 spin_lock(&sysctl_lock);
118 unuse_table(head);
119 spin_unlock(&sysctl_lock);
120}
121
122static struct ctl_table_set *
123lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
124{
125 struct ctl_table_set *set = &root->default_set;
126 if (root->lookup)
127 set = root->lookup(root, namespaces);
128 return set;
129}
130
131static struct list_head *
132lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
133{
134 struct ctl_table_set *set = lookup_header_set(root, namespaces);
135 return &set->list;
136}
137
138static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
139 struct ctl_table_header *prev)
140{
141 struct ctl_table_root *root;
142 struct list_head *header_list;
143 struct ctl_table_header *head;
144 struct list_head *tmp;
145
146 spin_lock(&sysctl_lock);
147 if (prev) {
148 head = prev;
149 tmp = &prev->ctl_entry;
150 unuse_table(prev);
151 goto next;
152 }
153 tmp = &root_table_header.ctl_entry;
154 for (;;) {
155 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
156
157 if (!use_table(head))
158 goto next;
159 spin_unlock(&sysctl_lock);
160 return head;
161 next:
162 root = head->root;
163 tmp = tmp->next;
164 header_list = lookup_header_list(root, namespaces);
165 if (tmp != header_list)
166 continue;
167
168 do {
169 root = list_entry(root->root_list.next,
170 struct ctl_table_root, root_list);
171 if (root == &sysctl_table_root)
172 goto out;
173 header_list = lookup_header_list(root, namespaces);
174 } while (list_empty(header_list));
175 tmp = header_list->next;
176 }
177out:
178 spin_unlock(&sysctl_lock);
179 return NULL;
180}
181
182static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
183{
184 return __sysctl_head_next(current->nsproxy, prev);
185}
186
187void register_sysctl_root(struct ctl_table_root *root)
188{
189 spin_lock(&sysctl_lock);
190 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
191 spin_unlock(&sysctl_lock);
192}
193
194/*
195 * sysctl_perm does NOT grant the superuser all rights automatically, because
196 * some sysctl variables are readonly even to root.
197 */
198
199static int test_perm(int mode, int op)
200{
201 if (!current_euid())
202 mode >>= 6;
203 else if (in_egroup_p(0))
204 mode >>= 3;
205 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
206 return 0;
207 return -EACCES;
208}
209
210static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
211{
212 int mode;
213
214 if (root->permissions)
215 mode = root->permissions(root, current->nsproxy, table);
216 else
217 mode = table->mode;
218
219 return test_perm(mode, op);
220}
221
Al Viro90434762008-07-15 08:54:06 -0400222static struct inode *proc_sys_make_inode(struct super_block *sb,
223 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800224{
225 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400226 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800227
Al Viro90434762008-07-15 08:54:06 -0400228 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800229 if (!inode)
230 goto out;
231
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400232 inode->i_ino = get_next_ino();
233
Al Viro90434762008-07-15 08:54:06 -0400234 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800235 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400236 ei->sysctl = head;
237 ei->sysctl_entry = table;
238
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800239 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400240 inode->i_mode = table->mode;
241 if (!table->child) {
242 inode->i_mode |= S_IFREG;
243 inode->i_op = &proc_sys_inode_operations;
244 inode->i_fop = &proc_sys_file_operations;
245 } else {
246 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400247 inode->i_op = &proc_sys_dir_operations;
248 inode->i_fop = &proc_sys_dir_file_operations;
249 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800250out:
251 return inode;
252}
253
Al Viro90434762008-07-15 08:54:06 -0400254static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800255{
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700256 for ( ; p->procname; p++) {
Lucas De Marchi36885d72011-06-10 02:36:05 -0300257 if (strlen(p->procname) != name->len)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800258 continue;
259
Lucas De Marchi36885d72011-06-10 02:36:05 -0300260 if (memcmp(p->procname, name->name, name->len) != 0)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800261 continue;
262
263 /* I have a match */
Al Viro90434762008-07-15 08:54:06 -0400264 return p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800265 }
266 return NULL;
267}
268
Adrian Bunk81324362008-10-03 00:33:54 +0400269static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800270{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800271 struct ctl_table_header *head = PROC_I(inode)->sysctl;
272 if (!head)
273 head = &root_table_header;
274 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800275}
276
277static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
278 struct nameidata *nd)
279{
Al Viro90434762008-07-15 08:54:06 -0400280 struct ctl_table_header *head = grab_header(dir);
281 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
282 struct ctl_table_header *h = NULL;
283 struct qstr *name = &dentry->d_name;
284 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800285 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400286 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800287
Al Viro90434762008-07-15 08:54:06 -0400288 if (IS_ERR(head))
289 return ERR_CAST(head);
290
291 if (table && !table->child) {
292 WARN_ON(1);
293 goto out;
294 }
295
296 table = table ? table->child : head->ctl_table;
297
298 p = find_in_table(table, name);
299 if (!p) {
300 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
301 if (h->attached_to != table)
302 continue;
303 p = find_in_table(h->attached_by, name);
304 if (p)
305 break;
306 }
307 }
308
309 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800310 goto out;
311
312 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400313 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
314 if (h)
315 sysctl_head_finish(h);
316
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800317 if (!inode)
318 goto out;
319
320 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100321 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800322 d_add(dentry, inode);
323
324out:
325 sysctl_head_finish(head);
326 return err;
327}
328
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700329static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
330 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800331{
Al Viro90434762008-07-15 08:54:06 -0400332 struct inode *inode = filp->f_path.dentry->d_inode;
333 struct ctl_table_header *head = grab_header(inode);
334 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100335 ssize_t error;
336 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800337
Al Viro90434762008-07-15 08:54:06 -0400338 if (IS_ERR(head))
339 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800340
341 /*
342 * At this point we know that the sysctl was not unregistered
343 * and won't be until we finish.
344 */
345 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700346 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800347 goto out;
348
Al Viro90434762008-07-15 08:54:06 -0400349 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
350 error = -EINVAL;
351 if (!table->proc_handler)
352 goto out;
353
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800354 /* careful: calling conventions are nasty here */
355 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700356 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800357 if (!error)
358 error = res;
359out:
360 sysctl_head_finish(head);
361
362 return error;
363}
364
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700365static ssize_t proc_sys_read(struct file *filp, char __user *buf,
366 size_t count, loff_t *ppos)
367{
368 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
369}
370
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800371static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
372 size_t count, loff_t *ppos)
373{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700374 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800375}
376
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700377static int proc_sys_open(struct inode *inode, struct file *filp)
378{
379 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
380
381 if (table->poll)
382 filp->private_data = proc_sys_poll_event(table->poll);
383
384 return 0;
385}
386
387static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
388{
389 struct inode *inode = filp->f_path.dentry->d_inode;
390 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
391 unsigned long event = (unsigned long)filp->private_data;
392 unsigned int ret = DEFAULT_POLLMASK;
393
394 if (!table->proc_handler)
395 goto out;
396
397 if (!table->poll)
398 goto out;
399
400 poll_wait(filp, &table->poll->wait, wait);
401
402 if (event != atomic_read(&table->poll->event)) {
403 filp->private_data = proc_sys_poll_event(table->poll);
404 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
405 }
406
407out:
408 return ret;
409}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800410
411static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400412 filldir_t filldir,
413 struct ctl_table_header *head,
414 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800415{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800416 struct dentry *child, *dir = filp->f_path.dentry;
417 struct inode *inode;
418 struct qstr qname;
419 ino_t ino = 0;
420 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800421
422 qname.name = table->procname;
423 qname.len = strlen(table->procname);
424 qname.hash = full_name_hash(qname.name, qname.len);
425
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800426 child = d_lookup(dir, &qname);
427 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400428 child = d_alloc(dir, &qname);
429 if (child) {
430 inode = proc_sys_make_inode(dir->d_sb, head, table);
431 if (!inode) {
432 dput(child);
433 return -ENOMEM;
434 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100435 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400436 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800437 }
Al Viro90434762008-07-15 08:54:06 -0400438 } else {
439 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800440 }
441 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800442 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400443 ino = inode->i_ino;
444 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800445 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400446 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
447}
448
449static int scan(struct ctl_table_header *head, ctl_table *table,
450 unsigned long *pos, struct file *file,
451 void *dirent, filldir_t filldir)
452{
453
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700454 for (; table->procname; table++, (*pos)++) {
Al Viro90434762008-07-15 08:54:06 -0400455 int res;
456
Al Viro90434762008-07-15 08:54:06 -0400457 if (*pos < file->f_pos)
458 continue;
459
460 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
461 if (res)
462 return res;
463
464 file->f_pos = *pos + 1;
465 }
466 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800467}
468
469static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
470{
Al Viro90434762008-07-15 08:54:06 -0400471 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800472 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400473 struct ctl_table_header *head = grab_header(inode);
474 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
475 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800476 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400477 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800478
Al Viro90434762008-07-15 08:54:06 -0400479 if (IS_ERR(head))
480 return PTR_ERR(head);
481
482 if (table && !table->child) {
483 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800484 goto out;
Al Viro90434762008-07-15 08:54:06 -0400485 }
486
487 table = table ? table->child : head->ctl_table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800488
489 ret = 0;
490 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
491 if (filp->f_pos == 0) {
492 if (filldir(dirent, ".", 1, filp->f_pos,
493 inode->i_ino, DT_DIR) < 0)
494 goto out;
495 filp->f_pos++;
496 }
497 if (filp->f_pos == 1) {
498 if (filldir(dirent, "..", 2, filp->f_pos,
499 parent_ino(dentry), DT_DIR) < 0)
500 goto out;
501 filp->f_pos++;
502 }
503 pos = 2;
504
Al Viro90434762008-07-15 08:54:06 -0400505 ret = scan(head, table, &pos, filp, dirent, filldir);
506 if (ret)
507 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800508
Al Viro90434762008-07-15 08:54:06 -0400509 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
510 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800511 continue;
Al Viro90434762008-07-15 08:54:06 -0400512 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
513 if (ret) {
514 sysctl_head_finish(h);
515 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800516 }
517 }
518 ret = 1;
519out:
520 sysctl_head_finish(head);
521 return ret;
522}
523
Al Viro10556cb2011-06-20 19:28:19 -0400524static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800525{
526 /*
527 * sysctl entries that are not writeable,
528 * are _NOT_ writeable, capabilities or not.
529 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200530 struct ctl_table_header *head;
531 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800532 int error;
533
Miklos Szeredif696a362008-07-31 13:41:58 +0200534 /* Executable files are not allowed under /proc/sys/ */
535 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
536 return -EACCES;
537
538 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400539 if (IS_ERR(head))
540 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800541
Miklos Szeredif696a362008-07-31 13:41:58 +0200542 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400543 if (!table) /* global root - r-xr-xr-x */
544 error = mask & MAY_WRITE ? -EACCES : 0;
545 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400546 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800547
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800548 sysctl_head_finish(head);
549 return error;
550}
551
552static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
553{
554 struct inode *inode = dentry->d_inode;
555 int error;
556
557 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
558 return -EPERM;
559
560 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200561 if (error)
562 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800563
Christoph Hellwig10257742010-06-04 11:30:02 +0200564 if ((attr->ia_valid & ATTR_SIZE) &&
565 attr->ia_size != i_size_read(inode)) {
566 error = vmtruncate(inode, attr->ia_size);
567 if (error)
568 return error;
569 }
570
571 setattr_copy(inode, attr);
572 mark_inode_dirty(inode);
573 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800574}
575
Al Viro90434762008-07-15 08:54:06 -0400576static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
577{
578 struct inode *inode = dentry->d_inode;
579 struct ctl_table_header *head = grab_header(inode);
580 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
581
582 if (IS_ERR(head))
583 return PTR_ERR(head);
584
585 generic_fillattr(inode, stat);
586 if (table)
587 stat->mode = (stat->mode & S_IFMT) | table->mode;
588
589 sysctl_head_finish(head);
590 return 0;
591}
592
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800593static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700594 .open = proc_sys_open,
595 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800596 .read = proc_sys_read,
597 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200598 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400599};
600
601static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700602 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800603 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200604 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800605};
606
Jan Engelhardt03a44822008-02-08 04:21:19 -0800607static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400608 .permission = proc_sys_permission,
609 .setattr = proc_sys_setattr,
610 .getattr = proc_sys_getattr,
611};
612
613static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800614 .lookup = proc_sys_lookup,
615 .permission = proc_sys_permission,
616 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400617 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800618};
619
620static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
621{
Nick Piggin34286d62011-01-07 17:49:57 +1100622 if (nd->flags & LOOKUP_RCU)
623 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400624 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
625}
626
Nick Pigginfe15ce42011-01-07 17:49:23 +1100627static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400628{
629 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
630}
631
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800632static int sysctl_is_seen(struct ctl_table_header *p)
633{
634 struct ctl_table_set *set = p->set;
635 int res;
636 spin_lock(&sysctl_lock);
637 if (p->unregistering)
638 res = 0;
639 else if (!set->is_seen)
640 res = 1;
641 else
642 res = set->is_seen(set);
643 spin_unlock(&sysctl_lock);
644 return res;
645}
646
Nick Piggin621e1552011-01-07 17:49:27 +1100647static int proc_sys_compare(const struct dentry *parent,
648 const struct inode *pinode,
649 const struct dentry *dentry, const struct inode *inode,
650 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400651{
Al Virodfef6dc2011-03-08 01:25:28 -0500652 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100653 /* Although proc doesn't have negative dentries, rcu-walk means
654 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500655 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100656 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500657 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100658 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400659 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100660 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400661 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500662 head = rcu_dereference(PROC_I(inode)->sysctl);
663 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800664}
665
Al Virod72f71e2009-02-20 05:58:47 +0000666static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800667 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400668 .d_delete = proc_sys_delete,
669 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800670};
671
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800672static struct ctl_table *is_branch_in(struct ctl_table *branch,
673 struct ctl_table *table)
674{
675 struct ctl_table *p;
676 const char *s = branch->procname;
677
678 /* branch should have named subdirectory as its first element */
679 if (!s || !branch->child)
680 return NULL;
681
682 /* ... and nothing else */
683 if (branch[1].procname)
684 return NULL;
685
686 /* table should contain subdirectory with the same name */
687 for (p = table; p->procname; p++) {
688 if (!p->child)
689 continue;
690 if (p->procname && strcmp(p->procname, s) == 0)
691 return p;
692 }
693 return NULL;
694}
695
696/* see if attaching q to p would be an improvement */
697static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
698{
699 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
700 struct ctl_table *next;
701 int is_better = 0;
702 int not_in_parent = !p->attached_by;
703
704 while ((next = is_branch_in(by, to)) != NULL) {
705 if (by == q->attached_by)
706 is_better = 1;
707 if (to == p->attached_by)
708 not_in_parent = 1;
709 by = by->child;
710 to = next->child;
711 }
712
713 if (is_better && not_in_parent) {
714 q->attached_by = by;
715 q->attached_to = to;
716 q->parent = p;
717 }
718}
719
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800720static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
721 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800722{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800723 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800724 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800725
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800726 for (entry = old; entry->procname; entry++) {
727 for (test = table; test->procname; test++) {
728 if (strcmp(entry->procname, test->procname) == 0) {
729 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
730 path, test->procname);
731 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800732 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800733 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800734 }
735 return error;
736}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800737
738static int sysctl_check_dups(struct nsproxy *namespaces,
739 struct ctl_table_header *header,
740 const char *path, struct ctl_table *table)
741{
742 struct ctl_table_root *root;
743 struct ctl_table_set *set;
744 struct ctl_table_header *dir_head, *head;
745 struct ctl_table *dir_table;
746 int error = 0;
747
748 /* No dups if we are the only member of our directory */
749 if (header->attached_by != table)
750 return 0;
751
752 dir_head = header->parent;
753 dir_table = header->attached_to;
754
755 error = sysctl_check_table_dups(path, dir_table, table);
756
757 root = &sysctl_table_root;
758 do {
759 set = lookup_header_set(root, namespaces);
760
761 list_for_each_entry(head, &set->list, ctl_entry) {
762 if (head->unregistering)
763 continue;
764 if (head->attached_to != dir_table)
765 continue;
766 error = sysctl_check_table_dups(path, head->attached_by,
767 table);
768 }
769 root = list_entry(root->root_list.next,
770 struct ctl_table_root, root_list);
771 } while (root != &sysctl_table_root);
772 return error;
773}
774
775static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
776{
777 struct va_format vaf;
778 va_list args;
779
780 va_start(args, fmt);
781 vaf.fmt = fmt;
782 vaf.va = &args;
783
784 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
785 path, table->procname, &vaf);
786
787 va_end(args);
788 return -EINVAL;
789}
790
791static int sysctl_check_table(const char *path, struct ctl_table *table)
792{
793 int err = 0;
794 for (; table->procname; table++) {
795 if (table->child)
796 err = sysctl_err(path, table, "Not a file");
797
798 if ((table->proc_handler == proc_dostring) ||
799 (table->proc_handler == proc_dointvec) ||
800 (table->proc_handler == proc_dointvec_minmax) ||
801 (table->proc_handler == proc_dointvec_jiffies) ||
802 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
803 (table->proc_handler == proc_dointvec_ms_jiffies) ||
804 (table->proc_handler == proc_doulongvec_minmax) ||
805 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
806 if (!table->data)
807 err = sysctl_err(path, table, "No data");
808 if (!table->maxlen)
809 err = sysctl_err(path, table, "No maxlen");
810 }
811 if (!table->proc_handler)
812 err = sysctl_err(path, table, "No proc_handler");
813
814 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
815 err = sysctl_err(path, table, "bogus .mode 0%o",
816 table->mode);
817 }
818 return err;
819}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800820
821/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800822 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800823 * @root: List of sysctl headers to register on
824 * @namespaces: Data to compute which lists of sysctl entries are visible
825 * @path: The path to the directory the sysctl table is in.
826 * @table: the top-level table structure
827 *
828 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
829 * array. A completely 0 filled entry terminates the table.
830 *
831 * The members of the &struct ctl_table structure are used as follows:
832 *
833 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
834 * enter a sysctl file
835 *
836 * data - a pointer to data for use by proc_handler
837 *
838 * maxlen - the maximum size in bytes of the data
839 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800840 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800841 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800842 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800843 *
844 * proc_handler - the text handler routine (described below)
845 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800846 * extra1, extra2 - extra pointers usable by the proc handler routines
847 *
848 * Leaf nodes in the sysctl tree will be represented by a single file
849 * under /proc; non-leaf nodes will be represented by directories.
850 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800851 * There must be a proc_handler routine for any terminal nodes.
852 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800853 *
854 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
855 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
856 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
857 *
858 * It is the handler's job to read the input buffer from user memory
859 * and process it. The handler should return 0 on success.
860 *
861 * This routine returns %NULL on a failure to register, and a pointer
862 * to the table header on success.
863 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800864struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800865 struct ctl_table_root *root,
866 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800867 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800868{
869 struct ctl_table_header *header;
870 struct ctl_table *new, **prevp;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800871 const char *name, *nextname;
872 unsigned int npath = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800873 struct ctl_table_set *set;
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800874 size_t path_bytes = 0;
875 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800876
877 /* Count the path components */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800878 for (name = path; name; name = nextname) {
879 int namelen;
880 nextname = strchr(name, '/');
881 if (nextname) {
882 namelen = nextname - name;
883 nextname++;
884 } else {
885 namelen = strlen(name);
886 }
887 if (namelen == 0)
888 continue;
889 path_bytes += namelen + 1;
890 npath++;
891 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800892
893 /*
894 * For each path component, allocate a 2-element ctl_table array.
895 * The first array element will be filled with the sysctl entry
896 * for this, the second will be the sentinel (procname == 0).
897 *
898 * We allocate everything in one go so that we don't have to
899 * worry about freeing additional memory in unregister_sysctl_table.
900 */
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800901 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800902 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
903 if (!header)
904 return NULL;
905
906 new = (struct ctl_table *) (header + 1);
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800907 new_name = (char *)(new + (2 * npath));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800908
909 /* Now connect the dots */
910 prevp = &header->ctl_table;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800911 for (name = path; name; name = nextname) {
912 int namelen;
913 nextname = strchr(name, '/');
914 if (nextname) {
915 namelen = nextname - name;
916 nextname++;
917 } else {
918 namelen = strlen(name);
919 }
920 if (namelen == 0)
921 continue;
922 memcpy(new_name, name, namelen);
923 new_name[namelen] = '\0';
924
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800925 new->procname = new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800926 new->mode = 0555;
927
928 *prevp = new;
929 prevp = &new->child;
930
931 new += 2;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800932 new_name += namelen + 1;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800933 }
934 *prevp = table;
935 header->ctl_table_arg = table;
936
937 INIT_LIST_HEAD(&header->ctl_entry);
938 header->used = 0;
939 header->unregistering = NULL;
940 header->root = root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800941 header->count = 1;
Eric W. Biederman938aaa42012-01-09 17:24:30 -0800942 header->nreg = 1;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800943 if (sysctl_check_table(path, table))
944 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -0800945
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800946 spin_lock(&sysctl_lock);
947 header->set = lookup_header_set(root, namespaces);
948 header->attached_by = header->ctl_table;
949 header->attached_to = root_table;
950 header->parent = &root_table_header;
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800951 set = header->set;
952 root = header->root;
953 for (;;) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800954 struct ctl_table_header *p;
955 list_for_each_entry(p, &set->list, ctl_entry) {
956 if (p->unregistering)
957 continue;
958 try_attach(p, header);
959 }
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800960 if (root == &sysctl_table_root)
961 break;
962 root = list_entry(root->root_list.prev,
963 struct ctl_table_root, root_list);
964 set = lookup_header_set(root, namespaces);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800965 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800966 if (sysctl_check_dups(namespaces, header, path, table))
967 goto fail_locked;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800968 header->parent->count++;
969 list_add_tail(&header->ctl_entry, &header->set->list);
970 spin_unlock(&sysctl_lock);
971
972 return header;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800973fail_locked:
974 spin_unlock(&sysctl_lock);
975fail:
976 kfree(header);
977 dump_stack();
978 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800979}
980
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800981static char *append_path(const char *path, char *pos, const char *name)
982{
983 int namelen;
984 namelen = strlen(name);
985 if (((pos - path) + namelen + 2) >= PATH_MAX)
986 return NULL;
987 memcpy(pos, name, namelen);
988 pos[namelen] = '/';
989 pos[namelen + 1] = '\0';
990 pos += namelen + 1;
991 return pos;
992}
993
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800994static int count_subheaders(struct ctl_table *table)
995{
996 int has_files = 0;
997 int nr_subheaders = 0;
998 struct ctl_table *entry;
999
1000 /* special case: no directory and empty directory */
1001 if (!table || !table->procname)
1002 return 1;
1003
1004 for (entry = table; entry->procname; entry++) {
1005 if (entry->child)
1006 nr_subheaders += count_subheaders(entry->child);
1007 else
1008 has_files = 1;
1009 }
1010 return nr_subheaders + has_files;
1011}
1012
1013static int register_leaf_sysctl_tables(const char *path, char *pos,
1014 struct ctl_table_header ***subheader,
1015 struct ctl_table_root *root, struct nsproxy *namespaces,
1016 struct ctl_table *table)
1017{
1018 struct ctl_table *ctl_table_arg = NULL;
1019 struct ctl_table *entry, *files;
1020 int nr_files = 0;
1021 int nr_dirs = 0;
1022 int err = -ENOMEM;
1023
1024 for (entry = table; entry->procname; entry++) {
1025 if (entry->child)
1026 nr_dirs++;
1027 else
1028 nr_files++;
1029 }
1030
1031 files = table;
1032 /* If there are mixed files and directories we need a new table */
1033 if (nr_dirs && nr_files) {
1034 struct ctl_table *new;
1035 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1036 GFP_KERNEL);
1037 if (!files)
1038 goto out;
1039
1040 ctl_table_arg = files;
1041 for (new = files, entry = table; entry->procname; entry++) {
1042 if (entry->child)
1043 continue;
1044 *new = *entry;
1045 new++;
1046 }
1047 }
1048
1049 /* Register everything except a directory full of subdirectories */
1050 if (nr_files || !nr_dirs) {
1051 struct ctl_table_header *header;
1052 header = __register_sysctl_table(root, namespaces, path, files);
1053 if (!header) {
1054 kfree(ctl_table_arg);
1055 goto out;
1056 }
1057
1058 /* Remember if we need to free the file table */
1059 header->ctl_table_arg = ctl_table_arg;
1060 **subheader = header;
1061 (*subheader)++;
1062 }
1063
1064 /* Recurse into the subdirectories. */
1065 for (entry = table; entry->procname; entry++) {
1066 char *child_pos;
1067
1068 if (!entry->child)
1069 continue;
1070
1071 err = -ENAMETOOLONG;
1072 child_pos = append_path(path, pos, entry->procname);
1073 if (!child_pos)
1074 goto out;
1075
1076 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1077 root, namespaces, entry->child);
1078 pos[0] = '\0';
1079 if (err)
1080 goto out;
1081 }
1082 err = 0;
1083out:
1084 /* On failure our caller will unregister all registered subheaders */
1085 return err;
1086}
1087
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001088/**
1089 * __register_sysctl_paths - register a sysctl table hierarchy
1090 * @root: List of sysctl headers to register on
1091 * @namespaces: Data to compute which lists of sysctl entries are visible
1092 * @path: The path to the directory the sysctl table is in.
1093 * @table: the top-level table structure
1094 *
1095 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1096 * array. A completely 0 filled entry terminates the table.
1097 *
1098 * See __register_sysctl_table for more details.
1099 */
1100struct ctl_table_header *__register_sysctl_paths(
1101 struct ctl_table_root *root,
1102 struct nsproxy *namespaces,
1103 const struct ctl_path *path, struct ctl_table *table)
1104{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001105 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001106 int nr_subheaders = count_subheaders(table);
1107 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001108 const struct ctl_path *component;
1109 char *new_path, *pos;
1110
1111 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1112 if (!new_path)
1113 return NULL;
1114
1115 pos[0] = '\0';
1116 for (component = path; component->procname; component++) {
1117 pos = append_path(new_path, pos, component->procname);
1118 if (!pos)
1119 goto out;
1120 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001121 while (table->procname && table->child && !table[1].procname) {
1122 pos = append_path(new_path, pos, table->procname);
1123 if (!pos)
1124 goto out;
1125 table = table->child;
1126 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001127 if (nr_subheaders == 1) {
1128 header = __register_sysctl_table(root, namespaces, new_path, table);
1129 if (header)
1130 header->ctl_table_arg = ctl_table_arg;
1131 } else {
1132 header = kzalloc(sizeof(*header) +
1133 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1134 if (!header)
1135 goto out;
1136
1137 subheaders = (struct ctl_table_header **) (header + 1);
1138 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001139 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001140
1141 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1142 root, namespaces, table))
1143 goto err_register_leaves;
1144 }
1145
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001146out:
1147 kfree(new_path);
1148 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001149
1150err_register_leaves:
1151 while (subheader > subheaders) {
1152 struct ctl_table_header *subh = *(--subheader);
1153 struct ctl_table *table = subh->ctl_table_arg;
1154 unregister_sysctl_table(subh);
1155 kfree(table);
1156 }
1157 kfree(header);
1158 header = NULL;
1159 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001160}
1161
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001162/**
1163 * register_sysctl_table_path - register a sysctl table hierarchy
1164 * @path: The path to the directory the sysctl table is in.
1165 * @table: the top-level table structure
1166 *
1167 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1168 * array. A completely 0 filled entry terminates the table.
1169 *
1170 * See __register_sysctl_paths for more details.
1171 */
1172struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1173 struct ctl_table *table)
1174{
1175 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1176 path, table);
1177}
1178EXPORT_SYMBOL(register_sysctl_paths);
1179
1180/**
1181 * register_sysctl_table - register a sysctl table hierarchy
1182 * @table: the top-level table structure
1183 *
1184 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1185 * array. A completely 0 filled entry terminates the table.
1186 *
1187 * See register_sysctl_paths for more details.
1188 */
1189struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1190{
1191 static const struct ctl_path null_path[] = { {} };
1192
1193 return register_sysctl_paths(null_path, table);
1194}
1195EXPORT_SYMBOL(register_sysctl_table);
1196
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001197static void drop_sysctl_table(struct ctl_table_header *header)
1198{
1199 if (--header->nreg)
1200 return;
1201
1202 start_unregistering(header);
1203 if (!--header->parent->count) {
1204 WARN_ON(1);
1205 kfree_rcu(header->parent, rcu);
1206 }
1207 if (!--header->count)
1208 kfree_rcu(header, rcu);
1209}
1210
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001211/**
1212 * unregister_sysctl_table - unregister a sysctl table hierarchy
1213 * @header: the header returned from register_sysctl_table
1214 *
1215 * Unregisters the sysctl table and all children. proc entries may not
1216 * actually be removed until they are no longer used by anyone.
1217 */
1218void unregister_sysctl_table(struct ctl_table_header * header)
1219{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001220 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001221 might_sleep();
1222
1223 if (header == NULL)
1224 return;
1225
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001226 nr_subheaders = count_subheaders(header->ctl_table_arg);
1227 if (unlikely(nr_subheaders > 1)) {
1228 struct ctl_table_header **subheaders;
1229 int i;
1230
1231 subheaders = (struct ctl_table_header **)(header + 1);
1232 for (i = nr_subheaders -1; i >= 0; i--) {
1233 struct ctl_table_header *subh = subheaders[i];
1234 struct ctl_table *table = subh->ctl_table_arg;
1235 unregister_sysctl_table(subh);
1236 kfree(table);
1237 }
1238 kfree(header);
1239 return;
1240 }
1241
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001242 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001243 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001244 spin_unlock(&sysctl_lock);
1245}
1246EXPORT_SYMBOL(unregister_sysctl_table);
1247
1248void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001249 int (*is_seen)(struct ctl_table_set *))
1250{
1251 INIT_LIST_HEAD(&p->list);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001252 p->is_seen = is_seen;
1253}
1254
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001255void retire_sysctl_set(struct ctl_table_set *set)
1256{
1257 WARN_ON(!list_empty(&set->list));
1258}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001259
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001260int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001261{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001262 struct proc_dir_entry *proc_sys_root;
1263
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001264 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001265 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1266 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001267 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001268
1269 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001270}