device_cgroup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 bool 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 deny + exception list:
  286. * the new exception *should* match the exceptions
  287. * - the dev cgroup has its default policy to allow + exception list:
  288. * the new exception should *not* match any of the exceptions
  289. */
  290. if (dev_cgroup->behavior == DEVCG_DEFAULT_DENY) {
  291. if (match)
  292. return true;
  293. } else {
  294. if (!match)
  295. return true;
  296. }
  297. return false;
  298. }
  299. /*
  300. * parent_has_perm:
  301. * when adding a new allow rule to a device exception list, the rule
  302. * must be allowed in the parent device
  303. */
  304. static int parent_has_perm(struct dev_cgroup *childcg,
  305. struct dev_exception_item *ex)
  306. {
  307. struct cgroup *pcg = childcg->css.cgroup->parent;
  308. struct dev_cgroup *parent;
  309. if (!pcg)
  310. return 1;
  311. parent = cgroup_to_devcgroup(pcg);
  312. return may_access(parent, ex);
  313. }
  314. /**
  315. * may_allow_all - checks if it's possible to change the behavior to
  316. * allow based on parent's rules.
  317. * @parent: device cgroup's parent
  318. * returns: != 0 in case it's allowed, 0 otherwise
  319. */
  320. static inline int may_allow_all(struct dev_cgroup *parent)
  321. {
  322. if (!parent)
  323. return 1;
  324. return parent->behavior == DEVCG_DEFAULT_ALLOW;
  325. }
  326. /*
  327. * Modify the exception list using allow/deny rules.
  328. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  329. * so we can give a container CAP_MKNOD to let it create devices but not
  330. * modify the exception list.
  331. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  332. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  333. * device exception list controls, but for now we'll stick with CAP_SYS_ADMIN
  334. *
  335. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  336. * new access is only allowed if you're in the top-level cgroup, or your
  337. * parent cgroup has the access you're asking for.
  338. */
  339. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  340. int filetype, const char *buffer)
  341. {
  342. const char *b;
  343. char temp[12]; /* 11 + 1 characters needed for a u32 */
  344. int count, rc;
  345. struct dev_exception_item ex;
  346. struct cgroup *p = devcgroup->css.cgroup;
  347. struct dev_cgroup *parent = NULL;
  348. if (!capable(CAP_SYS_ADMIN))
  349. return -EPERM;
  350. if (p->parent)
  351. parent = cgroup_to_devcgroup(p->parent);
  352. memset(&ex, 0, sizeof(ex));
  353. b = buffer;
  354. switch (*b) {
  355. case 'a':
  356. switch (filetype) {
  357. case DEVCG_ALLOW:
  358. if (!may_allow_all(parent))
  359. return -EPERM;
  360. dev_exception_clean(devcgroup);
  361. devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
  362. if (!parent)
  363. break;
  364. rc = dev_exceptions_copy(&devcgroup->exceptions,
  365. &parent->exceptions);
  366. if (rc)
  367. return rc;
  368. break;
  369. case DEVCG_DENY:
  370. dev_exception_clean(devcgroup);
  371. devcgroup->behavior = DEVCG_DEFAULT_DENY;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. return 0;
  377. case 'b':
  378. ex.type = DEV_BLOCK;
  379. break;
  380. case 'c':
  381. ex.type = DEV_CHAR;
  382. break;
  383. default:
  384. return -EINVAL;
  385. }
  386. b++;
  387. if (!isspace(*b))
  388. return -EINVAL;
  389. b++;
  390. if (*b == '*') {
  391. ex.major = ~0;
  392. b++;
  393. } else if (isdigit(*b)) {
  394. memset(temp, 0, sizeof(temp));
  395. for (count = 0; count < sizeof(temp) - 1; count++) {
  396. temp[count] = *b;
  397. b++;
  398. if (!isdigit(*b))
  399. break;
  400. }
  401. rc = kstrtou32(temp, 10, &ex.major);
  402. if (rc)
  403. return -EINVAL;
  404. } else {
  405. return -EINVAL;
  406. }
  407. if (*b != ':')
  408. return -EINVAL;
  409. b++;
  410. /* read minor */
  411. if (*b == '*') {
  412. ex.minor = ~0;
  413. b++;
  414. } else if (isdigit(*b)) {
  415. memset(temp, 0, sizeof(temp));
  416. for (count = 0; count < sizeof(temp) - 1; count++) {
  417. temp[count] = *b;
  418. b++;
  419. if (!isdigit(*b))
  420. break;
  421. }
  422. rc = kstrtou32(temp, 10, &ex.minor);
  423. if (rc)
  424. return -EINVAL;
  425. } else {
  426. return -EINVAL;
  427. }
  428. if (!isspace(*b))
  429. return -EINVAL;
  430. for (b++, count = 0; count < 3; count++, b++) {
  431. switch (*b) {
  432. case 'r':
  433. ex.access |= ACC_READ;
  434. break;
  435. case 'w':
  436. ex.access |= ACC_WRITE;
  437. break;
  438. case 'm':
  439. ex.access |= ACC_MKNOD;
  440. break;
  441. case '\n':
  442. case '\0':
  443. count = 3;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. }
  449. switch (filetype) {
  450. case DEVCG_ALLOW:
  451. if (!parent_has_perm(devcgroup, &ex))
  452. return -EPERM;
  453. /*
  454. * If the default policy is to allow by default, try to remove
  455. * an matching exception instead. And be silent about it: we
  456. * don't want to break compatibility
  457. */
  458. if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
  459. dev_exception_rm(devcgroup, &ex);
  460. return 0;
  461. }
  462. return dev_exception_add(devcgroup, &ex);
  463. case DEVCG_DENY:
  464. /*
  465. * If the default policy is to deny by default, try to remove
  466. * an matching exception instead. And be silent about it: we
  467. * don't want to break compatibility
  468. */
  469. if (devcgroup->behavior == DEVCG_DEFAULT_DENY) {
  470. dev_exception_rm(devcgroup, &ex);
  471. return 0;
  472. }
  473. return dev_exception_add(devcgroup, &ex);
  474. default:
  475. return -EINVAL;
  476. }
  477. return 0;
  478. }
  479. static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
  480. const char *buffer)
  481. {
  482. int retval;
  483. mutex_lock(&devcgroup_mutex);
  484. retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
  485. cft->private, buffer);
  486. mutex_unlock(&devcgroup_mutex);
  487. return retval;
  488. }
  489. static struct cftype dev_cgroup_files[] = {
  490. {
  491. .name = "allow",
  492. .write_string = devcgroup_access_write,
  493. .private = DEVCG_ALLOW,
  494. },
  495. {
  496. .name = "deny",
  497. .write_string = devcgroup_access_write,
  498. .private = DEVCG_DENY,
  499. },
  500. {
  501. .name = "list",
  502. .read_seq_string = devcgroup_seq_read,
  503. .private = DEVCG_LIST,
  504. },
  505. { } /* terminate */
  506. };
  507. struct cgroup_subsys devices_subsys = {
  508. .name = "devices",
  509. .can_attach = devcgroup_can_attach,
  510. .css_alloc = devcgroup_css_alloc,
  511. .css_free = devcgroup_css_free,
  512. .subsys_id = devices_subsys_id,
  513. .base_cftypes = dev_cgroup_files,
  514. /*
  515. * While devices cgroup has the rudimentary hierarchy support which
  516. * checks the parent's restriction, it doesn't properly propagates
  517. * config changes in ancestors to their descendents. A child
  518. * should only be allowed to add more restrictions to the parent's
  519. * configuration. Fix it and remove the following.
  520. */
  521. .broken_hierarchy = true,
  522. };
  523. /**
  524. * __devcgroup_check_permission - checks if an inode operation is permitted
  525. * @dev_cgroup: the dev cgroup to be tested against
  526. * @type: device type
  527. * @major: device major number
  528. * @minor: device minor number
  529. * @access: combination of ACC_WRITE, ACC_READ and ACC_MKNOD
  530. *
  531. * returns 0 on success, -EPERM case the operation is not permitted
  532. */
  533. static int __devcgroup_check_permission(short type, u32 major, u32 minor,
  534. short access)
  535. {
  536. struct dev_cgroup *dev_cgroup;
  537. struct dev_exception_item ex;
  538. int rc;
  539. memset(&ex, 0, sizeof(ex));
  540. ex.type = type;
  541. ex.major = major;
  542. ex.minor = minor;
  543. ex.access = access;
  544. rcu_read_lock();
  545. dev_cgroup = task_devcgroup(current);
  546. rc = may_access(dev_cgroup, &ex);
  547. rcu_read_unlock();
  548. if (!rc)
  549. return -EPERM;
  550. return 0;
  551. }
  552. int __devcgroup_inode_permission(struct inode *inode, int mask)
  553. {
  554. short type, access = 0;
  555. if (S_ISBLK(inode->i_mode))
  556. type = DEV_BLOCK;
  557. if (S_ISCHR(inode->i_mode))
  558. type = DEV_CHAR;
  559. if (mask & MAY_WRITE)
  560. access |= ACC_WRITE;
  561. if (mask & MAY_READ)
  562. access |= ACC_READ;
  563. return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
  564. access);
  565. }
  566. int devcgroup_inode_mknod(int mode, dev_t dev)
  567. {
  568. short type;
  569. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  570. return 0;
  571. if (S_ISBLK(mode))
  572. type = DEV_BLOCK;
  573. else
  574. type = DEV_CHAR;
  575. return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
  576. ACC_MKNOD);
  577. }