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
*css_to_devcgroup(struct cgroup_subsys_state
*s
)
54 return container_of(s
, struct dev_cgroup
, css
);
57 static inline struct dev_cgroup
*cgroup_to_devcgroup(struct cgroup
*cgroup
)
59 return css_to_devcgroup(cgroup_subsys_state(cgroup
, devices_subsys_id
));
62 struct cgroup_subsys devices_subsys
;
64 static int devcgroup_can_attach(struct cgroup_subsys
*ss
,
65 struct cgroup
*new_cgroup
, struct task_struct
*task
)
67 if (current
!= task
&& !capable(CAP_SYS_ADMIN
))
74 * called under cgroup_lock()
76 static int dev_whitelist_copy(struct list_head
*dest
, struct list_head
*orig
)
78 struct dev_whitelist_item
*wh
, *tmp
, *new;
80 list_for_each_entry(wh
, orig
, list
) {
81 new = kmalloc(sizeof(*wh
), GFP_KERNEL
);
84 new->major
= wh
->major
;
85 new->minor
= wh
->minor
;
87 new->access
= wh
->access
;
88 list_add_tail(&new->list
, dest
);
94 list_for_each_entry_safe(wh
, tmp
, dest
, list
) {
101 /* Stupid prototype - don't bother combining existing entries */
103 * called under cgroup_lock()
104 * since the list is visible to other tasks, we need the spinlock also
106 static int dev_whitelist_add(struct dev_cgroup
*dev_cgroup
,
107 struct dev_whitelist_item
*wh
)
109 struct dev_whitelist_item
*whcopy
, *walk
;
111 whcopy
= kmalloc(sizeof(*whcopy
), GFP_KERNEL
);
115 memcpy(whcopy
, wh
, sizeof(*whcopy
));
116 spin_lock(&dev_cgroup
->lock
);
117 list_for_each_entry(walk
, &dev_cgroup
->whitelist
, list
) {
118 if (walk
->type
!= wh
->type
)
120 if (walk
->major
!= wh
->major
)
122 if (walk
->minor
!= wh
->minor
)
125 walk
->access
|= wh
->access
;
131 list_add_tail(&whcopy
->list
, &dev_cgroup
->whitelist
);
132 spin_unlock(&dev_cgroup
->lock
);
137 * called under cgroup_lock()
138 * since the list is visible to other tasks, we need the spinlock also
140 static void dev_whitelist_rm(struct dev_cgroup
*dev_cgroup
,
141 struct dev_whitelist_item
*wh
)
143 struct dev_whitelist_item
*walk
, *tmp
;
145 spin_lock(&dev_cgroup
->lock
);
146 list_for_each_entry_safe(walk
, tmp
, &dev_cgroup
->whitelist
, list
) {
147 if (walk
->type
== DEV_ALL
)
149 if (walk
->type
!= wh
->type
)
151 if (walk
->major
!= ~0 && walk
->major
!= wh
->major
)
153 if (walk
->minor
!= ~0 && walk
->minor
!= wh
->minor
)
157 walk
->access
&= ~wh
->access
;
159 list_del(&walk
->list
);
163 spin_unlock(&dev_cgroup
->lock
);
167 * called from kernel/cgroup.c with cgroup_lock() held.
169 static struct cgroup_subsys_state
*devcgroup_create(struct cgroup_subsys
*ss
,
170 struct cgroup
*cgroup
)
172 struct dev_cgroup
*dev_cgroup
, *parent_dev_cgroup
;
173 struct cgroup
*parent_cgroup
;
176 dev_cgroup
= kzalloc(sizeof(*dev_cgroup
), GFP_KERNEL
);
178 return ERR_PTR(-ENOMEM
);
179 INIT_LIST_HEAD(&dev_cgroup
->whitelist
);
180 parent_cgroup
= cgroup
->parent
;
182 if (parent_cgroup
== NULL
) {
183 struct dev_whitelist_item
*wh
;
184 wh
= kmalloc(sizeof(*wh
), GFP_KERNEL
);
187 return ERR_PTR(-ENOMEM
);
189 wh
->minor
= wh
->major
= ~0;
191 wh
->access
= ACC_MKNOD
| ACC_READ
| ACC_WRITE
;
192 list_add(&wh
->list
, &dev_cgroup
->whitelist
);
194 parent_dev_cgroup
= cgroup_to_devcgroup(parent_cgroup
);
195 ret
= dev_whitelist_copy(&dev_cgroup
->whitelist
,
196 &parent_dev_cgroup
->whitelist
);
203 spin_lock_init(&dev_cgroup
->lock
);
204 return &dev_cgroup
->css
;
207 static void devcgroup_destroy(struct cgroup_subsys
*ss
,
208 struct cgroup
*cgroup
)
210 struct dev_cgroup
*dev_cgroup
;
211 struct dev_whitelist_item
*wh
, *tmp
;
213 dev_cgroup
= cgroup_to_devcgroup(cgroup
);
214 list_for_each_entry_safe(wh
, tmp
, &dev_cgroup
->whitelist
, list
) {
221 #define DEVCG_ALLOW 1
228 static void set_access(char *acc
, short access
)
231 memset(acc
, 0, ACCLEN
);
232 if (access
& ACC_READ
)
234 if (access
& ACC_WRITE
)
236 if (access
& ACC_MKNOD
)
240 static char type_to_char(short type
)
244 if (type
== DEV_CHAR
)
246 if (type
== DEV_BLOCK
)
251 static void set_majmin(char *str
, unsigned m
)
253 memset(str
, 0, MAJMINLEN
);
257 snprintf(str
, MAJMINLEN
, "%d", m
);
260 static int devcgroup_seq_read(struct cgroup
*cgroup
, struct cftype
*cft
,
263 struct dev_cgroup
*devcgroup
= cgroup_to_devcgroup(cgroup
);
264 struct dev_whitelist_item
*wh
;
265 char maj
[MAJMINLEN
], min
[MAJMINLEN
], acc
[ACCLEN
];
267 spin_lock(&devcgroup
->lock
);
268 list_for_each_entry(wh
, &devcgroup
->whitelist
, list
) {
269 set_access(acc
, wh
->access
);
270 set_majmin(maj
, wh
->major
);
271 set_majmin(min
, wh
->minor
);
272 seq_printf(m
, "%c %s:%s %s\n", type_to_char(wh
->type
),
275 spin_unlock(&devcgroup
->lock
);
281 * may_access_whitelist:
282 * does the access granted to dev_cgroup c contain the access
283 * requested in whitelist item refwh.
284 * return 1 if yes, 0 if no.
285 * call with c->lock held
287 static int may_access_whitelist(struct dev_cgroup
*c
,
288 struct dev_whitelist_item
*refwh
)
290 struct dev_whitelist_item
*whitem
;
292 list_for_each_entry(whitem
, &c
->whitelist
, list
) {
293 if (whitem
->type
& DEV_ALL
)
295 if ((refwh
->type
& DEV_BLOCK
) && !(whitem
->type
& DEV_BLOCK
))
297 if ((refwh
->type
& DEV_CHAR
) && !(whitem
->type
& DEV_CHAR
))
299 if (whitem
->major
!= ~0 && whitem
->major
!= refwh
->major
)
301 if (whitem
->minor
!= ~0 && whitem
->minor
!= refwh
->minor
)
303 if (refwh
->access
& (~(whitem
->access
| ACC_MASK
)))
312 * when adding a new allow rule to a device whitelist, the rule
313 * must be allowed in the parent device
315 static int parent_has_perm(struct cgroup
*childcg
,
316 struct dev_whitelist_item
*wh
)
318 struct cgroup
*pcg
= childcg
->parent
;
319 struct dev_cgroup
*parent
;
324 parent
= cgroup_to_devcgroup(pcg
);
325 spin_lock(&parent
->lock
);
326 ret
= may_access_whitelist(parent
, wh
);
327 spin_unlock(&parent
->lock
);
332 * Modify the whitelist using allow/deny rules.
333 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
334 * so we can give a container CAP_MKNOD to let it create devices but not
335 * modify the whitelist.
336 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
337 * us to also grant CAP_SYS_ADMIN to containers without giving away the
338 * device whitelist controls, but for now we'll stick with CAP_SYS_ADMIN
340 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
341 * new access is only allowed if you're in the top-level cgroup, or your
342 * parent cgroup has the access you're asking for.
344 static ssize_t
devcgroup_access_write(struct cgroup
*cgroup
, struct cftype
*cft
,
345 struct file
*file
, const char __user
*userbuf
,
346 size_t nbytes
, loff_t
*ppos
)
348 struct cgroup
*cur_cgroup
;
349 struct dev_cgroup
*devcgroup
, *cur_devcgroup
;
350 int filetype
= cft
->private;
352 int retval
= 0, count
;
353 struct dev_whitelist_item wh
;
355 if (!capable(CAP_SYS_ADMIN
))
358 devcgroup
= cgroup_to_devcgroup(cgroup
);
359 cur_cgroup
= task_cgroup(current
, devices_subsys
.subsys_id
);
360 cur_devcgroup
= cgroup_to_devcgroup(cur_cgroup
);
362 buffer
= kmalloc(nbytes
+1, GFP_KERNEL
);
366 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
370 buffer
[nbytes
] = 0; /* nul-terminate */
373 if (cgroup_is_removed(cgroup
)) {
378 memset(&wh
, 0, sizeof(wh
));
384 wh
.access
= ACC_MASK
;
407 } else if (isdigit(*b
)) {
409 while (isdigit(*b
)) {
410 wh
.major
= wh
.major
*10+(*b
-'0');
427 } else if (isdigit(*b
)) {
429 while (isdigit(*b
)) {
430 wh
.minor
= wh
.minor
*10+(*b
-'0');
441 for (b
++, count
= 0; count
< 3; count
++, b
++) {
444 wh
.access
|= ACC_READ
;
447 wh
.access
|= ACC_WRITE
;
450 wh
.access
|= ACC_MKNOD
;
466 if (!parent_has_perm(cgroup
, &wh
))
469 retval
= dev_whitelist_add(devcgroup
, &wh
);
472 dev_whitelist_rm(devcgroup
, &wh
);
489 static struct cftype dev_cgroup_files
[] = {
492 .write
= devcgroup_access_write
,
493 .private = DEVCG_ALLOW
,
497 .write
= devcgroup_access_write
,
498 .private = DEVCG_DENY
,
502 .read_seq_string
= devcgroup_seq_read
,
503 .private = DEVCG_LIST
,
507 static int devcgroup_populate(struct cgroup_subsys
*ss
,
508 struct cgroup
*cgroup
)
510 return cgroup_add_files(cgroup
, ss
, dev_cgroup_files
,
511 ARRAY_SIZE(dev_cgroup_files
));
514 struct cgroup_subsys devices_subsys
= {
516 .can_attach
= devcgroup_can_attach
,
517 .create
= devcgroup_create
,
518 .destroy
= devcgroup_destroy
,
519 .populate
= devcgroup_populate
,
520 .subsys_id
= devices_subsys_id
,
523 int devcgroup_inode_permission(struct inode
*inode
, int mask
)
525 struct dev_cgroup
*dev_cgroup
;
526 struct dev_whitelist_item
*wh
;
528 dev_t device
= inode
->i_rdev
;
531 if (!S_ISBLK(inode
->i_mode
) && !S_ISCHR(inode
->i_mode
))
533 dev_cgroup
= css_to_devcgroup(task_subsys_state(current
,
538 spin_lock(&dev_cgroup
->lock
);
539 list_for_each_entry(wh
, &dev_cgroup
->whitelist
, list
) {
540 if (wh
->type
& DEV_ALL
)
542 if ((wh
->type
& DEV_BLOCK
) && !S_ISBLK(inode
->i_mode
))
544 if ((wh
->type
& DEV_CHAR
) && !S_ISCHR(inode
->i_mode
))
546 if (wh
->major
!= ~0 && wh
->major
!= imajor(inode
))
548 if (wh
->minor
!= ~0 && wh
->minor
!= iminor(inode
))
551 if ((mask
& MAY_WRITE
) && !(wh
->access
& ACC_WRITE
))
553 if ((mask
& MAY_READ
) && !(wh
->access
& ACC_READ
))
555 spin_unlock(&dev_cgroup
->lock
);
558 spin_unlock(&dev_cgroup
->lock
);
563 int devcgroup_inode_mknod(int mode
, dev_t dev
)
565 struct dev_cgroup
*dev_cgroup
;
566 struct dev_whitelist_item
*wh
;
568 dev_cgroup
= css_to_devcgroup(task_subsys_state(current
,
573 spin_lock(&dev_cgroup
->lock
);
574 list_for_each_entry(wh
, &dev_cgroup
->whitelist
, list
) {
575 if (wh
->type
& DEV_ALL
)
577 if ((wh
->type
& DEV_BLOCK
) && !S_ISBLK(mode
))
579 if ((wh
->type
& DEV_CHAR
) && !S_ISCHR(mode
))
581 if (wh
->major
!= ~0 && wh
->major
!= MAJOR(dev
))
583 if (wh
->minor
!= ~0 && wh
->minor
!= MINOR(dev
))
586 if (!(wh
->access
& ACC_MKNOD
))
588 spin_unlock(&dev_cgroup
->lock
);
591 spin_unlock(&dev_cgroup
->lock
);