compat_linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 2000
  4. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  5. * Gerhard Tonn (ton@de.ibm.com)
  6. * Thomas Spatzier (tspat@de.ibm.com)
  7. *
  8. * Conversion between 31bit and 64bit native syscalls.
  9. *
  10. * Heavily inspired by the 32-bit Sparc compat code which is
  11. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  12. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/fs.h>
  18. #include <linux/mm.h>
  19. #include <linux/file.h>
  20. #include <linux/signal.h>
  21. #include <linux/resource.h>
  22. #include <linux/times.h>
  23. #include <linux/smp.h>
  24. #include <linux/sem.h>
  25. #include <linux/msg.h>
  26. #include <linux/shm.h>
  27. #include <linux/uio.h>
  28. #include <linux/quota.h>
  29. #include <linux/module.h>
  30. #include <linux/poll.h>
  31. #include <linux/personality.h>
  32. #include <linux/stat.h>
  33. #include <linux/filter.h>
  34. #include <linux/highmem.h>
  35. #include <linux/highuid.h>
  36. #include <linux/mman.h>
  37. #include <linux/ipv6.h>
  38. #include <linux/in.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/syscalls.h>
  41. #include <linux/sysctl.h>
  42. #include <linux/binfmts.h>
  43. #include <linux/capability.h>
  44. #include <linux/compat.h>
  45. #include <linux/vfs.h>
  46. #include <linux/ptrace.h>
  47. #include <linux/fadvise.h>
  48. #include <linux/ipc.h>
  49. #include <linux/slab.h>
  50. #include <asm/types.h>
  51. #include <asm/uaccess.h>
  52. #include <net/scm.h>
  53. #include <net/sock.h>
  54. #include "compat_linux.h"
  55. u32 psw32_user_bits = PSW32_MASK_DAT | PSW32_MASK_IO | PSW32_MASK_EXT |
  56. PSW32_DEFAULT_KEY | PSW32_MASK_BASE | PSW32_MASK_MCHECK |
  57. PSW32_MASK_PSTATE | PSW32_ASC_HOME;
  58. /* For this source file, we want overflow handling. */
  59. #undef high2lowuid
  60. #undef high2lowgid
  61. #undef low2highuid
  62. #undef low2highgid
  63. #undef SET_UID16
  64. #undef SET_GID16
  65. #undef NEW_TO_OLD_UID
  66. #undef NEW_TO_OLD_GID
  67. #undef SET_OLDSTAT_UID
  68. #undef SET_OLDSTAT_GID
  69. #undef SET_STAT_UID
  70. #undef SET_STAT_GID
  71. #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
  72. #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
  73. #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
  74. #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
  75. #define SET_UID16(var, uid) var = high2lowuid(uid)
  76. #define SET_GID16(var, gid) var = high2lowgid(gid)
  77. #define NEW_TO_OLD_UID(uid) high2lowuid(uid)
  78. #define NEW_TO_OLD_GID(gid) high2lowgid(gid)
  79. #define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  80. #define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  81. #define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  82. #define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  83. asmlinkage long sys32_chown16(const char __user * filename, u16 user, u16 group)
  84. {
  85. return sys_chown(filename, low2highuid(user), low2highgid(group));
  86. }
  87. asmlinkage long sys32_lchown16(const char __user * filename, u16 user, u16 group)
  88. {
  89. return sys_lchown(filename, low2highuid(user), low2highgid(group));
  90. }
  91. asmlinkage long sys32_fchown16(unsigned int fd, u16 user, u16 group)
  92. {
  93. return sys_fchown(fd, low2highuid(user), low2highgid(group));
  94. }
  95. asmlinkage long sys32_setregid16(u16 rgid, u16 egid)
  96. {
  97. return sys_setregid(low2highgid(rgid), low2highgid(egid));
  98. }
  99. asmlinkage long sys32_setgid16(u16 gid)
  100. {
  101. return sys_setgid((gid_t)gid);
  102. }
  103. asmlinkage long sys32_setreuid16(u16 ruid, u16 euid)
  104. {
  105. return sys_setreuid(low2highuid(ruid), low2highuid(euid));
  106. }
  107. asmlinkage long sys32_setuid16(u16 uid)
  108. {
  109. return sys_setuid((uid_t)uid);
  110. }
  111. asmlinkage long sys32_setresuid16(u16 ruid, u16 euid, u16 suid)
  112. {
  113. return sys_setresuid(low2highuid(ruid), low2highuid(euid),
  114. low2highuid(suid));
  115. }
  116. asmlinkage long sys32_getresuid16(u16 __user *ruidp, u16 __user *euidp, u16 __user *suidp)
  117. {
  118. const struct cred *cred = current_cred();
  119. int retval;
  120. u16 ruid, euid, suid;
  121. ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
  122. euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
  123. suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
  124. if (!(retval = put_user(ruid, ruidp)) &&
  125. !(retval = put_user(euid, euidp)))
  126. retval = put_user(suid, suidp);
  127. return retval;
  128. }
  129. asmlinkage long sys32_setresgid16(u16 rgid, u16 egid, u16 sgid)
  130. {
  131. return sys_setresgid(low2highgid(rgid), low2highgid(egid),
  132. low2highgid(sgid));
  133. }
  134. asmlinkage long sys32_getresgid16(u16 __user *rgidp, u16 __user *egidp, u16 __user *sgidp)
  135. {
  136. const struct cred *cred = current_cred();
  137. int retval;
  138. u16 rgid, egid, sgid;
  139. rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
  140. egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
  141. sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
  142. if (!(retval = put_user(rgid, rgidp)) &&
  143. !(retval = put_user(egid, egidp)))
  144. retval = put_user(sgid, sgidp);
  145. return retval;
  146. }
  147. asmlinkage long sys32_setfsuid16(u16 uid)
  148. {
  149. return sys_setfsuid((uid_t)uid);
  150. }
  151. asmlinkage long sys32_setfsgid16(u16 gid)
  152. {
  153. return sys_setfsgid((gid_t)gid);
  154. }
  155. static int groups16_to_user(u16 __user *grouplist, struct group_info *group_info)
  156. {
  157. struct user_namespace *user_ns = current_user_ns();
  158. int i;
  159. u16 group;
  160. kgid_t kgid;
  161. for (i = 0; i < group_info->ngroups; i++) {
  162. kgid = GROUP_AT(group_info, i);
  163. group = (u16)from_kgid_munged(user_ns, kgid);
  164. if (put_user(group, grouplist+i))
  165. return -EFAULT;
  166. }
  167. return 0;
  168. }
  169. static int groups16_from_user(struct group_info *group_info, u16 __user *grouplist)
  170. {
  171. struct user_namespace *user_ns = current_user_ns();
  172. int i;
  173. u16 group;
  174. kgid_t kgid;
  175. for (i = 0; i < group_info->ngroups; i++) {
  176. if (get_user(group, grouplist+i))
  177. return -EFAULT;
  178. kgid = make_kgid(user_ns, (gid_t)group);
  179. if (!gid_valid(kgid))
  180. return -EINVAL;
  181. GROUP_AT(group_info, i) = kgid;
  182. }
  183. return 0;
  184. }
  185. asmlinkage long sys32_getgroups16(int gidsetsize, u16 __user *grouplist)
  186. {
  187. const struct cred *cred = current_cred();
  188. int i;
  189. if (gidsetsize < 0)
  190. return -EINVAL;
  191. get_group_info(cred->group_info);
  192. i = cred->group_info->ngroups;
  193. if (gidsetsize) {
  194. if (i > gidsetsize) {
  195. i = -EINVAL;
  196. goto out;
  197. }
  198. if (groups16_to_user(grouplist, cred->group_info)) {
  199. i = -EFAULT;
  200. goto out;
  201. }
  202. }
  203. out:
  204. put_group_info(cred->group_info);
  205. return i;
  206. }
  207. asmlinkage long sys32_setgroups16(int gidsetsize, u16 __user *grouplist)
  208. {
  209. struct group_info *group_info;
  210. int retval;
  211. if (!capable(CAP_SETGID))
  212. return -EPERM;
  213. if ((unsigned)gidsetsize > NGROUPS_MAX)
  214. return -EINVAL;
  215. group_info = groups_alloc(gidsetsize);
  216. if (!group_info)
  217. return -ENOMEM;
  218. retval = groups16_from_user(group_info, grouplist);
  219. if (retval) {
  220. put_group_info(group_info);
  221. return retval;
  222. }
  223. retval = set_current_groups(group_info);
  224. put_group_info(group_info);
  225. return retval;
  226. }
  227. asmlinkage long sys32_getuid16(void)
  228. {
  229. return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
  230. }
  231. asmlinkage long sys32_geteuid16(void)
  232. {
  233. return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
  234. }
  235. asmlinkage long sys32_getgid16(void)
  236. {
  237. return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
  238. }
  239. asmlinkage long sys32_getegid16(void)
  240. {
  241. return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
  242. }
  243. #ifdef CONFIG_SYSVIPC
  244. COMPAT_SYSCALL_DEFINE5(s390_ipc, uint, call, int, first, unsigned long, second,
  245. unsigned long, third, compat_uptr_t, ptr)
  246. {
  247. if (call >> 16) /* hack for backward compatibility */
  248. return -EINVAL;
  249. return compat_sys_ipc(call, first, second, third, ptr, third);
  250. }
  251. #endif
  252. asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
  253. {
  254. if ((int)high < 0)
  255. return -EINVAL;
  256. else
  257. return sys_truncate(path, (high << 32) | low);
  258. }
  259. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
  260. {
  261. if ((int)high < 0)
  262. return -EINVAL;
  263. else
  264. return sys_ftruncate(fd, (high << 32) | low);
  265. }
  266. asmlinkage long sys32_pread64(unsigned int fd, char __user *ubuf,
  267. size_t count, u32 poshi, u32 poslo)
  268. {
  269. if ((compat_ssize_t) count < 0)
  270. return -EINVAL;
  271. return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  272. }
  273. asmlinkage long sys32_pwrite64(unsigned int fd, const char __user *ubuf,
  274. size_t count, u32 poshi, u32 poslo)
  275. {
  276. if ((compat_ssize_t) count < 0)
  277. return -EINVAL;
  278. return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  279. }
  280. asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
  281. {
  282. return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
  283. }
  284. struct stat64_emu31 {
  285. unsigned long long st_dev;
  286. unsigned int __pad1;
  287. #define STAT64_HAS_BROKEN_ST_INO 1
  288. u32 __st_ino;
  289. unsigned int st_mode;
  290. unsigned int st_nlink;
  291. u32 st_uid;
  292. u32 st_gid;
  293. unsigned long long st_rdev;
  294. unsigned int __pad3;
  295. long st_size;
  296. u32 st_blksize;
  297. unsigned char __pad4[4];
  298. u32 __pad5; /* future possible st_blocks high bits */
  299. u32 st_blocks; /* Number 512-byte blocks allocated. */
  300. u32 st_atime;
  301. u32 __pad6;
  302. u32 st_mtime;
  303. u32 __pad7;
  304. u32 st_ctime;
  305. u32 __pad8; /* will be high 32 bits of ctime someday */
  306. unsigned long st_ino;
  307. };
  308. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  309. {
  310. struct stat64_emu31 tmp;
  311. memset(&tmp, 0, sizeof(tmp));
  312. tmp.st_dev = huge_encode_dev(stat->dev);
  313. tmp.st_ino = stat->ino;
  314. tmp.__st_ino = (u32)stat->ino;
  315. tmp.st_mode = stat->mode;
  316. tmp.st_nlink = (unsigned int)stat->nlink;
  317. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  318. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  319. tmp.st_rdev = huge_encode_dev(stat->rdev);
  320. tmp.st_size = stat->size;
  321. tmp.st_blksize = (u32)stat->blksize;
  322. tmp.st_blocks = (u32)stat->blocks;
  323. tmp.st_atime = (u32)stat->atime.tv_sec;
  324. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  325. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  326. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  327. }
  328. asmlinkage long sys32_stat64(const char __user * filename, struct stat64_emu31 __user * statbuf)
  329. {
  330. struct kstat stat;
  331. int ret = vfs_stat(filename, &stat);
  332. if (!ret)
  333. ret = cp_stat64(statbuf, &stat);
  334. return ret;
  335. }
  336. asmlinkage long sys32_lstat64(const char __user * filename, struct stat64_emu31 __user * statbuf)
  337. {
  338. struct kstat stat;
  339. int ret = vfs_lstat(filename, &stat);
  340. if (!ret)
  341. ret = cp_stat64(statbuf, &stat);
  342. return ret;
  343. }
  344. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 __user * statbuf)
  345. {
  346. struct kstat stat;
  347. int ret = vfs_fstat(fd, &stat);
  348. if (!ret)
  349. ret = cp_stat64(statbuf, &stat);
  350. return ret;
  351. }
  352. asmlinkage long sys32_fstatat64(unsigned int dfd, const char __user *filename,
  353. struct stat64_emu31 __user* statbuf, int flag)
  354. {
  355. struct kstat stat;
  356. int error;
  357. error = vfs_fstatat(dfd, filename, &stat, flag);
  358. if (error)
  359. return error;
  360. return cp_stat64(statbuf, &stat);
  361. }
  362. /*
  363. * Linux/i386 didn't use to be able to handle more than
  364. * 4 system call parameters, so these system calls used a memory
  365. * block for parameter passing..
  366. */
  367. struct mmap_arg_struct_emu31 {
  368. compat_ulong_t addr;
  369. compat_ulong_t len;
  370. compat_ulong_t prot;
  371. compat_ulong_t flags;
  372. compat_ulong_t fd;
  373. compat_ulong_t offset;
  374. };
  375. asmlinkage unsigned long old32_mmap(struct mmap_arg_struct_emu31 __user *arg)
  376. {
  377. struct mmap_arg_struct_emu31 a;
  378. if (copy_from_user(&a, arg, sizeof(a)))
  379. return -EFAULT;
  380. if (a.offset & ~PAGE_MASK)
  381. return -EINVAL;
  382. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  383. a.offset >> PAGE_SHIFT);
  384. }
  385. asmlinkage long sys32_mmap2(struct mmap_arg_struct_emu31 __user *arg)
  386. {
  387. struct mmap_arg_struct_emu31 a;
  388. if (copy_from_user(&a, arg, sizeof(a)))
  389. return -EFAULT;
  390. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  391. }
  392. asmlinkage long sys32_read(unsigned int fd, char __user * buf, size_t count)
  393. {
  394. if ((compat_ssize_t) count < 0)
  395. return -EINVAL;
  396. return sys_read(fd, buf, count);
  397. }
  398. asmlinkage long sys32_write(unsigned int fd, const char __user * buf, size_t count)
  399. {
  400. if ((compat_ssize_t) count < 0)
  401. return -EINVAL;
  402. return sys_write(fd, buf, count);
  403. }
  404. /*
  405. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  406. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  407. * because the 31 bit values differ from the 64 bit values.
  408. */
  409. asmlinkage long
  410. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  411. {
  412. if (advise == 4)
  413. advise = POSIX_FADV_DONTNEED;
  414. else if (advise == 5)
  415. advise = POSIX_FADV_NOREUSE;
  416. return sys_fadvise64(fd, offset, len, advise);
  417. }
  418. struct fadvise64_64_args {
  419. int fd;
  420. long long offset;
  421. long long len;
  422. int advice;
  423. };
  424. asmlinkage long
  425. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  426. {
  427. struct fadvise64_64_args a;
  428. if ( copy_from_user(&a, args, sizeof(a)) )
  429. return -EFAULT;
  430. if (a.advice == 4)
  431. a.advice = POSIX_FADV_DONTNEED;
  432. else if (a.advice == 5)
  433. a.advice = POSIX_FADV_NOREUSE;
  434. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  435. }