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>
16 #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
20 #define DEV_ALL 4 /* this represents all devices */
23 * whitelist locking rules:
24 * cgroup_lock() cannot be taken under dev_cgroup->lock.
25 * dev_cgroup->lock can be taken with or without cgroup_lock().
27 * modifications always require cgroup_lock
28 * modifications to a list which is visible require the
29 * dev_cgroup->lock *and* cgroup_lock()
30 * walking the list requires dev_cgroup->lock or cgroup_lock().
32 * reasoning: dev_whitelist_copy() needs to kmalloc, so needs
33 * a mutex, which the cgroup_lock() is. Since modifying
34 * a visible list requires both locks, either lock can be
35 * taken for walking the list.
38 struct dev_whitelist_item {
42 struct list_head list;
46 struct cgroup_subsys_state css;
47 struct list_head whitelist;
51 static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
53 return container_of(cgroup_subsys_state(cgroup, devices_subsys_id),
54 struct dev_cgroup, css);
57 struct cgroup_subsys devices_subsys;
59 static int devcgroup_can_attach(struct cgroup_subsys *ss,
60 struct cgroup *new_cgroup, struct task_struct *task)
62 if (current != task && !capable(CAP_SYS_ADMIN))
69 * called under cgroup_lock()
71 static int dev_whitelist_copy(struct list_head *dest, struct list_head *orig)
73 struct dev_whitelist_item *wh, *tmp, *new;
75 list_for_each_entry(wh, orig, list) {
76 new = kmalloc(sizeof(*wh), GFP_KERNEL);
79 new->major = wh->major;
80 new->minor = wh->minor;
82 new->access = wh->access;
83 list_add_tail(&new->list, dest);
89 list_for_each_entry_safe(wh, tmp, dest, list) {
96 /* Stupid prototype - don't bother combining existing entries */
98 * called under cgroup_lock()
99 * since the list is visible to other tasks, we need the spinlock also
101 static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
102 struct dev_whitelist_item *wh)
104 struct dev_whitelist_item *whcopy;
106 whcopy = kmalloc(sizeof(*whcopy), GFP_KERNEL);
110 memcpy(whcopy, wh, sizeof(*whcopy));
111 spin_lock(&dev_cgroup->lock);
112 list_add_tail(&whcopy->list, &dev_cgroup->whitelist);
113 spin_unlock(&dev_cgroup->lock);
118 * called under cgroup_lock()
119 * since the list is visible to other tasks, we need the spinlock also
121 static void dev_whitelist_rm(struct dev_cgroup *dev_cgroup,
122 struct dev_whitelist_item *wh)
124 struct dev_whitelist_item *walk, *tmp;
126 spin_lock(&dev_cgroup->lock);
127 list_for_each_entry_safe(walk, tmp, &dev_cgroup->whitelist, list) {
128 if (walk->type == DEV_ALL)
130 if (walk->type != wh->type)
132 if (walk->major != ~0 && walk->major != wh->major)
134 if (walk->minor != ~0 && walk->minor != wh->minor)
138 walk->access &= ~wh->access;
140 list_del(&walk->list);
144 spin_unlock(&dev_cgroup->lock);
148 * called from kernel/cgroup.c with cgroup_lock() held.
150 static struct cgroup_subsys_state *devcgroup_create(struct cgroup_subsys *ss,
151 struct cgroup *cgroup)
153 struct dev_cgroup *dev_cgroup, *parent_dev_cgroup;
154 struct cgroup *parent_cgroup;
157 dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
159 return ERR_PTR(-ENOMEM);
160 INIT_LIST_HEAD(&dev_cgroup->whitelist);
161 parent_cgroup = cgroup->parent;
163 if (parent_cgroup == NULL) {
164 struct dev_whitelist_item *wh;
165 wh = kmalloc(sizeof(*wh), GFP_KERNEL);
168 return ERR_PTR(-ENOMEM);
170 wh->minor = wh->major = ~0;
172 wh->access = ACC_MKNOD | ACC_READ | ACC_WRITE;
173 list_add(&wh->list, &dev_cgroup->whitelist);
175 parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
176 ret = dev_whitelist_copy(&dev_cgroup->whitelist,
177 &parent_dev_cgroup->whitelist);
184 spin_lock_init(&dev_cgroup->lock);
185 return &dev_cgroup->css;
188 static void devcgroup_destroy(struct cgroup_subsys *ss,
189 struct cgroup *cgroup)
191 struct dev_cgroup *dev_cgroup;
192 struct dev_whitelist_item *wh, *tmp;
194 dev_cgroup = cgroup_to_devcgroup(cgroup);
195 list_for_each_entry_safe(wh, tmp, &dev_cgroup->whitelist, list) {
202 #define DEVCG_ALLOW 1
205 static void set_access(char *acc, short access)
209 if (access & ACC_READ)
211 if (access & ACC_WRITE)
213 if (access & ACC_MKNOD)
217 static char type_to_char(short type)
221 if (type == DEV_CHAR)
223 if (type == DEV_BLOCK)
228 static void set_majmin(char *str, int len, unsigned m)
234 snprintf(str, len, "%d", m);
237 static char *print_whitelist(struct dev_cgroup *devcgroup, int *len)
239 char *buf, *s, acc[4];
240 struct dev_whitelist_item *wh;
243 char maj[10], min[10];
245 buf = kmalloc(4096, GFP_KERNEL);
247 return ERR_PTR(-ENOMEM);
252 spin_lock(&devcgroup->lock);
253 list_for_each_entry(wh, &devcgroup->whitelist, list) {
254 set_access(acc, wh->access);
255 set_majmin(maj, 10, wh->major);
256 set_majmin(min, 10, wh->minor);
257 ret = snprintf(s, 4095-(s-buf), "%c %s:%s %s\n",
258 type_to_char(wh->type), maj, min, acc);
259 if (s+ret >= buf+4095) {
261 buf = ERR_PTR(-ENOMEM);
268 spin_unlock(&devcgroup->lock);
273 static ssize_t devcgroup_access_read(struct cgroup *cgroup,
274 struct cftype *cft, struct file *file,
275 char __user *userbuf, size_t nbytes, loff_t *ppos)
277 struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
278 int filetype = cft->private;
280 int uninitialized_var(len);
283 if (filetype != DEVCG_ALLOW)
285 buffer = print_whitelist(devcgroup, &len);
287 return PTR_ERR(buffer);
289 retval = simple_read_from_buffer(userbuf, nbytes, ppos, buffer, len);
295 * may_access_whitelist:
296 * does the access granted to dev_cgroup c contain the access
297 * requested in whitelist item refwh.
298 * return 1 if yes, 0 if no.
299 * call with c->lock held
301 static int may_access_whitelist(struct dev_cgroup *c,
302 struct dev_whitelist_item *refwh)
304 struct dev_whitelist_item *whitem;
306 list_for_each_entry(whitem, &c->whitelist, list) {
307 if (whitem->type & DEV_ALL)
309 if ((refwh->type & DEV_BLOCK) && !(whitem->type & DEV_BLOCK))
311 if ((refwh->type & DEV_CHAR) && !(whitem->type & DEV_CHAR))
313 if (whitem->major != ~0 && whitem->major != refwh->major)
315 if (whitem->minor != ~0 && whitem->minor != refwh->minor)
317 if (refwh->access & (~(whitem->access | ACC_MASK)))
326 * when adding a new allow rule to a device whitelist, the rule
327 * must be allowed in the parent device
329 static int parent_has_perm(struct cgroup *childcg,
330 struct dev_whitelist_item *wh)
332 struct cgroup *pcg = childcg->parent;
333 struct dev_cgroup *parent;
338 parent = cgroup_to_devcgroup(pcg);
339 spin_lock(&parent->lock);
340 ret = may_access_whitelist(parent, wh);
341 spin_unlock(&parent->lock);
346 * Modify the whitelist using allow/deny rules.
347 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
348 * so we can give a container CAP_MKNOD to let it create devices but not
349 * modify the whitelist.
350 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
351 * us to also grant CAP_SYS_ADMIN to containers without giving away the
352 * device whitelist controls, but for now we'll stick with CAP_SYS_ADMIN
354 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
355 * new access is only allowed if you're in the top-level cgroup, or your
356 * parent cgroup has the access you're asking for.
358 static ssize_t devcgroup_access_write(struct cgroup *cgroup, struct cftype *cft,
359 struct file *file, const char __user *userbuf,
360 size_t nbytes, loff_t *ppos)
362 struct cgroup *cur_cgroup;
363 struct dev_cgroup *devcgroup, *cur_devcgroup;
364 int filetype = cft->private;
366 int retval = 0, count;
367 struct dev_whitelist_item wh;
369 if (!capable(CAP_SYS_ADMIN))
372 devcgroup = cgroup_to_devcgroup(cgroup);
373 cur_cgroup = task_cgroup(current, devices_subsys.subsys_id);
374 cur_devcgroup = cgroup_to_devcgroup(cur_cgroup);
376 buffer = kmalloc(nbytes+1, GFP_KERNEL);
380 if (copy_from_user(buffer, userbuf, nbytes)) {
384 buffer[nbytes] = 0; /* nul-terminate */
387 if (cgroup_is_removed(cgroup)) {
392 memset(&wh, 0, sizeof(wh));
398 wh.access = ACC_MASK;
419 } else if (isdigit(*b)) {
421 while (isdigit(*b)) {
422 wh.major = wh.major*10+(*b-'0');
439 } else if (isdigit(*b)) {
441 while (isdigit(*b)) {
442 wh.minor = wh.minor*10+(*b-'0');
453 for (b++, count = 0; count < 3; count++, b++) {
456 wh.access |= ACC_READ;
459 wh.access |= ACC_WRITE;
462 wh.access |= ACC_MKNOD;
478 if (!parent_has_perm(cgroup, &wh))
481 retval = dev_whitelist_add(devcgroup, &wh);
484 dev_whitelist_rm(devcgroup, &wh);
501 static struct cftype dev_cgroup_files[] = {
504 .read = devcgroup_access_read,
505 .write = devcgroup_access_write,
506 .private = DEVCG_ALLOW,
510 .write = devcgroup_access_write,
511 .private = DEVCG_DENY,
515 static int devcgroup_populate(struct cgroup_subsys *ss,
516 struct cgroup *cgroup)
518 return cgroup_add_files(cgroup, ss, dev_cgroup_files,
519 ARRAY_SIZE(dev_cgroup_files));
522 struct cgroup_subsys devices_subsys = {
524 .can_attach = devcgroup_can_attach,
525 .create = devcgroup_create,
526 .destroy = devcgroup_destroy,
527 .populate = devcgroup_populate,
528 .subsys_id = devices_subsys_id,
531 int devcgroup_inode_permission(struct inode *inode, int mask)
533 struct cgroup *cgroup;
534 struct dev_cgroup *dev_cgroup;
535 struct dev_whitelist_item *wh;
537 dev_t device = inode->i_rdev;
540 if (!S_ISBLK(inode->i_mode) && !S_ISCHR(inode->i_mode))
542 cgroup = task_cgroup(current, devices_subsys.subsys_id);
543 dev_cgroup = cgroup_to_devcgroup(cgroup);
547 spin_lock(&dev_cgroup->lock);
548 list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
549 if (wh->type & DEV_ALL)
551 if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
553 if ((wh->type & DEV_CHAR) && !S_ISCHR(inode->i_mode))
555 if (wh->major != ~0 && wh->major != imajor(inode))
557 if (wh->minor != ~0 && wh->minor != iminor(inode))
560 if ((mask & MAY_WRITE) && !(wh->access & ACC_WRITE))
562 if ((mask & MAY_READ) && !(wh->access & ACC_READ))
564 spin_unlock(&dev_cgroup->lock);
567 spin_unlock(&dev_cgroup->lock);
572 int devcgroup_inode_mknod(int mode, dev_t dev)
574 struct cgroup *cgroup;
575 struct dev_cgroup *dev_cgroup;
576 struct dev_whitelist_item *wh;
578 cgroup = task_cgroup(current, devices_subsys.subsys_id);
579 dev_cgroup = cgroup_to_devcgroup(cgroup);
583 spin_lock(&dev_cgroup->lock);
584 list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
585 if (wh->type & DEV_ALL)
587 if ((wh->type & DEV_BLOCK) && !S_ISBLK(mode))
589 if ((wh->type & DEV_CHAR) && !S_ISCHR(mode))
591 if (wh->major != ~0 && wh->major != MAJOR(dev))
593 if (wh->minor != ~0 && wh->minor != MINOR(dev))
596 if (!(wh->access & ACC_MKNOD))
598 spin_unlock(&dev_cgroup->lock);
601 spin_unlock(&dev_cgroup->lock);