device_cgroup.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. #define ACC_MKNOD 1
  17. #define ACC_READ 2
  18. #define ACC_WRITE 4
  19. #define ACC_MASK (ACC_MKNOD | ACC_READ | ACC_WRITE)
  20. #define DEV_BLOCK 1
  21. #define DEV_CHAR 2
  22. #define DEV_ALL 4 /* this represents all devices */
  23. static DEFINE_MUTEX(devcgroup_mutex);
  24. enum devcg_behavior {
  25. DEVCG_DEFAULT_NONE,
  26. DEVCG_DEFAULT_ALLOW,
  27. DEVCG_DEFAULT_DENY,
  28. };
  29. /*
  30. * exception list locking rules:
  31. * hold devcgroup_mutex for update/read.
  32. * hold rcu_read_lock() for read.
  33. */
  34. struct dev_exception_item {
  35. u32 major, minor;
  36. short type;
  37. short access;
  38. struct list_head list;
  39. struct rcu_head rcu;
  40. };
  41. struct dev_cgroup {
  42. struct cgroup_subsys_state css;
  43. struct list_head exceptions;
  44. enum devcg_behavior behavior;
  45. };
  46. static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
  47. {
  48. return s ? container_of(s, struct dev_cgroup, css) : NULL;
  49. }
  50. static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
  51. {
  52. return css_to_devcgroup(task_css(task, devices_cgrp_id));
  53. }
  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);
  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. * match_exception - iterates the exception list trying to find a complete match
  261. * @exceptions: list of exceptions
  262. * @type: device type (DEV_BLOCK or DEV_CHAR)
  263. * @major: device file major number, ~0 to match all
  264. * @minor: device file minor number, ~0 to match all
  265. * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
  266. *
  267. * It is considered a complete match if an exception is found that will
  268. * contain the entire range of provided parameters.
  269. *
  270. * Return: true in case it matches an exception completely
  271. */
  272. static bool match_exception(struct list_head *exceptions, short type,
  273. u32 major, u32 minor, short access)
  274. {
  275. struct dev_exception_item *ex;
  276. list_for_each_entry_rcu(ex, exceptions, list) {
  277. if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  278. continue;
  279. if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  280. continue;
  281. if (ex->major != ~0 && ex->major != major)
  282. continue;
  283. if (ex->minor != ~0 && ex->minor != minor)
  284. continue;
  285. /* provided access cannot have more than the exception rule */
  286. if (access & (~ex->access))
  287. continue;
  288. return true;
  289. }
  290. return false;
  291. }
  292. /**
  293. * match_exception_partial - iterates the exception list trying to find a partial match
  294. * @exceptions: list of exceptions
  295. * @type: device type (DEV_BLOCK or DEV_CHAR)
  296. * @major: device file major number, ~0 to match all
  297. * @minor: device file minor number, ~0 to match all
  298. * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
  299. *
  300. * It is considered a partial match if an exception's range is found to
  301. * contain *any* of the devices specified by provided parameters. This is
  302. * used to make sure no extra access is being granted that is forbidden by
  303. * any of the exception list.
  304. *
  305. * Return: true in case the provided range mat matches an exception completely
  306. */
  307. static bool match_exception_partial(struct list_head *exceptions, short type,
  308. u32 major, u32 minor, short access)
  309. {
  310. struct dev_exception_item *ex;
  311. list_for_each_entry_rcu(ex, exceptions, list) {
  312. if ((type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  313. continue;
  314. if ((type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  315. continue;
  316. /*
  317. * We must be sure that both the exception and the provided
  318. * range aren't masking all devices
  319. */
  320. if (ex->major != ~0 && major != ~0 && ex->major != major)
  321. continue;
  322. if (ex->minor != ~0 && minor != ~0 && ex->minor != minor)
  323. continue;
  324. /*
  325. * In order to make sure the provided range isn't matching
  326. * an exception, all its access bits shouldn't match the
  327. * exception's access bits
  328. */
  329. if (!(access & ex->access))
  330. continue;
  331. return true;
  332. }
  333. return false;
  334. }
  335. /**
  336. * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
  337. * @dev_cgroup: dev cgroup to be tested against
  338. * @refex: new exception
  339. * @behavior: behavior of the exception's dev_cgroup
  340. *
  341. * This is used to make sure a child cgroup won't have more privileges
  342. * than its parent
  343. */
  344. static bool verify_new_ex(struct dev_cgroup *dev_cgroup,
  345. struct dev_exception_item *refex,
  346. enum devcg_behavior behavior)
  347. {
  348. bool match = false;
  349. RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&
  350. !lockdep_is_held(&devcgroup_mutex),
  351. "device_cgroup:verify_new_ex called without proper synchronization");
  352. if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  353. if (behavior == DEVCG_DEFAULT_ALLOW) {
  354. /*
  355. * new exception in the child doesn't matter, only
  356. * adding extra restrictions
  357. */
  358. return true;
  359. } else {
  360. /*
  361. * new exception in the child will add more devices
  362. * that can be acessed, so it can't match any of
  363. * parent's exceptions, even slightly
  364. */
  365. match = match_exception_partial(&dev_cgroup->exceptions,
  366. refex->type,
  367. refex->major,
  368. refex->minor,
  369. refex->access);
  370. if (match)
  371. return false;
  372. return true;
  373. }
  374. } else {
  375. /*
  376. * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
  377. * the new exception will add access to more devices and must
  378. * be contained completely in an parent's exception to be
  379. * allowed
  380. */
  381. match = match_exception(&dev_cgroup->exceptions, refex->type,
  382. refex->major, refex->minor,
  383. refex->access);
  384. if (match)
  385. /* parent has an exception that matches the proposed */
  386. return true;
  387. else
  388. return false;
  389. }
  390. return false;
  391. }
  392. /*
  393. * parent_has_perm:
  394. * when adding a new allow rule to a device exception list, the rule
  395. * must be allowed in the parent device
  396. */
  397. static int parent_has_perm(struct dev_cgroup *childcg,
  398. struct dev_exception_item *ex)
  399. {
  400. struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
  401. if (!parent)
  402. return 1;
  403. return verify_new_ex(parent, ex, childcg->behavior);
  404. }
  405. /**
  406. * parent_allows_removal - verify if it's ok to remove an exception
  407. * @childcg: child cgroup from where the exception will be removed
  408. * @ex: exception being removed
  409. *
  410. * When removing an exception in cgroups with default ALLOW policy, it must
  411. * be checked if removing it will give the child cgroup more access than the
  412. * parent.
  413. *
  414. * Return: true if it's ok to remove exception, false otherwise
  415. */
  416. static bool parent_allows_removal(struct dev_cgroup *childcg,
  417. struct dev_exception_item *ex)
  418. {
  419. struct dev_cgroup *parent = css_to_devcgroup(childcg->css.parent);
  420. if (!parent)
  421. return true;
  422. /* It's always allowed to remove access to devices */
  423. if (childcg->behavior == DEVCG_DEFAULT_DENY)
  424. return true;
  425. /*
  426. * Make sure you're not removing part or a whole exception existing in
  427. * the parent cgroup
  428. */
  429. return !match_exception_partial(&parent->exceptions, ex->type,
  430. ex->major, ex->minor, ex->access);
  431. }
  432. /**
  433. * may_allow_all - checks if it's possible to change the behavior to
  434. * allow based on parent's rules.
  435. * @parent: device cgroup's parent
  436. * returns: != 0 in case it's allowed, 0 otherwise
  437. */
  438. static inline int may_allow_all(struct dev_cgroup *parent)
  439. {
  440. if (!parent)
  441. return 1;
  442. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  443. }
  444. /**
  445. * revalidate_active_exceptions - walks through the active exception list and
  446. * revalidates the exceptions based on parent's
  447. * behavior and exceptions. The exceptions that
  448. * are no longer valid will be removed.
  449. * Called with devcgroup_mutex held.
  450. * @devcg: cgroup which exceptions will be checked
  451. *
  452. * This is one of the three key functions for hierarchy implementation.
  453. * This function is responsible for re-evaluating all the cgroup's active
  454. * exceptions due to a parent's exception change.
  455. * Refer to Documentation/cgroups/devices.txt for more details.
  456. */
  457. static void revalidate_active_exceptions(struct dev_cgroup *devcg)
  458. {
  459. struct dev_exception_item *ex;
  460. struct list_head *this, *tmp;
  461. list_for_each_safe(this, tmp, &devcg->exceptions) {
  462. ex = container_of(this, struct dev_exception_item, list);
  463. if (!parent_has_perm(devcg, ex))
  464. dev_exception_rm(devcg, ex);
  465. }
  466. }
  467. /**
  468. * propagate_exception - propagates a new exception to the children
  469. * @devcg_root: device cgroup that added a new exception
  470. * @ex: new exception to be propagated
  471. *
  472. * returns: 0 in case of success, != 0 in case of error
  473. */
  474. static int propagate_exception(struct dev_cgroup *devcg_root,
  475. struct dev_exception_item *ex)
  476. {
  477. struct cgroup_subsys_state *pos;
  478. int rc = 0;
  479. rcu_read_lock();
  480. css_for_each_descendant_pre(pos, &devcg_root->css) {
  481. struct dev_cgroup *devcg = css_to_devcgroup(pos);
  482. /*
  483. * Because devcgroup_mutex is held, no devcg will become
  484. * online or offline during the tree walk (see on/offline
  485. * methods), and online ones are safe to access outside RCU
  486. * read lock without bumping refcnt.
  487. */
  488. if (pos == &devcg_root->css || !is_devcg_online(devcg))
  489. continue;
  490. rcu_read_unlock();
  491. /*
  492. * in case both root's behavior and devcg is allow, a new
  493. * restriction means adding to the exception list
  494. */
  495. if (devcg_root->behavior == DEVCG_DEFAULT_ALLOW &&
  496. devcg->behavior == DEVCG_DEFAULT_ALLOW) {
  497. rc = dev_exception_add(devcg, ex);
  498. if (rc)
  499. return rc;
  500. } else {
  501. /*
  502. * in the other possible cases:
  503. * root's behavior: allow, devcg's: deny
  504. * root's behavior: deny, devcg's: deny
  505. * the exception will be removed
  506. */
  507. dev_exception_rm(devcg, ex);
  508. }
  509. revalidate_active_exceptions(devcg);
  510. rcu_read_lock();
  511. }
  512. rcu_read_unlock();
  513. return rc;
  514. }
  515. /*
  516. * Modify the exception list using allow/deny rules.
  517. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  518. * so we can give a container CAP_MKNOD to let it create devices but not
  519. * modify the exception list.
  520. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  521. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  522. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  523. *
  524. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  525. * new access is only allowed if you're in the top-level cgroup, or your
  526. * parent cgroup has the access you're asking for.
  527. */
  528. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  529. int filetype, char *buffer)
  530. {
  531. const char *b;
  532. char temp[12]; /* 11 + 1 characters needed for a u32 */
  533. int count, rc = 0;
  534. struct dev_exception_item ex;
  535. struct dev_cgroup *parent = css_to_devcgroup(devcgroup->css.parent);
  536. if (!capable(CAP_SYS_ADMIN))
  537. return -EPERM;
  538. memset(&ex, 0, sizeof(ex));
  539. b = buffer;
  540. switch (*b) {
  541. case 'a':
  542. switch (filetype) {
  543. case DEVCG_ALLOW:
  544. if (css_has_online_children(&devcgroup->css))
  545. return -EINVAL;
  546. if (!may_allow_all(parent))
  547. return -EPERM;
  548. dev_exception_clean(devcgroup);
  549. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  550. if (!parent)
  551. break;
  552. rc = dev_exceptions_copy(&devcgroup->exceptions,
  553. &parent->exceptions);
  554. if (rc)
  555. return rc;
  556. break;
  557. case DEVCG_DENY:
  558. if (css_has_online_children(&devcgroup->css))
  559. return -EINVAL;
  560. dev_exception_clean(devcgroup);
  561. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  562. break;
  563. default:
  564. return -EINVAL;
  565. }
  566. return 0;
  567. case 'b':
  568. ex.type = DEV_BLOCK;
  569. break;
  570. case 'c':
  571. ex.type = DEV_CHAR;
  572. break;
  573. default:
  574. return -EINVAL;
  575. }
  576. b++;
  577. if (!isspace(*b))
  578. return -EINVAL;
  579. b++;
  580. if (*b == '*') {
  581. ex.major = ~0;
  582. b++;
  583. } else if (isdigit(*b)) {
  584. memset(temp, 0, sizeof(temp));
  585. for (count = 0; count < sizeof(temp) - 1; count++) {
  586. temp[count] = *b;
  587. b++;
  588. if (!isdigit(*b))
  589. break;
  590. }
  591. rc = kstrtou32(temp, 10, &ex.major);
  592. if (rc)
  593. return -EINVAL;
  594. } else {
  595. return -EINVAL;
  596. }
  597. if (*b != ':')
  598. return -EINVAL;
  599. b++;
  600. /* read minor */
  601. if (*b == '*') {
  602. ex.minor = ~0;
  603. b++;
  604. } else if (isdigit(*b)) {
  605. memset(temp, 0, sizeof(temp));
  606. for (count = 0; count < sizeof(temp) - 1; count++) {
  607. temp[count] = *b;
  608. b++;
  609. if (!isdigit(*b))
  610. break;
  611. }
  612. rc = kstrtou32(temp, 10, &ex.minor);
  613. if (rc)
  614. return -EINVAL;
  615. } else {
  616. return -EINVAL;
  617. }
  618. if (!isspace(*b))
  619. return -EINVAL;
  620. for (b++, count = 0; count < 3; count++, b++) {
  621. switch (*b) {
  622. case 'r':
  623. ex.access |= ACC_READ;
  624. break;
  625. case 'w':
  626. ex.access |= ACC_WRITE;
  627. break;
  628. case 'm':
  629. ex.access |= ACC_MKNOD;
  630. break;
  631. case '\n':
  632. case '\0':
  633. count = 3;
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. }
  639. switch (filetype) {
  640. case DEVCG_ALLOW:
  641. /*
  642. * If the default policy is to allow by default, try to remove
  643. * an matching exception instead. And be silent about it: we
  644. * don't want to break compatibility
  645. */
  646. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  647. /* Check if the parent allows removing it first */
  648. if (!parent_allows_removal(devcgroup, &ex))
  649. return -EPERM;
  650. dev_exception_rm(devcgroup, &ex);
  651. break;
  652. }
  653. if (!parent_has_perm(devcgroup, &ex))
  654. return -EPERM;
  655. rc = dev_exception_add(devcgroup, &ex);
  656. break;
  657. case DEVCG_DENY:
  658. /*
  659. * If the default policy is to deny by default, try to remove
  660. * an matching exception instead. And be silent about it: we
  661. * don't want to break compatibility
  662. */
  663. if (devcgroup->behavior == DEVCG_DEFAULT_DENY)
  664. dev_exception_rm(devcgroup, &ex);
  665. else
  666. rc = dev_exception_add(devcgroup, &ex);
  667. if (rc)
  668. break;
  669. /* we only propagate new restrictions */
  670. rc = propagate_exception(devcgroup, &ex);
  671. break;
  672. default:
  673. rc = -EINVAL;
  674. }
  675. return rc;
  676. }
  677. static ssize_t devcgroup_access_write(struct kernfs_open_file *of,
  678. char *buf, size_t nbytes, loff_t off)
  679. {
  680. int retval;
  681. mutex_lock(&devcgroup_mutex);
  682. retval = devcgroup_update_access(css_to_devcgroup(of_css(of)),
  683. of_cft(of)->private, strstrip(buf));
  684. mutex_unlock(&devcgroup_mutex);
  685. return retval ?: nbytes;
  686. }
  687. static struct cftype dev_cgroup_files[] = {
  688. {
  689. .name = "allow",
  690. .write = devcgroup_access_write,
  691. .private = DEVCG_ALLOW,
  692. },
  693. {
  694. .name = "deny",
  695. .write = devcgroup_access_write,
  696. .private = DEVCG_DENY,
  697. },
  698. {
  699. .name = "list",
  700. .seq_show = devcgroup_seq_show,
  701. .private = DEVCG_LIST,
  702. },
  703. { } /* terminate */
  704. };
  705. struct cgroup_subsys devices_cgrp_subsys = {
  706. .css_alloc = devcgroup_css_alloc,
  707. .css_free = devcgroup_css_free,
  708. .css_online = devcgroup_online,
  709. .css_offline = devcgroup_offline,
  710. .legacy_cftypes = dev_cgroup_files,
  711. };
  712. /**
  713. * __devcgroup_check_permission - checks if an inode operation is permitted
  714. * @dev_cgroup: the dev cgroup to be tested against
  715. * @type: device type
  716. * @major: device major number
  717. * @minor: device minor number
  718. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  719. *
  720. * returns 0 on success, -EPERM case the operation is not permitted
  721. */
  722. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  723. short access)
  724. {
  725. struct dev_cgroup *dev_cgroup;
  726. bool rc;
  727. rcu_read_lock();
  728. dev_cgroup = task_devcgroup(current);
  729. if (dev_cgroup->behavior == DEVCG_DEFAULT_ALLOW)
  730. /* Can't match any of the exceptions, even partially */
  731. rc = !match_exception_partial(&dev_cgroup->exceptions,
  732. type, major, minor, access);
  733. else
  734. /* Need to match completely one exception to be allowed */
  735. rc = match_exception(&dev_cgroup->exceptions, type, major,
  736. minor, access);
  737. rcu_read_unlock();
  738. if (!rc)
  739. return -EPERM;
  740. return 0;
  741. }
  742. int __devcgroup_inode_permission(struct inode *inode, int mask)
  743. {
  744. short type, access = 0;
  745. if (S_ISBLK(inode->i_mode))
  746. type = DEV_BLOCK;
  747. if (S_ISCHR(inode->i_mode))
  748. type = DEV_CHAR;
  749. if (mask & MAY_WRITE)
  750. access |= ACC_WRITE;
  751. if (mask & MAY_READ)
  752. access |= ACC_READ;
  753. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  754. access);
  755. }
  756. int devcgroup_inode_mknod(int mode, dev_t dev)
  757. {
  758. short type;
  759. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  760. return 0;
  761. if (S_ISBLK(mode))
  762. type = DEV_BLOCK;
  763. else
  764. type = DEV_CHAR;
  765. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  766. ACC_MKNOD);
  767. }