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