blob: 44dfc415a379afc9c327388664e949bfcc855bcb [file] [log] [blame]
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07001/*
Lai Jiangshan47c59802008-10-18 20:28:07 -07002 * device_cgroup.c - device cgroup subsystem
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003 *
4 * Copyright 2007 IBM Corp
5 */
6
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>
Serge E. Hallyn29486df2008-04-29 01:00:14 -070012#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Lai Jiangshan47c59802008-10-18 20:28:07 -070014#include <linux/rcupdate.h>
Li Zefanb4046f02009-04-02 16:57:32 -070015#include <linux/mutex.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070016
17#define ACC_MKNOD 1
18#define ACC_READ 2
19#define ACC_WRITE 4
20#define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
21
22#define DEV_BLOCK 1
23#define DEV_CHAR 2
24#define DEV_ALL 4 /* this represents all devices */
25
Li Zefanb4046f02009-04-02 16:57:32 -070026static DEFINE_MUTEX(devcgroup_mutex);
27
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070028/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070029 * exception list locking rules:
Li Zefanb4046f02009-04-02 16:57:32 -070030 * hold devcgroup_mutex for update/read.
Lai Jiangshan47c59802008-10-18 20:28:07 -070031 * hold rcu_read_lock() for read.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070032 */
33
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070034struct dev_exception_item {
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070035 u32 major, minor;
36 short type;
37 short access;
38 struct list_head list;
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -070039 struct rcu_head rcu;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070040};
41
42struct dev_cgroup {
43 struct cgroup_subsys_state css;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070044 struct list_head exceptions;
Aristeu Rozanski66b8ef62012-10-04 17:15:13 -070045 bool deny_all;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070046};
47
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070048static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
49{
50 return container_of(s, struct dev_cgroup, css);
51}
52
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070053static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
54{
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070055 return css_to_devcgroup(cgroup_subsys_state(cgroup, devices_subsys_id));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070056}
57
Paul Menagef92523e2008-07-25 01:47:03 -070058static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
59{
60 return css_to_devcgroup(task_subsys_state(task, devices_subsys_id));
61}
62
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070063struct cgroup_subsys devices_subsys;
64
Li Zefan761b3ef2012-01-31 13:47:36 +080065static int devcgroup_can_attach(struct cgroup *new_cgrp,
66 struct cgroup_taskset *set)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070067{
Tejun Heo2f7ee562011-12-12 18:12:21 -080068 struct task_struct *task = cgroup_taskset_first(set);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070069
Tejun Heo2f7ee562011-12-12 18:12:21 -080070 if (current != task && !capable(CAP_SYS_ADMIN))
71 return -EPERM;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070072 return 0;
73}
74
75/*
Li Zefanb4046f02009-04-02 16:57:32 -070076 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070077 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070078static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070079{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070080 struct dev_exception_item *ex, *tmp, *new;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070081
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070082 list_for_each_entry(ex, orig, list) {
83 new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070084 if (!new)
85 goto free_and_exit;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070086 list_add_tail(&new->list, dest);
87 }
88
89 return 0;
90
91free_and_exit:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070092 list_for_each_entry_safe(ex, tmp, dest, list) {
93 list_del(&ex->list);
94 kfree(ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070095 }
96 return -ENOMEM;
97}
98
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070099/*
Li Zefanb4046f02009-04-02 16:57:32 -0700100 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700101 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700102static int dev_exception_add(struct dev_cgroup *dev_cgroup,
103 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700104{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700105 struct dev_exception_item *excopy, *walk;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700106
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700107 excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
108 if (!excopy)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700109 return -ENOMEM;
110
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700111 list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
112 if (walk->type != ex->type)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700113 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700114 if (walk->major != ex->major)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700115 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700116 if (walk->minor != ex->minor)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700117 continue;
118
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700119 walk->access |= ex->access;
120 kfree(excopy);
121 excopy = NULL;
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700122 }
123
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700124 if (excopy != NULL)
125 list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700126 return 0;
127}
128
129/*
Li Zefanb4046f02009-04-02 16:57:32 -0700130 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700131 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700132static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
133 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700134{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700135 struct dev_exception_item *walk, *tmp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700136
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700137 list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
138 if (walk->type != ex->type)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700139 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700140 if (walk->major != ex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700141 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700142 if (walk->minor != ex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700143 continue;
144
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700145 walk->access &= ~ex->access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700146 if (!walk->access) {
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700147 list_del_rcu(&walk->list);
Lai Jiangshan6034f7e2011-03-15 18:07:57 +0800148 kfree_rcu(walk, rcu);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700149 }
150 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700151}
152
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700153/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700154 * dev_exception_clean - frees all entries of the exception list
155 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700156 *
157 * called under devcgroup_mutex
158 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700159static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700160{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700161 struct dev_exception_item *ex, *tmp;
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700162
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700163 list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
164 list_del(&ex->list);
165 kfree(ex);
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700166 }
167}
168
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700169/*
170 * called from kernel/cgroup.c with cgroup_lock() held.
171 */
Li Zefan761b3ef2012-01-31 13:47:36 +0800172static struct cgroup_subsys_state *devcgroup_create(struct cgroup *cgroup)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700173{
174 struct dev_cgroup *dev_cgroup, *parent_dev_cgroup;
175 struct cgroup *parent_cgroup;
176 int ret;
177
178 dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
179 if (!dev_cgroup)
180 return ERR_PTR(-ENOMEM);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700181 INIT_LIST_HEAD(&dev_cgroup->exceptions);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700182 parent_cgroup = cgroup->parent;
183
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700184 if (parent_cgroup == NULL)
Aristeu Rozanski66b8ef62012-10-04 17:15:13 -0700185 dev_cgroup->deny_all = false;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700186 else {
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700187 parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
Li Zefanb4046f02009-04-02 16:57:32 -0700188 mutex_lock(&devcgroup_mutex);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700189 ret = dev_exceptions_copy(&dev_cgroup->exceptions,
190 &parent_dev_cgroup->exceptions);
Aristeu Rozanski66b8ef62012-10-04 17:15:13 -0700191 dev_cgroup->deny_all = parent_dev_cgroup->deny_all;
Li Zefanb4046f02009-04-02 16:57:32 -0700192 mutex_unlock(&devcgroup_mutex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700193 if (ret) {
194 kfree(dev_cgroup);
195 return ERR_PTR(ret);
196 }
197 }
198
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700199 return &dev_cgroup->css;
200}
201
Li Zefan761b3ef2012-01-31 13:47:36 +0800202static void devcgroup_destroy(struct cgroup *cgroup)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700203{
204 struct dev_cgroup *dev_cgroup;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700205
206 dev_cgroup = cgroup_to_devcgroup(cgroup);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700207 dev_exception_clean(dev_cgroup);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700208 kfree(dev_cgroup);
209}
210
211#define DEVCG_ALLOW 1
212#define DEVCG_DENY 2
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700213#define DEVCG_LIST 3
214
Li Zefan17d213f2008-07-13 12:14:02 -0700215#define MAJMINLEN 13
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700216#define ACCLEN 4
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700217
218static void set_access(char *acc, short access)
219{
220 int idx = 0;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700221 memset(acc, 0, ACCLEN);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700222 if (access & ACC_READ)
223 acc[idx++] = 'r';
224 if (access & ACC_WRITE)
225 acc[idx++] = 'w';
226 if (access & ACC_MKNOD)
227 acc[idx++] = 'm';
228}
229
230static char type_to_char(short type)
231{
232 if (type == DEV_ALL)
233 return 'a';
234 if (type == DEV_CHAR)
235 return 'c';
236 if (type == DEV_BLOCK)
237 return 'b';
238 return 'X';
239}
240
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700241static void set_majmin(char *str, unsigned m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700242{
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700243 if (m == ~0)
Li Zefan7759fc92008-07-25 01:47:08 -0700244 strcpy(str, "*");
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700245 else
Li Zefan7759fc92008-07-25 01:47:08 -0700246 sprintf(str, "%u", m);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700247}
248
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700249static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
250 struct seq_file *m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700251{
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700252 struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700253 struct dev_exception_item *ex;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700254 char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700255
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700256 rcu_read_lock();
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700257 /*
258 * To preserve the compatibility:
259 * - Only show the "all devices" when the default policy is to allow
260 * - List the exceptions in case the default policy is to deny
261 * This way, the file remains as a "whitelist of devices"
262 */
263 if (devcgroup->deny_all == false) {
264 set_access(acc, ACC_MASK);
265 set_majmin(maj, ~0);
266 set_majmin(min, ~0);
267 seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700268 maj, min, acc);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700269 } else {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700270 list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
271 set_access(acc, ex->access);
272 set_majmin(maj, ex->major);
273 set_majmin(min, ex->minor);
274 seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700275 maj, min, acc);
276 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700277 }
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700278 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700279
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700280 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700281}
282
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700283/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700284 * may_access - verifies if a new exception is part of what is allowed
285 * by a dev cgroup based on the default policy +
286 * exceptions. This is used to make sure a child cgroup
287 * won't have more privileges than its parent or to
288 * verify if a certain access is allowed.
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700289 * @dev_cgroup: dev cgroup to be tested against
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700290 * @refex: new exception
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700291 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700292static int may_access(struct dev_cgroup *dev_cgroup,
293 struct dev_exception_item *refex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700294{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700295 struct dev_exception_item *ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700296 bool match = false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700297
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700298 list_for_each_entry(ex, &dev_cgroup->exceptions, list) {
299 if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700300 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700301 if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700302 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700303 if (ex->major != ~0 && ex->major != refex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700304 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700305 if (ex->minor != ~0 && ex->minor != refex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700306 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700307 if (refex->access & (~ex->access))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700308 continue;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700309 match = true;
310 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700311 }
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700312
313 /*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700314 * In two cases we'll consider this new exception valid:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700315 * - the dev cgroup has its default policy to allow + exception list:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700316 * the new exception should *not* match any of the exceptions
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700317 * (!deny_all, !match)
318 * - the dev cgroup has its default policy to deny + exception list:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700319 * the new exception *should* match the exceptions
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700320 * (deny_all, match)
321 */
322 if (dev_cgroup->deny_all == match)
323 return 1;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700324 return 0;
325}
326
327/*
328 * parent_has_perm:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700329 * when adding a new allow rule to a device exception list, the rule
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700330 * must be allowed in the parent device
331 */
Paul Menagef92523e2008-07-25 01:47:03 -0700332static int parent_has_perm(struct dev_cgroup *childcg,
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700333 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700334{
Paul Menagef92523e2008-07-25 01:47:03 -0700335 struct cgroup *pcg = childcg->css.cgroup->parent;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700336 struct dev_cgroup *parent;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700337
338 if (!pcg)
339 return 1;
340 parent = cgroup_to_devcgroup(pcg);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700341 return may_access(parent, ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700342}
343
344/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700345 * Modify the exception list using allow/deny rules.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700346 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
347 * so we can give a container CAP_MKNOD to let it create devices but not
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700348 * modify the exception list.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700349 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
350 * us to also grant CAP_SYS_ADMIN to containers without giving away the
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700351 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700352 *
353 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
354 * new access is only allowed if you're in the top-level cgroup, or your
355 * parent cgroup has the access you're asking for.
356 */
Paul Menagef92523e2008-07-25 01:47:03 -0700357static int devcgroup_update_access(struct dev_cgroup *devcgroup,
358 int filetype, const char *buffer)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700359{
Paul Menagef92523e2008-07-25 01:47:03 -0700360 const char *b;
Li Zefan7759fc92008-07-25 01:47:08 -0700361 char *endp;
Li Zefanc012a542008-10-18 20:28:07 -0700362 int count;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700363 struct dev_exception_item ex;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700364
365 if (!capable(CAP_SYS_ADMIN))
366 return -EPERM;
367
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700368 memset(&ex, 0, sizeof(ex));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700369 b = buffer;
370
371 switch (*b) {
372 case 'a':
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700373 switch (filetype) {
374 case DEVCG_ALLOW:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700375 if (!parent_has_perm(devcgroup, &ex))
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700376 return -EPERM;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700377 dev_exception_clean(devcgroup);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700378 devcgroup->deny_all = false;
379 break;
380 case DEVCG_DENY:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700381 dev_exception_clean(devcgroup);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700382 devcgroup->deny_all = true;
383 break;
384 default:
385 return -EINVAL;
386 }
387 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700388 case 'b':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700389 ex.type = DEV_BLOCK;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700390 break;
391 case 'c':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700392 ex.type = DEV_CHAR;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700393 break;
394 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700395 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700396 }
397 b++;
Paul Menagef92523e2008-07-25 01:47:03 -0700398 if (!isspace(*b))
399 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700400 b++;
401 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700402 ex.major = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700403 b++;
404 } else if (isdigit(*b)) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700405 ex.major = simple_strtoul(b, &endp, 10);
Li Zefan7759fc92008-07-25 01:47:08 -0700406 b = endp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700407 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700408 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700409 }
Paul Menagef92523e2008-07-25 01:47:03 -0700410 if (*b != ':')
411 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700412 b++;
413
414 /* read minor */
415 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700416 ex.minor = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700417 b++;
418 } else if (isdigit(*b)) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700419 ex.minor = simple_strtoul(b, &endp, 10);
Li Zefan7759fc92008-07-25 01:47:08 -0700420 b = endp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700421 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700422 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700423 }
Paul Menagef92523e2008-07-25 01:47:03 -0700424 if (!isspace(*b))
425 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700426 for (b++, count = 0; count < 3; count++, b++) {
427 switch (*b) {
428 case 'r':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700429 ex.access |= ACC_READ;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700430 break;
431 case 'w':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700432 ex.access |= ACC_WRITE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700433 break;
434 case 'm':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700435 ex.access |= ACC_MKNOD;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700436 break;
437 case '\n':
438 case '\0':
439 count = 3;
440 break;
441 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700442 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700443 }
444 }
445
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700446 switch (filetype) {
447 case DEVCG_ALLOW:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700448 if (!parent_has_perm(devcgroup, &ex))
Paul Menagef92523e2008-07-25 01:47:03 -0700449 return -EPERM;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700450 /*
451 * If the default policy is to allow by default, try to remove
452 * an matching exception instead. And be silent about it: we
453 * don't want to break compatibility
454 */
455 if (devcgroup->deny_all == false) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700456 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700457 return 0;
458 }
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700459 return dev_exception_add(devcgroup, &ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700460 case DEVCG_DENY:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700461 /*
462 * If the default policy is to deny by default, try to remove
463 * an matching exception instead. And be silent about it: we
464 * don't want to break compatibility
465 */
466 if (devcgroup->deny_all == true) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700467 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700468 return 0;
469 }
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700470 return dev_exception_add(devcgroup, &ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700471 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700472 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700473 }
Paul Menagef92523e2008-07-25 01:47:03 -0700474 return 0;
475}
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700476
Paul Menagef92523e2008-07-25 01:47:03 -0700477static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
478 const char *buffer)
479{
480 int retval;
Li Zefanb4046f02009-04-02 16:57:32 -0700481
482 mutex_lock(&devcgroup_mutex);
Paul Menagef92523e2008-07-25 01:47:03 -0700483 retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
484 cft->private, buffer);
Li Zefanb4046f02009-04-02 16:57:32 -0700485 mutex_unlock(&devcgroup_mutex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700486 return retval;
487}
488
489static struct cftype dev_cgroup_files[] = {
490 {
491 .name = "allow",
Paul Menagef92523e2008-07-25 01:47:03 -0700492 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700493 .private = DEVCG_ALLOW,
494 },
495 {
496 .name = "deny",
Paul Menagef92523e2008-07-25 01:47:03 -0700497 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700498 .private = DEVCG_DENY,
499 },
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700500 {
501 .name = "list",
502 .read_seq_string = devcgroup_seq_read,
503 .private = DEVCG_LIST,
504 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700505 { } /* terminate */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700506};
507
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700508struct cgroup_subsys devices_subsys = {
509 .name = "devices",
510 .can_attach = devcgroup_can_attach,
511 .create = devcgroup_create,
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700512 .destroy = devcgroup_destroy,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700513 .subsys_id = devices_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700514 .base_cftypes = dev_cgroup_files,
Tejun Heo8c7f6ed2012-09-13 12:20:58 -0700515
516 /*
517 * While devices cgroup has the rudimentary hierarchy support which
518 * checks the parent's restriction, it doesn't properly propagates
519 * config changes in ancestors to their descendents. A child
520 * should only be allowed to add more restrictions to the parent's
521 * configuration. Fix it and remove the following.
522 */
523 .broken_hierarchy = true,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700524};
525
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700526/**
527 * __devcgroup_check_permission - checks if an inode operation is permitted
528 * @dev_cgroup: the dev cgroup to be tested against
529 * @type: device type
530 * @major: device major number
531 * @minor: device minor number
532 * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
533 *
534 * returns 0 on success, -EPERM case the operation is not permitted
535 */
536static int __devcgroup_check_permission(struct dev_cgroup *dev_cgroup,
537 short type, u32 major, u32 minor,
538 short access)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700539{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700540 struct dev_exception_item ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700541 int rc;
542
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700543 memset(&ex, 0, sizeof(ex));
544 ex.type = type;
545 ex.major = major;
546 ex.minor = minor;
547 ex.access = access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700548
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700549 rcu_read_lock();
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700550 rc = may_access(dev_cgroup, &ex);
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700551 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700552
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700553 if (!rc)
554 return -EPERM;
555
556 return 0;
557}
558
559int __devcgroup_inode_permission(struct inode *inode, int mask)
560{
561 struct dev_cgroup *dev_cgroup = task_devcgroup(current);
562 short type, access = 0;
563
564 if (S_ISBLK(inode->i_mode))
565 type = DEV_BLOCK;
566 if (S_ISCHR(inode->i_mode))
567 type = DEV_CHAR;
568 if (mask & MAY_WRITE)
569 access |= ACC_WRITE;
570 if (mask & MAY_READ)
571 access |= ACC_READ;
572
573 return __devcgroup_check_permission(dev_cgroup, type, imajor(inode),
574 iminor(inode), access);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700575}
576
577int devcgroup_inode_mknod(int mode, dev_t dev)
578{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700579 struct dev_cgroup *dev_cgroup = task_devcgroup(current);
580 short type;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700581
Serge E. Hallyn0b82ac32009-01-07 18:07:46 -0800582 if (!S_ISBLK(mode) && !S_ISCHR(mode))
583 return 0;
584
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700585 if (S_ISBLK(mode))
586 type = DEV_BLOCK;
587 else
588 type = DEV_CHAR;
Li Zefan36fd71d2008-09-02 14:35:52 -0700589
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700590 return __devcgroup_check_permission(dev_cgroup, type, MAJOR(dev),
591 MINOR(dev), ACC_MKNOD);
Li Zefan36fd71d2008-09-02 14:35:52 -0700592
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700593}