uid16.c 5.0 KB

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