blob: 880b075e5d73ffb391b4a2e9aabf4ff0e935f159 [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>
Al Viro40401532012-02-13 03:58:52 +00009#include <linux/sched.h>
Nick Piggin34286d62011-01-07 17:49:57 +110010#include <linux/namei.h>
Al Viro40401532012-02-13 03:58:52 +000011#include <linux/mm.h>
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080012#include <linux/module.h>
Chintan Pandya4d3267c2013-07-02 16:00:49 -070013#include <linux/kmemleak.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -080014#include "internal.h"
15
Al Virod72f71e2009-02-20 05:58:47 +000016static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080017static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080018static const struct inode_operations proc_sys_inode_operations;
Al Viro90434762008-07-15 08:54:06 -040019static const struct file_operations proc_sys_dir_file_operations;
20static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080021
Lucas De Marchif1ecf062011-11-02 13:39:22 -070022void proc_sys_poll_notify(struct ctl_table_poll *poll)
23{
24 if (!poll)
25 return;
26
27 atomic_inc(&poll->event);
28 wake_up_interruptible(&poll->wait);
29}
30
Eric W. Biedermana1945582012-01-21 17:51:48 -080031static struct ctl_table root_table[] = {
32 {
33 .procname = "",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080034 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
Eric W. Biedermana1945582012-01-21 17:51:48 -080035 },
36 { }
37};
Eric W. Biederman0e47c992012-01-07 23:24:30 -080038static struct ctl_table_root sysctl_table_root = {
Eric W. Biederman0e47c992012-01-07 23:24:30 -080039 .default_set.dir.header = {
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080040 {{.count = 1,
41 .nreg = 1,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080042 .ctl_table = root_table }},
Eric W. Biederman0e47c992012-01-07 23:24:30 -080043 .ctl_table_arg = root_table,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080044 .root = &sysctl_table_root,
45 .set = &sysctl_table_root.default_set,
46 },
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080047};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080048
49static DEFINE_SPINLOCK(sysctl_lock);
50
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080051static void drop_sysctl_table(struct ctl_table_header *header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -080052static int sysctl_follow_link(struct ctl_table_header **phead,
53 struct ctl_table **pentry, struct nsproxy *namespaces);
54static int insert_links(struct ctl_table_header *head);
55static void put_links(struct ctl_table_header *header);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080056
Eric W. Biederman69801282012-01-21 20:09:45 -080057static void sysctl_print_dir(struct ctl_dir *dir)
58{
59 if (dir->header.parent)
60 sysctl_print_dir(dir->header.parent);
61 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
62}
63
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080064static int namecmp(const char *name1, int len1, const char *name2, int len2)
65{
66 int minlen;
67 int cmp;
68
69 minlen = len1;
70 if (minlen > len2)
71 minlen = len2;
72
73 cmp = memcmp(name1, name2, minlen);
74 if (cmp == 0)
75 cmp = len1 - len2;
76 return cmp;
77}
78
Eric W. Biederman60f126d2012-01-30 21:23:52 -080079/* Called under sysctl_lock */
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080080static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c992012-01-07 23:24:30 -080081 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080082{
83 struct ctl_table_header *head;
84 struct ctl_table *entry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080085 struct rb_node *node = dir->root.rb_node;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080086
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080087 while (node)
88 {
89 struct ctl_node *ctl_node;
90 const char *procname;
91 int cmp;
92
93 ctl_node = rb_entry(node, struct ctl_node, node);
94 head = ctl_node->header;
95 entry = &head->ctl_table[ctl_node - head->node];
96 procname = entry->procname;
97
98 cmp = namecmp(name, namelen, procname, strlen(procname));
99 if (cmp < 0)
100 node = node->rb_left;
101 else if (cmp > 0)
102 node = node->rb_right;
103 else {
104 *phead = head;
105 return entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800106 }
107 }
108 return NULL;
109}
110
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800111static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
112{
113 struct rb_node *node = &head->node[entry - head->ctl_table].node;
114 struct rb_node **p = &head->parent->root.rb_node;
115 struct rb_node *parent = NULL;
116 const char *name = entry->procname;
117 int namelen = strlen(name);
118
119 while (*p) {
120 struct ctl_table_header *parent_head;
121 struct ctl_table *parent_entry;
122 struct ctl_node *parent_node;
123 const char *parent_name;
124 int cmp;
125
126 parent = *p;
127 parent_node = rb_entry(parent, struct ctl_node, node);
128 parent_head = parent_node->header;
129 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
130 parent_name = parent_entry->procname;
131
132 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
133 if (cmp < 0)
134 p = &(*p)->rb_left;
135 else if (cmp > 0)
136 p = &(*p)->rb_right;
137 else {
138 printk(KERN_ERR "sysctl duplicate entry: ");
139 sysctl_print_dir(head->parent);
140 printk(KERN_CONT "/%s\n", entry->procname);
141 return -EEXIST;
142 }
143 }
144
145 rb_link_node(node, parent, p);
146 return 0;
147}
148
149static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
150{
151 struct rb_node *node = &head->node[entry - head->ctl_table].node;
152
153 rb_erase(node, &head->parent->root);
154}
155
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800156static void init_header(struct ctl_table_header *head,
157 struct ctl_table_root *root, struct ctl_table_set *set,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800158 struct ctl_node *node, struct ctl_table *table)
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800159{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800160 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800161 head->ctl_table_arg = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800162 head->used = 0;
163 head->count = 1;
164 head->nreg = 1;
165 head->unregistering = NULL;
166 head->root = root;
167 head->set = set;
168 head->parent = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800169 head->node = node;
170 if (node) {
171 struct ctl_table *entry;
172 for (entry = table; entry->procname; entry++, node++) {
173 rb_init_node(&node->node);
174 node->header = head;
175 }
176 }
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800177}
178
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800179static void erase_header(struct ctl_table_header *head)
180{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800181 struct ctl_table *entry;
182 for (entry = head->ctl_table; entry->procname; entry++)
183 erase_entry(head, entry);
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800184}
185
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800186static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800187{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800188 struct ctl_table *entry;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800189 int err;
190
191 dir->header.nreg++;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800192 header->parent = dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800193 err = insert_links(header);
194 if (err)
195 goto fail_links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800196 for (entry = header->ctl_table; entry->procname; entry++) {
197 err = insert_entry(header, entry);
198 if (err)
199 goto fail;
200 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800201 return 0;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800202fail:
203 erase_header(header);
204 put_links(header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800205fail_links:
206 header->parent = NULL;
207 drop_sysctl_table(&dir->header);
208 return err;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800209}
210
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800211/* called under sysctl_lock */
212static int use_table(struct ctl_table_header *p)
213{
214 if (unlikely(p->unregistering))
215 return 0;
216 p->used++;
217 return 1;
218}
219
220/* called under sysctl_lock */
221static void unuse_table(struct ctl_table_header *p)
222{
223 if (!--p->used)
224 if (unlikely(p->unregistering))
225 complete(p->unregistering);
226}
227
228/* called under sysctl_lock, will reacquire if has to wait */
229static void start_unregistering(struct ctl_table_header *p)
230{
231 /*
232 * if p->used is 0, nobody will ever touch that entry again;
233 * we'll eliminate all paths to it before dropping sysctl_lock
234 */
235 if (unlikely(p->used)) {
236 struct completion wait;
237 init_completion(&wait);
238 p->unregistering = &wait;
239 spin_unlock(&sysctl_lock);
240 wait_for_completion(&wait);
241 spin_lock(&sysctl_lock);
242 } else {
243 /* anything non-NULL; we'll never dereference it */
244 p->unregistering = ERR_PTR(-EINVAL);
245 }
246 /*
247 * do not remove from the list until nobody holds it; walking the
248 * list in do_sysctl() relies on that.
249 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800250 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800251}
252
253static void sysctl_head_get(struct ctl_table_header *head)
254{
255 spin_lock(&sysctl_lock);
256 head->count++;
257 spin_unlock(&sysctl_lock);
258}
259
260void sysctl_head_put(struct ctl_table_header *head)
261{
262 spin_lock(&sysctl_lock);
263 if (!--head->count)
264 kfree_rcu(head, rcu);
265 spin_unlock(&sysctl_lock);
266}
267
268static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
269{
270 if (!head)
271 BUG();
272 spin_lock(&sysctl_lock);
273 if (!use_table(head))
274 head = ERR_PTR(-ENOENT);
275 spin_unlock(&sysctl_lock);
276 return head;
277}
278
279static void sysctl_head_finish(struct ctl_table_header *head)
280{
281 if (!head)
282 return;
283 spin_lock(&sysctl_lock);
284 unuse_table(head);
285 spin_unlock(&sysctl_lock);
286}
287
288static struct ctl_table_set *
289lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
290{
291 struct ctl_table_set *set = &root->default_set;
292 if (root->lookup)
293 set = root->lookup(root, namespaces);
294 return set;
295}
296
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800297static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800298 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800299 const char *name, int namelen)
300{
301 struct ctl_table_header *head;
302 struct ctl_table *entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800303
304 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800305 entry = find_entry(&head, dir, name, namelen);
306 if (entry && use_table(head))
307 *phead = head;
308 else
309 entry = NULL;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800310 spin_unlock(&sysctl_lock);
311 return entry;
312}
313
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800314static struct ctl_node *first_usable_entry(struct rb_node *node)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800315{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800316 struct ctl_node *ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800317
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800318 for (;node; node = rb_next(node)) {
319 ctl_node = rb_entry(node, struct ctl_node, node);
320 if (use_table(ctl_node->header))
321 return ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800322 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800323 return NULL;
324}
325
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800326static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800327 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800328{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800329 struct ctl_table_header *head = NULL;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800330 struct ctl_table *entry = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800331 struct ctl_node *ctl_node;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800332
333 spin_lock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800334 ctl_node = first_usable_entry(rb_first(&dir->root));
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800335 spin_unlock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800336 if (ctl_node) {
337 head = ctl_node->header;
338 entry = &head->ctl_table[ctl_node - head->node];
339 }
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800340 *phead = head;
341 *pentry = entry;
342}
343
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800344static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800345{
346 struct ctl_table_header *head = *phead;
347 struct ctl_table *entry = *pentry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800348 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800349
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800350 spin_lock(&sysctl_lock);
351 unuse_table(head);
352
353 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
354 spin_unlock(&sysctl_lock);
355 head = NULL;
356 if (ctl_node) {
357 head = ctl_node->header;
358 entry = &head->ctl_table[ctl_node - head->node];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800359 }
360 *phead = head;
361 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800362}
363
364void register_sysctl_root(struct ctl_table_root *root)
365{
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800366}
367
368/*
369 * sysctl_perm does NOT grant the superuser all rights automatically, because
370 * some sysctl variables are readonly even to root.
371 */
372
373static int test_perm(int mode, int op)
374{
375 if (!current_euid())
376 mode >>= 6;
377 else if (in_egroup_p(0))
378 mode >>= 3;
379 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
380 return 0;
381 return -EACCES;
382}
383
384static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
385{
386 int mode;
387
388 if (root->permissions)
389 mode = root->permissions(root, current->nsproxy, table);
390 else
391 mode = table->mode;
392
393 return test_perm(mode, op);
394}
395
Al Viro90434762008-07-15 08:54:06 -0400396static struct inode *proc_sys_make_inode(struct super_block *sb,
397 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800398{
399 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400400 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800401
Al Viro90434762008-07-15 08:54:06 -0400402 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800403 if (!inode)
404 goto out;
405
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400406 inode->i_ino = get_next_ino();
407
Al Viro90434762008-07-15 08:54:06 -0400408 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800409 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400410 ei->sysctl = head;
411 ei->sysctl_entry = table;
412
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800413 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400414 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800415 if (!S_ISDIR(table->mode)) {
Al Viro90434762008-07-15 08:54:06 -0400416 inode->i_mode |= S_IFREG;
417 inode->i_op = &proc_sys_inode_operations;
418 inode->i_fop = &proc_sys_file_operations;
419 } else {
420 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400421 inode->i_op = &proc_sys_dir_operations;
422 inode->i_fop = &proc_sys_dir_file_operations;
423 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800424out:
425 return inode;
426}
427
Adrian Bunk81324362008-10-03 00:33:54 +0400428static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800429{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800430 struct ctl_table_header *head = PROC_I(inode)->sysctl;
431 if (!head)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800432 head = &sysctl_table_root.default_set.dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800433 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800434}
435
436static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
Al Viro986122f2012-06-10 17:13:09 -0400437 unsigned int flags)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800438{
Al Viro90434762008-07-15 08:54:06 -0400439 struct ctl_table_header *head = grab_header(dir);
Al Viro90434762008-07-15 08:54:06 -0400440 struct ctl_table_header *h = NULL;
441 struct qstr *name = &dentry->d_name;
442 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800443 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400444 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800445 struct ctl_dir *ctl_dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800446 int ret;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800447
Al Viro90434762008-07-15 08:54:06 -0400448 if (IS_ERR(head))
449 return ERR_CAST(head);
450
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800451 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro90434762008-07-15 08:54:06 -0400452
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800453 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400454 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800455 goto out;
456
Eric W. Biederman4e757322012-01-30 21:24:59 -0800457 if (S_ISLNK(p->mode)) {
458 ret = sysctl_follow_link(&h, &p, current->nsproxy);
459 err = ERR_PTR(ret);
460 if (ret)
461 goto out;
462 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800463
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800464 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400465 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800466 if (!inode)
467 goto out;
468
469 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100470 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800471 d_add(dentry, inode);
472
473out:
Francesco Ruggeri2e4765e2012-09-13 15:03:37 -0700474 if (h)
475 sysctl_head_finish(h);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800476 sysctl_head_finish(head);
477 return err;
478}
479
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700480static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
481 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800482{
Al Viro90434762008-07-15 08:54:06 -0400483 struct inode *inode = filp->f_path.dentry->d_inode;
484 struct ctl_table_header *head = grab_header(inode);
485 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100486 ssize_t error;
487 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800488
Al Viro90434762008-07-15 08:54:06 -0400489 if (IS_ERR(head))
490 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800491
492 /*
493 * At this point we know that the sysctl was not unregistered
494 * and won't be until we finish.
495 */
496 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700497 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800498 goto out;
499
Al Viro90434762008-07-15 08:54:06 -0400500 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
501 error = -EINVAL;
502 if (!table->proc_handler)
503 goto out;
504
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800505 /* careful: calling conventions are nasty here */
506 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700507 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800508 if (!error)
509 error = res;
510out:
511 sysctl_head_finish(head);
512
513 return error;
514}
515
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700516static ssize_t proc_sys_read(struct file *filp, char __user *buf,
517 size_t count, loff_t *ppos)
518{
519 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
520}
521
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800522static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
523 size_t count, loff_t *ppos)
524{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700525 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800526}
527
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700528static int proc_sys_open(struct inode *inode, struct file *filp)
529{
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700530 struct ctl_table_header *head = grab_header(inode);
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700531 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
532
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700533 /* sysctl was unregistered */
534 if (IS_ERR(head))
535 return PTR_ERR(head);
536
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700537 if (table->poll)
538 filp->private_data = proc_sys_poll_event(table->poll);
539
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700540 sysctl_head_finish(head);
541
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700542 return 0;
543}
544
545static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
546{
547 struct inode *inode = filp->f_path.dentry->d_inode;
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700548 struct ctl_table_header *head = grab_header(inode);
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700549 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700550 unsigned int ret = DEFAULT_POLLMASK;
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700551 unsigned long event;
552
553 /* sysctl was unregistered */
554 if (IS_ERR(head))
555 return POLLERR | POLLHUP;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700556
557 if (!table->proc_handler)
558 goto out;
559
560 if (!table->poll)
561 goto out;
562
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700563 event = (unsigned long)filp->private_data;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700564 poll_wait(filp, &table->poll->wait, wait);
565
566 if (event != atomic_read(&table->poll->event)) {
567 filp->private_data = proc_sys_poll_event(table->poll);
568 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
569 }
570
571out:
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700572 sysctl_head_finish(head);
573
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700574 return ret;
575}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800576
577static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400578 filldir_t filldir,
579 struct ctl_table_header *head,
580 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800581{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800582 struct dentry *child, *dir = filp->f_path.dentry;
583 struct inode *inode;
584 struct qstr qname;
585 ino_t ino = 0;
586 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800587
588 qname.name = table->procname;
589 qname.len = strlen(table->procname);
590 qname.hash = full_name_hash(qname.name, qname.len);
591
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800592 child = d_lookup(dir, &qname);
593 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400594 child = d_alloc(dir, &qname);
595 if (child) {
596 inode = proc_sys_make_inode(dir->d_sb, head, table);
597 if (!inode) {
598 dput(child);
599 return -ENOMEM;
600 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100601 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400602 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800603 }
Al Viro90434762008-07-15 08:54:06 -0400604 } else {
605 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800606 }
607 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800608 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400609 ino = inode->i_ino;
610 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800611 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400612 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
613}
614
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800615static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
616 filldir_t filldir,
617 struct ctl_table_header *head,
618 struct ctl_table *table)
619{
620 int err, ret = 0;
621 head = sysctl_head_grab(head);
622
Eric W. Biederman4e757322012-01-30 21:24:59 -0800623 if (S_ISLNK(table->mode)) {
624 /* It is not an error if we can not follow the link ignore it */
625 err = sysctl_follow_link(&head, &table, current->nsproxy);
626 if (err)
627 goto out;
628 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800629
630 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
631out:
632 sysctl_head_finish(head);
633 return ret;
634}
635
Al Viro90434762008-07-15 08:54:06 -0400636static int scan(struct ctl_table_header *head, ctl_table *table,
637 unsigned long *pos, struct file *file,
638 void *dirent, filldir_t filldir)
639{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800640 int res;
Al Viro90434762008-07-15 08:54:06 -0400641
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800642 if ((*pos)++ < file->f_pos)
643 return 0;
Al Viro90434762008-07-15 08:54:06 -0400644
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800645 if (unlikely(S_ISLNK(table->mode)))
646 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
647 else
Al Viro90434762008-07-15 08:54:06 -0400648 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400649
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800650 if (res == 0)
651 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400652
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800653 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800654}
655
656static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
657{
Al Viro90434762008-07-15 08:54:06 -0400658 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800659 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400660 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400661 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800662 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800663 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800664 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400665 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800666
Al Viro90434762008-07-15 08:54:06 -0400667 if (IS_ERR(head))
668 return PTR_ERR(head);
669
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800670 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800671
672 ret = 0;
673 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
674 if (filp->f_pos == 0) {
675 if (filldir(dirent, ".", 1, filp->f_pos,
676 inode->i_ino, DT_DIR) < 0)
677 goto out;
678 filp->f_pos++;
679 }
680 if (filp->f_pos == 1) {
681 if (filldir(dirent, "..", 2, filp->f_pos,
682 parent_ino(dentry), DT_DIR) < 0)
683 goto out;
684 filp->f_pos++;
685 }
686 pos = 2;
687
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800688 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800689 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400690 if (ret) {
691 sysctl_head_finish(h);
692 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800693 }
694 }
695 ret = 1;
696out:
697 sysctl_head_finish(head);
698 return ret;
699}
700
Al Viro10556cb2011-06-20 19:28:19 -0400701static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800702{
703 /*
704 * sysctl entries that are not writeable,
705 * are _NOT_ writeable, capabilities or not.
706 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200707 struct ctl_table_header *head;
708 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800709 int error;
710
Miklos Szeredif696a362008-07-31 13:41:58 +0200711 /* Executable files are not allowed under /proc/sys/ */
712 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
713 return -EACCES;
714
715 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400716 if (IS_ERR(head))
717 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800718
Miklos Szeredif696a362008-07-31 13:41:58 +0200719 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400720 if (!table) /* global root - r-xr-xr-x */
721 error = mask & MAY_WRITE ? -EACCES : 0;
722 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400723 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800724
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800725 sysctl_head_finish(head);
726 return error;
727}
728
729static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
730{
731 struct inode *inode = dentry->d_inode;
732 int error;
733
734 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
735 return -EPERM;
736
737 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200738 if (error)
739 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800740
Christoph Hellwig10257742010-06-04 11:30:02 +0200741 if ((attr->ia_valid & ATTR_SIZE) &&
742 attr->ia_size != i_size_read(inode)) {
743 error = vmtruncate(inode, attr->ia_size);
744 if (error)
745 return error;
746 }
747
748 setattr_copy(inode, attr);
749 mark_inode_dirty(inode);
750 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800751}
752
Al Viro90434762008-07-15 08:54:06 -0400753static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
754{
755 struct inode *inode = dentry->d_inode;
756 struct ctl_table_header *head = grab_header(inode);
757 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
758
759 if (IS_ERR(head))
760 return PTR_ERR(head);
761
762 generic_fillattr(inode, stat);
763 if (table)
764 stat->mode = (stat->mode & S_IFMT) | table->mode;
765
766 sysctl_head_finish(head);
767 return 0;
768}
769
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800770static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700771 .open = proc_sys_open,
772 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800773 .read = proc_sys_read,
774 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200775 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400776};
777
778static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700779 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800780 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200781 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800782};
783
Jan Engelhardt03a44822008-02-08 04:21:19 -0800784static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400785 .permission = proc_sys_permission,
786 .setattr = proc_sys_setattr,
787 .getattr = proc_sys_getattr,
788};
789
790static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800791 .lookup = proc_sys_lookup,
792 .permission = proc_sys_permission,
793 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400794 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800795};
796
Al Viro4c9e6872012-06-10 16:03:43 -0400797static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800798{
Al Viro4c9e6872012-06-10 16:03:43 -0400799 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100800 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400801 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
802}
803
Nick Pigginfe15ce42011-01-07 17:49:23 +1100804static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400805{
806 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
807}
808
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800809static int sysctl_is_seen(struct ctl_table_header *p)
810{
811 struct ctl_table_set *set = p->set;
812 int res;
813 spin_lock(&sysctl_lock);
814 if (p->unregistering)
815 res = 0;
816 else if (!set->is_seen)
817 res = 1;
818 else
819 res = set->is_seen(set);
820 spin_unlock(&sysctl_lock);
821 return res;
822}
823
Nick Piggin621e1552011-01-07 17:49:27 +1100824static int proc_sys_compare(const struct dentry *parent,
825 const struct inode *pinode,
826 const struct dentry *dentry, const struct inode *inode,
827 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400828{
Al Virodfef6dc2011-03-08 01:25:28 -0500829 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100830 /* Although proc doesn't have negative dentries, rcu-walk means
831 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500832 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100833 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500834 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100835 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400836 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100837 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400838 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500839 head = rcu_dereference(PROC_I(inode)->sysctl);
840 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800841}
842
Al Virod72f71e2009-02-20 05:58:47 +0000843static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800844 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400845 .d_delete = proc_sys_delete,
846 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800847};
848
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800849static struct ctl_dir *find_subdir(struct ctl_dir *dir,
850 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800851{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800852 struct ctl_table_header *head;
853 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800854
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800855 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800856 if (!entry)
857 return ERR_PTR(-ENOENT);
Eric W. Biederman51f72f42012-01-30 20:09:33 -0800858 if (!S_ISDIR(entry->mode))
859 return ERR_PTR(-ENOTDIR);
860 return container_of(head, struct ctl_dir, header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800861}
862
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800863static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800864 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800865{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800866 struct ctl_table *table;
867 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800868 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800869 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800870
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800871 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
872 sizeof(struct ctl_table)*2 + namelen + 1,
873 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800874 if (!new)
875 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800876
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800877 node = (struct ctl_node *)(new + 1);
878 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800879 new_name = (char *)(table + 2);
880 memcpy(new_name, name, namelen);
881 new_name[namelen] = '\0';
882 table[0].procname = new_name;
883 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800884 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800885
886 return new;
887}
888
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800889/**
890 * get_subdir - find or create a subdir with the specified name.
891 * @dir: Directory to create the subdirectory in
892 * @name: The name of the subdirectory to find or create
893 * @namelen: The length of name
894 *
895 * Takes a directory with an elevated reference count so we know that
896 * if we drop the lock the directory will not go away. Upon success
897 * the reference is moved from @dir to the returned subdirectory.
898 * Upon error an error code is returned and the reference on @dir is
899 * simply dropped.
900 */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800901static struct ctl_dir *get_subdir(struct ctl_dir *dir,
902 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800903{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800904 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800905 struct ctl_dir *subdir, *new = NULL;
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800906 int err;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800907
908 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800909 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800910 if (!IS_ERR(subdir))
911 goto found;
912 if (PTR_ERR(subdir) != -ENOENT)
913 goto failed;
914
915 spin_unlock(&sysctl_lock);
916 new = new_dir(set, name, namelen);
917 spin_lock(&sysctl_lock);
918 subdir = ERR_PTR(-ENOMEM);
919 if (!new)
920 goto failed;
921
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800922 /* Was the subdir added while we dropped the lock? */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800923 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800924 if (!IS_ERR(subdir))
925 goto found;
926 if (PTR_ERR(subdir) != -ENOENT)
927 goto failed;
928
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800929 /* Nope. Use the our freshly made directory entry. */
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800930 err = insert_header(dir, &new->header);
931 subdir = ERR_PTR(err);
932 if (err)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800933 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800934 subdir = new;
935found:
936 subdir->header.nreg++;
937failed:
938 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800939 printk(KERN_ERR "sysctl could not get directory: ");
940 sysctl_print_dir(dir);
941 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800942 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800943 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800944 drop_sysctl_table(&dir->header);
945 if (new)
946 drop_sysctl_table(&new->header);
947 spin_unlock(&sysctl_lock);
948 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800949}
950
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800951static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
952{
953 struct ctl_dir *parent;
954 const char *procname;
955 if (!dir->header.parent)
956 return &set->dir;
957 parent = xlate_dir(set, dir->header.parent);
958 if (IS_ERR(parent))
959 return parent;
960 procname = dir->header.ctl_table[0].procname;
961 return find_subdir(parent, procname, strlen(procname));
962}
963
964static int sysctl_follow_link(struct ctl_table_header **phead,
965 struct ctl_table **pentry, struct nsproxy *namespaces)
966{
967 struct ctl_table_header *head;
968 struct ctl_table_root *root;
969 struct ctl_table_set *set;
970 struct ctl_table *entry;
971 struct ctl_dir *dir;
972 int ret;
973
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800974 ret = 0;
975 spin_lock(&sysctl_lock);
976 root = (*pentry)->data;
977 set = lookup_header_set(root, namespaces);
978 dir = xlate_dir(set, (*phead)->parent);
979 if (IS_ERR(dir))
980 ret = PTR_ERR(dir);
981 else {
982 const char *procname = (*pentry)->procname;
983 head = NULL;
984 entry = find_entry(&head, dir, procname, strlen(procname));
985 ret = -ENOENT;
986 if (entry && use_table(head)) {
987 unuse_table(*phead);
988 *phead = head;
989 *pentry = entry;
990 ret = 0;
991 }
992 }
993
994 spin_unlock(&sysctl_lock);
995 return ret;
996}
997
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800998static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
999{
1000 struct va_format vaf;
1001 va_list args;
1002
1003 va_start(args, fmt);
1004 vaf.fmt = fmt;
1005 vaf.va = &args;
1006
1007 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
1008 path, table->procname, &vaf);
1009
1010 va_end(args);
1011 return -EINVAL;
1012}
1013
1014static int sysctl_check_table(const char *path, struct ctl_table *table)
1015{
1016 int err = 0;
1017 for (; table->procname; table++) {
1018 if (table->child)
1019 err = sysctl_err(path, table, "Not a file");
1020
1021 if ((table->proc_handler == proc_dostring) ||
1022 (table->proc_handler == proc_dointvec) ||
1023 (table->proc_handler == proc_dointvec_minmax) ||
1024 (table->proc_handler == proc_dointvec_jiffies) ||
1025 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1026 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1027 (table->proc_handler == proc_doulongvec_minmax) ||
1028 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1029 if (!table->data)
1030 err = sysctl_err(path, table, "No data");
1031 if (!table->maxlen)
1032 err = sysctl_err(path, table, "No maxlen");
1033 }
1034 if (!table->proc_handler)
1035 err = sysctl_err(path, table, "No proc_handler");
1036
1037 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1038 err = sysctl_err(path, table, "bogus .mode 0%o",
1039 table->mode);
1040 }
1041 return err;
1042}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001043
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001044static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1045 struct ctl_table_root *link_root)
1046{
1047 struct ctl_table *link_table, *entry, *link;
1048 struct ctl_table_header *links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001049 struct ctl_node *node;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001050 char *link_name;
1051 int nr_entries, name_bytes;
1052
1053 name_bytes = 0;
1054 nr_entries = 0;
1055 for (entry = table; entry->procname; entry++) {
1056 nr_entries++;
1057 name_bytes += strlen(entry->procname) + 1;
1058 }
1059
1060 links = kzalloc(sizeof(struct ctl_table_header) +
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001061 sizeof(struct ctl_node)*nr_entries +
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001062 sizeof(struct ctl_table)*(nr_entries + 1) +
1063 name_bytes,
1064 GFP_KERNEL);
1065
1066 if (!links)
1067 return NULL;
1068
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001069 node = (struct ctl_node *)(links + 1);
1070 link_table = (struct ctl_table *)(node + nr_entries);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001071 link_name = (char *)&link_table[nr_entries + 1];
1072
1073 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1074 int len = strlen(entry->procname) + 1;
1075 memcpy(link_name, entry->procname, len);
1076 link->procname = link_name;
1077 link->mode = S_IFLNK|S_IRWXUGO;
1078 link->data = link_root;
1079 link_name += len;
1080 }
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001081 init_header(links, dir->header.root, dir->header.set, node, link_table);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001082 links->nreg = nr_entries;
1083
1084 return links;
1085}
1086
1087static bool get_links(struct ctl_dir *dir,
1088 struct ctl_table *table, struct ctl_table_root *link_root)
1089{
1090 struct ctl_table_header *head;
1091 struct ctl_table *entry, *link;
1092
1093 /* Are there links available for every entry in table? */
1094 for (entry = table; entry->procname; entry++) {
1095 const char *procname = entry->procname;
1096 link = find_entry(&head, dir, procname, strlen(procname));
1097 if (!link)
1098 return false;
1099 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1100 continue;
1101 if (S_ISLNK(link->mode) && (link->data == link_root))
1102 continue;
1103 return false;
1104 }
1105
1106 /* The checks passed. Increase the registration count on the links */
1107 for (entry = table; entry->procname; entry++) {
1108 const char *procname = entry->procname;
1109 link = find_entry(&head, dir, procname, strlen(procname));
1110 head->nreg++;
1111 }
1112 return true;
1113}
1114
1115static int insert_links(struct ctl_table_header *head)
1116{
1117 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1118 struct ctl_dir *core_parent = NULL;
1119 struct ctl_table_header *links;
1120 int err;
1121
1122 if (head->set == root_set)
1123 return 0;
1124
1125 core_parent = xlate_dir(root_set, head->parent);
1126 if (IS_ERR(core_parent))
1127 return 0;
1128
1129 if (get_links(core_parent, head->ctl_table, head->root))
1130 return 0;
1131
1132 core_parent->header.nreg++;
1133 spin_unlock(&sysctl_lock);
1134
1135 links = new_links(core_parent, head->ctl_table, head->root);
1136
1137 spin_lock(&sysctl_lock);
1138 err = -ENOMEM;
1139 if (!links)
1140 goto out;
1141
1142 err = 0;
1143 if (get_links(core_parent, head->ctl_table, head->root)) {
1144 kfree(links);
1145 goto out;
1146 }
1147
1148 err = insert_header(core_parent, links);
1149 if (err)
1150 kfree(links);
1151out:
1152 drop_sysctl_table(&core_parent->header);
1153 return err;
1154}
1155
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001156/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001157 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001158 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001159 * @path: The path to the directory the sysctl table is in.
1160 * @table: the top-level table structure
1161 *
1162 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1163 * array. A completely 0 filled entry terminates the table.
1164 *
1165 * The members of the &struct ctl_table structure are used as follows:
1166 *
1167 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1168 * enter a sysctl file
1169 *
1170 * data - a pointer to data for use by proc_handler
1171 *
1172 * maxlen - the maximum size in bytes of the data
1173 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001174 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001175 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001176 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001177 *
1178 * proc_handler - the text handler routine (described below)
1179 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001180 * extra1, extra2 - extra pointers usable by the proc handler routines
1181 *
1182 * Leaf nodes in the sysctl tree will be represented by a single file
1183 * under /proc; non-leaf nodes will be represented by directories.
1184 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001185 * There must be a proc_handler routine for any terminal nodes.
1186 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001187 *
1188 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1189 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1190 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1191 *
1192 * It is the handler's job to read the input buffer from user memory
1193 * and process it. The handler should return 0 on success.
1194 *
1195 * This routine returns %NULL on a failure to register, and a pointer
1196 * to the table header on success.
1197 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001198struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001199 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001200 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001201{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001202 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001203 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001204 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001205 struct ctl_dir *dir;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001206 struct ctl_table *entry;
1207 struct ctl_node *node;
1208 int nr_entries = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001209
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001210 for (entry = table; entry->procname; entry++)
1211 nr_entries++;
1212
1213 header = kzalloc(sizeof(struct ctl_table_header) +
1214 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001215 if (!header)
1216 return NULL;
1217
Chintan Pandya4d3267c2013-07-02 16:00:49 -07001218 kmemleak_not_leak(header);
1219
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001220 node = (struct ctl_node *)(header + 1);
1221 init_header(header, root, set, node, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001222 if (sysctl_check_table(path, table))
1223 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001224
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001225 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001226 dir = &set->dir;
Eric W. Biederman60f126d2012-01-30 21:23:52 -08001227 /* Reference moved down the diretory tree get_subdir */
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001228 dir->header.nreg++;
1229 spin_unlock(&sysctl_lock);
1230
1231 /* Find the directory for the ctl_table */
1232 for (name = path; name; name = nextname) {
1233 int namelen;
1234 nextname = strchr(name, '/');
1235 if (nextname) {
1236 namelen = nextname - name;
1237 nextname++;
1238 } else {
1239 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001240 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001241 if (namelen == 0)
1242 continue;
1243
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001244 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001245 if (IS_ERR(dir))
1246 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001247 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001248
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001249 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001250 if (insert_header(dir, header))
1251 goto fail_put_dir_locked;
1252
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001253 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001254 spin_unlock(&sysctl_lock);
1255
1256 return header;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001257
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001258fail_put_dir_locked:
1259 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001260 spin_unlock(&sysctl_lock);
1261fail:
1262 kfree(header);
1263 dump_stack();
1264 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001265}
1266
Eric W. Biedermanfea478d2012-01-20 21:47:03 -08001267/**
1268 * register_sysctl - register a sysctl table
1269 * @path: The path to the directory the sysctl table is in.
1270 * @table: the table structure
1271 *
1272 * Register a sysctl table. @table should be a filled in ctl_table
1273 * array. A completely 0 filled entry terminates the table.
1274 *
1275 * See __register_sysctl_table for more details.
1276 */
1277struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1278{
1279 return __register_sysctl_table(&sysctl_table_root.default_set,
1280 path, table);
1281}
1282EXPORT_SYMBOL(register_sysctl);
1283
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001284static char *append_path(const char *path, char *pos, const char *name)
1285{
1286 int namelen;
1287 namelen = strlen(name);
1288 if (((pos - path) + namelen + 2) >= PATH_MAX)
1289 return NULL;
1290 memcpy(pos, name, namelen);
1291 pos[namelen] = '/';
1292 pos[namelen + 1] = '\0';
1293 pos += namelen + 1;
1294 return pos;
1295}
1296
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001297static int count_subheaders(struct ctl_table *table)
1298{
1299 int has_files = 0;
1300 int nr_subheaders = 0;
1301 struct ctl_table *entry;
1302
1303 /* special case: no directory and empty directory */
1304 if (!table || !table->procname)
1305 return 1;
1306
1307 for (entry = table; entry->procname; entry++) {
1308 if (entry->child)
1309 nr_subheaders += count_subheaders(entry->child);
1310 else
1311 has_files = 1;
1312 }
1313 return nr_subheaders + has_files;
1314}
1315
1316static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001317 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001318 struct ctl_table *table)
1319{
1320 struct ctl_table *ctl_table_arg = NULL;
1321 struct ctl_table *entry, *files;
1322 int nr_files = 0;
1323 int nr_dirs = 0;
1324 int err = -ENOMEM;
1325
1326 for (entry = table; entry->procname; entry++) {
1327 if (entry->child)
1328 nr_dirs++;
1329 else
1330 nr_files++;
1331 }
1332
1333 files = table;
1334 /* If there are mixed files and directories we need a new table */
1335 if (nr_dirs && nr_files) {
1336 struct ctl_table *new;
1337 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1338 GFP_KERNEL);
1339 if (!files)
1340 goto out;
1341
1342 ctl_table_arg = files;
1343 for (new = files, entry = table; entry->procname; entry++) {
1344 if (entry->child)
1345 continue;
1346 *new = *entry;
1347 new++;
1348 }
1349 }
1350
1351 /* Register everything except a directory full of subdirectories */
1352 if (nr_files || !nr_dirs) {
1353 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001354 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001355 if (!header) {
1356 kfree(ctl_table_arg);
1357 goto out;
1358 }
1359
1360 /* Remember if we need to free the file table */
1361 header->ctl_table_arg = ctl_table_arg;
1362 **subheader = header;
1363 (*subheader)++;
1364 }
1365
1366 /* Recurse into the subdirectories. */
1367 for (entry = table; entry->procname; entry++) {
1368 char *child_pos;
1369
1370 if (!entry->child)
1371 continue;
1372
1373 err = -ENAMETOOLONG;
1374 child_pos = append_path(path, pos, entry->procname);
1375 if (!child_pos)
1376 goto out;
1377
1378 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001379 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001380 pos[0] = '\0';
1381 if (err)
1382 goto out;
1383 }
1384 err = 0;
1385out:
1386 /* On failure our caller will unregister all registered subheaders */
1387 return err;
1388}
1389
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001390/**
1391 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001392 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001393 * @path: The path to the directory the sysctl table is in.
1394 * @table: the top-level table structure
1395 *
1396 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1397 * array. A completely 0 filled entry terminates the table.
1398 *
1399 * See __register_sysctl_table for more details.
1400 */
1401struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001402 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001403 const struct ctl_path *path, struct ctl_table *table)
1404{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001405 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001406 int nr_subheaders = count_subheaders(table);
1407 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001408 const struct ctl_path *component;
1409 char *new_path, *pos;
1410
1411 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1412 if (!new_path)
1413 return NULL;
1414
1415 pos[0] = '\0';
1416 for (component = path; component->procname; component++) {
1417 pos = append_path(new_path, pos, component->procname);
1418 if (!pos)
1419 goto out;
1420 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001421 while (table->procname && table->child && !table[1].procname) {
1422 pos = append_path(new_path, pos, table->procname);
1423 if (!pos)
1424 goto out;
1425 table = table->child;
1426 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001427 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001428 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001429 if (header)
1430 header->ctl_table_arg = ctl_table_arg;
1431 } else {
1432 header = kzalloc(sizeof(*header) +
1433 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1434 if (!header)
1435 goto out;
1436
1437 subheaders = (struct ctl_table_header **) (header + 1);
1438 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001439 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001440
1441 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001442 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001443 goto err_register_leaves;
1444 }
1445
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001446out:
1447 kfree(new_path);
1448 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001449
1450err_register_leaves:
1451 while (subheader > subheaders) {
1452 struct ctl_table_header *subh = *(--subheader);
1453 struct ctl_table *table = subh->ctl_table_arg;
1454 unregister_sysctl_table(subh);
1455 kfree(table);
1456 }
1457 kfree(header);
1458 header = NULL;
1459 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001460}
1461
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001462/**
1463 * register_sysctl_table_path - register a sysctl table hierarchy
1464 * @path: The path to the directory the sysctl table is in.
1465 * @table: the top-level table structure
1466 *
1467 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1468 * array. A completely 0 filled entry terminates the table.
1469 *
1470 * See __register_sysctl_paths for more details.
1471 */
1472struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1473 struct ctl_table *table)
1474{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001475 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001476 path, table);
1477}
1478EXPORT_SYMBOL(register_sysctl_paths);
1479
1480/**
1481 * register_sysctl_table - register a sysctl table hierarchy
1482 * @table: the top-level table structure
1483 *
1484 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1485 * array. A completely 0 filled entry terminates the table.
1486 *
1487 * See register_sysctl_paths for more details.
1488 */
1489struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1490{
1491 static const struct ctl_path null_path[] = { {} };
1492
1493 return register_sysctl_paths(null_path, table);
1494}
1495EXPORT_SYMBOL(register_sysctl_table);
1496
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001497static void put_links(struct ctl_table_header *header)
1498{
1499 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1500 struct ctl_table_root *root = header->root;
1501 struct ctl_dir *parent = header->parent;
1502 struct ctl_dir *core_parent;
1503 struct ctl_table *entry;
1504
1505 if (header->set == root_set)
1506 return;
1507
1508 core_parent = xlate_dir(root_set, parent);
1509 if (IS_ERR(core_parent))
1510 return;
1511
1512 for (entry = header->ctl_table; entry->procname; entry++) {
1513 struct ctl_table_header *link_head;
1514 struct ctl_table *link;
1515 const char *name = entry->procname;
1516
1517 link = find_entry(&link_head, core_parent, name, strlen(name));
1518 if (link &&
1519 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1520 (S_ISLNK(link->mode) && (link->data == root)))) {
1521 drop_sysctl_table(link_head);
1522 }
1523 else {
1524 printk(KERN_ERR "sysctl link missing during unregister: ");
1525 sysctl_print_dir(parent);
1526 printk(KERN_CONT "/%s\n", name);
1527 }
1528 }
1529}
1530
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001531static void drop_sysctl_table(struct ctl_table_header *header)
1532{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001533 struct ctl_dir *parent = header->parent;
1534
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001535 if (--header->nreg)
1536 return;
1537
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001538 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001539 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001540 if (!--header->count)
1541 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001542
1543 if (parent)
1544 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001545}
1546
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001547/**
1548 * unregister_sysctl_table - unregister a sysctl table hierarchy
1549 * @header: the header returned from register_sysctl_table
1550 *
1551 * Unregisters the sysctl table and all children. proc entries may not
1552 * actually be removed until they are no longer used by anyone.
1553 */
1554void unregister_sysctl_table(struct ctl_table_header * header)
1555{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001556 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001557 might_sleep();
1558
1559 if (header == NULL)
1560 return;
1561
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001562 nr_subheaders = count_subheaders(header->ctl_table_arg);
1563 if (unlikely(nr_subheaders > 1)) {
1564 struct ctl_table_header **subheaders;
1565 int i;
1566
1567 subheaders = (struct ctl_table_header **)(header + 1);
1568 for (i = nr_subheaders -1; i >= 0; i--) {
1569 struct ctl_table_header *subh = subheaders[i];
1570 struct ctl_table *table = subh->ctl_table_arg;
1571 unregister_sysctl_table(subh);
1572 kfree(table);
1573 }
1574 kfree(header);
1575 return;
1576 }
1577
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001578 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001579 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001580 spin_unlock(&sysctl_lock);
1581}
1582EXPORT_SYMBOL(unregister_sysctl_table);
1583
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001584void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001585 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001586 int (*is_seen)(struct ctl_table_set *))
1587{
Dan Carpenter13474402012-01-30 16:40:29 +03001588 memset(set, 0, sizeof(*set));
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001589 set->is_seen = is_seen;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001590 init_header(&set->dir.header, root, set, NULL, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001591}
1592
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001593void retire_sysctl_set(struct ctl_table_set *set)
1594{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001595 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001596}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001597
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001598int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001599{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001600 struct proc_dir_entry *proc_sys_root;
1601
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001602 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001603 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1604 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001605 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001606
1607 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001608}