aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc/proc_sysctl.c
blob: 4e57fcf85982f648ec29f7c0962a1cebae990388 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * /proc/sys support
 */

#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/security.h>
#include "internal.h"

static struct dentry_operations proc_sys_dentry_operations;
static const struct file_operations proc_sys_file_operations;
static struct inode_operations proc_sys_inode_operations;

static void proc_sys_refresh_inode(struct inode *inode, struct ctl_table *table)
{
	/* Refresh the cached information bits in the inode */
	if (table) {
		inode->i_uid = 0;
		inode->i_gid = 0;
		inode->i_mode = table->mode;
		if (table->proc_handler) {
			inode->i_mode |= S_IFREG;
			inode->i_nlink = 1;
		} else {
			inode->i_mode |= S_IFDIR;
			inode->i_nlink = 0;	/* It is too hard to figure out */
		}
	}
}

static struct inode *proc_sys_make_inode(struct inode *dir, struct ctl_table *table)
{
	struct inode *inode;
	struct proc_inode *dir_ei, *ei;
	int depth;

	inode = new_inode(dir->i_sb);
	if (!inode)
		goto out;

	/* A directory is always one deeper than it's parent */
	dir_ei = PROC_I(dir);
	depth = dir_ei->fd + 1;

	ei = PROC_I(inode);
	ei->fd = depth;
	inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
	inode->i_op = &proc_sys_inode_operations;
	inode->i_fop = &proc_sys_file_operations;
	inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
	proc_sys_refresh_inode(inode, table);
out:
	return inode;
}

static struct dentry *proc_sys_ancestor(struct dentry *dentry, int depth)
{
	for (;;) {
		struct proc_inode *ei;

		ei = PROC_I(dentry->d_inode);
		if (ei->fd == depth)
			break; /* found */

		dentry = dentry->d_parent;
	}
	return dentry;
}

static struct ctl_table *proc_sys_lookup_table_one(struct ctl_table *table,
							struct qstr *name)
{
	int len;
	for ( ; table->ctl_name || table->procname; table++) {

		if (!table->procname)
			continue;

		len = strlen(table->procname);
		if (len != name->len)
			continue;

		if (memcmp(table->procname, name->name, len) != 0)
			continue;

		/* I have a match */
		return table;
	}
	return NULL;
}

static struct ctl_table *proc_sys_lookup_table(struct dentry *dentry,
						struct ctl_table *table)
{
	struct dentry *ancestor;
	struct proc_inode *ei;
	int depth, i;

	ei = PROC_I(dentry->d_inode);
	depth = ei->fd;

	if (depth == 0)
		return table;

	for (i = 1; table && (i <= depth); i++) {
		ancestor = proc_sys_ancestor(dentry, i);
		table = proc_sys_lookup_table_one(table, &ancestor->d_name);
		if (table)
			table = table->child;
	}
	return table;

}
static struct ctl_table *proc_sys_lookup_entry(struct dentry *dparent,
						struct qstr *name,
						struct ctl_table *table)
{
	table = proc_sys_lookup_table(dparent, table);
	if (table)
		table = proc_sys_lookup_table_one(table, name);
	return table;
}

static struct ctl_table *do_proc_sys_lookup(struct dentry *parent,
						struct qstr *name,
						struct ctl_table_header **ptr)
{
	struct ctl_table_header *head;
	struct ctl_table *table = NULL;

	for (head = sysctl_head_next(NULL); head;
			head = sysctl_head_next(head)) {
		table = proc_sys_lookup_entry(parent, name, head->ctl_table);
		if (table)
			break;
	}
	*ptr = head;
	return table;
}

static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
					struct nameidata *nd)
{
	struct ctl_table_header *head;
	struct inode *inode;
	struct dentry *err;
	struct ctl_table *table;

	err = ERR_PTR(-ENOENT);
	table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
	if (!table)
		goto out;

	err = ERR_PTR(-ENOMEM);
	inode = proc_sys_make_inode(dir, table);
	if (!inode)
		goto out;

	err = NULL;
	dentry->d_op = &proc_sys_dentry_operations;
	d_add(dentry, inode);

out:
	sysctl_head_finish(head);
	return err;
}

static ssize_t proc_sys_read(struct file *filp, char __user *buf,
				size_t count, loff_t *ppos)
{
	struct dentry *dentry = filp->f_dentry;
	struct ctl_table_header *head;
	struct ctl_table *table;
	ssize_t error;
	size_t res;

	table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
	/* Has the sysctl entry disappeared on us? */
	error = -ENOENT;
	if (!table)
		goto out;

	/* Has the sysctl entry been replaced by a directory? */
	error = -EISDIR;
	if (!table->proc_handler)
		goto out;

	/*
	 * At this point we know that the sysctl was not unregistered
	 * and won't be until we finish.
	 */
	error = -EPERM;
	if (sysctl_perm(table, MAY_READ))
		goto out;

	/* careful: calling conventions are nasty here */
	res = count;
	error = table->proc_handler(table, 0, filp, buf, &res, ppos);
	if (!error)
		error = res;
out:
	sysctl_head_finish(head);

	return error;
}

static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
				size_t count, loff_t *ppos)
{
	struct dentry *dentry = filp->f_dentry;
	struct ctl_table_header *head;
	struct ctl_table *table;
	ssize_t error;
	size_t res;

	table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
	/* Has the sysctl entry disappeared on us? */
	error = -ENOENT;
	if (!table)
		goto out;

	/* Has the sysctl entry been replaced by a directory? */
	error = -EISDIR;
	if (!table->proc_handler)
		goto out;

	/*
	 * At this point we know that the sysctl was not unregistered
	 * and won't be until we finish.
	 */
	error = -EPERM;
	if (sysctl_perm(table, MAY_WRITE))
		goto out;

	/* careful: calling conventions are nasty here */
	res = count;
	error = table->proc_handler(table, 1, filp, (char __user *)buf,
				    &res, ppos);
	if (!error)
		error = res;
out:
	sysctl_head_finish(head);

	return error;
}


static int proc_sys_fill_cache(struct file *filp, void *dirent,
				filldir_t filldir, struct ctl_table *table)
{
	struct ctl_table_header *head;
	struct ctl_table *child_table = NULL;
	struct dentry *child, *dir = filp->f_path.dentry;
	struct inode *inode;
	struct qstr qname;
	ino_t ino = 0;
	unsigned type = DT_UNKNOWN;
	int ret;

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

	/* Suppress duplicates.
	 * Only fill a directory entry if it is the value that
	 * an ordinary lookup of that name returns.  Hide all
	 * others.
	 *
	 * If we ever cache this translation in the dcache
	 * I should do a dcache lookup first.  But for now
	 * it is just simpler not to.
	 */
	ret = 0;
	child_table = do_proc_sys_lookup(dir, &qname, &head);
	sysctl_head_finish(head);
	if (child_table != table)
		return 0;

	child = d_lookup(dir, &qname);
	if (!child) {
		struct dentry *new;
		new = d_alloc(dir, &qname);
		if (new) {
			inode = proc_sys_make_inode(dir->d_inode, table);
			if (!inode)
				child = ERR_PTR(-ENOMEM);
			else {
				new->d_op = &proc_sys_dentry_operations;
				d_add(new, inode);
			}
			if (child)
				dput(new);
			else
				child = new;
		}
	}
	if (!child || IS_ERR(child) || !child->d_inode)
		goto end_instantiate;
	inode = child->d_inode;
	if (inode) {
		ino  = inode->i_ino;
		type = inode->i_mode >> 12;
	}
	dput(child);
end_instantiate:
	if (!ino)
		ino= find_inode_number(dir, &qname);
	if (!ino)
		ino = 1;
	return filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
}

static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
	struct dentry *dentry = filp->f_dentry;
	struct inode *inode = dentry->d_inode;
	struct ctl_table_header *head = NULL;
	struct ctl_table *table;
	unsigned long pos;
	int ret;

	ret = -ENOTDIR;
	if (!S_ISDIR(inode->i_mode))
		goto out;

	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;

	/* - Find each instance of the directory
	 * - Read all entries in each instance
	 * - Before returning an entry to user space lookup the entry
	 *   by name and if I find a different entry don't return
	 *   this one because it means it is a buried dup.
	 * For sysctl this should only happen for directory entries.
	 */
	for (head = sysctl_head_next(NULL); head; head = sysctl_head_next(head)) {
		table = proc_sys_lookup_table(dentry, head->ctl_table);

		if (!table)
			continue;

		for (; table->ctl_name || table->procname; table++, pos++) {
			/* Can't do anything without a proc name */
			if (!table->procname)
				continue;

			if (pos < filp->f_pos)
				continue;

			if (proc_sys_fill_cache(filp, dirent, filldir, table) < 0)
				goto out;
			filp->f_pos = pos + 1;
		}
	}
	ret = 1;
out:
	sysctl_head_finish(head);
	return ret;
}

static int proc_sys_permission(struct inode *inode, int mask, struct nameidata *nd)
{
	/*
	 * sysctl entries that are not writeable,
	 * are _NOT_ writeable, capabilities or not.
	 */
	struct ctl_table_header *head;
	struct ctl_table *table;
	struct dentry *dentry;
	int mode;
	int depth;
	int error;

	head = NULL;
	depth = PROC_I(inode)->fd;

	/* First check the cached permissions, in case we don't have
	 * enough information to lookup the sysctl table entry.
	 */
	error = -EACCES;
	mode = inode->i_mode;

	if (current->euid == 0)
		mode >>= 6;
	else if (in_group_p(0))
		mode >>= 3;

	if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
		error = 0;

	/* If we can't get a sysctl table entry the permission
	 * checks on the cached mode will have to be enough.
	 */
	if (!nd || !depth)
		goto out;

	dentry = nd->dentry;
	table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);

	/* If the entry does not exist deny permission */
	error = -EACCES;
	if (!table)
		goto out;

	/* Use the permissions on the sysctl table entry */
	error = sysctl_perm(table, mask);
out:
	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);
	if (!error)
		error = inode_setattr(inode, attr);

	return error;
}

/* I'm lazy and don't distinguish between files and directories,
 * until access time.
 */
static const struct file_operations proc_sys_file_operations = {
	.read		= proc_sys_read,
	.write		= proc_sys_write,
	.readdir	= proc_sys_readdir,
};

static struct inode_operations proc_sys_inode_operations = {
	.lookup		= proc_sys_lookup,
	.permission	= proc_sys_permission,
	.setattr	= proc_sys_setattr,
};

static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
{
	struct ctl_table_header *head;
	struct ctl_table *table;
	table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
	proc_sys_refresh_inode(dentry->d_inode, table);
	sysctl_head_finish(head);
	return !!table;
}

static struct dentry_operations proc_sys_dentry_operations = {
	.d_revalidate	= proc_sys_revalidate,
};

static struct proc_dir_entry *proc_sys_root;

int proc_sys_init(void)
{
	proc_sys_root = proc_mkdir("sys", NULL);
	proc_sys_root->proc_iops = &proc_sys_inode_operations;
	proc_sys_root->proc_fops = &proc_sys_file_operations;
	proc_sys_root->nlink = 0;
	return 0;
}