aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc
diff options
context:
space:
mode:
Diffstat (limited to 'fs/proc')
-rw-r--r--fs/proc/Makefile2
-rw-r--r--fs/proc/generic.c2
-rw-r--r--fs/proc/inode.c1
-rw-r--r--fs/proc/internal.h2
-rw-r--r--fs/proc/proc_sysctl.c479
-rw-r--r--fs/proc/root.c14
6 files changed, 487 insertions, 13 deletions
diff --git a/fs/proc/Makefile b/fs/proc/Makefile
index f6c776272572..a6b3a8f878f0 100644
--- a/fs/proc/Makefile
+++ b/fs/proc/Makefile
@@ -8,7 +8,7 @@ proc-y := nommu.o task_nommu.o
8proc-$(CONFIG_MMU) := mmu.o task_mmu.o 8proc-$(CONFIG_MMU) := mmu.o task_mmu.o
9 9
10proc-y += inode.o root.o base.o generic.o array.o \ 10proc-y += inode.o root.o base.o generic.o array.o \
11 proc_tty.o proc_misc.o 11 proc_tty.o proc_misc.o proc_sysctl.o
12 12
13proc-$(CONFIG_PROC_KCORE) += kcore.o 13proc-$(CONFIG_PROC_KCORE) += kcore.o
14proc-$(CONFIG_PROC_VMCORE) += vmcore.o 14proc-$(CONFIG_PROC_VMCORE) += vmcore.o
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 0cdc00d9d97e..775fb21294d8 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -32,7 +32,7 @@ static loff_t proc_file_lseek(struct file *, loff_t, int);
32 32
33DEFINE_SPINLOCK(proc_subdir_lock); 33DEFINE_SPINLOCK(proc_subdir_lock);
34 34
35int proc_match(int len, const char *name, struct proc_dir_entry *de) 35static int proc_match(int len, const char *name, struct proc_dir_entry *de)
36{ 36{
37 if (de->namelen != len) 37 if (de->namelen != len)
38 return 0; 38 return 0;
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index f6722be37dde..c372eb151a3a 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -161,6 +161,7 @@ struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
161 if (!inode) 161 if (!inode)
162 goto out_ino; 162 goto out_ino;
163 163
164 PROC_I(inode)->fd = 0;
164 PROC_I(inode)->pde = de; 165 PROC_I(inode)->pde = de;
165 if (de) { 166 if (de) {
166 if (de->mode) { 167 if (de->mode) {
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 277dcd66ebe2..c932aa65e198 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -11,6 +11,8 @@
11 11
12#include <linux/proc_fs.h> 12#include <linux/proc_fs.h>
13 13
14extern int proc_sys_init(void);
15
14struct vmalloc_info { 16struct vmalloc_info {
15 unsigned long used; 17 unsigned long used;
16 unsigned long largest_chunk; 18 unsigned long largest_chunk;
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
new file mode 100644
index 000000000000..20e8cbb34364
--- /dev/null
+++ b/fs/proc/proc_sysctl.c
@@ -0,0 +1,479 @@
1/*
2 * /proc/sys support
3 */
4
5#include <linux/sysctl.h>
6#include <linux/proc_fs.h>
7#include <linux/security.h>
8#include "internal.h"
9
10static struct dentry_operations proc_sys_dentry_operations;
11static const struct file_operations proc_sys_file_operations;
12static struct inode_operations proc_sys_inode_operations;
13
14static void proc_sys_refresh_inode(struct inode *inode, struct ctl_table *table)
15{
16 /* Refresh the cached information bits in the inode */
17 if (table) {
18 inode->i_uid = 0;
19 inode->i_gid = 0;
20 inode->i_mode = table->mode;
21 if (table->proc_handler) {
22 inode->i_mode |= S_IFREG;
23 inode->i_nlink = 1;
24 } else {
25 inode->i_mode |= S_IFDIR;
26 inode->i_nlink = 0; /* It is too hard to figure out */
27 }
28 }
29}
30
31static struct inode *proc_sys_make_inode(struct inode *dir, struct ctl_table *table)
32{
33 struct inode *inode;
34 struct proc_inode *dir_ei, *ei;
35 int depth;
36
37 inode = new_inode(dir->i_sb);
38 if (!inode)
39 goto out;
40
41 /* A directory is always one deeper than it's parent */
42 dir_ei = PROC_I(dir);
43 depth = dir_ei->fd + 1;
44
45 ei = PROC_I(inode);
46 ei->fd = depth;
47 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
48 inode->i_op = &proc_sys_inode_operations;
49 inode->i_fop = &proc_sys_file_operations;
50 inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
51 proc_sys_refresh_inode(inode, table);
52out:
53 return inode;
54}
55
56static struct dentry *proc_sys_ancestor(struct dentry *dentry, int depth)
57{
58 for (;;) {
59 struct proc_inode *ei;
60
61 ei = PROC_I(dentry->d_inode);
62 if (ei->fd == depth)
63 break; /* found */
64
65 dentry = dentry->d_parent;
66 }
67 return dentry;
68}
69
70static struct ctl_table *proc_sys_lookup_table_one(struct ctl_table *table,
71 struct qstr *name)
72{
73 int len;
74 for ( ; table->ctl_name || table->procname; table++) {
75
76 if (!table->procname)
77 continue;
78
79 len = strlen(table->procname);
80 if (len != name->len)
81 continue;
82
83 if (memcmp(table->procname, name->name, len) != 0)
84 continue;
85
86 /* I have a match */
87 return table;
88 }
89 return NULL;
90}
91
92static struct ctl_table *proc_sys_lookup_table(struct dentry *dentry,
93 struct ctl_table *table)
94{
95 struct dentry *ancestor;
96 struct proc_inode *ei;
97 int depth, i;
98
99 ei = PROC_I(dentry->d_inode);
100 depth = ei->fd;
101
102 if (depth == 0)
103 return table;
104
105 for (i = 1; table && (i <= depth); i++) {
106 ancestor = proc_sys_ancestor(dentry, i);
107 table = proc_sys_lookup_table_one(table, &ancestor->d_name);
108 if (table)
109 table = table->child;
110 }
111 return table;
112
113}
114static struct ctl_table *proc_sys_lookup_entry(struct dentry *dparent,
115 struct qstr *name,
116 struct ctl_table *table)
117{
118 table = proc_sys_lookup_table(dparent, table);
119 if (table)
120 table = proc_sys_lookup_table_one(table, name);
121 return table;
122}
123
124static struct ctl_table *do_proc_sys_lookup(struct dentry *parent,
125 struct qstr *name,
126 struct ctl_table_header **ptr)
127{
128 struct ctl_table_header *head;
129 struct ctl_table *table = NULL;
130
131 for (head = sysctl_head_next(NULL); head;
132 head = sysctl_head_next(head)) {
133 table = proc_sys_lookup_entry(parent, name, head->ctl_table);
134 if (table)
135 break;
136 }
137 *ptr = head;
138 return table;
139}
140
141static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
142 struct nameidata *nd)
143{
144 struct ctl_table_header *head;
145 struct inode *inode;
146 struct dentry *err;
147 struct ctl_table *table;
148
149 err = ERR_PTR(-ENOENT);
150 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
151 if (!table)
152 goto out;
153
154 err = ERR_PTR(-ENOMEM);
155 inode = proc_sys_make_inode(dir, table);
156 if (!inode)
157 goto out;
158
159 err = NULL;
160 dentry->d_op = &proc_sys_dentry_operations;
161 d_add(dentry, inode);
162
163out:
164 sysctl_head_finish(head);
165 return err;
166}
167
168static ssize_t proc_sys_read(struct file *filp, char __user *buf,
169 size_t count, loff_t *ppos)
170{
171 struct dentry *dentry = filp->f_dentry;
172 struct ctl_table_header *head;
173 struct ctl_table *table;
174 ssize_t error, res;
175
176 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
177 /* Has the sysctl entry disappeared on us? */
178 error = -ENOENT;
179 if (!table)
180 goto out;
181
182 /* Has the sysctl entry been replaced by a directory? */
183 error = -EISDIR;
184 if (!table->proc_handler)
185 goto out;
186
187 /*
188 * At this point we know that the sysctl was not unregistered
189 * and won't be until we finish.
190 */
191 error = -EPERM;
192 if (sysctl_perm(table, MAY_READ))
193 goto out;
194
195 /* careful: calling conventions are nasty here */
196 res = count;
197 error = table->proc_handler(table, 0, filp, buf, &res, ppos);
198 if (!error)
199 error = res;
200out:
201 sysctl_head_finish(head);
202
203 return error;
204}
205
206static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
207 size_t count, loff_t *ppos)
208{
209 struct dentry *dentry = filp->f_dentry;
210 struct ctl_table_header *head;
211 struct ctl_table *table;
212 ssize_t error, res;
213
214 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
215 /* Has the sysctl entry disappeared on us? */
216 error = -ENOENT;
217 if (!table)
218 goto out;
219
220 /* Has the sysctl entry been replaced by a directory? */
221 error = -EISDIR;
222 if (!table->proc_handler)
223 goto out;
224
225 /*
226 * At this point we know that the sysctl was not unregistered
227 * and won't be until we finish.
228 */
229 error = -EPERM;
230 if (sysctl_perm(table, MAY_WRITE))
231 goto out;
232
233 /* careful: calling conventions are nasty here */
234 res = count;
235 error = table->proc_handler(table, 1, filp, (char __user *)buf,
236 &res, ppos);
237 if (!error)
238 error = res;
239out:
240 sysctl_head_finish(head);
241
242 return error;
243}
244
245
246static int proc_sys_fill_cache(struct file *filp, void *dirent,
247 filldir_t filldir, struct ctl_table *table)
248{
249 struct ctl_table_header *head;
250 struct ctl_table *child_table = NULL;
251 struct dentry *child, *dir = filp->f_path.dentry;
252 struct inode *inode;
253 struct qstr qname;
254 ino_t ino = 0;
255 unsigned type = DT_UNKNOWN;
256 int ret;
257
258 qname.name = table->procname;
259 qname.len = strlen(table->procname);
260 qname.hash = full_name_hash(qname.name, qname.len);
261
262 /* Suppress duplicates.
263 * Only fill a directory entry if it is the value that
264 * an ordinary lookup of that name returns. Hide all
265 * others.
266 *
267 * If we ever cache this translation in the dcache
268 * I should do a dcache lookup first. But for now
269 * it is just simpler not to.
270 */
271 ret = 0;
272 child_table = do_proc_sys_lookup(dir, &qname, &head);
273 sysctl_head_finish(head);
274 if (child_table != table)
275 return 0;
276
277 child = d_lookup(dir, &qname);
278 if (!child) {
279 struct dentry *new;
280 new = d_alloc(dir, &qname);
281 if (new) {
282 inode = proc_sys_make_inode(dir->d_inode, table);
283 if (!inode)
284 child = ERR_PTR(-ENOMEM);
285 else {
286 new->d_op = &proc_sys_dentry_operations;
287 d_add(new, inode);
288 }
289 if (child)
290 dput(new);
291 else
292 child = new;
293 }
294 }
295 if (!child || IS_ERR(child) || !child->d_inode)
296 goto end_instantiate;
297 inode = child->d_inode;
298 if (inode) {
299 ino = inode->i_ino;
300 type = inode->i_mode >> 12;
301 }
302 dput(child);
303end_instantiate:
304 if (!ino)
305 ino= find_inode_number(dir, &qname);
306 if (!ino)
307 ino = 1;
308 return filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
309}
310
311static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
312{
313 struct dentry *dentry = filp->f_dentry;
314 struct inode *inode = dentry->d_inode;
315 struct ctl_table_header *head = NULL;
316 struct ctl_table *table;
317 unsigned long pos;
318 int ret;
319
320 ret = -ENOTDIR;
321 if (!S_ISDIR(inode->i_mode))
322 goto out;
323
324 ret = 0;
325 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
326 if (filp->f_pos == 0) {
327 if (filldir(dirent, ".", 1, filp->f_pos,
328 inode->i_ino, DT_DIR) < 0)
329 goto out;
330 filp->f_pos++;
331 }
332 if (filp->f_pos == 1) {
333 if (filldir(dirent, "..", 2, filp->f_pos,
334 parent_ino(dentry), DT_DIR) < 0)
335 goto out;
336 filp->f_pos++;
337 }
338 pos = 2;
339
340 /* - Find each instance of the directory
341 * - Read all entries in each instance
342 * - Before returning an entry to user space lookup the entry
343 * by name and if I find a different entry don't return
344 * this one because it means it is a buried dup.
345 * For sysctl this should only happen for directory entries.
346 */
347 for (head = sysctl_head_next(NULL); head; head = sysctl_head_next(head)) {
348 table = proc_sys_lookup_table(dentry, head->ctl_table);
349
350 if (!table)
351 continue;
352
353 for (; table->ctl_name || table->procname; table++, pos++) {
354 /* Can't do anything without a proc name */
355 if (!table->procname)
356 continue;
357
358 if (pos < filp->f_pos)
359 continue;
360
361 if (proc_sys_fill_cache(filp, dirent, filldir, table) < 0)
362 goto out;
363 filp->f_pos = pos + 1;
364 }
365 }
366 ret = 1;
367out:
368 sysctl_head_finish(head);
369 return ret;
370}
371
372static int proc_sys_permission(struct inode *inode, int mask, struct nameidata *nd)
373{
374 /*
375 * sysctl entries that are not writeable,
376 * are _NOT_ writeable, capabilities or not.
377 */
378 struct ctl_table_header *head;
379 struct ctl_table *table;
380 struct dentry *dentry;
381 int mode;
382 int depth;
383 int error;
384
385 head = NULL;
386 depth = PROC_I(inode)->fd;
387
388 /* First check the cached permissions, in case we don't have
389 * enough information to lookup the sysctl table entry.
390 */
391 error = -EACCES;
392 mode = inode->i_mode;
393
394 if (current->euid == 0)
395 mode >>= 6;
396 else if (in_group_p(0))
397 mode >>= 3;
398
399 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
400 error = 0;
401
402 /* If we can't get a sysctl table entry the permission
403 * checks on the cached mode will have to be enough.
404 */
405 if (!nd || !depth)
406 goto out;
407
408 dentry = nd->dentry;
409 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
410
411 /* If the entry does not exist deny permission */
412 error = -EACCES;
413 if (!table)
414 goto out;
415
416 /* Use the permissions on the sysctl table entry */
417 error = sysctl_perm(table, mask);
418out:
419 sysctl_head_finish(head);
420 return error;
421}
422
423static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
424{
425 struct inode *inode = dentry->d_inode;
426 int error;
427
428 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
429 return -EPERM;
430
431 error = inode_change_ok(inode, attr);
432 if (!error) {
433 error = security_inode_setattr(dentry, attr);
434 if (!error)
435 error = inode_setattr(inode, attr);
436 }
437
438 return error;
439}
440
441/* I'm lazy and don't distinguish between files and directories,
442 * until access time.
443 */
444static const struct file_operations proc_sys_file_operations = {
445 .read = proc_sys_read,
446 .write = proc_sys_write,
447 .readdir = proc_sys_readdir,
448};
449
450static struct inode_operations proc_sys_inode_operations = {
451 .lookup = proc_sys_lookup,
452 .permission = proc_sys_permission,
453 .setattr = proc_sys_setattr,
454};
455
456static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
457{
458 struct ctl_table_header *head;
459 struct ctl_table *table;
460 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
461 proc_sys_refresh_inode(dentry->d_inode, table);
462 sysctl_head_finish(head);
463 return !!table;
464}
465
466static struct dentry_operations proc_sys_dentry_operations = {
467 .d_revalidate = proc_sys_revalidate,
468};
469
470static struct proc_dir_entry *proc_sys_root;
471
472int proc_sys_init(void)
473{
474 proc_sys_root = proc_mkdir("sys", NULL);
475 proc_sys_root->proc_iops = &proc_sys_inode_operations;
476 proc_sys_root->proc_fops = &proc_sys_file_operations;
477 proc_sys_root->nlink = 0;
478 return 0;
479}
diff --git a/fs/proc/root.c b/fs/proc/root.c
index af154458b540..5834a744c2a9 100644
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -23,10 +23,6 @@
23 23
24struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver; 24struct proc_dir_entry *proc_net, *proc_net_stat, *proc_bus, *proc_root_fs, *proc_root_driver;
25 25
26#ifdef CONFIG_SYSCTL
27struct proc_dir_entry *proc_sys_root;
28#endif
29
30static int proc_get_sb(struct file_system_type *fs_type, 26static int proc_get_sb(struct file_system_type *fs_type,
31 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 27 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
32{ 28{
@@ -71,13 +67,6 @@ void __init proc_root_init(void)
71#ifdef CONFIG_SYSVIPC 67#ifdef CONFIG_SYSVIPC
72 proc_mkdir("sysvipc", NULL); 68 proc_mkdir("sysvipc", NULL);
73#endif 69#endif
74#ifdef CONFIG_SYSCTL
75 proc_sys_root = proc_mkdir("sys", NULL);
76#endif
77#if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
78 proc_mkdir("sys/fs", NULL);
79 proc_mkdir("sys/fs/binfmt_misc", NULL);
80#endif
81 proc_root_fs = proc_mkdir("fs", NULL); 70 proc_root_fs = proc_mkdir("fs", NULL);
82 proc_root_driver = proc_mkdir("driver", NULL); 71 proc_root_driver = proc_mkdir("driver", NULL);
83 proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */ 72 proc_mkdir("fs/nfsd", NULL); /* somewhere for the nfsd filesystem to be mounted */
@@ -90,6 +79,9 @@ void __init proc_root_init(void)
90 proc_device_tree_init(); 79 proc_device_tree_init();
91#endif 80#endif
92 proc_bus = proc_mkdir("bus", NULL); 81 proc_bus = proc_mkdir("bus", NULL);
82#ifdef CONFIG_SYSCTL
83 proc_sys_init();
84#endif
93} 85}
94 86
95static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat 87static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat