Skip to content

Commit c39a2a3

Browse files
aristeuhtejun
authored andcommitted
devcg: prepare may_access() for hierarchy support
Currently may_access() is only able to verify if an exception is valid for the current cgroup, which has the same behavior. With hierarchy, it'll be also used to verify if a cgroup local exception is valid towards its cgroup parent, which might have different behavior. v2: - updated patch description - rebased on top of a new patch to expand the may_access() logic to make it more clear - fixed argument description order in may_access() Acked-by: Tejun Heo <tj@kernel.org> Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Cc: Tejun Heo <tj@kernel.org> Cc: Serge Hallyn <serge.hallyn@canonical.com> Signed-off-by: Aristeu Rozanski <aris@redhat.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 26898fd commit c39a2a3

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

‎security/device_cgroup.c‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
static DEFINE_MUTEX(devcgroup_mutex);
2727

28+
enum devcg_behavior {
29+
DEVCG_DEFAULT_NONE,
30+
DEVCG_DEFAULT_ALLOW,
31+
DEVCG_DEFAULT_DENY,
32+
};
33+
2834
/*
2935
* exception list locking rules:
3036
* hold devcgroup_mutex for update/read.
@@ -42,10 +48,7 @@ struct dev_exception_item {
4248
struct dev_cgroup {
4349
struct cgroup_subsys_state css;
4450
struct list_head exceptions;
45-
enum {
46-
DEVCG_DEFAULT_ALLOW,
47-
DEVCG_DEFAULT_DENY,
48-
} behavior;
51+
enum devcg_behavior behavior;
4952
};
5053

5154
static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
@@ -304,9 +307,11 @@ static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
304307
* verify if a certain access is allowed.
305308
* @dev_cgroup: dev cgroup to be tested against
306309
* @refex: new exception
310+
* @behavior: behavior of the exception
307311
*/
308312
static bool may_access(struct dev_cgroup *dev_cgroup,
309-
struct dev_exception_item *refex)
313+
struct dev_exception_item *refex,
314+
enum devcg_behavior behavior)
310315
{
311316
struct dev_exception_item *ex;
312317
bool match = false;
@@ -330,19 +335,27 @@ static bool may_access(struct dev_cgroup *dev_cgroup,
330335
break;
331336
}
332337

333-
/*
334-
* In two cases we'll consider this new exception valid:
335-
* - the dev cgroup has its default policy to deny + exception list:
336-
* the new exception *should* match the exceptions
337-
* - the dev cgroup has its default policy to allow + exception list:
338-
* the new exception should *not* match any of the exceptions
339-
*/
340-
if (dev_cgroup->behavior == DEVCG_DEFAULT_DENY) {
341-
if (match)
338+
if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
339+
if (behavior == DEVCG_DEFAULT_ALLOW) {
340+
/* the exception will deny access to certain devices */
341+
return true;
342+
} else {
343+
/* the exception will allow access to certain devices */
344+
if (match)
345+
/*
346+
* a new exception allowing access shouldn't
347+
* match an parent's exception
348+
*/
349+
return false;
342350
return true;
351+
}
343352
} else {
344-
if (!match)
353+
/* only behavior == DEVCG_DEFAULT_DENY allowed here */
354+
if (match)
355+
/* parent has an exception that matches the proposed */
345356
return true;
357+
else
358+
return false;
346359
}
347360
return false;
348361
}
@@ -361,7 +374,7 @@ static int parent_has_perm(struct dev_cgroup *childcg,
361374
if (!pcg)
362375
return 1;
363376
parent = cgroup_to_devcgroup(pcg);
364-
return may_access(parent, ex);
377+
return may_access(parent, ex, childcg->behavior);
365378
}
366379

367380
/**
@@ -395,7 +408,7 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
395408
{
396409
const char *b;
397410
char temp[12]; /* 11 + 1 characters needed for a u32 */
398-
int count, rc;
411+
int count, rc = 0;
399412
struct dev_exception_item ex;
400413
struct cgroup *p = devcgroup->css.cgroup;
401414
struct dev_cgroup *parent = NULL;
@@ -612,7 +625,7 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
612625

613626
rcu_read_lock();
614627
dev_cgroup = task_devcgroup(current);
615-
rc = may_access(dev_cgroup, &ex);
628+
rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
616629
rcu_read_unlock();
617630

618631
if (!rc)

0 commit comments

Comments
 (0)