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