device_cgroup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. mutex_lock(&devcgroup_mutex);
  182. dev_exception_clean(dev_cgroup);
  183. mutex_unlock(&devcgroup_mutex);
  184. kfree(dev_cgroup);
  185. }
  186. #define DEVCG_ALLOW 1
  187. #define DEVCG_DENY 2
  188. #define DEVCG_LIST 3
  189. #define MAJMINLEN 13
  190. #define ACCLEN 4
  191. static void set_access(char *acc, short access)
  192. {
  193. int idx = 0;
  194. memset(acc, 0, ACCLEN);
  195. if (access & ACC_READ)
  196. acc[idx++] = 'r';
  197. if (access & ACC_WRITE)
  198. acc[idx++] = 'w';
  199. if (access & ACC_MKNOD)
  200. acc[idx++] = 'm';
  201. }
  202. static char type_to_char(short type)
  203. {
  204. if (type == DEV_ALL)
  205. return 'a';
  206. if (type == DEV_CHAR)
  207. return 'c';
  208. if (type == DEV_BLOCK)
  209. return 'b';
  210. return 'X';
  211. }
  212. static void set_majmin(char *str, unsigned m)
  213. {
  214. if (m == ~0)
  215. strcpy(str, "*");
  216. else
  217. sprintf(str, "%u", m);
  218. }
  219. static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
  220. struct seq_file *m)
  221. {
  222. struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
  223. struct dev_exception_item *ex;
  224. char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
  225. rcu_read_lock();
  226. /*
  227. * To preserve the compatibility:
  228. * - Only show the "all devices" when the default policy is to allow
  229. * - List the exceptions in case the default policy is to deny
  230. * This way, the file remains as a "whitelist of devices"
  231. */
  232. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  233. set_access(acc, ACC_MASK);
  234. set_majmin(maj, ~0);
  235. set_majmin(min, ~0);
  236. seq_printf(m, "%c %s:%s %s\n", type_to_char(DEV_ALL),
  237. maj, min, acc);
  238. } else {
  239. list_for_each_entry_rcu(ex, &devcgroup->exceptions, list) {
  240. set_access(acc, ex->access);
  241. set_majmin(maj, ex->major);
  242. set_majmin(min, ex->minor);
  243. seq_printf(m, "%c %s:%s %s\n", type_to_char(ex->type),
  244. maj, min, acc);
  245. }
  246. }
  247. rcu_read_unlock();
  248. return 0;
  249. }
  250. /**
  251. * may_access - verifies if a new exception is part of what is allowed
  252. * by a dev cgroup based on the default policy +
  253. * exceptions. This is used to make sure a child cgroup
  254. * won't have more privileges than its parent or to
  255. * verify if a certain access is allowed.
  256. * @dev_cgroup: dev cgroup to be tested against
  257. * @refex: new exception
  258. */
  259. static int may_access(struct dev_cgroup *dev_cgroup,
  260. struct dev_exception_item *refex)
  261. {
  262. struct dev_exception_item *ex;
  263. bool match = false;
  264. rcu_lockdep_assert(rcu_read_lock_held() ||
  265. lockdep_is_held(&devcgroup_mutex),
  266. "device_cgroup::may_access() called without proper synchronization");
  267. list_for_each_entry_rcu(ex, &dev_cgroup->exceptions, list) {
  268. if ((refex->type & DEV_BLOCK) && !(ex->type & DEV_BLOCK))
  269. continue;
  270. if ((refex->type & DEV_CHAR) && !(ex->type & DEV_CHAR))
  271. continue;
  272. if (ex->major != ~0 && ex->major != refex->major)
  273. continue;
  274. if (ex->minor != ~0 && ex->minor != refex->minor)
  275. continue;
  276. if (refex->access & (~ex->access))
  277. continue;
  278. match = true;
  279. break;
  280. }
  281. /*
  282. * In two cases we'll consider this new exception valid:
  283. * - the dev cgroup has its default policy to allow + exception list:
  284. * the new exception should *not* match any of the exceptions
  285. * (behavior == DEVCG_DEFAULT_ALLOW, !match)
  286. * - the dev cgroup has its default policy to deny + exception list:
  287. * the new exception *should* match the exceptions
  288. * (behavior == DEVCG_DEFAULT_DENY, match)
  289. */
  290. if ((dev_cgroup->behavior == DEVCG_DEFAULT_DENY) == match)
  291. return 1;
  292. return 0;
  293. }
  294. /*
  295. * parent_has_perm:
  296. * when adding a new allow rule to a device exception list, the rule
  297. * must be allowed in the parent device
  298. */
  299. static int parent_has_perm(struct dev_cgroup *childcg,
  300. struct dev_exception_item *ex)
  301. {
  302. struct cgroup *pcg = childcg->css.cgroup->parent;
  303. struct dev_cgroup *parent;
  304. if (!pcg)
  305. return 1;
  306. parent = cgroup_to_devcgroup(pcg);
  307. return may_access(parent, ex);
  308. }
  309. /**
  310. * may_allow_all - checks if it's possible to change the behavior to
  311. * allow based on parent's rules.
  312. * @parent: device cgroup's parent
  313. * returns: != 0 in case it's allowed, 0 otherwise
  314. */
  315. static inline int may_allow_all(struct dev_cgroup *parent)
  316. {
  317. if (!parent)
  318. return 1;
  319. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  320. }
  321. /*
  322. * Modify the exception list using allow/deny rules.
  323. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  324. * so we can give a container CAP_MKNOD to let it create devices but not
  325. * modify the exception list.
  326. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  327. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  328. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  329. *
  330. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  331. * new access is only allowed if you're in the top-level cgroup, or your
  332. * parent cgroup has the access you're asking for.
  333. */
  334. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  335. int filetype, const char *buffer)
  336. {
  337. const char *b;
  338. char temp[12]; /* 11 + 1 characters needed for a u32 */
  339. int count, rc;
  340. struct dev_exception_item ex;
  341. struct cgroup *p = devcgroup->css.cgroup;
  342. struct dev_cgroup *parent = NULL;
  343. if (!capable(CAP_SYS_ADMIN))
  344. return -EPERM;
  345. if (p->parent)
  346. parent = cgroup_to_devcgroup(p->parent);
  347. memset(&ex, 0, sizeof(ex));
  348. b = buffer;
  349. switch (*b) {
  350. case 'a':
  351. switch (filetype) {
  352. case DEVCG_ALLOW:
  353. if (!may_allow_all(parent))
  354. return -EPERM;
  355. dev_exception_clean(devcgroup);
  356. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  357. if (!parent)
  358. break;
  359. rc = dev_exceptions_copy(&devcgroup->exceptions,
  360. &parent->exceptions);
  361. if (rc)
  362. return rc;
  363. break;
  364. case DEVCG_DENY:
  365. dev_exception_clean(devcgroup);
  366. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. return 0;
  372. case 'b':
  373. ex.type = DEV_BLOCK;
  374. break;
  375. case 'c':
  376. ex.type = DEV_CHAR;
  377. break;
  378. default:
  379. return -EINVAL;
  380. }
  381. b++;
  382. if (!isspace(*b))
  383. return -EINVAL;
  384. b++;
  385. if (*b == '*') {
  386. ex.major = ~0;
  387. b++;
  388. } else if (isdigit(*b)) {
  389. memset(temp, 0, sizeof(temp));
  390. for (count = 0; count < sizeof(temp) - 1; count++) {
  391. temp[count] = *b;
  392. b++;
  393. if (!isdigit(*b))
  394. break;
  395. }
  396. rc = kstrtou32(temp, 10, &ex.major);
  397. if (rc)
  398. return -EINVAL;
  399. } else {
  400. return -EINVAL;
  401. }
  402. if (*b != ':')
  403. return -EINVAL;
  404. b++;
  405. /* read minor */
  406. if (*b == '*') {
  407. ex.minor = ~0;
  408. b++;
  409. } else if (isdigit(*b)) {
  410. memset(temp, 0, sizeof(temp));
  411. for (count = 0; count < sizeof(temp) - 1; count++) {
  412. temp[count] = *b;
  413. b++;
  414. if (!isdigit(*b))
  415. break;
  416. }
  417. rc = kstrtou32(temp, 10, &ex.minor);
  418. if (rc)
  419. return -EINVAL;
  420. } else {
  421. return -EINVAL;
  422. }
  423. if (!isspace(*b))
  424. return -EINVAL;
  425. for (b++, count = 0; count < 3; count++, b++) {
  426. switch (*b) {
  427. case 'r':
  428. ex.access |= ACC_READ;
  429. break;
  430. case 'w':
  431. ex.access |= ACC_WRITE;
  432. break;
  433. case 'm':
  434. ex.access |= ACC_MKNOD;
  435. break;
  436. case '\n':
  437. case '\0':
  438. count = 3;
  439. break;
  440. default:
  441. return -EINVAL;
  442. }
  443. }
  444. switch (filetype) {
  445. case DEVCG_ALLOW:
  446. if (!parent_has_perm(devcgroup, &ex))
  447. return -EPERM;
  448. /*
  449. * If the default policy is to allow by default, try to remove
  450. * an matching exception instead. And be silent about it: we
  451. * don't want to break compatibility
  452. */
  453. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  454. dev_exception_rm(devcgroup, &ex);
  455. return 0;
  456. }
  457. return dev_exception_add(devcgroup, &ex);
  458. case DEVCG_DENY:
  459. /*
  460. * If the default policy is to deny by default, try to remove
  461. * an matching exception instead. And be silent about it: we
  462. * don't want to break compatibility
  463. */
  464. if (devcgroup->behavior == DEVCG_DEFAULT_DENY) {
  465. dev_exception_rm(devcgroup, &ex);
  466. return 0;
  467. }
  468. return dev_exception_add(devcgroup, &ex);
  469. default:
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
  475. const char *buffer)
  476. {
  477. int retval;
  478. mutex_lock(&devcgroup_mutex);
  479. retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
  480. cft->private, buffer);
  481. mutex_unlock(&devcgroup_mutex);
  482. return retval;
  483. }
  484. static struct cftype dev_cgroup_files[] = {
  485. {
  486. .name = "allow",
  487. .write_string = devcgroup_access_write,
  488. .private = DEVCG_ALLOW,
  489. },
  490. {
  491. .name = "deny",
  492. .write_string = devcgroup_access_write,
  493. .private = DEVCG_DENY,
  494. },
  495. {
  496. .name = "list",
  497. .read_seq_string = devcgroup_seq_read,
  498. .private = DEVCG_LIST,
  499. },
  500. { } /* terminate */
  501. };
  502. struct cgroup_subsys devices_subsys = {
  503. .name = "devices",
  504. .can_attach = devcgroup_can_attach,
  505. .css_alloc = devcgroup_css_alloc,
  506. .css_free = devcgroup_css_free,
  507. .subsys_id = devices_subsys_id,
  508. .base_cftypes = dev_cgroup_files,
  509. /*
  510. * While devices cgroup has the rudimentary hierarchy support which
  511. * checks the parent's restriction, it doesn't properly propagates
  512. * config changes in ancestors to their descendents. A child
  513. * should only be allowed to add more restrictions to the parent's
  514. * configuration. Fix it and remove the following.
  515. */
  516. .broken_hierarchy = true,
  517. };
  518. /**
  519. * __devcgroup_check_permission - checks if an inode operation is permitted
  520. * @dev_cgroup: the dev cgroup to be tested against
  521. * @type: device type
  522. * @major: device major number
  523. * @minor: device minor number
  524. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  525. *
  526. * returns 0 on success, -EPERM case the operation is not permitted
  527. */
  528. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  529. short access)
  530. {
  531. struct dev_cgroup *dev_cgroup;
  532. struct dev_exception_item ex;
  533. int rc;
  534. memset(&ex, 0, sizeof(ex));
  535. ex.type = type;
  536. ex.major = major;
  537. ex.minor = minor;
  538. ex.access = access;
  539. rcu_read_lock();
  540. dev_cgroup = task_devcgroup(current);
  541. rc = may_access(dev_cgroup, &ex);
  542. rcu_read_unlock();
  543. if (!rc)
  544. return -EPERM;
  545. return 0;
  546. }
  547. int __devcgroup_inode_permission(struct inode *inode, int mask)
  548. {
  549. short type, access = 0;
  550. if (S_ISBLK(inode->i_mode))
  551. type = DEV_BLOCK;
  552. if (S_ISCHR(inode->i_mode))
  553. type = DEV_CHAR;
  554. if (mask & MAY_WRITE)
  555. access |= ACC_WRITE;
  556. if (mask & MAY_READ)
  557. access |= ACC_READ;
  558. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  559. access);
  560. }
  561. int devcgroup_inode_mknod(int mode, dev_t dev)
  562. {
  563. short type;
  564. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  565. return 0;
  566. if (S_ISBLK(mode))
  567. type = DEV_BLOCK;
  568. else
  569. type = DEV_CHAR;
  570. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  571. ACC_MKNOD);
  572. }