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