proc_sysctl.c 10.0 KB
Newer Older
1 2 3
/*
 * /proc/sys support
 */
A
Alexey Dobriyan 已提交
4
#include <linux/init.h>
5 6 7
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/security.h>
8
#include <linux/namei.h>
9 10
#include "internal.h"

A
Al Viro 已提交
11
static const struct dentry_operations proc_sys_dentry_operations;
12
static const struct file_operations proc_sys_file_operations;
13
static const struct inode_operations proc_sys_inode_operations;
A
Al Viro 已提交
14 15
static const struct file_operations proc_sys_dir_file_operations;
static const struct inode_operations proc_sys_dir_operations;
16

A
Al Viro 已提交
17 18
static struct inode *proc_sys_make_inode(struct super_block *sb,
		struct ctl_table_header *head, struct ctl_table *table)
19 20
{
	struct inode *inode;
A
Al Viro 已提交
21
	struct proc_inode *ei;
22

A
Al Viro 已提交
23
	inode = new_inode(sb);
24 25 26
	if (!inode)
		goto out;

27 28
	inode->i_ino = get_next_ino();

A
Al Viro 已提交
29
	sysctl_head_get(head);
30
	ei = PROC_I(inode);
A
Al Viro 已提交
31 32 33
	ei->sysctl = head;
	ei->sysctl_entry = table;

34
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
35
	inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
A
Al Viro 已提交
36 37 38 39 40 41 42 43 44 45 46
	inode->i_mode = table->mode;
	if (!table->child) {
		inode->i_mode |= S_IFREG;
		inode->i_op = &proc_sys_inode_operations;
		inode->i_fop = &proc_sys_file_operations;
	} else {
		inode->i_mode |= S_IFDIR;
		inode->i_nlink = 0;
		inode->i_op = &proc_sys_dir_operations;
		inode->i_fop = &proc_sys_dir_file_operations;
	}
47 48 49 50
out:
	return inode;
}

A
Al Viro 已提交
51
static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
52 53
{
	int len;
54
	for ( ; p->procname; p++) {
55

A
Al Viro 已提交
56
		if (!p->procname)
57 58
			continue;

A
Al Viro 已提交
59
		len = strlen(p->procname);
60 61 62
		if (len != name->len)
			continue;

A
Al Viro 已提交
63
		if (memcmp(p->procname, name->name, len) != 0)
64 65 66
			continue;

		/* I have a match */
A
Al Viro 已提交
67
		return p;
68 69 70 71
	}
	return NULL;
}

A
Adrian Bunk 已提交
72
static struct ctl_table_header *grab_header(struct inode *inode)
73
{
A
Al Viro 已提交
74 75 76 77 78
	if (PROC_I(inode)->sysctl)
		return sysctl_head_grab(PROC_I(inode)->sysctl);
	else
		return sysctl_head_next(NULL);
}
79

A
Al Viro 已提交
80 81 82 83 84 85 86 87 88 89
static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
					struct nameidata *nd)
{
	struct ctl_table_header *head = grab_header(dir);
	struct ctl_table *table = PROC_I(dir)->sysctl_entry;
	struct ctl_table_header *h = NULL;
	struct qstr *name = &dentry->d_name;
	struct ctl_table *p;
	struct inode *inode;
	struct dentry *err = ERR_PTR(-ENOENT);
90

A
Al Viro 已提交
91 92
	if (IS_ERR(head))
		return ERR_CAST(head);
93

A
Al Viro 已提交
94 95 96
	if (table && !table->child) {
		WARN_ON(1);
		goto out;
97 98
	}

A
Al Viro 已提交
99
	table = table ? table->child : head->ctl_table;
100

A
Al Viro 已提交
101 102 103 104 105 106 107 108 109
	p = find_in_table(table, name);
	if (!p) {
		for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
			if (h->attached_to != table)
				continue;
			p = find_in_table(h->attached_by, name);
			if (p)
				break;
		}
110 111
	}

A
Al Viro 已提交
112
	if (!p)
113 114 115
		goto out;

	err = ERR_PTR(-ENOMEM);
A
Al Viro 已提交
116 117 118 119
	inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
	if (h)
		sysctl_head_finish(h);

120 121 122 123
	if (!inode)
		goto out;

	err = NULL;
124
	d_set_d_op(dentry, &proc_sys_dentry_operations);
125 126 127 128 129 130 131
	d_add(dentry, inode);

out:
	sysctl_head_finish(head);
	return err;
}

132 133
static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
		size_t count, loff_t *ppos, int write)
134
{
A
Al Viro 已提交
135 136 137
	struct inode *inode = filp->f_path.dentry->d_inode;
	struct ctl_table_header *head = grab_header(inode);
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
138 139
	ssize_t error;
	size_t res;
140

A
Al Viro 已提交
141 142
	if (IS_ERR(head))
		return PTR_ERR(head);
143 144 145 146 147 148

	/*
	 * At this point we know that the sysctl was not unregistered
	 * and won't be until we finish.
	 */
	error = -EPERM;
149
	if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
150 151
		goto out;

A
Al Viro 已提交
152 153 154 155 156
	/* if that can happen at all, it should be -EINVAL, not -EISDIR */
	error = -EINVAL;
	if (!table->proc_handler)
		goto out;

157 158
	/* careful: calling conventions are nasty here */
	res = count;
159
	error = table->proc_handler(table, write, buf, &res, ppos);
160 161 162 163 164 165 166 167
	if (!error)
		error = res;
out:
	sysctl_head_finish(head);

	return error;
}

168
static ssize_t proc_sys_read(struct file *filp, char __user *buf,
169 170
				size_t count, loff_t *ppos)
{
171 172
	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
}
173

174 175 176 177
static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
				size_t count, loff_t *ppos)
{
	return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
178 179 180 181
}


static int proc_sys_fill_cache(struct file *filp, void *dirent,
A
Al Viro 已提交
182 183 184
				filldir_t filldir,
				struct ctl_table_header *head,
				struct ctl_table *table)
185 186 187 188 189 190 191 192 193 194 195 196 197
{
	struct dentry *child, *dir = filp->f_path.dentry;
	struct inode *inode;
	struct qstr qname;
	ino_t ino = 0;
	unsigned type = DT_UNKNOWN;

	qname.name = table->procname;
	qname.len  = strlen(table->procname);
	qname.hash = full_name_hash(qname.name, qname.len);

	child = d_lookup(dir, &qname);
	if (!child) {
A
Al Viro 已提交
198 199 200 201 202 203 204
		child = d_alloc(dir, &qname);
		if (child) {
			inode = proc_sys_make_inode(dir->d_sb, head, table);
			if (!inode) {
				dput(child);
				return -ENOMEM;
			} else {
205
				d_set_d_op(child, &proc_sys_dentry_operations);
A
Al Viro 已提交
206
				d_add(child, inode);
207
			}
A
Al Viro 已提交
208 209
		} else {
			return -ENOMEM;
210 211 212
		}
	}
	inode = child->d_inode;
A
Al Viro 已提交
213 214
	ino  = inode->i_ino;
	type = inode->i_mode >> 12;
215
	dput(child);
A
Al Viro 已提交
216 217 218 219 220 221 222 223
	return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
}

static int scan(struct ctl_table_header *head, ctl_table *table,
		unsigned long *pos, struct file *file,
		void *dirent, filldir_t filldir)
{

224
	for (; table->procname; table++, (*pos)++) {
A
Al Viro 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		int res;

		/* Can't do anything without a proc name */
		if (!table->procname)
			continue;

		if (*pos < file->f_pos)
			continue;

		res = proc_sys_fill_cache(file, dirent, filldir, head, table);
		if (res)
			return res;

		file->f_pos = *pos + 1;
	}
	return 0;
241 242 243 244
}

static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
A
Al Viro 已提交
245
	struct dentry *dentry = filp->f_path.dentry;
246
	struct inode *inode = dentry->d_inode;
A
Al Viro 已提交
247 248 249
	struct ctl_table_header *head = grab_header(inode);
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
	struct ctl_table_header *h = NULL;
250
	unsigned long pos;
A
Al Viro 已提交
251 252 253 254
	int ret = -EINVAL;

	if (IS_ERR(head))
		return PTR_ERR(head);
255

A
Al Viro 已提交
256 257
	if (table && !table->child) {
		WARN_ON(1);
258
		goto out;
A
Al Viro 已提交
259 260 261
	}

	table = table ? table->child : head->ctl_table;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	ret = 0;
	/* Avoid a switch here: arm builds fail with missing __cmpdi2 */
	if (filp->f_pos == 0) {
		if (filldir(dirent, ".", 1, filp->f_pos,
				inode->i_ino, DT_DIR) < 0)
			goto out;
		filp->f_pos++;
	}
	if (filp->f_pos == 1) {
		if (filldir(dirent, "..", 2, filp->f_pos,
				parent_ino(dentry), DT_DIR) < 0)
			goto out;
		filp->f_pos++;
	}
	pos = 2;

A
Al Viro 已提交
279 280 281
	ret = scan(head, table, &pos, filp, dirent, filldir);
	if (ret)
		goto out;
282

A
Al Viro 已提交
283 284
	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
		if (h->attached_to != table)
285
			continue;
A
Al Viro 已提交
286 287 288 289
		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
		if (ret) {
			sysctl_head_finish(h);
			break;
290 291 292 293 294 295 296 297
		}
	}
	ret = 1;
out:
	sysctl_head_finish(head);
	return ret;
}

298
static int proc_sys_permission(struct inode *inode, int mask)
299 300 301 302 303
{
	/*
	 * sysctl entries that are not writeable,
	 * are _NOT_ writeable, capabilities or not.
	 */
304 305
	struct ctl_table_header *head;
	struct ctl_table *table;
306 307
	int error;

308 309 310 311 312
	/* Executable files are not allowed under /proc/sys/ */
	if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
		return -EACCES;

	head = grab_header(inode);
A
Al Viro 已提交
313 314
	if (IS_ERR(head))
		return PTR_ERR(head);
315

316
	table = PROC_I(inode)->sysctl_entry;
A
Al Viro 已提交
317 318 319 320
	if (!table) /* global root - r-xr-xr-x */
		error = mask & MAY_WRITE ? -EACCES : 0;
	else /* Use the permissions on the sysctl table entry */
		error = sysctl_perm(head->root, table, mask);
321 322 323 324 325 326 327 328 329 330 331 332 333 334

	sysctl_head_finish(head);
	return error;
}

static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
{
	struct inode *inode = dentry->d_inode;
	int error;

	if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
		return -EPERM;

	error = inode_change_ok(inode, attr);
C
Christoph Hellwig 已提交
335 336 337 338 339 340 341 342 343
	if (error)
		return error;

	if ((attr->ia_valid & ATTR_SIZE) &&
	    attr->ia_size != i_size_read(inode)) {
		error = vmtruncate(inode, attr->ia_size);
		if (error)
			return error;
	}
344

C
Christoph Hellwig 已提交
345 346 347
	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	return 0;
348 349
}

A
Al Viro 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
{
	struct inode *inode = dentry->d_inode;
	struct ctl_table_header *head = grab_header(inode);
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;

	if (IS_ERR(head))
		return PTR_ERR(head);

	generic_fillattr(inode, stat);
	if (table)
		stat->mode = (stat->mode & S_IFMT) | table->mode;

	sysctl_head_finish(head);
	return 0;
}

367 368 369
static const struct file_operations proc_sys_file_operations = {
	.read		= proc_sys_read,
	.write		= proc_sys_write,
370
	.llseek		= default_llseek,
A
Al Viro 已提交
371 372 373
};

static const struct file_operations proc_sys_dir_file_operations = {
374
	.readdir	= proc_sys_readdir,
375
	.llseek		= generic_file_llseek,
376 377
};

378
static const struct inode_operations proc_sys_inode_operations = {
A
Al Viro 已提交
379 380 381 382 383 384
	.permission	= proc_sys_permission,
	.setattr	= proc_sys_setattr,
	.getattr	= proc_sys_getattr,
};

static const struct inode_operations proc_sys_dir_operations = {
385 386 387
	.lookup		= proc_sys_lookup,
	.permission	= proc_sys_permission,
	.setattr	= proc_sys_setattr,
A
Al Viro 已提交
388
	.getattr	= proc_sys_getattr,
389 390 391 392
};

static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
{
393 394
	if (nd->flags & LOOKUP_RCU)
		return -ECHILD;
A
Al Viro 已提交
395 396 397
	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
}

N
Nick Piggin 已提交
398
static int proc_sys_delete(const struct dentry *dentry)
A
Al Viro 已提交
399 400 401 402
{
	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
}

N
Nick Piggin 已提交
403 404 405 406
static int proc_sys_compare(const struct dentry *parent,
		const struct inode *pinode,
		const struct dentry *dentry, const struct inode *inode,
		unsigned int len, const char *str, const struct qstr *name)
A
Al Viro 已提交
407
{
N
Nick Piggin 已提交
408 409 410 411
	/* Although proc doesn't have negative dentries, rcu-walk means
	 * that inode here can be NULL */
	if (!inode)
		return 0;
N
Nick Piggin 已提交
412
	if (name->len != len)
A
Al Viro 已提交
413
		return 1;
N
Nick Piggin 已提交
414
	if (memcmp(name->name, str, len))
A
Al Viro 已提交
415
		return 1;
N
Nick Piggin 已提交
416
	return !sysctl_is_seen(PROC_I(inode)->sysctl);
417 418
}

A
Al Viro 已提交
419
static const struct dentry_operations proc_sys_dentry_operations = {
420
	.d_revalidate	= proc_sys_revalidate,
A
Al Viro 已提交
421 422
	.d_delete	= proc_sys_delete,
	.d_compare	= proc_sys_compare,
423 424
};

A
Alexey Dobriyan 已提交
425
int __init proc_sys_init(void)
426
{
A
Alexey Dobriyan 已提交
427 428
	struct proc_dir_entry *proc_sys_root;

429
	proc_sys_root = proc_mkdir("sys", NULL);
A
Al Viro 已提交
430 431
	proc_sys_root->proc_iops = &proc_sys_dir_operations;
	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
432 433 434
	proc_sys_root->nlink = 0;
	return 0;
}