device_cgroup.c 13 KB

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