groups.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Supplementary group IDs
  3. */
  4. #include <linux/cred.h>
  5. #include <linux/module.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. gid_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. int i;
  61. unsigned int count = group_info->ngroups;
  62. for (i = 0; i < group_info->nblocks; i++) {
  63. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  64. unsigned int len = cp_count * sizeof(*grouplist);
  65. if (copy_to_user(grouplist, group_info->blocks[i], len))
  66. return -EFAULT;
  67. grouplist += NGROUPS_PER_BLOCK;
  68. count -= cp_count;
  69. }
  70. return 0;
  71. }
  72. /* fill a group_info from a user-space array - it must be allocated already */
  73. static int groups_from_user(struct group_info *group_info,
  74. gid_t __user *grouplist)
  75. {
  76. int i;
  77. unsigned int count = group_info->ngroups;
  78. for (i = 0; i < group_info->nblocks; i++) {
  79. unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
  80. unsigned int len = cp_count * sizeof(*grouplist);
  81. if (copy_from_user(group_info->blocks[i], grouplist, len))
  82. return -EFAULT;
  83. grouplist += NGROUPS_PER_BLOCK;
  84. count -= cp_count;
  85. }
  86. return 0;
  87. }
  88. /* a simple Shell sort */
  89. static void groups_sort(struct group_info *group_info)
  90. {
  91. int base, max, stride;
  92. int gidsetsize = group_info->ngroups;
  93. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  94. ; /* nothing */
  95. stride /= 3;
  96. while (stride) {
  97. max = gidsetsize - stride;
  98. for (base = 0; base < max; base++) {
  99. int left = base;
  100. int right = left + stride;
  101. gid_t tmp = GROUP_AT(group_info, right);
  102. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  103. GROUP_AT(group_info, right) =
  104. GROUP_AT(group_info, left);
  105. right = left;
  106. left -= stride;
  107. }
  108. GROUP_AT(group_info, right) = tmp;
  109. }
  110. stride /= 3;
  111. }
  112. }
  113. /* a simple bsearch */
  114. int groups_search(const struct group_info *group_info, gid_t grp)
  115. {
  116. unsigned int left, right;
  117. if (!group_info)
  118. return 0;
  119. left = 0;
  120. right = group_info->ngroups;
  121. while (left < right) {
  122. unsigned int mid = (left+right)/2;
  123. if (grp > GROUP_AT(group_info, mid))
  124. left = mid + 1;
  125. else if (grp < GROUP_AT(group_info, mid))
  126. right = mid;
  127. else
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. /**
  133. * set_groups - Change a group subscription in a set of credentials
  134. * @new: The newly prepared set of credentials to alter
  135. * @group_info: The group list to install
  136. *
  137. * Validate a group subscription and, if valid, insert it into a set
  138. * of credentials.
  139. */
  140. int set_groups(struct cred *new, struct group_info *group_info)
  141. {
  142. put_group_info(new->group_info);
  143. groups_sort(group_info);
  144. get_group_info(group_info);
  145. new->group_info = group_info;
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(set_groups);
  149. /**
  150. * set_current_groups - Change current's group subscription
  151. * @group_info: The group list to impose
  152. *
  153. * Validate a group subscription and, if valid, impose it upon current's task
  154. * security record.
  155. */
  156. int set_current_groups(struct group_info *group_info)
  157. {
  158. struct cred *new;
  159. int ret;
  160. new = prepare_creds();
  161. if (!new)
  162. return -ENOMEM;
  163. ret = set_groups(new, group_info);
  164. if (ret < 0) {
  165. abort_creds(new);
  166. return ret;
  167. }
  168. return commit_creds(new);
  169. }
  170. EXPORT_SYMBOL(set_current_groups);
  171. SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist)
  172. {
  173. const struct cred *cred = current_cred();
  174. int i;
  175. if (gidsetsize < 0)
  176. return -EINVAL;
  177. /* no need to grab task_lock here; it cannot change */
  178. i = cred->group_info->ngroups;
  179. if (gidsetsize) {
  180. if (i > gidsetsize) {
  181. i = -EINVAL;
  182. goto out;
  183. }
  184. if (groups_to_user(grouplist, cred->group_info)) {
  185. i = -EFAULT;
  186. goto out;
  187. }
  188. }
  189. out:
  190. return i;
  191. }
  192. /*
  193. * SMP: Our groups are copy-on-write. We can set them safely
  194. * without another task interfering.
  195. */
  196. SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
  197. {
  198. struct group_info *group_info;
  199. int retval;
  200. if (!nsown_capable(CAP_SETGID))
  201. return -EPERM;
  202. if ((unsigned)gidsetsize > NGROUPS_MAX)
  203. return -EINVAL;
  204. group_info = groups_alloc(gidsetsize);
  205. if (!group_info)
  206. return -ENOMEM;
  207. retval = groups_from_user(group_info, grouplist);
  208. if (retval) {
  209. put_group_info(group_info);
  210. return retval;
  211. }
  212. retval = set_current_groups(group_info);
  213. put_group_info(group_info);
  214. return retval;
  215. }
  216. /*
  217. * Check whether we're fsgid/egid or in the supplemental group..
  218. */
  219. int in_group_p(gid_t grp)
  220. {
  221. const struct cred *cred = current_cred();
  222. int retval = 1;
  223. if (grp != cred->fsgid)
  224. retval = groups_search(cred->group_info, grp);
  225. return retval;
  226. }
  227. EXPORT_SYMBOL(in_group_p);
  228. int in_egroup_p(gid_t grp)
  229. {
  230. const struct cred *cred = current_cred();
  231. int retval = 1;
  232. if (grp != cred->egid)
  233. retval = groups_search(cred->group_info, grp);
  234. return retval;
  235. }
  236. EXPORT_SYMBOL(in_egroup_p);