Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 1 | /* |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 2 | * device_cgroup.c - device cgroup subsystem |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3 | * |
| 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. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 12 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 14 | #include <linux/rcupdate.h> |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 16 | |
| 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 Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 26 | static DEFINE_MUTEX(devcgroup_mutex); |
| 27 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 28 | enum devcg_behavior { |
| 29 | DEVCG_DEFAULT_NONE, |
| 30 | DEVCG_DEFAULT_ALLOW, |
| 31 | DEVCG_DEFAULT_DENY, |
| 32 | }; |
| 33 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 34 | /* |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 35 | * exception list locking rules: |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 36 | * hold devcgroup_mutex for update/read. |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 37 | * hold rcu_read_lock() for read. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 40 | struct dev_exception_item { |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 41 | u32 major, minor; |
| 42 | short type; |
| 43 | short access; |
| 44 | struct list_head list; |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 45 | struct rcu_head rcu; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct dev_cgroup { |
| 49 | struct cgroup_subsys_state css; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 50 | struct list_head exceptions; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 51 | enum devcg_behavior behavior; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Pavel Emelyanov | b66862f | 2008-06-05 22:46:24 -0700 | [diff] [blame] | 54 | static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s) |
| 55 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 56 | return s ? container_of(s, struct dev_cgroup, css) : NULL; |
Pavel Emelyanov | b66862f | 2008-06-05 22:46:24 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 59 | static inline struct dev_cgroup *task_devcgroup(struct task_struct *task) |
| 60 | { |
Tejun Heo | 8af01f5 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 61 | return css_to_devcgroup(task_css(task, devices_subsys_id)); |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 64 | struct cgroup_subsys devices_subsys; |
| 65 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 66 | static int devcgroup_can_attach(struct cgroup_subsys_state *new_css, |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 67 | struct cgroup_taskset *set) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 68 | { |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 69 | struct task_struct *task = cgroup_taskset_first(set); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 70 | |
Tejun Heo | 2f7ee56 | 2011-12-12 18:12:21 -0800 | [diff] [blame] | 71 | if (current != task && !capable(CAP_SYS_ADMIN)) |
| 72 | return -EPERM; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 77 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 78 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 79 | static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 80 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 81 | struct dev_exception_item *ex, *tmp, *new; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 82 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 83 | lockdep_assert_held(&devcgroup_mutex); |
| 84 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 85 | list_for_each_entry(ex, orig, list) { |
| 86 | new = kmemdup(ex, sizeof(*ex), GFP_KERNEL); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 87 | if (!new) |
| 88 | goto free_and_exit; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 89 | list_add_tail(&new->list, dest); |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | |
| 94 | free_and_exit: |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 95 | list_for_each_entry_safe(ex, tmp, dest, list) { |
| 96 | list_del(&ex->list); |
| 97 | kfree(ex); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 98 | } |
| 99 | return -ENOMEM; |
| 100 | } |
| 101 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 102 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 103 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 104 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 105 | static int dev_exception_add(struct dev_cgroup *dev_cgroup, |
| 106 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 107 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 108 | struct dev_exception_item *excopy, *walk; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 109 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 110 | lockdep_assert_held(&devcgroup_mutex); |
| 111 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 112 | excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL); |
| 113 | if (!excopy) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 114 | return -ENOMEM; |
| 115 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 116 | list_for_each_entry(walk, &dev_cgroup->exceptions, list) { |
| 117 | if (walk->type != ex->type) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 118 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 119 | if (walk->major != ex->major) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 120 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 121 | if (walk->minor != ex->minor) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 122 | continue; |
| 123 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 124 | walk->access |= ex->access; |
| 125 | kfree(excopy); |
| 126 | excopy = NULL; |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 129 | if (excopy != NULL) |
| 130 | list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 135 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 136 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 137 | static void dev_exception_rm(struct dev_cgroup *dev_cgroup, |
| 138 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 139 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 140 | struct dev_exception_item *walk, *tmp; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 141 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 142 | lockdep_assert_held(&devcgroup_mutex); |
| 143 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 144 | list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) { |
| 145 | if (walk->type != ex->type) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 146 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 147 | if (walk->major != ex->major) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 148 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 149 | if (walk->minor != ex->minor) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 150 | continue; |
| 151 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 152 | walk->access &= ~ex->access; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 153 | if (!walk->access) { |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 154 | list_del_rcu(&walk->list); |
Lai Jiangshan | 6034f7e | 2011-03-15 18:07:57 +0800 | [diff] [blame] | 155 | kfree_rcu(walk, rcu); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 160 | static 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 Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 170 | /** |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 171 | * dev_exception_clean - frees all entries of the exception list |
| 172 | * @dev_cgroup: dev_cgroup with the exception list to be cleaned |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 173 | * |
| 174 | * called under devcgroup_mutex |
| 175 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 176 | static void dev_exception_clean(struct dev_cgroup *dev_cgroup) |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 177 | { |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 178 | lockdep_assert_held(&devcgroup_mutex); |
| 179 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 180 | __dev_exception_clean(dev_cgroup); |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 183 | static inline bool is_devcg_online(const struct dev_cgroup *devcg) |
| 184 | { |
| 185 | return (devcg->behavior != DEVCG_DEFAULT_NONE); |
| 186 | } |
| 187 | |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 188 | /** |
| 189 | * devcgroup_online - initializes devcgroup's behavior and exceptions based on |
| 190 | * parent's |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 191 | * @css: css getting online |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 192 | * returns 0 in case of success, error code otherwise |
| 193 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 194 | static int devcgroup_online(struct cgroup_subsys_state *css) |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 195 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 196 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
| 197 | struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css_parent(css)); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 198 | int ret = 0; |
| 199 | |
| 200 | mutex_lock(&devcgroup_mutex); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 201 | |
| 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 Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 215 | static void devcgroup_offline(struct cgroup_subsys_state *css) |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 216 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 217 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 218 | |
| 219 | mutex_lock(&devcgroup_mutex); |
| 220 | dev_cgroup->behavior = DEVCG_DEFAULT_NONE; |
| 221 | mutex_unlock(&devcgroup_mutex); |
| 222 | } |
| 223 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 224 | /* |
| 225 | * called from kernel/cgroup.c with cgroup_lock() held. |
| 226 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 227 | static struct cgroup_subsys_state * |
| 228 | devcgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 229 | { |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 230 | struct dev_cgroup *dev_cgroup; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 231 | |
| 232 | dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL); |
| 233 | if (!dev_cgroup) |
| 234 | return ERR_PTR(-ENOMEM); |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 235 | INIT_LIST_HEAD(&dev_cgroup->exceptions); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 236 | dev_cgroup->behavior = DEVCG_DEFAULT_NONE; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 237 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 238 | return &dev_cgroup->css; |
| 239 | } |
| 240 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 241 | static void devcgroup_css_free(struct cgroup_subsys_state *css) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 242 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 243 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 244 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 245 | __dev_exception_clean(dev_cgroup); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 246 | kfree(dev_cgroup); |
| 247 | } |
| 248 | |
| 249 | #define DEVCG_ALLOW 1 |
| 250 | #define DEVCG_DENY 2 |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 251 | #define DEVCG_LIST 3 |
| 252 | |
Li Zefan | 17d213f | 2008-07-13 12:14:02 -0700 | [diff] [blame] | 253 | #define MAJMINLEN 13 |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 254 | #define ACCLEN 4 |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 255 | |
| 256 | static void set_access(char *acc, short access) |
| 257 | { |
| 258 | int idx = 0; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 259 | memset(acc, 0, ACCLEN); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 260 | 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 | |
| 268 | static 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. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 279 | static void set_majmin(char *str, unsigned m) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 280 | { |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 281 | if (m == ~0) |
Li Zefan | 7759fc9 | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 282 | strcpy(str, "*"); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 283 | else |
Li Zefan | 7759fc9 | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 284 | sprintf(str, "%u", m); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 287 | static int devcgroup_seq_read(struct cgroup_subsys_state *css, |
| 288 | struct cftype *cft, struct seq_file *m) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 289 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 290 | struct dev_cgroup *devcgroup = css_to_devcgroup(css); |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 291 | struct dev_exception_item *ex; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 292 | char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN]; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 293 | |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 294 | rcu_read_lock(); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 295 | /* |
| 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 Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 301 | if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 302 | 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. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 306 | maj, min, acc); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 307 | } else { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 308 | 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 Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 313 | maj, min, acc); |
| 314 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 315 | } |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 316 | rcu_read_unlock(); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 317 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 318 | return 0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 321 | /** |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 322 | * 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 Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 327 | * @dev_cgroup: dev cgroup to be tested against |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 328 | * @refex: new exception |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 329 | * @behavior: behavior of the exception |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 330 | */ |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 331 | static bool may_access(struct dev_cgroup *dev_cgroup, |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 332 | struct dev_exception_item *refex, |
| 333 | enum devcg_behavior behavior) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 334 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 335 | struct dev_exception_item *ex; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 336 | bool match = false; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 337 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 338 | rcu_lockdep_assert(rcu_read_lock_held() || |
| 339 | lockdep_is_held(&devcgroup_mutex), |
| 340 | "device_cgroup::may_access() called without proper synchronization"); |
| 341 | |
Tejun Heo | 201e72a | 2012-11-06 09:17:37 -0800 | [diff] [blame] | 342 | list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 343 | if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK)) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 344 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 345 | if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR)) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 346 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 347 | if (ex->major != ~0 && ex->major != refex->major) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 348 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 349 | if (ex->minor != ~0 && ex->minor != refex->minor) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 350 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 351 | if (refex->access & (~ex->access)) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 352 | continue; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 353 | match = true; |
| 354 | break; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 355 | } |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 356 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 357 | if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
| 358 | if (behavior == DEVCG_DEFAULT_ALLOW) { |
| 359 | /* the exception will deny access to certain devices */ |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 360 | return true; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 361 | } 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 Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 371 | } else { |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 372 | /* only behavior == DEVCG_DEFAULT_DENY allowed here */ |
| 373 | if (match) |
| 374 | /* parent has an exception that matches the proposed */ |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 375 | return true; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 376 | else |
| 377 | return false; |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 378 | } |
| 379 | return false; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | * parent_has_perm: |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 384 | * when adding a new allow rule to a device exception list, the rule |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 385 | * must be allowed in the parent device |
| 386 | */ |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 387 | static int parent_has_perm(struct dev_cgroup *childcg, |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 388 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 389 | { |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 390 | struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css)); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 391 | |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 392 | if (!parent) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 393 | return 1; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 394 | return may_access(parent, ex, childcg->behavior); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 397 | /** |
| 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 | */ |
| 403 | static inline int may_allow_all(struct dev_cgroup *parent) |
| 404 | { |
Aristeu Rozanski | 64e1047 | 2012-11-06 07:25:04 -0800 | [diff] [blame] | 405 | if (!parent) |
| 406 | return 1; |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 407 | return parent->behavior == DEVCG_DEFAULT_ALLOW; |
| 408 | } |
| 409 | |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 410 | /** |
| 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 | */ |
| 423 | static 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 Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 436 | * 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 | */ |
| 442 | static int propagate_exception(struct dev_cgroup *devcg_root, |
| 443 | struct dev_exception_item *ex) |
| 444 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 445 | struct cgroup_subsys_state *pos; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 446 | int rc = 0; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 447 | |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 448 | rcu_read_lock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 449 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 450 | css_for_each_descendant_pre(pos, &devcg_root->css) { |
| 451 | struct dev_cgroup *devcg = css_to_devcgroup(pos); |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 452 | |
| 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 Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 459 | if (pos == &devcg_root->css || !is_devcg_online(devcg)) |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 460 | continue; |
| 461 | |
| 462 | rcu_read_unlock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 463 | |
| 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 Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 484 | rcu_read_lock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 485 | } |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 486 | |
| 487 | rcu_read_unlock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 488 | return rc; |
| 489 | } |
| 490 | |
| 491 | static 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. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 498 | /* |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 499 | * Modify the exception list using allow/deny rules. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 500 | * 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 Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 502 | * modify the exception list. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 503 | * 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 Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 505 | * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 506 | * |
| 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 Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 511 | static int devcgroup_update_access(struct dev_cgroup *devcgroup, |
| 512 | int filetype, const char *buffer) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 513 | { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 514 | const char *b; |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 515 | char temp[12]; /* 11 + 1 characters needed for a u32 */ |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 516 | int count, rc = 0; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 517 | struct dev_exception_item ex; |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 518 | struct dev_cgroup *parent = css_to_devcgroup(css_parent(&devcgroup->css)); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 519 | |
| 520 | if (!capable(CAP_SYS_ADMIN)) |
| 521 | return -EPERM; |
| 522 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 523 | memset(&ex, 0, sizeof(ex)); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 524 | b = buffer; |
| 525 | |
| 526 | switch (*b) { |
| 527 | case 'a': |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 528 | switch (filetype) { |
| 529 | case DEVCG_ALLOW: |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 530 | if (has_children(devcgroup)) |
| 531 | return -EINVAL; |
| 532 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 533 | if (!may_allow_all(parent)) |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 534 | return -EPERM; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 535 | dev_exception_clean(devcgroup); |
Aristeu Rozanski | 64e1047 | 2012-11-06 07:25:04 -0800 | [diff] [blame] | 536 | devcgroup->behavior = DEVCG_DEFAULT_ALLOW; |
| 537 | if (!parent) |
| 538 | break; |
| 539 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 540 | rc = dev_exceptions_copy(&devcgroup->exceptions, |
| 541 | &parent->exceptions); |
| 542 | if (rc) |
| 543 | return rc; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 544 | break; |
| 545 | case DEVCG_DENY: |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 546 | if (has_children(devcgroup)) |
| 547 | return -EINVAL; |
| 548 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 549 | dev_exception_clean(devcgroup); |
Aristeu Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 550 | devcgroup->behavior = DEVCG_DEFAULT_DENY; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 551 | break; |
| 552 | default: |
| 553 | return -EINVAL; |
| 554 | } |
| 555 | return 0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 556 | case 'b': |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 557 | ex.type = DEV_BLOCK; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 558 | break; |
| 559 | case 'c': |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 560 | ex.type = DEV_CHAR; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 561 | break; |
| 562 | default: |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 563 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 564 | } |
| 565 | b++; |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 566 | if (!isspace(*b)) |
| 567 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 568 | b++; |
| 569 | if (*b == '*') { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 570 | ex.major = ~0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 571 | b++; |
| 572 | } else if (isdigit(*b)) { |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 573 | 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. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 583 | } else { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 584 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 585 | } |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 586 | if (*b != ':') |
| 587 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 588 | b++; |
| 589 | |
| 590 | /* read minor */ |
| 591 | if (*b == '*') { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 592 | ex.minor = ~0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 593 | b++; |
| 594 | } else if (isdigit(*b)) { |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 595 | 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. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 605 | } else { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 606 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 607 | } |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 608 | if (!isspace(*b)) |
| 609 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 610 | for (b++, count = 0; count < 3; count++, b++) { |
| 611 | switch (*b) { |
| 612 | case 'r': |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 613 | ex.access |= ACC_READ; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 614 | break; |
| 615 | case 'w': |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 616 | ex.access |= ACC_WRITE; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 617 | break; |
| 618 | case 'm': |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 619 | ex.access |= ACC_MKNOD; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 620 | break; |
| 621 | case '\n': |
| 622 | case '\0': |
| 623 | count = 3; |
| 624 | break; |
| 625 | default: |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 626 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 630 | switch (filetype) { |
| 631 | case DEVCG_ALLOW: |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 632 | if (!parent_has_perm(devcgroup, &ex)) |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 633 | return -EPERM; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 634 | /* |
| 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 Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 639 | if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 640 | dev_exception_rm(devcgroup, &ex); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 641 | return 0; |
| 642 | } |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 643 | rc = dev_exception_add(devcgroup, &ex); |
| 644 | break; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 645 | case DEVCG_DENY: |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 646 | /* |
| 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 Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 651 | if (devcgroup->behavior == DEVCG_DEFAULT_DENY) |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 652 | dev_exception_rm(devcgroup, &ex); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 653 | 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. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 661 | default: |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 662 | rc = -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 663 | } |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 664 | return rc; |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 665 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 666 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 667 | static int devcgroup_access_write(struct cgroup_subsys_state *css, |
| 668 | struct cftype *cft, const char *buffer) |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 669 | { |
| 670 | int retval; |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 671 | |
| 672 | mutex_lock(&devcgroup_mutex); |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 673 | retval = devcgroup_update_access(css_to_devcgroup(css), |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 674 | cft->private, buffer); |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 675 | mutex_unlock(&devcgroup_mutex); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 676 | return retval; |
| 677 | } |
| 678 | |
| 679 | static struct cftype dev_cgroup_files[] = { |
| 680 | { |
| 681 | .name = "allow", |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 682 | .write_string = devcgroup_access_write, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 683 | .private = DEVCG_ALLOW, |
| 684 | }, |
| 685 | { |
| 686 | .name = "deny", |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 687 | .write_string = devcgroup_access_write, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 688 | .private = DEVCG_DENY, |
| 689 | }, |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 690 | { |
| 691 | .name = "list", |
| 692 | .read_seq_string = devcgroup_seq_read, |
| 693 | .private = DEVCG_LIST, |
| 694 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 695 | { } /* terminate */ |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 696 | }; |
| 697 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 698 | struct cgroup_subsys devices_subsys = { |
| 699 | .name = "devices", |
| 700 | .can_attach = devcgroup_can_attach, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 701 | .css_alloc = devcgroup_css_alloc, |
| 702 | .css_free = devcgroup_css_free, |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 703 | .css_online = devcgroup_online, |
| 704 | .css_offline = devcgroup_offline, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 705 | .subsys_id = devices_subsys_id, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 706 | .base_cftypes = dev_cgroup_files, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 707 | }; |
| 708 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 709 | /** |
| 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 Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 719 | static int __devcgroup_check_permission(short type, u32 major, u32 minor, |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 720 | short access) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 721 | { |
Jiri Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 722 | struct dev_cgroup *dev_cgroup; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 723 | struct dev_exception_item ex; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 724 | int rc; |
| 725 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 726 | memset(&ex, 0, sizeof(ex)); |
| 727 | ex.type = type; |
| 728 | ex.major = major; |
| 729 | ex.minor = minor; |
| 730 | ex.access = access; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 731 | |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 732 | rcu_read_lock(); |
Jiri Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 733 | dev_cgroup = task_devcgroup(current); |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 734 | rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior); |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 735 | rcu_read_unlock(); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 736 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 737 | if (!rc) |
| 738 | return -EPERM; |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | int __devcgroup_inode_permission(struct inode *inode, int mask) |
| 744 | { |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 745 | 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 Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 756 | return __devcgroup_check_permission(type, imajor(inode), iminor(inode), |
| 757 | access); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | int devcgroup_inode_mknod(int mode, dev_t dev) |
| 761 | { |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 762 | short type; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 763 | |
Serge E. Hallyn | 0b82ac3 | 2009-01-07 18:07:46 -0800 | [diff] [blame] | 764 | if (!S_ISBLK(mode) && !S_ISCHR(mode)) |
| 765 | return 0; |
| 766 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 767 | if (S_ISBLK(mode)) |
| 768 | type = DEV_BLOCK; |
| 769 | else |
| 770 | type = DEV_CHAR; |
Li Zefan | 36fd71d | 2008-09-02 14:35:52 -0700 | [diff] [blame] | 771 | |
Jiri Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 772 | return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev), |
| 773 | ACC_MKNOD); |
Li Zefan | 36fd71d | 2008-09-02 14:35:52 -0700 | [diff] [blame] | 774 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 775 | } |