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