uid16.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Wrapper functions for 16bit uid back compatibility. All nicely tied
  3. * together in the faint hope we can take the out in five years time.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/mman.h>
  7. #include <linux/notifier.h>
  8. #include <linux/reboot.h>
  9. #include <linux/prctl.h>
  10. #include <linux/capability.h>
  11. #include <linux/init.h>
  12. #include <linux/highuid.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <asm/uaccess.h>
  16. SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
  17. {
  18. long ret = sys_chown(filename, low2highuid(user), low2highgid(group));
  19. /* avoid REGPARM breakage on x86: */
  20. asmlinkage_protect(3, ret, filename, user, group);
  21. return ret;
  22. }
  23. SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
  24. {
  25. long ret = sys_lchown(filename, low2highuid(user), low2highgid(group));
  26. /* avoid REGPARM breakage on x86: */
  27. asmlinkage_protect(3, ret, filename, user, group);
  28. return ret;
  29. }
  30. SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
  31. {
  32. long ret = sys_fchown(fd, low2highuid(user), low2highgid(group));
  33. /* avoid REGPARM breakage on x86: */
  34. asmlinkage_protect(3, ret, fd, user, group);
  35. return ret;
  36. }
  37. SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
  38. {
  39. long ret = sys_setregid(low2highgid(rgid), low2highgid(egid));
  40. /* avoid REGPARM breakage on x86: */
  41. asmlinkage_protect(2, ret, rgid, egid);
  42. return ret;
  43. }
  44. SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
  45. {
  46. long ret = sys_setgid(low2highgid(gid));
  47. /* avoid REGPARM breakage on x86: */
  48. asmlinkage_protect(1, ret, gid);
  49. return ret;
  50. }
  51. SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
  52. {
  53. long ret = sys_setreuid(low2highuid(ruid), low2highuid(euid));
  54. /* avoid REGPARM breakage on x86: */
  55. asmlinkage_protect(2, ret, ruid, euid);
  56. return ret;
  57. }
  58. SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
  59. {
  60. long ret = sys_setuid(low2highuid(uid));
  61. /* avoid REGPARM breakage on x86: */
  62. asmlinkage_protect(1, ret, uid);
  63. return ret;
  64. }
  65. SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
  66. {
  67. long ret = sys_setresuid(low2highuid(ruid), low2highuid(euid),
  68. low2highuid(suid));
  69. /* avoid REGPARM breakage on x86: */
  70. asmlinkage_protect(3, ret, ruid, euid, suid);
  71. return ret;
  72. }
  73. SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
  74. {
  75. const struct cred *cred = current_cred();
  76. int retval;
  77. old_uid_t ruid, euid, suid;
  78. ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
  79. euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
  80. suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
  81. if (!(retval = put_user(ruid, ruidp)) &&
  82. !(retval = put_user(euid, euidp)))
  83. retval = put_user(suid, suidp);
  84. return retval;
  85. }
  86. SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
  87. {
  88. long ret = sys_setresgid(low2highgid(rgid), low2highgid(egid),
  89. low2highgid(sgid));
  90. /* avoid REGPARM breakage on x86: */
  91. asmlinkage_protect(3, ret, rgid, egid, sgid);
  92. return ret;
  93. }
  94. SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
  95. {
  96. const struct cred *cred = current_cred();
  97. int retval;
  98. old_gid_t rgid, egid, sgid;
  99. rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
  100. egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
  101. sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
  102. if (!(retval = put_user(rgid, rgidp)) &&
  103. !(retval = put_user(egid, egidp)))
  104. retval = put_user(sgid, sgidp);
  105. return retval;
  106. }
  107. SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
  108. {
  109. long ret = sys_setfsuid(low2highuid(uid));
  110. /* avoid REGPARM breakage on x86: */
  111. asmlinkage_protect(1, ret, uid);
  112. return ret;
  113. }
  114. SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
  115. {
  116. long ret = sys_setfsgid(low2highgid(gid));
  117. /* avoid REGPARM breakage on x86: */
  118. asmlinkage_protect(1, ret, gid);
  119. return ret;
  120. }
  121. static int groups16_to_user(old_gid_t __user *grouplist,
  122. struct group_info *group_info)
  123. {
  124. struct user_namespace *user_ns = current_user_ns();
  125. int i;
  126. old_gid_t group;
  127. kgid_t kgid;
  128. for (i = 0; i < group_info->ngroups; i++) {
  129. kgid = GROUP_AT(group_info, i);
  130. group = high2lowgid(from_kgid_munged(user_ns, kgid));
  131. if (put_user(group, grouplist+i))
  132. return -EFAULT;
  133. }
  134. return 0;
  135. }
  136. static int groups16_from_user(struct group_info *group_info,
  137. old_gid_t __user *grouplist)
  138. {
  139. struct user_namespace *user_ns = current_user_ns();
  140. int i;
  141. old_gid_t group;
  142. kgid_t kgid;
  143. for (i = 0; i < group_info->ngroups; i++) {
  144. if (get_user(group, grouplist+i))
  145. return -EFAULT;
  146. kgid = make_kgid(user_ns, low2highgid(group));
  147. if (!gid_valid(kgid))
  148. return -EINVAL;
  149. GROUP_AT(group_info, i) = kgid;
  150. }
  151. return 0;
  152. }
  153. SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
  154. {
  155. const struct cred *cred = current_cred();
  156. int i;
  157. if (gidsetsize < 0)
  158. return -EINVAL;
  159. i = cred->group_info->ngroups;
  160. if (gidsetsize) {
  161. if (i > gidsetsize) {
  162. i = -EINVAL;
  163. goto out;
  164. }
  165. if (groups16_to_user(grouplist, cred->group_info)) {
  166. i = -EFAULT;
  167. goto out;
  168. }
  169. }
  170. out:
  171. return i;
  172. }
  173. SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
  174. {
  175. struct group_info *group_info;
  176. int retval;
  177. if (!nsown_capable(CAP_SETGID))
  178. return -EPERM;
  179. if ((unsigned)gidsetsize > NGROUPS_MAX)
  180. return -EINVAL;
  181. group_info = groups_alloc(gidsetsize);
  182. if (!group_info)
  183. return -ENOMEM;
  184. retval = groups16_from_user(group_info, grouplist);
  185. if (retval) {
  186. put_group_info(group_info);
  187. return retval;
  188. }
  189. retval = set_current_groups(group_info);
  190. put_group_info(group_info);
  191. return retval;
  192. }
  193. SYSCALL_DEFINE0(getuid16)
  194. {
  195. return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
  196. }
  197. SYSCALL_DEFINE0(geteuid16)
  198. {
  199. return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
  200. }
  201. SYSCALL_DEFINE0(getgid16)
  202. {
  203. return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
  204. }
  205. SYSCALL_DEFINE0(getegid16)
  206. {
  207. return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
  208. }