sys_ia32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
  4. * sys_sparc32
  5. *
  6. * Copyright (C) 2000 VA Linux Co
  7. * Copyright (C) 2000 Don Dugger <[email protected]>
  8. * Copyright (C) 1999 Arun Sharma <[email protected]>
  9. * Copyright (C) 1997,1998 Jakub Jelinek ([email protected])
  10. * Copyright (C) 1997 David S. Miller ([email protected])
  11. * Copyright (C) 2000 Hewlett-Packard Co.
  12. * Copyright (C) 2000 David Mosberger-Tang <[email protected]>
  13. * Copyright (C) 2000,2001,2002 Andi Kleen, SuSE Labs (x86-64 port)
  14. *
  15. * These routines maintain argument size conversion between 32bit and 64bit
  16. * environment. In 2.5 most of this should be moved to a generic directory.
  17. *
  18. * This file assumes that there is a hole at the end of user address space.
  19. *
  20. * Some of the functions are LE specific currently. These are
  21. * hopefully all marked. This should be fixed.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/fs.h>
  26. #include <linux/file.h>
  27. #include <linux/signal.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/times.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mm.h>
  32. #include <linux/uio.h>
  33. #include <linux/poll.h>
  34. #include <linux/personality.h>
  35. #include <linux/stat.h>
  36. #include <linux/rwsem.h>
  37. #include <linux/compat.h>
  38. #include <linux/vfs.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/highuid.h>
  41. #include <linux/sysctl.h>
  42. #include <linux/slab.h>
  43. #include <linux/sched/task.h>
  44. #include <asm/mman.h>
  45. #include <asm/types.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/atomic.h>
  48. #include <asm/vgtod.h>
  49. #include <asm/ia32.h>
  50. #define AA(__x) ((unsigned long)(__x))
  51. SYSCALL_DEFINE3(ia32_truncate64, const char __user *, filename,
  52. unsigned long, offset_low, unsigned long, offset_high)
  53. {
  54. return ksys_truncate(filename,
  55. ((loff_t) offset_high << 32) | offset_low);
  56. }
  57. SYSCALL_DEFINE3(ia32_ftruncate64, unsigned int, fd,
  58. unsigned long, offset_low, unsigned long, offset_high)
  59. {
  60. return ksys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
  61. }
  62. /* warning: next two assume little endian */
  63. SYSCALL_DEFINE5(ia32_pread64, unsigned int, fd, char __user *, ubuf,
  64. u32, count, u32, poslo, u32, poshi)
  65. {
  66. return ksys_pread64(fd, ubuf, count,
  67. ((loff_t)AA(poshi) << 32) | AA(poslo));
  68. }
  69. SYSCALL_DEFINE5(ia32_pwrite64, unsigned int, fd, const char __user *, ubuf,
  70. u32, count, u32, poslo, u32, poshi)
  71. {
  72. return ksys_pwrite64(fd, ubuf, count,
  73. ((loff_t)AA(poshi) << 32) | AA(poslo));
  74. }
  75. /*
  76. * Some system calls that need sign extended arguments. This could be
  77. * done by a generic wrapper.
  78. */
  79. SYSCALL_DEFINE6(ia32_fadvise64_64, int, fd, __u32, offset_low,
  80. __u32, offset_high, __u32, len_low, __u32, len_high,
  81. int, advice)
  82. {
  83. return ksys_fadvise64_64(fd,
  84. (((u64)offset_high)<<32) | offset_low,
  85. (((u64)len_high)<<32) | len_low,
  86. advice);
  87. }
  88. SYSCALL_DEFINE4(ia32_readahead, int, fd, unsigned int, off_lo,
  89. unsigned int, off_hi, size_t, count)
  90. {
  91. return ksys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
  92. }
  93. SYSCALL_DEFINE6(ia32_sync_file_range, int, fd, unsigned int, off_low,
  94. unsigned int, off_hi, unsigned int, n_low,
  95. unsigned int, n_hi, int, flags)
  96. {
  97. return ksys_sync_file_range(fd,
  98. ((u64)off_hi << 32) | off_low,
  99. ((u64)n_hi << 32) | n_low, flags);
  100. }
  101. SYSCALL_DEFINE5(ia32_fadvise64, int, fd, unsigned int, offset_lo,
  102. unsigned int, offset_hi, size_t, len, int, advice)
  103. {
  104. return ksys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
  105. len, advice);
  106. }
  107. SYSCALL_DEFINE6(ia32_fallocate, int, fd, int, mode,
  108. unsigned int, offset_lo, unsigned int, offset_hi,
  109. unsigned int, len_lo, unsigned int, len_hi)
  110. {
  111. return ksys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
  112. ((u64)len_hi << 32) | len_lo);
  113. }
  114. #ifdef CONFIG_IA32_EMULATION
  115. /*
  116. * Another set for IA32/LFS -- x86_64 struct stat is different due to
  117. * support for 64bit inode numbers.
  118. */
  119. static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
  120. {
  121. typeof(ubuf->st_uid) uid = 0;
  122. typeof(ubuf->st_gid) gid = 0;
  123. SET_UID(uid, from_kuid_munged(current_user_ns(), stat->uid));
  124. SET_GID(gid, from_kgid_munged(current_user_ns(), stat->gid));
  125. if (!user_write_access_begin(ubuf, sizeof(struct stat64)))
  126. return -EFAULT;
  127. unsafe_put_user(huge_encode_dev(stat->dev), &ubuf->st_dev, Efault);
  128. unsafe_put_user(stat->ino, &ubuf->__st_ino, Efault);
  129. unsafe_put_user(stat->ino, &ubuf->st_ino, Efault);
  130. unsafe_put_user(stat->mode, &ubuf->st_mode, Efault);
  131. unsafe_put_user(stat->nlink, &ubuf->st_nlink, Efault);
  132. unsafe_put_user(uid, &ubuf->st_uid, Efault);
  133. unsafe_put_user(gid, &ubuf->st_gid, Efault);
  134. unsafe_put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev, Efault);
  135. unsafe_put_user(stat->size, &ubuf->st_size, Efault);
  136. unsafe_put_user(stat->atime.tv_sec, &ubuf->st_atime, Efault);
  137. unsafe_put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec, Efault);
  138. unsafe_put_user(stat->mtime.tv_sec, &ubuf->st_mtime, Efault);
  139. unsafe_put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec, Efault);
  140. unsafe_put_user(stat->ctime.tv_sec, &ubuf->st_ctime, Efault);
  141. unsafe_put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec, Efault);
  142. unsafe_put_user(stat->blksize, &ubuf->st_blksize, Efault);
  143. unsafe_put_user(stat->blocks, &ubuf->st_blocks, Efault);
  144. user_access_end();
  145. return 0;
  146. Efault:
  147. user_write_access_end();
  148. return -EFAULT;
  149. }
  150. COMPAT_SYSCALL_DEFINE2(ia32_stat64, const char __user *, filename,
  151. struct stat64 __user *, statbuf)
  152. {
  153. struct kstat stat;
  154. int ret = vfs_stat(filename, &stat);
  155. if (!ret)
  156. ret = cp_stat64(statbuf, &stat);
  157. return ret;
  158. }
  159. COMPAT_SYSCALL_DEFINE2(ia32_lstat64, const char __user *, filename,
  160. struct stat64 __user *, statbuf)
  161. {
  162. struct kstat stat;
  163. int ret = vfs_lstat(filename, &stat);
  164. if (!ret)
  165. ret = cp_stat64(statbuf, &stat);
  166. return ret;
  167. }
  168. COMPAT_SYSCALL_DEFINE2(ia32_fstat64, unsigned int, fd,
  169. struct stat64 __user *, statbuf)
  170. {
  171. struct kstat stat;
  172. int ret = vfs_fstat(fd, &stat);
  173. if (!ret)
  174. ret = cp_stat64(statbuf, &stat);
  175. return ret;
  176. }
  177. COMPAT_SYSCALL_DEFINE4(ia32_fstatat64, unsigned int, dfd,
  178. const char __user *, filename,
  179. struct stat64 __user *, statbuf, int, flag)
  180. {
  181. struct kstat stat;
  182. int error;
  183. error = vfs_fstatat(dfd, filename, &stat, flag);
  184. if (error)
  185. return error;
  186. return cp_stat64(statbuf, &stat);
  187. }
  188. /*
  189. * Linux/i386 didn't use to be able to handle more than
  190. * 4 system call parameters, so these system calls used a memory
  191. * block for parameter passing..
  192. */
  193. struct mmap_arg_struct32 {
  194. unsigned int addr;
  195. unsigned int len;
  196. unsigned int prot;
  197. unsigned int flags;
  198. unsigned int fd;
  199. unsigned int offset;
  200. };
  201. COMPAT_SYSCALL_DEFINE1(ia32_mmap, struct mmap_arg_struct32 __user *, arg)
  202. {
  203. struct mmap_arg_struct32 a;
  204. if (copy_from_user(&a, arg, sizeof(a)))
  205. return -EFAULT;
  206. if (a.offset & ~PAGE_MASK)
  207. return -EINVAL;
  208. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  209. a.offset>>PAGE_SHIFT);
  210. }
  211. /*
  212. * The 32-bit clone ABI is CONFIG_CLONE_BACKWARDS
  213. */
  214. COMPAT_SYSCALL_DEFINE5(ia32_clone, unsigned long, clone_flags,
  215. unsigned long, newsp, int __user *, parent_tidptr,
  216. unsigned long, tls_val, int __user *, child_tidptr)
  217. {
  218. struct kernel_clone_args args = {
  219. .flags = (clone_flags & ~CSIGNAL),
  220. .pidfd = parent_tidptr,
  221. .child_tid = child_tidptr,
  222. .parent_tid = parent_tidptr,
  223. .exit_signal = (clone_flags & CSIGNAL),
  224. .stack = newsp,
  225. .tls = tls_val,
  226. };
  227. return kernel_clone(&args);
  228. }
  229. #endif /* CONFIG_IA32_EMULATION */