uid16.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "uid16.h"
  19. SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
  20. {
  21. return ksys_chown(filename, low2highuid(user), low2highgid(group));
  22. }
  23. SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
  24. {
  25. return ksys_lchown(filename, low2highuid(user), low2highgid(group));
  26. }
  27. SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
  28. {
  29. return ksys_fchown(fd, low2highuid(user), low2highgid(group));
  30. }
  31. SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
  32. {
  33. return __sys_setregid(low2highgid(rgid), low2highgid(egid));
  34. }
  35. SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
  36. {
  37. return __sys_setgid(low2highgid(gid));
  38. }
  39. SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
  40. {
  41. return __sys_setreuid(low2highuid(ruid), low2highuid(euid));
  42. }
  43. SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
  44. {
  45. return __sys_setuid(low2highuid(uid));
  46. }
  47. SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
  48. {
  49. return __sys_setresuid(low2highuid(ruid), low2highuid(euid),
  50. low2highuid(suid));
  51. }
  52. SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
  53. {
  54. const struct cred *cred = current_cred();
  55. int retval;
  56. old_uid_t ruid, euid, suid;
  57. ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
  58. euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
  59. suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
  60. if (!(retval = put_user(ruid, ruidp)) &&
  61. !(retval = put_user(euid, euidp)))
  62. retval = put_user(suid, suidp);
  63. return retval;
  64. }
  65. SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
  66. {
  67. return __sys_setresgid(low2highgid(rgid), low2highgid(egid),
  68. low2highgid(sgid));
  69. }
  70. SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
  71. {
  72. const struct cred *cred = current_cred();
  73. int retval;
  74. old_gid_t rgid, egid, sgid;
  75. rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
  76. egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
  77. sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
  78. if (!(retval = put_user(rgid, rgidp)) &&
  79. !(retval = put_user(egid, egidp)))
  80. retval = put_user(sgid, sgidp);
  81. return retval;
  82. }
  83. SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
  84. {
  85. return __sys_setfsuid(low2highuid(uid));
  86. }
  87. SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
  88. {
  89. return __sys_setfsgid(low2highgid(gid));
  90. }
  91. static int groups16_to_user(old_gid_t __user *grouplist,
  92. struct group_info *group_info)
  93. {
  94. struct user_namespace *user_ns = current_user_ns();
  95. int i;
  96. old_gid_t group;
  97. kgid_t kgid;
  98. for (i = 0; i < group_info->ngroups; i++) {
  99. kgid = group_info->gid[i];
  100. group = high2lowgid(from_kgid_munged(user_ns, kgid));
  101. if (put_user(group, grouplist+i))
  102. return -EFAULT;
  103. }
  104. return 0;
  105. }
  106. static int groups16_from_user(struct group_info *group_info,
  107. old_gid_t __user *grouplist)
  108. {
  109. struct user_namespace *user_ns = current_user_ns();
  110. int i;
  111. old_gid_t group;
  112. kgid_t kgid;
  113. for (i = 0; i < group_info->ngroups; i++) {
  114. if (get_user(group, grouplist+i))
  115. return -EFAULT;
  116. kgid = make_kgid(user_ns, low2highgid(group));
  117. if (!gid_valid(kgid))
  118. return -EINVAL;
  119. group_info->gid[i] = kgid;
  120. }
  121. return 0;
  122. }
  123. SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
  124. {
  125. const struct cred *cred = current_cred();
  126. int i;
  127. if (gidsetsize < 0)
  128. return -EINVAL;
  129. i = cred->group_info->ngroups;
  130. if (gidsetsize) {
  131. if (i > gidsetsize) {
  132. i = -EINVAL;
  133. goto out;
  134. }
  135. if (groups16_to_user(grouplist, cred->group_info)) {
  136. i = -EFAULT;
  137. goto out;
  138. }
  139. }
  140. out:
  141. return i;
  142. }
  143. SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
  144. {
  145. struct group_info *group_info;
  146. int retval;
  147. if (!may_setgroups())
  148. return -EPERM;
  149. if ((unsigned)gidsetsize > NGROUPS_MAX)
  150. return -EINVAL;
  151. group_info = groups_alloc(gidsetsize);
  152. if (!group_info)
  153. return -ENOMEM;
  154. retval = groups16_from_user(group_info, grouplist);
  155. if (retval) {
  156. put_group_info(group_info);
  157. return retval;
  158. }
  159. groups_sort(group_info);
  160. retval = set_current_groups(group_info);
  161. put_group_info(group_info);
  162. return retval;
  163. }
  164. SYSCALL_DEFINE0(getuid16)
  165. {
  166. return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
  167. }
  168. SYSCALL_DEFINE0(geteuid16)
  169. {
  170. return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
  171. }
  172. SYSCALL_DEFINE0(getgid16)
  173. {
  174. return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
  175. }
  176. SYSCALL_DEFINE0(getegid16)
  177. {
  178. return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
  179. }