blob: 3c0767d5a55faa85ef7be816adcc3cefb56a8ccd [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 Viro9043476f2008-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. Biederman076c3ee2012-01-09 21:42:02 -080056static int namecmp(const char *name1, int len1, const char *name2, int len2)
57{
58 int minlen;
59 int cmp;
60
61 minlen = len1;
62 if (minlen > len2)
63 minlen = len2;
64
65 cmp = memcmp(name1, name2, minlen);
66 if (cmp == 0)
67 cmp = len1 - len2;
68 return cmp;
69}
70
71static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080072 struct ctl_table_set *set, struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080073 const char *name, int namelen)
74{
75 struct ctl_table_header *head;
76 struct ctl_table *entry;
77
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080078 list_for_each_entry(head, &set->list, ctl_entry) {
79 if (head->unregistering)
80 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080081 if (head->parent != dir)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080082 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080083 for (entry = head->ctl_table; entry->procname; entry++) {
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080084 const char *procname = entry->procname;
85 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
86 *phead = head;
87 return entry;
88 }
89 }
90 }
91 return NULL;
92}
93
Eric W. Biedermane0d04522012-01-09 22:36:41 -080094static void init_header(struct ctl_table_header *head,
95 struct ctl_table_root *root, struct ctl_table_set *set,
96 struct ctl_table *table)
97{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080098 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -080099 head->ctl_table_arg = table;
100 INIT_LIST_HEAD(&head->ctl_entry);
101 head->used = 0;
102 head->count = 1;
103 head->nreg = 1;
104 head->unregistering = NULL;
105 head->root = root;
106 head->set = set;
107 head->parent = NULL;
108}
109
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800110static void erase_header(struct ctl_table_header *head)
111{
112 list_del_init(&head->ctl_entry);
113}
114
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800115static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800116{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800117 header->parent = dir;
118 header->parent->header.nreg++;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800119 list_add_tail(&header->ctl_entry, &header->set->list);
120}
121
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800122/* called under sysctl_lock */
123static int use_table(struct ctl_table_header *p)
124{
125 if (unlikely(p->unregistering))
126 return 0;
127 p->used++;
128 return 1;
129}
130
131/* called under sysctl_lock */
132static void unuse_table(struct ctl_table_header *p)
133{
134 if (!--p->used)
135 if (unlikely(p->unregistering))
136 complete(p->unregistering);
137}
138
139/* called under sysctl_lock, will reacquire if has to wait */
140static void start_unregistering(struct ctl_table_header *p)
141{
142 /*
143 * if p->used is 0, nobody will ever touch that entry again;
144 * we'll eliminate all paths to it before dropping sysctl_lock
145 */
146 if (unlikely(p->used)) {
147 struct completion wait;
148 init_completion(&wait);
149 p->unregistering = &wait;
150 spin_unlock(&sysctl_lock);
151 wait_for_completion(&wait);
152 spin_lock(&sysctl_lock);
153 } else {
154 /* anything non-NULL; we'll never dereference it */
155 p->unregistering = ERR_PTR(-EINVAL);
156 }
157 /*
158 * do not remove from the list until nobody holds it; walking the
159 * list in do_sysctl() relies on that.
160 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800161 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800162}
163
164static void sysctl_head_get(struct ctl_table_header *head)
165{
166 spin_lock(&sysctl_lock);
167 head->count++;
168 spin_unlock(&sysctl_lock);
169}
170
171void sysctl_head_put(struct ctl_table_header *head)
172{
173 spin_lock(&sysctl_lock);
174 if (!--head->count)
175 kfree_rcu(head, rcu);
176 spin_unlock(&sysctl_lock);
177}
178
179static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
180{
181 if (!head)
182 BUG();
183 spin_lock(&sysctl_lock);
184 if (!use_table(head))
185 head = ERR_PTR(-ENOENT);
186 spin_unlock(&sysctl_lock);
187 return head;
188}
189
190static void sysctl_head_finish(struct ctl_table_header *head)
191{
192 if (!head)
193 return;
194 spin_lock(&sysctl_lock);
195 unuse_table(head);
196 spin_unlock(&sysctl_lock);
197}
198
199static struct ctl_table_set *
200lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
201{
202 struct ctl_table_set *set = &root->default_set;
203 if (root->lookup)
204 set = root->lookup(root, namespaces);
205 return set;
206}
207
208static struct list_head *
209lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
210{
211 struct ctl_table_set *set = lookup_header_set(root, namespaces);
212 return &set->list;
213}
214
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800215static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800216 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800217 const char *name, int namelen)
218{
219 struct ctl_table_header *head;
220 struct ctl_table *entry;
221 struct ctl_table_root *root;
222 struct ctl_table_set *set;
223
224 spin_lock(&sysctl_lock);
225 root = &sysctl_table_root;
226 do {
227 set = lookup_header_set(root, current->nsproxy);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800228 entry = find_entry(&head, set, dir, name, namelen);
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800229 if (entry && use_table(head))
230 *phead = head;
231 else
232 entry = NULL;
233 root = list_entry(root->root_list.next,
234 struct ctl_table_root, root_list);
235 } while (!entry && root != &sysctl_table_root);
236 spin_unlock(&sysctl_lock);
237 return entry;
238}
239
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800240static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800241 struct ctl_table_root *root, struct list_head *tmp)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800242{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800243 struct nsproxy *namespaces = current->nsproxy;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800244 struct list_head *header_list;
245 struct ctl_table_header *head;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800246
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800247 goto next;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800248 for (;;) {
249 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800250 root = head->root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800251
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800252 if (head->parent != dir ||
253 !head->ctl_table->procname ||
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800254 !use_table(head))
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800255 goto next;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800256
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800257 return head;
258 next:
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800259 tmp = tmp->next;
260 header_list = lookup_header_list(root, namespaces);
261 if (tmp != header_list)
262 continue;
263
264 do {
265 root = list_entry(root->root_list.next,
266 struct ctl_table_root, root_list);
267 if (root == &sysctl_table_root)
268 goto out;
269 header_list = lookup_header_list(root, namespaces);
270 } while (list_empty(header_list));
271 tmp = header_list->next;
272 }
273out:
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800274 return NULL;
275}
276
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800277static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800278 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800279{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800280 struct ctl_table_header *head;
281 struct ctl_table *entry = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800282
283 spin_lock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800284 head = next_usable_entry(dir, &sysctl_table_root,
285 &sysctl_table_root.default_set.list);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800286 spin_unlock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800287 if (head)
288 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800289 *phead = head;
290 *pentry = entry;
291}
292
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800293static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800294{
295 struct ctl_table_header *head = *phead;
296 struct ctl_table *entry = *pentry;
297
298 entry++;
299 if (!entry->procname) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800300 spin_lock(&sysctl_lock);
301 unuse_table(head);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800302 head = next_usable_entry(head->parent, head->root, &head->ctl_entry);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800303 spin_unlock(&sysctl_lock);
304 if (head)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800305 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800306 }
307 *phead = head;
308 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800309}
310
311void register_sysctl_root(struct ctl_table_root *root)
312{
313 spin_lock(&sysctl_lock);
314 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
315 spin_unlock(&sysctl_lock);
316}
317
318/*
319 * sysctl_perm does NOT grant the superuser all rights automatically, because
320 * some sysctl variables are readonly even to root.
321 */
322
323static int test_perm(int mode, int op)
324{
325 if (!current_euid())
326 mode >>= 6;
327 else if (in_egroup_p(0))
328 mode >>= 3;
329 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
330 return 0;
331 return -EACCES;
332}
333
334static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
335{
336 int mode;
337
338 if (root->permissions)
339 mode = root->permissions(root, current->nsproxy, table);
340 else
341 mode = table->mode;
342
343 return test_perm(mode, op);
344}
345
Al Viro9043476f2008-07-15 08:54:06 -0400346static struct inode *proc_sys_make_inode(struct super_block *sb,
347 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800348{
349 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400350 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800351
Al Viro9043476f2008-07-15 08:54:06 -0400352 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800353 if (!inode)
354 goto out;
355
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400356 inode->i_ino = get_next_ino();
357
Al Viro9043476f2008-07-15 08:54:06 -0400358 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800359 ei = PROC_I(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400360 ei->sysctl = head;
361 ei->sysctl_entry = table;
362
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800363 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro9043476f2008-07-15 08:54:06 -0400364 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800365 if (!S_ISDIR(table->mode)) {
Al Viro9043476f2008-07-15 08:54:06 -0400366 inode->i_mode |= S_IFREG;
367 inode->i_op = &proc_sys_inode_operations;
368 inode->i_fop = &proc_sys_file_operations;
369 } else {
370 inode->i_mode |= S_IFDIR;
Al Viro9043476f2008-07-15 08:54:06 -0400371 inode->i_op = &proc_sys_dir_operations;
372 inode->i_fop = &proc_sys_dir_file_operations;
373 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800374out:
375 return inode;
376}
377
Adrian Bunk81324362008-10-03 00:33:54 +0400378static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800379{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800380 struct ctl_table_header *head = PROC_I(inode)->sysctl;
381 if (!head)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800382 head = &sysctl_root_dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800383 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800384}
385
386static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
387 struct nameidata *nd)
388{
Al Viro9043476f2008-07-15 08:54:06 -0400389 struct ctl_table_header *head = grab_header(dir);
Al Viro9043476f2008-07-15 08:54:06 -0400390 struct ctl_table_header *h = NULL;
391 struct qstr *name = &dentry->d_name;
392 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800393 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400394 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800395 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800396
Al Viro9043476f2008-07-15 08:54:06 -0400397 if (IS_ERR(head))
398 return ERR_CAST(head);
399
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800400 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro9043476f2008-07-15 08:54:06 -0400401
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800402 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro9043476f2008-07-15 08:54:06 -0400403 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800404 goto out;
405
406 err = ERR_PTR(-ENOMEM);
Al Viro9043476f2008-07-15 08:54:06 -0400407 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
408 if (h)
409 sysctl_head_finish(h);
410
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800411 if (!inode)
412 goto out;
413
414 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100415 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800416 d_add(dentry, inode);
417
418out:
419 sysctl_head_finish(head);
420 return err;
421}
422
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700423static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
424 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800425{
Al Viro9043476f2008-07-15 08:54:06 -0400426 struct inode *inode = filp->f_path.dentry->d_inode;
427 struct ctl_table_header *head = grab_header(inode);
428 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100429 ssize_t error;
430 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800431
Al Viro9043476f2008-07-15 08:54:06 -0400432 if (IS_ERR(head))
433 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800434
435 /*
436 * At this point we know that the sysctl was not unregistered
437 * and won't be until we finish.
438 */
439 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700440 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800441 goto out;
442
Al Viro9043476f2008-07-15 08:54:06 -0400443 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
444 error = -EINVAL;
445 if (!table->proc_handler)
446 goto out;
447
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800448 /* careful: calling conventions are nasty here */
449 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700450 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800451 if (!error)
452 error = res;
453out:
454 sysctl_head_finish(head);
455
456 return error;
457}
458
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700459static ssize_t proc_sys_read(struct file *filp, char __user *buf,
460 size_t count, loff_t *ppos)
461{
462 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
463}
464
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800465static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
466 size_t count, loff_t *ppos)
467{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700468 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800469}
470
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700471static int proc_sys_open(struct inode *inode, struct file *filp)
472{
473 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
474
475 if (table->poll)
476 filp->private_data = proc_sys_poll_event(table->poll);
477
478 return 0;
479}
480
481static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
482{
483 struct inode *inode = filp->f_path.dentry->d_inode;
484 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
485 unsigned long event = (unsigned long)filp->private_data;
486 unsigned int ret = DEFAULT_POLLMASK;
487
488 if (!table->proc_handler)
489 goto out;
490
491 if (!table->poll)
492 goto out;
493
494 poll_wait(filp, &table->poll->wait, wait);
495
496 if (event != atomic_read(&table->poll->event)) {
497 filp->private_data = proc_sys_poll_event(table->poll);
498 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
499 }
500
501out:
502 return ret;
503}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800504
505static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro9043476f2008-07-15 08:54:06 -0400506 filldir_t filldir,
507 struct ctl_table_header *head,
508 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800509{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800510 struct dentry *child, *dir = filp->f_path.dentry;
511 struct inode *inode;
512 struct qstr qname;
513 ino_t ino = 0;
514 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800515
516 qname.name = table->procname;
517 qname.len = strlen(table->procname);
518 qname.hash = full_name_hash(qname.name, qname.len);
519
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800520 child = d_lookup(dir, &qname);
521 if (!child) {
Al Viro9043476f2008-07-15 08:54:06 -0400522 child = d_alloc(dir, &qname);
523 if (child) {
524 inode = proc_sys_make_inode(dir->d_sb, head, table);
525 if (!inode) {
526 dput(child);
527 return -ENOMEM;
528 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100529 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro9043476f2008-07-15 08:54:06 -0400530 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800531 }
Al Viro9043476f2008-07-15 08:54:06 -0400532 } else {
533 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800534 }
535 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800536 inode = child->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400537 ino = inode->i_ino;
538 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800539 dput(child);
Al Viro9043476f2008-07-15 08:54:06 -0400540 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
541}
542
543static int scan(struct ctl_table_header *head, ctl_table *table,
544 unsigned long *pos, struct file *file,
545 void *dirent, filldir_t filldir)
546{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800547 int res;
Al Viro9043476f2008-07-15 08:54:06 -0400548
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800549 if ((*pos)++ < file->f_pos)
550 return 0;
Al Viro9043476f2008-07-15 08:54:06 -0400551
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800552 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro9043476f2008-07-15 08:54:06 -0400553
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800554 if (res == 0)
555 file->f_pos = *pos;
Al Viro9043476f2008-07-15 08:54:06 -0400556
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800557 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800558}
559
560static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
561{
Al Viro9043476f2008-07-15 08:54:06 -0400562 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800563 struct inode *inode = dentry->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400564 struct ctl_table_header *head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400565 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800566 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800567 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800568 unsigned long pos;
Al Viro9043476f2008-07-15 08:54:06 -0400569 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800570
Al Viro9043476f2008-07-15 08:54:06 -0400571 if (IS_ERR(head))
572 return PTR_ERR(head);
573
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800574 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800575
576 ret = 0;
577 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
578 if (filp->f_pos == 0) {
579 if (filldir(dirent, ".", 1, filp->f_pos,
580 inode->i_ino, DT_DIR) < 0)
581 goto out;
582 filp->f_pos++;
583 }
584 if (filp->f_pos == 1) {
585 if (filldir(dirent, "..", 2, filp->f_pos,
586 parent_ino(dentry), DT_DIR) < 0)
587 goto out;
588 filp->f_pos++;
589 }
590 pos = 2;
591
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800592 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800593 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro9043476f2008-07-15 08:54:06 -0400594 if (ret) {
595 sysctl_head_finish(h);
596 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800597 }
598 }
599 ret = 1;
600out:
601 sysctl_head_finish(head);
602 return ret;
603}
604
Al Viro10556cb22011-06-20 19:28:19 -0400605static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800606{
607 /*
608 * sysctl entries that are not writeable,
609 * are _NOT_ writeable, capabilities or not.
610 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200611 struct ctl_table_header *head;
612 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800613 int error;
614
Miklos Szeredif696a362008-07-31 13:41:58 +0200615 /* Executable files are not allowed under /proc/sys/ */
616 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
617 return -EACCES;
618
619 head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400620 if (IS_ERR(head))
621 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800622
Miklos Szeredif696a362008-07-31 13:41:58 +0200623 table = PROC_I(inode)->sysctl_entry;
Al Viro9043476f2008-07-15 08:54:06 -0400624 if (!table) /* global root - r-xr-xr-x */
625 error = mask & MAY_WRITE ? -EACCES : 0;
626 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400627 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800628
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800629 sysctl_head_finish(head);
630 return error;
631}
632
633static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
634{
635 struct inode *inode = dentry->d_inode;
636 int error;
637
638 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
639 return -EPERM;
640
641 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200642 if (error)
643 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800644
Christoph Hellwig10257742010-06-04 11:30:02 +0200645 if ((attr->ia_valid & ATTR_SIZE) &&
646 attr->ia_size != i_size_read(inode)) {
647 error = vmtruncate(inode, attr->ia_size);
648 if (error)
649 return error;
650 }
651
652 setattr_copy(inode, attr);
653 mark_inode_dirty(inode);
654 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800655}
656
Al Viro9043476f2008-07-15 08:54:06 -0400657static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
658{
659 struct inode *inode = dentry->d_inode;
660 struct ctl_table_header *head = grab_header(inode);
661 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
662
663 if (IS_ERR(head))
664 return PTR_ERR(head);
665
666 generic_fillattr(inode, stat);
667 if (table)
668 stat->mode = (stat->mode & S_IFMT) | table->mode;
669
670 sysctl_head_finish(head);
671 return 0;
672}
673
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800674static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700675 .open = proc_sys_open,
676 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800677 .read = proc_sys_read,
678 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200679 .llseek = default_llseek,
Al Viro9043476f2008-07-15 08:54:06 -0400680};
681
682static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700683 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800684 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200685 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800686};
687
Jan Engelhardt03a44822008-02-08 04:21:19 -0800688static const struct inode_operations proc_sys_inode_operations = {
Al Viro9043476f2008-07-15 08:54:06 -0400689 .permission = proc_sys_permission,
690 .setattr = proc_sys_setattr,
691 .getattr = proc_sys_getattr,
692};
693
694static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800695 .lookup = proc_sys_lookup,
696 .permission = proc_sys_permission,
697 .setattr = proc_sys_setattr,
Al Viro9043476f2008-07-15 08:54:06 -0400698 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800699};
700
701static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
702{
Nick Piggin34286d62011-01-07 17:49:57 +1100703 if (nd->flags & LOOKUP_RCU)
704 return -ECHILD;
Al Viro9043476f2008-07-15 08:54:06 -0400705 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
706}
707
Nick Pigginfe15ce42011-01-07 17:49:23 +1100708static int proc_sys_delete(const struct dentry *dentry)
Al Viro9043476f2008-07-15 08:54:06 -0400709{
710 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
711}
712
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800713static int sysctl_is_seen(struct ctl_table_header *p)
714{
715 struct ctl_table_set *set = p->set;
716 int res;
717 spin_lock(&sysctl_lock);
718 if (p->unregistering)
719 res = 0;
720 else if (!set->is_seen)
721 res = 1;
722 else
723 res = set->is_seen(set);
724 spin_unlock(&sysctl_lock);
725 return res;
726}
727
Nick Piggin621e1552011-01-07 17:49:27 +1100728static int proc_sys_compare(const struct dentry *parent,
729 const struct inode *pinode,
730 const struct dentry *dentry, const struct inode *inode,
731 unsigned int len, const char *str, const struct qstr *name)
Al Viro9043476f2008-07-15 08:54:06 -0400732{
Al Virodfef6dcd32011-03-08 01:25:28 -0500733 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100734 /* Although proc doesn't have negative dentries, rcu-walk means
735 * that inode here can be NULL */
Al Virodfef6dcd32011-03-08 01:25:28 -0500736 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100737 if (!inode)
Al Virodfef6dcd32011-03-08 01:25:28 -0500738 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100739 if (name->len != len)
Al Viro9043476f2008-07-15 08:54:06 -0400740 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100741 if (memcmp(name->name, str, len))
Al Viro9043476f2008-07-15 08:54:06 -0400742 return 1;
Al Virodfef6dcd32011-03-08 01:25:28 -0500743 head = rcu_dereference(PROC_I(inode)->sysctl);
744 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800745}
746
Al Virod72f71e2009-02-20 05:58:47 +0000747static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800748 .d_revalidate = proc_sys_revalidate,
Al Viro9043476f2008-07-15 08:54:06 -0400749 .d_delete = proc_sys_delete,
750 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800751};
752
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800753static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir,
754 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800755{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800756 struct ctl_table_header *head;
757 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800758
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800759 entry = find_entry(&head, set, dir, name, namelen);
760 if (!entry)
761 return ERR_PTR(-ENOENT);
762 if (S_ISDIR(entry->mode))
763 return container_of(head, struct ctl_dir, header);
764 return ERR_PTR(-ENOTDIR);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800765}
766
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800767static struct ctl_dir *new_dir(struct ctl_table_set *set,
768 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800769{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800770 struct ctl_table *table;
771 struct ctl_dir *new;
772 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800773
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800774 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
775 namelen + 1, GFP_KERNEL);
776 if (!new)
777 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800778
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800779 table = (struct ctl_table *)(new + 1);
780 new_name = (char *)(table + 2);
781 memcpy(new_name, name, namelen);
782 new_name[namelen] = '\0';
783 table[0].procname = new_name;
784 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
785 init_header(&new->header, set->root, set, table);
786
787 return new;
788}
789
790static struct ctl_dir *get_subdir(struct ctl_table_set *set,
791 struct ctl_dir *dir, const char *name, int namelen)
792{
793 struct ctl_dir *subdir, *new = NULL;
794
795 spin_lock(&sysctl_lock);
796 subdir = find_subdir(dir->header.set, dir, name, namelen);
797 if (!IS_ERR(subdir))
798 goto found;
799 if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set)
800 subdir = find_subdir(set, dir, name, namelen);
801 if (!IS_ERR(subdir))
802 goto found;
803 if (PTR_ERR(subdir) != -ENOENT)
804 goto failed;
805
806 spin_unlock(&sysctl_lock);
807 new = new_dir(set, name, namelen);
808 spin_lock(&sysctl_lock);
809 subdir = ERR_PTR(-ENOMEM);
810 if (!new)
811 goto failed;
812
813 subdir = find_subdir(set, dir, name, namelen);
814 if (!IS_ERR(subdir))
815 goto found;
816 if (PTR_ERR(subdir) != -ENOENT)
817 goto failed;
818
819 insert_header(dir, &new->header);
820 subdir = new;
821found:
822 subdir->header.nreg++;
823failed:
824 if (unlikely(IS_ERR(subdir))) {
825 printk(KERN_ERR "sysctl could not get directory: %*.*s %ld\n",
826 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800827 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800828 drop_sysctl_table(&dir->header);
829 if (new)
830 drop_sysctl_table(&new->header);
831 spin_unlock(&sysctl_lock);
832 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800833}
834
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800835static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
836 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800837{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800838 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800839 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800840
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800841 for (entry = old; entry->procname; entry++) {
842 for (test = table; test->procname; test++) {
843 if (strcmp(entry->procname, test->procname) == 0) {
844 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
845 path, test->procname);
846 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800847 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800848 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800849 }
850 return error;
851}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800852
853static int sysctl_check_dups(struct nsproxy *namespaces,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800854 struct ctl_dir *dir,
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800855 const char *path, struct ctl_table *table)
856{
857 struct ctl_table_root *root;
858 struct ctl_table_set *set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800859 struct ctl_table_header *head;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800860 int error = 0;
861
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800862 root = &sysctl_table_root;
863 do {
864 set = lookup_header_set(root, namespaces);
865
866 list_for_each_entry(head, &set->list, ctl_entry) {
867 if (head->unregistering)
868 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800869 if (head->parent != dir)
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800870 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800871 error = sysctl_check_table_dups(path, head->ctl_table,
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800872 table);
873 }
874 root = list_entry(root->root_list.next,
875 struct ctl_table_root, root_list);
876 } while (root != &sysctl_table_root);
877 return error;
878}
879
880static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
881{
882 struct va_format vaf;
883 va_list args;
884
885 va_start(args, fmt);
886 vaf.fmt = fmt;
887 vaf.va = &args;
888
889 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
890 path, table->procname, &vaf);
891
892 va_end(args);
893 return -EINVAL;
894}
895
896static int sysctl_check_table(const char *path, struct ctl_table *table)
897{
898 int err = 0;
899 for (; table->procname; table++) {
900 if (table->child)
901 err = sysctl_err(path, table, "Not a file");
902
903 if ((table->proc_handler == proc_dostring) ||
904 (table->proc_handler == proc_dointvec) ||
905 (table->proc_handler == proc_dointvec_minmax) ||
906 (table->proc_handler == proc_dointvec_jiffies) ||
907 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
908 (table->proc_handler == proc_dointvec_ms_jiffies) ||
909 (table->proc_handler == proc_doulongvec_minmax) ||
910 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
911 if (!table->data)
912 err = sysctl_err(path, table, "No data");
913 if (!table->maxlen)
914 err = sysctl_err(path, table, "No maxlen");
915 }
916 if (!table->proc_handler)
917 err = sysctl_err(path, table, "No proc_handler");
918
919 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
920 err = sysctl_err(path, table, "bogus .mode 0%o",
921 table->mode);
922 }
923 return err;
924}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800925
926/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800927 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800928 * @root: List of sysctl headers to register on
929 * @namespaces: Data to compute which lists of sysctl entries are visible
930 * @path: The path to the directory the sysctl table is in.
931 * @table: the top-level table structure
932 *
933 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
934 * array. A completely 0 filled entry terminates the table.
935 *
936 * The members of the &struct ctl_table structure are used as follows:
937 *
938 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
939 * enter a sysctl file
940 *
941 * data - a pointer to data for use by proc_handler
942 *
943 * maxlen - the maximum size in bytes of the data
944 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800945 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800946 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800947 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800948 *
949 * proc_handler - the text handler routine (described below)
950 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800951 * extra1, extra2 - extra pointers usable by the proc handler routines
952 *
953 * Leaf nodes in the sysctl tree will be represented by a single file
954 * under /proc; non-leaf nodes will be represented by directories.
955 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -0800956 * There must be a proc_handler routine for any terminal nodes.
957 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800958 *
959 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
960 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
961 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
962 *
963 * It is the handler's job to read the input buffer from user memory
964 * and process it. The handler should return 0 on success.
965 *
966 * This routine returns %NULL on a failure to register, and a pointer
967 * to the table header on success.
968 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800969struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800970 struct ctl_table_root *root,
971 struct nsproxy *namespaces,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800972 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800973{
974 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -0800975 const char *name, *nextname;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800976 struct ctl_table_set *set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800977 struct ctl_dir *dir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800978
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800979 header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800980 if (!header)
981 return NULL;
982
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800983 init_header(header, root, NULL, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800984 if (sysctl_check_table(path, table))
985 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -0800986
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800987 spin_lock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800988 header->set = set = lookup_header_set(root, namespaces);
989 dir = &sysctl_root_dir;
990 dir->header.nreg++;
991 spin_unlock(&sysctl_lock);
992
993 /* Find the directory for the ctl_table */
994 for (name = path; name; name = nextname) {
995 int namelen;
996 nextname = strchr(name, '/');
997 if (nextname) {
998 namelen = nextname - name;
999 nextname++;
1000 } else {
1001 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001002 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001003 if (namelen == 0)
1004 continue;
1005
1006 dir = get_subdir(set, dir, name, namelen);
1007 if (IS_ERR(dir))
1008 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001009 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001010 spin_lock(&sysctl_lock);
1011 if (sysctl_check_dups(namespaces, dir, path, table))
1012 goto fail_put_dir_locked;
1013 insert_header(dir, header);
1014 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001015 spin_unlock(&sysctl_lock);
1016
1017 return header;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001018fail_put_dir_locked:
1019 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001020 spin_unlock(&sysctl_lock);
1021fail:
1022 kfree(header);
1023 dump_stack();
1024 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001025}
1026
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001027static char *append_path(const char *path, char *pos, const char *name)
1028{
1029 int namelen;
1030 namelen = strlen(name);
1031 if (((pos - path) + namelen + 2) >= PATH_MAX)
1032 return NULL;
1033 memcpy(pos, name, namelen);
1034 pos[namelen] = '/';
1035 pos[namelen + 1] = '\0';
1036 pos += namelen + 1;
1037 return pos;
1038}
1039
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001040static int count_subheaders(struct ctl_table *table)
1041{
1042 int has_files = 0;
1043 int nr_subheaders = 0;
1044 struct ctl_table *entry;
1045
1046 /* special case: no directory and empty directory */
1047 if (!table || !table->procname)
1048 return 1;
1049
1050 for (entry = table; entry->procname; entry++) {
1051 if (entry->child)
1052 nr_subheaders += count_subheaders(entry->child);
1053 else
1054 has_files = 1;
1055 }
1056 return nr_subheaders + has_files;
1057}
1058
1059static int register_leaf_sysctl_tables(const char *path, char *pos,
1060 struct ctl_table_header ***subheader,
1061 struct ctl_table_root *root, struct nsproxy *namespaces,
1062 struct ctl_table *table)
1063{
1064 struct ctl_table *ctl_table_arg = NULL;
1065 struct ctl_table *entry, *files;
1066 int nr_files = 0;
1067 int nr_dirs = 0;
1068 int err = -ENOMEM;
1069
1070 for (entry = table; entry->procname; entry++) {
1071 if (entry->child)
1072 nr_dirs++;
1073 else
1074 nr_files++;
1075 }
1076
1077 files = table;
1078 /* If there are mixed files and directories we need a new table */
1079 if (nr_dirs && nr_files) {
1080 struct ctl_table *new;
1081 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1082 GFP_KERNEL);
1083 if (!files)
1084 goto out;
1085
1086 ctl_table_arg = files;
1087 for (new = files, entry = table; entry->procname; entry++) {
1088 if (entry->child)
1089 continue;
1090 *new = *entry;
1091 new++;
1092 }
1093 }
1094
1095 /* Register everything except a directory full of subdirectories */
1096 if (nr_files || !nr_dirs) {
1097 struct ctl_table_header *header;
1098 header = __register_sysctl_table(root, namespaces, path, files);
1099 if (!header) {
1100 kfree(ctl_table_arg);
1101 goto out;
1102 }
1103
1104 /* Remember if we need to free the file table */
1105 header->ctl_table_arg = ctl_table_arg;
1106 **subheader = header;
1107 (*subheader)++;
1108 }
1109
1110 /* Recurse into the subdirectories. */
1111 for (entry = table; entry->procname; entry++) {
1112 char *child_pos;
1113
1114 if (!entry->child)
1115 continue;
1116
1117 err = -ENAMETOOLONG;
1118 child_pos = append_path(path, pos, entry->procname);
1119 if (!child_pos)
1120 goto out;
1121
1122 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1123 root, namespaces, entry->child);
1124 pos[0] = '\0';
1125 if (err)
1126 goto out;
1127 }
1128 err = 0;
1129out:
1130 /* On failure our caller will unregister all registered subheaders */
1131 return err;
1132}
1133
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001134/**
1135 * __register_sysctl_paths - register a sysctl table hierarchy
1136 * @root: List of sysctl headers to register on
1137 * @namespaces: Data to compute which lists of sysctl entries are visible
1138 * @path: The path to the directory the sysctl table is in.
1139 * @table: the top-level table structure
1140 *
1141 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1142 * array. A completely 0 filled entry terminates the table.
1143 *
1144 * See __register_sysctl_table for more details.
1145 */
1146struct ctl_table_header *__register_sysctl_paths(
1147 struct ctl_table_root *root,
1148 struct nsproxy *namespaces,
1149 const struct ctl_path *path, struct ctl_table *table)
1150{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001151 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001152 int nr_subheaders = count_subheaders(table);
1153 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001154 const struct ctl_path *component;
1155 char *new_path, *pos;
1156
1157 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1158 if (!new_path)
1159 return NULL;
1160
1161 pos[0] = '\0';
1162 for (component = path; component->procname; component++) {
1163 pos = append_path(new_path, pos, component->procname);
1164 if (!pos)
1165 goto out;
1166 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001167 while (table->procname && table->child && !table[1].procname) {
1168 pos = append_path(new_path, pos, table->procname);
1169 if (!pos)
1170 goto out;
1171 table = table->child;
1172 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001173 if (nr_subheaders == 1) {
1174 header = __register_sysctl_table(root, namespaces, new_path, table);
1175 if (header)
1176 header->ctl_table_arg = ctl_table_arg;
1177 } else {
1178 header = kzalloc(sizeof(*header) +
1179 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1180 if (!header)
1181 goto out;
1182
1183 subheaders = (struct ctl_table_header **) (header + 1);
1184 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001185 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001186
1187 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1188 root, namespaces, table))
1189 goto err_register_leaves;
1190 }
1191
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001192out:
1193 kfree(new_path);
1194 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001195
1196err_register_leaves:
1197 while (subheader > subheaders) {
1198 struct ctl_table_header *subh = *(--subheader);
1199 struct ctl_table *table = subh->ctl_table_arg;
1200 unregister_sysctl_table(subh);
1201 kfree(table);
1202 }
1203 kfree(header);
1204 header = NULL;
1205 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001206}
1207
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001208/**
1209 * register_sysctl_table_path - register a sysctl table hierarchy
1210 * @path: The path to the directory the sysctl table is in.
1211 * @table: the top-level table structure
1212 *
1213 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1214 * array. A completely 0 filled entry terminates the table.
1215 *
1216 * See __register_sysctl_paths for more details.
1217 */
1218struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1219 struct ctl_table *table)
1220{
1221 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1222 path, table);
1223}
1224EXPORT_SYMBOL(register_sysctl_paths);
1225
1226/**
1227 * register_sysctl_table - register a sysctl table hierarchy
1228 * @table: the top-level table structure
1229 *
1230 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1231 * array. A completely 0 filled entry terminates the table.
1232 *
1233 * See register_sysctl_paths for more details.
1234 */
1235struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1236{
1237 static const struct ctl_path null_path[] = { {} };
1238
1239 return register_sysctl_paths(null_path, table);
1240}
1241EXPORT_SYMBOL(register_sysctl_table);
1242
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001243static void drop_sysctl_table(struct ctl_table_header *header)
1244{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001245 struct ctl_dir *parent = header->parent;
1246
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001247 if (--header->nreg)
1248 return;
1249
1250 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001251 if (!--header->count)
1252 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001253
1254 if (parent)
1255 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001256}
1257
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001258/**
1259 * unregister_sysctl_table - unregister a sysctl table hierarchy
1260 * @header: the header returned from register_sysctl_table
1261 *
1262 * Unregisters the sysctl table and all children. proc entries may not
1263 * actually be removed until they are no longer used by anyone.
1264 */
1265void unregister_sysctl_table(struct ctl_table_header * header)
1266{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001267 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001268 might_sleep();
1269
1270 if (header == NULL)
1271 return;
1272
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001273 nr_subheaders = count_subheaders(header->ctl_table_arg);
1274 if (unlikely(nr_subheaders > 1)) {
1275 struct ctl_table_header **subheaders;
1276 int i;
1277
1278 subheaders = (struct ctl_table_header **)(header + 1);
1279 for (i = nr_subheaders -1; i >= 0; i--) {
1280 struct ctl_table_header *subh = subheaders[i];
1281 struct ctl_table *table = subh->ctl_table_arg;
1282 unregister_sysctl_table(subh);
1283 kfree(table);
1284 }
1285 kfree(header);
1286 return;
1287 }
1288
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001289 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001290 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001291 spin_unlock(&sysctl_lock);
1292}
1293EXPORT_SYMBOL(unregister_sysctl_table);
1294
1295void setup_sysctl_set(struct ctl_table_set *p,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001296 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001297 int (*is_seen)(struct ctl_table_set *))
1298{
1299 INIT_LIST_HEAD(&p->list);
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001300 p->root = root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001301 p->is_seen = is_seen;
1302}
1303
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001304void retire_sysctl_set(struct ctl_table_set *set)
1305{
1306 WARN_ON(!list_empty(&set->list));
1307}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001308
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001309int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001310{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001311 struct proc_dir_entry *proc_sys_root;
1312
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001313 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro9043476f2008-07-15 08:54:06 -04001314 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1315 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001316 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001317
1318 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001319}