blob: 968c21557ba7db0870d2a90721af6bda430ac851 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002/*
Lai Jiangshan47c59802008-10-18 20:28:07 -07003 * device_cgroup.c - device cgroup subsystem
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07004 *
5 * Copyright 2007 IBM Corp
6 */
7
8#include <linux/device_cgroup.h>
9#include <linux/cgroup.h>
10#include <linux/ctype.h>
11#include <linux/list.h>
12#include <linux/uaccess.h>
Serge E. Hallyn29486df2008-04-29 01:00:14 -070013#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Lai Jiangshan47c59802008-10-18 20:28:07 -070015#include <linux/rcupdate.h>
Li Zefanb4046f02009-04-02 16:57:32 -070016#include <linux/mutex.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070017
Roman Gushchin67e306f2017-11-05 08:15:30 -050018#define DEVCG_ACC_MKNOD 1
19#define DEVCG_ACC_READ 2
20#define DEVCG_ACC_WRITE 4
21#define DEVCG_ACC_MASK (DEVCG_ACC_MKNOD | DEVCG_ACC_READ | DEVCG_ACC_WRITE)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070022
Roman Gushchin67e306f2017-11-05 08:15:30 -050023#define DEVCG_DEV_BLOCK 1
24#define DEVCG_DEV_CHAR 2
25#define DEVCG_DEV_ALL 4 /* this represents all devices */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070026
Li Zefanb4046f02009-04-02 16:57:32 -070027static DEFINE_MUTEX(devcgroup_mutex);
28
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -050029enum devcg_behavior {
30 DEVCG_DEFAULT_NONE,
31 DEVCG_DEFAULT_ALLOW,
32 DEVCG_DEFAULT_DENY,
33};
34
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070035/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070036 * exception list locking rules:
Li Zefanb4046f02009-04-02 16:57:32 -070037 * hold devcgroup_mutex for update/read.
Lai Jiangshan47c59802008-10-18 20:28:07 -070038 * hold rcu_read_lock() for read.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070039 */
40
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070041struct dev_exception_item {
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070042 u32 major, minor;
43 short type;
44 short access;
45 struct list_head list;
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -070046 struct rcu_head rcu;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070047};
48
49struct dev_cgroup {
50 struct cgroup_subsys_state css;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070051 struct list_head exceptions;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -050052 enum devcg_behavior behavior;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070053};
54
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070055static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
56{
Tejun Heoa7c6d552013-08-08 20:11:23 -040057 return s ? container_of(s, struct dev_cgroup, css) : NULL;
Pavel Emelyanovb66862f2008-06-05 22:46:24 -070058}
59
Paul Menagef92523e2008-07-25 01:47:03 -070060static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
61{
Tejun Heo073219e2014-02-08 10:36:58 -050062 return css_to_devcgroup(task_css(task, devices_cgrp_id));
Paul Menagef92523e2008-07-25 01:47:03 -070063}
64
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070065/*
Li Zefanb4046f02009-04-02 16:57:32 -070066 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070067 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070068static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070069{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070070 struct dev_exception_item *ex, *tmp, *new;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070071
Tejun Heo4b1c7842012-11-06 09:16:53 -080072 lockdep_assert_held(&devcgroup_mutex);
73
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070074 list_for_each_entry(ex, orig, list) {
75 new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070076 if (!new)
77 goto free_and_exit;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070078 list_add_tail(&new->list, dest);
79 }
80
81 return 0;
82
83free_and_exit:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070084 list_for_each_entry_safe(ex, tmp, dest, list) {
85 list_del(&ex->list);
86 kfree(ex);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070087 }
88 return -ENOMEM;
89}
90
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070091/*
Li Zefanb4046f02009-04-02 16:57:32 -070092 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070093 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070094static int dev_exception_add(struct dev_cgroup *dev_cgroup,
95 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070096{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -070097 struct dev_exception_item *excopy, *walk;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070098
Tejun Heo4b1c7842012-11-06 09:16:53 -080099 lockdep_assert_held(&devcgroup_mutex);
100
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700101 excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
102 if (!excopy)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700103 return -ENOMEM;
104
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700105 list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
106 if (walk->type != ex->type)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700107 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700108 if (walk->major != ex->major)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700109 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700110 if (walk->minor != ex->minor)
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700111 continue;
112
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700113 walk->access |= ex->access;
114 kfree(excopy);
115 excopy = NULL;
Pavel Emelyanovd1ee2972008-06-05 22:46:28 -0700116 }
117
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700118 if (excopy != NULL)
119 list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700120 return 0;
121}
122
123/*
Li Zefanb4046f02009-04-02 16:57:32 -0700124 * called under devcgroup_mutex
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700125 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700126static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
127 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700128{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700129 struct dev_exception_item *walk, *tmp;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700130
Tejun Heo4b1c7842012-11-06 09:16:53 -0800131 lockdep_assert_held(&devcgroup_mutex);
132
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700133 list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
134 if (walk->type != ex->type)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700135 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700136 if (walk->major != ex->major)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700137 continue;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700138 if (walk->minor != ex->minor)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700139 continue;
140
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700141 walk->access &= ~ex->access;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700142 if (!walk->access) {
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700143 list_del_rcu(&walk->list);
Lai Jiangshan6034f7e2011-03-15 18:07:57 +0800144 kfree_rcu(walk, rcu);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700145 }
146 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700147}
148
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800149static void __dev_exception_clean(struct dev_cgroup *dev_cgroup)
150{
151 struct dev_exception_item *ex, *tmp;
152
153 list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
154 list_del_rcu(&ex->list);
155 kfree_rcu(ex, rcu);
156 }
157}
158
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700159/**
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700160 * dev_exception_clean - frees all entries of the exception list
161 * @dev_cgroup: dev_cgroup with the exception list to be cleaned
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700162 *
163 * called under devcgroup_mutex
164 */
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700165static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700166{
Tejun Heo4b1c7842012-11-06 09:16:53 -0800167 lockdep_assert_held(&devcgroup_mutex);
168
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800169 __dev_exception_clean(dev_cgroup);
Aristeu Rozanski868539a2012-10-04 17:15:15 -0700170}
171
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500172static inline bool is_devcg_online(const struct dev_cgroup *devcg)
173{
174 return (devcg->behavior != DEVCG_DEFAULT_NONE);
175}
176
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500177/**
178 * devcgroup_online - initializes devcgroup's behavior and exceptions based on
179 * parent's
Tejun Heoeb954192013-08-08 20:11:23 -0400180 * @css: css getting online
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500181 * returns 0 in case of success, error code otherwise
182 */
Tejun Heoeb954192013-08-08 20:11:23 -0400183static int devcgroup_online(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500184{
Tejun Heoeb954192013-08-08 20:11:23 -0400185 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -0400186 struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css->parent);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500187 int ret = 0;
188
189 mutex_lock(&devcgroup_mutex);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500190
191 if (parent_dev_cgroup == NULL)
192 dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
193 else {
194 ret = dev_exceptions_copy(&dev_cgroup->exceptions,
195 &parent_dev_cgroup->exceptions);
196 if (!ret)
197 dev_cgroup->behavior = parent_dev_cgroup->behavior;
198 }
199 mutex_unlock(&devcgroup_mutex);
200
201 return ret;
202}
203
Tejun Heoeb954192013-08-08 20:11:23 -0400204static void devcgroup_offline(struct cgroup_subsys_state *css)
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500205{
Tejun Heoeb954192013-08-08 20:11:23 -0400206 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500207
208 mutex_lock(&devcgroup_mutex);
209 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
210 mutex_unlock(&devcgroup_mutex);
211}
212
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700213/*
214 * called from kernel/cgroup.c with cgroup_lock() held.
215 */
Tejun Heoeb954192013-08-08 20:11:23 -0400216static struct cgroup_subsys_state *
217devcgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700218{
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500219 struct dev_cgroup *dev_cgroup;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700220
221 dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
222 if (!dev_cgroup)
223 return ERR_PTR(-ENOMEM);
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700224 INIT_LIST_HEAD(&dev_cgroup->exceptions);
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500225 dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700226
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700227 return &dev_cgroup->css;
228}
229
Tejun Heoeb954192013-08-08 20:11:23 -0400230static void devcgroup_css_free(struct cgroup_subsys_state *css)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700231{
Tejun Heoeb954192013-08-08 20:11:23 -0400232 struct dev_cgroup *dev_cgroup = css_to_devcgroup(css);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700233
Jerry Snitselaar53eb8c82013-02-21 16:41:31 -0800234 __dev_exception_clean(dev_cgroup);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700235 kfree(dev_cgroup);
236}
237
238#define DEVCG_ALLOW 1
239#define DEVCG_DENY 2
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700240#define DEVCG_LIST 3
241
Li Zefan17d213f2008-07-13 12:14:02 -0700242#define MAJMINLEN 13
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700243#define ACCLEN 4
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700244
245static void set_access(char *acc, short access)
246{
247 int idx = 0;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700248 memset(acc, 0, ACCLEN);
Roman Gushchin67e306f2017-11-05 08:15:30 -0500249 if (access & DEVCG_ACC_READ)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700250 acc[idx++] = 'r';
Roman Gushchin67e306f2017-11-05 08:15:30 -0500251 if (access & DEVCG_ACC_WRITE)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700252 acc[idx++] = 'w';
Roman Gushchin67e306f2017-11-05 08:15:30 -0500253 if (access & DEVCG_ACC_MKNOD)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700254 acc[idx++] = 'm';
255}
256
257static char type_to_char(short type)
258{
Roman Gushchin67e306f2017-11-05 08:15:30 -0500259 if (type == DEVCG_DEV_ALL)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700260 return 'a';
Roman Gushchin67e306f2017-11-05 08:15:30 -0500261 if (type == DEVCG_DEV_CHAR)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700262 return 'c';
Roman Gushchin67e306f2017-11-05 08:15:30 -0500263 if (type == DEVCG_DEV_BLOCK)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700264 return 'b';
265 return 'X';
266}
267
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700268static void set_majmin(char *str, unsigned m)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700269{
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700270 if (m == ~0)
Li Zefan7759fc92008-07-25 01:47:08 -0700271 strcpy(str, "*");
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700272 else
Li Zefan7759fc92008-07-25 01:47:08 -0700273 sprintf(str, "%u", m);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700274}
275
Tejun Heo2da8ca82013-12-05 12:28:04 -0500276static int devcgroup_seq_show(struct seq_file *m, void *v)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700277{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500278 struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700279 struct dev_exception_item *ex;
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700280 char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700281
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700282 rcu_read_lock();
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700283 /*
284 * To preserve the compatibility:
285 * - Only show the "all devices" when the default policy is to allow
286 * - List the exceptions in case the default policy is to deny
287 * This way, the file remains as a "whitelist of devices"
288 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700289 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Roman Gushchin67e306f2017-11-05 08:15:30 -0500290 set_access(acc, DEVCG_ACC_MASK);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700291 set_majmin(maj, ~0);
292 set_majmin(min, ~0);
Roman Gushchin67e306f2017-11-05 08:15:30 -0500293 seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL),
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700294 maj, min, acc);
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700295 } else {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700296 list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
297 set_access(acc, ex->access);
298 set_majmin(maj, ex->major);
299 set_majmin(min, ex->minor);
300 seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700301 maj, min, acc);
302 }
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700303 }
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700304 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700305
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700306 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700307}
308
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700309/**
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400310 * match_exception - iterates the exception list trying to find a complete match
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400311 * @exceptions: list of exceptions
Roman Gushchin67e306f2017-11-05 08:15:30 -0500312 * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR)
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400313 * @major: device file major number, ~0 to match all
314 * @minor: device file minor number, ~0 to match all
Roman Gushchin67e306f2017-11-05 08:15:30 -0500315 * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD)
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400316 *
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400317 * It is considered a complete match if an exception is found that will
318 * contain the entire range of provided parameters.
319 *
320 * Return: true in case it matches an exception completely
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700321 */
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400322static bool match_exception(struct list_head *exceptions, short type,
323 u32 major, u32 minor, short access)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700324{
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700325 struct dev_exception_item *ex;
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400326
327 list_for_each_entry_rcu(ex, exceptions, list) {
Roman Gushchin67e306f2017-11-05 08:15:30 -0500328 if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK))
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400329 continue;
Roman Gushchin67e306f2017-11-05 08:15:30 -0500330 if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR))
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400331 continue;
332 if (ex->major != ~0 && ex->major != major)
333 continue;
334 if (ex->minor != ~0 && ex->minor != minor)
335 continue;
336 /* provided access cannot have more than the exception rule */
337 if (access & (~ex->access))
338 continue;
339 return true;
340 }
341 return false;
342}
343
344/**
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400345 * match_exception_partial - iterates the exception list trying to find a partial match
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400346 * @exceptions: list of exceptions
Roman Gushchin67e306f2017-11-05 08:15:30 -0500347 * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR)
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400348 * @major: device file major number, ~0 to match all
349 * @minor: device file minor number, ~0 to match all
Roman Gushchin67e306f2017-11-05 08:15:30 -0500350 * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD)
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400351 *
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400352 * It is considered a partial match if an exception's range is found to
353 * contain *any* of the devices specified by provided parameters. This is
354 * used to make sure no extra access is being granted that is forbidden by
355 * any of the exception list.
356 *
357 * Return: true in case the provided range mat matches an exception completely
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400358 */
359static bool match_exception_partial(struct list_head *exceptions, short type,
360 u32 major, u32 minor, short access)
361{
362 struct dev_exception_item *ex;
363
364 list_for_each_entry_rcu(ex, exceptions, list) {
Roman Gushchin67e306f2017-11-05 08:15:30 -0500365 if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK))
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400366 continue;
Roman Gushchin67e306f2017-11-05 08:15:30 -0500367 if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR))
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400368 continue;
369 /*
370 * We must be sure that both the exception and the provided
371 * range aren't masking all devices
372 */
373 if (ex->major != ~0 && major != ~0 && ex->major != major)
374 continue;
375 if (ex->minor != ~0 && minor != ~0 && ex->minor != minor)
376 continue;
377 /*
378 * In order to make sure the provided range isn't matching
379 * an exception, all its access bits shouldn't match the
380 * exception's access bits
381 */
382 if (!(access & ex->access))
383 continue;
384 return true;
385 }
386 return false;
387}
388
389/**
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400390 * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400391 * @dev_cgroup: dev cgroup to be tested against
392 * @refex: new exception
393 * @behavior: behavior of the exception's dev_cgroup
Aristeu Rozanskif5f3cf6f2014-04-24 15:33:21 -0400394 *
395 * This is used to make sure a child cgroup won't have more privileges
396 * than its parent
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400397 */
398static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
399 struct dev_exception_item *refex,
400 enum devcg_behavior behavior)
401{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700402 bool match = false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700403
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700404 RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
Paul E. McKenneydc3a04d2015-09-02 17:11:22 -0700405 !lockdep_is_held(&devcgroup_mutex),
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -0700406 "device_cgroup:verify_new_ex called without proper synchronization");
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700407
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500408 if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
409 if (behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400410 /*
411 * new exception in the child doesn't matter, only
412 * adding extra restrictions
413 */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500414 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500415 } else {
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400416 /*
417 * new exception in the child will add more devices
418 * that can be acessed, so it can't match any of
419 * parent's exceptions, even slightly
420 */
421 match = match_exception_partial(&dev_cgroup->exceptions,
422 refex->type,
423 refex->major,
424 refex->minor,
425 refex->access);
426
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500427 if (match)
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500428 return false;
429 return true;
430 }
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500431 } else {
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400432 /*
433 * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
434 * the new exception will add access to more devices and must
435 * be contained completely in an parent's exception to be
436 * allowed
437 */
438 match = match_exception(&dev_cgroup->exceptions, refex->type,
439 refex->major, refex->minor,
440 refex->access);
441
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500442 if (match)
443 /* parent has an exception that matches the proposed */
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500444 return true;
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500445 else
446 return false;
Aristeu Rozanski26898fd2013-02-15 11:55:44 -0500447 }
448 return false;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700449}
450
451/*
452 * parent_has_perm:
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700453 * when adding a new allow rule to a device exception list, the rule
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700454 * must be allowed in the parent device
455 */
Paul Menagef92523e2008-07-25 01:47:03 -0700456static int parent_has_perm(struct dev_cgroup *childcg,
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700457 struct dev_exception_item *ex)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700458{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400459 struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700460
Tejun Heo63876982013-08-08 20:11:23 -0400461 if (!parent)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700462 return 1;
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400463 return verify_new_ex(parent, ex, childcg->behavior);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700464}
465
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700466/**
Aristeu Rozanskid2c2b112014-05-05 11:18:59 -0400467 * parent_allows_removal - verify if it's ok to remove an exception
468 * @childcg: child cgroup from where the exception will be removed
469 * @ex: exception being removed
470 *
471 * When removing an exception in cgroups with default ALLOW policy, it must
472 * be checked if removing it will give the child cgroup more access than the
473 * parent.
474 *
475 * Return: true if it's ok to remove exception, false otherwise
476 */
477static bool parent_allows_removal(struct dev_cgroup *childcg,
478 struct dev_exception_item *ex)
479{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400480 struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
Aristeu Rozanskid2c2b112014-05-05 11:18:59 -0400481
482 if (!parent)
483 return true;
484
485 /* It's always allowed to remove access to devices */
486 if (childcg->behavior == DEVCG_DEFAULT_DENY)
487 return true;
488
489 /*
490 * Make sure you're not removing part or a whole exception existing in
491 * the parent cgroup
492 */
493 return !match_exception_partial(&parent->exceptions, ex->type,
494 ex->major, ex->minor, ex->access);
495}
496
497/**
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700498 * may_allow_all - checks if it's possible to change the behavior to
499 * allow based on parent's rules.
500 * @parent: device cgroup's parent
501 * returns: != 0 in case it's allowed, 0 otherwise
502 */
503static inline int may_allow_all(struct dev_cgroup *parent)
504{
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800505 if (!parent)
506 return 1;
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700507 return parent->behavior == DEVCG_DEFAULT_ALLOW;
508}
509
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500510/**
511 * revalidate_active_exceptions - walks through the active exception list and
512 * revalidates the exceptions based on parent's
513 * behavior and exceptions. The exceptions that
514 * are no longer valid will be removed.
515 * Called with devcgroup_mutex held.
516 * @devcg: cgroup which exceptions will be checked
517 *
518 * This is one of the three key functions for hierarchy implementation.
519 * This function is responsible for re-evaluating all the cgroup's active
520 * exceptions due to a parent's exception change.
521 * Refer to Documentation/cgroups/devices.txt for more details.
522 */
523static void revalidate_active_exceptions(struct dev_cgroup *devcg)
524{
525 struct dev_exception_item *ex;
526 struct list_head *this, *tmp;
527
528 list_for_each_safe(this, tmp, &devcg->exceptions) {
529 ex = container_of(this, struct dev_exception_item, list);
530 if (!parent_has_perm(devcg, ex))
531 dev_exception_rm(devcg, ex);
532 }
533}
534
535/**
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500536 * propagate_exception - propagates a new exception to the children
537 * @devcg_root: device cgroup that added a new exception
538 * @ex: new exception to be propagated
539 *
540 * returns: 0 in case of success, != 0 in case of error
541 */
542static int propagate_exception(struct dev_cgroup *devcg_root,
543 struct dev_exception_item *ex)
544{
Tejun Heo492eb212013-08-08 20:11:25 -0400545 struct cgroup_subsys_state *pos;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500546 int rc = 0;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500547
Tejun Heod591fb52013-05-24 10:55:38 +0900548 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500549
Tejun Heo492eb212013-08-08 20:11:25 -0400550 css_for_each_descendant_pre(pos, &devcg_root->css) {
551 struct dev_cgroup *devcg = css_to_devcgroup(pos);
Tejun Heod591fb52013-05-24 10:55:38 +0900552
553 /*
554 * Because devcgroup_mutex is held, no devcg will become
555 * online or offline during the tree walk (see on/offline
556 * methods), and online ones are safe to access outside RCU
557 * read lock without bumping refcnt.
558 */
Tejun Heobd8815a2013-08-08 20:11:27 -0400559 if (pos == &devcg_root->css || !is_devcg_online(devcg))
Tejun Heod591fb52013-05-24 10:55:38 +0900560 continue;
561
562 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500563
564 /*
565 * in case both root's behavior and devcg is allow, a new
566 * restriction means adding to the exception list
567 */
568 if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
569 devcg->behavior == DEVCG_DEFAULT_ALLOW) {
570 rc = dev_exception_add(devcg, ex);
571 if (rc)
572 break;
573 } else {
574 /*
575 * in the other possible cases:
576 * root's behavior: allow, devcg's: deny
577 * root's behavior: deny, devcg's: deny
578 * the exception will be removed
579 */
580 dev_exception_rm(devcg, ex);
581 }
582 revalidate_active_exceptions(devcg);
583
Tejun Heod591fb52013-05-24 10:55:38 +0900584 rcu_read_lock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500585 }
Tejun Heod591fb52013-05-24 10:55:38 +0900586
587 rcu_read_unlock();
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500588 return rc;
589}
590
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700591/*
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700592 * Modify the exception list using allow/deny rules.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700593 * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
594 * so we can give a container CAP_MKNOD to let it create devices but not
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700595 * modify the exception list.
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700596 * It seems likely we'll want to add a CAP_CONTAINER capability to allow
597 * us to also grant CAP_SYS_ADMIN to containers without giving away the
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700598 * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700599 *
600 * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
601 * new access is only allowed if you're in the top-level cgroup, or your
602 * parent cgroup has the access you're asking for.
603 */
Paul Menagef92523e2008-07-25 01:47:03 -0700604static int devcgroup_update_access(struct dev_cgroup *devcgroup,
Tejun Heo4d3bb512014-03-19 10:23:54 -0400605 int filetype, char *buffer)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700606{
Paul Menagef92523e2008-07-25 01:47:03 -0700607 const char *b;
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700608 char temp[12]; /* 11 + 1 characters needed for a u32 */
Aristeu Rozanskic39a2a32013-02-15 11:55:45 -0500609 int count, rc = 0;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700610 struct dev_exception_item ex;
Tejun Heo5c9d5352014-05-16 13:22:48 -0400611 struct dev_cgroup *parent = css_to_devcgroup(devcgroup->css.parent);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700612
613 if (!capable(CAP_SYS_ADMIN))
614 return -EPERM;
615
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700616 memset(&ex, 0, sizeof(ex));
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700617 b = buffer;
618
619 switch (*b) {
620 case 'a':
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700621 switch (filetype) {
622 case DEVCG_ALLOW:
Tejun Heo7a3bb242014-05-16 13:22:52 -0400623 if (css_has_online_children(&devcgroup->css))
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500624 return -EINVAL;
625
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700626 if (!may_allow_all(parent))
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700627 return -EPERM;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700628 dev_exception_clean(devcgroup);
Aristeu Rozanski64e10472012-11-06 07:25:04 -0800629 devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
630 if (!parent)
631 break;
632
Aristeu Rozanski4cef7292012-10-25 13:37:45 -0700633 rc = dev_exceptions_copy(&devcgroup->exceptions,
634 &parent->exceptions);
635 if (rc)
636 return rc;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700637 break;
638 case DEVCG_DENY:
Tejun Heo7a3bb242014-05-16 13:22:52 -0400639 if (css_has_online_children(&devcgroup->css))
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500640 return -EINVAL;
641
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700642 dev_exception_clean(devcgroup);
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700643 devcgroup->behavior = DEVCG_DEFAULT_DENY;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700644 break;
645 default:
646 return -EINVAL;
647 }
648 return 0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700649 case 'b':
Roman Gushchin67e306f2017-11-05 08:15:30 -0500650 ex.type = DEVCG_DEV_BLOCK;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700651 break;
652 case 'c':
Roman Gushchin67e306f2017-11-05 08:15:30 -0500653 ex.type = DEVCG_DEV_CHAR;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700654 break;
655 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700656 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700657 }
658 b++;
Paul Menagef92523e2008-07-25 01:47:03 -0700659 if (!isspace(*b))
660 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700661 b++;
662 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700663 ex.major = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700664 b++;
665 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700666 memset(temp, 0, sizeof(temp));
667 for (count = 0; count < sizeof(temp) - 1; count++) {
668 temp[count] = *b;
669 b++;
670 if (!isdigit(*b))
671 break;
672 }
673 rc = kstrtou32(temp, 10, &ex.major);
674 if (rc)
675 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700676 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700677 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700678 }
Paul Menagef92523e2008-07-25 01:47:03 -0700679 if (*b != ':')
680 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700681 b++;
682
683 /* read minor */
684 if (*b == '*') {
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700685 ex.minor = ~0;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700686 b++;
687 } else if (isdigit(*b)) {
Aristeu Rozanski26fd8402012-10-25 13:37:41 -0700688 memset(temp, 0, sizeof(temp));
689 for (count = 0; count < sizeof(temp) - 1; count++) {
690 temp[count] = *b;
691 b++;
692 if (!isdigit(*b))
693 break;
694 }
695 rc = kstrtou32(temp, 10, &ex.minor);
696 if (rc)
697 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700698 } else {
Paul Menagef92523e2008-07-25 01:47:03 -0700699 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700700 }
Paul Menagef92523e2008-07-25 01:47:03 -0700701 if (!isspace(*b))
702 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700703 for (b++, count = 0; count < 3; count++, b++) {
704 switch (*b) {
705 case 'r':
Roman Gushchin67e306f2017-11-05 08:15:30 -0500706 ex.access |= DEVCG_ACC_READ;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700707 break;
708 case 'w':
Roman Gushchin67e306f2017-11-05 08:15:30 -0500709 ex.access |= DEVCG_ACC_WRITE;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700710 break;
711 case 'm':
Roman Gushchin67e306f2017-11-05 08:15:30 -0500712 ex.access |= DEVCG_ACC_MKNOD;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700713 break;
714 case '\n':
715 case '\0':
716 count = 3;
717 break;
718 default:
Paul Menagef92523e2008-07-25 01:47:03 -0700719 return -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700720 }
721 }
722
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700723 switch (filetype) {
724 case DEVCG_ALLOW:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700725 /*
726 * If the default policy is to allow by default, try to remove
727 * an matching exception instead. And be silent about it: we
728 * don't want to break compatibility
729 */
Aristeu Rozanski5b7aa7d2012-10-25 13:37:38 -0700730 if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
Aristeu Rozanskid2c2b112014-05-05 11:18:59 -0400731 /* Check if the parent allows removing it first */
732 if (!parent_allows_removal(devcgroup, &ex))
733 return -EPERM;
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700734 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskid2c2b112014-05-05 11:18:59 -0400735 break;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700736 }
Aristeu Rozanskid2c2b112014-05-05 11:18:59 -0400737
738 if (!parent_has_perm(devcgroup, &ex))
739 return -EPERM;
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500740 rc = dev_exception_add(devcgroup, &ex);
741 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700742 case DEVCG_DENY:
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700743 /*
744 * If the default policy is to deny by default, try to remove
745 * an matching exception instead. And be silent about it: we
746 * don't want to break compatibility
747 */
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500748 if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
Aristeu Rozanskidb9aeca2012-10-04 17:15:20 -0700749 dev_exception_rm(devcgroup, &ex);
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500750 else
751 rc = dev_exception_add(devcgroup, &ex);
752
753 if (rc)
754 break;
755 /* we only propagate new restrictions */
756 rc = propagate_exception(devcgroup, &ex);
757 break;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700758 default:
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500759 rc = -EINVAL;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700760 }
Aristeu Rozanskibd2953e2013-02-15 11:55:47 -0500761 return rc;
Paul Menagef92523e2008-07-25 01:47:03 -0700762}
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700763
Tejun Heo451af502014-05-13 12:16:21 -0400764static ssize_t devcgroup_access_write(struct kernfs_open_file *of,
765 char *buf, size_t nbytes, loff_t off)
Paul Menagef92523e2008-07-25 01:47:03 -0700766{
767 int retval;
Li Zefanb4046f02009-04-02 16:57:32 -0700768
769 mutex_lock(&devcgroup_mutex);
Tejun Heo451af502014-05-13 12:16:21 -0400770 retval = devcgroup_update_access(css_to_devcgroup(of_css(of)),
771 of_cft(of)->private, strstrip(buf));
Li Zefanb4046f02009-04-02 16:57:32 -0700772 mutex_unlock(&devcgroup_mutex);
Tejun Heo451af502014-05-13 12:16:21 -0400773 return retval ?: nbytes;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700774}
775
776static struct cftype dev_cgroup_files[] = {
777 {
778 .name = "allow",
Tejun Heo451af502014-05-13 12:16:21 -0400779 .write = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700780 .private = DEVCG_ALLOW,
781 },
782 {
783 .name = "deny",
Tejun Heo451af502014-05-13 12:16:21 -0400784 .write = devcgroup_access_write,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700785 .private = DEVCG_DENY,
786 },
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700787 {
788 .name = "list",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500789 .seq_show = devcgroup_seq_show,
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700790 .private = DEVCG_LIST,
791 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700792 { } /* terminate */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700793};
794
Tejun Heo073219e2014-02-08 10:36:58 -0500795struct cgroup_subsys devices_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800796 .css_alloc = devcgroup_css_alloc,
797 .css_free = devcgroup_css_free,
Aristeu Rozanski1909554c2013-02-15 11:55:46 -0500798 .css_online = devcgroup_online,
799 .css_offline = devcgroup_offline,
Tejun Heo55779642014-07-15 11:05:09 -0400800 .legacy_cftypes = dev_cgroup_files,
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700801};
802
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700803/**
804 * __devcgroup_check_permission - checks if an inode operation is permitted
805 * @dev_cgroup: the dev cgroup to be tested against
806 * @type: device type
807 * @major: device major number
808 * @minor: device minor number
Roman Gushchin67e306f2017-11-05 08:15:30 -0500809 * @access: combination of DEVCG_ACC_WRITE, DEVCG_ACC_READ and DEVCG_ACC_MKNOD
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700810 *
811 * returns 0 on success, -EPERM case the operation is not permitted
812 */
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700813static int __devcgroup_check_permission(short type, u32 major, u32 minor,
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700814 short access)
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700815{
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700816 struct dev_cgroup *dev_cgroup;
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400817 bool rc;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700818
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700819 rcu_read_lock();
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700820 dev_cgroup = task_devcgroup(current);
Aristeu Rozanski79d71972014-04-21 12:13:03 -0400821 if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW)
822 /* Can't match any of the exceptions, even partially */
823 rc = !match_exception_partial(&dev_cgroup->exceptions,
824 type, major, minor, access);
825 else
826 /* Need to match completely one exception to be allowed */
827 rc = match_exception(&dev_cgroup->exceptions, type, major,
828 minor, access);
Pavel Emelyanov4efd1a12008-07-25 01:47:07 -0700829 rcu_read_unlock();
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700830
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700831 if (!rc)
832 return -EPERM;
833
834 return 0;
835}
836
837int __devcgroup_inode_permission(struct inode *inode, int mask)
838{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700839 short type, access = 0;
840
841 if (S_ISBLK(inode->i_mode))
Roman Gushchin67e306f2017-11-05 08:15:30 -0500842 type = DEVCG_DEV_BLOCK;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700843 if (S_ISCHR(inode->i_mode))
Roman Gushchin67e306f2017-11-05 08:15:30 -0500844 type = DEVCG_DEV_CHAR;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700845 if (mask & MAY_WRITE)
Roman Gushchin67e306f2017-11-05 08:15:30 -0500846 access |= DEVCG_ACC_WRITE;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700847 if (mask & MAY_READ)
Roman Gushchin67e306f2017-11-05 08:15:30 -0500848 access |= DEVCG_ACC_READ;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700849
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700850 return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
851 access);
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700852}
853
854int devcgroup_inode_mknod(int mode, dev_t dev)
855{
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700856 short type;
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700857
Serge E. Hallyn0b82ac32009-01-07 18:07:46 -0800858 if (!S_ISBLK(mode) && !S_ISCHR(mode))
859 return 0;
860
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700861 if (S_ISBLK(mode))
Roman Gushchin67e306f2017-11-05 08:15:30 -0500862 type = DEVCG_DEV_BLOCK;
Aristeu Rozanskiad676072012-10-04 17:15:17 -0700863 else
Roman Gushchin67e306f2017-11-05 08:15:30 -0500864 type = DEVCG_DEV_CHAR;
Li Zefan36fd71d2008-09-02 14:35:52 -0700865
Jiri Slaby8c9506d2012-10-25 13:37:34 -0700866 return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
Roman Gushchin67e306f2017-11-05 08:15:30 -0500867 DEVCG_ACC_MKNOD);
Li Zefan36fd71d2008-09-02 14:35:52 -0700868
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700869}