Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 2 | /* |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 3 | * device_cgroup.c - device cgroup subsystem |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2007 IBM Corp |
| 6 | */ |
| 7 | |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 8 | #include <linux/bpf-cgroup.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 9 | #include <linux/device_cgroup.h> |
| 10 | #include <linux/cgroup.h> |
| 11 | #include <linux/ctype.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/uaccess.h> |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 16 | #include <linux/rcupdate.h> |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 18 | |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_CGROUP_DEVICE |
| 20 | |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 21 | static DEFINE_MUTEX(devcgroup_mutex); |
| 22 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 23 | enum devcg_behavior { |
| 24 | DEVCG_DEFAULT_NONE, |
| 25 | DEVCG_DEFAULT_ALLOW, |
| 26 | DEVCG_DEFAULT_DENY, |
| 27 | }; |
| 28 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 29 | /* |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 30 | * exception list locking rules: |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 31 | * hold devcgroup_mutex for update/read. |
Lai Jiangshan | 47c5980 | 2008-10-18 20:28:07 -0700 | [diff] [blame] | 32 | * hold rcu_read_lock() for read. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 35 | struct dev_exception_item { |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 36 | u32 major, minor; |
| 37 | short type; |
| 38 | short access; |
| 39 | struct list_head list; |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 40 | struct rcu_head rcu; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct dev_cgroup { |
| 44 | struct cgroup_subsys_state css; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 45 | struct list_head exceptions; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 46 | enum devcg_behavior behavior; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Pavel Emelyanov | b66862f | 2008-06-05 22:46:24 -0700 | [diff] [blame] | 49 | static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s) |
| 50 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 51 | return s ? container_of(s, struct dev_cgroup, css) : NULL; |
Pavel Emelyanov | b66862f | 2008-06-05 22:46:24 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 54 | static inline struct dev_cgroup *task_devcgroup(struct task_struct *task) |
| 55 | { |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 56 | return css_to_devcgroup(task_css(task, devices_cgrp_id)); |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 59 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 60 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 61 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 62 | 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] | 63 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 64 | struct dev_exception_item *ex, *tmp, *new; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 65 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 66 | lockdep_assert_held(&devcgroup_mutex); |
| 67 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 68 | list_for_each_entry(ex, orig, list) { |
| 69 | new = kmemdup(ex, sizeof(*ex), GFP_KERNEL); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 70 | if (!new) |
| 71 | goto free_and_exit; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 72 | list_add_tail(&new->list, dest); |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | |
| 77 | free_and_exit: |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 78 | list_for_each_entry_safe(ex, tmp, dest, list) { |
| 79 | list_del(&ex->list); |
| 80 | kfree(ex); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 81 | } |
| 82 | return -ENOMEM; |
| 83 | } |
| 84 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 85 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 86 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 87 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 88 | static int dev_exception_add(struct dev_cgroup *dev_cgroup, |
| 89 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 90 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 91 | struct dev_exception_item *excopy, *walk; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 92 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 93 | lockdep_assert_held(&devcgroup_mutex); |
| 94 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 95 | excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL); |
| 96 | if (!excopy) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 97 | return -ENOMEM; |
| 98 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 99 | list_for_each_entry(walk, &dev_cgroup->exceptions, list) { |
| 100 | if (walk->type != ex->type) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 101 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 102 | if (walk->major != ex->major) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 103 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 104 | if (walk->minor != ex->minor) |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 105 | continue; |
| 106 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 107 | walk->access |= ex->access; |
| 108 | kfree(excopy); |
| 109 | excopy = NULL; |
Pavel Emelyanov | d1ee297 | 2008-06-05 22:46:28 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 112 | if (excopy != NULL) |
| 113 | list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 118 | * called under devcgroup_mutex |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 119 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 120 | static void dev_exception_rm(struct dev_cgroup *dev_cgroup, |
| 121 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 122 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 123 | struct dev_exception_item *walk, *tmp; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 124 | |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 125 | lockdep_assert_held(&devcgroup_mutex); |
| 126 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 127 | list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) { |
| 128 | if (walk->type != ex->type) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 129 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 130 | if (walk->major != ex->major) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 131 | continue; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 132 | if (walk->minor != ex->minor) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 133 | continue; |
| 134 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 135 | walk->access &= ~ex->access; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 136 | if (!walk->access) { |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 137 | list_del_rcu(&walk->list); |
Lai Jiangshan | 6034f7e | 2011-03-15 18:07:57 +0800 | [diff] [blame] | 138 | kfree_rcu(walk, rcu); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 143 | static void __dev_exception_clean(struct dev_cgroup *dev_cgroup) |
| 144 | { |
| 145 | struct dev_exception_item *ex, *tmp; |
| 146 | |
| 147 | list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) { |
| 148 | list_del_rcu(&ex->list); |
| 149 | kfree_rcu(ex, rcu); |
| 150 | } |
| 151 | } |
| 152 | |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 153 | /** |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 154 | * dev_exception_clean - frees all entries of the exception list |
| 155 | * @dev_cgroup: dev_cgroup with the exception list to be cleaned |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 156 | * |
| 157 | * called under devcgroup_mutex |
| 158 | */ |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 159 | static void dev_exception_clean(struct dev_cgroup *dev_cgroup) |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 160 | { |
Tejun Heo | 4b1c784 | 2012-11-06 09:16:53 -0800 | [diff] [blame] | 161 | lockdep_assert_held(&devcgroup_mutex); |
| 162 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 163 | __dev_exception_clean(dev_cgroup); |
Aristeu Rozanski | 868539a | 2012-10-04 17:15:15 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 166 | static inline bool is_devcg_online(const struct dev_cgroup *devcg) |
| 167 | { |
| 168 | return (devcg->behavior != DEVCG_DEFAULT_NONE); |
| 169 | } |
| 170 | |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 171 | /** |
| 172 | * devcgroup_online - initializes devcgroup's behavior and exceptions based on |
| 173 | * parent's |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 174 | * @css: css getting online |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 175 | * returns 0 in case of success, error code otherwise |
| 176 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 177 | static int devcgroup_online(struct cgroup_subsys_state *css) |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 178 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 179 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 180 | struct dev_cgroup *parent_dev_cgroup = css_to_devcgroup(css->parent); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 181 | int ret = 0; |
| 182 | |
| 183 | mutex_lock(&devcgroup_mutex); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 184 | |
| 185 | if (parent_dev_cgroup == NULL) |
| 186 | dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW; |
| 187 | else { |
| 188 | ret = dev_exceptions_copy(&dev_cgroup->exceptions, |
| 189 | &parent_dev_cgroup->exceptions); |
| 190 | if (!ret) |
| 191 | dev_cgroup->behavior = parent_dev_cgroup->behavior; |
| 192 | } |
| 193 | mutex_unlock(&devcgroup_mutex); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 198 | static void devcgroup_offline(struct cgroup_subsys_state *css) |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 199 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 200 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 201 | |
| 202 | mutex_lock(&devcgroup_mutex); |
| 203 | dev_cgroup->behavior = DEVCG_DEFAULT_NONE; |
| 204 | mutex_unlock(&devcgroup_mutex); |
| 205 | } |
| 206 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 207 | /* |
| 208 | * called from kernel/cgroup.c with cgroup_lock() held. |
| 209 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 210 | static struct cgroup_subsys_state * |
| 211 | devcgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 212 | { |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 213 | struct dev_cgroup *dev_cgroup; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 214 | |
| 215 | dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL); |
| 216 | if (!dev_cgroup) |
| 217 | return ERR_PTR(-ENOMEM); |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 218 | INIT_LIST_HEAD(&dev_cgroup->exceptions); |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 219 | dev_cgroup->behavior = DEVCG_DEFAULT_NONE; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 220 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 221 | return &dev_cgroup->css; |
| 222 | } |
| 223 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 224 | static void devcgroup_css_free(struct cgroup_subsys_state *css) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 225 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 226 | struct dev_cgroup *dev_cgroup = css_to_devcgroup(css); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 227 | |
Jerry Snitselaar | 53eb8c8 | 2013-02-21 16:41:31 -0800 | [diff] [blame] | 228 | __dev_exception_clean(dev_cgroup); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 229 | kfree(dev_cgroup); |
| 230 | } |
| 231 | |
| 232 | #define DEVCG_ALLOW 1 |
| 233 | #define DEVCG_DENY 2 |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 234 | #define DEVCG_LIST 3 |
| 235 | |
Li Zefan | 17d213f | 2008-07-13 12:14:02 -0700 | [diff] [blame] | 236 | #define MAJMINLEN 13 |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 237 | #define ACCLEN 4 |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 238 | |
| 239 | static void set_access(char *acc, short access) |
| 240 | { |
| 241 | int idx = 0; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 242 | memset(acc, 0, ACCLEN); |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 243 | if (access & DEVCG_ACC_READ) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 244 | acc[idx++] = 'r'; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 245 | if (access & DEVCG_ACC_WRITE) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 246 | acc[idx++] = 'w'; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 247 | if (access & DEVCG_ACC_MKNOD) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 248 | acc[idx++] = 'm'; |
| 249 | } |
| 250 | |
| 251 | static char type_to_char(short type) |
| 252 | { |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 253 | if (type == DEVCG_DEV_ALL) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 254 | return 'a'; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 255 | if (type == DEVCG_DEV_CHAR) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 256 | return 'c'; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 257 | if (type == DEVCG_DEV_BLOCK) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 258 | return 'b'; |
| 259 | return 'X'; |
| 260 | } |
| 261 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 262 | static void set_majmin(char *str, unsigned m) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 263 | { |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 264 | if (m == ~0) |
Li Zefan | 7759fc9 | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 265 | strcpy(str, "*"); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 266 | else |
Li Zefan | 7759fc9 | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 267 | sprintf(str, "%u", m); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 270 | static int devcgroup_seq_show(struct seq_file *m, void *v) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 271 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 272 | struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m)); |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 273 | struct dev_exception_item *ex; |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 274 | char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN]; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 275 | |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 276 | rcu_read_lock(); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 277 | /* |
| 278 | * To preserve the compatibility: |
| 279 | * - Only show the "all devices" when the default policy is to allow |
| 280 | * - List the exceptions in case the default policy is to deny |
| 281 | * This way, the file remains as a "whitelist of devices" |
| 282 | */ |
Aristeu Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 283 | if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 284 | set_access(acc, DEVCG_ACC_MASK); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 285 | set_majmin(maj, ~0); |
| 286 | set_majmin(min, ~0); |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 287 | seq_printf(m, "%c %s:%s %s\n", type_to_char(DEVCG_DEV_ALL), |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 288 | maj, min, acc); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 289 | } else { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 290 | list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) { |
| 291 | set_access(acc, ex->access); |
| 292 | set_majmin(maj, ex->major); |
| 293 | set_majmin(min, ex->minor); |
| 294 | 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] | 295 | maj, min, acc); |
| 296 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 297 | } |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 298 | rcu_read_unlock(); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 299 | |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 300 | return 0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 303 | /** |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 304 | * match_exception - iterates the exception list trying to find a complete match |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 305 | * @exceptions: list of exceptions |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 306 | * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 307 | * @major: device file major number, ~0 to match all |
| 308 | * @minor: device file minor number, ~0 to match all |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 309 | * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 310 | * |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 311 | * It is considered a complete match if an exception is found that will |
| 312 | * contain the entire range of provided parameters. |
| 313 | * |
| 314 | * Return: true in case it matches an exception completely |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 315 | */ |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 316 | static bool match_exception(struct list_head *exceptions, short type, |
| 317 | u32 major, u32 minor, short access) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 318 | { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 319 | struct dev_exception_item *ex; |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 320 | |
| 321 | list_for_each_entry_rcu(ex, exceptions, list) { |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 322 | if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK)) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 323 | continue; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 324 | if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR)) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 325 | continue; |
| 326 | if (ex->major != ~0 && ex->major != major) |
| 327 | continue; |
| 328 | if (ex->minor != ~0 && ex->minor != minor) |
| 329 | continue; |
| 330 | /* provided access cannot have more than the exception rule */ |
| 331 | if (access & (~ex->access)) |
| 332 | continue; |
| 333 | return true; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | /** |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 339 | * match_exception_partial - iterates the exception list trying to find a partial match |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 340 | * @exceptions: list of exceptions |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 341 | * @type: device type (DEVCG_DEV_BLOCK or DEVCG_DEV_CHAR) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 342 | * @major: device file major number, ~0 to match all |
| 343 | * @minor: device file minor number, ~0 to match all |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 344 | * @access: permission mask (DEVCG_ACC_READ, DEVCG_ACC_WRITE, DEVCG_ACC_MKNOD) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 345 | * |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 346 | * It is considered a partial match if an exception's range is found to |
| 347 | * contain *any* of the devices specified by provided parameters. This is |
| 348 | * used to make sure no extra access is being granted that is forbidden by |
| 349 | * any of the exception list. |
| 350 | * |
| 351 | * Return: true in case the provided range mat matches an exception completely |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 352 | */ |
| 353 | static bool match_exception_partial(struct list_head *exceptions, short type, |
| 354 | u32 major, u32 minor, short access) |
| 355 | { |
| 356 | struct dev_exception_item *ex; |
| 357 | |
Amol Grover | bc62d68 | 2020-04-06 16:29:50 +0530 | [diff] [blame] | 358 | list_for_each_entry_rcu(ex, exceptions, list, |
| 359 | lockdep_is_held(&devcgroup_mutex)) { |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 360 | if ((type & DEVCG_DEV_BLOCK) && !(ex->type & DEVCG_DEV_BLOCK)) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 361 | continue; |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 362 | if ((type & DEVCG_DEV_CHAR) && !(ex->type & DEVCG_DEV_CHAR)) |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 363 | continue; |
| 364 | /* |
| 365 | * We must be sure that both the exception and the provided |
| 366 | * range aren't masking all devices |
| 367 | */ |
| 368 | if (ex->major != ~0 && major != ~0 && ex->major != major) |
| 369 | continue; |
| 370 | if (ex->minor != ~0 && minor != ~0 && ex->minor != minor) |
| 371 | continue; |
| 372 | /* |
| 373 | * In order to make sure the provided range isn't matching |
| 374 | * an exception, all its access bits shouldn't match the |
| 375 | * exception's access bits |
| 376 | */ |
| 377 | if (!(access & ex->access)) |
| 378 | continue; |
| 379 | return true; |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | /** |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 385 | * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 386 | * @dev_cgroup: dev cgroup to be tested against |
| 387 | * @refex: new exception |
| 388 | * @behavior: behavior of the exception's dev_cgroup |
Aristeu Rozanski | f5f3cf6f | 2014-04-24 15:33:21 -0400 | [diff] [blame] | 389 | * |
| 390 | * This is used to make sure a child cgroup won't have more privileges |
| 391 | * than its parent |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 392 | */ |
| 393 | static bool verify_new_ex(struct dev_cgroup *dev_cgroup, |
| 394 | struct dev_exception_item *refex, |
| 395 | enum devcg_behavior behavior) |
| 396 | { |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 397 | bool match = false; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 398 | |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 399 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && |
Paul E. McKenney | dc3a04d | 2015-09-02 17:11:22 -0700 | [diff] [blame] | 400 | !lockdep_is_held(&devcgroup_mutex), |
Paul E. McKenney | f78f5b9 | 2015-06-18 15:50:02 -0700 | [diff] [blame] | 401 | "device_cgroup:verify_new_ex called without proper synchronization"); |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 402 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 403 | if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
| 404 | if (behavior == DEVCG_DEFAULT_ALLOW) { |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 405 | /* |
| 406 | * new exception in the child doesn't matter, only |
| 407 | * adding extra restrictions |
| 408 | */ |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 409 | return true; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 410 | } else { |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 411 | /* |
| 412 | * new exception in the child will add more devices |
| 413 | * that can be acessed, so it can't match any of |
| 414 | * parent's exceptions, even slightly |
| 415 | */ |
| 416 | match = match_exception_partial(&dev_cgroup->exceptions, |
| 417 | refex->type, |
| 418 | refex->major, |
| 419 | refex->minor, |
| 420 | refex->access); |
| 421 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 422 | if (match) |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 423 | return false; |
| 424 | return true; |
| 425 | } |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 426 | } else { |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 427 | /* |
| 428 | * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore |
| 429 | * the new exception will add access to more devices and must |
| 430 | * be contained completely in an parent's exception to be |
| 431 | * allowed |
| 432 | */ |
| 433 | match = match_exception(&dev_cgroup->exceptions, refex->type, |
| 434 | refex->major, refex->minor, |
| 435 | refex->access); |
| 436 | |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 437 | if (match) |
| 438 | /* parent has an exception that matches the proposed */ |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 439 | return true; |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 440 | else |
| 441 | return false; |
Aristeu Rozanski | 26898fd | 2013-02-15 11:55:44 -0500 | [diff] [blame] | 442 | } |
| 443 | return false; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * parent_has_perm: |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 448 | * 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] | 449 | * must be allowed in the parent device |
| 450 | */ |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 451 | static int parent_has_perm(struct dev_cgroup *childcg, |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 452 | struct dev_exception_item *ex) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 453 | { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 454 | struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 455 | |
Tejun Heo | 6387698 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 456 | if (!parent) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 457 | return 1; |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 458 | return verify_new_ex(parent, ex, childcg->behavior); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 461 | /** |
Aristeu Rozanski | d2c2b11 | 2014-05-05 11:18:59 -0400 | [diff] [blame] | 462 | * parent_allows_removal - verify if it's ok to remove an exception |
| 463 | * @childcg: child cgroup from where the exception will be removed |
| 464 | * @ex: exception being removed |
| 465 | * |
| 466 | * When removing an exception in cgroups with default ALLOW policy, it must |
| 467 | * be checked if removing it will give the child cgroup more access than the |
| 468 | * parent. |
| 469 | * |
| 470 | * Return: true if it's ok to remove exception, false otherwise |
| 471 | */ |
| 472 | static bool parent_allows_removal(struct dev_cgroup *childcg, |
| 473 | struct dev_exception_item *ex) |
| 474 | { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 475 | struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent); |
Aristeu Rozanski | d2c2b11 | 2014-05-05 11:18:59 -0400 | [diff] [blame] | 476 | |
| 477 | if (!parent) |
| 478 | return true; |
| 479 | |
| 480 | /* It's always allowed to remove access to devices */ |
| 481 | if (childcg->behavior == DEVCG_DEFAULT_DENY) |
| 482 | return true; |
| 483 | |
| 484 | /* |
| 485 | * Make sure you're not removing part or a whole exception existing in |
| 486 | * the parent cgroup |
| 487 | */ |
| 488 | return !match_exception_partial(&parent->exceptions, ex->type, |
| 489 | ex->major, ex->minor, ex->access); |
| 490 | } |
| 491 | |
| 492 | /** |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 493 | * may_allow_all - checks if it's possible to change the behavior to |
| 494 | * allow based on parent's rules. |
| 495 | * @parent: device cgroup's parent |
| 496 | * returns: != 0 in case it's allowed, 0 otherwise |
| 497 | */ |
| 498 | static inline int may_allow_all(struct dev_cgroup *parent) |
| 499 | { |
Aristeu Rozanski | 64e1047 | 2012-11-06 07:25:04 -0800 | [diff] [blame] | 500 | if (!parent) |
| 501 | return 1; |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 502 | return parent->behavior == DEVCG_DEFAULT_ALLOW; |
| 503 | } |
| 504 | |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 505 | /** |
| 506 | * revalidate_active_exceptions - walks through the active exception list and |
| 507 | * revalidates the exceptions based on parent's |
| 508 | * behavior and exceptions. The exceptions that |
| 509 | * are no longer valid will be removed. |
| 510 | * Called with devcgroup_mutex held. |
| 511 | * @devcg: cgroup which exceptions will be checked |
| 512 | * |
| 513 | * This is one of the three key functions for hierarchy implementation. |
| 514 | * This function is responsible for re-evaluating all the cgroup's active |
| 515 | * exceptions due to a parent's exception change. |
Mauro Carvalho Chehab | da82c92 | 2019-06-27 13:08:35 -0300 | [diff] [blame] | 516 | * Refer to Documentation/admin-guide/cgroup-v1/devices.rst for more details. |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 517 | */ |
| 518 | static void revalidate_active_exceptions(struct dev_cgroup *devcg) |
| 519 | { |
| 520 | struct dev_exception_item *ex; |
| 521 | struct list_head *this, *tmp; |
| 522 | |
| 523 | list_for_each_safe(this, tmp, &devcg->exceptions) { |
| 524 | ex = container_of(this, struct dev_exception_item, list); |
| 525 | if (!parent_has_perm(devcg, ex)) |
| 526 | dev_exception_rm(devcg, ex); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 531 | * propagate_exception - propagates a new exception to the children |
| 532 | * @devcg_root: device cgroup that added a new exception |
| 533 | * @ex: new exception to be propagated |
| 534 | * |
| 535 | * returns: 0 in case of success, != 0 in case of error |
| 536 | */ |
| 537 | static int propagate_exception(struct dev_cgroup *devcg_root, |
| 538 | struct dev_exception_item *ex) |
| 539 | { |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 540 | struct cgroup_subsys_state *pos; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 541 | int rc = 0; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 542 | |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 543 | rcu_read_lock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 544 | |
Tejun Heo | 492eb21 | 2013-08-08 20:11:25 -0400 | [diff] [blame] | 545 | css_for_each_descendant_pre(pos, &devcg_root->css) { |
| 546 | struct dev_cgroup *devcg = css_to_devcgroup(pos); |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * Because devcgroup_mutex is held, no devcg will become |
| 550 | * online or offline during the tree walk (see on/offline |
| 551 | * methods), and online ones are safe to access outside RCU |
| 552 | * read lock without bumping refcnt. |
| 553 | */ |
Tejun Heo | bd8815a | 2013-08-08 20:11:27 -0400 | [diff] [blame] | 554 | if (pos == &devcg_root->css || !is_devcg_online(devcg)) |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 555 | continue; |
| 556 | |
| 557 | rcu_read_unlock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | * in case both root's behavior and devcg is allow, a new |
| 561 | * restriction means adding to the exception list |
| 562 | */ |
| 563 | if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW && |
| 564 | devcg->behavior == DEVCG_DEFAULT_ALLOW) { |
| 565 | rc = dev_exception_add(devcg, ex); |
| 566 | if (rc) |
Jann Horn | 0fcc4c8 | 2019-03-19 02:36:59 +0100 | [diff] [blame] | 567 | return rc; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 568 | } else { |
| 569 | /* |
| 570 | * in the other possible cases: |
| 571 | * root's behavior: allow, devcg's: deny |
| 572 | * root's behavior: deny, devcg's: deny |
| 573 | * the exception will be removed |
| 574 | */ |
| 575 | dev_exception_rm(devcg, ex); |
| 576 | } |
| 577 | revalidate_active_exceptions(devcg); |
| 578 | |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 579 | rcu_read_lock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 580 | } |
Tejun Heo | d591fb5 | 2013-05-24 10:55:38 +0900 | [diff] [blame] | 581 | |
| 582 | rcu_read_unlock(); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 583 | return rc; |
| 584 | } |
| 585 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 586 | /* |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 587 | * Modify the exception list using allow/deny rules. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 588 | * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD |
| 589 | * 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] | 590 | * modify the exception list. |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 591 | * It seems likely we'll want to add a CAP_CONTAINER capability to allow |
| 592 | * 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] | 593 | * 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] | 594 | * |
| 595 | * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting |
| 596 | * new access is only allowed if you're in the top-level cgroup, or your |
| 597 | * parent cgroup has the access you're asking for. |
| 598 | */ |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 599 | static int devcgroup_update_access(struct dev_cgroup *devcgroup, |
Tejun Heo | 4d3bb51 | 2014-03-19 10:23:54 -0400 | [diff] [blame] | 600 | int filetype, char *buffer) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 601 | { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 602 | const char *b; |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 603 | char temp[12]; /* 11 + 1 characters needed for a u32 */ |
Aristeu Rozanski | c39a2a3 | 2013-02-15 11:55:45 -0500 | [diff] [blame] | 604 | int count, rc = 0; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 605 | struct dev_exception_item ex; |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 606 | struct dev_cgroup *parent = css_to_devcgroup(devcgroup->css.parent); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 607 | |
| 608 | if (!capable(CAP_SYS_ADMIN)) |
| 609 | return -EPERM; |
| 610 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 611 | memset(&ex, 0, sizeof(ex)); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 612 | b = buffer; |
| 613 | |
| 614 | switch (*b) { |
| 615 | case 'a': |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 616 | switch (filetype) { |
| 617 | case DEVCG_ALLOW: |
Tejun Heo | 7a3bb24 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 618 | if (css_has_online_children(&devcgroup->css)) |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 619 | return -EINVAL; |
| 620 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 621 | if (!may_allow_all(parent)) |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 622 | return -EPERM; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 623 | dev_exception_clean(devcgroup); |
Aristeu Rozanski | 64e1047 | 2012-11-06 07:25:04 -0800 | [diff] [blame] | 624 | devcgroup->behavior = DEVCG_DEFAULT_ALLOW; |
| 625 | if (!parent) |
| 626 | break; |
| 627 | |
Aristeu Rozanski | 4cef729 | 2012-10-25 13:37:45 -0700 | [diff] [blame] | 628 | rc = dev_exceptions_copy(&devcgroup->exceptions, |
| 629 | &parent->exceptions); |
| 630 | if (rc) |
| 631 | return rc; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 632 | break; |
| 633 | case DEVCG_DENY: |
Tejun Heo | 7a3bb24 | 2014-05-16 13:22:52 -0400 | [diff] [blame] | 634 | if (css_has_online_children(&devcgroup->css)) |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 635 | return -EINVAL; |
| 636 | |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 637 | dev_exception_clean(devcgroup); |
Aristeu Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 638 | devcgroup->behavior = DEVCG_DEFAULT_DENY; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 639 | break; |
| 640 | default: |
| 641 | return -EINVAL; |
| 642 | } |
| 643 | return 0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 644 | case 'b': |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 645 | ex.type = DEVCG_DEV_BLOCK; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 646 | break; |
| 647 | case 'c': |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 648 | ex.type = DEVCG_DEV_CHAR; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 649 | break; |
| 650 | default: |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 651 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 652 | } |
| 653 | b++; |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 654 | if (!isspace(*b)) |
| 655 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 656 | b++; |
| 657 | if (*b == '*') { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 658 | ex.major = ~0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 659 | b++; |
| 660 | } else if (isdigit(*b)) { |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 661 | memset(temp, 0, sizeof(temp)); |
| 662 | for (count = 0; count < sizeof(temp) - 1; count++) { |
| 663 | temp[count] = *b; |
| 664 | b++; |
| 665 | if (!isdigit(*b)) |
| 666 | break; |
| 667 | } |
| 668 | rc = kstrtou32(temp, 10, &ex.major); |
| 669 | if (rc) |
| 670 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 671 | } else { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 672 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 673 | } |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 674 | if (*b != ':') |
| 675 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 676 | b++; |
| 677 | |
| 678 | /* read minor */ |
| 679 | if (*b == '*') { |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 680 | ex.minor = ~0; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 681 | b++; |
| 682 | } else if (isdigit(*b)) { |
Aristeu Rozanski | 26fd840 | 2012-10-25 13:37:41 -0700 | [diff] [blame] | 683 | memset(temp, 0, sizeof(temp)); |
| 684 | for (count = 0; count < sizeof(temp) - 1; count++) { |
| 685 | temp[count] = *b; |
| 686 | b++; |
| 687 | if (!isdigit(*b)) |
| 688 | break; |
| 689 | } |
| 690 | rc = kstrtou32(temp, 10, &ex.minor); |
| 691 | if (rc) |
| 692 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 693 | } else { |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 694 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 695 | } |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 696 | if (!isspace(*b)) |
| 697 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 698 | for (b++, count = 0; count < 3; count++, b++) { |
| 699 | switch (*b) { |
| 700 | case 'r': |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 701 | ex.access |= DEVCG_ACC_READ; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 702 | break; |
| 703 | case 'w': |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 704 | ex.access |= DEVCG_ACC_WRITE; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 705 | break; |
| 706 | case 'm': |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 707 | ex.access |= DEVCG_ACC_MKNOD; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 708 | break; |
| 709 | case '\n': |
| 710 | case '\0': |
| 711 | count = 3; |
| 712 | break; |
| 713 | default: |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 714 | return -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 718 | switch (filetype) { |
| 719 | case DEVCG_ALLOW: |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 720 | /* |
| 721 | * If the default policy is to allow by default, try to remove |
| 722 | * an matching exception instead. And be silent about it: we |
| 723 | * don't want to break compatibility |
| 724 | */ |
Aristeu Rozanski | 5b7aa7d | 2012-10-25 13:37:38 -0700 | [diff] [blame] | 725 | if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) { |
Aristeu Rozanski | d2c2b11 | 2014-05-05 11:18:59 -0400 | [diff] [blame] | 726 | /* Check if the parent allows removing it first */ |
| 727 | if (!parent_allows_removal(devcgroup, &ex)) |
| 728 | return -EPERM; |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 729 | dev_exception_rm(devcgroup, &ex); |
Aristeu Rozanski | d2c2b11 | 2014-05-05 11:18:59 -0400 | [diff] [blame] | 730 | break; |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 731 | } |
Aristeu Rozanski | d2c2b11 | 2014-05-05 11:18:59 -0400 | [diff] [blame] | 732 | |
| 733 | if (!parent_has_perm(devcgroup, &ex)) |
| 734 | return -EPERM; |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 735 | rc = dev_exception_add(devcgroup, &ex); |
| 736 | break; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 737 | case DEVCG_DENY: |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 738 | /* |
| 739 | * If the default policy is to deny by default, try to remove |
| 740 | * an matching exception instead. And be silent about it: we |
| 741 | * don't want to break compatibility |
| 742 | */ |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 743 | if (devcgroup->behavior == DEVCG_DEFAULT_DENY) |
Aristeu Rozanski | db9aeca | 2012-10-04 17:15:20 -0700 | [diff] [blame] | 744 | dev_exception_rm(devcgroup, &ex); |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 745 | else |
| 746 | rc = dev_exception_add(devcgroup, &ex); |
| 747 | |
| 748 | if (rc) |
| 749 | break; |
| 750 | /* we only propagate new restrictions */ |
| 751 | rc = propagate_exception(devcgroup, &ex); |
| 752 | break; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 753 | default: |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 754 | rc = -EINVAL; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 755 | } |
Aristeu Rozanski | bd2953e | 2013-02-15 11:55:47 -0500 | [diff] [blame] | 756 | return rc; |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 757 | } |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 758 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 759 | static ssize_t devcgroup_access_write(struct kernfs_open_file *of, |
| 760 | char *buf, size_t nbytes, loff_t off) |
Paul Menage | f92523e | 2008-07-25 01:47:03 -0700 | [diff] [blame] | 761 | { |
| 762 | int retval; |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 763 | |
| 764 | mutex_lock(&devcgroup_mutex); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 765 | retval = devcgroup_update_access(css_to_devcgroup(of_css(of)), |
| 766 | of_cft(of)->private, strstrip(buf)); |
Li Zefan | b4046f0 | 2009-04-02 16:57:32 -0700 | [diff] [blame] | 767 | mutex_unlock(&devcgroup_mutex); |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 768 | return retval ?: nbytes; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static struct cftype dev_cgroup_files[] = { |
| 772 | { |
| 773 | .name = "allow", |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 774 | .write = devcgroup_access_write, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 775 | .private = DEVCG_ALLOW, |
| 776 | }, |
| 777 | { |
| 778 | .name = "deny", |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 779 | .write = devcgroup_access_write, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 780 | .private = DEVCG_DENY, |
| 781 | }, |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 782 | { |
| 783 | .name = "list", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 784 | .seq_show = devcgroup_seq_show, |
Serge E. Hallyn | 29486df | 2008-04-29 01:00:14 -0700 | [diff] [blame] | 785 | .private = DEVCG_LIST, |
| 786 | }, |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 787 | { } /* terminate */ |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 788 | }; |
| 789 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 790 | struct cgroup_subsys devices_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 791 | .css_alloc = devcgroup_css_alloc, |
| 792 | .css_free = devcgroup_css_free, |
Aristeu Rozanski | 1909554c | 2013-02-15 11:55:46 -0500 | [diff] [blame] | 793 | .css_online = devcgroup_online, |
| 794 | .css_offline = devcgroup_offline, |
Tejun Heo | 5577964 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 795 | .legacy_cftypes = dev_cgroup_files, |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 796 | }; |
| 797 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 798 | /** |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 799 | * devcgroup_legacy_check_permission - checks if an inode operation is permitted |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 800 | * @dev_cgroup: the dev cgroup to be tested against |
| 801 | * @type: device type |
| 802 | * @major: device major number |
| 803 | * @minor: device minor number |
Roman Gushchin | 67e306f | 2017-11-05 08:15:30 -0500 | [diff] [blame] | 804 | * @access: combination of DEVCG_ACC_WRITE, DEVCG_ACC_READ and DEVCG_ACC_MKNOD |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 805 | * |
| 806 | * returns 0 on success, -EPERM case the operation is not permitted |
| 807 | */ |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 808 | static int devcgroup_legacy_check_permission(short type, u32 major, u32 minor, |
Harish Kasiviswanathan | 4b7d4d4 | 2019-05-16 11:37:16 -0400 | [diff] [blame] | 809 | short access) |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 810 | { |
Jiri Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 811 | struct dev_cgroup *dev_cgroup; |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 812 | bool rc; |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 813 | |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 814 | rcu_read_lock(); |
Jiri Slaby | 8c9506d | 2012-10-25 13:37:34 -0700 | [diff] [blame] | 815 | dev_cgroup = task_devcgroup(current); |
Aristeu Rozanski | 79d7197 | 2014-04-21 12:13:03 -0400 | [diff] [blame] | 816 | if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) |
| 817 | /* Can't match any of the exceptions, even partially */ |
| 818 | rc = !match_exception_partial(&dev_cgroup->exceptions, |
| 819 | type, major, minor, access); |
| 820 | else |
| 821 | /* Need to match completely one exception to be allowed */ |
| 822 | rc = match_exception(&dev_cgroup->exceptions, type, major, |
| 823 | minor, access); |
Pavel Emelyanov | 4efd1a1 | 2008-07-25 01:47:07 -0700 | [diff] [blame] | 824 | rcu_read_unlock(); |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 825 | |
Aristeu Rozanski | ad67607 | 2012-10-04 17:15:17 -0700 | [diff] [blame] | 826 | if (!rc) |
| 827 | return -EPERM; |
| 828 | |
| 829 | return 0; |
| 830 | } |
Harish Kasiviswanathan | 4b7d4d4 | 2019-05-16 11:37:16 -0400 | [diff] [blame] | 831 | |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 832 | #endif /* CONFIG_CGROUP_DEVICE */ |
| 833 | |
| 834 | #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) |
| 835 | |
Harish Kasiviswanathan | 4b7d4d4 | 2019-05-16 11:37:16 -0400 | [diff] [blame] | 836 | int devcgroup_check_permission(short type, u32 major, u32 minor, short access) |
| 837 | { |
| 838 | int rc = BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access); |
| 839 | |
| 840 | if (rc) |
| 841 | return -EPERM; |
| 842 | |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 843 | #ifdef CONFIG_CGROUP_DEVICE |
| 844 | return devcgroup_legacy_check_permission(type, major, minor, access); |
| 845 | |
| 846 | #else /* CONFIG_CGROUP_DEVICE */ |
| 847 | return 0; |
| 848 | |
| 849 | #endif /* CONFIG_CGROUP_DEVICE */ |
Harish Kasiviswanathan | 4b7d4d4 | 2019-05-16 11:37:16 -0400 | [diff] [blame] | 850 | } |
| 851 | EXPORT_SYMBOL(devcgroup_check_permission); |
Odin Ugedal | eec8fd0 | 2020-04-03 19:55:28 +0200 | [diff] [blame] | 852 | #endif /* defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) */ |