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