device_cgroup.c 12 KB

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