]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/proc/proc_sysctl.c
On reading sysctl dirs we should return -EISDIR instead of -EINVAL.
[karo-tx-linux.git] / fs / proc / proc_sysctl.c
1 /*
2  * /proc/sys support
3  */
4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/proc_fs.h>
7 #include <linux/security.h>
8 #include <linux/namei.h>
9 #include "internal.h"
10
11 static const struct dentry_operations proc_sys_dentry_operations;
12 static const struct file_operations proc_sys_file_operations;
13 static const struct inode_operations proc_sys_inode_operations;
14 static const struct file_operations proc_sys_dir_file_operations;
15 static const struct inode_operations proc_sys_dir_operations;
16
17 static struct inode *proc_sys_make_inode(struct super_block *sb,
18                 struct ctl_table_header *head, struct ctl_table *table)
19 {
20         struct inode *inode;
21         struct proc_inode *ei;
22
23         inode = new_inode(sb);
24         if (!inode)
25                 goto out;
26
27         inode->i_ino = get_next_ino();
28
29         sysctl_head_get(head);
30         ei = PROC_I(inode);
31         ei->sysctl = head;
32         ei->sysctl_entry = table;
33
34         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
35         inode->i_mode = table->mode;
36         if (!table->child) {
37                 inode->i_mode |= S_IFREG;
38                 inode->i_op = &proc_sys_inode_operations;
39                 inode->i_fop = &proc_sys_file_operations;
40         } else {
41                 inode->i_mode |= S_IFDIR;
42                 inode->i_nlink = 0;
43                 inode->i_op = &proc_sys_dir_operations;
44                 inode->i_fop = &proc_sys_dir_file_operations;
45         }
46 out:
47         return inode;
48 }
49
50 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
51 {
52         for ( ; p->procname; p++) {
53                 if (strlen(p->procname) != name->len)
54                         continue;
55
56                 if (memcmp(p->procname, name->name, name->len) != 0)
57                         continue;
58
59                 /* I have a match */
60                 return p;
61         }
62         return NULL;
63 }
64
65 static struct ctl_table_header *grab_header(struct inode *inode)
66 {
67         if (PROC_I(inode)->sysctl)
68                 return sysctl_head_grab(PROC_I(inode)->sysctl);
69         else
70                 return sysctl_head_next(NULL);
71 }
72
73 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
74                                         struct nameidata *nd)
75 {
76         struct ctl_table_header *head = grab_header(dir);
77         struct ctl_table *table = PROC_I(dir)->sysctl_entry;
78         struct ctl_table_header *h = NULL;
79         struct qstr *name = &dentry->d_name;
80         struct ctl_table *p;
81         struct inode *inode;
82         struct dentry *err = ERR_PTR(-ENOENT);
83
84         if (IS_ERR(head))
85                 return ERR_CAST(head);
86
87         if (table && !table->child) {
88                 WARN_ON(1);
89                 goto out;
90         }
91
92         table = table ? table->child : head->ctl_table;
93
94         p = find_in_table(table, name);
95         if (!p) {
96                 for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
97                         if (h->attached_to != table)
98                                 continue;
99                         p = find_in_table(h->attached_by, name);
100                         if (p)
101                                 break;
102                 }
103         }
104
105         if (!p)
106                 goto out;
107
108         err = ERR_PTR(-ENOMEM);
109         inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
110         if (h)
111                 sysctl_head_finish(h);
112
113         if (!inode)
114                 goto out;
115
116         err = NULL;
117         d_set_d_op(dentry, &proc_sys_dentry_operations);
118         d_add(dentry, inode);
119
120 out:
121         sysctl_head_finish(head);
122         return err;
123 }
124
125 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
126                 size_t count, loff_t *ppos, int write)
127 {
128         struct inode *inode = filp->f_path.dentry->d_inode;
129         struct ctl_table_header *head = grab_header(inode);
130         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
131         ssize_t error;
132         size_t res;
133
134         if (IS_ERR(head))
135                 return PTR_ERR(head);
136
137         /*
138          * At this point we know that the sysctl was not unregistered
139          * and won't be until we finish.
140          */
141         error = -EPERM;
142         if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
143                 goto out;
144
145         /* if that can happen at all, it should be -EINVAL, not -EISDIR */
146         error = -EINVAL;
147         if (!table->proc_handler)
148                 goto out;
149
150         /* careful: calling conventions are nasty here */
151         res = count;
152         error = table->proc_handler(table, write, buf, &res, ppos);
153         if (!error)
154                 error = res;
155 out:
156         sysctl_head_finish(head);
157
158         return error;
159 }
160
161 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
162                                 size_t count, loff_t *ppos)
163 {
164         return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
165 }
166
167 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
168                                 size_t count, loff_t *ppos)
169 {
170         return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
171 }
172
173
174 static int proc_sys_fill_cache(struct file *filp, void *dirent,
175                                 filldir_t filldir,
176                                 struct ctl_table_header *head,
177                                 struct ctl_table *table)
178 {
179         struct dentry *child, *dir = filp->f_path.dentry;
180         struct inode *inode;
181         struct qstr qname;
182         ino_t ino = 0;
183         unsigned type = DT_UNKNOWN;
184
185         qname.name = table->procname;
186         qname.len  = strlen(table->procname);
187         qname.hash = full_name_hash(qname.name, qname.len);
188
189         child = d_lookup(dir, &qname);
190         if (!child) {
191                 child = d_alloc(dir, &qname);
192                 if (child) {
193                         inode = proc_sys_make_inode(dir->d_sb, head, table);
194                         if (!inode) {
195                                 dput(child);
196                                 return -ENOMEM;
197                         } else {
198                                 d_set_d_op(child, &proc_sys_dentry_operations);
199                                 d_add(child, inode);
200                         }
201                 } else {
202                         return -ENOMEM;
203                 }
204         }
205         inode = child->d_inode;
206         ino  = inode->i_ino;
207         type = inode->i_mode >> 12;
208         dput(child);
209         return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
210 }
211
212 static int scan(struct ctl_table_header *head, ctl_table *table,
213                 unsigned long *pos, struct file *file,
214                 void *dirent, filldir_t filldir)
215 {
216
217         for (; table->procname; table++, (*pos)++) {
218                 int res;
219
220                 if (*pos < file->f_pos)
221                         continue;
222
223                 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
224                 if (res)
225                         return res;
226
227                 file->f_pos = *pos + 1;
228         }
229         return 0;
230 }
231
232 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
233 {
234         struct dentry *dentry = filp->f_path.dentry;
235         struct inode *inode = dentry->d_inode;
236         struct ctl_table_header *head = grab_header(inode);
237         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
238         struct ctl_table_header *h = NULL;
239         unsigned long pos;
240         int ret = -EINVAL;
241
242         if (IS_ERR(head))
243                 return PTR_ERR(head);
244
245         if (table && !table->child) {
246                 WARN_ON(1);
247                 goto out;
248         }
249
250         table = table ? table->child : head->ctl_table;
251
252         ret = 0;
253         /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
254         if (filp->f_pos == 0) {
255                 if (filldir(dirent, ".", 1, filp->f_pos,
256                                 inode->i_ino, DT_DIR) < 0)
257                         goto out;
258                 filp->f_pos++;
259         }
260         if (filp->f_pos == 1) {
261                 if (filldir(dirent, "..", 2, filp->f_pos,
262                                 parent_ino(dentry), DT_DIR) < 0)
263                         goto out;
264                 filp->f_pos++;
265         }
266         pos = 2;
267
268         ret = scan(head, table, &pos, filp, dirent, filldir);
269         if (ret)
270                 goto out;
271
272         for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
273                 if (h->attached_to != table)
274                         continue;
275                 ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
276                 if (ret) {
277                         sysctl_head_finish(h);
278                         break;
279                 }
280         }
281         ret = 1;
282 out:
283         sysctl_head_finish(head);
284         return ret;
285 }
286
287 static int proc_sys_permission(struct inode *inode, int mask)
288 {
289         /*
290          * sysctl entries that are not writeable,
291          * are _NOT_ writeable, capabilities or not.
292          */
293         struct ctl_table_header *head;
294         struct ctl_table *table;
295         int error;
296
297         /* Executable files are not allowed under /proc/sys/ */
298         if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
299                 return -EACCES;
300
301         head = grab_header(inode);
302         if (IS_ERR(head))
303                 return PTR_ERR(head);
304
305         table = PROC_I(inode)->sysctl_entry;
306         if (!table) /* global root - r-xr-xr-x */
307                 error = mask & MAY_WRITE ? -EACCES : 0;
308         else /* Use the permissions on the sysctl table entry */
309                 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
310
311         sysctl_head_finish(head);
312         return error;
313 }
314
315 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
316 {
317         struct inode *inode = dentry->d_inode;
318         int error;
319
320         if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
321                 return -EPERM;
322
323         error = inode_change_ok(inode, attr);
324         if (error)
325                 return error;
326
327         if ((attr->ia_valid & ATTR_SIZE) &&
328             attr->ia_size != i_size_read(inode)) {
329                 error = vmtruncate(inode, attr->ia_size);
330                 if (error)
331                         return error;
332         }
333
334         setattr_copy(inode, attr);
335         mark_inode_dirty(inode);
336         return 0;
337 }
338
339 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
340 {
341         struct inode *inode = dentry->d_inode;
342         struct ctl_table_header *head = grab_header(inode);
343         struct ctl_table *table = PROC_I(inode)->sysctl_entry;
344
345         if (IS_ERR(head))
346                 return PTR_ERR(head);
347
348         generic_fillattr(inode, stat);
349         if (table)
350                 stat->mode = (stat->mode & S_IFMT) | table->mode;
351
352         sysctl_head_finish(head);
353         return 0;
354 }
355
356 static const struct file_operations proc_sys_file_operations = {
357         .read           = proc_sys_read,
358         .write          = proc_sys_write,
359         .llseek         = default_llseek,
360 };
361
362 static const struct file_operations proc_sys_dir_file_operations = {
363         .read           = generic_read_dir,
364         .readdir        = proc_sys_readdir,
365         .llseek         = generic_file_llseek,
366 };
367
368 static const struct inode_operations proc_sys_inode_operations = {
369         .permission     = proc_sys_permission,
370         .setattr        = proc_sys_setattr,
371         .getattr        = proc_sys_getattr,
372 };
373
374 static const struct inode_operations proc_sys_dir_operations = {
375         .lookup         = proc_sys_lookup,
376         .permission     = proc_sys_permission,
377         .setattr        = proc_sys_setattr,
378         .getattr        = proc_sys_getattr,
379 };
380
381 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
382 {
383         if (nd->flags & LOOKUP_RCU)
384                 return -ECHILD;
385         return !PROC_I(dentry->d_inode)->sysctl->unregistering;
386 }
387
388 static int proc_sys_delete(const struct dentry *dentry)
389 {
390         return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
391 }
392
393 static int proc_sys_compare(const struct dentry *parent,
394                 const struct inode *pinode,
395                 const struct dentry *dentry, const struct inode *inode,
396                 unsigned int len, const char *str, const struct qstr *name)
397 {
398         struct ctl_table_header *head;
399         /* Although proc doesn't have negative dentries, rcu-walk means
400          * that inode here can be NULL */
401         /* AV: can it, indeed? */
402         if (!inode)
403                 return 1;
404         if (name->len != len)
405                 return 1;
406         if (memcmp(name->name, str, len))
407                 return 1;
408         head = rcu_dereference(PROC_I(inode)->sysctl);
409         return !head || !sysctl_is_seen(head);
410 }
411
412 static const struct dentry_operations proc_sys_dentry_operations = {
413         .d_revalidate   = proc_sys_revalidate,
414         .d_delete       = proc_sys_delete,
415         .d_compare      = proc_sys_compare,
416 };
417
418 int __init proc_sys_init(void)
419 {
420         struct proc_dir_entry *proc_sys_root;
421
422         proc_sys_root = proc_mkdir("sys", NULL);
423         proc_sys_root->proc_iops = &proc_sys_dir_operations;
424         proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
425         proc_sys_root->nlink = 0;
426         return 0;
427 }