blob: 88d1b06cc5c0d9b3abf5d4e37abfc583ad78ca0e [file] [log] [blame]
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001/*
2 * /proc/sys support
3 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04004#include <linux/init.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08005#include <linux/sysctl.h>
Lucas De Marchif1ecf062011-11-02 13:39:22 -07006#include <linux/poll.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08007#include <linux/proc_fs.h>
8#include <linux/security.h>
Nick Piggin34286d62011-01-07 17:49:57 +11009#include <linux/namei.h>
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080010#include <linux/module.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -080011#include "internal.h"
12
Al Virod72f71e2009-02-20 05:58:47 +000013static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080014static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080015static const struct inode_operations proc_sys_inode_operations;
Al Viro90434762008-07-15 08:54:06 -040016static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080018
Lucas De Marchif1ecf062011-11-02 13:39:22 -070019void proc_sys_poll_notify(struct ctl_table_poll *poll)
20{
21 if (!poll)
22 return;
23
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
26}
27
Eric W. Biedermana1945582012-01-21 17:51:48 -080028static struct ctl_table root_table[] = {
29 {
30 .procname = "",
31 .mode = S_IRUGO|S_IXUGO,
32 .child = &root_table[1],
33 },
34 { }
35};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080036static struct ctl_table_root sysctl_table_root;
37static struct ctl_table_header root_table_header = {
38 {{.count = 1,
Eric W. Biederman938aaa42012-01-09 17:24:30 -080039 .nreg = 1,
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080042 .root = &sysctl_table_root,
43 .set = &sysctl_table_root.default_set,
44};
45static struct ctl_table_root sysctl_table_root = {
46 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
47 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
48};
49
50static DEFINE_SPINLOCK(sysctl_lock);
51
Eric W. Biedermane0d04522012-01-09 22:36:41 -080052static void init_header(struct ctl_table_header *head,
53 struct ctl_table_root *root, struct ctl_table_set *set,
54 struct ctl_table *table)
55{
56 head->ctl_table_arg = table;
57 INIT_LIST_HEAD(&head->ctl_entry);
58 head->used = 0;
59 head->count = 1;
60 head->nreg = 1;
61 head->unregistering = NULL;
62 head->root = root;
63 head->set = set;
64 head->parent = NULL;
65}
66
Eric W. Biederman8425d6a2012-01-09 17:35:01 -080067static void erase_header(struct ctl_table_header *head)
68{
69 list_del_init(&head->ctl_entry);
70}
71
72static void insert_header(struct ctl_table_header *header)
73{
74 header->parent->count++;
75 list_add_tail(&header->ctl_entry, &header->set->list);
76}
77
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080078/* called under sysctl_lock */
79static int use_table(struct ctl_table_header *p)
80{
81 if (unlikely(p->unregistering))
82 return 0;
83 p->used++;
84 return 1;
85}
86
87/* called under sysctl_lock */
88static void unuse_table(struct ctl_table_header *p)
89{
90 if (!--p->used)
91 if (unlikely(p->unregistering))
92 complete(p->unregistering);
93}
94
95/* called under sysctl_lock, will reacquire if has to wait */
96static void start_unregistering(struct ctl_table_header *p)
97{
98 /*
99 * if p->used is 0, nobody will ever touch that entry again;
100 * we'll eliminate all paths to it before dropping sysctl_lock
101 */
102 if (unlikely(p->used)) {
103 struct completion wait;
104 init_completion(&wait);
105 p->unregistering = &wait;
106 spin_unlock(&sysctl_lock);
107 wait_for_completion(&wait);
108 spin_lock(&sysctl_lock);
109 } else {
110 /* anything non-NULL; we'll never dereference it */
111 p->unregistering = ERR_PTR(-EINVAL);
112 }
113 /*
114 * do not remove from the list until nobody holds it; walking the
115 * list in do_sysctl() relies on that.
116 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800117 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800118}
119
120static void sysctl_head_get(struct ctl_table_header *head)
121{
122 spin_lock(&sysctl_lock);
123 head->count++;
124 spin_unlock(&sysctl_lock);
125}
126
127void sysctl_head_put(struct ctl_table_header *head)
128{
129 spin_lock(&sysctl_lock);
130 if (!--head->count)
131 kfree_rcu(head, rcu);
132 spin_unlock(&sysctl_lock);
133}
134
135static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
136{
137 if (!head)
138 BUG();
139 spin_lock(&sysctl_lock);
140 if (!use_table(head))
141 head = ERR_PTR(-ENOENT);
142 spin_unlock(&sysctl_lock);
143 return head;
144}
145
146static void sysctl_head_finish(struct ctl_table_header *head)
147{
148 if (!head)
149 return;
150 spin_lock(&sysctl_lock);
151 unuse_table(head);
152 spin_unlock(&sysctl_lock);
153}
154
155static struct ctl_table_set *
156lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
157{
158 struct ctl_table_set *set = &root->default_set;
159 if (root->lookup)
160 set = root->lookup(root, namespaces);
161 return set;
162}
163
164static struct list_head *
165lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
166{
167 struct ctl_table_set *set = lookup_header_set(root, namespaces);
168 return &set->list;
169}
170
171static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
172 struct ctl_table_header *prev)
173{
174 struct ctl_table_root *root;
175 struct list_head *header_list;
176 struct ctl_table_header *head;
177 struct list_head *tmp;
178
179 spin_lock(&sysctl_lock);
180 if (prev) {
181 head = prev;
182 tmp = &prev->ctl_entry;
183 unuse_table(prev);
184 goto next;
185 }
186 tmp = &root_table_header.ctl_entry;
187 for (;;) {
188 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
189
190 if (!use_table(head))
191 goto next;
192 spin_unlock(&sysctl_lock);
193 return head;
194 next:
195 root = head->root;
196 tmp = tmp->next;
197 header_list = lookup_header_list(root, namespaces);
198 if (tmp != header_list)
199 continue;
200
201 do {
202 root = list_entry(root->root_list.next,
203 struct ctl_table_root, root_list);
204 if (root == &sysctl_table_root)
205 goto out;
206 header_list = lookup_header_list(root, namespaces);
207 } while (list_empty(header_list));
208 tmp = header_list->next;
209 }
210out:
211 spin_unlock(&sysctl_lock);
212 return NULL;
213}
214
215static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
216{
217 return __sysctl_head_next(current->nsproxy, prev);
218}
219
220void register_sysctl_root(struct ctl_table_root *root)
221{
222 spin_lock(&sysctl_lock);
223 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
224 spin_unlock(&sysctl_lock);
225}
226
227/*
228 * sysctl_perm does NOT grant the superuser all rights automatically, because
229 * some sysctl variables are readonly even to root.
230 */
231
232static int test_perm(int mode, int op)
233{
234 if (!current_euid())
235 mode >>= 6;
236 else if (in_egroup_p(0))
237 mode >>= 3;
238 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
239 return 0;
240 return -EACCES;
241}
242
243static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
244{
245 int mode;
246
247 if (root->permissions)
248 mode = root->permissions(root, current->nsproxy, table);
249 else
250 mode = table->mode;
251
252 return test_perm(mode, op);
253}
254
Al Viro90434762008-07-15 08:54:06 -0400255static struct inode *proc_sys_make_inode(struct super_block *sb,
256 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800257{
258 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400259 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800260
Al Viro90434762008-07-15 08:54:06 -0400261 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800262 if (!inode)
263 goto out;
264
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400265 inode->i_ino = get_next_ino();
266
Al Viro90434762008-07-15 08:54:06 -0400267 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800268 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400269 ei->sysctl = head;
270 ei->sysctl_entry = table;
271
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800272 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400273 inode->i_mode = table->mode;
274 if (!table->child) {
275 inode->i_mode |= S_IFREG;
276 inode->i_op = &proc_sys_inode_operations;
277 inode->i_fop = &proc_sys_file_operations;
278 } else {
279 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400280 inode->i_op = &proc_sys_dir_operations;
281 inode->i_fop = &proc_sys_dir_file_operations;
282 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800283out:
284 return inode;
285}
286
Al Viro90434762008-07-15 08:54:06 -0400287static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800288{
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700289 for ( ; p->procname; p++) {
Lucas De Marchi36885d72011-06-10 02:36:05 -0300290 if (strlen(p->procname) != name->len)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800291 continue;
292
Lucas De Marchi36885d72011-06-10 02:36:05 -0300293 if (memcmp(p->procname, name->name, name->len) != 0)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800294 continue;
295
296 /* I have a match */
Al Viro90434762008-07-15 08:54:06 -0400297 return p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800298 }
299 return NULL;
300}
301
Adrian Bunk81324362008-10-03 00:33:54 +0400302static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800303{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800304 struct ctl_table_header *head = PROC_I(inode)->sysctl;
305 if (!head)
306 head = &root_table_header;
307 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800308}
309
310static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
311 struct nameidata *nd)
312{
Al Viro90434762008-07-15 08:54:06 -0400313 struct ctl_table_header *head = grab_header(dir);
314 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
315 struct ctl_table_header *h = NULL;
316 struct qstr *name = &dentry->d_name;
317 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800318 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400319 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800320
Al Viro90434762008-07-15 08:54:06 -0400321 if (IS_ERR(head))
322 return ERR_CAST(head);
323
324 if (table && !table->child) {
325 WARN_ON(1);
326 goto out;
327 }
328
Eric W. Biedermana1945582012-01-21 17:51:48 -0800329 table = table ? table->child : &head->ctl_table[1];
Al Viro90434762008-07-15 08:54:06 -0400330
331 p = find_in_table(table, name);
332 if (!p) {
333 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
334 if (h->attached_to != table)
335 continue;
336 p = find_in_table(h->attached_by, name);
337 if (p)
338 break;
339 }
340 }
341
342 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800343 goto out;
344
345 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400346 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
347 if (h)
348 sysctl_head_finish(h);
349
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800350 if (!inode)
351 goto out;
352
353 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100354 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800355 d_add(dentry, inode);
356
357out:
358 sysctl_head_finish(head);
359 return err;
360}
361
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700362static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
363 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800364{
Al Viro90434762008-07-15 08:54:06 -0400365 struct inode *inode = filp->f_path.dentry->d_inode;
366 struct ctl_table_header *head = grab_header(inode);
367 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100368 ssize_t error;
369 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800370
Al Viro90434762008-07-15 08:54:06 -0400371 if (IS_ERR(head))
372 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800373
374 /*
375 * At this point we know that the sysctl was not unregistered
376 * and won't be until we finish.
377 */
378 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700379 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800380 goto out;
381
Al Viro90434762008-07-15 08:54:06 -0400382 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
383 error = -EINVAL;
384 if (!table->proc_handler)
385 goto out;
386
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800387 /* careful: calling conventions are nasty here */
388 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700389 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800390 if (!error)
391 error = res;
392out:
393 sysctl_head_finish(head);
394
395 return error;
396}
397
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700398static ssize_t proc_sys_read(struct file *filp, char __user *buf,
399 size_t count, loff_t *ppos)
400{
401 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
402}
403
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800404static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
405 size_t count, loff_t *ppos)
406{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700407 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800408}
409
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700410static int proc_sys_open(struct inode *inode, struct file *filp)
411{
412 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
413
414 if (table->poll)
415 filp->private_data = proc_sys_poll_event(table->poll);
416
417 return 0;
418}
419
420static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
421{
422 struct inode *inode = filp->f_path.dentry->d_inode;
423 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
424 unsigned long event = (unsigned long)filp->private_data;
425 unsigned int ret = DEFAULT_POLLMASK;
426
427 if (!table->proc_handler)
428 goto out;
429
430 if (!table->poll)
431 goto out;
432
433 poll_wait(filp, &table->poll->wait, wait);
434
435 if (event != atomic_read(&table->poll->event)) {
436 filp->private_data = proc_sys_poll_event(table->poll);
437 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
438 }
439
440out:
441 return ret;
442}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800443
444static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400445 filldir_t filldir,
446 struct ctl_table_header *head,
447 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800448{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800449 struct dentry *child, *dir = filp->f_path.dentry;
450 struct inode *inode;
451 struct qstr qname;
452 ino_t ino = 0;
453 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800454
455 qname.name = table->procname;
456 qname.len = strlen(table->procname);
457 qname.hash = full_name_hash(qname.name, qname.len);
458
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800459 child = d_lookup(dir, &qname);
460 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400461 child = d_alloc(dir, &qname);
462 if (child) {
463 inode = proc_sys_make_inode(dir->d_sb, head, table);
464 if (!inode) {
465 dput(child);
466 return -ENOMEM;
467 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100468 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400469 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800470 }
Al Viro90434762008-07-15 08:54:06 -0400471 } else {
472 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800473 }
474 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800475 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400476 ino = inode->i_ino;
477 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800478 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400479 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
480}
481
482static int scan(struct ctl_table_header *head, ctl_table *table,
483 unsigned long *pos, struct file *file,
484 void *dirent, filldir_t filldir)
485{
486
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700487 for (; table->procname; table++, (*pos)++) {
Al Viro90434762008-07-15 08:54:06 -0400488 int res;
489
Al Viro90434762008-07-15 08:54:06 -0400490 if (*pos < file->f_pos)
491 continue;
492
493 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
494 if (res)
495 return res;
496
497 file->f_pos = *pos + 1;
498 }
499 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800500}
501
502static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
503{
Al Viro90434762008-07-15 08:54:06 -0400504 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800505 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400506 struct ctl_table_header *head = grab_header(inode);
507 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
508 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800509 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400510 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800511
Al Viro90434762008-07-15 08:54:06 -0400512 if (IS_ERR(head))
513 return PTR_ERR(head);
514
515 if (table && !table->child) {
516 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800517 goto out;
Al Viro90434762008-07-15 08:54:06 -0400518 }
519
Eric W. Biedermana1945582012-01-21 17:51:48 -0800520 table = table ? table->child : &head->ctl_table[1];
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800521
522 ret = 0;
523 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
524 if (filp->f_pos == 0) {
525 if (filldir(dirent, ".", 1, filp->f_pos,
526 inode->i_ino, DT_DIR) < 0)
527 goto out;
528 filp->f_pos++;
529 }
530 if (filp->f_pos == 1) {
531 if (filldir(dirent, "..", 2, filp->f_pos,
532 parent_ino(dentry), DT_DIR) < 0)
533 goto out;
534 filp->f_pos++;
535 }
536 pos = 2;
537
Al Viro90434762008-07-15 08:54:06 -0400538 ret = scan(head, table, &pos, filp, dirent, filldir);
539 if (ret)
540 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800541
Al Viro90434762008-07-15 08:54:06 -0400542 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
543 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800544 continue;
Al Viro90434762008-07-15 08:54:06 -0400545 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
546 if (ret) {
547 sysctl_head_finish(h);
548 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800549 }
550 }
551 ret = 1;
552out:
553 sysctl_head_finish(head);
554 return ret;
555}
556
Al Viro10556cb2011-06-20 19:28:19 -0400557static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800558{
559 /*
560 * sysctl entries that are not writeable,
561 * are _NOT_ writeable, capabilities or not.
562 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200563 struct ctl_table_header *head;
564 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800565 int error;
566
Miklos Szeredif696a362008-07-31 13:41:58 +0200567 /* Executable files are not allowed under /proc/sys/ */
568 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
569 return -EACCES;
570
571 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400572 if (IS_ERR(head))
573 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800574
Miklos Szeredif696a362008-07-31 13:41:58 +0200575 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400576 if (!table) /* global root - r-xr-xr-x */
577 error = mask & MAY_WRITE ? -EACCES : 0;
578 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400579 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800580
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800581 sysctl_head_finish(head);
582 return error;
583}
584
585static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
586{
587 struct inode *inode = dentry->d_inode;
588 int error;
589
590 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
591 return -EPERM;
592
593 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200594 if (error)
595 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800596
Christoph Hellwig10257742010-06-04 11:30:02 +0200597 if ((attr->ia_valid & ATTR_SIZE) &&
598 attr->ia_size != i_size_read(inode)) {
599 error = vmtruncate(inode, attr->ia_size);
600 if (error)
601 return error;
602 }
603
604 setattr_copy(inode, attr);
605 mark_inode_dirty(inode);
606 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800607}
608
Al Viro90434762008-07-15 08:54:06 -0400609static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
610{
611 struct inode *inode = dentry->d_inode;
612 struct ctl_table_header *head = grab_header(inode);
613 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
614
615 if (IS_ERR(head))
616 return PTR_ERR(head);
617
618 generic_fillattr(inode, stat);
619 if (table)
620 stat->mode = (stat->mode & S_IFMT) | table->mode;
621
622 sysctl_head_finish(head);
623 return 0;
624}
625
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800626static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700627 .open = proc_sys_open,
628 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800629 .read = proc_sys_read,
630 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200631 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400632};
633
634static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700635 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800636 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200637 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800638};
639
Jan Engelhardt03a44822008-02-08 04:21:19 -0800640static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400641 .permission = proc_sys_permission,
642 .setattr = proc_sys_setattr,
643 .getattr = proc_sys_getattr,
644};
645
646static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800647 .lookup = proc_sys_lookup,
648 .permission = proc_sys_permission,
649 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400650 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800651};
652
653static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
654{
Nick Piggin34286d62011-01-07 17:49:57 +1100655 if (nd->flags & LOOKUP_RCU)
656 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400657 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
658}
659
Nick Pigginfe15ce42011-01-07 17:49:23 +1100660static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400661{
662 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
663}
664
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800665static int sysctl_is_seen(struct ctl_table_header *p)
666{
667 struct ctl_table_set *set = p->set;
668 int res;
669 spin_lock(&sysctl_lock);
670 if (p->unregistering)
671 res = 0;
672 else if (!set->is_seen)
673 res = 1;
674 else
675 res = set->is_seen(set);
676 spin_unlock(&sysctl_lock);
677 return res;
678}
679
Nick Piggin621e1552011-01-07 17:49:27 +1100680static int proc_sys_compare(const struct dentry *parent,
681 const struct inode *pinode,
682 const struct dentry *dentry, const struct inode *inode,
683 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400684{
Al Virodfef6dc2011-03-08 01:25:28 -0500685 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100686 /* Although proc doesn't have negative dentries, rcu-walk means
687 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500688 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100689 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500690 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100691 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400692 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100693 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400694 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500695 head = rcu_dereference(PROC_I(inode)->sysctl);
696 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800697}
698
Al Virod72f71e2009-02-20 05:58:47 +0000699static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800700 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400701 .d_delete = proc_sys_delete,
702 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800703};
704
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800705static struct ctl_table *is_branch_in(struct ctl_table *branch,
706 struct ctl_table *table)
707{
708 struct ctl_table *p;
709 const char *s = branch->procname;
710
711 /* branch should have named subdirectory as its first element */
712 if (!s || !branch->child)
713 return NULL;
714
715 /* ... and nothing else */
716 if (branch[1].procname)
717 return NULL;
718
719 /* table should contain subdirectory with the same name */
720 for (p = table; p->procname; p++) {
721 if (!p->child)
722 continue;
723 if (p->procname && strcmp(p->procname, s) == 0)
724 return p;
725 }
726 return NULL;
727}
728
729/* see if attaching q to p would be an improvement */
730static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
731{
732 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
733 struct ctl_table *next;
734 int is_better = 0;
735 int not_in_parent = !p->attached_by;
736
737 while ((next = is_branch_in(by, to)) != NULL) {
738 if (by == q->attached_by)
739 is_better = 1;
740 if (to == p->attached_by)
741 not_in_parent = 1;
742 by = by->child;
743 to = next->child;
744 }
745
746 if (is_better && not_in_parent) {
747 q->attached_by = by;
748 q->attached_to = to;
749 q->parent = p;
750 }
751}
752
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800753static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
754 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800755{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800756 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800757 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800758
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800759 for (entry = old; entry->procname; entry++) {
760 for (test = table; test->procname; test++) {
761 if (strcmp(entry->procname, test->procname) == 0) {
762 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
763 path, test->procname);
764 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800765 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800766 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800767 }
768 return error;
769}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800770
771static int sysctl_check_dups(struct nsproxy *namespaces,
772 struct ctl_table_header *header,
773 const char *path, struct ctl_table *table)
774{
775 struct ctl_table_root *root;
776 struct ctl_table_set *set;
777 struct ctl_table_header *dir_head, *head;
778 struct ctl_table *dir_table;
779 int error = 0;
780
781 /* No dups if we are the only member of our directory */
782 if (header->attached_by != table)
783 return 0;
784
785 dir_head = header->parent;
786 dir_table = header->attached_to;
787
788 error = sysctl_check_table_dups(path, dir_table, table);
789
790 root = &sysctl_table_root;
791 do {
792 set = lookup_header_set(root, namespaces);
793
794 list_for_each_entry(head, &set->list, ctl_entry) {
795 if (head->unregistering)
796 continue;
797 if (head->attached_to != dir_table)
798 continue;
799 error = sysctl_check_table_dups(path, head->attached_by,
800 table);
801 }
802 root = list_entry(root->root_list.next,
803 struct ctl_table_root, root_list);
804 } while (root != &sysctl_table_root);
805 return error;
806}
807
808static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
809{
810 struct va_format vaf;
811 va_list args;
812
813 va_start(args, fmt);
814 vaf.fmt = fmt;
815 vaf.va = &args;
816
817 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
818 path, table->procname, &vaf);
819
820 va_end(args);
821 return -EINVAL;
822}
823
824static int sysctl_check_table(const char *path, struct ctl_table *table)
825{
826 int err = 0;
827 for (; table->procname; table++) {
828 if (table->child)
829 err = sysctl_err(path, table, "Not a file");
830
831 if ((table->proc_handler == proc_dostring) ||
832 (table->proc_handler == proc_dointvec) ||
833 (table->proc_handler == proc_dointvec_minmax) ||
834 (table->proc_handler == proc_dointvec_jiffies) ||
835 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
836 (table->proc_handler == proc_dointvec_ms_jiffies) ||
837 (table->proc_handler == proc_doulongvec_minmax) ||
838 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
839 if (!table->data)
840 err = sysctl_err(path, table, "No data");
841 if (!table->maxlen)
842 err = sysctl_err(path, table, "No maxlen");
843 }
844 if (!table->proc_handler)
845 err = sysctl_err(path, table, "No proc_handler");
846
847 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
848 err = sysctl_err(path, table, "bogus .mode 0%o",
849 table->mode);
850 }
851 return err;
852}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800853
854/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800855 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800856 * @root: List of sysctl headers to register on
857 * @namespaces: Data to compute which lists of sysctl entries are visible
858 * @path: The path to the directory the sysctl table is in.
859 * @table: the top-level table structure
860 *
861 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
862 * array. A completely 0 filled entry terminates the table.
863 *
864 * The members of the &struct ctl_table structure are used as follows:
865 *
866 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
867 * enter a sysctl file
868 *
869 * data - a pointer to data for use by proc_handler
870 *
871 * maxlen - the maximum size in bytes of the data
872 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800873 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800874 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800875 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800876 *
877 * proc_handler - the text handler routine (described below)
878 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800879 * extra1, extra2 - extra pointers usable by the proc handler routines
880 *
881 * Leaf nodes in the sysctl tree will be represented by a single file
882 * under /proc; non-leaf nodes will be represented by directories.
883 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800884 * There must be a proc_handler routine for any terminal nodes.
885 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800886 *
887 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
888 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
889 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
890 *
891 * It is the handler's job to read the input buffer from user memory
892 * and process it. The handler should return 0 on success.
893 *
894 * This routine returns %NULL on a failure to register, and a pointer
895 * to the table header on success.
896 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800897struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800898 struct ctl_table_root *root,
899 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800900 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800901{
902 struct ctl_table_header *header;
903 struct ctl_table *new, **prevp;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800904 const char *name, *nextname;
905 unsigned int npath = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800906 struct ctl_table_set *set;
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800907 size_t path_bytes = 0;
908 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800909
910 /* Count the path components */
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 path_bytes += namelen + 1;
923 npath++;
924 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800925
926 /*
927 * For each path component, allocate a 2-element ctl_table array.
928 * The first array element will be filled with the sysctl entry
929 * for this, the second will be the sentinel (procname == 0).
930 *
931 * We allocate everything in one go so that we don't have to
932 * worry about freeing additional memory in unregister_sysctl_table.
933 */
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800934 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800935 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
936 if (!header)
937 return NULL;
938
939 new = (struct ctl_table *) (header + 1);
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800940 new_name = (char *)(new + (2 * npath));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800941
942 /* Now connect the dots */
943 prevp = &header->ctl_table;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800944 for (name = path; name; name = nextname) {
945 int namelen;
946 nextname = strchr(name, '/');
947 if (nextname) {
948 namelen = nextname - name;
949 nextname++;
950 } else {
951 namelen = strlen(name);
952 }
953 if (namelen == 0)
954 continue;
955 memcpy(new_name, name, namelen);
956 new_name[namelen] = '\0';
957
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800958 new->procname = new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800959 new->mode = 0555;
960
961 *prevp = new;
962 prevp = &new->child;
963
964 new += 2;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800965 new_name += namelen + 1;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800966 }
967 *prevp = table;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800968
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800969 init_header(header, root, NULL, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800970 if (sysctl_check_table(path, table))
971 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -0800972
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800973 spin_lock(&sysctl_lock);
974 header->set = lookup_header_set(root, namespaces);
975 header->attached_by = header->ctl_table;
Eric W. Biedermana1945582012-01-21 17:51:48 -0800976 header->attached_to = &root_table[1];
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800977 header->parent = &root_table_header;
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800978 set = header->set;
979 root = header->root;
980 for (;;) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800981 struct ctl_table_header *p;
982 list_for_each_entry(p, &set->list, ctl_entry) {
983 if (p->unregistering)
984 continue;
985 try_attach(p, header);
986 }
Eric W. Biedermanbd295b52012-01-22 21:10:21 -0800987 if (root == &sysctl_table_root)
988 break;
989 root = list_entry(root->root_list.prev,
990 struct ctl_table_root, root_list);
991 set = lookup_header_set(root, namespaces);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800992 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800993 if (sysctl_check_dups(namespaces, header, path, table))
994 goto fail_locked;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800995 insert_header(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800996 spin_unlock(&sysctl_lock);
997
998 return header;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800999fail_locked:
1000 spin_unlock(&sysctl_lock);
1001fail:
1002 kfree(header);
1003 dump_stack();
1004 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001005}
1006
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001007static char *append_path(const char *path, char *pos, const char *name)
1008{
1009 int namelen;
1010 namelen = strlen(name);
1011 if (((pos - path) + namelen + 2) >= PATH_MAX)
1012 return NULL;
1013 memcpy(pos, name, namelen);
1014 pos[namelen] = '/';
1015 pos[namelen + 1] = '\0';
1016 pos += namelen + 1;
1017 return pos;
1018}
1019
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001020static int count_subheaders(struct ctl_table *table)
1021{
1022 int has_files = 0;
1023 int nr_subheaders = 0;
1024 struct ctl_table *entry;
1025
1026 /* special case: no directory and empty directory */
1027 if (!table || !table->procname)
1028 return 1;
1029
1030 for (entry = table; entry->procname; entry++) {
1031 if (entry->child)
1032 nr_subheaders += count_subheaders(entry->child);
1033 else
1034 has_files = 1;
1035 }
1036 return nr_subheaders + has_files;
1037}
1038
1039static int register_leaf_sysctl_tables(const char *path, char *pos,
1040 struct ctl_table_header ***subheader,
1041 struct ctl_table_root *root, struct nsproxy *namespaces,
1042 struct ctl_table *table)
1043{
1044 struct ctl_table *ctl_table_arg = NULL;
1045 struct ctl_table *entry, *files;
1046 int nr_files = 0;
1047 int nr_dirs = 0;
1048 int err = -ENOMEM;
1049
1050 for (entry = table; entry->procname; entry++) {
1051 if (entry->child)
1052 nr_dirs++;
1053 else
1054 nr_files++;
1055 }
1056
1057 files = table;
1058 /* If there are mixed files and directories we need a new table */
1059 if (nr_dirs && nr_files) {
1060 struct ctl_table *new;
1061 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1062 GFP_KERNEL);
1063 if (!files)
1064 goto out;
1065
1066 ctl_table_arg = files;
1067 for (new = files, entry = table; entry->procname; entry++) {
1068 if (entry->child)
1069 continue;
1070 *new = *entry;
1071 new++;
1072 }
1073 }
1074
1075 /* Register everything except a directory full of subdirectories */
1076 if (nr_files || !nr_dirs) {
1077 struct ctl_table_header *header;
1078 header = __register_sysctl_table(root, namespaces, path, files);
1079 if (!header) {
1080 kfree(ctl_table_arg);
1081 goto out;
1082 }
1083
1084 /* Remember if we need to free the file table */
1085 header->ctl_table_arg = ctl_table_arg;
1086 **subheader = header;
1087 (*subheader)++;
1088 }
1089
1090 /* Recurse into the subdirectories. */
1091 for (entry = table; entry->procname; entry++) {
1092 char *child_pos;
1093
1094 if (!entry->child)
1095 continue;
1096
1097 err = -ENAMETOOLONG;
1098 child_pos = append_path(path, pos, entry->procname);
1099 if (!child_pos)
1100 goto out;
1101
1102 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1103 root, namespaces, entry->child);
1104 pos[0] = '\0';
1105 if (err)
1106 goto out;
1107 }
1108 err = 0;
1109out:
1110 /* On failure our caller will unregister all registered subheaders */
1111 return err;
1112}
1113
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001114/**
1115 * __register_sysctl_paths - register a sysctl table hierarchy
1116 * @root: List of sysctl headers to register on
1117 * @namespaces: Data to compute which lists of sysctl entries are visible
1118 * @path: The path to the directory the sysctl table is in.
1119 * @table: the top-level table structure
1120 *
1121 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1122 * array. A completely 0 filled entry terminates the table.
1123 *
1124 * See __register_sysctl_table for more details.
1125 */
1126struct ctl_table_header *__register_sysctl_paths(
1127 struct ctl_table_root *root,
1128 struct nsproxy *namespaces,
1129 const struct ctl_path *path, struct ctl_table *table)
1130{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001131 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001132 int nr_subheaders = count_subheaders(table);
1133 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001134 const struct ctl_path *component;
1135 char *new_path, *pos;
1136
1137 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1138 if (!new_path)
1139 return NULL;
1140
1141 pos[0] = '\0';
1142 for (component = path; component->procname; component++) {
1143 pos = append_path(new_path, pos, component->procname);
1144 if (!pos)
1145 goto out;
1146 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001147 while (table->procname && table->child && !table[1].procname) {
1148 pos = append_path(new_path, pos, table->procname);
1149 if (!pos)
1150 goto out;
1151 table = table->child;
1152 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001153 if (nr_subheaders == 1) {
1154 header = __register_sysctl_table(root, namespaces, new_path, table);
1155 if (header)
1156 header->ctl_table_arg = ctl_table_arg;
1157 } else {
1158 header = kzalloc(sizeof(*header) +
1159 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1160 if (!header)
1161 goto out;
1162
1163 subheaders = (struct ctl_table_header **) (header + 1);
1164 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001165 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001166
1167 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1168 root, namespaces, table))
1169 goto err_register_leaves;
1170 }
1171
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001172out:
1173 kfree(new_path);
1174 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001175
1176err_register_leaves:
1177 while (subheader > subheaders) {
1178 struct ctl_table_header *subh = *(--subheader);
1179 struct ctl_table *table = subh->ctl_table_arg;
1180 unregister_sysctl_table(subh);
1181 kfree(table);
1182 }
1183 kfree(header);
1184 header = NULL;
1185 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001186}
1187
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001188/**
1189 * register_sysctl_table_path - register a sysctl table hierarchy
1190 * @path: The path to the directory the sysctl table is in.
1191 * @table: the top-level table structure
1192 *
1193 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1194 * array. A completely 0 filled entry terminates the table.
1195 *
1196 * See __register_sysctl_paths for more details.
1197 */
1198struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1199 struct ctl_table *table)
1200{
1201 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1202 path, table);
1203}
1204EXPORT_SYMBOL(register_sysctl_paths);
1205
1206/**
1207 * register_sysctl_table - register a sysctl table hierarchy
1208 * @table: the top-level table structure
1209 *
1210 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1211 * array. A completely 0 filled entry terminates the table.
1212 *
1213 * See register_sysctl_paths for more details.
1214 */
1215struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1216{
1217 static const struct ctl_path null_path[] = { {} };
1218
1219 return register_sysctl_paths(null_path, table);
1220}
1221EXPORT_SYMBOL(register_sysctl_table);
1222
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001223static void drop_sysctl_table(struct ctl_table_header *header)
1224{
1225 if (--header->nreg)
1226 return;
1227
1228 start_unregistering(header);
1229 if (!--header->parent->count) {
1230 WARN_ON(1);
1231 kfree_rcu(header->parent, rcu);
1232 }
1233 if (!--header->count)
1234 kfree_rcu(header, rcu);
1235}
1236
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001237/**
1238 * unregister_sysctl_table - unregister a sysctl table hierarchy
1239 * @header: the header returned from register_sysctl_table
1240 *
1241 * Unregisters the sysctl table and all children. proc entries may not
1242 * actually be removed until they are no longer used by anyone.
1243 */
1244void unregister_sysctl_table(struct ctl_table_header * header)
1245{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001246 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001247 might_sleep();
1248
1249 if (header == NULL)
1250 return;
1251
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001252 nr_subheaders = count_subheaders(header->ctl_table_arg);
1253 if (unlikely(nr_subheaders > 1)) {
1254 struct ctl_table_header **subheaders;
1255 int i;
1256
1257 subheaders = (struct ctl_table_header **)(header + 1);
1258 for (i = nr_subheaders -1; i >= 0; i--) {
1259 struct ctl_table_header *subh = subheaders[i];
1260 struct ctl_table *table = subh->ctl_table_arg;
1261 unregister_sysctl_table(subh);
1262 kfree(table);
1263 }
1264 kfree(header);
1265 return;
1266 }
1267
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001268 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001269 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001270 spin_unlock(&sysctl_lock);
1271}
1272EXPORT_SYMBOL(unregister_sysctl_table);
1273
1274void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001275 int (*is_seen)(struct ctl_table_set *))
1276{
1277 INIT_LIST_HEAD(&p->list);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001278 p->is_seen = is_seen;
1279}
1280
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001281void retire_sysctl_set(struct ctl_table_set *set)
1282{
1283 WARN_ON(!list_empty(&set->list));
1284}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001285
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001286int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001287{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001288 struct proc_dir_entry *proc_sys_root;
1289
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001290 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001291 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1292 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001293 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001294
1295 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001296}