groups.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <linux/user_namespace.h>
  10. #include <linux/vmalloc.h>
  11. #include <asm/uaccess.h>
  12. struct group_info *groups_alloc(int gidsetsize)
  13. {
  14. struct group_info *gi;
  15. unsigned int len;
  16. len = sizeof(struct group_info) + sizeof(kgid_t) * gidsetsize;
  17. gi = kmalloc(len, GFP_KERNEL_ACCOUNT|__GFP_NOWARN|__GFP_NORETRY);
  18. if (!gi)
  19. gi = __vmalloc(len, GFP_KERNEL_ACCOUNT|__GFP_HIGHMEM, PAGE_KERNEL);
  20. if (!gi)
  21. return NULL;
  22. atomic_set(&gi->usage, 1);
  23. gi->ngroups = gidsetsize;
  24. return gi;
  25. }
  26. EXPORT_SYMBOL(groups_alloc);
  27. void groups_free(struct group_info *group_info)
  28. {
  29. kvfree(group_info);
  30. }
  31. EXPORT_SYMBOL(groups_free);
  32. /* export the group_info to a user-space array */
  33. static int groups_to_user(gid_t __user *grouplist,
  34. const struct group_info *group_info)
  35. {
  36. struct user_namespace *user_ns = current_user_ns();
  37. int i;
  38. unsigned int count = group_info->ngroups;
  39. for (i = 0; i < count; i++) {
  40. gid_t gid;
  41. gid = from_kgid_munged(user_ns, group_info->gid[i]);
  42. if (put_user(gid, grouplist+i))
  43. return -EFAULT;
  44. }
  45. return 0;
  46. }
  47. /* fill a group_info from a user-space array - it must be allocated already */
  48. static int groups_from_user(struct group_info *group_info,
  49. gid_t __user *grouplist)
  50. {
  51. struct user_namespace *user_ns = current_user_ns();
  52. int i;
  53. unsigned int count = group_info->ngroups;
  54. for (i = 0; i < count; i++) {
  55. gid_t gid;
  56. kgid_t kgid;
  57. if (get_user(gid, grouplist+i))
  58. return -EFAULT;
  59. kgid = make_kgid(user_ns, gid);
  60. if (!gid_valid(kgid))
  61. return -EINVAL;
  62. group_info->gid[i] = kgid;
  63. }
  64. return 0;
  65. }
  66. /* a simple Shell sort */
  67. void groups_sort(struct group_info *group_info)
  68. {
  69. int base, max, stride;
  70. int gidsetsize = group_info->ngroups;
  71. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  72. ; /* nothing */
  73. stride /= 3;
  74. while (stride) {
  75. max = gidsetsize - stride;
  76. for (base = 0; base < max; base++) {
  77. int left = base;
  78. int right = left + stride;
  79. kgid_t tmp = group_info->gid[right];
  80. while (left >= 0 && gid_gt(group_info->gid[left], tmp)) {
  81. group_info->gid[right] = group_info->gid[left];
  82. right = left;
  83. left -= stride;
  84. }
  85. group_info->gid[right] = tmp;
  86. }
  87. stride /= 3;
  88. }
  89. }
  90. EXPORT_SYMBOL(groups_sort);
  91. /* a simple bsearch */
  92. int groups_search(const struct group_info *group_info, kgid_t grp)
  93. {
  94. unsigned int left, right;
  95. if (!group_info)
  96. return 0;
  97. left = 0;
  98. right = group_info->ngroups;
  99. while (left < right) {
  100. unsigned int mid = (left+right)/2;
  101. if (gid_gt(grp, group_info->gid[mid]))
  102. left = mid + 1;
  103. else if (gid_lt(grp, group_info->gid[mid]))
  104. right = mid;
  105. else
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. /**
  111. * set_groups - Change a group subscription in a set of credentials
  112. * @new: The newly prepared set of credentials to alter
  113. * @group_info: The group list to install
  114. */
  115. void set_groups(struct cred *new, struct group_info *group_info)
  116. {
  117. put_group_info(new->group_info);
  118. get_group_info(group_info);
  119. new->group_info = group_info;
  120. }
  121. EXPORT_SYMBOL(set_groups);
  122. /**
  123. * set_current_groups - Change current's group subscription
  124. * @group_info: The group list to impose
  125. *
  126. * Validate a group subscription and, if valid, impose it upon current's task
  127. * security record.
  128. */
  129. int set_current_groups(struct group_info *group_info)
  130. {
  131. struct cred *new;
  132. new = prepare_creds();
  133. if (!new)
  134. return -ENOMEM;
  135. set_groups(new, group_info);
  136. return commit_creds(new);
  137. }
  138. EXPORT_SYMBOL(set_current_groups);
  139. SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist)
  140. {
  141. const struct cred *cred = current_cred();
  142. int i;
  143. if (gidsetsize < 0)
  144. return -EINVAL;
  145. /* no need to grab task_lock here; it cannot change */
  146. i = cred->group_info->ngroups;
  147. if (gidsetsize) {
  148. if (i > gidsetsize) {
  149. i = -EINVAL;
  150. goto out;
  151. }
  152. if (groups_to_user(grouplist, cred->group_info)) {
  153. i = -EFAULT;
  154. goto out;
  155. }
  156. }
  157. out:
  158. return i;
  159. }
  160. bool may_setgroups(void)
  161. {
  162. struct user_namespace *user_ns = current_user_ns();
  163. return ns_capable(user_ns, CAP_SETGID) &&
  164. userns_may_setgroups(user_ns);
  165. }
  166. /*
  167. * SMP: Our groups are copy-on-write. We can set them safely
  168. * without another task interfering.
  169. */
  170. SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
  171. {
  172. struct group_info *group_info;
  173. int retval;
  174. if (!may_setgroups())
  175. return -EPERM;
  176. if ((unsigned)gidsetsize > NGROUPS_MAX)
  177. return -EINVAL;
  178. group_info = groups_alloc(gidsetsize);
  179. if (!group_info)
  180. return -ENOMEM;
  181. retval = groups_from_user(group_info, grouplist);
  182. if (retval) {
  183. put_group_info(group_info);
  184. return retval;
  185. }
  186. groups_sort(group_info);
  187. retval = set_current_groups(group_info);
  188. put_group_info(group_info);
  189. return retval;
  190. }
  191. /*
  192. * Check whether we're fsgid/egid or in the supplemental group..
  193. */
  194. int in_group_p(kgid_t grp)
  195. {
  196. const struct cred *cred = current_cred();
  197. int retval = 1;
  198. if (!gid_eq(grp, cred->fsgid))
  199. retval = groups_search(cred->group_info, grp);
  200. return retval;
  201. }
  202. EXPORT_SYMBOL(in_group_p);
  203. int in_egroup_p(kgid_t grp)
  204. {
  205. const struct cred *cred = current_cred();
  206. int retval = 1;
  207. if (!gid_eq(grp, cred->egid))
  208. retval = groups_search(cred->group_info, grp);
  209. return retval;
  210. }
  211. EXPORT_SYMBOL(in_egroup_p);