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