device_cgroup.c 13 KB

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