blob: 06e6f10ee8ec6611175fcb982e146a0923b3f75b [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,
32 .ctl_table = root_table,
33 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
34 .root = &sysctl_table_root,
35 .set = &sysctl_table_root.default_set,
36};
37static struct ctl_table_root sysctl_table_root = {
38 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
39 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
40};
41
42static DEFINE_SPINLOCK(sysctl_lock);
43
44/* called under sysctl_lock */
45static int use_table(struct ctl_table_header *p)
46{
47 if (unlikely(p->unregistering))
48 return 0;
49 p->used++;
50 return 1;
51}
52
53/* called under sysctl_lock */
54static void unuse_table(struct ctl_table_header *p)
55{
56 if (!--p->used)
57 if (unlikely(p->unregistering))
58 complete(p->unregistering);
59}
60
61/* called under sysctl_lock, will reacquire if has to wait */
62static void start_unregistering(struct ctl_table_header *p)
63{
64 /*
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
67 */
68 if (unlikely(p->used)) {
69 struct completion wait;
70 init_completion(&wait);
71 p->unregistering = &wait;
72 spin_unlock(&sysctl_lock);
73 wait_for_completion(&wait);
74 spin_lock(&sysctl_lock);
75 } else {
76 /* anything non-NULL; we'll never dereference it */
77 p->unregistering = ERR_PTR(-EINVAL);
78 }
79 /*
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
82 */
83 list_del_init(&p->ctl_entry);
84}
85
86static void sysctl_head_get(struct ctl_table_header *head)
87{
88 spin_lock(&sysctl_lock);
89 head->count++;
90 spin_unlock(&sysctl_lock);
91}
92
93void sysctl_head_put(struct ctl_table_header *head)
94{
95 spin_lock(&sysctl_lock);
96 if (!--head->count)
97 kfree_rcu(head, rcu);
98 spin_unlock(&sysctl_lock);
99}
100
101static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
102{
103 if (!head)
104 BUG();
105 spin_lock(&sysctl_lock);
106 if (!use_table(head))
107 head = ERR_PTR(-ENOENT);
108 spin_unlock(&sysctl_lock);
109 return head;
110}
111
112static void sysctl_head_finish(struct ctl_table_header *head)
113{
114 if (!head)
115 return;
116 spin_lock(&sysctl_lock);
117 unuse_table(head);
118 spin_unlock(&sysctl_lock);
119}
120
121static struct ctl_table_set *
122lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
123{
124 struct ctl_table_set *set = &root->default_set;
125 if (root->lookup)
126 set = root->lookup(root, namespaces);
127 return set;
128}
129
130static struct list_head *
131lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
132{
133 struct ctl_table_set *set = lookup_header_set(root, namespaces);
134 return &set->list;
135}
136
137static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
138 struct ctl_table_header *prev)
139{
140 struct ctl_table_root *root;
141 struct list_head *header_list;
142 struct ctl_table_header *head;
143 struct list_head *tmp;
144
145 spin_lock(&sysctl_lock);
146 if (prev) {
147 head = prev;
148 tmp = &prev->ctl_entry;
149 unuse_table(prev);
150 goto next;
151 }
152 tmp = &root_table_header.ctl_entry;
153 for (;;) {
154 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
155
156 if (!use_table(head))
157 goto next;
158 spin_unlock(&sysctl_lock);
159 return head;
160 next:
161 root = head->root;
162 tmp = tmp->next;
163 header_list = lookup_header_list(root, namespaces);
164 if (tmp != header_list)
165 continue;
166
167 do {
168 root = list_entry(root->root_list.next,
169 struct ctl_table_root, root_list);
170 if (root == &sysctl_table_root)
171 goto out;
172 header_list = lookup_header_list(root, namespaces);
173 } while (list_empty(header_list));
174 tmp = header_list->next;
175 }
176out:
177 spin_unlock(&sysctl_lock);
178 return NULL;
179}
180
181static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
182{
183 return __sysctl_head_next(current->nsproxy, prev);
184}
185
186void register_sysctl_root(struct ctl_table_root *root)
187{
188 spin_lock(&sysctl_lock);
189 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
190 spin_unlock(&sysctl_lock);
191}
192
193/*
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
196 */
197
198static int test_perm(int mode, int op)
199{
200 if (!current_euid())
201 mode >>= 6;
202 else if (in_egroup_p(0))
203 mode >>= 3;
204 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
205 return 0;
206 return -EACCES;
207}
208
209static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
210{
211 int mode;
212
213 if (root->permissions)
214 mode = root->permissions(root, current->nsproxy, table);
215 else
216 mode = table->mode;
217
218 return test_perm(mode, op);
219}
220
221static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
222{
223 for (; table->procname; table++) {
224 table->parent = parent;
225 if (table->child)
226 sysctl_set_parent(table, table->child);
227 }
228}
229
230
Al Viro90434762008-07-15 08:54:06 -0400231static struct inode *proc_sys_make_inode(struct super_block *sb,
232 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800233{
234 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400235 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800236
Al Viro90434762008-07-15 08:54:06 -0400237 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800238 if (!inode)
239 goto out;
240
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400241 inode->i_ino = get_next_ino();
242
Al Viro90434762008-07-15 08:54:06 -0400243 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800244 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400245 ei->sysctl = head;
246 ei->sysctl_entry = table;
247
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800248 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400249 inode->i_mode = table->mode;
250 if (!table->child) {
251 inode->i_mode |= S_IFREG;
252 inode->i_op = &proc_sys_inode_operations;
253 inode->i_fop = &proc_sys_file_operations;
254 } else {
255 inode->i_mode |= S_IFDIR;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200256 clear_nlink(inode);
Al Viro90434762008-07-15 08:54:06 -0400257 inode->i_op = &proc_sys_dir_operations;
258 inode->i_fop = &proc_sys_dir_file_operations;
259 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800260out:
261 return inode;
262}
263
Al Viro90434762008-07-15 08:54:06 -0400264static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800265{
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700266 for ( ; p->procname; p++) {
Lucas De Marchi36885d72011-06-10 02:36:05 -0300267 if (strlen(p->procname) != name->len)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800268 continue;
269
Lucas De Marchi36885d72011-06-10 02:36:05 -0300270 if (memcmp(p->procname, name->name, name->len) != 0)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800271 continue;
272
273 /* I have a match */
Al Viro90434762008-07-15 08:54:06 -0400274 return p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800275 }
276 return NULL;
277}
278
Adrian Bunk81324362008-10-03 00:33:54 +0400279static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800280{
Al Viro90434762008-07-15 08:54:06 -0400281 if (PROC_I(inode)->sysctl)
282 return sysctl_head_grab(PROC_I(inode)->sysctl);
283 else
284 return sysctl_head_next(NULL);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800285}
286
287static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
288 struct nameidata *nd)
289{
Al Viro90434762008-07-15 08:54:06 -0400290 struct ctl_table_header *head = grab_header(dir);
291 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
292 struct ctl_table_header *h = NULL;
293 struct qstr *name = &dentry->d_name;
294 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800295 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400296 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800297
Al Viro90434762008-07-15 08:54:06 -0400298 if (IS_ERR(head))
299 return ERR_CAST(head);
300
301 if (table && !table->child) {
302 WARN_ON(1);
303 goto out;
304 }
305
306 table = table ? table->child : head->ctl_table;
307
308 p = find_in_table(table, name);
309 if (!p) {
310 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
311 if (h->attached_to != table)
312 continue;
313 p = find_in_table(h->attached_by, name);
314 if (p)
315 break;
316 }
317 }
318
319 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800320 goto out;
321
322 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400323 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
324 if (h)
325 sysctl_head_finish(h);
326
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800327 if (!inode)
328 goto out;
329
330 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100331 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800332 d_add(dentry, inode);
333
334out:
335 sysctl_head_finish(head);
336 return err;
337}
338
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700339static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
340 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800341{
Al Viro90434762008-07-15 08:54:06 -0400342 struct inode *inode = filp->f_path.dentry->d_inode;
343 struct ctl_table_header *head = grab_header(inode);
344 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100345 ssize_t error;
346 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800347
Al Viro90434762008-07-15 08:54:06 -0400348 if (IS_ERR(head))
349 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800350
351 /*
352 * At this point we know that the sysctl was not unregistered
353 * and won't be until we finish.
354 */
355 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700356 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800357 goto out;
358
Al Viro90434762008-07-15 08:54:06 -0400359 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
360 error = -EINVAL;
361 if (!table->proc_handler)
362 goto out;
363
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800364 /* careful: calling conventions are nasty here */
365 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700366 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800367 if (!error)
368 error = res;
369out:
370 sysctl_head_finish(head);
371
372 return error;
373}
374
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700375static ssize_t proc_sys_read(struct file *filp, char __user *buf,
376 size_t count, loff_t *ppos)
377{
378 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
379}
380
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800381static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
382 size_t count, loff_t *ppos)
383{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700384 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800385}
386
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700387static int proc_sys_open(struct inode *inode, struct file *filp)
388{
389 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
390
391 if (table->poll)
392 filp->private_data = proc_sys_poll_event(table->poll);
393
394 return 0;
395}
396
397static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
398{
399 struct inode *inode = filp->f_path.dentry->d_inode;
400 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
401 unsigned long event = (unsigned long)filp->private_data;
402 unsigned int ret = DEFAULT_POLLMASK;
403
404 if (!table->proc_handler)
405 goto out;
406
407 if (!table->poll)
408 goto out;
409
410 poll_wait(filp, &table->poll->wait, wait);
411
412 if (event != atomic_read(&table->poll->event)) {
413 filp->private_data = proc_sys_poll_event(table->poll);
414 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
415 }
416
417out:
418 return ret;
419}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800420
421static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400422 filldir_t filldir,
423 struct ctl_table_header *head,
424 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800425{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800426 struct dentry *child, *dir = filp->f_path.dentry;
427 struct inode *inode;
428 struct qstr qname;
429 ino_t ino = 0;
430 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800431
432 qname.name = table->procname;
433 qname.len = strlen(table->procname);
434 qname.hash = full_name_hash(qname.name, qname.len);
435
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800436 child = d_lookup(dir, &qname);
437 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400438 child = d_alloc(dir, &qname);
439 if (child) {
440 inode = proc_sys_make_inode(dir->d_sb, head, table);
441 if (!inode) {
442 dput(child);
443 return -ENOMEM;
444 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100445 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400446 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800447 }
Al Viro90434762008-07-15 08:54:06 -0400448 } else {
449 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800450 }
451 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800452 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400453 ino = inode->i_ino;
454 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800455 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400456 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
457}
458
459static int scan(struct ctl_table_header *head, ctl_table *table,
460 unsigned long *pos, struct file *file,
461 void *dirent, filldir_t filldir)
462{
463
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700464 for (; table->procname; table++, (*pos)++) {
Al Viro90434762008-07-15 08:54:06 -0400465 int res;
466
Al Viro90434762008-07-15 08:54:06 -0400467 if (*pos < file->f_pos)
468 continue;
469
470 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
471 if (res)
472 return res;
473
474 file->f_pos = *pos + 1;
475 }
476 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800477}
478
479static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
480{
Al Viro90434762008-07-15 08:54:06 -0400481 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800482 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400483 struct ctl_table_header *head = grab_header(inode);
484 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
485 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800486 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400487 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800488
Al Viro90434762008-07-15 08:54:06 -0400489 if (IS_ERR(head))
490 return PTR_ERR(head);
491
492 if (table && !table->child) {
493 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800494 goto out;
Al Viro90434762008-07-15 08:54:06 -0400495 }
496
497 table = table ? table->child : head->ctl_table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800498
499 ret = 0;
500 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
501 if (filp->f_pos == 0) {
502 if (filldir(dirent, ".", 1, filp->f_pos,
503 inode->i_ino, DT_DIR) < 0)
504 goto out;
505 filp->f_pos++;
506 }
507 if (filp->f_pos == 1) {
508 if (filldir(dirent, "..", 2, filp->f_pos,
509 parent_ino(dentry), DT_DIR) < 0)
510 goto out;
511 filp->f_pos++;
512 }
513 pos = 2;
514
Al Viro90434762008-07-15 08:54:06 -0400515 ret = scan(head, table, &pos, filp, dirent, filldir);
516 if (ret)
517 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800518
Al Viro90434762008-07-15 08:54:06 -0400519 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
520 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800521 continue;
Al Viro90434762008-07-15 08:54:06 -0400522 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
523 if (ret) {
524 sysctl_head_finish(h);
525 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800526 }
527 }
528 ret = 1;
529out:
530 sysctl_head_finish(head);
531 return ret;
532}
533
Al Viro10556cb2011-06-20 19:28:19 -0400534static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800535{
536 /*
537 * sysctl entries that are not writeable,
538 * are _NOT_ writeable, capabilities or not.
539 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200540 struct ctl_table_header *head;
541 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800542 int error;
543
Miklos Szeredif696a362008-07-31 13:41:58 +0200544 /* Executable files are not allowed under /proc/sys/ */
545 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
546 return -EACCES;
547
548 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400549 if (IS_ERR(head))
550 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800551
Miklos Szeredif696a362008-07-31 13:41:58 +0200552 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400553 if (!table) /* global root - r-xr-xr-x */
554 error = mask & MAY_WRITE ? -EACCES : 0;
555 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400556 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800557
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800558 sysctl_head_finish(head);
559 return error;
560}
561
562static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
563{
564 struct inode *inode = dentry->d_inode;
565 int error;
566
567 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
568 return -EPERM;
569
570 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200571 if (error)
572 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800573
Christoph Hellwig10257742010-06-04 11:30:02 +0200574 if ((attr->ia_valid & ATTR_SIZE) &&
575 attr->ia_size != i_size_read(inode)) {
576 error = vmtruncate(inode, attr->ia_size);
577 if (error)
578 return error;
579 }
580
581 setattr_copy(inode, attr);
582 mark_inode_dirty(inode);
583 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800584}
585
Al Viro90434762008-07-15 08:54:06 -0400586static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
587{
588 struct inode *inode = dentry->d_inode;
589 struct ctl_table_header *head = grab_header(inode);
590 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
591
592 if (IS_ERR(head))
593 return PTR_ERR(head);
594
595 generic_fillattr(inode, stat);
596 if (table)
597 stat->mode = (stat->mode & S_IFMT) | table->mode;
598
599 sysctl_head_finish(head);
600 return 0;
601}
602
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800603static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700604 .open = proc_sys_open,
605 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800606 .read = proc_sys_read,
607 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200608 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400609};
610
611static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700612 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800613 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200614 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800615};
616
Jan Engelhardt03a44822008-02-08 04:21:19 -0800617static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400618 .permission = proc_sys_permission,
619 .setattr = proc_sys_setattr,
620 .getattr = proc_sys_getattr,
621};
622
623static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800624 .lookup = proc_sys_lookup,
625 .permission = proc_sys_permission,
626 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400627 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800628};
629
630static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
631{
Nick Piggin34286d62011-01-07 17:49:57 +1100632 if (nd->flags & LOOKUP_RCU)
633 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400634 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
635}
636
Nick Pigginfe15ce42011-01-07 17:49:23 +1100637static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400638{
639 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
640}
641
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800642static int sysctl_is_seen(struct ctl_table_header *p)
643{
644 struct ctl_table_set *set = p->set;
645 int res;
646 spin_lock(&sysctl_lock);
647 if (p->unregistering)
648 res = 0;
649 else if (!set->is_seen)
650 res = 1;
651 else
652 res = set->is_seen(set);
653 spin_unlock(&sysctl_lock);
654 return res;
655}
656
Nick Piggin621e1552011-01-07 17:49:27 +1100657static int proc_sys_compare(const struct dentry *parent,
658 const struct inode *pinode,
659 const struct dentry *dentry, const struct inode *inode,
660 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400661{
Al Virodfef6dc2011-03-08 01:25:28 -0500662 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100663 /* Although proc doesn't have negative dentries, rcu-walk means
664 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500665 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100666 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500667 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100668 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400669 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100670 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400671 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500672 head = rcu_dereference(PROC_I(inode)->sysctl);
673 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800674}
675
Al Virod72f71e2009-02-20 05:58:47 +0000676static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800677 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400678 .d_delete = proc_sys_delete,
679 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800680};
681
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800682static struct ctl_table *is_branch_in(struct ctl_table *branch,
683 struct ctl_table *table)
684{
685 struct ctl_table *p;
686 const char *s = branch->procname;
687
688 /* branch should have named subdirectory as its first element */
689 if (!s || !branch->child)
690 return NULL;
691
692 /* ... and nothing else */
693 if (branch[1].procname)
694 return NULL;
695
696 /* table should contain subdirectory with the same name */
697 for (p = table; p->procname; p++) {
698 if (!p->child)
699 continue;
700 if (p->procname && strcmp(p->procname, s) == 0)
701 return p;
702 }
703 return NULL;
704}
705
706/* see if attaching q to p would be an improvement */
707static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
708{
709 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
710 struct ctl_table *next;
711 int is_better = 0;
712 int not_in_parent = !p->attached_by;
713
714 while ((next = is_branch_in(by, to)) != NULL) {
715 if (by == q->attached_by)
716 is_better = 1;
717 if (to == p->attached_by)
718 not_in_parent = 1;
719 by = by->child;
720 to = next->child;
721 }
722
723 if (is_better && not_in_parent) {
724 q->attached_by = by;
725 q->attached_to = to;
726 q->parent = p;
727 }
728}
729
730#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
731static int sysctl_depth(struct ctl_table *table)
732{
733 struct ctl_table *tmp;
734 int depth;
735
736 depth = 0;
737 for (tmp = table; tmp->parent; tmp = tmp->parent)
738 depth++;
739
740 return depth;
741}
742
743static struct ctl_table *sysctl_parent(struct ctl_table *table, int n)
744{
745 int i;
746
747 for (i = 0; table && i < n; i++)
748 table = table->parent;
749
750 return table;
751}
752
753
754static void sysctl_print_path(struct ctl_table *table)
755{
756 struct ctl_table *tmp;
757 int depth, i;
758 depth = sysctl_depth(table);
759 if (table->procname) {
760 for (i = depth; i >= 0; i--) {
761 tmp = sysctl_parent(table, i);
762 printk("/%s", tmp->procname?tmp->procname:"");
763 }
764 }
765 printk(" ");
766}
767
768static struct ctl_table *sysctl_check_lookup(struct nsproxy *namespaces,
769 struct ctl_table *table)
770{
771 struct ctl_table_header *head;
772 struct ctl_table *ref, *test;
773 int depth, cur_depth;
774
775 depth = sysctl_depth(table);
776
777 for (head = __sysctl_head_next(namespaces, NULL); head;
778 head = __sysctl_head_next(namespaces, head)) {
779 cur_depth = depth;
780 ref = head->ctl_table;
781repeat:
782 test = sysctl_parent(table, cur_depth);
783 for (; ref->procname; ref++) {
784 int match = 0;
785 if (cur_depth && !ref->child)
786 continue;
787
788 if (test->procname && ref->procname &&
789 (strcmp(test->procname, ref->procname) == 0))
790 match++;
791
792 if (match) {
793 if (cur_depth != 0) {
794 cur_depth--;
795 ref = ref->child;
796 goto repeat;
797 }
798 goto out;
799 }
800 }
801 }
802 ref = NULL;
803out:
804 sysctl_head_finish(head);
805 return ref;
806}
807
808static void set_fail(const char **fail, struct ctl_table *table, const char *str)
809{
810 if (*fail) {
811 printk(KERN_ERR "sysctl table check failed: ");
812 sysctl_print_path(table);
813 printk(" %s\n", *fail);
814 dump_stack();
815 }
816 *fail = str;
817}
818
819static void sysctl_check_leaf(struct nsproxy *namespaces,
820 struct ctl_table *table, const char **fail)
821{
822 struct ctl_table *ref;
823
824 ref = sysctl_check_lookup(namespaces, table);
825 if (ref && (ref != table))
826 set_fail(fail, table, "Sysctl already exists");
827}
828
829static int sysctl_check_table(struct nsproxy *namespaces, struct ctl_table *table)
830{
831 int error = 0;
832 for (; table->procname; table++) {
833 const char *fail = NULL;
834
835 if (table->parent) {
836 if (!table->parent->procname)
837 set_fail(&fail, table, "Parent without procname");
838 }
839 if (table->child) {
840 if (table->data)
841 set_fail(&fail, table, "Directory with data?");
842 if (table->maxlen)
843 set_fail(&fail, table, "Directory with maxlen?");
844 if ((table->mode & (S_IRUGO|S_IXUGO)) != table->mode)
845 set_fail(&fail, table, "Writable sysctl directory");
846 if (table->proc_handler)
847 set_fail(&fail, table, "Directory with proc_handler");
848 if (table->extra1)
849 set_fail(&fail, table, "Directory with extra1");
850 if (table->extra2)
851 set_fail(&fail, table, "Directory with extra2");
852 } else {
853 if ((table->proc_handler == proc_dostring) ||
854 (table->proc_handler == proc_dointvec) ||
855 (table->proc_handler == proc_dointvec_minmax) ||
856 (table->proc_handler == proc_dointvec_jiffies) ||
857 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
858 (table->proc_handler == proc_dointvec_ms_jiffies) ||
859 (table->proc_handler == proc_doulongvec_minmax) ||
860 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
861 if (!table->data)
862 set_fail(&fail, table, "No data");
863 if (!table->maxlen)
864 set_fail(&fail, table, "No maxlen");
865 }
866#ifdef CONFIG_PROC_SYSCTL
867 if (!table->proc_handler)
868 set_fail(&fail, table, "No proc_handler");
869#endif
870 sysctl_check_leaf(namespaces, table, &fail);
871 }
872 if (table->mode > 0777)
873 set_fail(&fail, table, "bogus .mode");
874 if (fail) {
875 set_fail(&fail, table, NULL);
876 error = -EINVAL;
877 }
878 if (table->child)
879 error |= sysctl_check_table(namespaces, table->child);
880 }
881 return error;
882}
883#endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
884
885/**
886 * __register_sysctl_paths - register a sysctl hierarchy
887 * @root: List of sysctl headers to register on
888 * @namespaces: Data to compute which lists of sysctl entries are visible
889 * @path: The path to the directory the sysctl table is in.
890 * @table: the top-level table structure
891 *
892 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
893 * array. A completely 0 filled entry terminates the table.
894 *
895 * The members of the &struct ctl_table structure are used as follows:
896 *
897 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
898 * enter a sysctl file
899 *
900 * data - a pointer to data for use by proc_handler
901 *
902 * maxlen - the maximum size in bytes of the data
903 *
904 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
905 *
906 * child - a pointer to the child sysctl table if this entry is a directory, or
907 * %NULL.
908 *
909 * proc_handler - the text handler routine (described below)
910 *
911 * de - for internal use by the sysctl routines
912 *
913 * extra1, extra2 - extra pointers usable by the proc handler routines
914 *
915 * Leaf nodes in the sysctl tree will be represented by a single file
916 * under /proc; non-leaf nodes will be represented by directories.
917 *
918 * sysctl(2) can automatically manage read and write requests through
919 * the sysctl table. The data and maxlen fields of the ctl_table
920 * struct enable minimal validation of the values being written to be
921 * performed, and the mode field allows minimal authentication.
922 *
923 * There must be a proc_handler routine for any terminal nodes
924 * mirrored under /proc/sys (non-terminals are handled by a built-in
925 * directory handler). Several default handlers are available to
926 * cover common cases -
927 *
928 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
929 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
930 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
931 *
932 * It is the handler's job to read the input buffer from user memory
933 * and process it. The handler should return 0 on success.
934 *
935 * This routine returns %NULL on a failure to register, and a pointer
936 * to the table header on success.
937 */
938struct ctl_table_header *__register_sysctl_paths(
939 struct ctl_table_root *root,
940 struct nsproxy *namespaces,
941 const struct ctl_path *path, struct ctl_table *table)
942{
943 struct ctl_table_header *header;
944 struct ctl_table *new, **prevp;
945 unsigned int n, npath;
946 struct ctl_table_set *set;
947
948 /* Count the path components */
949 for (npath = 0; path[npath].procname; ++npath)
950 ;
951
952 /*
953 * For each path component, allocate a 2-element ctl_table array.
954 * The first array element will be filled with the sysctl entry
955 * for this, the second will be the sentinel (procname == 0).
956 *
957 * We allocate everything in one go so that we don't have to
958 * worry about freeing additional memory in unregister_sysctl_table.
959 */
960 header = kzalloc(sizeof(struct ctl_table_header) +
961 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
962 if (!header)
963 return NULL;
964
965 new = (struct ctl_table *) (header + 1);
966
967 /* Now connect the dots */
968 prevp = &header->ctl_table;
969 for (n = 0; n < npath; ++n, ++path) {
970 /* Copy the procname */
971 new->procname = path->procname;
972 new->mode = 0555;
973
974 *prevp = new;
975 prevp = &new->child;
976
977 new += 2;
978 }
979 *prevp = table;
980 header->ctl_table_arg = table;
981
982 INIT_LIST_HEAD(&header->ctl_entry);
983 header->used = 0;
984 header->unregistering = NULL;
985 header->root = root;
986 sysctl_set_parent(NULL, header->ctl_table);
987 header->count = 1;
988#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
989 if (sysctl_check_table(namespaces, header->ctl_table)) {
990 kfree(header);
991 return NULL;
992 }
993#endif
994 spin_lock(&sysctl_lock);
995 header->set = lookup_header_set(root, namespaces);
996 header->attached_by = header->ctl_table;
997 header->attached_to = root_table;
998 header->parent = &root_table_header;
999 for (set = header->set; set; set = set->parent) {
1000 struct ctl_table_header *p;
1001 list_for_each_entry(p, &set->list, ctl_entry) {
1002 if (p->unregistering)
1003 continue;
1004 try_attach(p, header);
1005 }
1006 }
1007 header->parent->count++;
1008 list_add_tail(&header->ctl_entry, &header->set->list);
1009 spin_unlock(&sysctl_lock);
1010
1011 return header;
1012}
1013
1014/**
1015 * register_sysctl_table_path - register a sysctl table hierarchy
1016 * @path: The path to the directory the sysctl table is in.
1017 * @table: the top-level table structure
1018 *
1019 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1020 * array. A completely 0 filled entry terminates the table.
1021 *
1022 * See __register_sysctl_paths for more details.
1023 */
1024struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1025 struct ctl_table *table)
1026{
1027 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1028 path, table);
1029}
1030EXPORT_SYMBOL(register_sysctl_paths);
1031
1032/**
1033 * register_sysctl_table - register a sysctl table hierarchy
1034 * @table: the top-level table structure
1035 *
1036 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1037 * array. A completely 0 filled entry terminates the table.
1038 *
1039 * See register_sysctl_paths for more details.
1040 */
1041struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1042{
1043 static const struct ctl_path null_path[] = { {} };
1044
1045 return register_sysctl_paths(null_path, table);
1046}
1047EXPORT_SYMBOL(register_sysctl_table);
1048
1049/**
1050 * unregister_sysctl_table - unregister a sysctl table hierarchy
1051 * @header: the header returned from register_sysctl_table
1052 *
1053 * Unregisters the sysctl table and all children. proc entries may not
1054 * actually be removed until they are no longer used by anyone.
1055 */
1056void unregister_sysctl_table(struct ctl_table_header * header)
1057{
1058 might_sleep();
1059
1060 if (header == NULL)
1061 return;
1062
1063 spin_lock(&sysctl_lock);
1064 start_unregistering(header);
1065 if (!--header->parent->count) {
1066 WARN_ON(1);
1067 kfree_rcu(header->parent, rcu);
1068 }
1069 if (!--header->count)
1070 kfree_rcu(header, rcu);
1071 spin_unlock(&sysctl_lock);
1072}
1073EXPORT_SYMBOL(unregister_sysctl_table);
1074
1075void setup_sysctl_set(struct ctl_table_set *p,
1076 struct ctl_table_set *parent,
1077 int (*is_seen)(struct ctl_table_set *))
1078{
1079 INIT_LIST_HEAD(&p->list);
1080 p->parent = parent ? parent : &sysctl_table_root.default_set;
1081 p->is_seen = is_seen;
1082}
1083
1084
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001085int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001086{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001087 struct proc_dir_entry *proc_sys_root;
1088
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001089 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001090 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1091 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001092 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001093
1094 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001095}