Skip to content

Commit b2daa1d

Browse files
aristeuJiri Slaby
authored andcommitted
device_cgroup: rework device access check and exception checking
commit 79d7197 upstream. Whenever a device file is opened and checked against current device cgroup rules, it uses the same function (may_access()) as when a new exception rule is added by writing devices.{allow,deny}. And in both cases, the algorithm is the same, doesn't matter the behavior. First problem is having device access to be considered the same as rule checking. Consider the following structure: A (default behavior: allow, exceptions disallow access) \ B (default behavior: allow, exceptions disallow access) A new exception is added to B by writing devices.deny: c 12:34 rw When checking if that exception is allowed in may_access(): if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) { if (behavior == DEVCG_DEFAULT_ALLOW) { /* the exception will deny access to certain devices */ return true; Which is ok, since B is not getting more privileges than A, it doesn't matter and the rule is accepted Now, consider it's a device file open check and the process belongs to cgroup B. The access will be generated as: behavior: allow exception: c 12:34 rw The very same chunk of code will allow it, even if there's an explicit exception telling to do otherwise. A simple test case: # mkdir new_group # cd new_group # echo $$ >tasks # echo "c 1:3 w" >devices.deny # echo >/dev/null # echo $? 0 This is a serious bug and was introduced on c39a2a3 devcg: prepare may_access() for hierarchy support To solve this problem, the device file open function was split from the new exception check. Second problem is how exceptions are processed by may_access(). The first part of the said function tries to match fully with an existing exception: list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) { if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK)) continue; if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR)) continue; if (ex->major != ~0 && ex->major != refex->major) continue; if (ex->minor != ~0 && ex->minor != refex->minor) continue; if (refex->access & (~ex->access)) continue; match = true; break; } That means the new exception should be contained into an existing one to be considered a match: New exception Existing match? notes b 12:34 rwm b 12:34 rwm yes b 12:34 r b *:34 rw yes b 12:34 rw b 12:34 w no extra "r" b *:34 rw b 12:34 rw no too broad "*" b *:34 rw b *:34 rwm yes Which is fine in some cases. Consider: A (default behavior: deny, exceptions allow access) \ B (default behavior: deny, exceptions allow access) In this case the full match makes sense, the new exception cannot add more access than the parent allows But this doesn't always work, consider: A (default behavior: allow, exceptions disallow access) \ B (default behavior: deny, exceptions allow access) In this case, a new exception in B shouldn't match any of the exceptions in A, after all you can't allow something that was forbidden by A. But consider this scenario: New exception Existing in A match? outcome b 12:34 rw b 12:34 r no exception is accepted Because the new exception has "w" as extra, it doesn't match, so it'll be added to B's exception list. The same problem can happen during a file access check. Consider a cgroup with allow as default behavior: Access Exception match? b 12:34 rw b 12:34 r no In this case, the access didn't match any of the exceptions in the cgroup, which is required since exceptions will disallow access. To solve this problem, two new functions were created to match an exception either fully or partially. In the example above, a partial check will be performed and it'll produce a match since at least "b 12:34 r" from "b 12:34 rw" access matches. Cc: cgroups@vger.kernel.org Cc: Tejun Heo <tj@kernel.org> Cc: Serge Hallyn <serge.hallyn@canonical.com> Cc: Li Zefan <lizefan@huawei.com> Signed-off-by: Aristeu Rozanski <arozansk@redhat.com> Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 26bc562 commit b2daa1d

1 file changed

Lines changed: 122 additions & 40 deletions

File tree

‎security/device_cgroup.c‎

Lines changed: 122 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -319,57 +319,139 @@ static int devcgroup_seq_read(struct cgroup_subsys_state *css,
319319
}
320320

321321
/**
322-
* may_access - verifies if a new exception is part of what is allowed
323-
* by a dev cgroup based on the default policy +
324-
* exceptions. This is used to make sure a child cgroup
325-
* won't have more privileges than its parent or to
326-
* verify if a certain access is allowed.
327-
* @dev_cgroup: dev cgroup to be tested against
328-
* @refex: new exception
329-
* @behavior: behavior of the exception
322+
* match_exception - iterates the exception list trying to match a rule
323+
* based on type, major, minor and access type. It is
324+
* considered a match if an exception is found that
325+
* will contain the entire range of provided parameters.
326+
* @exceptions: list of exceptions
327+
* @type: device type (DEV_BLOCK or DEV_CHAR)
328+
* @major: device file major number, ~0 to match all
329+
* @minor: device file minor number, ~0 to match all
330+
* @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
331+
*
332+
* returns: true in case it matches an exception completely
330333
*/
331-
static bool may_access(struct dev_cgroup *dev_cgroup,
332-
struct dev_exception_item *refex,
333-
enum devcg_behavior behavior)
334+
static bool match_exception(struct list_head *exceptions, short type,
335+
u32 major, u32 minor, short access)
334336
{
335337
struct dev_exception_item *ex;
336-
bool match = false;
337338

338-
rcu_lockdep_assert(rcu_read_lock_held() ||
339-
lockdep_is_held(&devcgroup_mutex),
340-
"device_cgroup::may_access() called without proper synchronization");
339+
list_for_each_entry_rcu(ex, exceptions, list) {
340+
if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
341+
continue;
342+
if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
343+
continue;
344+
if (ex->major != ~0 && ex->major != major)
345+
continue;
346+
if (ex->minor != ~0 && ex->minor != minor)
347+
continue;
348+
/* provided access cannot have more than the exception rule */
349+
if (access & (~ex->access))
350+
continue;
351+
return true;
352+
}
353+
return false;
354+
}
355+
356+
/**
357+
* match_exception_partial - iterates the exception list trying to match a rule
358+
* based on type, major, minor and access type. It is
359+
* considered a match if an exception's range is
360+
* found to contain *any* of the devices specified by
361+
* provided parameters. This is used to make sure no
362+
* extra access is being granted that is forbidden by
363+
* any of the exception list.
364+
* @exceptions: list of exceptions
365+
* @type: device type (DEV_BLOCK or DEV_CHAR)
366+
* @major: device file major number, ~0 to match all
367+
* @minor: device file minor number, ~0 to match all
368+
* @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
369+
*
370+
* returns: true in case the provided range mat matches an exception completely
371+
*/
372+
static bool match_exception_partial(struct list_head *exceptions, short type,
373+
u32 major, u32 minor, short access)
374+
{
375+
struct dev_exception_item *ex;
341376

342-
list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
343-
if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
377+
list_for_each_entry_rcu(ex, exceptions, list) {
378+
if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
344379
continue;
345-
if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
380+
if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
346381
continue;
347-
if (ex->major != ~0 && ex->major != refex->major)
382+
/*
383+
* We must be sure that both the exception and the provided
384+
* range aren't masking all devices
385+
*/
386+
if (ex->major != ~0 && major != ~0 && ex->major != major)
348387
continue;
349-
if (ex->minor != ~0 && ex->minor != refex->minor)
388+
if (ex->minor != ~0 && minor != ~0 && ex->minor != minor)
350389
continue;
351-
if (refex->access & (~ex->access))
390+
/*
391+
* In order to make sure the provided range isn't matching
392+
* an exception, all its access bits shouldn't match the
393+
* exception's access bits
394+
*/
395+
if (!(access & ex->access))
352396
continue;
353-
match = true;
354-
break;
397+
return true;
355398
}
399+
return false;
400+
}
401+
402+
/**
403+
* verify_new_ex - verifies if a new exception is part of what is allowed
404+
* by a dev cgroup based on the default policy +
405+
* exceptions. This is used to make sure a child cgroup
406+
* won't have more privileges than its parent
407+
* @dev_cgroup: dev cgroup to be tested against
408+
* @refex: new exception
409+
* @behavior: behavior of the exception's dev_cgroup
410+
*/
411+
static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
412+
struct dev_exception_item *refex,
413+
enum devcg_behavior behavior)
414+
{
415+
bool match = false;
416+
417+
rcu_lockdep_assert(rcu_read_lock_held() ||
418+
lockdep_is_held(&devcgroup_mutex),
419+
"device_cgroup:verify_new_ex called without proper synchronization");
356420

357421
if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
358422
if (behavior == DEVCG_DEFAULT_ALLOW) {
359-
/* the exception will deny access to certain devices */
423+
/*
424+
* new exception in the child doesn't matter, only
425+
* adding extra restrictions
426+
*/
360427
return true;
361428
} else {
362-
/* the exception will allow access to certain devices */
429+
/*
430+
* new exception in the child will add more devices
431+
* that can be acessed, so it can't match any of
432+
* parent's exceptions, even slightly
433+
*/
434+
match = match_exception_partial(&dev_cgroup->exceptions,
435+
refex->type,
436+
refex->major,
437+
refex->minor,
438+
refex->access);
439+
363440
if (match)
364-
/*
365-
* a new exception allowing access shouldn't
366-
* match an parent's exception
367-
*/
368441
return false;
369442
return true;
370443
}
371444
} else {
372-
/* only behavior == DEVCG_DEFAULT_DENY allowed here */
445+
/*
446+
* Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
447+
* the new exception will add access to more devices and must
448+
* be contained completely in an parent's exception to be
449+
* allowed
450+
*/
451+
match = match_exception(&dev_cgroup->exceptions, refex->type,
452+
refex->major, refex->minor,
453+
refex->access);
454+
373455
if (match)
374456
/* parent has an exception that matches the proposed */
375457
return true;
@@ -391,7 +473,7 @@ static int parent_has_perm(struct dev_cgroup *childcg,
391473

392474
if (!parent)
393475
return 1;
394-
return may_access(parent, ex, childcg->behavior);
476+
return verify_new_ex(parent, ex, childcg->behavior);
395477
}
396478

397479
/**
@@ -720,18 +802,18 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
720802
short access)
721803
{
722804
struct dev_cgroup *dev_cgroup;
723-
struct dev_exception_item ex;
724-
int rc;
725-
726-
memset(&ex, 0, sizeof(ex));
727-
ex.type = type;
728-
ex.major = major;
729-
ex.minor = minor;
730-
ex.access = access;
805+
bool rc;
731806

732807
rcu_read_lock();
733808
dev_cgroup = task_devcgroup(current);
734-
rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
809+
if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW)
810+
/* Can't match any of the exceptions, even partially */
811+
rc = !match_exception_partial(&dev_cgroup->exceptions,
812+
type, major, minor, access);
813+
else
814+
/* Need to match completely one exception to be allowed */
815+
rc = match_exception(&dev_cgroup->exceptions, type, major,
816+
minor, access);
735817
rcu_read_unlock();
736818

737819
if (!rc)

0 commit comments

Comments
 (0)