device_cgroup.c 21 KB

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