1 // SPDX-License-Identifier: GPL-2.0
3 * device_cgroup.c - device cgroup subsystem
5 * Copyright 2007 IBM Corp
8 #include <linux/device_cgroup.h>
9 #include <linux/cgroup.h>
10 #include <linux/ctype.h>
11 #include <linux/list.h>
12 #include <linux/uaccess.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/rcupdate.h>
16 #include <linux/mutex.h>
21 #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
25 #define DEV_ALL 4 /* this represents all devices */
27 static DEFINE_MUTEX(devcgroup_mutex
);
36 * exception list locking rules:
37 * hold devcgroup_mutex for update/read.
38 * hold rcu_read_lock() for read.
41 struct dev_exception_item
{
45 struct list_head list
;
50 struct cgroup_subsys_state css
;
51 struct list_head exceptions
;
52 enum devcg_behavior behavior
;
55 static inline struct dev_cgroup
*css_to_devcgroup(struct cgroup_subsys_state
*s
)
57 return s
? container_of(s
, struct dev_cgroup
, css
) : NULL
;
60 static inline struct dev_cgroup
*task_devcgroup(struct task_struct
*task
)
62 return css_to_devcgroup(task_css(task
, devices_cgrp_id
));
66 * called under devcgroup_mutex
68 static int dev_exceptions_copy(struct list_head
*dest
, struct list_head
*orig
)
70 struct dev_exception_item
*ex
, *tmp
, *new;
72 lockdep_assert_held(&devcgroup_mutex
);
74 list_for_each_entry(ex
, orig
, list
) {
75 new = kmemdup(ex
, sizeof(*ex
), GFP_KERNEL
);
78 list_add_tail(&new->list
, dest
);
84 list_for_each_entry_safe(ex
, tmp
, dest
, list
) {
92 * called under devcgroup_mutex
94 static int dev_exception_add(struct dev_cgroup
*dev_cgroup
,
95 struct dev_exception_item
*ex
)
97 struct dev_exception_item
*excopy
, *walk
;
99 lockdep_assert_held(&devcgroup_mutex
);
101 excopy
= kmemdup(ex
, sizeof(*ex
), GFP_KERNEL
);
105 list_for_each_entry(walk
, &dev_cgroup
->exceptions
, list
) {
106 if (walk
->type
!= ex
->type
)
108 if (walk
->major
!= ex
->major
)
110 if (walk
->minor
!= ex
->minor
)
113 walk
->access
|= ex
->access
;
119 list_add_tail_rcu(&excopy
->list
, &dev_cgroup
->exceptions
);
124 * called under devcgroup_mutex
126 static void dev_exception_rm(struct dev_cgroup
*dev_cgroup
,
127 struct dev_exception_item
*ex
)
129 struct dev_exception_item
*walk
, *tmp
;
131 lockdep_assert_held(&devcgroup_mutex
);
133 list_for_each_entry_safe(walk
, tmp
, &dev_cgroup
->exceptions
, list
) {
134 if (walk
->type
!= ex
->type
)
136 if (walk
->major
!= ex
->major
)
138 if (walk
->minor
!= ex
->minor
)
141 walk
->access
&= ~ex
->access
;
143 list_del_rcu(&walk
->list
);
144 kfree_rcu(walk
, rcu
);
149 static void __dev_exception_clean(struct dev_cgroup
*dev_cgroup
)
151 struct dev_exception_item
*ex
, *tmp
;
153 list_for_each_entry_safe(ex
, tmp
, &dev_cgroup
->exceptions
, list
) {
154 list_del_rcu(&ex
->list
);
160 * dev_exception_clean - frees all entries of the exception list
161 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
163 * called under devcgroup_mutex
165 static void dev_exception_clean(struct dev_cgroup
*dev_cgroup
)
167 lockdep_assert_held(&devcgroup_mutex
);
169 __dev_exception_clean(dev_cgroup
);
172 static inline bool is_devcg_online(const struct dev_cgroup
*devcg
)
174 return (devcg
->behavior
!= DEVCG_DEFAULT_NONE
);
178 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
180 * @css: css getting online
181 * returns 0 in case of success, error code otherwise
183 static int devcgroup_online(struct cgroup_subsys_state
*css
)
185 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
186 struct dev_cgroup
*parent_dev_cgroup
= css_to_devcgroup(css
->parent
);
189 mutex_lock(&devcgroup_mutex
);
191 if (parent_dev_cgroup
== NULL
)
192 dev_cgroup
->behavior
= DEVCG_DEFAULT_ALLOW
;
194 ret
= dev_exceptions_copy(&dev_cgroup
->exceptions
,
195 &parent_dev_cgroup
->exceptions
);
197 dev_cgroup
->behavior
= parent_dev_cgroup
->behavior
;
199 mutex_unlock(&devcgroup_mutex
);
204 static void devcgroup_offline(struct cgroup_subsys_state
*css
)
206 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
208 mutex_lock(&devcgroup_mutex
);
209 dev_cgroup
->behavior
= DEVCG_DEFAULT_NONE
;
210 mutex_unlock(&devcgroup_mutex
);
214 * called from kernel/cgroup.c with cgroup_lock() held.
216 static struct cgroup_subsys_state
*
217 devcgroup_css_alloc(struct cgroup_subsys_state
*parent_css
)
219 struct dev_cgroup
*dev_cgroup
;
221 dev_cgroup
= kzalloc(sizeof(*dev_cgroup
), GFP_KERNEL
);
223 return ERR_PTR(-ENOMEM
);
224 INIT_LIST_HEAD(&dev_cgroup
->exceptions
);
225 dev_cgroup
->behavior
= DEVCG_DEFAULT_NONE
;
227 return &dev_cgroup
->css
;
230 static void devcgroup_css_free(struct cgroup_subsys_state
*css
)
232 struct dev_cgroup
*dev_cgroup
= css_to_devcgroup(css
);
234 __dev_exception_clean(dev_cgroup
);
238 #define DEVCG_ALLOW 1
245 static void set_access(char *acc
, short access
)
248 memset(acc
, 0, ACCLEN
);
249 if (access
& ACC_READ
)
251 if (access
& ACC_WRITE
)
253 if (access
& ACC_MKNOD
)
257 static char type_to_char(short type
)
261 if (type
== DEV_CHAR
)
263 if (type
== DEV_BLOCK
)
268 static void set_majmin(char *str
, unsigned m
)
273 sprintf(str
, "%u", m
);
276 static int devcgroup_seq_show(struct seq_file
*m
, void *v
)
278 struct dev_cgroup
*devcgroup
= css_to_devcgroup(seq_css(m
));
279 struct dev_exception_item
*ex
;
280 char maj
[MAJMINLEN
], min
[MAJMINLEN
], acc
[ACCLEN
];
284 * To preserve the compatibility:
285 * - Only show the "all devices" when the default policy is to allow
286 * - List the exceptions in case the default policy is to deny
287 * This way, the file remains as a "whitelist of devices"
289 if (devcgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
290 set_access(acc
, ACC_MASK
);
293 seq_printf(m
, "%c %s:%s %s\n", type_to_char(DEV_ALL
),
296 list_for_each_entry_rcu(ex
, &devcgroup
->exceptions
, list
) {
297 set_access(acc
, ex
->access
);
298 set_majmin(maj
, ex
->major
);
299 set_majmin(min
, ex
->minor
);
300 seq_printf(m
, "%c %s:%s %s\n", type_to_char(ex
->type
),
310 * match_exception - iterates the exception list trying to find a complete match
311 * @exceptions: list of exceptions
312 * @type: device type (DEV_BLOCK or DEV_CHAR)
313 * @major: device file major number, ~0 to match all
314 * @minor: device file minor number, ~0 to match all
315 * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
317 * It is considered a complete match if an exception is found that will
318 * contain the entire range of provided parameters.
320 * Return: true in case it matches an exception completely
322 static bool match_exception(struct list_head
*exceptions
, short type
,
323 u32 major
, u32 minor
, short access
)
325 struct dev_exception_item
*ex
;
327 list_for_each_entry_rcu(ex
, exceptions
, list
) {
328 if ((type
& DEV_BLOCK
) && !(ex
->type
& DEV_BLOCK
))
330 if ((type
& DEV_CHAR
) && !(ex
->type
& DEV_CHAR
))
332 if (ex
->major
!= ~0 && ex
->major
!= major
)
334 if (ex
->minor
!= ~0 && ex
->minor
!= minor
)
336 /* provided access cannot have more than the exception rule */
337 if (access
& (~ex
->access
))
345 * match_exception_partial - iterates the exception list trying to find a partial match
346 * @exceptions: list of exceptions
347 * @type: device type (DEV_BLOCK or DEV_CHAR)
348 * @major: device file major number, ~0 to match all
349 * @minor: device file minor number, ~0 to match all
350 * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
352 * It is considered a partial match if an exception's range is found to
353 * contain *any* of the devices specified by provided parameters. This is
354 * used to make sure no extra access is being granted that is forbidden by
355 * any of the exception list.
357 * Return: true in case the provided range mat matches an exception completely
359 static bool match_exception_partial(struct list_head
*exceptions
, short type
,
360 u32 major
, u32 minor
, short access
)
362 struct dev_exception_item
*ex
;
364 list_for_each_entry_rcu(ex
, exceptions
, list
) {
365 if ((type
& DEV_BLOCK
) && !(ex
->type
& DEV_BLOCK
))
367 if ((type
& DEV_CHAR
) && !(ex
->type
& DEV_CHAR
))
370 * We must be sure that both the exception and the provided
371 * range aren't masking all devices
373 if (ex
->major
!= ~0 && major
!= ~0 && ex
->major
!= major
)
375 if (ex
->minor
!= ~0 && minor
!= ~0 && ex
->minor
!= minor
)
378 * In order to make sure the provided range isn't matching
379 * an exception, all its access bits shouldn't match the
380 * exception's access bits
382 if (!(access
& ex
->access
))
390 * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
391 * @dev_cgroup: dev cgroup to be tested against
392 * @refex: new exception
393 * @behavior: behavior of the exception's dev_cgroup
395 * This is used to make sure a child cgroup won't have more privileges
398 static bool verify_new_ex(struct dev_cgroup
*dev_cgroup
,
399 struct dev_exception_item
*refex
,
400 enum devcg_behavior behavior
)
404 RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
405 !lockdep_is_held(&devcgroup_mutex
),
406 "device_cgroup:verify_new_ex called without proper synchronization");
408 if (dev_cgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
409 if (behavior
== DEVCG_DEFAULT_ALLOW
) {
411 * new exception in the child doesn't matter, only
412 * adding extra restrictions
417 * new exception in the child will add more devices
418 * that can be acessed, so it can't match any of
419 * parent's exceptions, even slightly
421 match
= match_exception_partial(&dev_cgroup
->exceptions
,
433 * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
434 * the new exception will add access to more devices and must
435 * be contained completely in an parent's exception to be
438 match
= match_exception(&dev_cgroup
->exceptions
, refex
->type
,
439 refex
->major
, refex
->minor
,
443 /* parent has an exception that matches the proposed */
453 * when adding a new allow rule to a device exception list, the rule
454 * must be allowed in the parent device
456 static int parent_has_perm(struct dev_cgroup
*childcg
,
457 struct dev_exception_item
*ex
)
459 struct dev_cgroup
*parent
= css_to_devcgroup(childcg
->css
.parent
);
463 return verify_new_ex(parent
, ex
, childcg
->behavior
);
467 * parent_allows_removal - verify if it's ok to remove an exception
468 * @childcg: child cgroup from where the exception will be removed
469 * @ex: exception being removed
471 * When removing an exception in cgroups with default ALLOW policy, it must
472 * be checked if removing it will give the child cgroup more access than the
475 * Return: true if it's ok to remove exception, false otherwise
477 static bool parent_allows_removal(struct dev_cgroup
*childcg
,
478 struct dev_exception_item
*ex
)
480 struct dev_cgroup
*parent
= css_to_devcgroup(childcg
->css
.parent
);
485 /* It's always allowed to remove access to devices */
486 if (childcg
->behavior
== DEVCG_DEFAULT_DENY
)
490 * Make sure you're not removing part or a whole exception existing in
493 return !match_exception_partial(&parent
->exceptions
, ex
->type
,
494 ex
->major
, ex
->minor
, ex
->access
);
498 * may_allow_all - checks if it's possible to change the behavior to
499 * allow based on parent's rules.
500 * @parent: device cgroup's parent
501 * returns: != 0 in case it's allowed, 0 otherwise
503 static inline int may_allow_all(struct dev_cgroup
*parent
)
507 return parent
->behavior
== DEVCG_DEFAULT_ALLOW
;
511 * revalidate_active_exceptions - walks through the active exception list and
512 * revalidates the exceptions based on parent's
513 * behavior and exceptions. The exceptions that
514 * are no longer valid will be removed.
515 * Called with devcgroup_mutex held.
516 * @devcg: cgroup which exceptions will be checked
518 * This is one of the three key functions for hierarchy implementation.
519 * This function is responsible for re-evaluating all the cgroup's active
520 * exceptions due to a parent's exception change.
521 * Refer to Documentation/cgroups/devices.txt for more details.
523 static void revalidate_active_exceptions(struct dev_cgroup
*devcg
)
525 struct dev_exception_item
*ex
;
526 struct list_head
*this, *tmp
;
528 list_for_each_safe(this, tmp
, &devcg
->exceptions
) {
529 ex
= container_of(this, struct dev_exception_item
, list
);
530 if (!parent_has_perm(devcg
, ex
))
531 dev_exception_rm(devcg
, ex
);
536 * propagate_exception - propagates a new exception to the children
537 * @devcg_root: device cgroup that added a new exception
538 * @ex: new exception to be propagated
540 * returns: 0 in case of success, != 0 in case of error
542 static int propagate_exception(struct dev_cgroup
*devcg_root
,
543 struct dev_exception_item
*ex
)
545 struct cgroup_subsys_state
*pos
;
550 css_for_each_descendant_pre(pos
, &devcg_root
->css
) {
551 struct dev_cgroup
*devcg
= css_to_devcgroup(pos
);
554 * Because devcgroup_mutex is held, no devcg will become
555 * online or offline during the tree walk (see on/offline
556 * methods), and online ones are safe to access outside RCU
557 * read lock without bumping refcnt.
559 if (pos
== &devcg_root
->css
|| !is_devcg_online(devcg
))
565 * in case both root's behavior and devcg is allow, a new
566 * restriction means adding to the exception list
568 if (devcg_root
->behavior
== DEVCG_DEFAULT_ALLOW
&&
569 devcg
->behavior
== DEVCG_DEFAULT_ALLOW
) {
570 rc
= dev_exception_add(devcg
, ex
);
575 * in the other possible cases:
576 * root's behavior: allow, devcg's: deny
577 * root's behavior: deny, devcg's: deny
578 * the exception will be removed
580 dev_exception_rm(devcg
, ex
);
582 revalidate_active_exceptions(devcg
);
592 * Modify the exception list using allow/deny rules.
593 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
594 * so we can give a container CAP_MKNOD to let it create devices but not
595 * modify the exception list.
596 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
597 * us to also grant CAP_SYS_ADMIN to containers without giving away the
598 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
600 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
601 * new access is only allowed if you're in the top-level cgroup, or your
602 * parent cgroup has the access you're asking for.
604 static int devcgroup_update_access(struct dev_cgroup
*devcgroup
,
605 int filetype
, char *buffer
)
608 char temp
[12]; /* 11 + 1 characters needed for a u32 */
610 struct dev_exception_item ex
;
611 struct dev_cgroup
*parent
= css_to_devcgroup(devcgroup
->css
.parent
);
613 if (!capable(CAP_SYS_ADMIN
))
616 memset(&ex
, 0, sizeof(ex
));
623 if (css_has_online_children(&devcgroup
->css
))
626 if (!may_allow_all(parent
))
628 dev_exception_clean(devcgroup
);
629 devcgroup
->behavior
= DEVCG_DEFAULT_ALLOW
;
633 rc
= dev_exceptions_copy(&devcgroup
->exceptions
,
634 &parent
->exceptions
);
639 if (css_has_online_children(&devcgroup
->css
))
642 dev_exception_clean(devcgroup
);
643 devcgroup
->behavior
= DEVCG_DEFAULT_DENY
;
665 } else if (isdigit(*b
)) {
666 memset(temp
, 0, sizeof(temp
));
667 for (count
= 0; count
< sizeof(temp
) - 1; count
++) {
673 rc
= kstrtou32(temp
, 10, &ex
.major
);
687 } else if (isdigit(*b
)) {
688 memset(temp
, 0, sizeof(temp
));
689 for (count
= 0; count
< sizeof(temp
) - 1; count
++) {
695 rc
= kstrtou32(temp
, 10, &ex
.minor
);
703 for (b
++, count
= 0; count
< 3; count
++, b
++) {
706 ex
.access
|= ACC_READ
;
709 ex
.access
|= ACC_WRITE
;
712 ex
.access
|= ACC_MKNOD
;
726 * If the default policy is to allow by default, try to remove
727 * an matching exception instead. And be silent about it: we
728 * don't want to break compatibility
730 if (devcgroup
->behavior
== DEVCG_DEFAULT_ALLOW
) {
731 /* Check if the parent allows removing it first */
732 if (!parent_allows_removal(devcgroup
, &ex
))
734 dev_exception_rm(devcgroup
, &ex
);
738 if (!parent_has_perm(devcgroup
, &ex
))
740 rc
= dev_exception_add(devcgroup
, &ex
);
744 * If the default policy is to deny by default, try to remove
745 * an matching exception instead. And be silent about it: we
746 * don't want to break compatibility
748 if (devcgroup
->behavior
== DEVCG_DEFAULT_DENY
)
749 dev_exception_rm(devcgroup
, &ex
);
751 rc
= dev_exception_add(devcgroup
, &ex
);
755 /* we only propagate new restrictions */
756 rc
= propagate_exception(devcgroup
, &ex
);
764 static ssize_t
devcgroup_access_write(struct kernfs_open_file
*of
,
765 char *buf
, size_t nbytes
, loff_t off
)
769 mutex_lock(&devcgroup_mutex
);
770 retval
= devcgroup_update_access(css_to_devcgroup(of_css(of
)),
771 of_cft(of
)->private, strstrip(buf
));
772 mutex_unlock(&devcgroup_mutex
);
773 return retval
?: nbytes
;
776 static struct cftype dev_cgroup_files
[] = {
779 .write
= devcgroup_access_write
,
780 .private = DEVCG_ALLOW
,
784 .write
= devcgroup_access_write
,
785 .private = DEVCG_DENY
,
789 .seq_show
= devcgroup_seq_show
,
790 .private = DEVCG_LIST
,
795 struct cgroup_subsys devices_cgrp_subsys
= {
796 .css_alloc
= devcgroup_css_alloc
,
797 .css_free
= devcgroup_css_free
,
798 .css_online
= devcgroup_online
,
799 .css_offline
= devcgroup_offline
,
800 .legacy_cftypes
= dev_cgroup_files
,
804 * __devcgroup_check_permission - checks if an inode operation is permitted
805 * @dev_cgroup: the dev cgroup to be tested against
807 * @major: device major number
808 * @minor: device minor number
809 * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
811 * returns 0 on success, -EPERM case the operation is not permitted
813 static int __devcgroup_check_permission(short type
, u32 major
, u32 minor
,
816 struct dev_cgroup
*dev_cgroup
;
820 dev_cgroup
= task_devcgroup(current
);
821 if (dev_cgroup
->behavior
== DEVCG_DEFAULT_ALLOW
)
822 /* Can't match any of the exceptions, even partially */
823 rc
= !match_exception_partial(&dev_cgroup
->exceptions
,
824 type
, major
, minor
, access
);
826 /* Need to match completely one exception to be allowed */
827 rc
= match_exception(&dev_cgroup
->exceptions
, type
, major
,
837 int __devcgroup_inode_permission(struct inode
*inode
, int mask
)
839 short type
, access
= 0;
841 if (S_ISBLK(inode
->i_mode
))
843 if (S_ISCHR(inode
->i_mode
))
845 if (mask
& MAY_WRITE
)
850 return __devcgroup_check_permission(type
, imajor(inode
), iminor(inode
),
854 int devcgroup_inode_mknod(int mode
, dev_t dev
)
858 if (!S_ISBLK(mode
) && !S_ISCHR(mode
))
866 return __devcgroup_check_permission(type
, MAJOR(dev
), MINOR(dev
),