device_cgroup.c 14 KB

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