2 * dev_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>
17 #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
21 #define DEV_ALL 4 /* this represents all devices */
24 * whitelist locking rules:
25 * cgroup_lock() cannot be taken under dev_cgroup->lock.
26 * dev_cgroup->lock can be taken with or without cgroup_lock().
28 * modifications always require cgroup_lock
29 * modifications to a list which is visible require the
30 * dev_cgroup->lock *and* cgroup_lock()
31 * walking the list requires dev_cgroup->lock or cgroup_lock().
33 * reasoning: dev_whitelist_copy() needs to kmalloc, so needs
34 * a mutex, which the cgroup_lock() is. Since modifying
35 * a visible list requires both locks, either lock can be
36 * taken for walking the list.
39 struct dev_whitelist_item
{
43 struct list_head list
;
47 struct cgroup_subsys_state css
;
48 struct list_head whitelist
;
52 static inline struct dev_cgroup
*cgroup_to_devcgroup(struct cgroup
*cgroup
)
54 return container_of(cgroup_subsys_state(cgroup
, devices_subsys_id
),
55 struct dev_cgroup
, css
);
58 struct cgroup_subsys devices_subsys
;
60 static int devcgroup_can_attach(struct cgroup_subsys
*ss
,
61 struct cgroup
*new_cgroup
, struct task_struct
*task
)
63 if (current
!= task
&& !capable(CAP_SYS_ADMIN
))
70 * called under cgroup_lock()
72 static int dev_whitelist_copy(struct list_head
*dest
, struct list_head
*orig
)
74 struct dev_whitelist_item
*wh
, *tmp
, *new;
76 list_for_each_entry(wh
, orig
, list
) {
77 new = kmalloc(sizeof(*wh
), GFP_KERNEL
);
80 new->major
= wh
->major
;
81 new->minor
= wh
->minor
;
83 new->access
= wh
->access
;
84 list_add_tail(&new->list
, dest
);
90 list_for_each_entry_safe(wh
, tmp
, dest
, list
) {
97 /* Stupid prototype - don't bother combining existing entries */
99 * called under cgroup_lock()
100 * since the list is visible to other tasks, we need the spinlock also
102 static int dev_whitelist_add(struct dev_cgroup
*dev_cgroup
,
103 struct dev_whitelist_item
*wh
)
105 struct dev_whitelist_item
*whcopy
;
107 whcopy
= kmalloc(sizeof(*whcopy
), GFP_KERNEL
);
111 memcpy(whcopy
, wh
, sizeof(*whcopy
));
112 spin_lock(&dev_cgroup
->lock
);
113 list_add_tail(&whcopy
->list
, &dev_cgroup
->whitelist
);
114 spin_unlock(&dev_cgroup
->lock
);
119 * called under cgroup_lock()
120 * since the list is visible to other tasks, we need the spinlock also
122 static void dev_whitelist_rm(struct dev_cgroup
*dev_cgroup
,
123 struct dev_whitelist_item
*wh
)
125 struct dev_whitelist_item
*walk
, *tmp
;
127 spin_lock(&dev_cgroup
->lock
);
128 list_for_each_entry_safe(walk
, tmp
, &dev_cgroup
->whitelist
, list
) {
129 if (walk
->type
== DEV_ALL
)
131 if (walk
->type
!= wh
->type
)
133 if (walk
->major
!= ~0 && walk
->major
!= wh
->major
)
135 if (walk
->minor
!= ~0 && walk
->minor
!= wh
->minor
)
139 walk
->access
&= ~wh
->access
;
141 list_del(&walk
->list
);
145 spin_unlock(&dev_cgroup
->lock
);
149 * called from kernel/cgroup.c with cgroup_lock() held.
151 static struct cgroup_subsys_state
*devcgroup_create(struct cgroup_subsys
*ss
,
152 struct cgroup
*cgroup
)
154 struct dev_cgroup
*dev_cgroup
, *parent_dev_cgroup
;
155 struct cgroup
*parent_cgroup
;
158 dev_cgroup
= kzalloc(sizeof(*dev_cgroup
), GFP_KERNEL
);
160 return ERR_PTR(-ENOMEM
);
161 INIT_LIST_HEAD(&dev_cgroup
->whitelist
);
162 parent_cgroup
= cgroup
->parent
;
164 if (parent_cgroup
== NULL
) {
165 struct dev_whitelist_item
*wh
;
166 wh
= kmalloc(sizeof(*wh
), GFP_KERNEL
);
169 return ERR_PTR(-ENOMEM
);
171 wh
->minor
= wh
->major
= ~0;
173 wh
->access
= ACC_MKNOD
| ACC_READ
| ACC_WRITE
;
174 list_add(&wh
->list
, &dev_cgroup
->whitelist
);
176 parent_dev_cgroup
= cgroup_to_devcgroup(parent_cgroup
);
177 ret
= dev_whitelist_copy(&dev_cgroup
->whitelist
,
178 &parent_dev_cgroup
->whitelist
);
185 spin_lock_init(&dev_cgroup
->lock
);
186 return &dev_cgroup
->css
;
189 static void devcgroup_destroy(struct cgroup_subsys
*ss
,
190 struct cgroup
*cgroup
)
192 struct dev_cgroup
*dev_cgroup
;
193 struct dev_whitelist_item
*wh
, *tmp
;
195 dev_cgroup
= cgroup_to_devcgroup(cgroup
);
196 list_for_each_entry_safe(wh
, tmp
, &dev_cgroup
->whitelist
, list
) {
203 #define DEVCG_ALLOW 1
210 static void set_access(char *acc
, short access
)
213 memset(acc
, 0, ACCLEN
);
214 if (access
& ACC_READ
)
216 if (access
& ACC_WRITE
)
218 if (access
& ACC_MKNOD
)
222 static char type_to_char(short type
)
226 if (type
== DEV_CHAR
)
228 if (type
== DEV_BLOCK
)
233 static void set_majmin(char *str
, unsigned m
)
235 memset(str
, 0, MAJMINLEN
);
239 snprintf(str
, MAJMINLEN
, "%d", m
);
242 static int devcgroup_seq_read(struct cgroup
*cgroup
, struct cftype
*cft
,
245 struct dev_cgroup
*devcgroup
= cgroup_to_devcgroup(cgroup
);
246 struct dev_whitelist_item
*wh
;
247 char maj
[MAJMINLEN
], min
[MAJMINLEN
], acc
[ACCLEN
];
249 spin_lock(&devcgroup
->lock
);
250 list_for_each_entry(wh
, &devcgroup
->whitelist
, list
) {
251 set_access(acc
, wh
->access
);
252 set_majmin(maj
, wh
->major
);
253 set_majmin(min
, wh
->minor
);
254 seq_printf(m
, "%c %s:%s %s\n", type_to_char(wh
->type
),
257 spin_unlock(&devcgroup
->lock
);
263 * may_access_whitelist:
264 * does the access granted to dev_cgroup c contain the access
265 * requested in whitelist item refwh.
266 * return 1 if yes, 0 if no.
267 * call with c->lock held
269 static int may_access_whitelist(struct dev_cgroup
*c
,
270 struct dev_whitelist_item
*refwh
)
272 struct dev_whitelist_item
*whitem
;
274 list_for_each_entry(whitem
, &c
->whitelist
, list
) {
275 if (whitem
->type
& DEV_ALL
)
277 if ((refwh
->type
& DEV_BLOCK
) && !(whitem
->type
& DEV_BLOCK
))
279 if ((refwh
->type
& DEV_CHAR
) && !(whitem
->type
& DEV_CHAR
))
281 if (whitem
->major
!= ~0 && whitem
->major
!= refwh
->major
)
283 if (whitem
->minor
!= ~0 && whitem
->minor
!= refwh
->minor
)
285 if (refwh
->access
& (~(whitem
->access
| ACC_MASK
)))
294 * when adding a new allow rule to a device whitelist, the rule
295 * must be allowed in the parent device
297 static int parent_has_perm(struct cgroup
*childcg
,
298 struct dev_whitelist_item
*wh
)
300 struct cgroup
*pcg
= childcg
->parent
;
301 struct dev_cgroup
*parent
;
306 parent
= cgroup_to_devcgroup(pcg
);
307 spin_lock(&parent
->lock
);
308 ret
= may_access_whitelist(parent
, wh
);
309 spin_unlock(&parent
->lock
);
314 * Modify the whitelist using allow/deny rules.
315 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
316 * so we can give a container CAP_MKNOD to let it create devices but not
317 * modify the whitelist.
318 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
319 * us to also grant CAP_SYS_ADMIN to containers without giving away the
320 * device whitelist controls, but for now we'll stick with CAP_SYS_ADMIN
322 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
323 * new access is only allowed if you're in the top-level cgroup, or your
324 * parent cgroup has the access you're asking for.
326 static ssize_t
devcgroup_access_write(struct cgroup
*cgroup
, struct cftype
*cft
,
327 struct file
*file
, const char __user
*userbuf
,
328 size_t nbytes
, loff_t
*ppos
)
330 struct cgroup
*cur_cgroup
;
331 struct dev_cgroup
*devcgroup
, *cur_devcgroup
;
332 int filetype
= cft
->private;
334 int retval
= 0, count
;
335 struct dev_whitelist_item wh
;
337 if (!capable(CAP_SYS_ADMIN
))
340 devcgroup
= cgroup_to_devcgroup(cgroup
);
341 cur_cgroup
= task_cgroup(current
, devices_subsys
.subsys_id
);
342 cur_devcgroup
= cgroup_to_devcgroup(cur_cgroup
);
344 buffer
= kmalloc(nbytes
+1, GFP_KERNEL
);
348 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
352 buffer
[nbytes
] = 0; /* nul-terminate */
355 if (cgroup_is_removed(cgroup
)) {
360 memset(&wh
, 0, sizeof(wh
));
366 wh
.access
= ACC_MASK
;
387 } else if (isdigit(*b
)) {
389 while (isdigit(*b
)) {
390 wh
.major
= wh
.major
*10+(*b
-'0');
407 } else if (isdigit(*b
)) {
409 while (isdigit(*b
)) {
410 wh
.minor
= wh
.minor
*10+(*b
-'0');
421 for (b
++, count
= 0; count
< 3; count
++, b
++) {
424 wh
.access
|= ACC_READ
;
427 wh
.access
|= ACC_WRITE
;
430 wh
.access
|= ACC_MKNOD
;
446 if (!parent_has_perm(cgroup
, &wh
))
449 retval
= dev_whitelist_add(devcgroup
, &wh
);
452 dev_whitelist_rm(devcgroup
, &wh
);
469 static struct cftype dev_cgroup_files
[] = {
472 .write
= devcgroup_access_write
,
473 .private = DEVCG_ALLOW
,
477 .write
= devcgroup_access_write
,
478 .private = DEVCG_DENY
,
482 .read_seq_string
= devcgroup_seq_read
,
483 .private = DEVCG_LIST
,
487 static int devcgroup_populate(struct cgroup_subsys
*ss
,
488 struct cgroup
*cgroup
)
490 return cgroup_add_files(cgroup
, ss
, dev_cgroup_files
,
491 ARRAY_SIZE(dev_cgroup_files
));
494 struct cgroup_subsys devices_subsys
= {
496 .can_attach
= devcgroup_can_attach
,
497 .create
= devcgroup_create
,
498 .destroy
= devcgroup_destroy
,
499 .populate
= devcgroup_populate
,
500 .subsys_id
= devices_subsys_id
,
503 int devcgroup_inode_permission(struct inode
*inode
, int mask
)
505 struct cgroup
*cgroup
;
506 struct dev_cgroup
*dev_cgroup
;
507 struct dev_whitelist_item
*wh
;
509 dev_t device
= inode
->i_rdev
;
512 if (!S_ISBLK(inode
->i_mode
) && !S_ISCHR(inode
->i_mode
))
514 cgroup
= task_cgroup(current
, devices_subsys
.subsys_id
);
515 dev_cgroup
= cgroup_to_devcgroup(cgroup
);
519 spin_lock(&dev_cgroup
->lock
);
520 list_for_each_entry(wh
, &dev_cgroup
->whitelist
, list
) {
521 if (wh
->type
& DEV_ALL
)
523 if ((wh
->type
& DEV_BLOCK
) && !S_ISBLK(inode
->i_mode
))
525 if ((wh
->type
& DEV_CHAR
) && !S_ISCHR(inode
->i_mode
))
527 if (wh
->major
!= ~0 && wh
->major
!= imajor(inode
))
529 if (wh
->minor
!= ~0 && wh
->minor
!= iminor(inode
))
532 if ((mask
& MAY_WRITE
) && !(wh
->access
& ACC_WRITE
))
534 if ((mask
& MAY_READ
) && !(wh
->access
& ACC_READ
))
536 spin_unlock(&dev_cgroup
->lock
);
539 spin_unlock(&dev_cgroup
->lock
);
544 int devcgroup_inode_mknod(int mode
, dev_t dev
)
546 struct cgroup
*cgroup
;
547 struct dev_cgroup
*dev_cgroup
;
548 struct dev_whitelist_item
*wh
;
550 cgroup
= task_cgroup(current
, devices_subsys
.subsys_id
);
551 dev_cgroup
= cgroup_to_devcgroup(cgroup
);
555 spin_lock(&dev_cgroup
->lock
);
556 list_for_each_entry(wh
, &dev_cgroup
->whitelist
, list
) {
557 if (wh
->type
& DEV_ALL
)
559 if ((wh
->type
& DEV_BLOCK
) && !S_ISBLK(mode
))
561 if ((wh
->type
& DEV_CHAR
) && !S_ISCHR(mode
))
563 if (wh
->major
!= ~0 && wh
->major
!= MAJOR(dev
))
565 if (wh
->minor
!= ~0 && wh
->minor
!= MINOR(dev
))
568 if (!(wh
->access
& ACC_MKNOD
))
570 spin_unlock(&dev_cgroup
->lock
);
573 spin_unlock(&dev_cgroup
->lock
);