device_cgroup.c 12 KB

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