groups.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Supplementary group IDs
  3. */
  4. #include <linux/cred.h>
  5. #include <linux/export.h>
  6. #include <linux/slab.h>
  7. #include <linux/security.h>
  8. #include <linux/syscalls.h>
  9. #include <asm/uaccess.h>
  10. /* init to 2 - one for init_task, one to ensure it is never freed */
  11. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  12. struct group_info *groups_alloc(int gidsetsize)
  13. {
  14. struct group_info *group_info;
  15. int nblocks;
  16. int i;
  17. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  18. /* Make sure we always allocate at least one indirect block pointer */
  19. nblocks = nblocks ? : 1;
  20. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  21. if (!group_info)
  22. return NULL;
  23. group_info->ngroups = gidsetsize;
  24. group_info->nblocks = nblocks;
  25. atomic_set(&group_info->usage, 1);
  26. if (gidsetsize <= NGROUPS_SMALL)
  27. group_info->blocks[0] = group_info->small_block;
  28. else {
  29. for (i = 0; i < nblocks; i++) {
  30. kgid_t *b;
  31. b = (void *)__get_free_page(GFP_USER);
  32. if (!b)
  33. goto out_undo_partial_alloc;
  34. group_info->blocks[i] = b;
  35. }
  36. }
  37. return group_info;
  38. out_undo_partial_alloc:
  39. while (--i >= 0) {
  40. free_page((unsigned long)group_info->blocks[i]);
  41. }
  42. kfree(group_info);
  43. return NULL;
  44. }
  45. EXPORT_SYMBOL(groups_alloc);
  46. void groups_free(struct group_info *group_info)
  47. {
  48. if (group_info->blocks[0] != group_info->small_block) {
  49. int i;
  50. for (i = 0; i < group_info->nblocks; i++)
  51. free_page((unsigned long)group_info->blocks[i]);
  52. }
  53. kfree(group_info);
  54. }
  55. EXPORT_SYMBOL(groups_free);
  56. /* export the group_info to a user-space array */
  57. static int groups_to_user(gid_t __user *grouplist,
  58. const struct group_info *group_info)
  59. {
  60. struct user_namespace *user_ns = current_user_ns();
  61. int i;
  62. unsigned int count = group_info->ngroups;
  63. for (i = 0; i < count; i++) {
  64. gid_t gid;
  65. gid = from_kgid_munged(user_ns, GROUP_AT(group_info, i));
  66. if (put_user(gid, grouplist+i))
  67. return -EFAULT;
  68. }
  69. return 0;
  70. }
  71. /* fill a group_info from a user-space array - it must be allocated already */
  72. static int groups_from_user(struct group_info *group_info,
  73. gid_t __user *grouplist)
  74. {
  75. struct user_namespace *user_ns = current_user_ns();
  76. int i;
  77. unsigned int count = group_info->ngroups;
  78. for (i = 0; i < count; i++) {
  79. gid_t gid;
  80. kgid_t kgid;
  81. if (get_user(gid, grouplist+i))
  82. return -EFAULT;
  83. kgid = make_kgid(user_ns, gid);
  84. if (!gid_valid(kgid))
  85. return -EINVAL;
  86. GROUP_AT(group_info, i) = kgid;
  87. }
  88. return 0;
  89. }
  90. /* a simple Shell sort */
  91. static void groups_sort(struct group_info *group_info)
  92. {
  93. int base, max, stride;
  94. int gidsetsize = group_info->ngroups;
  95. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  96. ; /* nothing */
  97. stride /= 3;
  98. while (stride) {
  99. max = gidsetsize - stride;
  100. for (base = 0; base < max; base++) {
  101. int left = base;
  102. int right = left + stride;
  103. kgid_t tmp = GROUP_AT(group_info, right);
  104. while (left >= 0 && gid_gt(GROUP_AT(group_info, left), tmp)) {
  105. GROUP_AT(group_info, right) =
  106. GROUP_AT(group_info, left);
  107. right = left;
  108. left -= stride;
  109. }
  110. GROUP_AT(group_info, right) = tmp;
  111. }
  112. stride /= 3;
  113. }
  114. }
  115. /* a simple bsearch */
  116. int groups_search(const struct group_info *group_info, kgid_t grp)
  117. {
  118. unsigned int left, right;
  119. if (!group_info)
  120. return 0;
  121. left = 0;
  122. right = group_info->ngroups;
  123. while (left < right) {
  124. unsigned int mid = (left+right)/2;
  125. if (gid_gt(grp, GROUP_AT(group_info, mid)))
  126. left = mid + 1;
  127. else if (gid_lt(grp, GROUP_AT(group_info, mid)))
  128. right = mid;
  129. else
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. /**
  135. * set_groups_sorted - Change a group subscription in a set of credentials
  136. * <at> new: The newly prepared set of credentials to alter
  137. * <at> group_info: The group list to install; must be sorted
  138. */
  139. static void set_groups_sorted(struct cred *new, struct group_info *group_info)
  140. {
  141. put_group_info(new->group_info);
  142. get_group_info(group_info);
  143. new->group_info = group_info;
  144. }
  145. /**
  146. * set_groups - Change a group subscription in a set of credentials
  147. * @new: The newly prepared set of credentials to alter
  148. * @group_info: The group list to install
  149. *
  150. * Validate a group subscription and, if valid, insert it into a set
  151. * of credentials.
  152. */
  153. int set_groups(struct cred *new, struct group_info *group_info)
  154. {
  155. groups_sort(group_info);
  156. set_groups_sorted(new, group_info);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(set_groups);
  160. /**
  161. * set_current_groups - Change current's group subscription
  162. * @group_info: The group list to impose
  163. *
  164. * Validate a group subscription and, if valid, impose it upon current's task
  165. * security record.
  166. */
  167. int set_current_groups(struct group_info *group_info)
  168. {
  169. struct cred *new;
  170. int ret;
  171. new = prepare_creds();
  172. if (!new)
  173. return -ENOMEM;
  174. ret = set_groups(new, group_info);
  175. if (ret < 0) {
  176. abort_creds(new);
  177. return ret;
  178. }
  179. return commit_creds(new);
  180. }
  181. EXPORT_SYMBOL(set_current_groups);
  182. SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist)
  183. {
  184. const struct cred *cred = current_cred();
  185. int i;
  186. if (gidsetsize < 0)
  187. return -EINVAL;
  188. /* no need to grab task_lock here; it cannot change */
  189. i = cred->group_info->ngroups;
  190. if (gidsetsize) {
  191. if (i > gidsetsize) {
  192. i = -EINVAL;
  193. goto out;
  194. }
  195. if (groups_to_user(grouplist, cred->group_info)) {
  196. i = -EFAULT;
  197. goto out;
  198. }
  199. }
  200. out:
  201. return i;
  202. }
  203. /*
  204. * SMP: Our groups are copy-on-write. We can set them safely
  205. * without another task interfering.
  206. */
  207. SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
  208. {
  209. struct group_info *group_info;
  210. int retval;
  211. if (!nsown_capable(CAP_SETGID))
  212. return -EPERM;
  213. if ((unsigned)gidsetsize > NGROUPS_MAX)
  214. return -EINVAL;
  215. group_info = groups_alloc(gidsetsize);
  216. if (!group_info)
  217. return -ENOMEM;
  218. retval = groups_from_user(group_info, grouplist);
  219. if (retval) {
  220. put_group_info(group_info);
  221. return retval;
  222. }
  223. retval = set_current_groups(group_info);
  224. put_group_info(group_info);
  225. return retval;
  226. }
  227. /*
  228. * Check whether we're fsgid/egid or in the supplemental group..
  229. */
  230. int in_group_p(kgid_t grp)
  231. {
  232. const struct cred *cred = current_cred();
  233. int retval = 1;
  234. if (!gid_eq(grp, cred->fsgid))
  235. retval = groups_search(cred->group_info, grp);
  236. return retval;
  237. }
  238. EXPORT_SYMBOL(in_group_p);
  239. int in_egroup_p(kgid_t grp)
  240. {
  241. const struct cred *cred = current_cred();
  242. int retval = 1;
  243. if (!gid_eq(grp, cred->egid))
  244. retval = groups_search(cred->group_info, grp);
  245. return retval;
  246. }
  247. EXPORT_SYMBOL(in_egroup_p);