xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision de4e83bd)
1 /*
2  * /proc/sys support
3  */
4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/namei.h>
10 #include "internal.h"
11 
12 static const struct dentry_operations proc_sys_dentry_operations;
13 static const struct file_operations proc_sys_file_operations;
14 static const struct inode_operations proc_sys_inode_operations;
15 static const struct file_operations proc_sys_dir_file_operations;
16 static const struct inode_operations proc_sys_dir_operations;
17 
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 
27 static struct inode *proc_sys_make_inode(struct super_block *sb,
28 		struct ctl_table_header *head, struct ctl_table *table)
29 {
30 	struct inode *inode;
31 	struct proc_inode *ei;
32 
33 	inode = new_inode(sb);
34 	if (!inode)
35 		goto out;
36 
37 	inode->i_ino = get_next_ino();
38 
39 	sysctl_head_get(head);
40 	ei = PROC_I(inode);
41 	ei->sysctl = head;
42 	ei->sysctl_entry = table;
43 
44 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
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;
52 		clear_nlink(inode);
53 		inode->i_op = &proc_sys_dir_operations;
54 		inode->i_fop = &proc_sys_dir_file_operations;
55 	}
56 out:
57 	return inode;
58 }
59 
60 static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
61 {
62 	for ( ; p->procname; p++) {
63 		if (strlen(p->procname) != name->len)
64 			continue;
65 
66 		if (memcmp(p->procname, name->name, name->len) != 0)
67 			continue;
68 
69 		/* I have a match */
70 		return p;
71 	}
72 	return NULL;
73 }
74 
75 static struct ctl_table_header *grab_header(struct inode *inode)
76 {
77 	if (PROC_I(inode)->sysctl)
78 		return sysctl_head_grab(PROC_I(inode)->sysctl);
79 	else
80 		return sysctl_head_next(NULL);
81 }
82 
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);
93 
94 	if (IS_ERR(head))
95 		return ERR_CAST(head);
96 
97 	if (table && !table->child) {
98 		WARN_ON(1);
99 		goto out;
100 	}
101 
102 	table = table ? table->child : head->ctl_table;
103 
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 		}
113 	}
114 
115 	if (!p)
116 		goto out;
117 
118 	err = ERR_PTR(-ENOMEM);
119 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
120 	if (h)
121 		sysctl_head_finish(h);
122 
123 	if (!inode)
124 		goto out;
125 
126 	err = NULL;
127 	d_set_d_op(dentry, &proc_sys_dentry_operations);
128 	d_add(dentry, inode);
129 
130 out:
131 	sysctl_head_finish(head);
132 	return err;
133 }
134 
135 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
136 		size_t count, loff_t *ppos, int write)
137 {
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;
141 	ssize_t error;
142 	size_t res;
143 
144 	if (IS_ERR(head))
145 		return PTR_ERR(head);
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;
152 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
153 		goto out;
154 
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 
160 	/* careful: calling conventions are nasty here */
161 	res = count;
162 	error = table->proc_handler(table, write, buf, &res, ppos);
163 	if (!error)
164 		error = res;
165 out:
166 	sysctl_head_finish(head);
167 
168 	return error;
169 }
170 
171 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
172 				size_t count, loff_t *ppos)
173 {
174 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
175 }
176 
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);
181 }
182 
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 }
216 
217 static int proc_sys_fill_cache(struct file *filp, void *dirent,
218 				filldir_t filldir,
219 				struct ctl_table_header *head,
220 				struct ctl_table *table)
221 {
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;
227 
228 	qname.name = table->procname;
229 	qname.len  = strlen(table->procname);
230 	qname.hash = full_name_hash(qname.name, qname.len);
231 
232 	child = d_lookup(dir, &qname);
233 	if (!child) {
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 {
241 				d_set_d_op(child, &proc_sys_dentry_operations);
242 				d_add(child, inode);
243 			}
244 		} else {
245 			return -ENOMEM;
246 		}
247 	}
248 	inode = child->d_inode;
249 	ino  = inode->i_ino;
250 	type = inode->i_mode >> 12;
251 	dput(child);
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 
260 	for (; table->procname; table++, (*pos)++) {
261 		int res;
262 
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;
273 }
274 
275 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
276 {
277 	struct dentry *dentry = filp->f_path.dentry;
278 	struct inode *inode = dentry->d_inode;
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;
282 	unsigned long pos;
283 	int ret = -EINVAL;
284 
285 	if (IS_ERR(head))
286 		return PTR_ERR(head);
287 
288 	if (table && !table->child) {
289 		WARN_ON(1);
290 		goto out;
291 	}
292 
293 	table = table ? table->child : head->ctl_table;
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 
311 	ret = scan(head, table, &pos, filp, dirent, filldir);
312 	if (ret)
313 		goto out;
314 
315 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
316 		if (h->attached_to != table)
317 			continue;
318 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
319 		if (ret) {
320 			sysctl_head_finish(h);
321 			break;
322 		}
323 	}
324 	ret = 1;
325 out:
326 	sysctl_head_finish(head);
327 	return ret;
328 }
329 
330 static int proc_sys_permission(struct inode *inode, int mask)
331 {
332 	/*
333 	 * sysctl entries that are not writeable,
334 	 * are _NOT_ writeable, capabilities or not.
335 	 */
336 	struct ctl_table_header *head;
337 	struct ctl_table *table;
338 	int error;
339 
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);
345 	if (IS_ERR(head))
346 		return PTR_ERR(head);
347 
348 	table = PROC_I(inode)->sysctl_entry;
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 */
352 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
353 
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);
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 	}
376 
377 	setattr_copy(inode, attr);
378 	mark_inode_dirty(inode);
379 	return 0;
380 }
381 
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 
399 static const struct file_operations proc_sys_file_operations = {
400 	.open		= proc_sys_open,
401 	.poll		= proc_sys_poll,
402 	.read		= proc_sys_read,
403 	.write		= proc_sys_write,
404 	.llseek		= default_llseek,
405 };
406 
407 static const struct file_operations proc_sys_dir_file_operations = {
408 	.read		= generic_read_dir,
409 	.readdir	= proc_sys_readdir,
410 	.llseek		= generic_file_llseek,
411 };
412 
413 static const struct inode_operations proc_sys_inode_operations = {
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 = {
420 	.lookup		= proc_sys_lookup,
421 	.permission	= proc_sys_permission,
422 	.setattr	= proc_sys_setattr,
423 	.getattr	= proc_sys_getattr,
424 };
425 
426 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
427 {
428 	if (nd->flags & LOOKUP_RCU)
429 		return -ECHILD;
430 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
431 }
432 
433 static int proc_sys_delete(const struct dentry *dentry)
434 {
435 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
436 }
437 
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)
442 {
443 	struct ctl_table_header *head;
444 	/* Although proc doesn't have negative dentries, rcu-walk means
445 	 * that inode here can be NULL */
446 	/* AV: can it, indeed? */
447 	if (!inode)
448 		return 1;
449 	if (name->len != len)
450 		return 1;
451 	if (memcmp(name->name, str, len))
452 		return 1;
453 	head = rcu_dereference(PROC_I(inode)->sysctl);
454 	return !head || !sysctl_is_seen(head);
455 }
456 
457 static const struct dentry_operations proc_sys_dentry_operations = {
458 	.d_revalidate	= proc_sys_revalidate,
459 	.d_delete	= proc_sys_delete,
460 	.d_compare	= proc_sys_compare,
461 };
462 
463 int __init proc_sys_init(void)
464 {
465 	struct proc_dir_entry *proc_sys_root;
466 
467 	proc_sys_root = proc_mkdir("sys", NULL);
468 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
469 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
470 	proc_sys_root->nlink = 0;
471 
472 	return sysctl_init();
473 }
474