device_cgroup.c 12 KB

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