xref: /openbmc/linux/fs/proc/proc_sysctl.c (revision de4e83bd)
177b14db5SEric W. Biederman /*
277b14db5SEric W. Biederman  * /proc/sys support
377b14db5SEric W. Biederman  */
41e0edd3fSAlexey Dobriyan #include <linux/init.h>
577b14db5SEric W. Biederman #include <linux/sysctl.h>
6f1ecf068SLucas De Marchi #include <linux/poll.h>
777b14db5SEric W. Biederman #include <linux/proc_fs.h>
877b14db5SEric W. Biederman #include <linux/security.h>
934286d66SNick Piggin #include <linux/namei.h>
1077b14db5SEric W. Biederman #include "internal.h"
1177b14db5SEric W. Biederman 
12d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations;
1377b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations;
1403a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations;
159043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations;
169043476fSAl Viro static const struct inode_operations proc_sys_dir_operations;
1777b14db5SEric W. Biederman 
18f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll)
19f1ecf068SLucas De Marchi {
20f1ecf068SLucas De Marchi 	if (!poll)
21f1ecf068SLucas De Marchi 		return;
22f1ecf068SLucas De Marchi 
23f1ecf068SLucas De Marchi 	atomic_inc(&poll->event);
24f1ecf068SLucas De Marchi 	wake_up_interruptible(&poll->wait);
25f1ecf068SLucas De Marchi }
26f1ecf068SLucas De Marchi 
279043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb,
289043476fSAl Viro 		struct ctl_table_header *head, struct ctl_table *table)
2977b14db5SEric W. Biederman {
3077b14db5SEric W. Biederman 	struct inode *inode;
319043476fSAl Viro 	struct proc_inode *ei;
3277b14db5SEric W. Biederman 
339043476fSAl Viro 	inode = new_inode(sb);
3477b14db5SEric W. Biederman 	if (!inode)
3577b14db5SEric W. Biederman 		goto out;
3677b14db5SEric W. Biederman 
3785fe4025SChristoph Hellwig 	inode->i_ino = get_next_ino();
3885fe4025SChristoph Hellwig 
399043476fSAl Viro 	sysctl_head_get(head);
4077b14db5SEric W. Biederman 	ei = PROC_I(inode);
419043476fSAl Viro 	ei->sysctl = head;
429043476fSAl Viro 	ei->sysctl_entry = table;
439043476fSAl Viro 
4477b14db5SEric W. Biederman 	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
459043476fSAl Viro 	inode->i_mode = table->mode;
469043476fSAl Viro 	if (!table->child) {
479043476fSAl Viro 		inode->i_mode |= S_IFREG;
4877b14db5SEric W. Biederman 		inode->i_op = &proc_sys_inode_operations;
4977b14db5SEric W. Biederman 		inode->i_fop = &proc_sys_file_operations;
509043476fSAl Viro 	} else {
519043476fSAl Viro 		inode->i_mode |= S_IFDIR;
526d6b77f1SMiklos Szeredi 		clear_nlink(inode);
539043476fSAl Viro 		inode->i_op = &proc_sys_dir_operations;
549043476fSAl Viro 		inode->i_fop = &proc_sys_dir_file_operations;
559043476fSAl Viro 	}
5677b14db5SEric W. Biederman out:
5777b14db5SEric W. Biederman 	return inode;
5877b14db5SEric W. Biederman }
5977b14db5SEric W. Biederman 
609043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
6177b14db5SEric W. Biederman {
622315ffa0SEric W. Biederman 	for ( ; p->procname; p++) {
6336885d7bSLucas De Marchi 		if (strlen(p->procname) != name->len)
6477b14db5SEric W. Biederman 			continue;
6577b14db5SEric W. Biederman 
6636885d7bSLucas De Marchi 		if (memcmp(p->procname, name->name, name->len) != 0)
6777b14db5SEric W. Biederman 			continue;
6877b14db5SEric W. Biederman 
6977b14db5SEric W. Biederman 		/* I have a match */
709043476fSAl Viro 		return p;
7177b14db5SEric W. Biederman 	}
7277b14db5SEric W. Biederman 	return NULL;
7377b14db5SEric W. Biederman }
7477b14db5SEric W. Biederman 
7581324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode)
7677b14db5SEric W. Biederman {
779043476fSAl Viro 	if (PROC_I(inode)->sysctl)
789043476fSAl Viro 		return sysctl_head_grab(PROC_I(inode)->sysctl);
799043476fSAl Viro 	else
809043476fSAl Viro 		return sysctl_head_next(NULL);
8177b14db5SEric W. Biederman }
8277b14db5SEric W. Biederman 
8377b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
8477b14db5SEric W. Biederman 					struct nameidata *nd)
8577b14db5SEric W. Biederman {
869043476fSAl Viro 	struct ctl_table_header *head = grab_header(dir);
879043476fSAl Viro 	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
889043476fSAl Viro 	struct ctl_table_header *h = NULL;
899043476fSAl Viro 	struct qstr *name = &dentry->d_name;
909043476fSAl Viro 	struct ctl_table *p;
9177b14db5SEric W. Biederman 	struct inode *inode;
929043476fSAl Viro 	struct dentry *err = ERR_PTR(-ENOENT);
9377b14db5SEric W. Biederman 
949043476fSAl Viro 	if (IS_ERR(head))
959043476fSAl Viro 		return ERR_CAST(head);
969043476fSAl Viro 
979043476fSAl Viro 	if (table && !table->child) {
989043476fSAl Viro 		WARN_ON(1);
999043476fSAl Viro 		goto out;
1009043476fSAl Viro 	}
1019043476fSAl Viro 
1029043476fSAl Viro 	table = table ? table->child : head->ctl_table;
1039043476fSAl Viro 
1049043476fSAl Viro 	p = find_in_table(table, name);
1059043476fSAl Viro 	if (!p) {
1069043476fSAl Viro 		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
1079043476fSAl Viro 			if (h->attached_to != table)
1089043476fSAl Viro 				continue;
1099043476fSAl Viro 			p = find_in_table(h->attached_by, name);
1109043476fSAl Viro 			if (p)
1119043476fSAl Viro 				break;
1129043476fSAl Viro 		}
1139043476fSAl Viro 	}
1149043476fSAl Viro 
1159043476fSAl Viro 	if (!p)
11677b14db5SEric W. Biederman 		goto out;
11777b14db5SEric W. Biederman 
11877b14db5SEric W. Biederman 	err = ERR_PTR(-ENOMEM);
1199043476fSAl Viro 	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
1209043476fSAl Viro 	if (h)
1219043476fSAl Viro 		sysctl_head_finish(h);
1229043476fSAl Viro 
12377b14db5SEric W. Biederman 	if (!inode)
12477b14db5SEric W. Biederman 		goto out;
12577b14db5SEric W. Biederman 
12677b14db5SEric W. Biederman 	err = NULL;
127fb045adbSNick Piggin 	d_set_d_op(dentry, &proc_sys_dentry_operations);
12877b14db5SEric W. Biederman 	d_add(dentry, inode);
12977b14db5SEric W. Biederman 
13077b14db5SEric W. Biederman out:
13177b14db5SEric W. Biederman 	sysctl_head_finish(head);
13277b14db5SEric W. Biederman 	return err;
13377b14db5SEric W. Biederman }
13477b14db5SEric W. Biederman 
1357708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
1367708bfb1SPavel Emelyanov 		size_t count, loff_t *ppos, int write)
13777b14db5SEric W. Biederman {
1389043476fSAl Viro 	struct inode *inode = filp->f_path.dentry->d_inode;
1399043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
1409043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
1412a2da53bSDavid Howells 	ssize_t error;
1422a2da53bSDavid Howells 	size_t res;
14377b14db5SEric W. Biederman 
1449043476fSAl Viro 	if (IS_ERR(head))
1459043476fSAl Viro 		return PTR_ERR(head);
14677b14db5SEric W. Biederman 
14777b14db5SEric W. Biederman 	/*
14877b14db5SEric W. Biederman 	 * At this point we know that the sysctl was not unregistered
14977b14db5SEric W. Biederman 	 * and won't be until we finish.
15077b14db5SEric W. Biederman 	 */
15177b14db5SEric W. Biederman 	error = -EPERM;
152d7321cd6SPavel Emelyanov 	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
15377b14db5SEric W. Biederman 		goto out;
15477b14db5SEric W. Biederman 
1559043476fSAl Viro 	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
1569043476fSAl Viro 	error = -EINVAL;
1579043476fSAl Viro 	if (!table->proc_handler)
1589043476fSAl Viro 		goto out;
1599043476fSAl Viro 
16077b14db5SEric W. Biederman 	/* careful: calling conventions are nasty here */
16177b14db5SEric W. Biederman 	res = count;
1628d65af78SAlexey Dobriyan 	error = table->proc_handler(table, write, buf, &res, ppos);
16377b14db5SEric W. Biederman 	if (!error)
16477b14db5SEric W. Biederman 		error = res;
16577b14db5SEric W. Biederman out:
16677b14db5SEric W. Biederman 	sysctl_head_finish(head);
16777b14db5SEric W. Biederman 
16877b14db5SEric W. Biederman 	return error;
16977b14db5SEric W. Biederman }
17077b14db5SEric W. Biederman 
1717708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf,
1727708bfb1SPavel Emelyanov 				size_t count, loff_t *ppos)
1737708bfb1SPavel Emelyanov {
1747708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
1757708bfb1SPavel Emelyanov }
1767708bfb1SPavel Emelyanov 
17777b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
17877b14db5SEric W. Biederman 				size_t count, loff_t *ppos)
17977b14db5SEric W. Biederman {
1807708bfb1SPavel Emelyanov 	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
18177b14db5SEric W. Biederman }
18277b14db5SEric W. Biederman 
183f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp)
184f1ecf068SLucas De Marchi {
185f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
186f1ecf068SLucas De Marchi 
187f1ecf068SLucas De Marchi 	if (table->poll)
188f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
189f1ecf068SLucas De Marchi 
190f1ecf068SLucas De Marchi 	return 0;
191f1ecf068SLucas De Marchi }
192f1ecf068SLucas De Marchi 
193f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
194f1ecf068SLucas De Marchi {
195f1ecf068SLucas De Marchi 	struct inode *inode = filp->f_path.dentry->d_inode;
196f1ecf068SLucas De Marchi 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
197f1ecf068SLucas De Marchi 	unsigned long event = (unsigned long)filp->private_data;
198f1ecf068SLucas De Marchi 	unsigned int ret = DEFAULT_POLLMASK;
199f1ecf068SLucas De Marchi 
200f1ecf068SLucas De Marchi 	if (!table->proc_handler)
201f1ecf068SLucas De Marchi 		goto out;
202f1ecf068SLucas De Marchi 
203f1ecf068SLucas De Marchi 	if (!table->poll)
204f1ecf068SLucas De Marchi 		goto out;
205f1ecf068SLucas De Marchi 
206f1ecf068SLucas De Marchi 	poll_wait(filp, &table->poll->wait, wait);
207f1ecf068SLucas De Marchi 
208f1ecf068SLucas De Marchi 	if (event != atomic_read(&table->poll->event)) {
209f1ecf068SLucas De Marchi 		filp->private_data = proc_sys_poll_event(table->poll);
210f1ecf068SLucas De Marchi 		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
211f1ecf068SLucas De Marchi 	}
212f1ecf068SLucas De Marchi 
213f1ecf068SLucas De Marchi out:
214f1ecf068SLucas De Marchi 	return ret;
215f1ecf068SLucas De Marchi }
21677b14db5SEric W. Biederman 
21777b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent,
2189043476fSAl Viro 				filldir_t filldir,
2199043476fSAl Viro 				struct ctl_table_header *head,
2209043476fSAl Viro 				struct ctl_table *table)
22177b14db5SEric W. Biederman {
22277b14db5SEric W. Biederman 	struct dentry *child, *dir = filp->f_path.dentry;
22377b14db5SEric W. Biederman 	struct inode *inode;
22477b14db5SEric W. Biederman 	struct qstr qname;
22577b14db5SEric W. Biederman 	ino_t ino = 0;
22677b14db5SEric W. Biederman 	unsigned type = DT_UNKNOWN;
22777b14db5SEric W. Biederman 
22877b14db5SEric W. Biederman 	qname.name = table->procname;
22977b14db5SEric W. Biederman 	qname.len  = strlen(table->procname);
23077b14db5SEric W. Biederman 	qname.hash = full_name_hash(qname.name, qname.len);
23177b14db5SEric W. Biederman 
23277b14db5SEric W. Biederman 	child = d_lookup(dir, &qname);
23377b14db5SEric W. Biederman 	if (!child) {
2349043476fSAl Viro 		child = d_alloc(dir, &qname);
2359043476fSAl Viro 		if (child) {
2369043476fSAl Viro 			inode = proc_sys_make_inode(dir->d_sb, head, table);
2379043476fSAl Viro 			if (!inode) {
2389043476fSAl Viro 				dput(child);
2399043476fSAl Viro 				return -ENOMEM;
2409043476fSAl Viro 			} else {
241fb045adbSNick Piggin 				d_set_d_op(child, &proc_sys_dentry_operations);
2429043476fSAl Viro 				d_add(child, inode);
24377b14db5SEric W. Biederman 			}
2449043476fSAl Viro 		} else {
2459043476fSAl Viro 			return -ENOMEM;
24677b14db5SEric W. Biederman 		}
24777b14db5SEric W. Biederman 	}
24877b14db5SEric W. Biederman 	inode = child->d_inode;
24977b14db5SEric W. Biederman 	ino  = inode->i_ino;
25077b14db5SEric W. Biederman 	type = inode->i_mode >> 12;
25177b14db5SEric W. Biederman 	dput(child);
2529043476fSAl Viro 	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
2539043476fSAl Viro }
2549043476fSAl Viro 
2559043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table,
2569043476fSAl Viro 		unsigned long *pos, struct file *file,
2579043476fSAl Viro 		void *dirent, filldir_t filldir)
2589043476fSAl Viro {
2599043476fSAl Viro 
2602315ffa0SEric W. Biederman 	for (; table->procname; table++, (*pos)++) {
2619043476fSAl Viro 		int res;
2629043476fSAl Viro 
2639043476fSAl Viro 		if (*pos < file->f_pos)
2649043476fSAl Viro 			continue;
2659043476fSAl Viro 
2669043476fSAl Viro 		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
2679043476fSAl Viro 		if (res)
2689043476fSAl Viro 			return res;
2699043476fSAl Viro 
2709043476fSAl Viro 		file->f_pos = *pos + 1;
2719043476fSAl Viro 	}
2729043476fSAl Viro 	return 0;
27377b14db5SEric W. Biederman }
27477b14db5SEric W. Biederman 
27577b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
27677b14db5SEric W. Biederman {
2779043476fSAl Viro 	struct dentry *dentry = filp->f_path.dentry;
27877b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
2799043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
2809043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2819043476fSAl Viro 	struct ctl_table_header *h = NULL;
28277b14db5SEric W. Biederman 	unsigned long pos;
2839043476fSAl Viro 	int ret = -EINVAL;
28477b14db5SEric W. Biederman 
2859043476fSAl Viro 	if (IS_ERR(head))
2869043476fSAl Viro 		return PTR_ERR(head);
2879043476fSAl Viro 
2889043476fSAl Viro 	if (table && !table->child) {
2899043476fSAl Viro 		WARN_ON(1);
29077b14db5SEric W. Biederman 		goto out;
2919043476fSAl Viro 	}
2929043476fSAl Viro 
2939043476fSAl Viro 	table = table ? table->child : head->ctl_table;
29477b14db5SEric W. Biederman 
29577b14db5SEric W. Biederman 	ret = 0;
29677b14db5SEric W. Biederman 	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
29777b14db5SEric W. Biederman 	if (filp->f_pos == 0) {
29877b14db5SEric W. Biederman 		if (filldir(dirent, ".", 1, filp->f_pos,
29977b14db5SEric W. Biederman 				inode->i_ino, DT_DIR) < 0)
30077b14db5SEric W. Biederman 			goto out;
30177b14db5SEric W. Biederman 		filp->f_pos++;
30277b14db5SEric W. Biederman 	}
30377b14db5SEric W. Biederman 	if (filp->f_pos == 1) {
30477b14db5SEric W. Biederman 		if (filldir(dirent, "..", 2, filp->f_pos,
30577b14db5SEric W. Biederman 				parent_ino(dentry), DT_DIR) < 0)
30677b14db5SEric W. Biederman 			goto out;
30777b14db5SEric W. Biederman 		filp->f_pos++;
30877b14db5SEric W. Biederman 	}
30977b14db5SEric W. Biederman 	pos = 2;
31077b14db5SEric W. Biederman 
3119043476fSAl Viro 	ret = scan(head, table, &pos, filp, dirent, filldir);
3129043476fSAl Viro 	if (ret)
31377b14db5SEric W. Biederman 		goto out;
3149043476fSAl Viro 
3159043476fSAl Viro 	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
3169043476fSAl Viro 		if (h->attached_to != table)
3179043476fSAl Viro 			continue;
3189043476fSAl Viro 		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
3199043476fSAl Viro 		if (ret) {
3209043476fSAl Viro 			sysctl_head_finish(h);
3219043476fSAl Viro 			break;
32277b14db5SEric W. Biederman 		}
32377b14db5SEric W. Biederman 	}
32477b14db5SEric W. Biederman 	ret = 1;
32577b14db5SEric W. Biederman out:
32677b14db5SEric W. Biederman 	sysctl_head_finish(head);
32777b14db5SEric W. Biederman 	return ret;
32877b14db5SEric W. Biederman }
32977b14db5SEric W. Biederman 
33010556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask)
33177b14db5SEric W. Biederman {
33277b14db5SEric W. Biederman 	/*
33377b14db5SEric W. Biederman 	 * sysctl entries that are not writeable,
33477b14db5SEric W. Biederman 	 * are _NOT_ writeable, capabilities or not.
33577b14db5SEric W. Biederman 	 */
336f696a365SMiklos Szeredi 	struct ctl_table_header *head;
337f696a365SMiklos Szeredi 	struct ctl_table *table;
33877b14db5SEric W. Biederman 	int error;
33977b14db5SEric W. Biederman 
340f696a365SMiklos Szeredi 	/* Executable files are not allowed under /proc/sys/ */
341f696a365SMiklos Szeredi 	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
342f696a365SMiklos Szeredi 		return -EACCES;
343f696a365SMiklos Szeredi 
344f696a365SMiklos Szeredi 	head = grab_header(inode);
3459043476fSAl Viro 	if (IS_ERR(head))
3469043476fSAl Viro 		return PTR_ERR(head);
34777b14db5SEric W. Biederman 
348f696a365SMiklos Szeredi 	table = PROC_I(inode)->sysctl_entry;
3499043476fSAl Viro 	if (!table) /* global root - r-xr-xr-x */
3509043476fSAl Viro 		error = mask & MAY_WRITE ? -EACCES : 0;
3519043476fSAl Viro 	else /* Use the permissions on the sysctl table entry */
3521fc0f78cSAl Viro 		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
3539043476fSAl Viro 
35477b14db5SEric W. Biederman 	sysctl_head_finish(head);
35577b14db5SEric W. Biederman 	return error;
35677b14db5SEric W. Biederman }
35777b14db5SEric W. Biederman 
35877b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
35977b14db5SEric W. Biederman {
36077b14db5SEric W. Biederman 	struct inode *inode = dentry->d_inode;
36177b14db5SEric W. Biederman 	int error;
36277b14db5SEric W. Biederman 
36377b14db5SEric W. Biederman 	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
36477b14db5SEric W. Biederman 		return -EPERM;
36577b14db5SEric W. Biederman 
36677b14db5SEric W. Biederman 	error = inode_change_ok(inode, attr);
3671025774cSChristoph Hellwig 	if (error)
36877b14db5SEric W. Biederman 		return error;
3691025774cSChristoph Hellwig 
3701025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
3711025774cSChristoph Hellwig 	    attr->ia_size != i_size_read(inode)) {
3721025774cSChristoph Hellwig 		error = vmtruncate(inode, attr->ia_size);
3731025774cSChristoph Hellwig 		if (error)
3741025774cSChristoph Hellwig 			return error;
3751025774cSChristoph Hellwig 	}
3761025774cSChristoph Hellwig 
3771025774cSChristoph Hellwig 	setattr_copy(inode, attr);
3781025774cSChristoph Hellwig 	mark_inode_dirty(inode);
3791025774cSChristoph Hellwig 	return 0;
38077b14db5SEric W. Biederman }
38177b14db5SEric W. Biederman 
3829043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3839043476fSAl Viro {
3849043476fSAl Viro 	struct inode *inode = dentry->d_inode;
3859043476fSAl Viro 	struct ctl_table_header *head = grab_header(inode);
3869043476fSAl Viro 	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
3879043476fSAl Viro 
3889043476fSAl Viro 	if (IS_ERR(head))
3899043476fSAl Viro 		return PTR_ERR(head);
3909043476fSAl Viro 
3919043476fSAl Viro 	generic_fillattr(inode, stat);
3929043476fSAl Viro 	if (table)
3939043476fSAl Viro 		stat->mode = (stat->mode & S_IFMT) | table->mode;
3949043476fSAl Viro 
3959043476fSAl Viro 	sysctl_head_finish(head);
3969043476fSAl Viro 	return 0;
3979043476fSAl Viro }
3989043476fSAl Viro 
39977b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = {
400f1ecf068SLucas De Marchi 	.open		= proc_sys_open,
401f1ecf068SLucas De Marchi 	.poll		= proc_sys_poll,
40277b14db5SEric W. Biederman 	.read		= proc_sys_read,
40377b14db5SEric W. Biederman 	.write		= proc_sys_write,
4046038f373SArnd Bergmann 	.llseek		= default_llseek,
4059043476fSAl Viro };
4069043476fSAl Viro 
4079043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = {
408887df078SPavel Emelyanov 	.read		= generic_read_dir,
40977b14db5SEric W. Biederman 	.readdir	= proc_sys_readdir,
4103222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
41177b14db5SEric W. Biederman };
41277b14db5SEric W. Biederman 
41303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = {
4149043476fSAl Viro 	.permission	= proc_sys_permission,
4159043476fSAl Viro 	.setattr	= proc_sys_setattr,
4169043476fSAl Viro 	.getattr	= proc_sys_getattr,
4179043476fSAl Viro };
4189043476fSAl Viro 
4199043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = {
42077b14db5SEric W. Biederman 	.lookup		= proc_sys_lookup,
42177b14db5SEric W. Biederman 	.permission	= proc_sys_permission,
42277b14db5SEric W. Biederman 	.setattr	= proc_sys_setattr,
4239043476fSAl Viro 	.getattr	= proc_sys_getattr,
42477b14db5SEric W. Biederman };
42577b14db5SEric W. Biederman 
42677b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
42777b14db5SEric W. Biederman {
42834286d66SNick Piggin 	if (nd->flags & LOOKUP_RCU)
42934286d66SNick Piggin 		return -ECHILD;
4309043476fSAl Viro 	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
4319043476fSAl Viro }
4329043476fSAl Viro 
433fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry)
4349043476fSAl Viro {
4359043476fSAl Viro 	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
4369043476fSAl Viro }
4379043476fSAl Viro 
438621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent,
439621e155aSNick Piggin 		const struct inode *pinode,
440621e155aSNick Piggin 		const struct dentry *dentry, const struct inode *inode,
441621e155aSNick Piggin 		unsigned int len, const char *str, const struct qstr *name)
4429043476fSAl Viro {
443dfef6dcdSAl Viro 	struct ctl_table_header *head;
44431e6b01fSNick Piggin 	/* Although proc doesn't have negative dentries, rcu-walk means
44531e6b01fSNick Piggin 	 * that inode here can be NULL */
446dfef6dcdSAl Viro 	/* AV: can it, indeed? */
44731e6b01fSNick Piggin 	if (!inode)
448dfef6dcdSAl Viro 		return 1;
449621e155aSNick Piggin 	if (name->len != len)
4509043476fSAl Viro 		return 1;
451621e155aSNick Piggin 	if (memcmp(name->name, str, len))
4529043476fSAl Viro 		return 1;
453dfef6dcdSAl Viro 	head = rcu_dereference(PROC_I(inode)->sysctl);
454dfef6dcdSAl Viro 	return !head || !sysctl_is_seen(head);
45577b14db5SEric W. Biederman }
45677b14db5SEric W. Biederman 
457d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = {
45877b14db5SEric W. Biederman 	.d_revalidate	= proc_sys_revalidate,
4599043476fSAl Viro 	.d_delete	= proc_sys_delete,
4609043476fSAl Viro 	.d_compare	= proc_sys_compare,
46177b14db5SEric W. Biederman };
46277b14db5SEric W. Biederman 
4631e0edd3fSAlexey Dobriyan int __init proc_sys_init(void)
46477b14db5SEric W. Biederman {
465e1675231SAlexey Dobriyan 	struct proc_dir_entry *proc_sys_root;
466e1675231SAlexey Dobriyan 
46777b14db5SEric W. Biederman 	proc_sys_root = proc_mkdir("sys", NULL);
4689043476fSAl Viro 	proc_sys_root->proc_iops = &proc_sys_dir_operations;
4699043476fSAl Viro 	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
47077b14db5SEric W. Biederman 	proc_sys_root->nlink = 0;
471de4e83bdSEric W. Biederman 
472de4e83bdSEric W. Biederman 	return sysctl_init();
47377b14db5SEric W. Biederman }
474