blob: a78556514a873118fbf17b85f19c73139bdcf380 [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 = "",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080031 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
Eric W. Biedermana1945582012-01-21 17:51:48 -080032 },
33 { }
34};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080035static struct ctl_table_root sysctl_table_root;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080036static struct ctl_dir sysctl_root_dir = {
37 .header = {
38 {{.count = 1,
39 .nreg = 1,
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
42 .root = &sysctl_table_root,
43 .set = &sysctl_table_root.default_set,
44 },
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080045};
46static struct ctl_table_root sysctl_table_root = {
47 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080048 .default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry),
Eric W. Biederman9eb47c22012-01-22 21:26:00 -080049 .default_set.root = &sysctl_table_root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080050};
51
52static DEFINE_SPINLOCK(sysctl_lock);
53
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080054static void drop_sysctl_table(struct ctl_table_header *header);
55
Eric W. Biederman69801282012-01-21 20:09:45 -080056static void sysctl_print_dir(struct ctl_dir *dir)
57{
58 if (dir->header.parent)
59 sysctl_print_dir(dir->header.parent);
60 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
61}
62
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080063static int namecmp(const char *name1, int len1, const char *name2, int len2)
64{
65 int minlen;
66 int cmp;
67
68 minlen = len1;
69 if (minlen > len2)
70 minlen = len2;
71
72 cmp = memcmp(name1, name2, minlen);
73 if (cmp == 0)
74 cmp = len1 - len2;
75 return cmp;
76}
77
78static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080079 struct ctl_table_set *set, struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080080 const char *name, int namelen)
81{
82 struct ctl_table_header *head;
83 struct ctl_table *entry;
84
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080085 list_for_each_entry(head, &set->list, ctl_entry) {
86 if (head->unregistering)
87 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080088 if (head->parent != dir)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080089 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080090 for (entry = head->ctl_table; entry->procname; entry++) {
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080091 const char *procname = entry->procname;
92 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
93 *phead = head;
94 return entry;
95 }
96 }
97 }
98 return NULL;
99}
100
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800101static void init_header(struct ctl_table_header *head,
102 struct ctl_table_root *root, struct ctl_table_set *set,
103 struct ctl_table *table)
104{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800105 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800106 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
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800122static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800123{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800124 header->parent = dir;
125 header->parent->header.nreg++;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800126 list_add_tail(&header->ctl_entry, &header->set->list);
127}
128
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800129/* called under sysctl_lock */
130static int use_table(struct ctl_table_header *p)
131{
132 if (unlikely(p->unregistering))
133 return 0;
134 p->used++;
135 return 1;
136}
137
138/* called under sysctl_lock */
139static void unuse_table(struct ctl_table_header *p)
140{
141 if (!--p->used)
142 if (unlikely(p->unregistering))
143 complete(p->unregistering);
144}
145
146/* called under sysctl_lock, will reacquire if has to wait */
147static void start_unregistering(struct ctl_table_header *p)
148{
149 /*
150 * if p->used is 0, nobody will ever touch that entry again;
151 * we'll eliminate all paths to it before dropping sysctl_lock
152 */
153 if (unlikely(p->used)) {
154 struct completion wait;
155 init_completion(&wait);
156 p->unregistering = &wait;
157 spin_unlock(&sysctl_lock);
158 wait_for_completion(&wait);
159 spin_lock(&sysctl_lock);
160 } else {
161 /* anything non-NULL; we'll never dereference it */
162 p->unregistering = ERR_PTR(-EINVAL);
163 }
164 /*
165 * do not remove from the list until nobody holds it; walking the
166 * list in do_sysctl() relies on that.
167 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800168 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800169}
170
171static void sysctl_head_get(struct ctl_table_header *head)
172{
173 spin_lock(&sysctl_lock);
174 head->count++;
175 spin_unlock(&sysctl_lock);
176}
177
178void sysctl_head_put(struct ctl_table_header *head)
179{
180 spin_lock(&sysctl_lock);
181 if (!--head->count)
182 kfree_rcu(head, rcu);
183 spin_unlock(&sysctl_lock);
184}
185
186static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
187{
188 if (!head)
189 BUG();
190 spin_lock(&sysctl_lock);
191 if (!use_table(head))
192 head = ERR_PTR(-ENOENT);
193 spin_unlock(&sysctl_lock);
194 return head;
195}
196
197static void sysctl_head_finish(struct ctl_table_header *head)
198{
199 if (!head)
200 return;
201 spin_lock(&sysctl_lock);
202 unuse_table(head);
203 spin_unlock(&sysctl_lock);
204}
205
206static struct ctl_table_set *
207lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
208{
209 struct ctl_table_set *set = &root->default_set;
210 if (root->lookup)
211 set = root->lookup(root, namespaces);
212 return set;
213}
214
215static struct list_head *
216lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
217{
218 struct ctl_table_set *set = lookup_header_set(root, namespaces);
219 return &set->list;
220}
221
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800222static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800223 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800224 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);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800235 entry = find_entry(&head, set, dir, name, namelen);
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800236 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. Biederman7ec66d02011-12-29 08:24:29 -0800247static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800248 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. Biederman7ec66d02011-12-29 08:24:29 -0800259 if (head->parent != dir ||
260 !head->ctl_table->procname ||
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800261 !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. Biederman7ec66d02011-12-29 08:24:29 -0800284static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800285 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800286{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800287 struct ctl_table_header *head;
288 struct ctl_table *entry = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800289
290 spin_lock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800291 head = next_usable_entry(dir, &sysctl_table_root,
292 &sysctl_table_root.default_set.list);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800293 spin_unlock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800294 if (head)
295 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800296 *phead = head;
297 *pentry = entry;
298}
299
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800300static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800301{
302 struct ctl_table_header *head = *phead;
303 struct ctl_table *entry = *pentry;
304
305 entry++;
306 if (!entry->procname) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800307 spin_lock(&sysctl_lock);
308 unuse_table(head);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800309 head = next_usable_entry(head->parent, head->root, &head->ctl_entry);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800310 spin_unlock(&sysctl_lock);
311 if (head)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800312 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800313 }
314 *phead = head;
315 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800316}
317
318void register_sysctl_root(struct ctl_table_root *root)
319{
320 spin_lock(&sysctl_lock);
321 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
322 spin_unlock(&sysctl_lock);
323}
324
325/*
326 * sysctl_perm does NOT grant the superuser all rights automatically, because
327 * some sysctl variables are readonly even to root.
328 */
329
330static int test_perm(int mode, int op)
331{
332 if (!current_euid())
333 mode >>= 6;
334 else if (in_egroup_p(0))
335 mode >>= 3;
336 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
337 return 0;
338 return -EACCES;
339}
340
341static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
342{
343 int mode;
344
345 if (root->permissions)
346 mode = root->permissions(root, current->nsproxy, table);
347 else
348 mode = table->mode;
349
350 return test_perm(mode, op);
351}
352
Al Viro90434762008-07-15 08:54:06 -0400353static struct inode *proc_sys_make_inode(struct super_block *sb,
354 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800355{
356 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400357 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800358
Al Viro90434762008-07-15 08:54:06 -0400359 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800360 if (!inode)
361 goto out;
362
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400363 inode->i_ino = get_next_ino();
364
Al Viro90434762008-07-15 08:54:06 -0400365 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800366 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400367 ei->sysctl = head;
368 ei->sysctl_entry = table;
369
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800370 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400371 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800372 if (!S_ISDIR(table->mode)) {
Al Viro90434762008-07-15 08:54:06 -0400373 inode->i_mode |= S_IFREG;
374 inode->i_op = &proc_sys_inode_operations;
375 inode->i_fop = &proc_sys_file_operations;
376 } else {
377 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400378 inode->i_op = &proc_sys_dir_operations;
379 inode->i_fop = &proc_sys_dir_file_operations;
380 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800381out:
382 return inode;
383}
384
Adrian Bunk81324362008-10-03 00:33:54 +0400385static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800386{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800387 struct ctl_table_header *head = PROC_I(inode)->sysctl;
388 if (!head)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800389 head = &sysctl_root_dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800390 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800391}
392
393static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
394 struct nameidata *nd)
395{
Al Viro90434762008-07-15 08:54:06 -0400396 struct ctl_table_header *head = grab_header(dir);
Al Viro90434762008-07-15 08:54:06 -0400397 struct ctl_table_header *h = NULL;
398 struct qstr *name = &dentry->d_name;
399 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800400 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400401 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800402 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800403
Al Viro90434762008-07-15 08:54:06 -0400404 if (IS_ERR(head))
405 return ERR_CAST(head);
406
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800407 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro90434762008-07-15 08:54:06 -0400408
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800409 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400410 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800411 goto out;
412
413 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400414 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
415 if (h)
416 sysctl_head_finish(h);
417
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800418 if (!inode)
419 goto out;
420
421 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100422 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800423 d_add(dentry, inode);
424
425out:
426 sysctl_head_finish(head);
427 return err;
428}
429
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700430static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
431 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800432{
Al Viro90434762008-07-15 08:54:06 -0400433 struct inode *inode = filp->f_path.dentry->d_inode;
434 struct ctl_table_header *head = grab_header(inode);
435 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100436 ssize_t error;
437 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800438
Al Viro90434762008-07-15 08:54:06 -0400439 if (IS_ERR(head))
440 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800441
442 /*
443 * At this point we know that the sysctl was not unregistered
444 * and won't be until we finish.
445 */
446 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700447 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800448 goto out;
449
Al Viro90434762008-07-15 08:54:06 -0400450 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
451 error = -EINVAL;
452 if (!table->proc_handler)
453 goto out;
454
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800455 /* careful: calling conventions are nasty here */
456 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700457 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800458 if (!error)
459 error = res;
460out:
461 sysctl_head_finish(head);
462
463 return error;
464}
465
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700466static ssize_t proc_sys_read(struct file *filp, char __user *buf,
467 size_t count, loff_t *ppos)
468{
469 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
470}
471
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800472static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
473 size_t count, loff_t *ppos)
474{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700475 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800476}
477
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700478static int proc_sys_open(struct inode *inode, struct file *filp)
479{
480 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
481
482 if (table->poll)
483 filp->private_data = proc_sys_poll_event(table->poll);
484
485 return 0;
486}
487
488static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
489{
490 struct inode *inode = filp->f_path.dentry->d_inode;
491 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
492 unsigned long event = (unsigned long)filp->private_data;
493 unsigned int ret = DEFAULT_POLLMASK;
494
495 if (!table->proc_handler)
496 goto out;
497
498 if (!table->poll)
499 goto out;
500
501 poll_wait(filp, &table->poll->wait, wait);
502
503 if (event != atomic_read(&table->poll->event)) {
504 filp->private_data = proc_sys_poll_event(table->poll);
505 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
506 }
507
508out:
509 return ret;
510}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800511
512static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400513 filldir_t filldir,
514 struct ctl_table_header *head,
515 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800516{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800517 struct dentry *child, *dir = filp->f_path.dentry;
518 struct inode *inode;
519 struct qstr qname;
520 ino_t ino = 0;
521 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800522
523 qname.name = table->procname;
524 qname.len = strlen(table->procname);
525 qname.hash = full_name_hash(qname.name, qname.len);
526
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800527 child = d_lookup(dir, &qname);
528 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400529 child = d_alloc(dir, &qname);
530 if (child) {
531 inode = proc_sys_make_inode(dir->d_sb, head, table);
532 if (!inode) {
533 dput(child);
534 return -ENOMEM;
535 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100536 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400537 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800538 }
Al Viro90434762008-07-15 08:54:06 -0400539 } else {
540 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800541 }
542 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800543 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400544 ino = inode->i_ino;
545 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800546 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400547 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
548}
549
550static int scan(struct ctl_table_header *head, ctl_table *table,
551 unsigned long *pos, struct file *file,
552 void *dirent, filldir_t filldir)
553{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800554 int res;
Al Viro90434762008-07-15 08:54:06 -0400555
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800556 if ((*pos)++ < file->f_pos)
557 return 0;
Al Viro90434762008-07-15 08:54:06 -0400558
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800559 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400560
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800561 if (res == 0)
562 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400563
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800564 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800565}
566
567static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
568{
Al Viro90434762008-07-15 08:54:06 -0400569 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800570 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400571 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400572 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800573 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800574 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800575 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400576 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800577
Al Viro90434762008-07-15 08:54:06 -0400578 if (IS_ERR(head))
579 return PTR_ERR(head);
580
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800581 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800582
583 ret = 0;
584 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
585 if (filp->f_pos == 0) {
586 if (filldir(dirent, ".", 1, filp->f_pos,
587 inode->i_ino, DT_DIR) < 0)
588 goto out;
589 filp->f_pos++;
590 }
591 if (filp->f_pos == 1) {
592 if (filldir(dirent, "..", 2, filp->f_pos,
593 parent_ino(dentry), DT_DIR) < 0)
594 goto out;
595 filp->f_pos++;
596 }
597 pos = 2;
598
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800599 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800600 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400601 if (ret) {
602 sysctl_head_finish(h);
603 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800604 }
605 }
606 ret = 1;
607out:
608 sysctl_head_finish(head);
609 return ret;
610}
611
Al Viro10556cb2011-06-20 19:28:19 -0400612static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800613{
614 /*
615 * sysctl entries that are not writeable,
616 * are _NOT_ writeable, capabilities or not.
617 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200618 struct ctl_table_header *head;
619 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800620 int error;
621
Miklos Szeredif696a362008-07-31 13:41:58 +0200622 /* Executable files are not allowed under /proc/sys/ */
623 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
624 return -EACCES;
625
626 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400627 if (IS_ERR(head))
628 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800629
Miklos Szeredif696a362008-07-31 13:41:58 +0200630 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400631 if (!table) /* global root - r-xr-xr-x */
632 error = mask & MAY_WRITE ? -EACCES : 0;
633 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400634 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800635
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800636 sysctl_head_finish(head);
637 return error;
638}
639
640static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
641{
642 struct inode *inode = dentry->d_inode;
643 int error;
644
645 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
646 return -EPERM;
647
648 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200649 if (error)
650 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800651
Christoph Hellwig10257742010-06-04 11:30:02 +0200652 if ((attr->ia_valid & ATTR_SIZE) &&
653 attr->ia_size != i_size_read(inode)) {
654 error = vmtruncate(inode, attr->ia_size);
655 if (error)
656 return error;
657 }
658
659 setattr_copy(inode, attr);
660 mark_inode_dirty(inode);
661 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800662}
663
Al Viro90434762008-07-15 08:54:06 -0400664static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
665{
666 struct inode *inode = dentry->d_inode;
667 struct ctl_table_header *head = grab_header(inode);
668 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
669
670 if (IS_ERR(head))
671 return PTR_ERR(head);
672
673 generic_fillattr(inode, stat);
674 if (table)
675 stat->mode = (stat->mode & S_IFMT) | table->mode;
676
677 sysctl_head_finish(head);
678 return 0;
679}
680
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800681static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700682 .open = proc_sys_open,
683 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800684 .read = proc_sys_read,
685 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200686 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400687};
688
689static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700690 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800691 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200692 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800693};
694
Jan Engelhardt03a44822008-02-08 04:21:19 -0800695static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400696 .permission = proc_sys_permission,
697 .setattr = proc_sys_setattr,
698 .getattr = proc_sys_getattr,
699};
700
701static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800702 .lookup = proc_sys_lookup,
703 .permission = proc_sys_permission,
704 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400705 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800706};
707
708static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
709{
Nick Piggin34286d62011-01-07 17:49:57 +1100710 if (nd->flags & LOOKUP_RCU)
711 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400712 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
713}
714
Nick Pigginfe15ce42011-01-07 17:49:23 +1100715static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400716{
717 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
718}
719
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800720static int sysctl_is_seen(struct ctl_table_header *p)
721{
722 struct ctl_table_set *set = p->set;
723 int res;
724 spin_lock(&sysctl_lock);
725 if (p->unregistering)
726 res = 0;
727 else if (!set->is_seen)
728 res = 1;
729 else
730 res = set->is_seen(set);
731 spin_unlock(&sysctl_lock);
732 return res;
733}
734
Nick Piggin621e1552011-01-07 17:49:27 +1100735static int proc_sys_compare(const struct dentry *parent,
736 const struct inode *pinode,
737 const struct dentry *dentry, const struct inode *inode,
738 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400739{
Al Virodfef6dc2011-03-08 01:25:28 -0500740 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100741 /* Although proc doesn't have negative dentries, rcu-walk means
742 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500743 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100744 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500745 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100746 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400747 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100748 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400749 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500750 head = rcu_dereference(PROC_I(inode)->sysctl);
751 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800752}
753
Al Virod72f71e2009-02-20 05:58:47 +0000754static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800755 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400756 .d_delete = proc_sys_delete,
757 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800758};
759
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800760static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir,
761 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800762{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800763 struct ctl_table_header *head;
764 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800765
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800766 entry = find_entry(&head, set, dir, name, namelen);
767 if (!entry)
768 return ERR_PTR(-ENOENT);
769 if (S_ISDIR(entry->mode))
770 return container_of(head, struct ctl_dir, header);
771 return ERR_PTR(-ENOTDIR);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800772}
773
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800774static struct ctl_dir *new_dir(struct ctl_table_set *set,
775 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800776{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800777 struct ctl_table *table;
778 struct ctl_dir *new;
779 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800780
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800781 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
782 namelen + 1, GFP_KERNEL);
783 if (!new)
784 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800785
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800786 table = (struct ctl_table *)(new + 1);
787 new_name = (char *)(table + 2);
788 memcpy(new_name, name, namelen);
789 new_name[namelen] = '\0';
790 table[0].procname = new_name;
791 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
792 init_header(&new->header, set->root, set, table);
793
794 return new;
795}
796
797static struct ctl_dir *get_subdir(struct ctl_table_set *set,
798 struct ctl_dir *dir, const char *name, int namelen)
799{
800 struct ctl_dir *subdir, *new = NULL;
801
802 spin_lock(&sysctl_lock);
803 subdir = find_subdir(dir->header.set, dir, name, namelen);
804 if (!IS_ERR(subdir))
805 goto found;
806 if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set)
807 subdir = find_subdir(set, dir, name, namelen);
808 if (!IS_ERR(subdir))
809 goto found;
810 if (PTR_ERR(subdir) != -ENOENT)
811 goto failed;
812
813 spin_unlock(&sysctl_lock);
814 new = new_dir(set, name, namelen);
815 spin_lock(&sysctl_lock);
816 subdir = ERR_PTR(-ENOMEM);
817 if (!new)
818 goto failed;
819
820 subdir = find_subdir(set, dir, name, namelen);
821 if (!IS_ERR(subdir))
822 goto found;
823 if (PTR_ERR(subdir) != -ENOENT)
824 goto failed;
825
826 insert_header(dir, &new->header);
827 subdir = new;
828found:
829 subdir->header.nreg++;
830failed:
831 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800832 printk(KERN_ERR "sysctl could not get directory: ");
833 sysctl_print_dir(dir);
834 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800835 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800836 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800837 drop_sysctl_table(&dir->header);
838 if (new)
839 drop_sysctl_table(&new->header);
840 spin_unlock(&sysctl_lock);
841 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800842}
843
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800844static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
845 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800846{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800847 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800848 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800849
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800850 for (entry = old; entry->procname; entry++) {
851 for (test = table; test->procname; test++) {
852 if (strcmp(entry->procname, test->procname) == 0) {
853 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
854 path, test->procname);
855 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800856 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800857 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800858 }
859 return error;
860}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800861
862static int sysctl_check_dups(struct nsproxy *namespaces,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800863 struct ctl_dir *dir,
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800864 const char *path, struct ctl_table *table)
865{
866 struct ctl_table_root *root;
867 struct ctl_table_set *set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800868 struct ctl_table_header *head;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800869 int error = 0;
870
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800871 root = &sysctl_table_root;
872 do {
873 set = lookup_header_set(root, namespaces);
874
875 list_for_each_entry(head, &set->list, ctl_entry) {
876 if (head->unregistering)
877 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800878 if (head->parent != dir)
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800879 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800880 error = sysctl_check_table_dups(path, head->ctl_table,
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800881 table);
882 }
883 root = list_entry(root->root_list.next,
884 struct ctl_table_root, root_list);
885 } while (root != &sysctl_table_root);
886 return error;
887}
888
889static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
890{
891 struct va_format vaf;
892 va_list args;
893
894 va_start(args, fmt);
895 vaf.fmt = fmt;
896 vaf.va = &args;
897
898 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
899 path, table->procname, &vaf);
900
901 va_end(args);
902 return -EINVAL;
903}
904
905static int sysctl_check_table(const char *path, struct ctl_table *table)
906{
907 int err = 0;
908 for (; table->procname; table++) {
909 if (table->child)
910 err = sysctl_err(path, table, "Not a file");
911
912 if ((table->proc_handler == proc_dostring) ||
913 (table->proc_handler == proc_dointvec) ||
914 (table->proc_handler == proc_dointvec_minmax) ||
915 (table->proc_handler == proc_dointvec_jiffies) ||
916 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
917 (table->proc_handler == proc_dointvec_ms_jiffies) ||
918 (table->proc_handler == proc_doulongvec_minmax) ||
919 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
920 if (!table->data)
921 err = sysctl_err(path, table, "No data");
922 if (!table->maxlen)
923 err = sysctl_err(path, table, "No maxlen");
924 }
925 if (!table->proc_handler)
926 err = sysctl_err(path, table, "No proc_handler");
927
928 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
929 err = sysctl_err(path, table, "bogus .mode 0%o",
930 table->mode);
931 }
932 return err;
933}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800934
935/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800936 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800937 * @root: List of sysctl headers to register on
938 * @namespaces: Data to compute which lists of sysctl entries are visible
939 * @path: The path to the directory the sysctl table is in.
940 * @table: the top-level table structure
941 *
942 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
943 * array. A completely 0 filled entry terminates the table.
944 *
945 * The members of the &struct ctl_table structure are used as follows:
946 *
947 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
948 * enter a sysctl file
949 *
950 * data - a pointer to data for use by proc_handler
951 *
952 * maxlen - the maximum size in bytes of the data
953 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800954 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800955 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800956 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800957 *
958 * proc_handler - the text handler routine (described below)
959 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800960 * extra1, extra2 - extra pointers usable by the proc handler routines
961 *
962 * Leaf nodes in the sysctl tree will be represented by a single file
963 * under /proc; non-leaf nodes will be represented by directories.
964 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800965 * There must be a proc_handler routine for any terminal nodes.
966 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800967 *
968 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
969 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
970 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
971 *
972 * It is the handler's job to read the input buffer from user memory
973 * and process it. The handler should return 0 on success.
974 *
975 * This routine returns %NULL on a failure to register, and a pointer
976 * to the table header on success.
977 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800978struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800979 struct ctl_table_root *root,
980 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800981 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800982{
983 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800984 const char *name, *nextname;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800985 struct ctl_table_set *set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800986 struct ctl_dir *dir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800987
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800988 header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800989 if (!header)
990 return NULL;
991
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800992 init_header(header, root, NULL, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800993 if (sysctl_check_table(path, table))
994 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -0800995
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800996 spin_lock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800997 header->set = set = lookup_header_set(root, namespaces);
998 dir = &sysctl_root_dir;
999 dir->header.nreg++;
1000 spin_unlock(&sysctl_lock);
1001
1002 /* Find the directory for the ctl_table */
1003 for (name = path; name; name = nextname) {
1004 int namelen;
1005 nextname = strchr(name, '/');
1006 if (nextname) {
1007 namelen = nextname - name;
1008 nextname++;
1009 } else {
1010 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001011 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001012 if (namelen == 0)
1013 continue;
1014
1015 dir = get_subdir(set, dir, name, namelen);
1016 if (IS_ERR(dir))
1017 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001018 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001019 spin_lock(&sysctl_lock);
1020 if (sysctl_check_dups(namespaces, dir, path, table))
1021 goto fail_put_dir_locked;
1022 insert_header(dir, header);
1023 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001024 spin_unlock(&sysctl_lock);
1025
1026 return header;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001027fail_put_dir_locked:
1028 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001029 spin_unlock(&sysctl_lock);
1030fail:
1031 kfree(header);
1032 dump_stack();
1033 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001034}
1035
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001036static char *append_path(const char *path, char *pos, const char *name)
1037{
1038 int namelen;
1039 namelen = strlen(name);
1040 if (((pos - path) + namelen + 2) >= PATH_MAX)
1041 return NULL;
1042 memcpy(pos, name, namelen);
1043 pos[namelen] = '/';
1044 pos[namelen + 1] = '\0';
1045 pos += namelen + 1;
1046 return pos;
1047}
1048
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001049static int count_subheaders(struct ctl_table *table)
1050{
1051 int has_files = 0;
1052 int nr_subheaders = 0;
1053 struct ctl_table *entry;
1054
1055 /* special case: no directory and empty directory */
1056 if (!table || !table->procname)
1057 return 1;
1058
1059 for (entry = table; entry->procname; entry++) {
1060 if (entry->child)
1061 nr_subheaders += count_subheaders(entry->child);
1062 else
1063 has_files = 1;
1064 }
1065 return nr_subheaders + has_files;
1066}
1067
1068static int register_leaf_sysctl_tables(const char *path, char *pos,
1069 struct ctl_table_header ***subheader,
1070 struct ctl_table_root *root, struct nsproxy *namespaces,
1071 struct ctl_table *table)
1072{
1073 struct ctl_table *ctl_table_arg = NULL;
1074 struct ctl_table *entry, *files;
1075 int nr_files = 0;
1076 int nr_dirs = 0;
1077 int err = -ENOMEM;
1078
1079 for (entry = table; entry->procname; entry++) {
1080 if (entry->child)
1081 nr_dirs++;
1082 else
1083 nr_files++;
1084 }
1085
1086 files = table;
1087 /* If there are mixed files and directories we need a new table */
1088 if (nr_dirs && nr_files) {
1089 struct ctl_table *new;
1090 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1091 GFP_KERNEL);
1092 if (!files)
1093 goto out;
1094
1095 ctl_table_arg = files;
1096 for (new = files, entry = table; entry->procname; entry++) {
1097 if (entry->child)
1098 continue;
1099 *new = *entry;
1100 new++;
1101 }
1102 }
1103
1104 /* Register everything except a directory full of subdirectories */
1105 if (nr_files || !nr_dirs) {
1106 struct ctl_table_header *header;
1107 header = __register_sysctl_table(root, namespaces, path, files);
1108 if (!header) {
1109 kfree(ctl_table_arg);
1110 goto out;
1111 }
1112
1113 /* Remember if we need to free the file table */
1114 header->ctl_table_arg = ctl_table_arg;
1115 **subheader = header;
1116 (*subheader)++;
1117 }
1118
1119 /* Recurse into the subdirectories. */
1120 for (entry = table; entry->procname; entry++) {
1121 char *child_pos;
1122
1123 if (!entry->child)
1124 continue;
1125
1126 err = -ENAMETOOLONG;
1127 child_pos = append_path(path, pos, entry->procname);
1128 if (!child_pos)
1129 goto out;
1130
1131 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1132 root, namespaces, entry->child);
1133 pos[0] = '\0';
1134 if (err)
1135 goto out;
1136 }
1137 err = 0;
1138out:
1139 /* On failure our caller will unregister all registered subheaders */
1140 return err;
1141}
1142
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001143/**
1144 * __register_sysctl_paths - register a sysctl table hierarchy
1145 * @root: List of sysctl headers to register on
1146 * @namespaces: Data to compute which lists of sysctl entries are visible
1147 * @path: The path to the directory the sysctl table is in.
1148 * @table: the top-level table structure
1149 *
1150 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1151 * array. A completely 0 filled entry terminates the table.
1152 *
1153 * See __register_sysctl_table for more details.
1154 */
1155struct ctl_table_header *__register_sysctl_paths(
1156 struct ctl_table_root *root,
1157 struct nsproxy *namespaces,
1158 const struct ctl_path *path, struct ctl_table *table)
1159{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001160 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001161 int nr_subheaders = count_subheaders(table);
1162 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001163 const struct ctl_path *component;
1164 char *new_path, *pos;
1165
1166 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1167 if (!new_path)
1168 return NULL;
1169
1170 pos[0] = '\0';
1171 for (component = path; component->procname; component++) {
1172 pos = append_path(new_path, pos, component->procname);
1173 if (!pos)
1174 goto out;
1175 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001176 while (table->procname && table->child && !table[1].procname) {
1177 pos = append_path(new_path, pos, table->procname);
1178 if (!pos)
1179 goto out;
1180 table = table->child;
1181 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001182 if (nr_subheaders == 1) {
1183 header = __register_sysctl_table(root, namespaces, new_path, table);
1184 if (header)
1185 header->ctl_table_arg = ctl_table_arg;
1186 } else {
1187 header = kzalloc(sizeof(*header) +
1188 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1189 if (!header)
1190 goto out;
1191
1192 subheaders = (struct ctl_table_header **) (header + 1);
1193 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001194 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001195
1196 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1197 root, namespaces, table))
1198 goto err_register_leaves;
1199 }
1200
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001201out:
1202 kfree(new_path);
1203 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001204
1205err_register_leaves:
1206 while (subheader > subheaders) {
1207 struct ctl_table_header *subh = *(--subheader);
1208 struct ctl_table *table = subh->ctl_table_arg;
1209 unregister_sysctl_table(subh);
1210 kfree(table);
1211 }
1212 kfree(header);
1213 header = NULL;
1214 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001215}
1216
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001217/**
1218 * register_sysctl_table_path - register a sysctl table hierarchy
1219 * @path: The path to the directory the sysctl table is in.
1220 * @table: the top-level table structure
1221 *
1222 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1223 * array. A completely 0 filled entry terminates the table.
1224 *
1225 * See __register_sysctl_paths for more details.
1226 */
1227struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1228 struct ctl_table *table)
1229{
1230 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1231 path, table);
1232}
1233EXPORT_SYMBOL(register_sysctl_paths);
1234
1235/**
1236 * register_sysctl_table - register a sysctl table hierarchy
1237 * @table: the top-level table structure
1238 *
1239 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1240 * array. A completely 0 filled entry terminates the table.
1241 *
1242 * See register_sysctl_paths for more details.
1243 */
1244struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1245{
1246 static const struct ctl_path null_path[] = { {} };
1247
1248 return register_sysctl_paths(null_path, table);
1249}
1250EXPORT_SYMBOL(register_sysctl_table);
1251
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001252static void drop_sysctl_table(struct ctl_table_header *header)
1253{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001254 struct ctl_dir *parent = header->parent;
1255
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001256 if (--header->nreg)
1257 return;
1258
1259 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001260 if (!--header->count)
1261 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001262
1263 if (parent)
1264 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001265}
1266
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001267/**
1268 * unregister_sysctl_table - unregister a sysctl table hierarchy
1269 * @header: the header returned from register_sysctl_table
1270 *
1271 * Unregisters the sysctl table and all children. proc entries may not
1272 * actually be removed until they are no longer used by anyone.
1273 */
1274void unregister_sysctl_table(struct ctl_table_header * header)
1275{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001276 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001277 might_sleep();
1278
1279 if (header == NULL)
1280 return;
1281
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001282 nr_subheaders = count_subheaders(header->ctl_table_arg);
1283 if (unlikely(nr_subheaders > 1)) {
1284 struct ctl_table_header **subheaders;
1285 int i;
1286
1287 subheaders = (struct ctl_table_header **)(header + 1);
1288 for (i = nr_subheaders -1; i >= 0; i--) {
1289 struct ctl_table_header *subh = subheaders[i];
1290 struct ctl_table *table = subh->ctl_table_arg;
1291 unregister_sysctl_table(subh);
1292 kfree(table);
1293 }
1294 kfree(header);
1295 return;
1296 }
1297
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001298 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001299 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001300 spin_unlock(&sysctl_lock);
1301}
1302EXPORT_SYMBOL(unregister_sysctl_table);
1303
1304void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001305 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001306 int (*is_seen)(struct ctl_table_set *))
1307{
1308 INIT_LIST_HEAD(&p->list);
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001309 p->root = root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001310 p->is_seen = is_seen;
1311}
1312
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001313void retire_sysctl_set(struct ctl_table_set *set)
1314{
1315 WARN_ON(!list_empty(&set->list));
1316}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001317
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001318int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001319{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001320 struct proc_dir_entry *proc_sys_root;
1321
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001322 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001323 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1324 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001325 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001326
1327 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001328}