device_cgroup.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * device_cgroup.c - device cgroup subsystem
  3. *
  4. * Copyright 2007 IBM Corp
  5. */
  6. #include <linux/device_cgroup.h>
  7. #include <linux/cgroup.h>
  8. #include <linux/ctype.h>
  9. #include <linux/list.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/mutex.h>
  15. #define ACC_MKNOD 1
  16. #define ACC_READ 2
  17. #define ACC_WRITE 4
  18. #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
  19. #define DEV_BLOCK 1
  20. #define DEV_CHAR 2
  21. #define DEV_ALL 4 /* this represents all devices */
  22. static DEFINE_MUTEX(devcgroup_mutex);
  23. enum devcg_behavior {
  24. DEVCG_DEFAULT_NONE,
  25. DEVCG_DEFAULT_ALLOW,
  26. DEVCG_DEFAULT_DENY,
  27. };
  28. /*
  29. * exception list locking rules:
  30. * hold devcgroup_mutex for update/read.
  31. * hold rcu_read_lock() for read.
  32. */
  33. struct dev_exception_item {
  34. u32 major, minor;
  35. short type;
  36. short access;
  37. struct list_head list;
  38. struct rcu_head rcu;
  39. };
  40. struct dev_cgroup {
  41. struct cgroup_subsys_state css;
  42. struct list_head exceptions;
  43. enum devcg_behavior behavior;
  44. /* temporary list for pending propagation operations */
  45. struct list_head propagate_pending;
  46. };
  47. static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
  48. {
  49. return container_of(s, struct dev_cgroup, css);
  50. }
  51. static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
  52. {
  53. return css_to_devcgroup(cgroup_subsys_state(cgroup, devices_subsys_id));
  54. }
  55. static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
  56. {
  57. return css_to_devcgroup(task_subsys_state(task, devices_subsys_id));
  58. }
  59. struct cgroup_subsys devices_subsys;
  60. static int devcgroup_can_attach(struct cgroup *new_cgrp,
  61. struct cgroup_taskset *set)
  62. {
  63. struct task_struct *task = cgroup_taskset_first(set);
  64. if (current != task && !capable(CAP_SYS_ADMIN))
  65. return -EPERM;
  66. return 0;
  67. }
  68. /*
  69. * called under devcgroup_mutex
  70. */
  71. static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
  72. {
  73. struct dev_exception_item *ex, *tmp, *new;
  74. lockdep_assert_held(&devcgroup_mutex);
  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. return 0;
  82. free_and_exit:
  83. list_for_each_entry_safe(ex, tmp, dest, list) {
  84. list_del(&ex->list);
  85. kfree(ex);
  86. }
  87. return -ENOMEM;
  88. }
  89. /*
  90. * called under devcgroup_mutex
  91. */
  92. static int dev_exception_add(struct dev_cgroup *dev_cgroup,
  93. struct dev_exception_item *ex)
  94. {
  95. struct dev_exception_item *excopy, *walk;
  96. lockdep_assert_held(&devcgroup_mutex);
  97. excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
  98. if (!excopy)
  99. return -ENOMEM;
  100. list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
  101. if (walk->type != ex->type)
  102. continue;
  103. if (walk->major != ex->major)
  104. continue;
  105. if (walk->minor != ex->minor)
  106. continue;
  107. walk->access |= ex->access;
  108. kfree(excopy);
  109. excopy = NULL;
  110. }
  111. if (excopy != NULL)
  112. list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
  113. return 0;
  114. }
  115. /*
  116. * called under devcgroup_mutex
  117. */
  118. static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
  119. struct dev_exception_item *ex)
  120. {
  121. struct dev_exception_item *walk, *tmp;
  122. lockdep_assert_held(&devcgroup_mutex);
  123. list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
  124. if (walk->type != ex->type)
  125. continue;
  126. if (walk->major != ex->major)
  127. continue;
  128. if (walk->minor != ex->minor)
  129. continue;
  130. walk->access &= ~ex->access;
  131. if (!walk->access) {
  132. list_del_rcu(&walk->list);
  133. kfree_rcu(walk, rcu);
  134. }
  135. }
  136. }
  137. static void __dev_exception_clean(struct dev_cgroup *dev_cgroup)
  138. {
  139. struct dev_exception_item *ex, *tmp;
  140. list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
  141. list_del_rcu(&ex->list);
  142. kfree_rcu(ex, rcu);
  143. }
  144. }
  145. /**
  146. * dev_exception_clean - frees all entries of the exception list
  147. * @dev_cgroup: dev_cgroup with the exception list to be cleaned
  148. *
  149. * called under devcgroup_mutex
  150. */
  151. static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
  152. {
  153. lockdep_assert_held(&devcgroup_mutex);
  154. __dev_exception_clean(dev_cgroup);
  155. }
  156. static inline bool is_devcg_online(const struct dev_cgroup *devcg)
  157. {
  158. return (devcg->behavior != DEVCG_DEFAULT_NONE);
  159. }
  160. /**
  161. * devcgroup_online - initializes devcgroup's behavior and exceptions based on
  162. * parent's
  163. * @cgroup: cgroup getting online
  164. * returns 0 in case of success, error code otherwise
  165. */
  166. static int devcgroup_online(struct cgroup *cgroup)
  167. {
  168. struct dev_cgroup *dev_cgroup, *parent_dev_cgroup = NULL;
  169. int ret = 0;
  170. mutex_lock(&devcgroup_mutex);
  171. dev_cgroup = cgroup_to_devcgroup(cgroup);
  172. if (cgroup->parent)
  173. parent_dev_cgroup = cgroup_to_devcgroup(cgroup->parent);
  174. if (parent_dev_cgroup == NULL)
  175. dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
  176. else {
  177. ret = dev_exceptions_copy(&dev_cgroup->exceptions,
  178. &parent_dev_cgroup->exceptions);
  179. if (!ret)
  180. dev_cgroup->behavior = parent_dev_cgroup->behavior;
  181. }
  182. mutex_unlock(&devcgroup_mutex);
  183. return ret;
  184. }
  185. static void devcgroup_offline(struct cgroup *cgroup)
  186. {
  187. struct dev_cgroup *dev_cgroup = cgroup_to_devcgroup(cgroup);
  188. mutex_lock(&devcgroup_mutex);
  189. dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
  190. mutex_unlock(&devcgroup_mutex);
  191. }
  192. /*
  193. * called from kernel/cgroup.c with cgroup_lock() held.
  194. */
  195. static struct cgroup_subsys_state *devcgroup_css_alloc(struct cgroup *cgroup)
  196. {
  197. struct dev_cgroup *dev_cgroup;
  198. struct cgroup *parent_cgroup;
  199. dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
  200. if (!dev_cgroup)
  201. return ERR_PTR(-ENOMEM);
  202. INIT_LIST_HEAD(&dev_cgroup->exceptions);
  203. INIT_LIST_HEAD(&dev_cgroup->propagate_pending);
  204. dev_cgroup->behavior = DEVCG_DEFAULT_NONE;
  205. parent_cgroup = cgroup->parent;
  206. return &dev_cgroup->css;
  207. }
  208. static void devcgroup_css_free(struct cgroup *cgroup)
  209. {
  210. struct dev_cgroup *dev_cgroup;
  211. dev_cgroup = cgroup_to_devcgroup(cgroup);
  212. __dev_exception_clean(dev_cgroup);
  213. kfree(dev_cgroup);
  214. }
  215. #define DEVCG_ALLOW 1
  216. #define DEVCG_DENY 2
  217. #define DEVCG_LIST 3
  218. #define MAJMINLEN 13
  219. #define ACCLEN 4
  220. static void set_access(char *acc, short access)
  221. {
  222. int idx = 0;
  223. memset(acc, 0, ACCLEN);
  224. if (access & ACC_READ)
  225. acc[idx++] = 'r';
  226. if (access & ACC_WRITE)
  227. acc[idx++] = 'w';
  228. if (access & ACC_MKNOD)
  229. acc[idx++] = 'm';
  230. }
  231. static char type_to_char(short type)
  232. {
  233. if (type == DEV_ALL)
  234. return 'a';
  235. if (type == DEV_CHAR)
  236. return 'c';
  237. if (type == DEV_BLOCK)
  238. return 'b';
  239. return 'X';
  240. }
  241. static void set_majmin(char *str, unsigned m)
  242. {
  243. if (m == ~0)
  244. strcpy(str, "*");
  245. else
  246. sprintf(str, "%u", m);
  247. }
  248. static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
  249. struct seq_file *m)
  250. {
  251. struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
  252. struct dev_exception_item *ex;
  253. char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
  254. rcu_read_lock();
  255. /*
  256. * To preserve the compatibility:
  257. * - Only show the "all devices" when the default policy is to allow
  258. * - List the exceptions in case the default policy is to deny
  259. * This way, the file remains as a "whitelist of devices"
  260. */
  261. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  262. set_access(acc, ACC_MASK);
  263. set_majmin(maj, ~0);
  264. set_majmin(min, ~0);
  265. seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
  266. maj, min, acc);
  267. } else {
  268. list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
  269. set_access(acc, ex->access);
  270. set_majmin(maj, ex->major);
  271. set_majmin(min, ex->minor);
  272. seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
  273. maj, min, acc);
  274. }
  275. }
  276. rcu_read_unlock();
  277. return 0;
  278. }
  279. /**
  280. * may_access - verifies if a new exception is part of what is allowed
  281. * by a dev cgroup based on the default policy +
  282. * exceptions. This is used to make sure a child cgroup
  283. * won't have more privileges than its parent or to
  284. * verify if a certain access is allowed.
  285. * @dev_cgroup: dev cgroup to be tested against
  286. * @refex: new exception
  287. * @behavior: behavior of the exception
  288. */
  289. static bool may_access(struct dev_cgroup *dev_cgroup,
  290. struct dev_exception_item *refex,
  291. enum devcg_behavior behavior)
  292. {
  293. struct dev_exception_item *ex;
  294. bool match = false;
  295. rcu_lockdep_assert(rcu_read_lock_held() ||
  296. lockdep_is_held(&devcgroup_mutex),
  297. "device_cgroup::may_access() called without proper synchronization");
  298. list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
  299. if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  300. continue;
  301. if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  302. continue;
  303. if (ex->major != ~0 && ex->major != refex->major)
  304. continue;
  305. if (ex->minor != ~0 && ex->minor != refex->minor)
  306. continue;
  307. if (refex->access & (~ex->access))
  308. continue;
  309. match = true;
  310. break;
  311. }
  312. if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  313. if (behavior == DEVCG_DEFAULT_ALLOW) {
  314. /* the exception will deny access to certain devices */
  315. return true;
  316. } else {
  317. /* the exception will allow access to certain devices */
  318. if (match)
  319. /*
  320. * a new exception allowing access shouldn't
  321. * match an parent's exception
  322. */
  323. return false;
  324. return true;
  325. }
  326. } else {
  327. /* only behavior == DEVCG_DEFAULT_DENY allowed here */
  328. if (match)
  329. /* parent has an exception that matches the proposed */
  330. return true;
  331. else
  332. return false;
  333. }
  334. return false;
  335. }
  336. /*
  337. * parent_has_perm:
  338. * when adding a new allow rule to a device exception list, the rule
  339. * must be allowed in the parent device
  340. */
  341. static int parent_has_perm(struct dev_cgroup *childcg,
  342. struct dev_exception_item *ex)
  343. {
  344. struct cgroup *pcg = childcg->css.cgroup->parent;
  345. struct dev_cgroup *parent;
  346. if (!pcg)
  347. return 1;
  348. parent = cgroup_to_devcgroup(pcg);
  349. return may_access(parent, ex, childcg->behavior);
  350. }
  351. /**
  352. * may_allow_all - checks if it's possible to change the behavior to
  353. * allow based on parent's rules.
  354. * @parent: device cgroup's parent
  355. * returns: != 0 in case it's allowed, 0 otherwise
  356. */
  357. static inline int may_allow_all(struct dev_cgroup *parent)
  358. {
  359. if (!parent)
  360. return 1;
  361. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  362. }
  363. /**
  364. * revalidate_active_exceptions - walks through the active exception list and
  365. * revalidates the exceptions based on parent's
  366. * behavior and exceptions. The exceptions that
  367. * are no longer valid will be removed.
  368. * Called with devcgroup_mutex held.
  369. * @devcg: cgroup which exceptions will be checked
  370. *
  371. * This is one of the three key functions for hierarchy implementation.
  372. * This function is responsible for re-evaluating all the cgroup's active
  373. * exceptions due to a parent's exception change.
  374. * Refer to Documentation/cgroups/devices.txt for more details.
  375. */
  376. static void revalidate_active_exceptions(struct dev_cgroup *devcg)
  377. {
  378. struct dev_exception_item *ex;
  379. struct list_head *this, *tmp;
  380. list_for_each_safe(this, tmp, &devcg->exceptions) {
  381. ex = container_of(this, struct dev_exception_item, list);
  382. if (!parent_has_perm(devcg, ex))
  383. dev_exception_rm(devcg, ex);
  384. }
  385. }
  386. /**
  387. * get_online_devcg - walks the cgroup tree and fills a list with the online
  388. * groups
  389. * @root: cgroup used as starting point
  390. * @online: list that will be filled with online groups
  391. *
  392. * Must be called with devcgroup_mutex held. Grabs RCU lock.
  393. * Because devcgroup_mutex is held, no devcg will become online or offline
  394. * during the tree walk (see devcgroup_online, devcgroup_offline)
  395. * A separated list is needed because propagate_behavior() and
  396. * propagate_exception() need to allocate memory and can block.
  397. */
  398. static void get_online_devcg(struct cgroup *root, struct list_head *online)
  399. {
  400. struct cgroup *pos;
  401. struct dev_cgroup *devcg;
  402. lockdep_assert_held(&devcgroup_mutex);
  403. rcu_read_lock();
  404. cgroup_for_each_descendant_pre(pos, root) {
  405. devcg = cgroup_to_devcgroup(pos);
  406. if (is_devcg_online(devcg))
  407. list_add_tail(&devcg->propagate_pending, online);
  408. }
  409. rcu_read_unlock();
  410. }
  411. /**
  412. * propagate_exception - propagates a new exception to the children
  413. * @devcg_root: device cgroup that added a new exception
  414. * @ex: new exception to be propagated
  415. *
  416. * returns: 0 in case of success, != 0 in case of error
  417. */
  418. static int propagate_exception(struct dev_cgroup *devcg_root,
  419. struct dev_exception_item *ex)
  420. {
  421. struct cgroup *root = devcg_root->css.cgroup;
  422. struct dev_cgroup *devcg, *parent, *tmp;
  423. int rc = 0;
  424. LIST_HEAD(pending);
  425. get_online_devcg(root, &pending);
  426. list_for_each_entry_safe(devcg, tmp, &pending, propagate_pending) {
  427. parent = cgroup_to_devcgroup(devcg->css.cgroup->parent);
  428. /*
  429. * in case both root's behavior and devcg is allow, a new
  430. * restriction means adding to the exception list
  431. */
  432. if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
  433. devcg->behavior == DEVCG_DEFAULT_ALLOW) {
  434. rc = dev_exception_add(devcg, ex);
  435. if (rc)
  436. break;
  437. } else {
  438. /*
  439. * in the other possible cases:
  440. * root's behavior: allow, devcg's: deny
  441. * root's behavior: deny, devcg's: deny
  442. * the exception will be removed
  443. */
  444. dev_exception_rm(devcg, ex);
  445. }
  446. revalidate_active_exceptions(devcg);
  447. list_del_init(&devcg->propagate_pending);
  448. }
  449. return rc;
  450. }
  451. static inline bool has_children(struct dev_cgroup *devcgroup)
  452. {
  453. struct cgroup *cgrp = devcgroup->css.cgroup;
  454. return !list_empty(&cgrp->children);
  455. }
  456. /*
  457. * Modify the exception list using allow/deny rules.
  458. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  459. * so we can give a container CAP_MKNOD to let it create devices but not
  460. * modify the exception list.
  461. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  462. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  463. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  464. *
  465. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  466. * new access is only allowed if you're in the top-level cgroup, or your
  467. * parent cgroup has the access you're asking for.
  468. */
  469. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  470. int filetype, const char *buffer)
  471. {
  472. const char *b;
  473. char temp[12]; /* 11 + 1 characters needed for a u32 */
  474. int count, rc = 0;
  475. struct dev_exception_item ex;
  476. struct cgroup *p = devcgroup->css.cgroup;
  477. struct dev_cgroup *parent = NULL;
  478. if (!capable(CAP_SYS_ADMIN))
  479. return -EPERM;
  480. if (p->parent)
  481. parent = cgroup_to_devcgroup(p->parent);
  482. memset(&ex, 0, sizeof(ex));
  483. b = buffer;
  484. switch (*b) {
  485. case 'a':
  486. switch (filetype) {
  487. case DEVCG_ALLOW:
  488. if (has_children(devcgroup))
  489. return -EINVAL;
  490. if (!may_allow_all(parent))
  491. return -EPERM;
  492. dev_exception_clean(devcgroup);
  493. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  494. if (!parent)
  495. break;
  496. rc = dev_exceptions_copy(&devcgroup->exceptions,
  497. &parent->exceptions);
  498. if (rc)
  499. return rc;
  500. break;
  501. case DEVCG_DENY:
  502. if (has_children(devcgroup))
  503. return -EINVAL;
  504. dev_exception_clean(devcgroup);
  505. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. return 0;
  511. case 'b':
  512. ex.type = DEV_BLOCK;
  513. break;
  514. case 'c':
  515. ex.type = DEV_CHAR;
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. b++;
  521. if (!isspace(*b))
  522. return -EINVAL;
  523. b++;
  524. if (*b == '*') {
  525. ex.major = ~0;
  526. b++;
  527. } else if (isdigit(*b)) {
  528. memset(temp, 0, sizeof(temp));
  529. for (count = 0; count < sizeof(temp) - 1; count++) {
  530. temp[count] = *b;
  531. b++;
  532. if (!isdigit(*b))
  533. break;
  534. }
  535. rc = kstrtou32(temp, 10, &ex.major);
  536. if (rc)
  537. return -EINVAL;
  538. } else {
  539. return -EINVAL;
  540. }
  541. if (*b != ':')
  542. return -EINVAL;
  543. b++;
  544. /* read minor */
  545. if (*b == '*') {
  546. ex.minor = ~0;
  547. b++;
  548. } else if (isdigit(*b)) {
  549. memset(temp, 0, sizeof(temp));
  550. for (count = 0; count < sizeof(temp) - 1; count++) {
  551. temp[count] = *b;
  552. b++;
  553. if (!isdigit(*b))
  554. break;
  555. }
  556. rc = kstrtou32(temp, 10, &ex.minor);
  557. if (rc)
  558. return -EINVAL;
  559. } else {
  560. return -EINVAL;
  561. }
  562. if (!isspace(*b))
  563. return -EINVAL;
  564. for (b++, count = 0; count < 3; count++, b++) {
  565. switch (*b) {
  566. case 'r':
  567. ex.access |= ACC_READ;
  568. break;
  569. case 'w':
  570. ex.access |= ACC_WRITE;
  571. break;
  572. case 'm':
  573. ex.access |= ACC_MKNOD;
  574. break;
  575. case '\n':
  576. case '\0':
  577. count = 3;
  578. break;
  579. default:
  580. return -EINVAL;
  581. }
  582. }
  583. switch (filetype) {
  584. case DEVCG_ALLOW:
  585. if (!parent_has_perm(devcgroup, &ex))
  586. return -EPERM;
  587. /*
  588. * If the default policy is to allow by default, try to remove
  589. * an matching exception instead. And be silent about it: we
  590. * don't want to break compatibility
  591. */
  592. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  593. dev_exception_rm(devcgroup, &ex);
  594. return 0;
  595. }
  596. rc = dev_exception_add(devcgroup, &ex);
  597. break;
  598. case DEVCG_DENY:
  599. /*
  600. * If the default policy is to deny by default, try to remove
  601. * an matching exception instead. And be silent about it: we
  602. * don't want to break compatibility
  603. */
  604. if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
  605. dev_exception_rm(devcgroup, &ex);
  606. else
  607. rc = dev_exception_add(devcgroup, &ex);
  608. if (rc)
  609. break;
  610. /* we only propagate new restrictions */
  611. rc = propagate_exception(devcgroup, &ex);
  612. break;
  613. default:
  614. rc = -EINVAL;
  615. }
  616. return rc;
  617. }
  618. static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
  619. const char *buffer)
  620. {
  621. int retval;
  622. mutex_lock(&devcgroup_mutex);
  623. retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
  624. cft->private, buffer);
  625. mutex_unlock(&devcgroup_mutex);
  626. return retval;
  627. }
  628. static struct cftype dev_cgroup_files[] = {
  629. {
  630. .name = "allow",
  631. .write_string = devcgroup_access_write,
  632. .private = DEVCG_ALLOW,
  633. },
  634. {
  635. .name = "deny",
  636. .write_string = devcgroup_access_write,
  637. .private = DEVCG_DENY,
  638. },
  639. {
  640. .name = "list",
  641. .read_seq_string = devcgroup_seq_read,
  642. .private = DEVCG_LIST,
  643. },
  644. { } /* terminate */
  645. };
  646. struct cgroup_subsys devices_subsys = {
  647. .name = "devices",
  648. .can_attach = devcgroup_can_attach,
  649. .css_alloc = devcgroup_css_alloc,
  650. .css_free = devcgroup_css_free,
  651. .css_online = devcgroup_online,
  652. .css_offline = devcgroup_offline,
  653. .subsys_id = devices_subsys_id,
  654. .base_cftypes = dev_cgroup_files,
  655. /*
  656. * While devices cgroup has the rudimentary hierarchy support which
  657. * checks the parent's restriction, it doesn't properly propagates
  658. * config changes in ancestors to their descendents. A child
  659. * should only be allowed to add more restrictions to the parent's
  660. * configuration. Fix it and remove the following.
  661. */
  662. .broken_hierarchy = true,
  663. };
  664. /**
  665. * __devcgroup_check_permission - checks if an inode operation is permitted
  666. * @dev_cgroup: the dev cgroup to be tested against
  667. * @type: device type
  668. * @major: device major number
  669. * @minor: device minor number
  670. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  671. *
  672. * returns 0 on success, -EPERM case the operation is not permitted
  673. */
  674. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  675. short access)
  676. {
  677. struct dev_cgroup *dev_cgroup;
  678. struct dev_exception_item ex;
  679. int rc;
  680. memset(&ex, 0, sizeof(ex));
  681. ex.type = type;
  682. ex.major = major;
  683. ex.minor = minor;
  684. ex.access = access;
  685. rcu_read_lock();
  686. dev_cgroup = task_devcgroup(current);
  687. rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
  688. rcu_read_unlock();
  689. if (!rc)
  690. return -EPERM;
  691. return 0;
  692. }
  693. int __devcgroup_inode_permission(struct inode *inode, int mask)
  694. {
  695. short type, access = 0;
  696. if (S_ISBLK(inode->i_mode))
  697. type = DEV_BLOCK;
  698. if (S_ISCHR(inode->i_mode))
  699. type = DEV_CHAR;
  700. if (mask & MAY_WRITE)
  701. access |= ACC_WRITE;
  702. if (mask & MAY_READ)
  703. access |= ACC_READ;
  704. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  705. access);
  706. }
  707. int devcgroup_inode_mknod(int mode, dev_t dev)
  708. {
  709. short type;
  710. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  711. return 0;
  712. if (S_ISBLK(mode))
  713. type = DEV_BLOCK;
  714. else
  715. type = DEV_CHAR;
  716. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  717. ACC_MKNOD);
  718. }