device_cgroup.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 13
  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, "%u", 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))
  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. wh.major = ~0;
  337. wh.minor = ~0;
  338. goto handle;
  339. case 'b':
  340. wh.type = DEV_BLOCK;
  341. break;
  342. case 'c':
  343. wh.type = DEV_CHAR;
  344. break;
  345. default:
  346. retval = -EINVAL;
  347. goto out2;
  348. }
  349. b++;
  350. if (!isspace(*b)) {
  351. retval = -EINVAL;
  352. goto out2;
  353. }
  354. b++;
  355. if (*b == '*') {
  356. wh.major = ~0;
  357. b++;
  358. } else if (isdigit(*b)) {
  359. wh.major = 0;
  360. while (isdigit(*b)) {
  361. wh.major = wh.major*10+(*b-'0');
  362. b++;
  363. }
  364. } else {
  365. retval = -EINVAL;
  366. goto out2;
  367. }
  368. if (*b != ':') {
  369. retval = -EINVAL;
  370. goto out2;
  371. }
  372. b++;
  373. /* read minor */
  374. if (*b == '*') {
  375. wh.minor = ~0;
  376. b++;
  377. } else if (isdigit(*b)) {
  378. wh.minor = 0;
  379. while (isdigit(*b)) {
  380. wh.minor = wh.minor*10+(*b-'0');
  381. b++;
  382. }
  383. } else {
  384. retval = -EINVAL;
  385. goto out2;
  386. }
  387. if (!isspace(*b)) {
  388. retval = -EINVAL;
  389. goto out2;
  390. }
  391. for (b++, count = 0; count < 3; count++, b++) {
  392. switch (*b) {
  393. case 'r':
  394. wh.access |= ACC_READ;
  395. break;
  396. case 'w':
  397. wh.access |= ACC_WRITE;
  398. break;
  399. case 'm':
  400. wh.access |= ACC_MKNOD;
  401. break;
  402. case '\n':
  403. case '\0':
  404. count = 3;
  405. break;
  406. default:
  407. retval = -EINVAL;
  408. goto out2;
  409. }
  410. }
  411. handle:
  412. retval = 0;
  413. switch (filetype) {
  414. case DEVCG_ALLOW:
  415. if (!parent_has_perm(cgroup, &wh))
  416. retval = -EPERM;
  417. else
  418. retval = dev_whitelist_add(devcgroup, &wh);
  419. break;
  420. case DEVCG_DENY:
  421. dev_whitelist_rm(devcgroup, &wh);
  422. break;
  423. default:
  424. retval = -EINVAL;
  425. goto out2;
  426. }
  427. if (retval == 0)
  428. retval = nbytes;
  429. out2:
  430. cgroup_unlock();
  431. out1:
  432. kfree(buffer);
  433. return retval;
  434. }
  435. static struct cftype dev_cgroup_files[] = {
  436. {
  437. .name = "allow",
  438. .write = devcgroup_access_write,
  439. .private = DEVCG_ALLOW,
  440. },
  441. {
  442. .name = "deny",
  443. .write = devcgroup_access_write,
  444. .private = DEVCG_DENY,
  445. },
  446. {
  447. .name = "list",
  448. .read_seq_string = devcgroup_seq_read,
  449. .private = DEVCG_LIST,
  450. },
  451. };
  452. static int devcgroup_populate(struct cgroup_subsys *ss,
  453. struct cgroup *cgroup)
  454. {
  455. return cgroup_add_files(cgroup, ss, dev_cgroup_files,
  456. ARRAY_SIZE(dev_cgroup_files));
  457. }
  458. struct cgroup_subsys devices_subsys = {
  459. .name = "devices",
  460. .can_attach = devcgroup_can_attach,
  461. .create = devcgroup_create,
  462. .destroy = devcgroup_destroy,
  463. .populate = devcgroup_populate,
  464. .subsys_id = devices_subsys_id,
  465. };
  466. int devcgroup_inode_permission(struct inode *inode, int mask)
  467. {
  468. struct dev_cgroup *dev_cgroup;
  469. struct dev_whitelist_item *wh;
  470. dev_t device = inode->i_rdev;
  471. if (!device)
  472. return 0;
  473. if (!S_ISBLK(inode->i_mode) && !S_ISCHR(inode->i_mode))
  474. return 0;
  475. dev_cgroup = css_to_devcgroup(task_subsys_state(current,
  476. devices_subsys_id));
  477. if (!dev_cgroup)
  478. return 0;
  479. spin_lock(&dev_cgroup->lock);
  480. list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
  481. if (wh->type & DEV_ALL)
  482. goto acc_check;
  483. if ((wh->type & DEV_BLOCK) && !S_ISBLK(inode->i_mode))
  484. continue;
  485. if ((wh->type & DEV_CHAR) && !S_ISCHR(inode->i_mode))
  486. continue;
  487. if (wh->major != ~0 && wh->major != imajor(inode))
  488. continue;
  489. if (wh->minor != ~0 && wh->minor != iminor(inode))
  490. continue;
  491. acc_check:
  492. if ((mask & MAY_WRITE) && !(wh->access & ACC_WRITE))
  493. continue;
  494. if ((mask & MAY_READ) && !(wh->access & ACC_READ))
  495. continue;
  496. spin_unlock(&dev_cgroup->lock);
  497. return 0;
  498. }
  499. spin_unlock(&dev_cgroup->lock);
  500. return -EPERM;
  501. }
  502. int devcgroup_inode_mknod(int mode, dev_t dev)
  503. {
  504. struct dev_cgroup *dev_cgroup;
  505. struct dev_whitelist_item *wh;
  506. dev_cgroup = css_to_devcgroup(task_subsys_state(current,
  507. devices_subsys_id));
  508. if (!dev_cgroup)
  509. return 0;
  510. spin_lock(&dev_cgroup->lock);
  511. list_for_each_entry(wh, &dev_cgroup->whitelist, list) {
  512. if (wh->type & DEV_ALL)
  513. goto acc_check;
  514. if ((wh->type & DEV_BLOCK) && !S_ISBLK(mode))
  515. continue;
  516. if ((wh->type & DEV_CHAR) && !S_ISCHR(mode))
  517. continue;
  518. if (wh->major != ~0 && wh->major != MAJOR(dev))
  519. continue;
  520. if (wh->minor != ~0 && wh->minor != MINOR(dev))
  521. continue;
  522. acc_check:
  523. if (!(wh->access & ACC_MKNOD))
  524. continue;
  525. spin_unlock(&dev_cgroup->lock);
  526. return 0;
  527. }
  528. spin_unlock(&dev_cgroup->lock);
  529. return -EPERM;
  530. }