5 #include <linux/sysctl.h>
6 #include <linux/proc_fs.h>
7 #include <linux/security.h>
10 static struct dentry_operations proc_sys_dentry_operations;
11 static const struct file_operations proc_sys_file_operations;
12 static struct inode_operations proc_sys_inode_operations;
14 static void proc_sys_refresh_inode(struct inode *inode, struct ctl_table *table)
16 /* Refresh the cached information bits in the inode */
20 inode->i_mode = table->mode;
21 if (table->proc_handler) {
22 inode->i_mode |= S_IFREG;
25 inode->i_mode |= S_IFDIR;
26 inode->i_nlink = 0; /* It is too hard to figure out */
31 static struct inode *proc_sys_make_inode(struct inode *dir, struct ctl_table *table)
34 struct proc_inode *dir_ei, *ei;
37 inode = new_inode(dir->i_sb);
41 /* A directory is always one deeper than it's parent */
43 depth = dir_ei->fd + 1;
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);
56 static struct dentry *proc_sys_ancestor(struct dentry *dentry, int depth)
59 struct proc_inode *ei;
61 ei = PROC_I(dentry->d_inode);
65 dentry = dentry->d_parent;
70 static struct ctl_table *proc_sys_lookup_table_one(struct ctl_table *table,
74 for ( ; table->ctl_name || table->procname; table++) {
79 len = strlen(table->procname);
83 if (memcmp(table->procname, name->name, len) != 0)
92 static struct ctl_table *proc_sys_lookup_table(struct dentry *dentry,
93 struct ctl_table *table)
95 struct dentry *ancestor;
96 struct proc_inode *ei;
99 ei = PROC_I(dentry->d_inode);
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);
109 table = table->child;
114 static struct ctl_table *proc_sys_lookup_entry(struct dentry *dparent,
116 struct ctl_table *table)
118 table = proc_sys_lookup_table(dparent, table);
120 table = proc_sys_lookup_table_one(table, name);
124 static struct ctl_table *do_proc_sys_lookup(struct dentry *parent,
126 struct ctl_table_header **ptr)
128 struct ctl_table_header *head;
129 struct ctl_table *table = NULL;
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);
141 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
142 struct nameidata *nd)
144 struct ctl_table_header *head;
147 struct ctl_table *table;
149 err = ERR_PTR(-ENOENT);
150 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
154 err = ERR_PTR(-ENOMEM);
155 inode = proc_sys_make_inode(dir, table);
160 dentry->d_op = &proc_sys_dentry_operations;
161 d_add(dentry, inode);
164 sysctl_head_finish(head);
168 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
169 size_t count, loff_t *ppos)
171 struct dentry *dentry = filp->f_dentry;
172 struct ctl_table_header *head;
173 struct ctl_table *table;
176 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
177 /* Has the sysctl entry disappeared on us? */
182 /* Has the sysctl entry been replaced by a directory? */
184 if (!table->proc_handler)
188 * At this point we know that the sysctl was not unregistered
189 * and won't be until we finish.
192 if (sysctl_perm(table, MAY_READ))
195 /* careful: calling conventions are nasty here */
197 error = table->proc_handler(table, 0, filp, buf, &res, ppos);
201 sysctl_head_finish(head);
206 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
207 size_t count, loff_t *ppos)
209 struct dentry *dentry = filp->f_dentry;
210 struct ctl_table_header *head;
211 struct ctl_table *table;
214 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
215 /* Has the sysctl entry disappeared on us? */
220 /* Has the sysctl entry been replaced by a directory? */
222 if (!table->proc_handler)
226 * At this point we know that the sysctl was not unregistered
227 * and won't be until we finish.
230 if (sysctl_perm(table, MAY_WRITE))
233 /* careful: calling conventions are nasty here */
235 error = table->proc_handler(table, 1, filp, (char __user *)buf,
240 sysctl_head_finish(head);
246 static int proc_sys_fill_cache(struct file *filp, void *dirent,
247 filldir_t filldir, struct ctl_table *table)
249 struct ctl_table_header *head;
250 struct ctl_table *child_table = NULL;
251 struct dentry *child, *dir = filp->f_path.dentry;
255 unsigned type = DT_UNKNOWN;
258 qname.name = table->procname;
259 qname.len = strlen(table->procname);
260 qname.hash = full_name_hash(qname.name, qname.len);
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
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.
272 child_table = do_proc_sys_lookup(dir, &qname, &head);
273 sysctl_head_finish(head);
274 if (child_table != table)
277 child = d_lookup(dir, &qname);
280 new = d_alloc(dir, &qname);
282 inode = proc_sys_make_inode(dir->d_inode, table);
284 child = ERR_PTR(-ENOMEM);
286 new->d_op = &proc_sys_dentry_operations;
295 if (!child || IS_ERR(child) || !child->d_inode)
296 goto end_instantiate;
297 inode = child->d_inode;
300 type = inode->i_mode >> 12;
305 ino= find_inode_number(dir, &qname);
308 return filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
311 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
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;
321 if (!S_ISDIR(inode->i_mode))
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)
332 if (filp->f_pos == 1) {
333 if (filldir(dirent, "..", 2, filp->f_pos,
334 parent_ino(dentry), DT_DIR) < 0)
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.
347 for (head = sysctl_head_next(NULL); head; head = sysctl_head_next(head)) {
348 table = proc_sys_lookup_table(dentry, head->ctl_table);
353 for (; table->ctl_name || table->procname; table++, pos++) {
354 /* Can't do anything without a proc name */
355 if (!table->procname)
358 if (pos < filp->f_pos)
361 if (proc_sys_fill_cache(filp, dirent, filldir, table) < 0)
363 filp->f_pos = pos + 1;
368 sysctl_head_finish(head);
372 static int proc_sys_permission(struct inode *inode, int mask, struct nameidata *nd)
375 * sysctl entries that are not writeable,
376 * are _NOT_ writeable, capabilities or not.
378 struct ctl_table_header *head;
379 struct ctl_table *table;
380 struct dentry *dentry;
386 depth = PROC_I(inode)->fd;
388 /* First check the cached permissions, in case we don't have
389 * enough information to lookup the sysctl table entry.
392 mode = inode->i_mode;
394 if (current->euid == 0)
396 else if (in_group_p(0))
399 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
402 /* If we can't get a sysctl table entry the permission
403 * checks on the cached mode will have to be enough.
409 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
411 /* If the entry does not exist deny permission */
416 /* Use the permissions on the sysctl table entry */
417 error = sysctl_perm(table, mask);
419 sysctl_head_finish(head);
423 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
425 struct inode *inode = dentry->d_inode;
428 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
431 error = inode_change_ok(inode, attr);
433 error = inode_setattr(inode, attr);
438 /* I'm lazy and don't distinguish between files and directories,
441 static const struct file_operations proc_sys_file_operations = {
442 .read = proc_sys_read,
443 .write = proc_sys_write,
444 .readdir = proc_sys_readdir,
447 static struct inode_operations proc_sys_inode_operations = {
448 .lookup = proc_sys_lookup,
449 .permission = proc_sys_permission,
450 .setattr = proc_sys_setattr,
453 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
455 struct ctl_table_header *head;
456 struct ctl_table *table;
457 table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
458 proc_sys_refresh_inode(dentry->d_inode, table);
459 sysctl_head_finish(head);
463 static struct dentry_operations proc_sys_dentry_operations = {
464 .d_revalidate = proc_sys_revalidate,
467 static struct proc_dir_entry *proc_sys_root;
469 int proc_sys_init(void)
471 proc_sys_root = proc_mkdir("sys", NULL);
472 proc_sys_root->proc_iops = &proc_sys_inode_operations;
473 proc_sys_root->proc_fops = &proc_sys_file_operations;
474 proc_sys_root->nlink = 0;