device_cgroup.c 15 KB

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