uid16.c 5.0 KB

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