device_cgroup.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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_show(struct seq_file *m, void *v)
  230. {
  231. struct dev_cgroup *devcgroup = css_to_devcgroup(seq_css(m));
  232. struct dev_exception_item *ex;
  233. char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
  234. rcu_read_lock();
  235. /*
  236. * To preserve the compatibility:
  237. * - Only show the "all devices" when the default policy is to allow
  238. * - List the exceptions in case the default policy is to deny
  239. * This way, the file remains as a "whitelist of devices"
  240. */
  241. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  242. set_access(acc, ACC_MASK);
  243. set_majmin(maj, ~0);
  244. set_majmin(min, ~0);
  245. seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
  246. maj, min, acc);
  247. } else {
  248. list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
  249. set_access(acc, ex->access);
  250. set_majmin(maj, ex->major);
  251. set_majmin(min, ex->minor);
  252. seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
  253. maj, min, acc);
  254. }
  255. }
  256. rcu_read_unlock();
  257. return 0;
  258. }
  259. /**
  260. * may_access - verifies if a new exception is part of what is allowed
  261. * by a dev cgroup based on the default policy +
  262. * exceptions. This is used to make sure a child cgroup
  263. * won't have more privileges than its parent or to
  264. * verify if a certain access is allowed.
  265. * @dev_cgroup: dev cgroup to be tested against
  266. * @refex: new exception
  267. * @behavior: behavior of the exception
  268. */
  269. static bool may_access(struct dev_cgroup *dev_cgroup,
  270. struct dev_exception_item *refex,
  271. enum devcg_behavior behavior)
  272. {
  273. struct dev_exception_item *ex;
  274. bool match = false;
  275. rcu_lockdep_assert(rcu_read_lock_held() ||
  276. lockdep_is_held(&devcgroup_mutex),
  277. "device_cgroup::may_access() called without proper synchronization");
  278. list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
  279. if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  280. continue;
  281. if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  282. continue;
  283. if (ex->major != ~0 && ex->major != refex->major)
  284. continue;
  285. if (ex->minor != ~0 && ex->minor != refex->minor)
  286. continue;
  287. if (refex->access & (~ex->access))
  288. continue;
  289. match = true;
  290. break;
  291. }
  292. if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  293. if (behavior == DEVCG_DEFAULT_ALLOW) {
  294. /* the exception will deny access to certain devices */
  295. return true;
  296. } else {
  297. /* the exception will allow access to certain devices */
  298. if (match)
  299. /*
  300. * a new exception allowing access shouldn't
  301. * match an parent's exception
  302. */
  303. return false;
  304. return true;
  305. }
  306. } else {
  307. /* only behavior == DEVCG_DEFAULT_DENY allowed here */
  308. if (match)
  309. /* parent has an exception that matches the proposed */
  310. return true;
  311. else
  312. return false;
  313. }
  314. return false;
  315. }
  316. /*
  317. * parent_has_perm:
  318. * when adding a new allow rule to a device exception list, the rule
  319. * must be allowed in the parent device
  320. */
  321. static int parent_has_perm(struct dev_cgroup *childcg,
  322. struct dev_exception_item *ex)
  323. {
  324. struct dev_cgroup *parent = css_to_devcgroup(css_parent(&childcg->css));
  325. if (!parent)
  326. return 1;
  327. return may_access(parent, ex, childcg->behavior);
  328. }
  329. /**
  330. * may_allow_all - checks if it's possible to change the behavior to
  331. * allow based on parent's rules.
  332. * @parent: device cgroup's parent
  333. * returns: != 0 in case it's allowed, 0 otherwise
  334. */
  335. static inline int may_allow_all(struct dev_cgroup *parent)
  336. {
  337. if (!parent)
  338. return 1;
  339. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  340. }
  341. /**
  342. * revalidate_active_exceptions - walks through the active exception list and
  343. * revalidates the exceptions based on parent's
  344. * behavior and exceptions. The exceptions that
  345. * are no longer valid will be removed.
  346. * Called with devcgroup_mutex held.
  347. * @devcg: cgroup which exceptions will be checked
  348. *
  349. * This is one of the three key functions for hierarchy implementation.
  350. * This function is responsible for re-evaluating all the cgroup's active
  351. * exceptions due to a parent's exception change.
  352. * Refer to Documentation/cgroups/devices.txt for more details.
  353. */
  354. static void revalidate_active_exceptions(struct dev_cgroup *devcg)
  355. {
  356. struct dev_exception_item *ex;
  357. struct list_head *this, *tmp;
  358. list_for_each_safe(this, tmp, &devcg->exceptions) {
  359. ex = container_of(this, struct dev_exception_item, list);
  360. if (!parent_has_perm(devcg, ex))
  361. dev_exception_rm(devcg, ex);
  362. }
  363. }
  364. /**
  365. * propagate_exception - propagates a new exception to the children
  366. * @devcg_root: device cgroup that added a new exception
  367. * @ex: new exception to be propagated
  368. *
  369. * returns: 0 in case of success, != 0 in case of error
  370. */
  371. static int propagate_exception(struct dev_cgroup *devcg_root,
  372. struct dev_exception_item *ex)
  373. {
  374. struct cgroup_subsys_state *pos;
  375. int rc = 0;
  376. rcu_read_lock();
  377. css_for_each_descendant_pre(pos, &devcg_root->css) {
  378. struct dev_cgroup *devcg = css_to_devcgroup(pos);
  379. /*
  380. * Because devcgroup_mutex is held, no devcg will become
  381. * online or offline during the tree walk (see on/offline
  382. * methods), and online ones are safe to access outside RCU
  383. * read lock without bumping refcnt.
  384. */
  385. if (pos == &devcg_root->css || !is_devcg_online(devcg))
  386. continue;
  387. rcu_read_unlock();
  388. /*
  389. * in case both root's behavior and devcg is allow, a new
  390. * restriction means adding to the exception list
  391. */
  392. if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
  393. devcg->behavior == DEVCG_DEFAULT_ALLOW) {
  394. rc = dev_exception_add(devcg, ex);
  395. if (rc)
  396. break;
  397. } else {
  398. /*
  399. * in the other possible cases:
  400. * root's behavior: allow, devcg's: deny
  401. * root's behavior: deny, devcg's: deny
  402. * the exception will be removed
  403. */
  404. dev_exception_rm(devcg, ex);
  405. }
  406. revalidate_active_exceptions(devcg);
  407. rcu_read_lock();
  408. }
  409. rcu_read_unlock();
  410. return rc;
  411. }
  412. static inline bool has_children(struct dev_cgroup *devcgroup)
  413. {
  414. struct cgroup *cgrp = devcgroup->css.cgroup;
  415. return !list_empty(&cgrp->children);
  416. }
  417. /*
  418. * Modify the exception list using allow/deny rules.
  419. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  420. * so we can give a container CAP_MKNOD to let it create devices but not
  421. * modify the exception list.
  422. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  423. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  424. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  425. *
  426. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  427. * new access is only allowed if you're in the top-level cgroup, or your
  428. * parent cgroup has the access you're asking for.
  429. */
  430. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  431. int filetype, const char *buffer)
  432. {
  433. const char *b;
  434. char temp[12]; /* 11 + 1 characters needed for a u32 */
  435. int count, rc = 0;
  436. struct dev_exception_item ex;
  437. struct dev_cgroup *parent = css_to_devcgroup(css_parent(&devcgroup->css));
  438. if (!capable(CAP_SYS_ADMIN))
  439. return -EPERM;
  440. memset(&ex, 0, sizeof(ex));
  441. b = buffer;
  442. switch (*b) {
  443. case 'a':
  444. switch (filetype) {
  445. case DEVCG_ALLOW:
  446. if (has_children(devcgroup))
  447. return -EINVAL;
  448. if (!may_allow_all(parent))
  449. return -EPERM;
  450. dev_exception_clean(devcgroup);
  451. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  452. if (!parent)
  453. break;
  454. rc = dev_exceptions_copy(&devcgroup->exceptions,
  455. &parent->exceptions);
  456. if (rc)
  457. return rc;
  458. break;
  459. case DEVCG_DENY:
  460. if (has_children(devcgroup))
  461. return -EINVAL;
  462. dev_exception_clean(devcgroup);
  463. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  464. break;
  465. default:
  466. return -EINVAL;
  467. }
  468. return 0;
  469. case 'b':
  470. ex.type = DEV_BLOCK;
  471. break;
  472. case 'c':
  473. ex.type = DEV_CHAR;
  474. break;
  475. default:
  476. return -EINVAL;
  477. }
  478. b++;
  479. if (!isspace(*b))
  480. return -EINVAL;
  481. b++;
  482. if (*b == '*') {
  483. ex.major = ~0;
  484. b++;
  485. } else if (isdigit(*b)) {
  486. memset(temp, 0, sizeof(temp));
  487. for (count = 0; count < sizeof(temp) - 1; count++) {
  488. temp[count] = *b;
  489. b++;
  490. if (!isdigit(*b))
  491. break;
  492. }
  493. rc = kstrtou32(temp, 10, &ex.major);
  494. if (rc)
  495. return -EINVAL;
  496. } else {
  497. return -EINVAL;
  498. }
  499. if (*b != ':')
  500. return -EINVAL;
  501. b++;
  502. /* read minor */
  503. if (*b == '*') {
  504. ex.minor = ~0;
  505. b++;
  506. } else if (isdigit(*b)) {
  507. memset(temp, 0, sizeof(temp));
  508. for (count = 0; count < sizeof(temp) - 1; count++) {
  509. temp[count] = *b;
  510. b++;
  511. if (!isdigit(*b))
  512. break;
  513. }
  514. rc = kstrtou32(temp, 10, &ex.minor);
  515. if (rc)
  516. return -EINVAL;
  517. } else {
  518. return -EINVAL;
  519. }
  520. if (!isspace(*b))
  521. return -EINVAL;
  522. for (b++, count = 0; count < 3; count++, b++) {
  523. switch (*b) {
  524. case 'r':
  525. ex.access |= ACC_READ;
  526. break;
  527. case 'w':
  528. ex.access |= ACC_WRITE;
  529. break;
  530. case 'm':
  531. ex.access |= ACC_MKNOD;
  532. break;
  533. case '\n':
  534. case '\0':
  535. count = 3;
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. }
  541. switch (filetype) {
  542. case DEVCG_ALLOW:
  543. if (!parent_has_perm(devcgroup, &ex))
  544. return -EPERM;
  545. /*
  546. * If the default policy is to allow by default, try to remove
  547. * an matching exception instead. And be silent about it: we
  548. * don't want to break compatibility
  549. */
  550. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  551. dev_exception_rm(devcgroup, &ex);
  552. return 0;
  553. }
  554. rc = dev_exception_add(devcgroup, &ex);
  555. break;
  556. case DEVCG_DENY:
  557. /*
  558. * If the default policy is to deny by default, try to remove
  559. * an matching exception instead. And be silent about it: we
  560. * don't want to break compatibility
  561. */
  562. if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
  563. dev_exception_rm(devcgroup, &ex);
  564. else
  565. rc = dev_exception_add(devcgroup, &ex);
  566. if (rc)
  567. break;
  568. /* we only propagate new restrictions */
  569. rc = propagate_exception(devcgroup, &ex);
  570. break;
  571. default:
  572. rc = -EINVAL;
  573. }
  574. return rc;
  575. }
  576. static int devcgroup_access_write(struct cgroup_subsys_state *css,
  577. struct cftype *cft, const char *buffer)
  578. {
  579. int retval;
  580. mutex_lock(&devcgroup_mutex);
  581. retval = devcgroup_update_access(css_to_devcgroup(css),
  582. cft->private, buffer);
  583. mutex_unlock(&devcgroup_mutex);
  584. return retval;
  585. }
  586. static struct cftype dev_cgroup_files[] = {
  587. {
  588. .name = "allow",
  589. .write_string = devcgroup_access_write,
  590. .private = DEVCG_ALLOW,
  591. },
  592. {
  593. .name = "deny",
  594. .write_string = devcgroup_access_write,
  595. .private = DEVCG_DENY,
  596. },
  597. {
  598. .name = "list",
  599. .seq_show = devcgroup_seq_show,
  600. .private = DEVCG_LIST,
  601. },
  602. { } /* terminate */
  603. };
  604. struct cgroup_subsys devices_subsys = {
  605. .name = "devices",
  606. .css_alloc = devcgroup_css_alloc,
  607. .css_free = devcgroup_css_free,
  608. .css_online = devcgroup_online,
  609. .css_offline = devcgroup_offline,
  610. .subsys_id = devices_subsys_id,
  611. .base_cftypes = dev_cgroup_files,
  612. };
  613. /**
  614. * __devcgroup_check_permission - checks if an inode operation is permitted
  615. * @dev_cgroup: the dev cgroup to be tested against
  616. * @type: device type
  617. * @major: device major number
  618. * @minor: device minor number
  619. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  620. *
  621. * returns 0 on success, -EPERM case the operation is not permitted
  622. */
  623. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  624. short access)
  625. {
  626. struct dev_cgroup *dev_cgroup;
  627. struct dev_exception_item ex;
  628. int rc;
  629. memset(&ex, 0, sizeof(ex));
  630. ex.type = type;
  631. ex.major = major;
  632. ex.minor = minor;
  633. ex.access = access;
  634. rcu_read_lock();
  635. dev_cgroup = task_devcgroup(current);
  636. rc = may_access(dev_cgroup, &ex, dev_cgroup->behavior);
  637. rcu_read_unlock();
  638. if (!rc)
  639. return -EPERM;
  640. return 0;
  641. }
  642. int __devcgroup_inode_permission(struct inode *inode, int mask)
  643. {
  644. short type, access = 0;
  645. if (S_ISBLK(inode->i_mode))
  646. type = DEV_BLOCK;
  647. if (S_ISCHR(inode->i_mode))
  648. type = DEV_CHAR;
  649. if (mask & MAY_WRITE)
  650. access |= ACC_WRITE;
  651. if (mask & MAY_READ)
  652. access |= ACC_READ;
  653. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  654. access);
  655. }
  656. int devcgroup_inode_mknod(int mode, dev_t dev)
  657. {
  658. short type;
  659. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  660. return 0;
  661. if (S_ISBLK(mode))
  662. type = DEV_BLOCK;
  663. else
  664. type = DEV_CHAR;
  665. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  666. ACC_MKNOD);
  667. }