blob: 7c2a0a71049e9be5c22a5792ec35ad266949c114 [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
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -050028enum devcg_behavior {
29 DEVCG_DEFAULT_NONE,
30 DEVCG_DEFAULT_ALLOW,
31 DEVCG_DEFAULT_DENY,
32};
33
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070035 * exception list locking rules:
Li Zefanb4046f02009-04-02 16:57:32 -070036 * hold devcgroup_mutex for update/read.
Lai Jiangshan47c59802008-10-18 20:28:07 -070037 * hold rcu_read_lock() for read.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070038 */
39
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070040struct dev_exception_item {
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070041 u32 major, minor;
42 short type;
43 short access;
44 struct list_head list;
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -070045 struct rcu_head rcu;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070046};
47
48struct dev_cgroup {
49 struct cgroup_subsys_state css;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070050 struct list_head exceptions;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -050051 enum devcg_behavior behavior;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070052};
53
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070054static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
55{
Tejun Heoa7c6d552013-08-08 20:11:23 -040056 return s ? container_of(s, struct dev_cgroup, css) : NULL;
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070057}
58
Paul Menagef92523e2008-07-25 01:47:03 -070059static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
60{
Tejun Heo8af01f52013-08-08 20:11:22 -040061 return css_to_devcgroup(task_css(task, devices_subsys_id));
Paul Menagef92523e2008-07-25 01:47:03 -070062}
63
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070064struct cgroup_subsys devices_subsys;
65
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070066/*
Li Zefanb4046f02009-04-02 16:57:32 -070067 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070068 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070069static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070070{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070071 struct dev_exception_item *ex, *tmp, *new;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070072
Tejun Heo4b1c7842012-11-06 09:16:53 -080073 lockdep_assert_held(&devcgroup_mutex);
74
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070075 list_for_each_entry(ex, orig, list) {
76 new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070077 if (!new)
78 goto free_and_exit;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070079 list_add_tail(&new->list, dest);
80 }
81
82 return 0;
83
84free_and_exit:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070085 list_for_each_entry_safe(ex, tmp, dest, list) {
86 list_del(&ex->list);
87 kfree(ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070088 }
89 return -ENOMEM;
90}
91
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070092/*
Li Zefanb4046f02009-04-02 16:57:32 -070093 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070094 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070095static int dev_exception_add(struct dev_cgroup *dev_cgroup,
96 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070097{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070098 struct dev_exception_item *excopy, *walk;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070099
Tejun Heo4b1c7842012-11-06 09:16:53 -0800100 lockdep_assert_held(&devcgroup_mutex);
101
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700102 excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
103 if (!excopy)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700104 return -ENOMEM;
105
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700106 list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
107 if (walk->type != ex->type)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700108 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700109 if (walk->major != ex->major)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700110 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700111 if (walk->minor != ex->minor)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700112 continue;
113
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700114 walk->access |= ex->access;
115 kfree(excopy);
116 excopy = NULL;
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700117 }
118
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700119 if (excopy != NULL)
120 list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700121 return 0;
122}
123
124/*
Li Zefanb4046f02009-04-02 16:57:32 -0700125 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700126 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700127static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
128 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700129{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700130 struct dev_exception_item *walk, *tmp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700131
Tejun Heo4b1c7842012-11-06 09:16:53 -0800132 lockdep_assert_held(&devcgroup_mutex);
133
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700134 list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
135 if (walk->type != ex->type)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700136 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700137 if (walk->major != ex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700138 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700139 if (walk->minor != ex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700140 continue;
141
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700142 walk->access &= ~ex->access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700143 if (!walk->access) {
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700144 list_del_rcu(&walk->list);
Lai Jiangshan6034f7e2011-03-15 18:07:57 +0800145 kfree_rcu(walk, rcu);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700146 }
147 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700148}
149
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800150static void __dev_exception_clean(struct dev_cgroup *dev_cgroup)
151{
152 struct dev_exception_item *ex, *tmp;
153
154 list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
155 list_del_rcu(&ex->list);
156 kfree_rcu(ex, rcu);
157 }
158}
159
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700160/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700161 * dev_exception_clean - frees all entries of the exception list
162 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700163 *
164 * called under devcgroup_mutex
165 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700166static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700167{
Tejun Heo4b1c7842012-11-06 09:16:53 -0800168 lockdep_assert_held(&devcgroup_mutex);
169
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800170 __dev_exception_clean(dev_cgroup);
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700171}
172
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500173static inline bool is_devcg_online(const struct dev_cgroup *devcg)
174{
175 return (devcg->behavior != DEVCG_DEFAULT_NONE);
176}
177
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500178/**
179 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
180 * parent's
Tejun Heoeb954192013-08-08 20:11:23 -0400181 * @css: css getting online
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500182 * returns 0 in case of success, error code otherwise
183 */
Tejun Heoeb954192013-08-08 20:11:23 -0400184static int devcgroup_online(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500185{
Tejun Heoeb954192013-08-08 20:11:23 -0400186 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
187 struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css_parent(css));
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500188 int ret = 0;
189
190 mutex_lock(&devcgroup_mutex);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500191
192 if (parent_dev_cgroup == NULL)
193 dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
194 else {
195 ret = dev_exceptions_copy(&dev_cgroup->exceptions,
196 &parent_dev_cgroup->exceptions);
197 if (!ret)
198 dev_cgroup->behavior = parent_dev_cgroup->behavior;
199 }
200 mutex_unlock(&devcgroup_mutex);
201
202 return ret;
203}
204
Tejun Heoeb954192013-08-08 20:11:23 -0400205static void devcgroup_offline(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500206{
Tejun Heoeb954192013-08-08 20:11:23 -0400207 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500208
209 mutex_lock(&devcgroup_mutex);
210 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
211 mutex_unlock(&devcgroup_mutex);
212}
213
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700214/*
215 * called from kernel/cgroup.c with cgroup_lock() held.
216 */
Tejun Heoeb954192013-08-08 20:11:23 -0400217static struct cgroup_subsys_state *
218devcgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700219{
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500220 struct dev_cgroup *dev_cgroup;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700221
222 dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
223 if (!dev_cgroup)
224 return ERR_PTR(-ENOMEM);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700225 INIT_LIST_HEAD(&dev_cgroup->exceptions);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500226 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700227
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700228 return &dev_cgroup->css;
229}
230
Tejun Heoeb954192013-08-08 20:11:23 -0400231static void devcgroup_css_free(struct cgroup_subsys_state *css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700232{
Tejun Heoeb954192013-08-08 20:11:23 -0400233 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700234
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800235 __dev_exception_clean(dev_cgroup);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700236 kfree(dev_cgroup);
237}
238
239#define DEVCG_ALLOW 1
240#define DEVCG_DENY 2
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700241#define DEVCG_LIST 3
242
Li Zefan17d213f2008-07-13 12:14:02 -0700243#define MAJMINLEN 13
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700244#define ACCLEN 4
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700245
246static void set_access(char *acc, short access)
247{
248 int idx = 0;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700249 memset(acc, 0, ACCLEN);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700250 if (access & ACC_READ)
251 acc[idx++] = 'r';
252 if (access & ACC_WRITE)
253 acc[idx++] = 'w';
254 if (access & ACC_MKNOD)
255 acc[idx++] = 'm';
256}
257
258static char type_to_char(short type)
259{
260 if (type == DEV_ALL)
261 return 'a';
262 if (type == DEV_CHAR)
263 return 'c';
264 if (type == DEV_BLOCK)
265 return 'b';
266 return 'X';
267}
268
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700269static void set_majmin(char *str, unsigned m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700270{
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700271 if (m == ~0)
Li Zefan7759fc92008-07-25 01:47:08 -0700272 strcpy(str, "*");
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700273 else
Li Zefan7759fc92008-07-25 01:47:08 -0700274 sprintf(str, "%u", m);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700275}
276
Tejun Heo182446d2013-08-08 20:11:24 -0400277static int devcgroup_seq_read(struct cgroup_subsys_state *css,
278 struct cftype *cft, struct seq_file *m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700279{
Tejun Heo182446d2013-08-08 20:11:24 -0400280 struct dev_cgroup *devcgroup = css_to_devcgroup(css);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700281 struct dev_exception_item *ex;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700282 char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700283
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700284 rcu_read_lock();
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700285 /*
286 * To preserve the compatibility:
287 * - Only show the "all devices" when the default policy is to allow
288 * - List the exceptions in case the default policy is to deny
289 * This way, the file remains as a "whitelist of devices"
290 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700291 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700292 set_access(acc, ACC_MASK);
293 set_majmin(maj, ~0);
294 set_majmin(min, ~0);
295 seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700296 maj, min, acc);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700297 } else {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700298 list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
299 set_access(acc, ex->access);
300 set_majmin(maj, ex->major);
301 set_majmin(min, ex->minor);
302 seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700303 maj, min, acc);
304 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700305 }
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700306 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700307
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700308 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700309}
310
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700311/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700312 * may_access - verifies if a new exception is part of what is allowed
313 * by a dev cgroup based on the default policy +
314 * exceptions. This is used to make sure a child cgroup
315 * won't have more privileges than its parent or to
316 * verify if a certain access is allowed.
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700317 * @dev_cgroup: dev cgroup to be tested against
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700318 * @refex: new exception
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500319 * @behavior: behavior of the exception
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700320 */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500321static bool may_access(struct dev_cgroup *dev_cgroup,
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500322 struct dev_exception_item *refex,
323 enum devcg_behavior behavior)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700324{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700325 struct dev_exception_item *ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700326 bool match = false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700327
Tejun Heo4b1c7842012-11-06 09:16:53 -0800328 rcu_lockdep_assert(rcu_read_lock_held() ||
329 lockdep_is_held(&devcgroup_mutex),
330 "device_cgroup::may_access() called without proper synchronization");
331
Tejun Heo201e72a2012-11-06 09:17:37 -0800332 list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700333 if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700334 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700335 if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700336 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700337 if (ex->major != ~0 && ex->major != refex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700338 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700339 if (ex->minor != ~0 && ex->minor != refex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700340 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700341 if (refex->access & (~ex->access))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700342 continue;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700343 match = true;
344 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700345 }
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700346
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500347 if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
348 if (behavior == DEVCG_DEFAULT_ALLOW) {
349 /* the exception will deny access to certain devices */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500350 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500351 } else {
352 /* the exception will allow access to certain devices */
353 if (match)
354 /*
355 * a new exception allowing access shouldn't
356 * match an parent's exception
357 */
358 return false;
359 return true;
360 }
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500361 } else {
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500362 /* only behavior == DEVCG_DEFAULT_DENY allowed here */
363 if (match)
364 /* parent has an exception that matches the proposed */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500365 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500366 else
367 return false;
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500368 }
369 return false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700370}
371
372/*
373 * parent_has_perm:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700374 * when adding a new allow rule to a device exception list, the rule
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700375 * must be allowed in the parent device
376 */
Paul Menagef92523e2008-07-25 01:47:03 -0700377static int parent_has_perm(struct dev_cgroup *childcg,
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700378 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700379{
Tejun Heo63876982013-08-08 20:11:23 -0400380 struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700381
Tejun Heo63876982013-08-08 20:11:23 -0400382 if (!parent)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700383 return 1;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500384 return may_access(parent, ex, childcg->behavior);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700385}
386
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700387/**
388 * may_allow_all - checks if it's possible to change the behavior to
389 * allow based on parent's rules.
390 * @parent: device cgroup's parent
391 * returns: != 0 in case it's allowed, 0 otherwise
392 */
393static inline int may_allow_all(struct dev_cgroup *parent)
394{
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800395 if (!parent)
396 return 1;
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700397 return parent->behavior == DEVCG_DEFAULT_ALLOW;
398}
399
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500400/**
401 * revalidate_active_exceptions - walks through the active exception list and
402 * revalidates the exceptions based on parent's
403 * behavior and exceptions. The exceptions that
404 * are no longer valid will be removed.
405 * Called with devcgroup_mutex held.
406 * @devcg: cgroup which exceptions will be checked
407 *
408 * This is one of the three key functions for hierarchy implementation.
409 * This function is responsible for re-evaluating all the cgroup's active
410 * exceptions due to a parent's exception change.
411 * Refer to Documentation/cgroups/devices.txt for more details.
412 */
413static void revalidate_active_exceptions(struct dev_cgroup *devcg)
414{
415 struct dev_exception_item *ex;
416 struct list_head *this, *tmp;
417
418 list_for_each_safe(this, tmp, &devcg->exceptions) {
419 ex = container_of(this, struct dev_exception_item, list);
420 if (!parent_has_perm(devcg, ex))
421 dev_exception_rm(devcg, ex);
422 }
423}
424
425/**
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500426 * propagate_exception - propagates a new exception to the children
427 * @devcg_root: device cgroup that added a new exception
428 * @ex: new exception to be propagated
429 *
430 * returns: 0 in case of success, != 0 in case of error
431 */
432static int propagate_exception(struct dev_cgroup *devcg_root,
433 struct dev_exception_item *ex)
434{
Tejun Heo492eb212013-08-08 20:11:25 -0400435 struct cgroup_subsys_state *pos;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500436 int rc = 0;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500437
Tejun Heod591fb52013-05-24 10:55:38 +0900438 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500439
Tejun Heo492eb212013-08-08 20:11:25 -0400440 css_for_each_descendant_pre(pos, &devcg_root->css) {
441 struct dev_cgroup *devcg = css_to_devcgroup(pos);
Tejun Heod591fb52013-05-24 10:55:38 +0900442
443 /*
444 * Because devcgroup_mutex is held, no devcg will become
445 * online or offline during the tree walk (see on/offline
446 * methods), and online ones are safe to access outside RCU
447 * read lock without bumping refcnt.
448 */
Tejun Heobd8815a2013-08-08 20:11:27 -0400449 if (pos == &devcg_root->css || !is_devcg_online(devcg))
Tejun Heod591fb52013-05-24 10:55:38 +0900450 continue;
451
452 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500453
454 /*
455 * in case both root's behavior and devcg is allow, a new
456 * restriction means adding to the exception list
457 */
458 if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
459 devcg->behavior == DEVCG_DEFAULT_ALLOW) {
460 rc = dev_exception_add(devcg, ex);
461 if (rc)
462 break;
463 } else {
464 /*
465 * in the other possible cases:
466 * root's behavior: allow, devcg's: deny
467 * root's behavior: deny, devcg's: deny
468 * the exception will be removed
469 */
470 dev_exception_rm(devcg, ex);
471 }
472 revalidate_active_exceptions(devcg);
473
Tejun Heod591fb52013-05-24 10:55:38 +0900474 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500475 }
Tejun Heod591fb52013-05-24 10:55:38 +0900476
477 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500478 return rc;
479}
480
481static inline bool has_children(struct dev_cgroup *devcgroup)
482{
483 struct cgroup *cgrp = devcgroup->css.cgroup;
484
485 return !list_empty(&cgrp->children);
486}
487
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700488/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700489 * Modify the exception list using allow/deny rules.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700490 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
491 * so we can give a container CAP_MKNOD to let it create devices but not
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700492 * modify the exception list.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700493 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
494 * us to also grant CAP_SYS_ADMIN to containers without giving away the
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700495 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700496 *
497 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
498 * new access is only allowed if you're in the top-level cgroup, or your
499 * parent cgroup has the access you're asking for.
500 */
Paul Menagef92523e2008-07-25 01:47:03 -0700501static int devcgroup_update_access(struct dev_cgroup *devcgroup,
502 int filetype, const char *buffer)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700503{
Paul Menagef92523e2008-07-25 01:47:03 -0700504 const char *b;
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700505 char temp[12]; /* 11 + 1 characters needed for a u32 */
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500506 int count, rc = 0;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700507 struct dev_exception_item ex;
Tejun Heo63876982013-08-08 20:11:23 -0400508 struct dev_cgroup *parent = css_to_devcgroup(css_parent(&devcgroup->css));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700509
510 if (!capable(CAP_SYS_ADMIN))
511 return -EPERM;
512
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700513 memset(&ex, 0, sizeof(ex));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700514 b = buffer;
515
516 switch (*b) {
517 case 'a':
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700518 switch (filetype) {
519 case DEVCG_ALLOW:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500520 if (has_children(devcgroup))
521 return -EINVAL;
522
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700523 if (!may_allow_all(parent))
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700524 return -EPERM;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700525 dev_exception_clean(devcgroup);
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800526 devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
527 if (!parent)
528 break;
529
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700530 rc = dev_exceptions_copy(&devcgroup->exceptions,
531 &parent->exceptions);
532 if (rc)
533 return rc;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700534 break;
535 case DEVCG_DENY:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500536 if (has_children(devcgroup))
537 return -EINVAL;
538
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700539 dev_exception_clean(devcgroup);
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700540 devcgroup->behavior = DEVCG_DEFAULT_DENY;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700541 break;
542 default:
543 return -EINVAL;
544 }
545 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700546 case 'b':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700547 ex.type = DEV_BLOCK;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700548 break;
549 case 'c':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700550 ex.type = DEV_CHAR;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700551 break;
552 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700553 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700554 }
555 b++;
Paul Menagef92523e2008-07-25 01:47:03 -0700556 if (!isspace(*b))
557 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700558 b++;
559 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700560 ex.major = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700561 b++;
562 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700563 memset(temp, 0, sizeof(temp));
564 for (count = 0; count < sizeof(temp) - 1; count++) {
565 temp[count] = *b;
566 b++;
567 if (!isdigit(*b))
568 break;
569 }
570 rc = kstrtou32(temp, 10, &ex.major);
571 if (rc)
572 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700573 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700574 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700575 }
Paul Menagef92523e2008-07-25 01:47:03 -0700576 if (*b != ':')
577 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700578 b++;
579
580 /* read minor */
581 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700582 ex.minor = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700583 b++;
584 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700585 memset(temp, 0, sizeof(temp));
586 for (count = 0; count < sizeof(temp) - 1; count++) {
587 temp[count] = *b;
588 b++;
589 if (!isdigit(*b))
590 break;
591 }
592 rc = kstrtou32(temp, 10, &ex.minor);
593 if (rc)
594 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700595 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700596 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700597 }
Paul Menagef92523e2008-07-25 01:47:03 -0700598 if (!isspace(*b))
599 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700600 for (b++, count = 0; count < 3; count++, b++) {
601 switch (*b) {
602 case 'r':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700603 ex.access |= ACC_READ;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700604 break;
605 case 'w':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700606 ex.access |= ACC_WRITE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700607 break;
608 case 'm':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700609 ex.access |= ACC_MKNOD;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700610 break;
611 case '\n':
612 case '\0':
613 count = 3;
614 break;
615 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700616 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700617 }
618 }
619
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700620 switch (filetype) {
621 case DEVCG_ALLOW:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700622 if (!parent_has_perm(devcgroup, &ex))
Paul Menagef92523e2008-07-25 01:47:03 -0700623 return -EPERM;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700624 /*
625 * If the default policy is to allow by default, try to remove
626 * an matching exception instead. And be silent about it: we
627 * don't want to break compatibility
628 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700629 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700630 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700631 return 0;
632 }
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500633 rc = dev_exception_add(devcgroup, &ex);
634 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700635 case DEVCG_DENY:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700636 /*
637 * If the default policy is to deny by default, try to remove
638 * an matching exception instead. And be silent about it: we
639 * don't want to break compatibility
640 */
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500641 if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700642 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500643 else
644 rc = dev_exception_add(devcgroup, &ex);
645
646 if (rc)
647 break;
648 /* we only propagate new restrictions */
649 rc = propagate_exception(devcgroup, &ex);
650 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700651 default:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500652 rc = -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700653 }
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500654 return rc;
Paul Menagef92523e2008-07-25 01:47:03 -0700655}
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700656
Tejun Heo182446d2013-08-08 20:11:24 -0400657static int devcgroup_access_write(struct cgroup_subsys_state *css,
658 struct cftype *cft, const char *buffer)
Paul Menagef92523e2008-07-25 01:47:03 -0700659{
660 int retval;
Li Zefanb4046f02009-04-02 16:57:32 -0700661
662 mutex_lock(&devcgroup_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -0400663 retval = devcgroup_update_access(css_to_devcgroup(css),
Paul Menagef92523e2008-07-25 01:47:03 -0700664 cft->private, buffer);
Li Zefanb4046f02009-04-02 16:57:32 -0700665 mutex_unlock(&devcgroup_mutex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700666 return retval;
667}
668
669static struct cftype dev_cgroup_files[] = {
670 {
671 .name = "allow",
Paul Menagef92523e2008-07-25 01:47:03 -0700672 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700673 .private = DEVCG_ALLOW,
674 },
675 {
676 .name = "deny",
Paul Menagef92523e2008-07-25 01:47:03 -0700677 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700678 .private = DEVCG_DENY,
679 },
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700680 {
681 .name = "list",
682 .read_seq_string = devcgroup_seq_read,
683 .private = DEVCG_LIST,
684 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700685 { } /* terminate */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700686};
687
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700688struct cgroup_subsys devices_subsys = {
689 .name = "devices",
Tejun Heo92fb9742012-11-19 08:13:38 -0800690 .css_alloc = devcgroup_css_alloc,
691 .css_free = devcgroup_css_free,
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500692 .css_online = devcgroup_online,
693 .css_offline = devcgroup_offline,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700694 .subsys_id = devices_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700695 .base_cftypes = dev_cgroup_files,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700696};
697
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700698/**
699 * __devcgroup_check_permission - checks if an inode operation is permitted
700 * @dev_cgroup: the dev cgroup to be tested against
701 * @type: device type
702 * @major: device major number
703 * @minor: device minor number
704 * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
705 *
706 * returns 0 on success, -EPERM case the operation is not permitted
707 */
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700708static int __devcgroup_check_permission(short type, u32 major, u32 minor,
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700709 short access)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700710{
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700711 struct dev_cgroup *dev_cgroup;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700712 struct dev_exception_item ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700713 int rc;
714
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700715 memset(&ex, 0, sizeof(ex));
716 ex.type = type;
717 ex.major = major;
718 ex.minor = minor;
719 ex.access = access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700720
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700721 rcu_read_lock();
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700722 dev_cgroup = task_devcgroup(current);
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500723 rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700724 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700725
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700726 if (!rc)
727 return -EPERM;
728
729 return 0;
730}
731
732int __devcgroup_inode_permission(struct inode *inode, int mask)
733{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700734 short type, access = 0;
735
736 if (S_ISBLK(inode->i_mode))
737 type = DEV_BLOCK;
738 if (S_ISCHR(inode->i_mode))
739 type = DEV_CHAR;
740 if (mask & MAY_WRITE)
741 access |= ACC_WRITE;
742 if (mask & MAY_READ)
743 access |= ACC_READ;
744
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700745 return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
746 access);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700747}
748
749int devcgroup_inode_mknod(int mode, dev_t dev)
750{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700751 short type;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700752
Serge E. Hallyn0b82ac32009-01-07 18:07:46 -0800753 if (!S_ISBLK(mode) && !S_ISCHR(mode))
754 return 0;
755
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700756 if (S_ISBLK(mode))
757 type = DEV_BLOCK;
758 else
759 type = DEV_CHAR;
Li Zefan36fd71d2008-09-02 14:35:52 -0700760
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700761 return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
762 ACC_MKNOD);
Li Zefan36fd71d2008-09-02 14:35:52 -0700763
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700764}