device_cgroup.c 17 KB

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