Skip to content

Commit 08ce5f1

Browse files
Serge E. Hallyntorvalds
authored andcommitted
cgroups: implement device whitelist
Implement a cgroup to track and enforce open and mknod restrictions on device files. A device cgroup associates a device access whitelist with each cgroup. A whitelist entry has 4 fields. 'type' is a (all), c (char), or b (block). 'all' means it applies to all types and all major and minor numbers. Major and minor are either an integer or * for all. Access is a composition of r (read), w (write), and m (mknod). The root device cgroup starts with rwm to 'all'. A child devcg gets a copy of the parent. Admins can then remove devices from the whitelist or add new entries. A child cgroup can never receive a device access which is denied its parent. However when a device access is removed from a parent it will not also be removed from the child(ren). An entry is added using devices.allow, and removed using devices.deny. For instance echo 'c 1:3 mr' > /cgroups/1/devices.allow allows cgroup 1 to read and mknod the device usually known as /dev/null. Doing echo a > /cgroups/1/devices.deny will remove the default 'a *:* mrw' entry. CAP_SYS_ADMIN is needed to change permissions or move another task to a new cgroup. A cgroup may not be granted more permissions than the cgroup's parent has. Any task can move itself between cgroups. This won't be sufficient, but we can decide the best way to adequately restrict movement later. [akpm@linux-foundation.org: coding-style fixes] [akpm@linux-foundation.org: fix may-be-used-uninitialized warning] Signed-off-by: Serge E. Hallyn <serue@us.ibm.com> Acked-by: James Morris <jmorris@namei.org> Looks-good-to: Pavel Emelyanov <xemul@openvz.org> Cc: Daniel Hokka Zakrisson <daniel@hozac.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Paul Menage <menage@google.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d447ea2 commit 08ce5f1

7 files changed

Lines changed: 686 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Device Whitelist Controller
2+
3+
1. Description:
4+
5+
Implement a cgroup to track and enforce open and mknod restrictions
6+
on device files. A device cgroup associates a device access
7+
whitelist with each cgroup. A whitelist entry has 4 fields.
8+
'type' is a (all), c (char), or b (block). 'all' means it applies
9+
to all types and all major and minor numbers. Major and minor are
10+
either an integer or * for all. Access is a composition of r
11+
(read), w (write), and m (mknod).
12+
13+
The root device cgroup starts with rwm to 'all'. A child device
14+
cgroup gets a copy of the parent. Administrators can then remove
15+
devices from the whitelist or add new entries. A child cgroup can
16+
never receive a device access which is denied its parent. However
17+
when a device access is removed from a parent it will not also be
18+
removed from the child(ren).
19+
20+
2. User Interface
21+
22+
An entry is added using devices.allow, and removed using
23+
devices.deny. For instance
24+
25+
echo 'c 1:3 mr' > /cgroups/1/devices.allow
26+
27+
allows cgroup 1 to read and mknod the device usually known as
28+
/dev/null. Doing
29+
30+
echo a > /cgroups/1/devices.deny
31+
32+
will remove the default 'a *:* mrw' entry.
33+
34+
3. Security
35+
36+
Any task can move itself between cgroups. This clearly won't
37+
suffice, but we can decide the best way to adequately restrict
38+
movement as people get some experience with this. We may just want
39+
to require CAP_SYS_ADMIN, which at least is a separate bit from
40+
CAP_MKNOD. We may want to just refuse moving to a cgroup which
41+
isn't a descendent of the current one. Or we may want to use
42+
CAP_MAC_ADMIN, since we really are trying to lock down root.
43+
44+
CAP_SYS_ADMIN is needed to modify the whitelist or move another
45+
task to a new cgroup. (Again we'll probably want to change that).
46+
47+
A cgroup may not be granted more permissions than the cgroup's
48+
parent has.

‎fs/namei.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/capability.h>
3131
#include <linux/file.h>
3232
#include <linux/fcntl.h>
33+
#include <linux/device_cgroup.h>
3334
#include <asm/namei.h>
3435
#include <asm/uaccess.h>
3536

@@ -281,6 +282,10 @@ int permission(struct inode *inode, int mask, struct nameidata *nd)
281282
if (retval)
282283
return retval;
283284

285+
retval = devcgroup_inode_permission(inode, mask);
286+
if (retval)
287+
return retval;
288+
284289
return security_inode_permission(inode, mask, nd);
285290
}
286291

@@ -2028,6 +2033,10 @@ int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
20282033
if (!dir->i_op || !dir->i_op->mknod)
20292034
return -EPERM;
20302035

2036+
error = devcgroup_inode_mknod(mode, dev);
2037+
if (error)
2038+
return error;
2039+
20312040
error = security_inode_mknod(dir, dentry, mode, dev);
20322041
if (error)
20332042
return error;

‎include/linux/cgroup_subsys.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ SUBSYS(mem_cgroup)
4242
#endif
4343

4444
/* */
45+
46+
#ifdef CONFIG_CGROUP_DEVICE
47+
SUBSYS(devices)
48+
#endif
49+
50+
/* */

‎include/linux/device_cgroup.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <linux/module.h>
2+
#include <linux/fs.h>
3+
4+
#ifdef CONFIG_CGROUP_DEVICE
5+
extern int devcgroup_inode_permission(struct inode *inode, int mask);
6+
extern int devcgroup_inode_mknod(int mode, dev_t dev);
7+
#else
8+
static inline int devcgroup_inode_permission(struct inode *inode, int mask)
9+
{ return 0; }
10+
static inline int devcgroup_inode_mknod(int mode, dev_t dev)
11+
{ return 0; }
12+
#endif

‎init/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ config CGROUP_NS
298298
for instance virtual servers and checkpoint/restart
299299
jobs.
300300

301+
config CGROUP_DEVICE
302+
bool "Device controller for cgroups"
303+
depends on CGROUPS && EXPERIMENTAL
304+
help
305+
Provides a cgroup implementing whitelists for devices which
306+
a process in the cgroup can mknod or open.
307+
301308
config CPUSETS
302309
bool "Cpuset support"
303310
depends on SMP && CGROUPS

‎security/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ obj-$(CONFIG_SECURITY_SELINUX) += selinux/built-in.o
1818
obj-$(CONFIG_SECURITY_SMACK) += commoncap.o smack/built-in.o
1919
obj-$(CONFIG_SECURITY_CAPABILITIES) += commoncap.o capability.o
2020
obj-$(CONFIG_SECURITY_ROOTPLUG) += commoncap.o root_plug.o
21+
obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o

0 commit comments

Comments
 (0)