device_cgroup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. * whitelist locking rules:
  25. * hold devcgroup_mutex for update/read.
  26. * hold rcu_read_lock() for read.
  27. */
  28. struct dev_whitelist_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 whitelist;
  38. bool deny_all;
  39. };
  40. static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
  41. {
  42. return container_of(s, struct dev_cgroup, css);
  43. }
  44. static inline struct dev_cgroup *cgroup_to_devcgroup(struct cgroup *cgroup)
  45. {
  46. return css_to_devcgroup(cgroup_subsys_state(cgroup, devices_subsys_id));
  47. }
  48. static inline struct dev_cgroup *task_devcgroup(struct task_struct *task)
  49. {
  50. return css_to_devcgroup(task_subsys_state(task, devices_subsys_id));
  51. }
  52. struct cgroup_subsys devices_subsys;
  53. static int devcgroup_can_attach(struct cgroup *new_cgrp,
  54. struct cgroup_taskset *set)
  55. {
  56. struct task_struct *task = cgroup_taskset_first(set);
  57. if (current != task && !capable(CAP_SYS_ADMIN))
  58. return -EPERM;
  59. return 0;
  60. }
  61. /*
  62. * called under devcgroup_mutex
  63. */
  64. static int dev_whitelist_copy(struct list_head *dest, struct list_head *orig)
  65. {
  66. struct dev_whitelist_item *wh, *tmp, *new;
  67. list_for_each_entry(wh, orig, list) {
  68. new = kmemdup(wh, sizeof(*wh), GFP_KERNEL);
  69. if (!new)
  70. goto free_and_exit;
  71. list_add_tail(&new->list, dest);
  72. }
  73. return 0;
  74. free_and_exit:
  75. list_for_each_entry_safe(wh, tmp, dest, list) {
  76. list_del(&wh->list);
  77. kfree(wh);
  78. }
  79. return -ENOMEM;
  80. }
  81. /* Stupid prototype - don't bother combining existing entries */
  82. /*
  83. * called under devcgroup_mutex
  84. */
  85. static int dev_whitelist_add(struct dev_cgroup *dev_cgroup,
  86. struct dev_whitelist_item *wh)
  87. {
  88. struct dev_whitelist_item *whcopy, *walk;
  89. whcopy = kmemdup(wh, sizeof(*wh), GFP_KERNEL);
  90. if (!whcopy)
  91. return -ENOMEM;
  92. list_for_each_entry(walk, &dev_cgroup->whitelist, list) {
  93. if (walk->type != wh->type)
  94. continue;
  95. if (walk->major != wh->major)
  96. continue;
  97. if (walk->minor != wh->minor)
  98. continue;
  99. walk->access |= wh->access;
  100. kfree(whcopy);
  101. whcopy = NULL;
  102. }
  103. if (whcopy != NULL)
  104. list_add_tail_rcu(&whcopy->list, &dev_cgroup->whitelist);
  105. return 0;
  106. }
  107. /*
  108. * called under devcgroup_mutex
  109. */
  110. static void dev_whitelist_rm(struct dev_cgroup *dev_cgroup,
  111. struct dev_whitelist_item *wh)
  112. {
  113. struct dev_whitelist_item *walk, *tmp;
  114. list_for_each_entry_safe(walk, tmp, &dev_cgroup->whitelist, list) {
  115. if (walk->type == DEV_ALL)
  116. goto remove;
  117. if (walk->type != wh->type)
  118. continue;
  119. if (walk->major != ~0 && walk->major != wh->major)
  120. continue;
  121. if (walk->minor != ~0 && walk->minor != wh->minor)
  122. continue;
  123. remove:
  124. walk->access &= ~wh->access;
  125. if (!walk->access) {
  126. list_del_rcu(&walk->list);
  127. kfree_rcu(walk, rcu);
  128. }
  129. }
  130. }
  131. /**
  132. * dev_whitelist_clean - frees all entries of the whitelist
  133. * @dev_cgroup: dev_cgroup with the whitelist to be cleaned
  134. *
  135. * called under devcgroup_mutex
  136. */
  137. static void dev_whitelist_clean(struct dev_cgroup *dev_cgroup)
  138. {
  139. struct dev_whitelist_item *wh, *tmp;
  140. list_for_each_entry_safe(wh, tmp, &dev_cgroup->whitelist, list) {
  141. list_del(&wh->list);
  142. kfree(wh);
  143. }
  144. }
  145. /*
  146. * called from kernel/cgroup.c with cgroup_lock() held.
  147. */
  148. static struct cgroup_subsys_state *devcgroup_create(struct cgroup *cgroup)
  149. {
  150. struct dev_cgroup *dev_cgroup, *parent_dev_cgroup;
  151. struct cgroup *parent_cgroup;
  152. int ret;
  153. dev_cgroup = kzalloc(sizeof(*dev_cgroup), GFP_KERNEL);
  154. if (!dev_cgroup)
  155. return ERR_PTR(-ENOMEM);
  156. INIT_LIST_HEAD(&dev_cgroup->whitelist);
  157. parent_cgroup = cgroup->parent;
  158. if (parent_cgroup == NULL) {
  159. struct dev_whitelist_item *wh;
  160. wh = kmalloc(sizeof(*wh), GFP_KERNEL);
  161. if (!wh) {
  162. kfree(dev_cgroup);
  163. return ERR_PTR(-ENOMEM);
  164. }
  165. wh->minor = wh->major = ~0;
  166. wh->type = DEV_ALL;
  167. wh->access = ACC_MASK;
  168. dev_cgroup->deny_all = false;
  169. list_add(&wh->list, &dev_cgroup->whitelist);
  170. } else {
  171. parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
  172. mutex_lock(&devcgroup_mutex);
  173. ret = dev_whitelist_copy(&dev_cgroup->whitelist,
  174. &parent_dev_cgroup->whitelist);
  175. dev_cgroup->deny_all = parent_dev_cgroup->deny_all;
  176. mutex_unlock(&devcgroup_mutex);
  177. if (ret) {
  178. kfree(dev_cgroup);
  179. return ERR_PTR(ret);
  180. }
  181. }
  182. return &dev_cgroup->css;
  183. }
  184. static void devcgroup_destroy(struct cgroup *cgroup)
  185. {
  186. struct dev_cgroup *dev_cgroup;
  187. dev_cgroup = cgroup_to_devcgroup(cgroup);
  188. dev_whitelist_clean(dev_cgroup);
  189. kfree(dev_cgroup);
  190. }
  191. #define DEVCG_ALLOW 1
  192. #define DEVCG_DENY 2
  193. #define DEVCG_LIST 3
  194. #define MAJMINLEN 13
  195. #define ACCLEN 4
  196. static void set_access(char *acc, short access)
  197. {
  198. int idx = 0;
  199. memset(acc, 0, ACCLEN);
  200. if (access & ACC_READ)
  201. acc[idx++] = 'r';
  202. if (access & ACC_WRITE)
  203. acc[idx++] = 'w';
  204. if (access & ACC_MKNOD)
  205. acc[idx++] = 'm';
  206. }
  207. static char type_to_char(short type)
  208. {
  209. if (type == DEV_ALL)
  210. return 'a';
  211. if (type == DEV_CHAR)
  212. return 'c';
  213. if (type == DEV_BLOCK)
  214. return 'b';
  215. return 'X';
  216. }
  217. static void set_majmin(char *str, unsigned m)
  218. {
  219. if (m == ~0)
  220. strcpy(str, "*");
  221. else
  222. sprintf(str, "%u", m);
  223. }
  224. static int devcgroup_seq_read(struct cgroup *cgroup, struct cftype *cft,
  225. struct seq_file *m)
  226. {
  227. struct dev_cgroup *devcgroup = cgroup_to_devcgroup(cgroup);
  228. struct dev_whitelist_item *wh;
  229. char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
  230. rcu_read_lock();
  231. list_for_each_entry_rcu(wh, &devcgroup->whitelist, list) {
  232. set_access(acc, wh->access);
  233. set_majmin(maj, wh->major);
  234. set_majmin(min, wh->minor);
  235. seq_printf(m, "%c %s:%s %s\n", type_to_char(wh->type),
  236. maj, min, acc);
  237. }
  238. rcu_read_unlock();
  239. return 0;
  240. }
  241. /*
  242. * may_access_whitelist:
  243. * does the access granted to dev_cgroup c contain the access
  244. * requested in whitelist item refwh.
  245. * return 1 if yes, 0 if no.
  246. * call with devcgroup_mutex held
  247. */
  248. static int may_access_whitelist(struct dev_cgroup *c,
  249. struct dev_whitelist_item *refwh)
  250. {
  251. struct dev_whitelist_item *whitem;
  252. list_for_each_entry(whitem, &c->whitelist, list) {
  253. if (whitem->type & DEV_ALL)
  254. return 1;
  255. if ((refwh->type & DEV_BLOCK) && !(whitem->type & DEV_BLOCK))
  256. continue;
  257. if ((refwh->type & DEV_CHAR) && !(whitem->type & DEV_CHAR))
  258. continue;
  259. if (whitem->major != ~0 && whitem->major != refwh->major)
  260. continue;
  261. if (whitem->minor != ~0 && whitem->minor != refwh->minor)
  262. continue;
  263. if (refwh->access & (~whitem->access))
  264. continue;
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * parent_has_perm:
  271. * when adding a new allow rule to a device whitelist, the rule
  272. * must be allowed in the parent device
  273. */
  274. static int parent_has_perm(struct dev_cgroup *childcg,
  275. struct dev_whitelist_item *wh)
  276. {
  277. struct cgroup *pcg = childcg->css.cgroup->parent;
  278. struct dev_cgroup *parent;
  279. if (!pcg)
  280. return 1;
  281. parent = cgroup_to_devcgroup(pcg);
  282. return may_access_whitelist(parent, wh);
  283. }
  284. /*
  285. * Modify the whitelist using allow/deny rules.
  286. * CAP_SYS_ADMIN is needed for this. It's at least separate from CAP_MKNOD
  287. * so we can give a container CAP_MKNOD to let it create devices but not
  288. * modify the whitelist.
  289. * It seems likely we'll want to add a CAP_CONTAINER capability to allow
  290. * us to also grant CAP_SYS_ADMIN to containers without giving away the
  291. * device whitelist controls, but for now we'll stick with CAP_SYS_ADMIN
  292. *
  293. * Taking rules away is always allowed (given CAP_SYS_ADMIN). Granting
  294. * new access is only allowed if you're in the top-level cgroup, or your
  295. * parent cgroup has the access you're asking for.
  296. */
  297. static int devcgroup_update_access(struct dev_cgroup *devcgroup,
  298. int filetype, const char *buffer)
  299. {
  300. const char *b;
  301. char *endp;
  302. int count;
  303. struct dev_whitelist_item wh;
  304. if (!capable(CAP_SYS_ADMIN))
  305. return -EPERM;
  306. memset(&wh, 0, sizeof(wh));
  307. b = buffer;
  308. switch (*b) {
  309. case 'a':
  310. wh.type = DEV_ALL;
  311. wh.access = ACC_MASK;
  312. wh.major = ~0;
  313. wh.minor = ~0;
  314. goto handle;
  315. case 'b':
  316. wh.type = DEV_BLOCK;
  317. break;
  318. case 'c':
  319. wh.type = DEV_CHAR;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. b++;
  325. if (!isspace(*b))
  326. return -EINVAL;
  327. b++;
  328. if (*b == '*') {
  329. wh.major = ~0;
  330. b++;
  331. } else if (isdigit(*b)) {
  332. wh.major = simple_strtoul(b, &endp, 10);
  333. b = endp;
  334. } else {
  335. return -EINVAL;
  336. }
  337. if (*b != ':')
  338. return -EINVAL;
  339. b++;
  340. /* read minor */
  341. if (*b == '*') {
  342. wh.minor = ~0;
  343. b++;
  344. } else if (isdigit(*b)) {
  345. wh.minor = simple_strtoul(b, &endp, 10);
  346. b = endp;
  347. } else {
  348. return -EINVAL;
  349. }
  350. if (!isspace(*b))
  351. return -EINVAL;
  352. for (b++, count = 0; count < 3; count++, b++) {
  353. switch (*b) {
  354. case 'r':
  355. wh.access |= ACC_READ;
  356. break;
  357. case 'w':
  358. wh.access |= ACC_WRITE;
  359. break;
  360. case 'm':
  361. wh.access |= ACC_MKNOD;
  362. break;
  363. case '\n':
  364. case '\0':
  365. count = 3;
  366. break;
  367. default:
  368. return -EINVAL;
  369. }
  370. }
  371. handle:
  372. switch (filetype) {
  373. case DEVCG_ALLOW:
  374. if (!parent_has_perm(devcgroup, &wh))
  375. return -EPERM;
  376. devcgroup->deny_all = false;
  377. return dev_whitelist_add(devcgroup, &wh);
  378. case DEVCG_DENY:
  379. dev_whitelist_rm(devcgroup, &wh);
  380. devcgroup->deny_all = true;
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. return 0;
  386. }
  387. static int devcgroup_access_write(struct cgroup *cgrp, struct cftype *cft,
  388. const char *buffer)
  389. {
  390. int retval;
  391. mutex_lock(&devcgroup_mutex);
  392. retval = devcgroup_update_access(cgroup_to_devcgroup(cgrp),
  393. cft->private, buffer);
  394. mutex_unlock(&devcgroup_mutex);
  395. return retval;
  396. }
  397. static struct cftype dev_cgroup_files[] = {
  398. {
  399. .name = "allow",
  400. .write_string = devcgroup_access_write,
  401. .private = DEVCG_ALLOW,
  402. },
  403. {
  404. .name = "deny",
  405. .write_string = devcgroup_access_write,
  406. .private = DEVCG_DENY,
  407. },
  408. {
  409. .name = "list",
  410. .read_seq_string = devcgroup_seq_read,
  411. .private = DEVCG_LIST,
  412. },
  413. { } /* terminate */
  414. };
  415. struct cgroup_subsys devices_subsys = {
  416. .name = "devices",
  417. .can_attach = devcgroup_can_attach,
  418. .create = devcgroup_create,
  419. .destroy = devcgroup_destroy,
  420. .subsys_id = devices_subsys_id,
  421. .base_cftypes = dev_cgroup_files,
  422. /*
  423. * While devices cgroup has the rudimentary hierarchy support which
  424. * checks the parent's restriction, it doesn't properly propagates
  425. * config changes in ancestors to their descendents. A child
  426. * should only be allowed to add more restrictions to the parent's
  427. * configuration. Fix it and remove the following.
  428. */
  429. .broken_hierarchy = true,
  430. };
  431. int __devcgroup_inode_permission(struct inode *inode, int mask)
  432. {
  433. struct dev_cgroup *dev_cgroup;
  434. struct dev_whitelist_item *wh;
  435. rcu_read_lock();
  436. dev_cgroup = task_devcgroup(current);
  437. list_for_each_entry_rcu(wh, &dev_cgroup->whitelist, list) {
  438. if (wh->type & DEV_ALL)
  439. goto found;
  440. if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
  441. continue;
  442. if ((wh->type & DEV_CHAR) && !S_ISCHR(inode->i_mode))
  443. continue;
  444. if (wh->major != ~0 && wh->major != imajor(inode))
  445. continue;
  446. if (wh->minor != ~0 && wh->minor != iminor(inode))
  447. continue;
  448. if ((mask & MAY_WRITE) && !(wh->access & ACC_WRITE))
  449. continue;
  450. if ((mask & MAY_READ) && !(wh->access & ACC_READ))
  451. continue;
  452. found:
  453. rcu_read_unlock();
  454. return 0;
  455. }
  456. rcu_read_unlock();
  457. return -EPERM;
  458. }
  459. int devcgroup_inode_mknod(int mode, dev_t dev)
  460. {
  461. struct dev_cgroup *dev_cgroup;
  462. struct dev_whitelist_item *wh;
  463. if (!S_ISBLK(mode) && !S_ISCHR(mode))
  464. return 0;
  465. rcu_read_lock();
  466. dev_cgroup = task_devcgroup(current);
  467. list_for_each_entry_rcu(wh, &dev_cgroup->whitelist, list) {
  468. if (wh->type & DEV_ALL)
  469. goto found;
  470. if ((wh->type & DEV_BLOCK) && !S_ISBLK(mode))
  471. continue;
  472. if ((wh->type & DEV_CHAR) && !S_ISCHR(mode))
  473. continue;
  474. if (wh->major != ~0 && wh->major != MAJOR(dev))
  475. continue;
  476. if (wh->minor != ~0 && wh->minor != MINOR(dev))
  477. continue;
  478. if (!(wh->access & ACC_MKNOD))
  479. continue;
  480. found:
  481. rcu_read_unlock();
  482. return 0;
  483. }
  484. rcu_read_unlock();
  485. return -EPERM;
  486. }