2 * device_cgroup.c - device cgroup subsystem
4 * Copyright 2007 IBM Corp
7 #include <linux/device_cgroup.h>
8 #include <linux/cgroup.h>
9 #include <linux/ctype.h>
10 #include <linux/list.h>
11 #include <linux/uaccess.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/rcupdate.h>
15 #include <linux/mutex.h>
20 #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
24 #define DEV_ALL 4 /* this represents all devices */
26 static DEFINE_MUTEX(devcgroup_mutex
);
35 * exception list locking rules:
36 * hold devcgroup_mutex for update/read.
37 * hold rcu_read_lock() for read.
40 struct dev_exception_item
{
44 struct list_head list
;
49 struct cgroup_subsys_state css
;
50 struct list_head exceptions
;
51 enum devcg_behavior behavior
;
54 static inline struct dev_cgroup
*css_to_devcgroup(struct cgroup_subsys_state
*s
)
56 return s
? container_of(s
, struct dev_cgroup
, css
) : NULL
;
59 static inline struct dev_cgroup
*task_devcgroup(struct task_struct
*task
)
61 return css_to_devcgroup(task_css(task
, devices_subsys_id
));
64 struct cgroup_subsys devices_subsys
;
66 static int devcgroup_can_attach(struct cgroup_subsys_state
*new_css
,
67 struct cgroup_taskset
*set
)
69 struct task_struct
*task
= cgroup_taskset_first(set
);
71 if (current
!= task
&& !capable(CAP_SYS_ADMIN
))
77 * called under devcgroup_mutex
79 static int dev_exceptions_copy(struct list_head
*dest
, struct list_head
*orig
)
81 struct dev_exception_item
*ex
, *tmp
, *new;
83 lockdep_assert_held(&devcgroup_mutex
);
85 list_for_each_entry(ex
, orig
, list
) {
86 new = kmemdup(ex
, sizeof(*ex
), GFP_KERNEL
);
89 list_add_tail(&new->list
, dest
);
95 list_for_each_entry_safe(ex
, tmp
, dest
, list
) {
103 * called under devcgroup_mutex
105 static int dev_exception_add(struct dev_cgroup
*dev_cgroup
,
106 struct dev_exception_item
*ex
)
108 struct dev_exception_item
*excopy
, *walk
;
110 lockdep_assert_held(&devcgroup_mutex
);
112 excopy
= kmemdup(ex
, sizeof(*ex
), GFP_KERNEL
);
116 list_for_each_entry(walk
, &dev_cgroup
->exceptions
, list
) {
117 if (walk
->type
!= ex
->type
)
119 if (walk
->major
!= ex
->major
)
121 if (walk
->minor
!= ex
->minor
)
124 walk
->access
|= ex
->access
;
130 list_add_tail_rcu(&excopy
->list
, &dev_cgroup
->exceptions
);
135 * called under devcgroup_mutex
137 static void dev_exception_rm(struct dev_cgroup
*dev_cgroup
,
138 struct dev_exception_item
*ex
)
140 struct dev_exception_item
*walk
, *tmp
;
142 lockdep_assert_held(&devcgroup_mutex
);
144 list_for_each_entry_safe(walk
, tmp
, &dev_cgroup
->exceptions
, list
) {
145 if (walk
->type
!= ex
->type
)
147 if (walk
->major
!= ex
->major
)
149 if (walk
->minor
!= ex
->minor
)
152 walk
->access
&= ~ex
->access
;
154 list_del_rcu(&walk
->list
);
155 kfree_rcu(walk
, rcu
);
160 static void __dev_exception_clean(struct dev_cgroup
*dev_cgroup
)
162 struct dev_exception_item
*ex
, *tmp
;
164 list_for_each_entry_safe(ex
, tmp
, &dev_cgroup
->exceptions
, list
) {
165 list_del_rcu(&ex
->list
);
171 * dev_exception_clean - frees all entries of the exception list
172 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
174 * called under devcgroup_mutex
176 static void dev_exception_clean(struct dev_cgroup
*dev_cgroup
)
178 lockdep_assert_held(&devcgroup_mutex
);
180 __dev_exception_clean(dev_cgroup
);
183 static inline bool is_devcg_online(const struct dev_cgroup
*devcg
)
185 return (devcg
->behavior
!= DEVCG_DEFAULT_NONE
);
189 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
191 * @css: css getting online
192 * returns 0 in case of success, error code otherwise
194 static int devcgroup_online(struct cgroup_subsys_state
*css
)
196 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
197 struct dev_cgroup
*parent_dev_cgroup
= css_to_devcgroup(css_parent(css
));
200 mutex_lock(&devcgroup_mutex
);
202 if (parent_dev_cgroup
== NULL
)
203 dev_cgroup
->behavior
= DEVCG_DEFAULT_ALLOW
;
205 ret
= dev_exceptions_copy(&dev_cgroup
->exceptions
,
206 &parent_dev_cgroup
->exceptions
);
208 dev_cgroup
->behavior
= parent_dev_cgroup
->behavior
;
210 mutex_unlock(&devcgroup_mutex
);
215 static void devcgroup_offline(struct cgroup_subsys_state
*css
)
217 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
219 mutex_lock(&devcgroup_mutex
);
220 dev_cgroup
->behavior
= DEVCG_DEFAULT_NONE
;
221 mutex_unlock(&devcgroup_mutex
);
225 * called from kernel/cgroup.c with cgroup_lock() held.
227 static struct cgroup_subsys_state
*
228 devcgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
230 struct dev_cgroup
*dev_cgroup
;
232 dev_cgroup
= kzalloc(sizeof(*dev_cgroup
), GFP_KERNEL
);
234 return ERR_PTR(-ENOMEM
);
235 INIT_LIST_HEAD(&dev_cgroup
->exceptions
);
236 dev_cgroup
->behavior
= DEVCG_DEFAULT_NONE
;
238 return &dev_cgroup
->css
;
241 static void devcgroup_css_free(struct cgroup_subsys_state
*css
)
243 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
245 __dev_exception_clean(dev_cgroup
);
249 #define DEVCG_ALLOW 1
256 static void set_access(char *acc
, short access
)
259 memset(acc
, 0, ACCLEN
);
260 if (access
& ACC_READ
)
262 if (access
& ACC_WRITE
)
264 if (access
& ACC_MKNOD
)
268 static char type_to_char(short type
)
272 if (type
== DEV_CHAR
)
274 if (type
== DEV_BLOCK
)
279 static void set_majmin(char *str
, unsigned m
)
284 sprintf(str
, "%u", m
);
287 static int devcgroup_seq_read(struct cgroup_subsys_state
*css
,
288 struct cftype
*cft
, struct seq_file
*m
)
290 struct dev_cgroup
*devcgroup
= css_to_devcgroup(css
);
291 struct dev_exception_item
*ex
;
292 char maj
[MAJMINLEN
], min
[MAJMINLEN
], acc
[ACCLEN
];
296 * To preserve the compatibility:
297 * - Only show the "all devices" when the default policy is to allow
298 * - List the exceptions in case the default policy is to deny
299 * This way, the file remains as a "whitelist of devices"
301 if (devcgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
302 set_access(acc
, ACC_MASK
);
305 seq_printf(m
, "%c %s:%s %s\n", type_to_char(DEV_ALL
),
308 list_for_each_entry_rcu(ex
, &devcgroup
->exceptions
, list
) {
309 set_access(acc
, ex
->access
);
310 set_majmin(maj
, ex
->major
);
311 set_majmin(min
, ex
->minor
);
312 seq_printf(m
, "%c %s:%s %s\n", type_to_char(ex
->type
),
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
331 static bool may_access(struct dev_cgroup
*dev_cgroup
,
332 struct dev_exception_item
*refex
,
333 enum devcg_behavior behavior
)
335 struct dev_exception_item
*ex
;
338 rcu_lockdep_assert(rcu_read_lock_held() ||
339 lockdep_is_held(&devcgroup_mutex
),
340 "device_cgroup::may_access() called without proper synchronization");
342 list_for_each_entry_rcu(ex
, &dev_cgroup
->exceptions
, list
) {
343 if ((refex
->type
& DEV_BLOCK
) && !(ex
->type
& DEV_BLOCK
))
345 if ((refex
->type
& DEV_CHAR
) && !(ex
->type
& DEV_CHAR
))
347 if (ex
->major
!= ~0 && ex
->major
!= refex
->major
)
349 if (ex
->minor
!= ~0 && ex
->minor
!= refex
->minor
)
351 if (refex
->access
& (~ex
->access
))
357 if (dev_cgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
358 if (behavior
== DEVCG_DEFAULT_ALLOW
) {
359 /* the exception will deny access to certain devices */
362 /* the exception will allow access to certain devices */
365 * a new exception allowing access shouldn't
366 * match an parent's exception
372 /* only behavior == DEVCG_DEFAULT_DENY allowed here */
374 /* parent has an exception that matches the proposed */
384 * when adding a new allow rule to a device exception list, the rule
385 * must be allowed in the parent device
387 static int parent_has_perm(struct dev_cgroup
*childcg
,
388 struct dev_exception_item
*ex
)
390 struct dev_cgroup
*parent
= css_to_devcgroup(css_parent(&childcg
->css
));
394 return may_access(parent
, ex
, childcg
->behavior
);
398 * may_allow_all - checks if it's possible to change the behavior to
399 * allow based on parent's rules.
400 * @parent: device cgroup's parent
401 * returns: != 0 in case it's allowed, 0 otherwise
403 static inline int may_allow_all(struct dev_cgroup
*parent
)
407 return parent
->behavior
== DEVCG_DEFAULT_ALLOW
;
411 * revalidate_active_exceptions - walks through the active exception list and
412 * revalidates the exceptions based on parent's
413 * behavior and exceptions. The exceptions that
414 * are no longer valid will be removed.
415 * Called with devcgroup_mutex held.
416 * @devcg: cgroup which exceptions will be checked
418 * This is one of the three key functions for hierarchy implementation.
419 * This function is responsible for re-evaluating all the cgroup's active
420 * exceptions due to a parent's exception change.
421 * Refer to Documentation/cgroups/devices.txt for more details.
423 static void revalidate_active_exceptions(struct dev_cgroup
*devcg
)
425 struct dev_exception_item
*ex
;
426 struct list_head
*this, *tmp
;
428 list_for_each_safe(this, tmp
, &devcg
->exceptions
) {
429 ex
= container_of(this, struct dev_exception_item
, list
);
430 if (!parent_has_perm(devcg
, ex
))
431 dev_exception_rm(devcg
, ex
);
436 * propagate_exception - propagates a new exception to the children
437 * @devcg_root: device cgroup that added a new exception
438 * @ex: new exception to be propagated
440 * returns: 0 in case of success, != 0 in case of error
442 static int propagate_exception(struct dev_cgroup
*devcg_root
,
443 struct dev_exception_item
*ex
)
445 struct cgroup_subsys_state
*pos
;
450 css_for_each_descendant_pre(pos
, &devcg_root
->css
) {
451 struct dev_cgroup
*devcg
= css_to_devcgroup(pos
);
454 * Because devcgroup_mutex is held, no devcg will become
455 * online or offline during the tree walk (see on/offline
456 * methods), and online ones are safe to access outside RCU
457 * read lock without bumping refcnt.
459 if (pos
== &devcg_root
->css
|| !is_devcg_online(devcg
))
465 * in case both root's behavior and devcg is allow, a new
466 * restriction means adding to the exception list
468 if (devcg_root
->behavior
== DEVCG_DEFAULT_ALLOW
&&
469 devcg
->behavior
== DEVCG_DEFAULT_ALLOW
) {
470 rc
= dev_exception_add(devcg
, ex
);
475 * in the other possible cases:
476 * root's behavior: allow, devcg's: deny
477 * root's behavior: deny, devcg's: deny
478 * the exception will be removed
480 dev_exception_rm(devcg
, ex
);
482 revalidate_active_exceptions(devcg
);
491 static inline bool has_children(struct dev_cgroup
*devcgroup
)
493 struct cgroup
*cgrp
= devcgroup
->css
.cgroup
;
495 return !list_empty(&cgrp
->children
);
499 * Modify the exception list using allow/deny rules.
500 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
501 * so we can give a container CAP_MKNOD to let it create devices but not
502 * modify the exception list.
503 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
504 * us to also grant CAP_SYS_ADMIN to containers without giving away the
505 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
507 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
508 * new access is only allowed if you're in the top-level cgroup, or your
509 * parent cgroup has the access you're asking for.
511 static int devcgroup_update_access(struct dev_cgroup
*devcgroup
,
512 int filetype
, const char *buffer
)
515 char temp
[12]; /* 11 + 1 characters needed for a u32 */
517 struct dev_exception_item ex
;
518 struct dev_cgroup
*parent
= css_to_devcgroup(css_parent(&devcgroup
->css
));
520 if (!capable(CAP_SYS_ADMIN
))
523 memset(&ex
, 0, sizeof(ex
));
530 if (has_children(devcgroup
))
533 if (!may_allow_all(parent
))
535 dev_exception_clean(devcgroup
);
536 devcgroup
->behavior
= DEVCG_DEFAULT_ALLOW
;
540 rc
= dev_exceptions_copy(&devcgroup
->exceptions
,
541 &parent
->exceptions
);
546 if (has_children(devcgroup
))
549 dev_exception_clean(devcgroup
);
550 devcgroup
->behavior
= DEVCG_DEFAULT_DENY
;
572 } else if (isdigit(*b
)) {
573 memset(temp
, 0, sizeof(temp
));
574 for (count
= 0; count
< sizeof(temp
) - 1; count
++) {
580 rc
= kstrtou32(temp
, 10, &ex
.major
);
594 } else if (isdigit(*b
)) {
595 memset(temp
, 0, sizeof(temp
));
596 for (count
= 0; count
< sizeof(temp
) - 1; count
++) {
602 rc
= kstrtou32(temp
, 10, &ex
.minor
);
610 for (b
++, count
= 0; count
< 3; count
++, b
++) {
613 ex
.access
|= ACC_READ
;
616 ex
.access
|= ACC_WRITE
;
619 ex
.access
|= ACC_MKNOD
;
632 if (!parent_has_perm(devcgroup
, &ex
))
635 * If the default policy is to allow by default, try to remove
636 * an matching exception instead. And be silent about it: we
637 * don't want to break compatibility
639 if (devcgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
640 dev_exception_rm(devcgroup
, &ex
);
643 rc
= dev_exception_add(devcgroup
, &ex
);
647 * If the default policy is to deny by default, try to remove
648 * an matching exception instead. And be silent about it: we
649 * don't want to break compatibility
651 if (devcgroup
->behavior
== DEVCG_DEFAULT_DENY
)
652 dev_exception_rm(devcgroup
, &ex
);
654 rc
= dev_exception_add(devcgroup
, &ex
);
658 /* we only propagate new restrictions */
659 rc
= propagate_exception(devcgroup
, &ex
);
667 static int devcgroup_access_write(struct cgroup_subsys_state
*css
,
668 struct cftype
*cft
, const char *buffer
)
672 mutex_lock(&devcgroup_mutex
);
673 retval
= devcgroup_update_access(css_to_devcgroup(css
),
674 cft
->private, buffer
);
675 mutex_unlock(&devcgroup_mutex
);
679 static struct cftype dev_cgroup_files
[] = {
682 .write_string
= devcgroup_access_write
,
683 .private = DEVCG_ALLOW
,
687 .write_string
= devcgroup_access_write
,
688 .private = DEVCG_DENY
,
692 .read_seq_string
= devcgroup_seq_read
,
693 .private = DEVCG_LIST
,
698 struct cgroup_subsys devices_subsys
= {
700 .can_attach
= devcgroup_can_attach
,
701 .css_alloc
= devcgroup_css_alloc
,
702 .css_free
= devcgroup_css_free
,
703 .css_online
= devcgroup_online
,
704 .css_offline
= devcgroup_offline
,
705 .subsys_id
= devices_subsys_id
,
706 .base_cftypes
= dev_cgroup_files
,
710 * __devcgroup_check_permission - checks if an inode operation is permitted
711 * @dev_cgroup: the dev cgroup to be tested against
713 * @major: device major number
714 * @minor: device minor number
715 * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
717 * returns 0 on success, -EPERM case the operation is not permitted
719 static int __devcgroup_check_permission(short type
, u32 major
, u32 minor
,
722 struct dev_cgroup
*dev_cgroup
;
723 struct dev_exception_item ex
;
726 memset(&ex
, 0, sizeof(ex
));
733 dev_cgroup
= task_devcgroup(current
);
734 rc
= may_access(dev_cgroup
, &ex
, dev_cgroup
->behavior
);
743 int __devcgroup_inode_permission(struct inode
*inode
, int mask
)
745 short type
, access
= 0;
747 if (S_ISBLK(inode
->i_mode
))
749 if (S_ISCHR(inode
->i_mode
))
751 if (mask
& MAY_WRITE
)
756 return __devcgroup_check_permission(type
, imajor(inode
), iminor(inode
),
760 int devcgroup_inode_mknod(int mode
, dev_t dev
)
764 if (!S_ISBLK(mode
) && !S_ISCHR(mode
))
772 return __devcgroup_check_permission(type
, MAJOR(dev
), MINOR(dev
),