blob: c123628d3f848ee25497858a96c861a835f723d2 [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
Tejun Heoeb954192013-08-08 20:11:23 -040066static int devcgroup_can_attach(struct cgroup_subsys_state *new_css,
Li Zefan761b3ef52012-01-31 13:47:36 +080067 struct cgroup_taskset *set)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070068{
Tejun Heo2f7ee562011-12-12 18:12:21 -080069 struct task_struct *task = cgroup_taskset_first(set);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070070
Tejun Heo2f7ee562011-12-12 18:12:21 -080071 if (current != task && !capable(CAP_SYS_ADMIN))
72 return -EPERM;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070073 return 0;
74}
75
76/*
Li Zefanb4046f02009-04-02 16:57:32 -070077 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070078 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070079static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070080{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070081 struct dev_exception_item *ex, *tmp, *new;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070082
Tejun Heo4b1c7842012-11-06 09:16:53 -080083 lockdep_assert_held(&devcgroup_mutex);
84
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070085 list_for_each_entry(ex, orig, list) {
86 new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070087 if (!new)
88 goto free_and_exit;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070089 list_add_tail(&new->list, dest);
90 }
91
92 return 0;
93
94free_and_exit:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070095 list_for_each_entry_safe(ex, tmp, dest, list) {
96 list_del(&ex->list);
97 kfree(ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070098 }
99 return -ENOMEM;
100}
101
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700102/*
Li Zefanb4046f02009-04-02 16:57:32 -0700103 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700104 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700105static int dev_exception_add(struct dev_cgroup *dev_cgroup,
106 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700107{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700108 struct dev_exception_item *excopy, *walk;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700109
Tejun Heo4b1c7842012-11-06 09:16:53 -0800110 lockdep_assert_held(&devcgroup_mutex);
111
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700112 excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
113 if (!excopy)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700114 return -ENOMEM;
115
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700116 list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
117 if (walk->type != ex->type)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700118 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700119 if (walk->major != ex->major)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700120 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700121 if (walk->minor != ex->minor)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700122 continue;
123
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700124 walk->access |= ex->access;
125 kfree(excopy);
126 excopy = NULL;
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700127 }
128
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700129 if (excopy != NULL)
130 list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700131 return 0;
132}
133
134/*
Li Zefanb4046f02009-04-02 16:57:32 -0700135 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700136 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700137static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
138 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700139{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700140 struct dev_exception_item *walk, *tmp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700141
Tejun Heo4b1c7842012-11-06 09:16:53 -0800142 lockdep_assert_held(&devcgroup_mutex);
143
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700144 list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
145 if (walk->type != ex->type)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700146 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700147 if (walk->major != ex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700148 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700149 if (walk->minor != ex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700150 continue;
151
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700152 walk->access &= ~ex->access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700153 if (!walk->access) {
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700154 list_del_rcu(&walk->list);
Lai Jiangshan6034f7e2011-03-15 18:07:57 +0800155 kfree_rcu(walk, rcu);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700156 }
157 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700158}
159
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800160static void __dev_exception_clean(struct dev_cgroup *dev_cgroup)
161{
162 struct dev_exception_item *ex, *tmp;
163
164 list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
165 list_del_rcu(&ex->list);
166 kfree_rcu(ex, rcu);
167 }
168}
169
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700170/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700171 * dev_exception_clean - frees all entries of the exception list
172 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700173 *
174 * called under devcgroup_mutex
175 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700176static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700177{
Tejun Heo4b1c7842012-11-06 09:16:53 -0800178 lockdep_assert_held(&devcgroup_mutex);
179
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800180 __dev_exception_clean(dev_cgroup);
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700181}
182
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500183static inline bool is_devcg_online(const struct dev_cgroup *devcg)
184{
185 return (devcg->behavior != DEVCG_DEFAULT_NONE);
186}
187
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500188/**
189 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
190 * parent's
Tejun Heoeb954192013-08-08 20:11:23 -0400191 * @css: css getting online
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500192 * returns 0 in case of success, error code otherwise
193 */
Tejun Heoeb954192013-08-08 20:11:23 -0400194static int devcgroup_online(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500195{
Tejun Heoeb954192013-08-08 20:11:23 -0400196 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
197 struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css_parent(css));
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500198 int ret = 0;
199
200 mutex_lock(&devcgroup_mutex);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500201
202 if (parent_dev_cgroup == NULL)
203 dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
204 else {
205 ret = dev_exceptions_copy(&dev_cgroup->exceptions,
206 &parent_dev_cgroup->exceptions);
207 if (!ret)
208 dev_cgroup->behavior = parent_dev_cgroup->behavior;
209 }
210 mutex_unlock(&devcgroup_mutex);
211
212 return ret;
213}
214
Tejun Heoeb954192013-08-08 20:11:23 -0400215static void devcgroup_offline(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500216{
Tejun Heoeb954192013-08-08 20:11:23 -0400217 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500218
219 mutex_lock(&devcgroup_mutex);
220 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
221 mutex_unlock(&devcgroup_mutex);
222}
223
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700224/*
225 * called from kernel/cgroup.c with cgroup_lock() held.
226 */
Tejun Heoeb954192013-08-08 20:11:23 -0400227static struct cgroup_subsys_state *
228devcgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700229{
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500230 struct dev_cgroup *dev_cgroup;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700231
232 dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
233 if (!dev_cgroup)
234 return ERR_PTR(-ENOMEM);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700235 INIT_LIST_HEAD(&dev_cgroup->exceptions);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500236 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700237
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700238 return &dev_cgroup->css;
239}
240
Tejun Heoeb954192013-08-08 20:11:23 -0400241static void devcgroup_css_free(struct cgroup_subsys_state *css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700242{
Tejun Heoeb954192013-08-08 20:11:23 -0400243 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700244
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800245 __dev_exception_clean(dev_cgroup);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700246 kfree(dev_cgroup);
247}
248
249#define DEVCG_ALLOW 1
250#define DEVCG_DENY 2
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700251#define DEVCG_LIST 3
252
Li Zefan17d213f2008-07-13 12:14:02 -0700253#define MAJMINLEN 13
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700254#define ACCLEN 4
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700255
256static void set_access(char *acc, short access)
257{
258 int idx = 0;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700259 memset(acc, 0, ACCLEN);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700260 if (access & ACC_READ)
261 acc[idx++] = 'r';
262 if (access & ACC_WRITE)
263 acc[idx++] = 'w';
264 if (access & ACC_MKNOD)
265 acc[idx++] = 'm';
266}
267
268static char type_to_char(short type)
269{
270 if (type == DEV_ALL)
271 return 'a';
272 if (type == DEV_CHAR)
273 return 'c';
274 if (type == DEV_BLOCK)
275 return 'b';
276 return 'X';
277}
278
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700279static void set_majmin(char *str, unsigned m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700280{
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700281 if (m == ~0)
Li Zefan7759fc92008-07-25 01:47:08 -0700282 strcpy(str, "*");
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700283 else
Li Zefan7759fc92008-07-25 01:47:08 -0700284 sprintf(str, "%u", m);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700285}
286
Tejun Heo182446d2013-08-08 20:11:24 -0400287static int devcgroup_seq_read(struct cgroup_subsys_state *css,
288 struct cftype *cft, struct seq_file *m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700289{
Tejun Heo182446d2013-08-08 20:11:24 -0400290 struct dev_cgroup *devcgroup = css_to_devcgroup(css);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700291 struct dev_exception_item *ex;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700292 char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700293
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700294 rcu_read_lock();
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700295 /*
296 * To preserve the compatibility:
297 * - Only show the "all devices" when the default policy is to allow
298 * - List the exceptions in case the default policy is to deny
299 * This way, the file remains as a "whitelist of devices"
300 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700301 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700302 set_access(acc, ACC_MASK);
303 set_majmin(maj, ~0);
304 set_majmin(min, ~0);
305 seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700306 maj, min, acc);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700307 } else {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700308 list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
309 set_access(acc, ex->access);
310 set_majmin(maj, ex->major);
311 set_majmin(min, ex->minor);
312 seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700313 maj, min, acc);
314 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700315 }
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700316 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700317
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700318 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700319}
320
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700321/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700322 * may_access - verifies if a new exception is part of what is allowed
323 * by a dev cgroup based on the default policy +
324 * exceptions. This is used to make sure a child cgroup
325 * won't have more privileges than its parent or to
326 * verify if a certain access is allowed.
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700327 * @dev_cgroup: dev cgroup to be tested against
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700328 * @refex: new exception
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500329 * @behavior: behavior of the exception
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700330 */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500331static bool may_access(struct dev_cgroup *dev_cgroup,
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500332 struct dev_exception_item *refex,
333 enum devcg_behavior behavior)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700334{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700335 struct dev_exception_item *ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700336 bool match = false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700337
Tejun Heo4b1c7842012-11-06 09:16:53 -0800338 rcu_lockdep_assert(rcu_read_lock_held() ||
339 lockdep_is_held(&devcgroup_mutex),
340 "device_cgroup::may_access() called without proper synchronization");
341
Tejun Heo201e72a2012-11-06 09:17:37 -0800342 list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700343 if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700344 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700345 if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700346 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700347 if (ex->major != ~0 && ex->major != refex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700348 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700349 if (ex->minor != ~0 && ex->minor != refex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700350 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700351 if (refex->access & (~ex->access))
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700352 continue;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700353 match = true;
354 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700355 }
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700356
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500357 if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
358 if (behavior == DEVCG_DEFAULT_ALLOW) {
359 /* the exception will deny access to certain devices */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500360 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500361 } else {
362 /* the exception will allow access to certain devices */
363 if (match)
364 /*
365 * a new exception allowing access shouldn't
366 * match an parent's exception
367 */
368 return false;
369 return true;
370 }
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500371 } else {
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500372 /* only behavior == DEVCG_DEFAULT_DENY allowed here */
373 if (match)
374 /* parent has an exception that matches the proposed */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500375 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500376 else
377 return false;
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500378 }
379 return false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700380}
381
382/*
383 * parent_has_perm:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700384 * when adding a new allow rule to a device exception list, the rule
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700385 * must be allowed in the parent device
386 */
Paul Menagef92523e2008-07-25 01:47:03 -0700387static int parent_has_perm(struct dev_cgroup *childcg,
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700388 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700389{
Tejun Heo63876982013-08-08 20:11:23 -0400390 struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700391
Tejun Heo63876982013-08-08 20:11:23 -0400392 if (!parent)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700393 return 1;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500394 return may_access(parent, ex, childcg->behavior);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700395}
396
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700397/**
398 * may_allow_all - checks if it's possible to change the behavior to
399 * allow based on parent's rules.
400 * @parent: device cgroup's parent
401 * returns: != 0 in case it's allowed, 0 otherwise
402 */
403static inline int may_allow_all(struct dev_cgroup *parent)
404{
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800405 if (!parent)
406 return 1;
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700407 return parent->behavior == DEVCG_DEFAULT_ALLOW;
408}
409
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500410/**
411 * revalidate_active_exceptions - walks through the active exception list and
412 * revalidates the exceptions based on parent's
413 * behavior and exceptions. The exceptions that
414 * are no longer valid will be removed.
415 * Called with devcgroup_mutex held.
416 * @devcg: cgroup which exceptions will be checked
417 *
418 * This is one of the three key functions for hierarchy implementation.
419 * This function is responsible for re-evaluating all the cgroup's active
420 * exceptions due to a parent's exception change.
421 * Refer to Documentation/cgroups/devices.txt for more details.
422 */
423static void revalidate_active_exceptions(struct dev_cgroup *devcg)
424{
425 struct dev_exception_item *ex;
426 struct list_head *this, *tmp;
427
428 list_for_each_safe(this, tmp, &devcg->exceptions) {
429 ex = container_of(this, struct dev_exception_item, list);
430 if (!parent_has_perm(devcg, ex))
431 dev_exception_rm(devcg, ex);
432 }
433}
434
435/**
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500436 * propagate_exception - propagates a new exception to the children
437 * @devcg_root: device cgroup that added a new exception
438 * @ex: new exception to be propagated
439 *
440 * returns: 0 in case of success, != 0 in case of error
441 */
442static int propagate_exception(struct dev_cgroup *devcg_root,
443 struct dev_exception_item *ex)
444{
Tejun Heo492eb212013-08-08 20:11:25 -0400445 struct cgroup_subsys_state *pos;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500446 int rc = 0;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500447
Tejun Heod591fb52013-05-24 10:55:38 +0900448 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500449
Tejun Heo492eb212013-08-08 20:11:25 -0400450 css_for_each_descendant_pre(pos, &devcg_root->css) {
451 struct dev_cgroup *devcg = css_to_devcgroup(pos);
Tejun Heod591fb52013-05-24 10:55:38 +0900452
453 /*
454 * Because devcgroup_mutex is held, no devcg will become
455 * online or offline during the tree walk (see on/offline
456 * methods), and online ones are safe to access outside RCU
457 * read lock without bumping refcnt.
458 */
Tejun Heobd8815a2013-08-08 20:11:27 -0400459 if (pos == &devcg_root->css || !is_devcg_online(devcg))
Tejun Heod591fb52013-05-24 10:55:38 +0900460 continue;
461
462 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500463
464 /*
465 * in case both root's behavior and devcg is allow, a new
466 * restriction means adding to the exception list
467 */
468 if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
469 devcg->behavior == DEVCG_DEFAULT_ALLOW) {
470 rc = dev_exception_add(devcg, ex);
471 if (rc)
472 break;
473 } else {
474 /*
475 * in the other possible cases:
476 * root's behavior: allow, devcg's: deny
477 * root's behavior: deny, devcg's: deny
478 * the exception will be removed
479 */
480 dev_exception_rm(devcg, ex);
481 }
482 revalidate_active_exceptions(devcg);
483
Tejun Heod591fb52013-05-24 10:55:38 +0900484 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500485 }
Tejun Heod591fb52013-05-24 10:55:38 +0900486
487 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500488 return rc;
489}
490
491static inline bool has_children(struct dev_cgroup *devcgroup)
492{
493 struct cgroup *cgrp = devcgroup->css.cgroup;
494
495 return !list_empty(&cgrp->children);
496}
497
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700498/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700499 * Modify the exception list using allow/deny rules.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700500 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
501 * so we can give a container CAP_MKNOD to let it create devices but not
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700502 * modify the exception list.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700503 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
504 * us to also grant CAP_SYS_ADMIN to containers without giving away the
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700505 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700506 *
507 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
508 * new access is only allowed if you're in the top-level cgroup, or your
509 * parent cgroup has the access you're asking for.
510 */
Paul Menagef92523e2008-07-25 01:47:03 -0700511static int devcgroup_update_access(struct dev_cgroup *devcgroup,
512 int filetype, const char *buffer)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700513{
Paul Menagef92523e2008-07-25 01:47:03 -0700514 const char *b;
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700515 char temp[12]; /* 11 + 1 characters needed for a u32 */
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500516 int count, rc = 0;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700517 struct dev_exception_item ex;
Tejun Heo63876982013-08-08 20:11:23 -0400518 struct dev_cgroup *parent = css_to_devcgroup(css_parent(&devcgroup->css));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700519
520 if (!capable(CAP_SYS_ADMIN))
521 return -EPERM;
522
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700523 memset(&ex, 0, sizeof(ex));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700524 b = buffer;
525
526 switch (*b) {
527 case 'a':
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700528 switch (filetype) {
529 case DEVCG_ALLOW:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500530 if (has_children(devcgroup))
531 return -EINVAL;
532
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700533 if (!may_allow_all(parent))
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700534 return -EPERM;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700535 dev_exception_clean(devcgroup);
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800536 devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
537 if (!parent)
538 break;
539
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700540 rc = dev_exceptions_copy(&devcgroup->exceptions,
541 &parent->exceptions);
542 if (rc)
543 return rc;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700544 break;
545 case DEVCG_DENY:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500546 if (has_children(devcgroup))
547 return -EINVAL;
548
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700549 dev_exception_clean(devcgroup);
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700550 devcgroup->behavior = DEVCG_DEFAULT_DENY;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700551 break;
552 default:
553 return -EINVAL;
554 }
555 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700556 case 'b':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700557 ex.type = DEV_BLOCK;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700558 break;
559 case 'c':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700560 ex.type = DEV_CHAR;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700561 break;
562 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700563 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700564 }
565 b++;
Paul Menagef92523e2008-07-25 01:47:03 -0700566 if (!isspace(*b))
567 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700568 b++;
569 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700570 ex.major = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700571 b++;
572 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700573 memset(temp, 0, sizeof(temp));
574 for (count = 0; count < sizeof(temp) - 1; count++) {
575 temp[count] = *b;
576 b++;
577 if (!isdigit(*b))
578 break;
579 }
580 rc = kstrtou32(temp, 10, &ex.major);
581 if (rc)
582 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700583 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700584 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700585 }
Paul Menagef92523e2008-07-25 01:47:03 -0700586 if (*b != ':')
587 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700588 b++;
589
590 /* read minor */
591 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700592 ex.minor = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700593 b++;
594 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700595 memset(temp, 0, sizeof(temp));
596 for (count = 0; count < sizeof(temp) - 1; count++) {
597 temp[count] = *b;
598 b++;
599 if (!isdigit(*b))
600 break;
601 }
602 rc = kstrtou32(temp, 10, &ex.minor);
603 if (rc)
604 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700605 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700606 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700607 }
Paul Menagef92523e2008-07-25 01:47:03 -0700608 if (!isspace(*b))
609 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700610 for (b++, count = 0; count < 3; count++, b++) {
611 switch (*b) {
612 case 'r':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700613 ex.access |= ACC_READ;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700614 break;
615 case 'w':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700616 ex.access |= ACC_WRITE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700617 break;
618 case 'm':
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700619 ex.access |= ACC_MKNOD;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700620 break;
621 case '\n':
622 case '\0':
623 count = 3;
624 break;
625 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700626 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700627 }
628 }
629
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700630 switch (filetype) {
631 case DEVCG_ALLOW:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700632 if (!parent_has_perm(devcgroup, &ex))
Paul Menagef92523e2008-07-25 01:47:03 -0700633 return -EPERM;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700634 /*
635 * If the default policy is to allow by default, try to remove
636 * an matching exception instead. And be silent about it: we
637 * don't want to break compatibility
638 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700639 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700640 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700641 return 0;
642 }
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500643 rc = dev_exception_add(devcgroup, &ex);
644 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700645 case DEVCG_DENY:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700646 /*
647 * If the default policy is to deny by default, try to remove
648 * an matching exception instead. And be silent about it: we
649 * don't want to break compatibility
650 */
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500651 if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700652 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500653 else
654 rc = dev_exception_add(devcgroup, &ex);
655
656 if (rc)
657 break;
658 /* we only propagate new restrictions */
659 rc = propagate_exception(devcgroup, &ex);
660 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700661 default:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500662 rc = -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700663 }
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500664 return rc;
Paul Menagef92523e2008-07-25 01:47:03 -0700665}
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700666
Tejun Heo182446d2013-08-08 20:11:24 -0400667static int devcgroup_access_write(struct cgroup_subsys_state *css,
668 struct cftype *cft, const char *buffer)
Paul Menagef92523e2008-07-25 01:47:03 -0700669{
670 int retval;
Li Zefanb4046f02009-04-02 16:57:32 -0700671
672 mutex_lock(&devcgroup_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -0400673 retval = devcgroup_update_access(css_to_devcgroup(css),
Paul Menagef92523e2008-07-25 01:47:03 -0700674 cft->private, buffer);
Li Zefanb4046f02009-04-02 16:57:32 -0700675 mutex_unlock(&devcgroup_mutex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700676 return retval;
677}
678
679static struct cftype dev_cgroup_files[] = {
680 {
681 .name = "allow",
Paul Menagef92523e2008-07-25 01:47:03 -0700682 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700683 .private = DEVCG_ALLOW,
684 },
685 {
686 .name = "deny",
Paul Menagef92523e2008-07-25 01:47:03 -0700687 .write_string = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700688 .private = DEVCG_DENY,
689 },
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700690 {
691 .name = "list",
692 .read_seq_string = devcgroup_seq_read,
693 .private = DEVCG_LIST,
694 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700695 { } /* terminate */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700696};
697
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700698struct cgroup_subsys devices_subsys = {
699 .name = "devices",
700 .can_attach = devcgroup_can_attach,
Tejun Heo92fb9742012-11-19 08:13:38 -0800701 .css_alloc = devcgroup_css_alloc,
702 .css_free = devcgroup_css_free,
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500703 .css_online = devcgroup_online,
704 .css_offline = devcgroup_offline,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700705 .subsys_id = devices_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -0700706 .base_cftypes = dev_cgroup_files,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700707};
708
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700709/**
710 * __devcgroup_check_permission - checks if an inode operation is permitted
711 * @dev_cgroup: the dev cgroup to be tested against
712 * @type: device type
713 * @major: device major number
714 * @minor: device minor number
715 * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
716 *
717 * returns 0 on success, -EPERM case the operation is not permitted
718 */
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700719static int __devcgroup_check_permission(short type, u32 major, u32 minor,
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700720 short access)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700721{
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700722 struct dev_cgroup *dev_cgroup;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700723 struct dev_exception_item ex;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700724 int rc;
725
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700726 memset(&ex, 0, sizeof(ex));
727 ex.type = type;
728 ex.major = major;
729 ex.minor = minor;
730 ex.access = access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700731
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700732 rcu_read_lock();
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700733 dev_cgroup = task_devcgroup(current);
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500734 rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700735 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700736
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700737 if (!rc)
738 return -EPERM;
739
740 return 0;
741}
742
743int __devcgroup_inode_permission(struct inode *inode, int mask)
744{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700745 short type, access = 0;
746
747 if (S_ISBLK(inode->i_mode))
748 type = DEV_BLOCK;
749 if (S_ISCHR(inode->i_mode))
750 type = DEV_CHAR;
751 if (mask & MAY_WRITE)
752 access |= ACC_WRITE;
753 if (mask & MAY_READ)
754 access |= ACC_READ;
755
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700756 return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
757 access);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700758}
759
760int devcgroup_inode_mknod(int mode, dev_t dev)
761{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700762 short type;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700763
Serge E. Hallyn0b82ac32009-01-07 18:07:46 -0800764 if (!S_ISBLK(mode) && !S_ISCHR(mode))
765 return 0;
766
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700767 if (S_ISBLK(mode))
768 type = DEV_BLOCK;
769 else
770 type = DEV_CHAR;
Li Zefan36fd71d2008-09-02 14:35:52 -0700771
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700772 return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
773 ACC_MKNOD);
Li Zefan36fd71d2008-09-02 14:35:52 -0700774
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700775}