blob: 65c13dddceaea79111d20d75f0ea0f441bdb555f [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),
Eric W. Biederman9eb47c22012-01-22 21:26:00 -080048 .default_set.root = &sysctl_table_root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080049};
50
51static DEFINE_SPINLOCK(sysctl_lock);
52
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080053static int namecmp(const char *name1, int len1, const char *name2, int len2)
54{
55 int minlen;
56 int cmp;
57
58 minlen = len1;
59 if (minlen > len2)
60 minlen = len2;
61
62 cmp = memcmp(name1, name2, minlen);
63 if (cmp == 0)
64 cmp = len1 - len2;
65 return cmp;
66}
67
68static struct ctl_table *find_entry(struct ctl_table_header **phead,
69 struct ctl_table_set *set,
70 struct ctl_table_header *dir_head, struct ctl_table *dir,
71 const char *name, int namelen)
72{
73 struct ctl_table_header *head;
74 struct ctl_table *entry;
75
76 if (dir_head->set == set) {
77 for (entry = dir; entry->procname; entry++) {
78 const char *procname = entry->procname;
79 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
80 *phead = dir_head;
81 return entry;
82 }
83 }
84 }
85
86 list_for_each_entry(head, &set->list, ctl_entry) {
87 if (head->unregistering)
88 continue;
89 if (head->attached_to != dir)
90 continue;
91 for (entry = head->attached_by; entry->procname; entry++) {
92 const char *procname = entry->procname;
93 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
94 *phead = head;
95 return entry;
96 }
97 }
98 }
99 return NULL;
100}
101
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800102static void init_header(struct ctl_table_header *head,
103 struct ctl_table_root *root, struct ctl_table_set *set,
104 struct ctl_table *table)
105{
106 head->ctl_table_arg = table;
107 INIT_LIST_HEAD(&head->ctl_entry);
108 head->used = 0;
109 head->count = 1;
110 head->nreg = 1;
111 head->unregistering = NULL;
112 head->root = root;
113 head->set = set;
114 head->parent = NULL;
115}
116
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800117static void erase_header(struct ctl_table_header *head)
118{
119 list_del_init(&head->ctl_entry);
120}
121
122static void insert_header(struct ctl_table_header *header)
123{
124 header->parent->count++;
125 list_add_tail(&header->ctl_entry, &header->set->list);
126}
127
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800128/* called under sysctl_lock */
129static int use_table(struct ctl_table_header *p)
130{
131 if (unlikely(p->unregistering))
132 return 0;
133 p->used++;
134 return 1;
135}
136
137/* called under sysctl_lock */
138static void unuse_table(struct ctl_table_header *p)
139{
140 if (!--p->used)
141 if (unlikely(p->unregistering))
142 complete(p->unregistering);
143}
144
145/* called under sysctl_lock, will reacquire if has to wait */
146static void start_unregistering(struct ctl_table_header *p)
147{
148 /*
149 * if p->used is 0, nobody will ever touch that entry again;
150 * we'll eliminate all paths to it before dropping sysctl_lock
151 */
152 if (unlikely(p->used)) {
153 struct completion wait;
154 init_completion(&wait);
155 p->unregistering = &wait;
156 spin_unlock(&sysctl_lock);
157 wait_for_completion(&wait);
158 spin_lock(&sysctl_lock);
159 } else {
160 /* anything non-NULL; we'll never dereference it */
161 p->unregistering = ERR_PTR(-EINVAL);
162 }
163 /*
164 * do not remove from the list until nobody holds it; walking the
165 * list in do_sysctl() relies on that.
166 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800167 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800168}
169
170static void sysctl_head_get(struct ctl_table_header *head)
171{
172 spin_lock(&sysctl_lock);
173 head->count++;
174 spin_unlock(&sysctl_lock);
175}
176
177void sysctl_head_put(struct ctl_table_header *head)
178{
179 spin_lock(&sysctl_lock);
180 if (!--head->count)
181 kfree_rcu(head, rcu);
182 spin_unlock(&sysctl_lock);
183}
184
185static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
186{
187 if (!head)
188 BUG();
189 spin_lock(&sysctl_lock);
190 if (!use_table(head))
191 head = ERR_PTR(-ENOENT);
192 spin_unlock(&sysctl_lock);
193 return head;
194}
195
196static void sysctl_head_finish(struct ctl_table_header *head)
197{
198 if (!head)
199 return;
200 spin_lock(&sysctl_lock);
201 unuse_table(head);
202 spin_unlock(&sysctl_lock);
203}
204
205static struct ctl_table_set *
206lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
207{
208 struct ctl_table_set *set = &root->default_set;
209 if (root->lookup)
210 set = root->lookup(root, namespaces);
211 return set;
212}
213
214static struct list_head *
215lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
216{
217 struct ctl_table_set *set = lookup_header_set(root, namespaces);
218 return &set->list;
219}
220
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800221static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
222 struct ctl_table_header *dir_head,
223 struct ctl_table *dir,
224 const char *name, int namelen)
225{
226 struct ctl_table_header *head;
227 struct ctl_table *entry;
228 struct ctl_table_root *root;
229 struct ctl_table_set *set;
230
231 spin_lock(&sysctl_lock);
232 root = &sysctl_table_root;
233 do {
234 set = lookup_header_set(root, current->nsproxy);
235 entry = find_entry(&head, set, dir_head, dir, name, namelen);
236 if (entry && use_table(head))
237 *phead = head;
238 else
239 entry = NULL;
240 root = list_entry(root->root_list.next,
241 struct ctl_table_root, root_list);
242 } while (!entry && root != &sysctl_table_root);
243 spin_unlock(&sysctl_lock);
244 return entry;
245}
246
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800247static struct ctl_table_header *next_usable_entry(struct ctl_table *dir,
248 struct ctl_table_root *root, struct list_head *tmp)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800249{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800250 struct nsproxy *namespaces = current->nsproxy;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800251 struct list_head *header_list;
252 struct ctl_table_header *head;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800253
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800254 goto next;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800255 for (;;) {
256 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800257 root = head->root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800258
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800259 if (head->attached_to != dir ||
260 !head->attached_by->procname ||
261 !use_table(head))
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800262 goto next;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800263
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800264 return head;
265 next:
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800266 tmp = tmp->next;
267 header_list = lookup_header_list(root, namespaces);
268 if (tmp != header_list)
269 continue;
270
271 do {
272 root = list_entry(root->root_list.next,
273 struct ctl_table_root, root_list);
274 if (root == &sysctl_table_root)
275 goto out;
276 header_list = lookup_header_list(root, namespaces);
277 } while (list_empty(header_list));
278 tmp = header_list->next;
279 }
280out:
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800281 return NULL;
282}
283
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800284static void first_entry(
285 struct ctl_table_header *dir_head, struct ctl_table *dir,
286 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800287{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800288 struct ctl_table_header *head = dir_head;
289 struct ctl_table *entry = dir;
290
291 spin_lock(&sysctl_lock);
292 if (entry->procname) {
293 use_table(head);
294 } else {
295 head = next_usable_entry(dir, &sysctl_table_root,
296 &sysctl_table_root.default_set.list);
297 if (head)
298 entry = head->attached_by;
299 }
300 spin_unlock(&sysctl_lock);
301 *phead = head;
302 *pentry = entry;
303}
304
305static void next_entry(struct ctl_table *dir,
306 struct ctl_table_header **phead, struct ctl_table **pentry)
307{
308 struct ctl_table_header *head = *phead;
309 struct ctl_table *entry = *pentry;
310
311 entry++;
312 if (!entry->procname) {
313 struct ctl_table_root *root = head->root;
314 struct list_head *tmp = &head->ctl_entry;
315 if (head->attached_to != dir) {
316 root = &sysctl_table_root;
317 tmp = &sysctl_table_root.default_set.list;
318 }
319 spin_lock(&sysctl_lock);
320 unuse_table(head);
321 head = next_usable_entry(dir, root, tmp);
322 spin_unlock(&sysctl_lock);
323 if (head)
324 entry = head->attached_by;
325 }
326 *phead = head;
327 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800328}
329
330void register_sysctl_root(struct ctl_table_root *root)
331{
332 spin_lock(&sysctl_lock);
333 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
334 spin_unlock(&sysctl_lock);
335}
336
337/*
338 * sysctl_perm does NOT grant the superuser all rights automatically, because
339 * some sysctl variables are readonly even to root.
340 */
341
342static int test_perm(int mode, int op)
343{
344 if (!current_euid())
345 mode >>= 6;
346 else if (in_egroup_p(0))
347 mode >>= 3;
348 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
349 return 0;
350 return -EACCES;
351}
352
353static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
354{
355 int mode;
356
357 if (root->permissions)
358 mode = root->permissions(root, current->nsproxy, table);
359 else
360 mode = table->mode;
361
362 return test_perm(mode, op);
363}
364
Al Viro90434762008-07-15 08:54:06 -0400365static struct inode *proc_sys_make_inode(struct super_block *sb,
366 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800367{
368 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400369 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800370
Al Viro90434762008-07-15 08:54:06 -0400371 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800372 if (!inode)
373 goto out;
374
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400375 inode->i_ino = get_next_ino();
376
Al Viro90434762008-07-15 08:54:06 -0400377 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800378 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400379 ei->sysctl = head;
380 ei->sysctl_entry = table;
381
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800382 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400383 inode->i_mode = table->mode;
384 if (!table->child) {
385 inode->i_mode |= S_IFREG;
386 inode->i_op = &proc_sys_inode_operations;
387 inode->i_fop = &proc_sys_file_operations;
388 } else {
389 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400390 inode->i_op = &proc_sys_dir_operations;
391 inode->i_fop = &proc_sys_dir_file_operations;
392 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800393out:
394 return inode;
395}
396
Adrian Bunk81324362008-10-03 00:33:54 +0400397static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800398{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800399 struct ctl_table_header *head = PROC_I(inode)->sysctl;
400 if (!head)
401 head = &root_table_header;
402 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800403}
404
405static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
406 struct nameidata *nd)
407{
Al Viro90434762008-07-15 08:54:06 -0400408 struct ctl_table_header *head = grab_header(dir);
409 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
410 struct ctl_table_header *h = NULL;
411 struct qstr *name = &dentry->d_name;
412 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800413 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400414 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800415
Al Viro90434762008-07-15 08:54:06 -0400416 if (IS_ERR(head))
417 return ERR_CAST(head);
418
419 if (table && !table->child) {
420 WARN_ON(1);
421 goto out;
422 }
423
Eric W. Biedermana1945582012-01-21 17:51:48 -0800424 table = table ? table->child : &head->ctl_table[1];
Al Viro90434762008-07-15 08:54:06 -0400425
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800426 p = lookup_entry(&h, head, table, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400427 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800428 goto out;
429
430 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400431 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
432 if (h)
433 sysctl_head_finish(h);
434
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800435 if (!inode)
436 goto out;
437
438 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100439 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800440 d_add(dentry, inode);
441
442out:
443 sysctl_head_finish(head);
444 return err;
445}
446
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700447static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
448 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800449{
Al Viro90434762008-07-15 08:54:06 -0400450 struct inode *inode = filp->f_path.dentry->d_inode;
451 struct ctl_table_header *head = grab_header(inode);
452 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100453 ssize_t error;
454 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800455
Al Viro90434762008-07-15 08:54:06 -0400456 if (IS_ERR(head))
457 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800458
459 /*
460 * At this point we know that the sysctl was not unregistered
461 * and won't be until we finish.
462 */
463 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700464 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800465 goto out;
466
Al Viro90434762008-07-15 08:54:06 -0400467 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
468 error = -EINVAL;
469 if (!table->proc_handler)
470 goto out;
471
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800472 /* careful: calling conventions are nasty here */
473 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700474 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800475 if (!error)
476 error = res;
477out:
478 sysctl_head_finish(head);
479
480 return error;
481}
482
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700483static ssize_t proc_sys_read(struct file *filp, char __user *buf,
484 size_t count, loff_t *ppos)
485{
486 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
487}
488
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800489static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
490 size_t count, loff_t *ppos)
491{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700492 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800493}
494
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700495static int proc_sys_open(struct inode *inode, struct file *filp)
496{
497 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
498
499 if (table->poll)
500 filp->private_data = proc_sys_poll_event(table->poll);
501
502 return 0;
503}
504
505static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
506{
507 struct inode *inode = filp->f_path.dentry->d_inode;
508 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
509 unsigned long event = (unsigned long)filp->private_data;
510 unsigned int ret = DEFAULT_POLLMASK;
511
512 if (!table->proc_handler)
513 goto out;
514
515 if (!table->poll)
516 goto out;
517
518 poll_wait(filp, &table->poll->wait, wait);
519
520 if (event != atomic_read(&table->poll->event)) {
521 filp->private_data = proc_sys_poll_event(table->poll);
522 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
523 }
524
525out:
526 return ret;
527}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800528
529static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400530 filldir_t filldir,
531 struct ctl_table_header *head,
532 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800533{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800534 struct dentry *child, *dir = filp->f_path.dentry;
535 struct inode *inode;
536 struct qstr qname;
537 ino_t ino = 0;
538 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800539
540 qname.name = table->procname;
541 qname.len = strlen(table->procname);
542 qname.hash = full_name_hash(qname.name, qname.len);
543
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800544 child = d_lookup(dir, &qname);
545 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400546 child = d_alloc(dir, &qname);
547 if (child) {
548 inode = proc_sys_make_inode(dir->d_sb, head, table);
549 if (!inode) {
550 dput(child);
551 return -ENOMEM;
552 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100553 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400554 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800555 }
Al Viro90434762008-07-15 08:54:06 -0400556 } else {
557 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800558 }
559 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800560 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400561 ino = inode->i_ino;
562 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800563 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400564 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
565}
566
567static int scan(struct ctl_table_header *head, ctl_table *table,
568 unsigned long *pos, struct file *file,
569 void *dirent, filldir_t filldir)
570{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800571 int res;
Al Viro90434762008-07-15 08:54:06 -0400572
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800573 if ((*pos)++ < file->f_pos)
574 return 0;
Al Viro90434762008-07-15 08:54:06 -0400575
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800576 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400577
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800578 if (res == 0)
579 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400580
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800581 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800582}
583
584static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
585{
Al Viro90434762008-07-15 08:54:06 -0400586 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800587 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400588 struct ctl_table_header *head = grab_header(inode);
589 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
590 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800591 struct ctl_table *entry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800592 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400593 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800594
Al Viro90434762008-07-15 08:54:06 -0400595 if (IS_ERR(head))
596 return PTR_ERR(head);
597
598 if (table && !table->child) {
599 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800600 goto out;
Al Viro90434762008-07-15 08:54:06 -0400601 }
602
Eric W. Biedermana1945582012-01-21 17:51:48 -0800603 table = table ? table->child : &head->ctl_table[1];
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800604
605 ret = 0;
606 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
607 if (filp->f_pos == 0) {
608 if (filldir(dirent, ".", 1, filp->f_pos,
609 inode->i_ino, DT_DIR) < 0)
610 goto out;
611 filp->f_pos++;
612 }
613 if (filp->f_pos == 1) {
614 if (filldir(dirent, "..", 2, filp->f_pos,
615 parent_ino(dentry), DT_DIR) < 0)
616 goto out;
617 filp->f_pos++;
618 }
619 pos = 2;
620
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800621 for (first_entry(head, table, &h, &entry); h; next_entry(table, &h, &entry)) {
622 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400623 if (ret) {
624 sysctl_head_finish(h);
625 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800626 }
627 }
628 ret = 1;
629out:
630 sysctl_head_finish(head);
631 return ret;
632}
633
Al Viro10556cb2011-06-20 19:28:19 -0400634static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800635{
636 /*
637 * sysctl entries that are not writeable,
638 * are _NOT_ writeable, capabilities or not.
639 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200640 struct ctl_table_header *head;
641 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800642 int error;
643
Miklos Szeredif696a362008-07-31 13:41:58 +0200644 /* Executable files are not allowed under /proc/sys/ */
645 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
646 return -EACCES;
647
648 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400649 if (IS_ERR(head))
650 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800651
Miklos Szeredif696a362008-07-31 13:41:58 +0200652 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400653 if (!table) /* global root - r-xr-xr-x */
654 error = mask & MAY_WRITE ? -EACCES : 0;
655 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400656 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800657
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800658 sysctl_head_finish(head);
659 return error;
660}
661
662static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
663{
664 struct inode *inode = dentry->d_inode;
665 int error;
666
667 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
668 return -EPERM;
669
670 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200671 if (error)
672 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800673
Christoph Hellwig10257742010-06-04 11:30:02 +0200674 if ((attr->ia_valid & ATTR_SIZE) &&
675 attr->ia_size != i_size_read(inode)) {
676 error = vmtruncate(inode, attr->ia_size);
677 if (error)
678 return error;
679 }
680
681 setattr_copy(inode, attr);
682 mark_inode_dirty(inode);
683 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800684}
685
Al Viro90434762008-07-15 08:54:06 -0400686static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
687{
688 struct inode *inode = dentry->d_inode;
689 struct ctl_table_header *head = grab_header(inode);
690 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
691
692 if (IS_ERR(head))
693 return PTR_ERR(head);
694
695 generic_fillattr(inode, stat);
696 if (table)
697 stat->mode = (stat->mode & S_IFMT) | table->mode;
698
699 sysctl_head_finish(head);
700 return 0;
701}
702
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800703static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700704 .open = proc_sys_open,
705 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800706 .read = proc_sys_read,
707 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200708 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400709};
710
711static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700712 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800713 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200714 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800715};
716
Jan Engelhardt03a44822008-02-08 04:21:19 -0800717static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400718 .permission = proc_sys_permission,
719 .setattr = proc_sys_setattr,
720 .getattr = proc_sys_getattr,
721};
722
723static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800724 .lookup = proc_sys_lookup,
725 .permission = proc_sys_permission,
726 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400727 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800728};
729
730static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
731{
Nick Piggin34286d62011-01-07 17:49:57 +1100732 if (nd->flags & LOOKUP_RCU)
733 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400734 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
735}
736
Nick Pigginfe15ce42011-01-07 17:49:23 +1100737static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400738{
739 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
740}
741
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800742static int sysctl_is_seen(struct ctl_table_header *p)
743{
744 struct ctl_table_set *set = p->set;
745 int res;
746 spin_lock(&sysctl_lock);
747 if (p->unregistering)
748 res = 0;
749 else if (!set->is_seen)
750 res = 1;
751 else
752 res = set->is_seen(set);
753 spin_unlock(&sysctl_lock);
754 return res;
755}
756
Nick Piggin621e1552011-01-07 17:49:27 +1100757static int proc_sys_compare(const struct dentry *parent,
758 const struct inode *pinode,
759 const struct dentry *dentry, const struct inode *inode,
760 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400761{
Al Virodfef6dc2011-03-08 01:25:28 -0500762 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100763 /* Although proc doesn't have negative dentries, rcu-walk means
764 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500765 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100766 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500767 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100768 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400769 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100770 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400771 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500772 head = rcu_dereference(PROC_I(inode)->sysctl);
773 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800774}
775
Al Virod72f71e2009-02-20 05:58:47 +0000776static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800777 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400778 .d_delete = proc_sys_delete,
779 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800780};
781
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800782static struct ctl_table *is_branch_in(struct ctl_table *branch,
783 struct ctl_table *table)
784{
785 struct ctl_table *p;
786 const char *s = branch->procname;
787
788 /* branch should have named subdirectory as its first element */
789 if (!s || !branch->child)
790 return NULL;
791
792 /* ... and nothing else */
793 if (branch[1].procname)
794 return NULL;
795
796 /* table should contain subdirectory with the same name */
797 for (p = table; p->procname; p++) {
798 if (!p->child)
799 continue;
800 if (p->procname && strcmp(p->procname, s) == 0)
801 return p;
802 }
803 return NULL;
804}
805
806/* see if attaching q to p would be an improvement */
807static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
808{
809 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
810 struct ctl_table *next;
811 int is_better = 0;
812 int not_in_parent = !p->attached_by;
813
814 while ((next = is_branch_in(by, to)) != NULL) {
815 if (by == q->attached_by)
816 is_better = 1;
817 if (to == p->attached_by)
818 not_in_parent = 1;
819 by = by->child;
820 to = next->child;
821 }
822
823 if (is_better && not_in_parent) {
824 q->attached_by = by;
825 q->attached_to = to;
826 q->parent = p;
827 }
828}
829
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800830static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
831 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800832{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800833 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800834 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800835
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800836 for (entry = old; entry->procname; entry++) {
837 for (test = table; test->procname; test++) {
838 if (strcmp(entry->procname, test->procname) == 0) {
839 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
840 path, test->procname);
841 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800842 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800843 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800844 }
845 return error;
846}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800847
848static int sysctl_check_dups(struct nsproxy *namespaces,
849 struct ctl_table_header *header,
850 const char *path, struct ctl_table *table)
851{
852 struct ctl_table_root *root;
853 struct ctl_table_set *set;
854 struct ctl_table_header *dir_head, *head;
855 struct ctl_table *dir_table;
856 int error = 0;
857
858 /* No dups if we are the only member of our directory */
859 if (header->attached_by != table)
860 return 0;
861
862 dir_head = header->parent;
863 dir_table = header->attached_to;
864
865 error = sysctl_check_table_dups(path, dir_table, table);
866
867 root = &sysctl_table_root;
868 do {
869 set = lookup_header_set(root, namespaces);
870
871 list_for_each_entry(head, &set->list, ctl_entry) {
872 if (head->unregistering)
873 continue;
874 if (head->attached_to != dir_table)
875 continue;
876 error = sysctl_check_table_dups(path, head->attached_by,
877 table);
878 }
879 root = list_entry(root->root_list.next,
880 struct ctl_table_root, root_list);
881 } while (root != &sysctl_table_root);
882 return error;
883}
884
885static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
886{
887 struct va_format vaf;
888 va_list args;
889
890 va_start(args, fmt);
891 vaf.fmt = fmt;
892 vaf.va = &args;
893
894 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
895 path, table->procname, &vaf);
896
897 va_end(args);
898 return -EINVAL;
899}
900
901static int sysctl_check_table(const char *path, struct ctl_table *table)
902{
903 int err = 0;
904 for (; table->procname; table++) {
905 if (table->child)
906 err = sysctl_err(path, table, "Not a file");
907
908 if ((table->proc_handler == proc_dostring) ||
909 (table->proc_handler == proc_dointvec) ||
910 (table->proc_handler == proc_dointvec_minmax) ||
911 (table->proc_handler == proc_dointvec_jiffies) ||
912 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
913 (table->proc_handler == proc_dointvec_ms_jiffies) ||
914 (table->proc_handler == proc_doulongvec_minmax) ||
915 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
916 if (!table->data)
917 err = sysctl_err(path, table, "No data");
918 if (!table->maxlen)
919 err = sysctl_err(path, table, "No maxlen");
920 }
921 if (!table->proc_handler)
922 err = sysctl_err(path, table, "No proc_handler");
923
924 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
925 err = sysctl_err(path, table, "bogus .mode 0%o",
926 table->mode);
927 }
928 return err;
929}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800930
931/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800932 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800933 * @root: List of sysctl headers to register on
934 * @namespaces: Data to compute which lists of sysctl entries are visible
935 * @path: The path to the directory the sysctl table is in.
936 * @table: the top-level table structure
937 *
938 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
939 * array. A completely 0 filled entry terminates the table.
940 *
941 * The members of the &struct ctl_table structure are used as follows:
942 *
943 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
944 * enter a sysctl file
945 *
946 * data - a pointer to data for use by proc_handler
947 *
948 * maxlen - the maximum size in bytes of the data
949 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800950 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800951 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800952 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800953 *
954 * proc_handler - the text handler routine (described below)
955 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800956 * extra1, extra2 - extra pointers usable by the proc handler routines
957 *
958 * Leaf nodes in the sysctl tree will be represented by a single file
959 * under /proc; non-leaf nodes will be represented by directories.
960 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800961 * There must be a proc_handler routine for any terminal nodes.
962 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800963 *
964 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
965 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
966 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
967 *
968 * It is the handler's job to read the input buffer from user memory
969 * and process it. The handler should return 0 on success.
970 *
971 * This routine returns %NULL on a failure to register, and a pointer
972 * to the table header on success.
973 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800974struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800975 struct ctl_table_root *root,
976 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800977 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800978{
979 struct ctl_table_header *header;
980 struct ctl_table *new, **prevp;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800981 const char *name, *nextname;
982 unsigned int npath = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800983 struct ctl_table_set *set;
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -0800984 size_t path_bytes = 0;
985 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800986
987 /* Count the path components */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800988 for (name = path; name; name = nextname) {
989 int namelen;
990 nextname = strchr(name, '/');
991 if (nextname) {
992 namelen = nextname - name;
993 nextname++;
994 } else {
995 namelen = strlen(name);
996 }
997 if (namelen == 0)
998 continue;
999 path_bytes += namelen + 1;
1000 npath++;
1001 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001002
1003 /*
1004 * For each path component, allocate a 2-element ctl_table array.
1005 * The first array element will be filled with the sysctl entry
1006 * for this, the second will be the sentinel (procname == 0).
1007 *
1008 * We allocate everything in one go so that we don't have to
1009 * worry about freeing additional memory in unregister_sysctl_table.
1010 */
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -08001011 header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001012 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
1013 if (!header)
1014 return NULL;
1015
1016 new = (struct ctl_table *) (header + 1);
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -08001017 new_name = (char *)(new + (2 * npath));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001018
1019 /* Now connect the dots */
1020 prevp = &header->ctl_table;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001021 for (name = path; name; name = nextname) {
1022 int namelen;
1023 nextname = strchr(name, '/');
1024 if (nextname) {
1025 namelen = nextname - name;
1026 nextname++;
1027 } else {
1028 namelen = strlen(name);
1029 }
1030 if (namelen == 0)
1031 continue;
1032 memcpy(new_name, name, namelen);
1033 new_name[namelen] = '\0';
1034
Eric W. Biedermanf05e53a2012-01-21 10:03:13 -08001035 new->procname = new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001036 new->mode = 0555;
1037
1038 *prevp = new;
1039 prevp = &new->child;
1040
1041 new += 2;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001042 new_name += namelen + 1;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001043 }
1044 *prevp = table;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001045
Eric W. Biedermane0d04522012-01-09 22:36:41 -08001046 init_header(header, root, NULL, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001047 if (sysctl_check_table(path, table))
1048 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001049
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001050 spin_lock(&sysctl_lock);
1051 header->set = lookup_header_set(root, namespaces);
1052 header->attached_by = header->ctl_table;
Eric W. Biedermana1945582012-01-21 17:51:48 -08001053 header->attached_to = &root_table[1];
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001054 header->parent = &root_table_header;
Eric W. Biedermanbd295b52012-01-22 21:10:21 -08001055 set = header->set;
1056 root = header->root;
1057 for (;;) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001058 struct ctl_table_header *p;
1059 list_for_each_entry(p, &set->list, ctl_entry) {
1060 if (p->unregistering)
1061 continue;
1062 try_attach(p, header);
1063 }
Eric W. Biedermanbd295b52012-01-22 21:10:21 -08001064 if (root == &sysctl_table_root)
1065 break;
1066 root = list_entry(root->root_list.prev,
1067 struct ctl_table_root, root_list);
1068 set = lookup_header_set(root, namespaces);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001069 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001070 if (sysctl_check_dups(namespaces, header, path, table))
1071 goto fail_locked;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -08001072 insert_header(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001073 spin_unlock(&sysctl_lock);
1074
1075 return header;
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001076fail_locked:
1077 spin_unlock(&sysctl_lock);
1078fail:
1079 kfree(header);
1080 dump_stack();
1081 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001082}
1083
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001084static char *append_path(const char *path, char *pos, const char *name)
1085{
1086 int namelen;
1087 namelen = strlen(name);
1088 if (((pos - path) + namelen + 2) >= PATH_MAX)
1089 return NULL;
1090 memcpy(pos, name, namelen);
1091 pos[namelen] = '/';
1092 pos[namelen + 1] = '\0';
1093 pos += namelen + 1;
1094 return pos;
1095}
1096
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001097static int count_subheaders(struct ctl_table *table)
1098{
1099 int has_files = 0;
1100 int nr_subheaders = 0;
1101 struct ctl_table *entry;
1102
1103 /* special case: no directory and empty directory */
1104 if (!table || !table->procname)
1105 return 1;
1106
1107 for (entry = table; entry->procname; entry++) {
1108 if (entry->child)
1109 nr_subheaders += count_subheaders(entry->child);
1110 else
1111 has_files = 1;
1112 }
1113 return nr_subheaders + has_files;
1114}
1115
1116static int register_leaf_sysctl_tables(const char *path, char *pos,
1117 struct ctl_table_header ***subheader,
1118 struct ctl_table_root *root, struct nsproxy *namespaces,
1119 struct ctl_table *table)
1120{
1121 struct ctl_table *ctl_table_arg = NULL;
1122 struct ctl_table *entry, *files;
1123 int nr_files = 0;
1124 int nr_dirs = 0;
1125 int err = -ENOMEM;
1126
1127 for (entry = table; entry->procname; entry++) {
1128 if (entry->child)
1129 nr_dirs++;
1130 else
1131 nr_files++;
1132 }
1133
1134 files = table;
1135 /* If there are mixed files and directories we need a new table */
1136 if (nr_dirs && nr_files) {
1137 struct ctl_table *new;
1138 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1139 GFP_KERNEL);
1140 if (!files)
1141 goto out;
1142
1143 ctl_table_arg = files;
1144 for (new = files, entry = table; entry->procname; entry++) {
1145 if (entry->child)
1146 continue;
1147 *new = *entry;
1148 new++;
1149 }
1150 }
1151
1152 /* Register everything except a directory full of subdirectories */
1153 if (nr_files || !nr_dirs) {
1154 struct ctl_table_header *header;
1155 header = __register_sysctl_table(root, namespaces, path, files);
1156 if (!header) {
1157 kfree(ctl_table_arg);
1158 goto out;
1159 }
1160
1161 /* Remember if we need to free the file table */
1162 header->ctl_table_arg = ctl_table_arg;
1163 **subheader = header;
1164 (*subheader)++;
1165 }
1166
1167 /* Recurse into the subdirectories. */
1168 for (entry = table; entry->procname; entry++) {
1169 char *child_pos;
1170
1171 if (!entry->child)
1172 continue;
1173
1174 err = -ENAMETOOLONG;
1175 child_pos = append_path(path, pos, entry->procname);
1176 if (!child_pos)
1177 goto out;
1178
1179 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1180 root, namespaces, entry->child);
1181 pos[0] = '\0';
1182 if (err)
1183 goto out;
1184 }
1185 err = 0;
1186out:
1187 /* On failure our caller will unregister all registered subheaders */
1188 return err;
1189}
1190
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001191/**
1192 * __register_sysctl_paths - register a sysctl table hierarchy
1193 * @root: List of sysctl headers to register on
1194 * @namespaces: Data to compute which lists of sysctl entries are visible
1195 * @path: The path to the directory the sysctl table is in.
1196 * @table: the top-level table structure
1197 *
1198 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1199 * array. A completely 0 filled entry terminates the table.
1200 *
1201 * See __register_sysctl_table for more details.
1202 */
1203struct ctl_table_header *__register_sysctl_paths(
1204 struct ctl_table_root *root,
1205 struct nsproxy *namespaces,
1206 const struct ctl_path *path, struct ctl_table *table)
1207{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001208 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001209 int nr_subheaders = count_subheaders(table);
1210 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001211 const struct ctl_path *component;
1212 char *new_path, *pos;
1213
1214 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1215 if (!new_path)
1216 return NULL;
1217
1218 pos[0] = '\0';
1219 for (component = path; component->procname; component++) {
1220 pos = append_path(new_path, pos, component->procname);
1221 if (!pos)
1222 goto out;
1223 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001224 while (table->procname && table->child && !table[1].procname) {
1225 pos = append_path(new_path, pos, table->procname);
1226 if (!pos)
1227 goto out;
1228 table = table->child;
1229 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001230 if (nr_subheaders == 1) {
1231 header = __register_sysctl_table(root, namespaces, new_path, table);
1232 if (header)
1233 header->ctl_table_arg = ctl_table_arg;
1234 } else {
1235 header = kzalloc(sizeof(*header) +
1236 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1237 if (!header)
1238 goto out;
1239
1240 subheaders = (struct ctl_table_header **) (header + 1);
1241 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001242 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001243
1244 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1245 root, namespaces, table))
1246 goto err_register_leaves;
1247 }
1248
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001249out:
1250 kfree(new_path);
1251 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001252
1253err_register_leaves:
1254 while (subheader > subheaders) {
1255 struct ctl_table_header *subh = *(--subheader);
1256 struct ctl_table *table = subh->ctl_table_arg;
1257 unregister_sysctl_table(subh);
1258 kfree(table);
1259 }
1260 kfree(header);
1261 header = NULL;
1262 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001263}
1264
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001265/**
1266 * register_sysctl_table_path - register a sysctl table hierarchy
1267 * @path: The path to the directory the sysctl table is in.
1268 * @table: the top-level table structure
1269 *
1270 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1271 * array. A completely 0 filled entry terminates the table.
1272 *
1273 * See __register_sysctl_paths for more details.
1274 */
1275struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1276 struct ctl_table *table)
1277{
1278 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1279 path, table);
1280}
1281EXPORT_SYMBOL(register_sysctl_paths);
1282
1283/**
1284 * register_sysctl_table - register a sysctl table hierarchy
1285 * @table: the top-level table structure
1286 *
1287 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1288 * array. A completely 0 filled entry terminates the table.
1289 *
1290 * See register_sysctl_paths for more details.
1291 */
1292struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1293{
1294 static const struct ctl_path null_path[] = { {} };
1295
1296 return register_sysctl_paths(null_path, table);
1297}
1298EXPORT_SYMBOL(register_sysctl_table);
1299
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001300static void drop_sysctl_table(struct ctl_table_header *header)
1301{
1302 if (--header->nreg)
1303 return;
1304
1305 start_unregistering(header);
1306 if (!--header->parent->count) {
1307 WARN_ON(1);
1308 kfree_rcu(header->parent, rcu);
1309 }
1310 if (!--header->count)
1311 kfree_rcu(header, rcu);
1312}
1313
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001314/**
1315 * unregister_sysctl_table - unregister a sysctl table hierarchy
1316 * @header: the header returned from register_sysctl_table
1317 *
1318 * Unregisters the sysctl table and all children. proc entries may not
1319 * actually be removed until they are no longer used by anyone.
1320 */
1321void unregister_sysctl_table(struct ctl_table_header * header)
1322{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001323 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001324 might_sleep();
1325
1326 if (header == NULL)
1327 return;
1328
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001329 nr_subheaders = count_subheaders(header->ctl_table_arg);
1330 if (unlikely(nr_subheaders > 1)) {
1331 struct ctl_table_header **subheaders;
1332 int i;
1333
1334 subheaders = (struct ctl_table_header **)(header + 1);
1335 for (i = nr_subheaders -1; i >= 0; i--) {
1336 struct ctl_table_header *subh = subheaders[i];
1337 struct ctl_table *table = subh->ctl_table_arg;
1338 unregister_sysctl_table(subh);
1339 kfree(table);
1340 }
1341 kfree(header);
1342 return;
1343 }
1344
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001345 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001346 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001347 spin_unlock(&sysctl_lock);
1348}
1349EXPORT_SYMBOL(unregister_sysctl_table);
1350
1351void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001352 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001353 int (*is_seen)(struct ctl_table_set *))
1354{
1355 INIT_LIST_HEAD(&p->list);
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001356 p->root = root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001357 p->is_seen = is_seen;
1358}
1359
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001360void retire_sysctl_set(struct ctl_table_set *set)
1361{
1362 WARN_ON(!list_empty(&set->list));
1363}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001364
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001365int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001366{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001367 struct proc_dir_entry *proc_sys_root;
1368
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001369 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001370 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1371 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001372 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001373
1374 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001375}