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