device_cgroup.c 13 KB

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