device_cgroup.c 11 KB

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