device_cgroup.c 18 KB

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