Skip to content

Commit 26a41cd

Browse files
committed
Merge branch 'for-3.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup fixes from Tejun Heo: "During recent restructuring, device_cgroup unified config input check and enforcement logic; unfortunately, it turned out to share too much. Aristeu's patches fix the breakage and marked for -stable backport. The other two patches are fallouts from kernfs conversion. The blkcg change is temporary and will go away once kernfs internal locking gets simplified (patches pending)" * 'for-3.15-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: blkcg: use trylock on blkcg_pol_mutex in blkcg_reset_stats() device_cgroup: check if exception removal is allowed device_cgroup: fix the comment format for recently added functions device_cgroup: rework device access check and exception checking cgroup: fix the retry path of cgroup_mount()
2 parents 619b589 + 36c38fb commit 26a41cd

3 files changed

Lines changed: 177 additions & 48 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,20 @@ static int blkcg_reset_stats(struct cgroup_subsys_state *css,
451451
struct blkcg_gq *blkg;
452452
int i;
453453

454-
mutex_lock(&blkcg_pol_mutex);
454+
/*
455+
* XXX: We invoke cgroup_add/rm_cftypes() under blkcg_pol_mutex
456+
* which ends up putting cgroup's internal cgroup_tree_mutex under
457+
* it; however, cgroup_tree_mutex is nested above cgroup file
458+
* active protection and grabbing blkcg_pol_mutex from a cgroup
459+
* file operation creates a possible circular dependency. cgroup
460+
* internal locking is planned to go through further simplification
461+
* and this issue should go away soon. For now, let's trylock
462+
* blkcg_pol_mutex and restart the write on failure.
463+
*
464+
* http://lkml.kernel.org/g/5363C04B.4010400@oracle.com
465+
*/
466+
if (!mutex_trylock(&blkcg_pol_mutex))
467+
return restart_syscall();
455468
spin_lock_irq(&blkcg->lock);
456469

457470
/*

‎kernel/cgroup.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,15 +1495,15 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
14951495
*/
14961496
if (!use_task_css_set_links)
14971497
cgroup_enable_task_cg_lists();
1498-
retry:
1498+
14991499
mutex_lock(&cgroup_tree_mutex);
15001500
mutex_lock(&cgroup_mutex);
15011501

15021502
/* First find the desired set of subsystems */
15031503
ret = parse_cgroupfs_options(data, &opts);
15041504
if (ret)
15051505
goto out_unlock;
1506-
1506+
retry:
15071507
/* look for a matching existing root */
15081508
if (!opts.subsys_mask && !opts.none && !opts.name) {
15091509
cgrp_dfl_root_visible = true;
@@ -1562,9 +1562,9 @@ static struct dentry *cgroup_mount(struct file_system_type *fs_type,
15621562
if (!atomic_inc_not_zero(&root->cgrp.refcnt)) {
15631563
mutex_unlock(&cgroup_mutex);
15641564
mutex_unlock(&cgroup_tree_mutex);
1565-
kfree(opts.release_agent);
1566-
kfree(opts.name);
15671565
msleep(10);
1566+
mutex_lock(&cgroup_tree_mutex);
1567+
mutex_lock(&cgroup_mutex);
15681568
goto retry;
15691569
}
15701570

‎security/device_cgroup.c‎

Lines changed: 159 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -306,57 +306,138 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
306306
}
307307

308308
/**
309-
* may_access - verifies if a new exception is part of what is allowed
310-
* by a dev cgroup based on the default policy +
311-
* exceptions. This is used to make sure a child cgroup
312-
* won't have more privileges than its parent or to
313-
* verify if a certain access is allowed.
314-
* @dev_cgroup: dev cgroup to be tested against
315-
* @refex: new exception
316-
* @behavior: behavior of the exception
309+
* match_exception - iterates the exception list trying to find a complete match
310+
* @exceptions: list of exceptions
311+
* @type: device type (DEV_BLOCK or DEV_CHAR)
312+
* @major: device file major number, ~0 to match all
313+
* @minor: device file minor number, ~0 to match all
314+
* @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
315+
*
316+
* It is considered a complete match if an exception is found that will
317+
* contain the entire range of provided parameters.
318+
*
319+
* Return: true in case it matches an exception completely
317320
*/
318-
static bool may_access(struct dev_cgroup *dev_cgroup,
319-
struct dev_exception_item *refex,
320-
enum devcg_behavior behavior)
321+
static bool match_exception(struct list_head *exceptions, short type,
322+
u32 major, u32 minor, short access)
321323
{
322324
struct dev_exception_item *ex;
323-
bool match = false;
324325

325-
rcu_lockdep_assert(rcu_read_lock_held() ||
326-
lockdep_is_held(&devcgroup_mutex),
327-
"device_cgroup::may_access() called without proper synchronization");
326+
list_for_each_entry_rcu(ex, exceptions, list) {
327+
if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
328+
continue;
329+
if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
330+
continue;
331+
if (ex->major != ~0 && ex->major != major)
332+
continue;
333+
if (ex->minor != ~0 && ex->minor != minor)
334+
continue;
335+
/* provided access cannot have more than the exception rule */
336+
if (access & (~ex->access))
337+
continue;
338+
return true;
339+
}
340+
return false;
341+
}
342+
343+
/**
344+
* match_exception_partial - iterates the exception list trying to find a partial match
345+
* @exceptions: list of exceptions
346+
* @type: device type (DEV_BLOCK or DEV_CHAR)
347+
* @major: device file major number, ~0 to match all
348+
* @minor: device file minor number, ~0 to match all
349+
* @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
350+
*
351+
* It is considered a partial match if an exception's range is found to
352+
* contain *any* of the devices specified by provided parameters. This is
353+
* used to make sure no extra access is being granted that is forbidden by
354+
* any of the exception list.
355+
*
356+
* Return: true in case the provided range mat matches an exception completely
357+
*/
358+
static bool match_exception_partial(struct list_head *exceptions, short type,
359+
u32 major, u32 minor, short access)
360+
{
361+
struct dev_exception_item *ex;
328362

329-
list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
330-
if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
363+
list_for_each_entry_rcu(ex, exceptions, list) {
364+
if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
331365
continue;
332-
if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
366+
if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
333367
continue;
334-
if (ex->major != ~0 && ex->major != refex->major)
368+
/*
369+
* We must be sure that both the exception and the provided
370+
* range aren't masking all devices
371+
*/
372+
if (ex->major != ~0 && major != ~0 && ex->major != major)
335373
continue;
336-
if (ex->minor != ~0 && ex->minor != refex->minor)
374+
if (ex->minor != ~0 && minor != ~0 && ex->minor != minor)
337375
continue;
338-
if (refex->access & (~ex->access))
376+
/*
377+
* In order to make sure the provided range isn't matching
378+
* an exception, all its access bits shouldn't match the
379+
* exception's access bits
380+
*/
381+
if (!(access & ex->access))
339382
continue;
340-
match = true;
341-
break;
383+
return true;
342384
}
385+
return false;
386+
}
387+
388+
/**
389+
* verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
390+
* @dev_cgroup: dev cgroup to be tested against
391+
* @refex: new exception
392+
* @behavior: behavior of the exception's dev_cgroup
393+
*
394+
* This is used to make sure a child cgroup won't have more privileges
395+
* than its parent
396+
*/
397+
static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
398+
struct dev_exception_item *refex,
399+
enum devcg_behavior behavior)
400+
{
401+
bool match = false;
402+
403+
rcu_lockdep_assert(rcu_read_lock_held() ||
404+
lockdep_is_held(&devcgroup_mutex),
405+
"device_cgroup:verify_new_ex called without proper synchronization");
343406

344407
if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
345408
if (behavior == DEVCG_DEFAULT_ALLOW) {
346-
/* the exception will deny access to certain devices */
409+
/*
410+
* new exception in the child doesn't matter, only
411+
* adding extra restrictions
412+
*/
347413
return true;
348414
} else {
349-
/* the exception will allow access to certain devices */
415+
/*
416+
* new exception in the child will add more devices
417+
* that can be acessed, so it can't match any of
418+
* parent's exceptions, even slightly
419+
*/
420+
match = match_exception_partial(&dev_cgroup->exceptions,
421+
refex->type,
422+
refex->major,
423+
refex->minor,
424+
refex->access);
425+
350426
if (match)
351-
/*
352-
* a new exception allowing access shouldn't
353-
* match an parent's exception
354-
*/
355427
return false;
356428
return true;
357429
}
358430
} else {
359-
/* only behavior == DEVCG_DEFAULT_DENY allowed here */
431+
/*
432+
* Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
433+
* the new exception will add access to more devices and must
434+
* be contained completely in an parent's exception to be
435+
* allowed
436+
*/
437+
match = match_exception(&dev_cgroup->exceptions, refex->type,
438+
refex->major, refex->minor,
439+
refex->access);
440+
360441
if (match)
361442
/* parent has an exception that matches the proposed */
362443
return true;
@@ -378,7 +459,38 @@ static int parent_has_perm(struct dev_cgroup *childcg,
378459

379460
if (!parent)
380461
return 1;
381-
return may_access(parent, ex, childcg->behavior);
462+
return verify_new_ex(parent, ex, childcg->behavior);
463+
}
464+
465+
/**
466+
* parent_allows_removal - verify if it's ok to remove an exception
467+
* @childcg: child cgroup from where the exception will be removed
468+
* @ex: exception being removed
469+
*
470+
* When removing an exception in cgroups with default ALLOW policy, it must
471+
* be checked if removing it will give the child cgroup more access than the
472+
* parent.
473+
*
474+
* Return: true if it's ok to remove exception, false otherwise
475+
*/
476+
static bool parent_allows_removal(struct dev_cgroup *childcg,
477+
struct dev_exception_item *ex)
478+
{
479+
struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css));
480+
481+
if (!parent)
482+
return true;
483+
484+
/* It's always allowed to remove access to devices */
485+
if (childcg->behavior == DEVCG_DEFAULT_DENY)
486+
return true;
487+
488+
/*
489+
* Make sure you're not removing part or a whole exception existing in
490+
* the parent cgroup
491+
*/
492+
return !match_exception_partial(&parent->exceptions, ex->type,
493+
ex->major, ex->minor, ex->access);
382494
}
383495

384496
/**
@@ -616,17 +728,21 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
616728

617729
switch (filetype) {
618730
case DEVCG_ALLOW:
619-
if (!parent_has_perm(devcgroup, &ex))
620-
return -EPERM;
621731
/*
622732
* If the default policy is to allow by default, try to remove
623733
* an matching exception instead. And be silent about it: we
624734
* don't want to break compatibility
625735
*/
626736
if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
737+
/* Check if the parent allows removing it first */
738+
if (!parent_allows_removal(devcgroup, &ex))
739+
return -EPERM;
627740
dev_exception_rm(devcgroup, &ex);
628-
return 0;
741+
break;
629742
}
743+
744+
if (!parent_has_perm(devcgroup, &ex))
745+
return -EPERM;
630746
rc = dev_exception_add(devcgroup, &ex);
631747
break;
632748
case DEVCG_DENY:
@@ -704,18 +820,18 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
704820
short access)
705821
{
706822
struct dev_cgroup *dev_cgroup;
707-
struct dev_exception_item ex;
708-
int rc;
709-
710-
memset(&ex, 0, sizeof(ex));
711-
ex.type = type;
712-
ex.major = major;
713-
ex.minor = minor;
714-
ex.access = access;
823+
bool rc;
715824

716825
rcu_read_lock();
717826
dev_cgroup = task_devcgroup(current);
718-
rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
827+
if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW)
828+
/* Can't match any of the exceptions, even partially */
829+
rc = !match_exception_partial(&dev_cgroup->exceptions,
830+
type, major, minor, access);
831+
else
832+
/* Need to match completely one exception to be allowed */
833+
rc = match_exception(&dev_cgroup->exceptions, type, major,
834+
minor, access);
719835
rcu_read_unlock();
720836

721837
if (!rc)

0 commit comments

Comments
 (0)