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

A
Al Viro 已提交
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;
A
Al Viro 已提交
15 16
static const struct file_operations proc_sys_dir_file_operations;
static const struct inode_operations proc_sys_dir_operations;
17

L
Lucas De Marchi 已提交
18 19 20 21 22 23 24 25 26
void proc_sys_poll_notify(struct ctl_table_poll *poll)
{
	if (!poll)
		return;

	atomic_inc(&poll->event);
	wake_up_interruptible(&poll->wait);
}

A
Al Viro 已提交
27 28
static struct inode *proc_sys_make_inode(struct super_block *sb,
		struct ctl_table_header *head, struct ctl_table *table)
29 30
{
	struct inode *inode;
A
Al Viro 已提交
31
	struct proc_inode *ei;
32

A
Al Viro 已提交
33
	inode = new_inode(sb);
34 35 36
	if (!inode)
		goto out;

37 38
	inode->i_ino = get_next_ino();

A
Al Viro 已提交
39
	sysctl_head_get(head);
40
	ei = PROC_I(inode);
A
Al Viro 已提交
41 42 43
	ei->sysctl = head;
	ei->sysctl_entry = table;

44
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
A
Al Viro 已提交
45 46 47 48 49 50 51
	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;
52
		clear_nlink(inode);
A
Al Viro 已提交
53 54 55
		inode->i_op = &proc_sys_dir_operations;
		inode->i_fop = &proc_sys_dir_file_operations;
	}
56 57 58 59
out:
	return inode;
}

A
Al Viro 已提交
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 65
			continue;

66
		if (memcmp(p->procname, name->name, name->len) != 0)
67 68 69
			continue;

		/* I have a match */
A
Al Viro 已提交
70
		return p;
71 72 73 74
	}
	return NULL;
}

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

A
Al Viro 已提交
83 84 85 86 87 88 89 90 91 92
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);
93

A
Al Viro 已提交
94 95
	if (IS_ERR(head))
		return ERR_CAST(head);
96

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

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

A
Al Viro 已提交
104 105 106 107 108 109 110 111 112
	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;
		}
113 114
	}

A
Al Viro 已提交
115
	if (!p)
116 117 118
		goto out;

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

123 124 125 126
	if (!inode)
		goto out;

	err = NULL;
127
	d_set_d_op(dentry, &proc_sys_dentry_operations);
128 129 130 131 132 133 134
	d_add(dentry, inode);

out:
	sysctl_head_finish(head);
	return err;
}

135 136
static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
		size_t count, loff_t *ppos, int write)
137
{
A
Al Viro 已提交
138 139 140
	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;
141 142
	ssize_t error;
	size_t res;
143

A
Al Viro 已提交
144 145
	if (IS_ERR(head))
		return PTR_ERR(head);
146 147 148 149 150 151

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

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

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

	return error;
}

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

177 178 179 180
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);
181 182
}

L
Lucas De Marchi 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static int proc_sys_open(struct inode *inode, struct file *filp)
{
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;

	if (table->poll)
		filp->private_data = proc_sys_poll_event(table->poll);

	return 0;
}

static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
{
	struct inode *inode = filp->f_path.dentry->d_inode;
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
	unsigned long event = (unsigned long)filp->private_data;
	unsigned int ret = DEFAULT_POLLMASK;

	if (!table->proc_handler)
		goto out;

	if (!table->poll)
		goto out;

	poll_wait(filp, &table->poll->wait, wait);

	if (event != atomic_read(&table->poll->event)) {
		filp->private_data = proc_sys_poll_event(table->poll);
		ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
	}

out:
	return ret;
}
216 217

static int proc_sys_fill_cache(struct file *filp, void *dirent,
A
Al Viro 已提交
218 219 220
				filldir_t filldir,
				struct ctl_table_header *head,
				struct ctl_table *table)
221 222 223 224 225 226 227 228 229 230 231 232 233
{
	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 已提交
234 235 236 237 238 239 240
		child = d_alloc(dir, &qname);
		if (child) {
			inode = proc_sys_make_inode(dir->d_sb, head, table);
			if (!inode) {
				dput(child);
				return -ENOMEM;
			} else {
241
				d_set_d_op(child, &proc_sys_dentry_operations);
A
Al Viro 已提交
242
				d_add(child, inode);
243
			}
A
Al Viro 已提交
244 245
		} else {
			return -ENOMEM;
246 247 248
		}
	}
	inode = child->d_inode;
A
Al Viro 已提交
249 250
	ino  = inode->i_ino;
	type = inode->i_mode >> 12;
251
	dput(child);
A
Al Viro 已提交
252 253 254 255 256 257 258 259
	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)
{

260
	for (; table->procname; table++, (*pos)++) {
A
Al Viro 已提交
261 262 263 264 265 266 267 268 269 270 271 272
		int res;

		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;
273 274 275 276
}

static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
A
Al Viro 已提交
277
	struct dentry *dentry = filp->f_path.dentry;
278
	struct inode *inode = dentry->d_inode;
A
Al Viro 已提交
279 280 281
	struct ctl_table_header *head = grab_header(inode);
	struct ctl_table *table = PROC_I(inode)->sysctl_entry;
	struct ctl_table_header *h = NULL;
282
	unsigned long pos;
A
Al Viro 已提交
283 284 285 286
	int ret = -EINVAL;

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

A
Al Viro 已提交
288 289
	if (table && !table->child) {
		WARN_ON(1);
290
		goto out;
A
Al Viro 已提交
291 292 293
	}

	table = table ? table->child : head->ctl_table;
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

	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 已提交
311 312 313
	ret = scan(head, table, &pos, filp, dirent, filldir);
	if (ret)
		goto out;
314

A
Al Viro 已提交
315 316
	for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
		if (h->attached_to != table)
317
			continue;
A
Al Viro 已提交
318 319 320 321
		ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
		if (ret) {
			sysctl_head_finish(h);
			break;
322 323 324 325 326 327 328 329
		}
	}
	ret = 1;
out:
	sysctl_head_finish(head);
	return ret;
}

330
static int proc_sys_permission(struct inode *inode, int mask)
331 332 333 334 335
{
	/*
	 * sysctl entries that are not writeable,
	 * are _NOT_ writeable, capabilities or not.
	 */
336 337
	struct ctl_table_header *head;
	struct ctl_table *table;
338 339
	int error;

340 341 342 343 344
	/* 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 已提交
345 346
	if (IS_ERR(head))
		return PTR_ERR(head);
347

348
	table = PROC_I(inode)->sysctl_entry;
A
Al Viro 已提交
349 350 351
	if (!table) /* global root - r-xr-xr-x */
		error = mask & MAY_WRITE ? -EACCES : 0;
	else /* Use the permissions on the sysctl table entry */
352
		error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
353 354 355 356 357 358 359 360 361 362 363 364 365 366

	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 已提交
367 368 369 370 371 372 373 374 375
	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;
	}
376

C
Christoph Hellwig 已提交
377 378 379
	setattr_copy(inode, attr);
	mark_inode_dirty(inode);
	return 0;
380 381
}

A
Al Viro 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
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;
}

399
static const struct file_operations proc_sys_file_operations = {
L
Lucas De Marchi 已提交
400 401
	.open		= proc_sys_open,
	.poll		= proc_sys_poll,
402 403
	.read		= proc_sys_read,
	.write		= proc_sys_write,
404
	.llseek		= default_llseek,
A
Al Viro 已提交
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 = {
A
Al Viro 已提交
414 415 416 417 418 419
	.permission	= proc_sys_permission,
	.setattr	= proc_sys_setattr,
	.getattr	= proc_sys_getattr,
};

static const struct inode_operations proc_sys_dir_operations = {
420 421 422
	.lookup		= proc_sys_lookup,
	.permission	= proc_sys_permission,
	.setattr	= proc_sys_setattr,
A
Al Viro 已提交
423
	.getattr	= proc_sys_getattr,
424 425 426 427
};

static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
{
428 429
	if (nd->flags & LOOKUP_RCU)
		return -ECHILD;
A
Al Viro 已提交
430 431 432
	return !PROC_I(dentry->d_inode)->sysctl->unregistering;
}

N
Nick Piggin 已提交
433
static int proc_sys_delete(const struct dentry *dentry)
A
Al Viro 已提交
434 435 436 437
{
	return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
}

N
Nick Piggin 已提交
438 439 440 441
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 已提交
442
{
A
Al Viro 已提交
443
	struct ctl_table_header *head;
N
Nick Piggin 已提交
444 445
	/* Although proc doesn't have negative dentries, rcu-walk means
	 * that inode here can be NULL */
A
Al Viro 已提交
446
	/* AV: can it, indeed? */
N
Nick Piggin 已提交
447
	if (!inode)
A
Al Viro 已提交
448
		return 1;
N
Nick Piggin 已提交
449
	if (name->len != len)
A
Al Viro 已提交
450
		return 1;
N
Nick Piggin 已提交
451
	if (memcmp(name->name, str, len))
A
Al Viro 已提交
452
		return 1;
A
Al Viro 已提交
453 454
	head = rcu_dereference(PROC_I(inode)->sysctl);
	return !head || !sysctl_is_seen(head);
455 456
}

A
Al Viro 已提交
457
static const struct dentry_operations proc_sys_dentry_operations = {
458
	.d_revalidate	= proc_sys_revalidate,
A
Al Viro 已提交
459 460
	.d_delete	= proc_sys_delete,
	.d_compare	= proc_sys_compare,
461 462
};

A
Alexey Dobriyan 已提交
463
int __init proc_sys_init(void)
464
{
A
Alexey Dobriyan 已提交
465 466
	struct proc_dir_entry *proc_sys_root;

467
	proc_sys_root = proc_mkdir("sys", NULL);
A
Al Viro 已提交
468 469
	proc_sys_root->proc_iops = &proc_sys_dir_operations;
	proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
470
	proc_sys_root->nlink = 0;
471 472

	return sysctl_init();
473
}