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