blob: 09a1f92a34ef2fce52b07cf394d040708e86a22b [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>
6#include <linux/proc_fs.h>
7#include <linux/security.h>
Nick Piggin34286d62011-01-07 17:49:57 +11008#include <linux/namei.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08009#include "internal.h"
10
Al Virod72f71e2009-02-20 05:58:47 +000011static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080012static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080013static const struct inode_operations proc_sys_inode_operations;
Al Viro9043476f2008-07-15 08:54:06 -040014static const struct file_operations proc_sys_dir_file_operations;
15static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080016
Al Viro9043476f2008-07-15 08:54:06 -040017static struct inode *proc_sys_make_inode(struct super_block *sb,
18 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -080019{
20 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -040021 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080022
Al Viro9043476f2008-07-15 08:54:06 -040023 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -080024 if (!inode)
25 goto out;
26
Christoph Hellwig85fe4022010-10-23 11:19:54 -040027 inode->i_ino = get_next_ino();
28
Al Viro9043476f2008-07-15 08:54:06 -040029 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -080030 ei = PROC_I(inode);
Al Viro9043476f2008-07-15 08:54:06 -040031 ei->sysctl = head;
32 ei->sysctl_entry = table;
33
Eric W. Biederman77b14db2007-02-14 00:34:12 -080034 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman86a71dbd2007-02-14 00:34:16 -080035 inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
Al Viro9043476f2008-07-15 08:54:06 -040036 inode->i_mode = table->mode;
37 if (!table->child) {
38 inode->i_mode |= S_IFREG;
39 inode->i_op = &proc_sys_inode_operations;
40 inode->i_fop = &proc_sys_file_operations;
41 } else {
42 inode->i_mode |= S_IFDIR;
43 inode->i_nlink = 0;
44 inode->i_op = &proc_sys_dir_operations;
45 inode->i_fop = &proc_sys_dir_file_operations;
46 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -080047out:
48 return inode;
49}
50
Al Viro9043476f2008-07-15 08:54:06 -040051static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
Eric W. Biederman77b14db2007-02-14 00:34:12 -080052{
53 int len;
Eric W. Biederman2315ffa2009-04-03 03:18:02 -070054 for ( ; p->procname; p++) {
Eric W. Biederman77b14db2007-02-14 00:34:12 -080055
Al Viro9043476f2008-07-15 08:54:06 -040056 if (!p->procname)
Eric W. Biederman77b14db2007-02-14 00:34:12 -080057 continue;
58
Al Viro9043476f2008-07-15 08:54:06 -040059 len = strlen(p->procname);
Eric W. Biederman77b14db2007-02-14 00:34:12 -080060 if (len != name->len)
61 continue;
62
Al Viro9043476f2008-07-15 08:54:06 -040063 if (memcmp(p->procname, name->name, len) != 0)
Eric W. Biederman77b14db2007-02-14 00:34:12 -080064 continue;
65
66 /* I have a match */
Al Viro9043476f2008-07-15 08:54:06 -040067 return p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080068 }
69 return NULL;
70}
71
Adrian Bunk81324362008-10-03 00:33:54 +040072static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -080073{
Al Viro9043476f2008-07-15 08:54:06 -040074 if (PROC_I(inode)->sysctl)
75 return sysctl_head_grab(PROC_I(inode)->sysctl);
76 else
77 return sysctl_head_next(NULL);
Eric W. Biederman77b14db2007-02-14 00:34:12 -080078}
79
80static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
81 struct nameidata *nd)
82{
Al Viro9043476f2008-07-15 08:54:06 -040083 struct ctl_table_header *head = grab_header(dir);
84 struct ctl_table *table = PROC_I(dir)->sysctl_entry;
85 struct ctl_table_header *h = NULL;
86 struct qstr *name = &dentry->d_name;
87 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080088 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -040089 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman77b14db2007-02-14 00:34:12 -080090
Al Viro9043476f2008-07-15 08:54:06 -040091 if (IS_ERR(head))
92 return ERR_CAST(head);
93
94 if (table && !table->child) {
95 WARN_ON(1);
96 goto out;
97 }
98
99 table = table ? table->child : head->ctl_table;
100
101 p = find_in_table(table, name);
102 if (!p) {
103 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
104 if (h->attached_to != table)
105 continue;
106 p = find_in_table(h->attached_by, name);
107 if (p)
108 break;
109 }
110 }
111
112 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800113 goto out;
114
115 err = ERR_PTR(-ENOMEM);
Al Viro9043476f2008-07-15 08:54:06 -0400116 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
117 if (h)
118 sysctl_head_finish(h);
119
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800120 if (!inode)
121 goto out;
122
123 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100124 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800125 d_add(dentry, inode);
126
127out:
128 sysctl_head_finish(head);
129 return err;
130}
131
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700132static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
133 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800134{
Al Viro9043476f2008-07-15 08:54:06 -0400135 struct inode *inode = filp->f_path.dentry->d_inode;
136 struct ctl_table_header *head = grab_header(inode);
137 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100138 ssize_t error;
139 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800140
Al Viro9043476f2008-07-15 08:54:06 -0400141 if (IS_ERR(head))
142 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800143
144 /*
145 * At this point we know that the sysctl was not unregistered
146 * and won't be until we finish.
147 */
148 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700149 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800150 goto out;
151
Al Viro9043476f2008-07-15 08:54:06 -0400152 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
153 error = -EINVAL;
154 if (!table->proc_handler)
155 goto out;
156
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800157 /* careful: calling conventions are nasty here */
158 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700159 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800160 if (!error)
161 error = res;
162out:
163 sysctl_head_finish(head);
164
165 return error;
166}
167
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700168static ssize_t proc_sys_read(struct file *filp, char __user *buf,
169 size_t count, loff_t *ppos)
170{
171 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
172}
173
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800174static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
175 size_t count, loff_t *ppos)
176{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700177 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800178}
179
180
181static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro9043476f2008-07-15 08:54:06 -0400182 filldir_t filldir,
183 struct ctl_table_header *head,
184 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800185{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800186 struct dentry *child, *dir = filp->f_path.dentry;
187 struct inode *inode;
188 struct qstr qname;
189 ino_t ino = 0;
190 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800191
192 qname.name = table->procname;
193 qname.len = strlen(table->procname);
194 qname.hash = full_name_hash(qname.name, qname.len);
195
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800196 child = d_lookup(dir, &qname);
197 if (!child) {
Al Viro9043476f2008-07-15 08:54:06 -0400198 child = d_alloc(dir, &qname);
199 if (child) {
200 inode = proc_sys_make_inode(dir->d_sb, head, table);
201 if (!inode) {
202 dput(child);
203 return -ENOMEM;
204 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100205 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro9043476f2008-07-15 08:54:06 -0400206 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800207 }
Al Viro9043476f2008-07-15 08:54:06 -0400208 } else {
209 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800210 }
211 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800212 inode = child->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400213 ino = inode->i_ino;
214 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800215 dput(child);
Al Viro9043476f2008-07-15 08:54:06 -0400216 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
217}
218
219static int scan(struct ctl_table_header *head, ctl_table *table,
220 unsigned long *pos, struct file *file,
221 void *dirent, filldir_t filldir)
222{
223
Eric W. Biederman2315ffa2009-04-03 03:18:02 -0700224 for (; table->procname; table++, (*pos)++) {
Al Viro9043476f2008-07-15 08:54:06 -0400225 int res;
226
227 /* Can't do anything without a proc name */
228 if (!table->procname)
229 continue;
230
231 if (*pos < file->f_pos)
232 continue;
233
234 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
235 if (res)
236 return res;
237
238 file->f_pos = *pos + 1;
239 }
240 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800241}
242
243static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
244{
Al Viro9043476f2008-07-15 08:54:06 -0400245 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800246 struct inode *inode = dentry->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400247 struct ctl_table_header *head = grab_header(inode);
248 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
249 struct ctl_table_header *h = NULL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800250 unsigned long pos;
Al Viro9043476f2008-07-15 08:54:06 -0400251 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800252
Al Viro9043476f2008-07-15 08:54:06 -0400253 if (IS_ERR(head))
254 return PTR_ERR(head);
255
256 if (table && !table->child) {
257 WARN_ON(1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800258 goto out;
Al Viro9043476f2008-07-15 08:54:06 -0400259 }
260
261 table = table ? table->child : head->ctl_table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800262
263 ret = 0;
264 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
265 if (filp->f_pos == 0) {
266 if (filldir(dirent, ".", 1, filp->f_pos,
267 inode->i_ino, DT_DIR) < 0)
268 goto out;
269 filp->f_pos++;
270 }
271 if (filp->f_pos == 1) {
272 if (filldir(dirent, "..", 2, filp->f_pos,
273 parent_ino(dentry), DT_DIR) < 0)
274 goto out;
275 filp->f_pos++;
276 }
277 pos = 2;
278
Al Viro9043476f2008-07-15 08:54:06 -0400279 ret = scan(head, table, &pos, filp, dirent, filldir);
280 if (ret)
281 goto out;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800282
Al Viro9043476f2008-07-15 08:54:06 -0400283 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
284 if (h->attached_to != table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800285 continue;
Al Viro9043476f2008-07-15 08:54:06 -0400286 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
287 if (ret) {
288 sysctl_head_finish(h);
289 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800290 }
291 }
292 ret = 1;
293out:
294 sysctl_head_finish(head);
295 return ret;
296}
297
Nick Pigginb74c79e2011-01-07 17:49:58 +1100298static int proc_sys_permission(struct inode *inode, int mask,unsigned int flags)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800299{
300 /*
301 * sysctl entries that are not writeable,
302 * are _NOT_ writeable, capabilities or not.
303 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200304 struct ctl_table_header *head;
305 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800306 int error;
307
Nick Pigginb74c79e2011-01-07 17:49:58 +1100308 if (flags & IPERM_FLAG_RCU)
309 return -ECHILD;
310
Miklos Szeredif696a362008-07-31 13:41:58 +0200311 /* Executable files are not allowed under /proc/sys/ */
312 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
313 return -EACCES;
314
315 head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400316 if (IS_ERR(head))
317 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800318
Miklos Szeredif696a362008-07-31 13:41:58 +0200319 table = PROC_I(inode)->sysctl_entry;
Al Viro9043476f2008-07-15 08:54:06 -0400320 if (!table) /* global root - r-xr-xr-x */
321 error = mask & MAY_WRITE ? -EACCES : 0;
322 else /* Use the permissions on the sysctl table entry */
323 error = sysctl_perm(head->root, table, mask);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800324
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800325 sysctl_head_finish(head);
326 return error;
327}
328
329static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
330{
331 struct inode *inode = dentry->d_inode;
332 int error;
333
334 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
335 return -EPERM;
336
337 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200338 if (error)
339 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800340
Christoph Hellwig10257742010-06-04 11:30:02 +0200341 if ((attr->ia_valid & ATTR_SIZE) &&
342 attr->ia_size != i_size_read(inode)) {
343 error = vmtruncate(inode, attr->ia_size);
344 if (error)
345 return error;
346 }
347
348 setattr_copy(inode, attr);
349 mark_inode_dirty(inode);
350 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800351}
352
Al Viro9043476f2008-07-15 08:54:06 -0400353static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
354{
355 struct inode *inode = dentry->d_inode;
356 struct ctl_table_header *head = grab_header(inode);
357 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
358
359 if (IS_ERR(head))
360 return PTR_ERR(head);
361
362 generic_fillattr(inode, stat);
363 if (table)
364 stat->mode = (stat->mode & S_IFMT) | table->mode;
365
366 sysctl_head_finish(head);
367 return 0;
368}
369
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800370static const struct file_operations proc_sys_file_operations = {
371 .read = proc_sys_read,
372 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200373 .llseek = default_llseek,
Al Viro9043476f2008-07-15 08:54:06 -0400374};
375
376static const struct file_operations proc_sys_dir_file_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800377 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200378 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800379};
380
Jan Engelhardt03a44822008-02-08 04:21:19 -0800381static const struct inode_operations proc_sys_inode_operations = {
Al Viro9043476f2008-07-15 08:54:06 -0400382 .permission = proc_sys_permission,
383 .setattr = proc_sys_setattr,
384 .getattr = proc_sys_getattr,
385};
386
387static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800388 .lookup = proc_sys_lookup,
389 .permission = proc_sys_permission,
390 .setattr = proc_sys_setattr,
Al Viro9043476f2008-07-15 08:54:06 -0400391 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800392};
393
394static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
395{
Nick Piggin34286d62011-01-07 17:49:57 +1100396 if (nd->flags & LOOKUP_RCU)
397 return -ECHILD;
Al Viro9043476f2008-07-15 08:54:06 -0400398 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
399}
400
Nick Pigginfe15ce42011-01-07 17:49:23 +1100401static int proc_sys_delete(const struct dentry *dentry)
Al Viro9043476f2008-07-15 08:54:06 -0400402{
403 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
404}
405
Nick Piggin621e1552011-01-07 17:49:27 +1100406static int proc_sys_compare(const struct dentry *parent,
407 const struct inode *pinode,
408 const struct dentry *dentry, const struct inode *inode,
409 unsigned int len, const char *str, const struct qstr *name)
Al Viro9043476f2008-07-15 08:54:06 -0400410{
Nick Piggin31e6b012011-01-07 17:49:52 +1100411 /* Although proc doesn't have negative dentries, rcu-walk means
412 * that inode here can be NULL */
413 if (!inode)
414 return 0;
Nick Piggin621e1552011-01-07 17:49:27 +1100415 if (name->len != len)
Al Viro9043476f2008-07-15 08:54:06 -0400416 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100417 if (memcmp(name->name, str, len))
Al Viro9043476f2008-07-15 08:54:06 -0400418 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100419 return !sysctl_is_seen(PROC_I(inode)->sysctl);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800420}
421
Al Virod72f71e2009-02-20 05:58:47 +0000422static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800423 .d_revalidate = proc_sys_revalidate,
Al Viro9043476f2008-07-15 08:54:06 -0400424 .d_delete = proc_sys_delete,
425 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800426};
427
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400428int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800429{
Alexey Dobriyane1675232008-10-03 00:23:32 +0400430 struct proc_dir_entry *proc_sys_root;
431
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800432 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro9043476f2008-07-15 08:54:06 -0400433 proc_sys_root->proc_iops = &proc_sys_dir_operations;
434 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800435 proc_sys_root->nlink = 0;
436 return 0;
437}