device_cgroup.c 20.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
L
Lai Jiangshan 已提交
3
 * device_cgroup.c - device cgroup subsystem
4 5 6 7 8 9 10 11 12
 *
 * Copyright 2007 IBM Corp
 */

#include <linux/device_cgroup.h>
#include <linux/cgroup.h>
#include <linux/ctype.h>
#include <linux/list.h>
#include <linux/uaccess.h>
13
#include <linux/seq_file.h>
14
#include <linux/slab.h>
L
Lai Jiangshan 已提交
15
#include <linux/rcupdate.h>
L
Li Zefan 已提交
16
#include <linux/mutex.h>
17

L
Li Zefan 已提交
18 19
static DEFINE_MUTEX(devcgroup_mutex);

20 21 22 23 24 25
enum devcg_behavior {
	DEVCG_DEFAULT_NONE,
	DEVCG_DEFAULT_ALLOW,
	DEVCG_DEFAULT_DENY,
};

26
/*
27
 * exception list locking rules:
L
Li Zefan 已提交
28
 * hold devcgroup_mutex for update/read.
L
Lai Jiangshan 已提交
29
 * hold rcu_read_lock() for read.
30 31
 */

32
struct dev_exception_item {
33 34 35 36
	u32 major, minor;
	short type;
	short access;
	struct list_head list;
37
	struct rcu_head rcu;
38 39 40 41
};

struct dev_cgroup {
	struct cgroup_subsys_state css;
42
	struct list_head exceptions;
43
	enum devcg_behavior behavior;
44 45
};

46 47
static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
{
48
	return s ? container_of(s, struct dev_cgroup, css) : NULL;
49 50
}

51 52
static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
{
53
	return css_to_devcgroup(task_css(task, devices_cgrp_id));
54 55
}

56
/*
L
Li Zefan 已提交
57
 * called under devcgroup_mutex
58
 */
59
static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
60
{
61
	struct dev_exception_item *ex, *tmp, *new;
62

T
Tejun Heo 已提交
63 64
	lockdep_assert_held(&devcgroup_mutex);

65 66
	list_for_each_entry(ex, orig, list) {
		new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
67 68 69 70 71 72 73 74
		if (!new)
			goto free_and_exit;
		list_add_tail(&new->list, dest);
	}

	return 0;

free_and_exit:
75 76 77
	list_for_each_entry_safe(ex, tmp, dest, list) {
		list_del(&ex->list);
		kfree(ex);
78 79 80 81 82
	}
	return -ENOMEM;
}

/*
L
Li Zefan 已提交
83
 * called under devcgroup_mutex
84
 */
85 86
static int dev_exception_add(struct dev_cgroup *dev_cgroup,
			     struct dev_exception_item *ex)
87
{
88
	struct dev_exception_item *excopy, *walk;
89

T
Tejun Heo 已提交
90 91
	lockdep_assert_held(&devcgroup_mutex);

92 93
	excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
	if (!excopy)
94 95
		return -ENOMEM;

96 97
	list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
		if (walk->type != ex->type)
98
			continue;
99
		if (walk->major != ex->major)
100
			continue;
101
		if (walk->minor != ex->minor)
102 103
			continue;

104 105 106
		walk->access |= ex->access;
		kfree(excopy);
		excopy = NULL;
107 108
	}

109 110
	if (excopy != NULL)
		list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
111 112 113 114
	return 0;
}

/*
L
Li Zefan 已提交
115
 * called under devcgroup_mutex
116
 */
117 118
static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
			     struct dev_exception_item *ex)
119
{
120
	struct dev_exception_item *walk, *tmp;
121

T
Tejun Heo 已提交
122 123
	lockdep_assert_held(&devcgroup_mutex);

124 125
	list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
		if (walk->type != ex->type)
126
			continue;
127
		if (walk->major != ex->major)
128
			continue;
129
		if (walk->minor != ex->minor)
130 131
			continue;

132
		walk->access &= ~ex->access;
133
		if (!walk->access) {
134
			list_del_rcu(&walk->list);
135
			kfree_rcu(walk, rcu);
136 137 138 139
		}
	}
}

140 141 142 143 144 145 146 147 148 149
static void __dev_exception_clean(struct dev_cgroup *dev_cgroup)
{
	struct dev_exception_item *ex, *tmp;

	list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
		list_del_rcu(&ex->list);
		kfree_rcu(ex, rcu);
	}
}

150
/**
151 152
 * dev_exception_clean - frees all entries of the exception list
 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
153 154 155
 *
 * called under devcgroup_mutex
 */
156
static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
157
{
T
Tejun Heo 已提交
158 159
	lockdep_assert_held(&devcgroup_mutex);

160
	__dev_exception_clean(dev_cgroup);
161 162
}

163 164 165 166 167
static inline bool is_devcg_online(const struct dev_cgroup *devcg)
{
	return (devcg->behavior != DEVCG_DEFAULT_NONE);
}

168 169 170
/**
 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
 * 		      parent's
171
 * @css: css getting online
172 173
 * returns 0 in case of success, error code otherwise
 */
174
static int devcgroup_online(struct cgroup_subsys_state *css)
175
{
176
	struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
T
Tejun Heo 已提交
177
	struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css->parent);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	int ret = 0;

	mutex_lock(&devcgroup_mutex);

	if (parent_dev_cgroup == NULL)
		dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
	else {
		ret = dev_exceptions_copy(&dev_cgroup->exceptions,
					  &parent_dev_cgroup->exceptions);
		if (!ret)
			dev_cgroup->behavior = parent_dev_cgroup->behavior;
	}
	mutex_unlock(&devcgroup_mutex);

	return ret;
}

195
static void devcgroup_offline(struct cgroup_subsys_state *css)
196
{
197
	struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
198 199 200 201 202 203

	mutex_lock(&devcgroup_mutex);
	dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
	mutex_unlock(&devcgroup_mutex);
}

204 205 206
/*
 * called from kernel/cgroup.c with cgroup_lock() held.
 */
207 208
static struct cgroup_subsys_state *
devcgroup_css_alloc(struct cgroup_subsys_state *parent_css)
209
{
210
	struct dev_cgroup *dev_cgroup;
211 212 213 214

	dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
	if (!dev_cgroup)
		return ERR_PTR(-ENOMEM);
215
	INIT_LIST_HEAD(&dev_cgroup->exceptions);
216
	dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
217 218 219 220

	return &dev_cgroup->css;
}

221
static void devcgroup_css_free(struct cgroup_subsys_state *css)
222
{
223
	struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
224

225
	__dev_exception_clean(dev_cgroup);
226 227 228 229 230
	kfree(dev_cgroup);
}

#define DEVCG_ALLOW 1
#define DEVCG_DENY 2
231 232
#define DEVCG_LIST 3

233
#define MAJMINLEN 13
234
#define ACCLEN 4
235 236 237 238

static void set_access(char *acc, short access)
{
	int idx = 0;
239
	memset(acc, 0, ACCLEN);
240
	if (access & DEVCG_ACC_READ)
241
		acc[idx++] = 'r';
242
	if (access & DEVCG_ACC_WRITE)
243
		acc[idx++] = 'w';
244
	if (access & DEVCG_ACC_MKNOD)
245 246 247 248 249
		acc[idx++] = 'm';
}

static char type_to_char(short type)
{
250
	if (type == DEVCG_DEV_ALL)
251
		return 'a';
252
	if (type == DEVCG_DEV_CHAR)
253
		return 'c';
254
	if (type == DEVCG_DEV_BLOCK)
255 256 257 258
		return 'b';
	return 'X';
}

259
static void set_majmin(char *str, unsigned m)
260 261
{
	if (m == ~0)
L
Li Zefan 已提交
262
		strcpy(str, "*");
263
	else
L
Li Zefan 已提交
264
		sprintf(str, "%u", m);
265 266
}

267
static int devcgroup_seq_show(struct seq_file *m, void *v)
268
{
269
	struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
270
	struct dev_exception_item *ex;
271
	char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
272

273
	rcu_read_lock();
274 275 276 277 278 279
	/*
	 * To preserve the compatibility:
	 * - Only show the "all devices" when the default policy is to allow
	 * - List the exceptions in case the default policy is to deny
	 * This way, the file remains as a "whitelist of devices"
	 */
280
	if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
281
		set_access(acc, DEVCG_ACC_MASK);
282 283
		set_majmin(maj, ~0);
		set_majmin(min, ~0);
284
		seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
285
			   maj, min, acc);
286
	} else {
287 288 289 290 291
		list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
			set_access(acc, ex->access);
			set_majmin(maj, ex->major);
			set_majmin(min, ex->minor);
			seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
292 293
				   maj, min, acc);
		}
294
	}
295
	rcu_read_unlock();
296

297
	return 0;
298 299
}

300
/**
301
 * match_exception	- iterates the exception list trying to find a complete match
302
 * @exceptions: list of exceptions
303
 * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR)
304 305
 * @major: device file major number, ~0 to match all
 * @minor: device file minor number, ~0 to match all
306
 * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD)
307
 *
308 309 310 311
 * It is considered a complete match if an exception is found that will
 * contain the entire range of provided parameters.
 *
 * Return: true in case it matches an exception completely
312
 */
313 314
static bool match_exception(struct list_head *exceptions, short type,
			    u32 major, u32 minor, short access)
315
{
316
	struct dev_exception_item *ex;
317

318
	list_for_each_entry_rcu(ex, exceptions, list) {
319
		if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK))
320
			continue;
321
		if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR))
322 323 324 325 326 327 328 329 330 331 332 333 334 335
			continue;
		if (ex->major != ~0 && ex->major != major)
			continue;
		if (ex->minor != ~0 && ex->minor != minor)
			continue;
		/* provided access cannot have more than the exception rule */
		if (access & (~ex->access))
			continue;
		return true;
	}
	return false;
}

/**
336
 * match_exception_partial - iterates the exception list trying to find a partial match
337
 * @exceptions: list of exceptions
338
 * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR)
339 340
 * @major: device file major number, ~0 to match all
 * @minor: device file minor number, ~0 to match all
341
 * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD)
342
 *
343 344 345 346 347 348
 * It is considered a partial match if an exception's range is found to
 * contain *any* of the devices specified by provided parameters. This is
 * used to make sure no extra access is being granted that is forbidden by
 * any of the exception list.
 *
 * Return: true in case the provided range mat matches an exception completely
349 350 351 352 353
 */
static bool match_exception_partial(struct list_head *exceptions, short type,
				    u32 major, u32 minor, short access)
{
	struct dev_exception_item *ex;
T
Tejun Heo 已提交
354

355
	list_for_each_entry_rcu(ex, exceptions, list) {
356
		if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK))
357
			continue;
358
		if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR))
359
			continue;
360 361 362 363 364
		/*
		 * We must be sure that both the exception and the provided
		 * range aren't masking all devices
		 */
		if (ex->major != ~0 && major != ~0 && ex->major != major)
365
			continue;
366
		if (ex->minor != ~0 && minor != ~0 && ex->minor != minor)
367
			continue;
368 369 370 371 372 373
		/*
		 * In order to make sure the provided range isn't matching
		 * an exception, all its access bits shouldn't match the
		 * exception's access bits
		 */
		if (!(access & ex->access))
374
			continue;
375
		return true;
376
	}
377 378 379 380
	return false;
}

/**
381
 * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
382 383 384
 * @dev_cgroup: dev cgroup to be tested against
 * @refex: new exception
 * @behavior: behavior of the exception's dev_cgroup
385 386 387
 *
 * This is used to make sure a child cgroup won't have more privileges
 * than its parent
388 389 390 391 392 393 394
 */
static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
		          struct dev_exception_item *refex,
		          enum devcg_behavior behavior)
{
	bool match = false;

395
	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
396
			 !lockdep_is_held(&devcgroup_mutex),
397
			 "device_cgroup:verify_new_ex called without proper synchronization");
398

399 400
	if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
		if (behavior == DEVCG_DEFAULT_ALLOW) {
401 402 403 404
			/*
			 * new exception in the child doesn't matter, only
			 * adding extra restrictions
			 */ 
405 406
			return true;
		} else {
407 408 409 410 411 412 413 414 415 416 417
			/*
			 * new exception in the child will add more devices
			 * that can be acessed, so it can't match any of
			 * parent's exceptions, even slightly
			 */ 
			match = match_exception_partial(&dev_cgroup->exceptions,
							refex->type,
							refex->major,
							refex->minor,
							refex->access);

418 419
			if (match)
				return false;
420
			return true;
421
		}
422
	} else {
423 424 425 426 427 428 429 430 431 432
		/*
		 * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
		 * the new exception will add access to more devices and must
		 * be contained completely in an parent's exception to be
		 * allowed
		 */
		match = match_exception(&dev_cgroup->exceptions, refex->type,
					refex->major, refex->minor,
					refex->access);

433 434
		if (match)
			/* parent has an exception that matches the proposed */
435
			return true;
436 437
		else
			return false;
438 439
	}
	return false;
440 441 442 443
}

/*
 * parent_has_perm:
444
 * when adding a new allow rule to a device exception list, the rule
445 446
 * must be allowed in the parent device
 */
447
static int parent_has_perm(struct dev_cgroup *childcg,
448
				  struct dev_exception_item *ex)
449
{
T
Tejun Heo 已提交
450
	struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
451

T
Tejun Heo 已提交
452
	if (!parent)
453
		return 1;
454
	return verify_new_ex(parent, ex, childcg->behavior);
455 456
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470
/**
 * parent_allows_removal - verify if it's ok to remove an exception
 * @childcg: child cgroup from where the exception will be removed
 * @ex: exception being removed
 *
 * When removing an exception in cgroups with default ALLOW policy, it must
 * be checked if removing it will give the child cgroup more access than the
 * parent.
 *
 * Return: true if it's ok to remove exception, false otherwise
 */
static bool parent_allows_removal(struct dev_cgroup *childcg,
				  struct dev_exception_item *ex)
{
T
Tejun Heo 已提交
471
	struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

	if (!parent)
		return true;

	/* It's always allowed to remove access to devices */
	if (childcg->behavior == DEVCG_DEFAULT_DENY)
		return true;

	/*
	 * Make sure you're not removing part or a whole exception existing in
	 * the parent cgroup
	 */
	return !match_exception_partial(&parent->exceptions, ex->type,
					ex->major, ex->minor, ex->access);
}

488 489 490 491 492 493 494 495
/**
 * may_allow_all - checks if it's possible to change the behavior to
 *		   allow based on parent's rules.
 * @parent: device cgroup's parent
 * returns: != 0 in case it's allowed, 0 otherwise
 */
static inline int may_allow_all(struct dev_cgroup *parent)
{
496 497
	if (!parent)
		return 1;
498 499 500
	return parent->behavior == DEVCG_DEFAULT_ALLOW;
}

501 502 503 504 505 506 507 508 509 510 511
/**
 * revalidate_active_exceptions - walks through the active exception list and
 * 				  revalidates the exceptions based on parent's
 * 				  behavior and exceptions. The exceptions that
 * 				  are no longer valid will be removed.
 * 				  Called with devcgroup_mutex held.
 * @devcg: cgroup which exceptions will be checked
 *
 * This is one of the three key functions for hierarchy implementation.
 * This function is responsible for re-evaluating all the cgroup's active
 * exceptions due to a parent's exception change.
512
 * Refer to Documentation/cgroup-v1/devices.txt for more details.
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
 */
static void revalidate_active_exceptions(struct dev_cgroup *devcg)
{
	struct dev_exception_item *ex;
	struct list_head *this, *tmp;

	list_for_each_safe(this, tmp, &devcg->exceptions) {
		ex = container_of(this, struct dev_exception_item, list);
		if (!parent_has_perm(devcg, ex))
			dev_exception_rm(devcg, ex);
	}
}

/**
 * propagate_exception - propagates a new exception to the children
 * @devcg_root: device cgroup that added a new exception
 * @ex: new exception to be propagated
 *
 * returns: 0 in case of success, != 0 in case of error
 */
static int propagate_exception(struct dev_cgroup *devcg_root,
			       struct dev_exception_item *ex)
{
536
	struct cgroup_subsys_state *pos;
537 538
	int rc = 0;

539
	rcu_read_lock();
540

541 542
	css_for_each_descendant_pre(pos, &devcg_root->css) {
		struct dev_cgroup *devcg = css_to_devcgroup(pos);
543 544 545 546 547 548 549

		/*
		 * Because devcgroup_mutex is held, no devcg will become
		 * online or offline during the tree walk (see on/offline
		 * methods), and online ones are safe to access outside RCU
		 * read lock without bumping refcnt.
		 */
550
		if (pos == &devcg_root->css || !is_devcg_online(devcg))
551 552 553
			continue;

		rcu_read_unlock();
554 555 556 557 558 559 560 561 562

		/*
		 * in case both root's behavior and devcg is allow, a new
		 * restriction means adding to the exception list
		 */
		if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
		    devcg->behavior == DEVCG_DEFAULT_ALLOW) {
			rc = dev_exception_add(devcg, ex);
			if (rc)
563
				return rc;
564 565 566 567 568 569 570 571 572 573 574
		} else {
			/*
			 * in the other possible cases:
			 * root's behavior: allow, devcg's: deny
			 * root's behavior: deny, devcg's: deny
			 * the exception will be removed
			 */
			dev_exception_rm(devcg, ex);
		}
		revalidate_active_exceptions(devcg);

575
		rcu_read_lock();
576
	}
577 578

	rcu_read_unlock();
579 580 581
	return rc;
}

582
/*
583
 * Modify the exception list using allow/deny rules.
584 585
 * CAP_SYS_ADMIN is needed for this.  It's at least separate from CAP_MKNOD
 * so we can give a container CAP_MKNOD to let it create devices but not
586
 * modify the exception list.
587 588
 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
 * us to also grant CAP_SYS_ADMIN to containers without giving away the
589
 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
590 591 592 593 594
 *
 * Taking rules away is always allowed (given CAP_SYS_ADMIN).  Granting
 * new access is only allowed if you're in the top-level cgroup, or your
 * parent cgroup has the access you're asking for.
 */
595
static int devcgroup_update_access(struct dev_cgroup *devcgroup,
596
				   int filetype, char *buffer)
597
{
598
	const char *b;
599
	char temp[12];		/* 11 + 1 characters needed for a u32 */
600
	int count, rc = 0;
601
	struct dev_exception_item ex;
T
Tejun Heo 已提交
602
	struct dev_cgroup *parent = css_to_devcgroup(devcgroup->css.parent);
603 604 605 606

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

607
	memset(&ex, 0, sizeof(ex));
608 609 610 611
	b = buffer;

	switch (*b) {
	case 'a':
612 613
		switch (filetype) {
		case DEVCG_ALLOW:
614
			if (css_has_online_children(&devcgroup->css))
615 616
				return -EINVAL;

617
			if (!may_allow_all(parent))
618
				return -EPERM;
619
			dev_exception_clean(devcgroup);
620 621 622 623
			devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
			if (!parent)
				break;

624 625 626 627
			rc = dev_exceptions_copy(&devcgroup->exceptions,
						 &parent->exceptions);
			if (rc)
				return rc;
628 629
			break;
		case DEVCG_DENY:
630
			if (css_has_online_children(&devcgroup->css))
631 632
				return -EINVAL;

633
			dev_exception_clean(devcgroup);
634
			devcgroup->behavior = DEVCG_DEFAULT_DENY;
635 636 637 638 639
			break;
		default:
			return -EINVAL;
		}
		return 0;
640
	case 'b':
641
		ex.type = DEVCG_DEV_BLOCK;
642 643
		break;
	case 'c':
644
		ex.type = DEVCG_DEV_CHAR;
645 646
		break;
	default:
647
		return -EINVAL;
648 649
	}
	b++;
650 651
	if (!isspace(*b))
		return -EINVAL;
652 653
	b++;
	if (*b == '*') {
654
		ex.major = ~0;
655 656
		b++;
	} else if (isdigit(*b)) {
657 658 659 660 661 662 663 664 665 666
		memset(temp, 0, sizeof(temp));
		for (count = 0; count < sizeof(temp) - 1; count++) {
			temp[count] = *b;
			b++;
			if (!isdigit(*b))
				break;
		}
		rc = kstrtou32(temp, 10, &ex.major);
		if (rc)
			return -EINVAL;
667
	} else {
668
		return -EINVAL;
669
	}
670 671
	if (*b != ':')
		return -EINVAL;
672 673 674 675
	b++;

	/* read minor */
	if (*b == '*') {
676
		ex.minor = ~0;
677 678
		b++;
	} else if (isdigit(*b)) {
679 680 681 682 683 684 685 686 687 688
		memset(temp, 0, sizeof(temp));
		for (count = 0; count < sizeof(temp) - 1; count++) {
			temp[count] = *b;
			b++;
			if (!isdigit(*b))
				break;
		}
		rc = kstrtou32(temp, 10, &ex.minor);
		if (rc)
			return -EINVAL;
689
	} else {
690
		return -EINVAL;
691
	}
692 693
	if (!isspace(*b))
		return -EINVAL;
694 695 696
	for (b++, count = 0; count < 3; count++, b++) {
		switch (*b) {
		case 'r':
697
			ex.access |= DEVCG_ACC_READ;
698 699
			break;
		case 'w':
700
			ex.access |= DEVCG_ACC_WRITE;
701 702
			break;
		case 'm':
703
			ex.access |= DEVCG_ACC_MKNOD;
704 705 706 707 708 709
			break;
		case '\n':
		case '\0':
			count = 3;
			break;
		default:
710
			return -EINVAL;
711 712 713 714 715
		}
	}

	switch (filetype) {
	case DEVCG_ALLOW:
716 717 718 719 720
		/*
		 * If the default policy is to allow by default, try to remove
		 * an matching exception instead. And be silent about it: we
		 * don't want to break compatibility
		 */
721
		if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
722 723 724
			/* Check if the parent allows removing it first */
			if (!parent_allows_removal(devcgroup, &ex))
				return -EPERM;
725
			dev_exception_rm(devcgroup, &ex);
726
			break;
727
		}
728 729 730

		if (!parent_has_perm(devcgroup, &ex))
			return -EPERM;
731 732
		rc = dev_exception_add(devcgroup, &ex);
		break;
733
	case DEVCG_DENY:
734 735 736 737 738
		/*
		 * If the default policy is to deny by default, try to remove
		 * an matching exception instead. And be silent about it: we
		 * don't want to break compatibility
		 */
739
		if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
740
			dev_exception_rm(devcgroup, &ex);
741 742 743 744 745 746 747 748
		else
			rc = dev_exception_add(devcgroup, &ex);

		if (rc)
			break;
		/* we only propagate new restrictions */
		rc = propagate_exception(devcgroup, &ex);
		break;
749
	default:
750
		rc = -EINVAL;
751
	}
752
	return rc;
753
}
754

755 756
static ssize_t devcgroup_access_write(struct kernfs_open_file *of,
				      char *buf, size_t nbytes, loff_t off)
757 758
{
	int retval;
L
Li Zefan 已提交
759 760

	mutex_lock(&devcgroup_mutex);
761 762
	retval = devcgroup_update_access(css_to_devcgroup(of_css(of)),
					 of_cft(of)->private, strstrip(buf));
L
Li Zefan 已提交
763
	mutex_unlock(&devcgroup_mutex);
764
	return retval ?: nbytes;
765 766 767 768 769
}

static struct cftype dev_cgroup_files[] = {
	{
		.name = "allow",
770
		.write = devcgroup_access_write,
771 772 773 774
		.private = DEVCG_ALLOW,
	},
	{
		.name = "deny",
775
		.write = devcgroup_access_write,
776 777
		.private = DEVCG_DENY,
	},
778 779
	{
		.name = "list",
780
		.seq_show = devcgroup_seq_show,
781 782
		.private = DEVCG_LIST,
	},
783
	{ }	/* terminate */
784 785
};

786
struct cgroup_subsys devices_cgrp_subsys = {
787 788
	.css_alloc = devcgroup_css_alloc,
	.css_free = devcgroup_css_free,
789 790
	.css_online = devcgroup_online,
	.css_offline = devcgroup_offline,
791
	.legacy_cftypes = dev_cgroup_files,
792 793
};

794 795 796 797 798 799
/**
 * __devcgroup_check_permission - checks if an inode operation is permitted
 * @dev_cgroup: the dev cgroup to be tested against
 * @type: device type
 * @major: device major number
 * @minor: device minor number
800
 * @access: combination of DEVCG_ACC_WRITE, DEVCG_ACC_READ and DEVCG_ACC_MKNOD
801 802 803
 *
 * returns 0 on success, -EPERM case the operation is not permitted
 */
804 805
int __devcgroup_check_permission(short type, u32 major, u32 minor,
				 short access)
806
{
J
Jiri Slaby 已提交
807
	struct dev_cgroup *dev_cgroup;
808
	bool rc;
L
Li Zefan 已提交
809

810
	rcu_read_lock();
J
Jiri Slaby 已提交
811
	dev_cgroup = task_devcgroup(current);
812 813 814 815 816 817 818 819
	if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW)
		/* Can't match any of the exceptions, even partially */
		rc = !match_exception_partial(&dev_cgroup->exceptions,
					      type, major, minor, access);
	else
		/* Need to match completely one exception to be allowed */
		rc = match_exception(&dev_cgroup->exceptions, type, major,
				     minor, access);
820
	rcu_read_unlock();
821

822 823
	if (!rc)
		return -EPERM;
L
Li Zefan 已提交
824

825 826
	return 0;
}