groups.c 4.9 KB

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