device_cgroup.c 20 KB

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