groups.c 4.9 KB

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