device_cgroup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. /*
  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 {
  39. DEVCG_DEFAULT_ALLOW,
  40. DEVCG_DEFAULT_DENY,
  41. } behavior;
  42. };
  43. static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
  44. {
  45. return container_of(s, struct dev_cgroup, css);
  46. }
  47. static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
  48. {
  49. return css_to_devcgroup(cgroup_subsys_state(cgroup, devices_subsys_id));
  50. }
  51. static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
  52. {
  53. return css_to_devcgroup(task_subsys_state(task, devices_subsys_id));
  54. }
  55. struct cgroup_subsys devices_subsys;
  56. static int devcgroup_can_attach(struct cgroup *new_cgrp,
  57. struct cgroup_taskset *set)
  58. {
  59. struct task_struct *task = cgroup_taskset_first(set);
  60. if (current != task && !capable(CAP_SYS_ADMIN))
  61. return -EPERM;
  62. return 0;
  63. }
  64. /*
  65. * called under devcgroup_mutex
  66. */
  67. static int dev_exceptions_copy(struct list_head *dest, struct list_head *orig)
  68. {
  69. struct dev_exception_item *ex, *tmp, *new;
  70. lockdep_assert_held(&devcgroup_mutex);
  71. list_for_each_entry(ex, orig, list) {
  72. new = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
  73. if (!new)
  74. goto free_and_exit;
  75. list_add_tail(&new->list, dest);
  76. }
  77. return 0;
  78. free_and_exit:
  79. list_for_each_entry_safe(ex, tmp, dest, list) {
  80. list_del(&ex->list);
  81. kfree(ex);
  82. }
  83. return -ENOMEM;
  84. }
  85. /*
  86. * called under devcgroup_mutex
  87. */
  88. static int dev_exception_add(struct dev_cgroup *dev_cgroup,
  89. struct dev_exception_item *ex)
  90. {
  91. struct dev_exception_item *excopy, *walk;
  92. lockdep_assert_held(&devcgroup_mutex);
  93. excopy = kmemdup(ex, sizeof(*ex), GFP_KERNEL);
  94. if (!excopy)
  95. return -ENOMEM;
  96. list_for_each_entry(walk, &dev_cgroup->exceptions, list) {
  97. if (walk->type != ex->type)
  98. continue;
  99. if (walk->major != ex->major)
  100. continue;
  101. if (walk->minor != ex->minor)
  102. continue;
  103. walk->access |= ex->access;
  104. kfree(excopy);
  105. excopy = NULL;
  106. }
  107. if (excopy != NULL)
  108. list_add_tail_rcu(&excopy->list, &dev_cgroup->exceptions);
  109. return 0;
  110. }
  111. /*
  112. * called under devcgroup_mutex
  113. */
  114. static void dev_exception_rm(struct dev_cgroup *dev_cgroup,
  115. struct dev_exception_item *ex)
  116. {
  117. struct dev_exception_item *walk, *tmp;
  118. lockdep_assert_held(&devcgroup_mutex);
  119. list_for_each_entry_safe(walk, tmp, &dev_cgroup->exceptions, list) {
  120. if (walk->type != ex->type)
  121. continue;
  122. if (walk->major != ex->major)
  123. continue;
  124. if (walk->minor != ex->minor)
  125. continue;
  126. walk->access &= ~ex->access;
  127. if (!walk->access) {
  128. list_del_rcu(&walk->list);
  129. kfree_rcu(walk, rcu);
  130. }
  131. }
  132. }
  133. /**
  134. * dev_exception_clean - frees all entries of the exception list
  135. * @dev_cgroup: dev_cgroup with the exception list to be cleaned
  136. *
  137. * called under devcgroup_mutex
  138. */
  139. static void dev_exception_clean(struct dev_cgroup *dev_cgroup)
  140. {
  141. struct dev_exception_item *ex, *tmp;
  142. lockdep_assert_held(&devcgroup_mutex);
  143. list_for_each_entry_safe(ex, tmp, &dev_cgroup->exceptions, list) {
  144. list_del_rcu(&ex->list);
  145. kfree_rcu(ex, rcu);
  146. }
  147. }
  148. /*
  149. * called from kernel/cgroup.c with cgroup_lock() held.
  150. */
  151. static struct cgroup_subsys_state *devcgroup_css_alloc(struct cgroup *cgroup)
  152. {
  153. struct dev_cgroup *dev_cgroup, *parent_dev_cgroup;
  154. struct cgroup *parent_cgroup;
  155. int ret;
  156. dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
  157. if (!dev_cgroup)
  158. return ERR_PTR(-ENOMEM);
  159. INIT_LIST_HEAD(&dev_cgroup->exceptions);
  160. parent_cgroup = cgroup->parent;
  161. if (parent_cgroup == NULL)
  162. dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
  163. else {
  164. parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
  165. mutex_lock(&devcgroup_mutex);
  166. ret = dev_exceptions_copy(&dev_cgroup->exceptions,
  167. &parent_dev_cgroup->exceptions);
  168. dev_cgroup->behavior = parent_dev_cgroup->behavior;
  169. mutex_unlock(&devcgroup_mutex);
  170. if (ret) {
  171. kfree(dev_cgroup);
  172. return ERR_PTR(ret);
  173. }
  174. }
  175. return &dev_cgroup->css;
  176. }
  177. static void devcgroup_css_free(struct cgroup *cgroup)
  178. {
  179. struct dev_cgroup *dev_cgroup;
  180. dev_cgroup = cgroup_to_devcgroup(cgroup);
  181. dev_exception_clean(dev_cgroup);
  182. kfree(dev_cgroup);
  183. }
  184. #define DEVCG_ALLOW 1
  185. #define DEVCG_DENY 2
  186. #define DEVCG_LIST 3
  187. #define MAJMINLEN 13
  188. #define ACCLEN 4
  189. static void set_access(char *acc, short access)
  190. {
  191. int idx = 0;
  192. memset(acc, 0, ACCLEN);
  193. if (access & ACC_READ)
  194. acc[idx++] = 'r';
  195. if (access & ACC_WRITE)
  196. acc[idx++] = 'w';
  197. if (access & ACC_MKNOD)
  198. acc[idx++] = 'm';
  199. }
  200. static char type_to_char(short type)
  201. {
  202. if (type == DEV_ALL)
  203. return 'a';
  204. if (type == DEV_CHAR)
  205. return 'c';
  206. if (type == DEV_BLOCK)
  207. return 'b';
  208. return 'X';
  209. }
  210. static void set_majmin(char *str, unsigned m)
  211. {
  212. if (m == ~0)
  213. strcpy(str, "*");
  214. else
  215. sprintf(str, "%u", m);
  216. }
  217. static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
  218. struct seq_file *m)
  219. {
  220. struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
  221. struct dev_exception_item *ex;
  222. char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
  223. rcu_read_lock();
  224. /*
  225. * To preserve the compatibility:
  226. * - Only show the "all devices" when the default policy is to allow
  227. * - List the exceptions in case the default policy is to deny
  228. * This way, the file remains as a "whitelist of devices"
  229. */
  230. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  231. set_access(acc, ACC_MASK);
  232. set_majmin(maj, ~0);
  233. set_majmin(min, ~0);
  234. seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
  235. maj, min, acc);
  236. } else {
  237. list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
  238. set_access(acc, ex->access);
  239. set_majmin(maj, ex->major);
  240. set_majmin(min, ex->minor);
  241. seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
  242. maj, min, acc);
  243. }
  244. }
  245. rcu_read_unlock();
  246. return 0;
  247. }
  248. /**
  249. * may_access - verifies if a new exception is part of what is allowed
  250. * by a dev cgroup based on the default policy +
  251. * exceptions. This is used to make sure a child cgroup
  252. * won't have more privileges than its parent or to
  253. * verify if a certain access is allowed.
  254. * @dev_cgroup: dev cgroup to be tested against
  255. * @refex: new exception
  256. */
  257. static int may_access(struct dev_cgroup *dev_cgroup,
  258. struct dev_exception_item *refex)
  259. {
  260. struct dev_exception_item *ex;
  261. bool match = false;
  262. rcu_lockdep_assert(rcu_read_lock_held() ||
  263. lockdep_is_held(&devcgroup_mutex),
  264. "device_cgroup::may_access() called without proper synchronization");
  265. list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
  266. if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  267. continue;
  268. if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  269. continue;
  270. if (ex->major != ~0 && ex->major != refex->major)
  271. continue;
  272. if (ex->minor != ~0 && ex->minor != refex->minor)
  273. continue;
  274. if (refex->access & (~ex->access))
  275. continue;
  276. match = true;
  277. break;
  278. }
  279. /*
  280. * In two cases we'll consider this new exception valid:
  281. * - the dev cgroup has its default policy to allow + exception list:
  282. * the new exception should *not* match any of the exceptions
  283. * (behavior == DEVCG_DEFAULT_ALLOW, !match)
  284. * - the dev cgroup has its default policy to deny + exception list:
  285. * the new exception *should* match the exceptions
  286. * (behavior == DEVCG_DEFAULT_DENY, match)
  287. */
  288. if ((dev_cgroup->behavior == DEVCG_DEFAULT_DENY) == match)
  289. return 1;
  290. return 0;
  291. }
  292. /*
  293. * parent_has_perm:
  294. * when adding a new allow rule to a device exception list, the rule
  295. * must be allowed in the parent device
  296. */
  297. static int parent_has_perm(struct dev_cgroup *childcg,
  298. struct dev_exception_item *ex)
  299. {
  300. struct cgroup *pcg = childcg->css.cgroup->parent;
  301. struct dev_cgroup *parent;
  302. if (!pcg)
  303. return 1;
  304. parent = cgroup_to_devcgroup(pcg);
  305. return may_access(parent, ex);
  306. }
  307. /**
  308. * may_allow_all - checks if it's possible to change the behavior to
  309. * allow based on parent's rules.
  310. * @parent: device cgroup's parent
  311. * returns: != 0 in case it's allowed, 0 otherwise
  312. */
  313. static inline int may_allow_all(struct dev_cgroup *parent)
  314. {
  315. if (!parent)
  316. return 1;
  317. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  318. }
  319. /*
  320. * Modify the exception list using allow/deny rules.
  321. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  322. * so we can give a container CAP_MKNOD to let it create devices but not
  323. * modify the exception list.
  324. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  325. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  326. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  327. *
  328. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  329. * new access is only allowed if you're in the top-level cgroup, or your
  330. * parent cgroup has the access you're asking for.
  331. */
  332. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  333. int filetype, const char *buffer)
  334. {
  335. const char *b;
  336. char temp[12]; /* 11 + 1 characters needed for a u32 */
  337. int count, rc;
  338. struct dev_exception_item ex;
  339. struct cgroup *p = devcgroup->css.cgroup;
  340. struct dev_cgroup *parent = NULL;
  341. if (!capable(CAP_SYS_ADMIN))
  342. return -EPERM;
  343. if (p->parent)
  344. parent = cgroup_to_devcgroup(p->parent);
  345. memset(&ex, 0, sizeof(ex));
  346. b = buffer;
  347. switch (*b) {
  348. case 'a':
  349. switch (filetype) {
  350. case DEVCG_ALLOW:
  351. if (!may_allow_all(parent))
  352. return -EPERM;
  353. dev_exception_clean(devcgroup);
  354. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  355. if (!parent)
  356. break;
  357. rc = dev_exceptions_copy(&devcgroup->exceptions,
  358. &parent->exceptions);
  359. if (rc)
  360. return rc;
  361. break;
  362. case DEVCG_DENY:
  363. dev_exception_clean(devcgroup);
  364. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  365. break;
  366. default:
  367. return -EINVAL;
  368. }
  369. return 0;
  370. case 'b':
  371. ex.type = DEV_BLOCK;
  372. break;
  373. case 'c':
  374. ex.type = DEV_CHAR;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. b++;
  380. if (!isspace(*b))
  381. return -EINVAL;
  382. b++;
  383. if (*b == '*') {
  384. ex.major = ~0;
  385. b++;
  386. } else if (isdigit(*b)) {
  387. memset(temp, 0, sizeof(temp));
  388. for (count = 0; count < sizeof(temp) - 1; count++) {
  389. temp[count] = *b;
  390. b++;
  391. if (!isdigit(*b))
  392. break;
  393. }
  394. rc = kstrtou32(temp, 10, &ex.major);
  395. if (rc)
  396. return -EINVAL;
  397. } else {
  398. return -EINVAL;
  399. }
  400. if (*b != ':')
  401. return -EINVAL;
  402. b++;
  403. /* read minor */
  404. if (*b == '*') {
  405. ex.minor = ~0;
  406. b++;
  407. } else if (isdigit(*b)) {
  408. memset(temp, 0, sizeof(temp));
  409. for (count = 0; count < sizeof(temp) - 1; count++) {
  410. temp[count] = *b;
  411. b++;
  412. if (!isdigit(*b))
  413. break;
  414. }
  415. rc = kstrtou32(temp, 10, &ex.minor);
  416. if (rc)
  417. return -EINVAL;
  418. } else {
  419. return -EINVAL;
  420. }
  421. if (!isspace(*b))
  422. return -EINVAL;
  423. for (b++, count = 0; count < 3; count++, b++) {
  424. switch (*b) {
  425. case 'r':
  426. ex.access |= ACC_READ;
  427. break;
  428. case 'w':
  429. ex.access |= ACC_WRITE;
  430. break;
  431. case 'm':
  432. ex.access |= ACC_MKNOD;
  433. break;
  434. case '\n':
  435. case '\0':
  436. count = 3;
  437. break;
  438. default:
  439. return -EINVAL;
  440. }
  441. }
  442. switch (filetype) {
  443. case DEVCG_ALLOW:
  444. if (!parent_has_perm(devcgroup, &ex))
  445. return -EPERM;
  446. /*
  447. * If the default policy is to allow by default, try to remove
  448. * an matching exception instead. And be silent about it: we
  449. * don't want to break compatibility
  450. */
  451. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  452. dev_exception_rm(devcgroup, &ex);
  453. return 0;
  454. }
  455. return dev_exception_add(devcgroup, &ex);
  456. case DEVCG_DENY:
  457. /*
  458. * If the default policy is to deny by default, try to remove
  459. * an matching exception instead. And be silent about it: we
  460. * don't want to break compatibility
  461. */
  462. if (devcgroup->behavior == DEVCG_DEFAULT_DENY) {
  463. dev_exception_rm(devcgroup, &ex);
  464. return 0;
  465. }
  466. return dev_exception_add(devcgroup, &ex);
  467. default:
  468. return -EINVAL;
  469. }
  470. return 0;
  471. }
  472. static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
  473. const char *buffer)
  474. {
  475. int retval;
  476. mutex_lock(&devcgroup_mutex);
  477. retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
  478. cft->private, buffer);
  479. mutex_unlock(&devcgroup_mutex);
  480. return retval;
  481. }
  482. static struct cftype dev_cgroup_files[] = {
  483. {
  484. .name = "allow",
  485. .write_string = devcgroup_access_write,
  486. .private = DEVCG_ALLOW,
  487. },
  488. {
  489. .name = "deny",
  490. .write_string = devcgroup_access_write,
  491. .private = DEVCG_DENY,
  492. },
  493. {
  494. .name = "list",
  495. .read_seq_string = devcgroup_seq_read,
  496. .private = DEVCG_LIST,
  497. },
  498. { } /* terminate */
  499. };
  500. struct cgroup_subsys devices_subsys = {
  501. .name = "devices",
  502. .can_attach = devcgroup_can_attach,
  503. .css_alloc = devcgroup_css_alloc,
  504. .css_free = devcgroup_css_free,
  505. .subsys_id = devices_subsys_id,
  506. .base_cftypes = dev_cgroup_files,
  507. /*
  508. * While devices cgroup has the rudimentary hierarchy support which
  509. * checks the parent's restriction, it doesn't properly propagates
  510. * config changes in ancestors to their descendents. A child
  511. * should only be allowed to add more restrictions to the parent's
  512. * configuration. Fix it and remove the following.
  513. */
  514. .broken_hierarchy = true,
  515. };
  516. /**
  517. * __devcgroup_check_permission - checks if an inode operation is permitted
  518. * @dev_cgroup: the dev cgroup to be tested against
  519. * @type: device type
  520. * @major: device major number
  521. * @minor: device minor number
  522. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  523. *
  524. * returns 0 on success, -EPERM case the operation is not permitted
  525. */
  526. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  527. short access)
  528. {
  529. struct dev_cgroup *dev_cgroup;
  530. struct dev_exception_item ex;
  531. int rc;
  532. memset(&ex, 0, sizeof(ex));
  533. ex.type = type;
  534. ex.major = major;
  535. ex.minor = minor;
  536. ex.access = access;
  537. rcu_read_lock();
  538. dev_cgroup = task_devcgroup(current);
  539. rc = may_access(dev_cgroup, &ex);
  540. rcu_read_unlock();
  541. if (!rc)
  542. return -EPERM;
  543. return 0;
  544. }
  545. int __devcgroup_inode_permission(struct inode *inode, int mask)
  546. {
  547. short type, access = 0;
  548. if (S_ISBLK(inode->i_mode))
  549. type = DEV_BLOCK;
  550. if (S_ISCHR(inode->i_mode))
  551. type = DEV_CHAR;
  552. if (mask & MAY_WRITE)
  553. access |= ACC_WRITE;
  554. if (mask & MAY_READ)
  555. access |= ACC_READ;
  556. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  557. access);
  558. }
  559. int devcgroup_inode_mknod(int mode, dev_t dev)
  560. {
  561. short type;
  562. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  563. return 0;
  564. if (S_ISBLK(mode))
  565. type = DEV_BLOCK;
  566. else
  567. type = DEV_CHAR;
  568. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  569. ACC_MKNOD);
  570. }