compat_linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /* For this source file, we want overflow handling. */
  56. #undef high2lowuid
  57. #undef high2lowgid
  58. #undef low2highuid
  59. #undef low2highgid
  60. #undef SET_UID16
  61. #undef SET_GID16
  62. #undef NEW_TO_OLD_UID
  63. #undef NEW_TO_OLD_GID
  64. #undef SET_OLDSTAT_UID
  65. #undef SET_OLDSTAT_GID
  66. #undef SET_STAT_UID
  67. #undef SET_STAT_GID
  68. #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
  69. #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
  70. #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
  71. #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
  72. #define SET_UID16(var, uid) var = high2lowuid(uid)
  73. #define SET_GID16(var, gid) var = high2lowgid(gid)
  74. #define NEW_TO_OLD_UID(uid) high2lowuid(uid)
  75. #define NEW_TO_OLD_GID(gid) high2lowgid(gid)
  76. #define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  77. #define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  78. #define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  79. #define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  80. COMPAT_SYSCALL_DEFINE3(s390_chown16, const char __user *, filename,
  81. u16, user, u16, group)
  82. {
  83. return sys_chown(filename, low2highuid(user), low2highgid(group));
  84. }
  85. COMPAT_SYSCALL_DEFINE3(s390_lchown16, const char __user *,
  86. filename, u16, user, u16, group)
  87. {
  88. return sys_lchown(filename, low2highuid(user), low2highgid(group));
  89. }
  90. COMPAT_SYSCALL_DEFINE3(s390_fchown16, unsigned int, fd, u16, user, u16, group)
  91. {
  92. return sys_fchown(fd, low2highuid(user), low2highgid(group));
  93. }
  94. COMPAT_SYSCALL_DEFINE2(s390_setregid16, u16, rgid, u16, egid)
  95. {
  96. return sys_setregid(low2highgid(rgid), low2highgid(egid));
  97. }
  98. COMPAT_SYSCALL_DEFINE1(s390_setgid16, u16, gid)
  99. {
  100. return sys_setgid(low2highgid(gid));
  101. }
  102. COMPAT_SYSCALL_DEFINE2(s390_setreuid16, u16, ruid, u16, euid)
  103. {
  104. return sys_setreuid(low2highuid(ruid), low2highuid(euid));
  105. }
  106. COMPAT_SYSCALL_DEFINE1(s390_setuid16, u16, uid)
  107. {
  108. return sys_setuid(low2highuid(uid));
  109. }
  110. COMPAT_SYSCALL_DEFINE3(s390_setresuid16, u16, ruid, u16, euid, u16, suid)
  111. {
  112. return sys_setresuid(low2highuid(ruid), low2highuid(euid),
  113. low2highuid(suid));
  114. }
  115. COMPAT_SYSCALL_DEFINE3(s390_getresuid16, u16 __user *, ruidp,
  116. 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. COMPAT_SYSCALL_DEFINE3(s390_setresgid16, u16, rgid, u16, egid, u16, sgid)
  130. {
  131. return sys_setresgid(low2highgid(rgid), low2highgid(egid),
  132. low2highgid(sgid));
  133. }
  134. COMPAT_SYSCALL_DEFINE3(s390_getresgid16, u16 __user *, rgidp,
  135. u16 __user *, egidp, u16 __user *, sgidp)
  136. {
  137. const struct cred *cred = current_cred();
  138. int retval;
  139. u16 rgid, egid, sgid;
  140. rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
  141. egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
  142. sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
  143. if (!(retval = put_user(rgid, rgidp)) &&
  144. !(retval = put_user(egid, egidp)))
  145. retval = put_user(sgid, sgidp);
  146. return retval;
  147. }
  148. COMPAT_SYSCALL_DEFINE1(s390_setfsuid16, u16, uid)
  149. {
  150. return sys_setfsuid(low2highuid(uid));
  151. }
  152. COMPAT_SYSCALL_DEFINE1(s390_setfsgid16, u16, gid)
  153. {
  154. return sys_setfsgid(low2highgid(gid));
  155. }
  156. static int groups16_to_user(u16 __user *grouplist, struct group_info *group_info)
  157. {
  158. struct user_namespace *user_ns = current_user_ns();
  159. int i;
  160. u16 group;
  161. kgid_t kgid;
  162. for (i = 0; i < group_info->ngroups; i++) {
  163. kgid = GROUP_AT(group_info, i);
  164. group = (u16)from_kgid_munged(user_ns, kgid);
  165. if (put_user(group, grouplist+i))
  166. return -EFAULT;
  167. }
  168. return 0;
  169. }
  170. static int groups16_from_user(struct group_info *group_info, u16 __user *grouplist)
  171. {
  172. struct user_namespace *user_ns = current_user_ns();
  173. int i;
  174. u16 group;
  175. kgid_t kgid;
  176. for (i = 0; i < group_info->ngroups; i++) {
  177. if (get_user(group, grouplist+i))
  178. return -EFAULT;
  179. kgid = make_kgid(user_ns, (gid_t)group);
  180. if (!gid_valid(kgid))
  181. return -EINVAL;
  182. GROUP_AT(group_info, i) = kgid;
  183. }
  184. return 0;
  185. }
  186. COMPAT_SYSCALL_DEFINE2(s390_getgroups16, int, gidsetsize, u16 __user *, grouplist)
  187. {
  188. const struct cred *cred = current_cred();
  189. int i;
  190. if (gidsetsize < 0)
  191. return -EINVAL;
  192. get_group_info(cred->group_info);
  193. i = cred->group_info->ngroups;
  194. if (gidsetsize) {
  195. if (i > gidsetsize) {
  196. i = -EINVAL;
  197. goto out;
  198. }
  199. if (groups16_to_user(grouplist, cred->group_info)) {
  200. i = -EFAULT;
  201. goto out;
  202. }
  203. }
  204. out:
  205. put_group_info(cred->group_info);
  206. return i;
  207. }
  208. COMPAT_SYSCALL_DEFINE2(s390_setgroups16, int, gidsetsize, u16 __user *, grouplist)
  209. {
  210. struct group_info *group_info;
  211. int retval;
  212. if (!may_setgroups())
  213. return -EPERM;
  214. if ((unsigned)gidsetsize > NGROUPS_MAX)
  215. return -EINVAL;
  216. group_info = groups_alloc(gidsetsize);
  217. if (!group_info)
  218. return -ENOMEM;
  219. retval = groups16_from_user(group_info, grouplist);
  220. if (retval) {
  221. put_group_info(group_info);
  222. return retval;
  223. }
  224. groups_sort(group_info);
  225. retval = set_current_groups(group_info);
  226. put_group_info(group_info);
  227. return retval;
  228. }
  229. COMPAT_SYSCALL_DEFINE0(s390_getuid16)
  230. {
  231. return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
  232. }
  233. COMPAT_SYSCALL_DEFINE0(s390_geteuid16)
  234. {
  235. return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
  236. }
  237. COMPAT_SYSCALL_DEFINE0(s390_getgid16)
  238. {
  239. return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
  240. }
  241. COMPAT_SYSCALL_DEFINE0(s390_getegid16)
  242. {
  243. return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
  244. }
  245. #ifdef CONFIG_SYSVIPC
  246. COMPAT_SYSCALL_DEFINE5(s390_ipc, uint, call, int, first, compat_ulong_t, second,
  247. compat_ulong_t, third, compat_uptr_t, ptr)
  248. {
  249. if (call >> 16) /* hack for backward compatibility */
  250. return -EINVAL;
  251. return compat_sys_ipc(call, first, second, third, ptr, third);
  252. }
  253. #endif
  254. COMPAT_SYSCALL_DEFINE3(s390_truncate64, const char __user *, path, u32, high, u32, low)
  255. {
  256. return sys_truncate(path, (unsigned long)high << 32 | low);
  257. }
  258. COMPAT_SYSCALL_DEFINE3(s390_ftruncate64, unsigned int, fd, u32, high, u32, low)
  259. {
  260. return sys_ftruncate(fd, (unsigned long)high << 32 | low);
  261. }
  262. COMPAT_SYSCALL_DEFINE5(s390_pread64, unsigned int, fd, char __user *, ubuf,
  263. compat_size_t, count, u32, high, u32, low)
  264. {
  265. if ((compat_ssize_t) count < 0)
  266. return -EINVAL;
  267. return sys_pread64(fd, ubuf, count, (unsigned long)high << 32 | low);
  268. }
  269. COMPAT_SYSCALL_DEFINE5(s390_pwrite64, unsigned int, fd, const char __user *, ubuf,
  270. compat_size_t, count, u32, high, u32, low)
  271. {
  272. if ((compat_ssize_t) count < 0)
  273. return -EINVAL;
  274. return sys_pwrite64(fd, ubuf, count, (unsigned long)high << 32 | low);
  275. }
  276. COMPAT_SYSCALL_DEFINE4(s390_readahead, int, fd, u32, high, u32, low, s32, count)
  277. {
  278. return sys_readahead(fd, (unsigned long)high << 32 | low, count);
  279. }
  280. struct stat64_emu31 {
  281. unsigned long long st_dev;
  282. unsigned int __pad1;
  283. #define STAT64_HAS_BROKEN_ST_INO 1
  284. u32 __st_ino;
  285. unsigned int st_mode;
  286. unsigned int st_nlink;
  287. u32 st_uid;
  288. u32 st_gid;
  289. unsigned long long st_rdev;
  290. unsigned int __pad3;
  291. long st_size;
  292. u32 st_blksize;
  293. unsigned char __pad4[4];
  294. u32 __pad5; /* future possible st_blocks high bits */
  295. u32 st_blocks; /* Number 512-byte blocks allocated. */
  296. u32 st_atime;
  297. u32 __pad6;
  298. u32 st_mtime;
  299. u32 __pad7;
  300. u32 st_ctime;
  301. u32 __pad8; /* will be high 32 bits of ctime someday */
  302. unsigned long st_ino;
  303. };
  304. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  305. {
  306. struct stat64_emu31 tmp;
  307. memset(&tmp, 0, sizeof(tmp));
  308. tmp.st_dev = huge_encode_dev(stat->dev);
  309. tmp.st_ino = stat->ino;
  310. tmp.__st_ino = (u32)stat->ino;
  311. tmp.st_mode = stat->mode;
  312. tmp.st_nlink = (unsigned int)stat->nlink;
  313. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  314. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  315. tmp.st_rdev = huge_encode_dev(stat->rdev);
  316. tmp.st_size = stat->size;
  317. tmp.st_blksize = (u32)stat->blksize;
  318. tmp.st_blocks = (u32)stat->blocks;
  319. tmp.st_atime = (u32)stat->atime.tv_sec;
  320. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  321. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  322. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  323. }
  324. COMPAT_SYSCALL_DEFINE2(s390_stat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  325. {
  326. struct kstat stat;
  327. int ret = vfs_stat(filename, &stat);
  328. if (!ret)
  329. ret = cp_stat64(statbuf, &stat);
  330. return ret;
  331. }
  332. COMPAT_SYSCALL_DEFINE2(s390_lstat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  333. {
  334. struct kstat stat;
  335. int ret = vfs_lstat(filename, &stat);
  336. if (!ret)
  337. ret = cp_stat64(statbuf, &stat);
  338. return ret;
  339. }
  340. COMPAT_SYSCALL_DEFINE2(s390_fstat64, unsigned int, fd, struct stat64_emu31 __user *, statbuf)
  341. {
  342. struct kstat stat;
  343. int ret = vfs_fstat(fd, &stat);
  344. if (!ret)
  345. ret = cp_stat64(statbuf, &stat);
  346. return ret;
  347. }
  348. COMPAT_SYSCALL_DEFINE4(s390_fstatat64, unsigned int, dfd, const char __user *, filename,
  349. struct stat64_emu31 __user *, statbuf, int, flag)
  350. {
  351. struct kstat stat;
  352. int error;
  353. error = vfs_fstatat(dfd, filename, &stat, flag);
  354. if (error)
  355. return error;
  356. return cp_stat64(statbuf, &stat);
  357. }
  358. /*
  359. * Linux/i386 didn't use to be able to handle more than
  360. * 4 system call parameters, so these system calls used a memory
  361. * block for parameter passing..
  362. */
  363. struct mmap_arg_struct_emu31 {
  364. compat_ulong_t addr;
  365. compat_ulong_t len;
  366. compat_ulong_t prot;
  367. compat_ulong_t flags;
  368. compat_ulong_t fd;
  369. compat_ulong_t offset;
  370. };
  371. COMPAT_SYSCALL_DEFINE1(s390_old_mmap, struct mmap_arg_struct_emu31 __user *, arg)
  372. {
  373. struct mmap_arg_struct_emu31 a;
  374. if (copy_from_user(&a, arg, sizeof(a)))
  375. return -EFAULT;
  376. if (a.offset & ~PAGE_MASK)
  377. return -EINVAL;
  378. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  379. a.offset >> PAGE_SHIFT);
  380. }
  381. COMPAT_SYSCALL_DEFINE1(s390_mmap2, struct mmap_arg_struct_emu31 __user *, arg)
  382. {
  383. struct mmap_arg_struct_emu31 a;
  384. if (copy_from_user(&a, arg, sizeof(a)))
  385. return -EFAULT;
  386. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  387. }
  388. COMPAT_SYSCALL_DEFINE3(s390_read, unsigned int, fd, char __user *, buf, compat_size_t, count)
  389. {
  390. if ((compat_ssize_t) count < 0)
  391. return -EINVAL;
  392. return sys_read(fd, buf, count);
  393. }
  394. COMPAT_SYSCALL_DEFINE3(s390_write, unsigned int, fd, const char __user *, buf, compat_size_t, count)
  395. {
  396. if ((compat_ssize_t) count < 0)
  397. return -EINVAL;
  398. return sys_write(fd, buf, count);
  399. }
  400. /*
  401. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  402. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  403. * because the 31 bit values differ from the 64 bit values.
  404. */
  405. COMPAT_SYSCALL_DEFINE5(s390_fadvise64, int, fd, u32, high, u32, low, compat_size_t, len, int, advise)
  406. {
  407. if (advise == 4)
  408. advise = POSIX_FADV_DONTNEED;
  409. else if (advise == 5)
  410. advise = POSIX_FADV_NOREUSE;
  411. return sys_fadvise64(fd, (unsigned long)high << 32 | low, len, advise);
  412. }
  413. struct fadvise64_64_args {
  414. int fd;
  415. long long offset;
  416. long long len;
  417. int advice;
  418. };
  419. COMPAT_SYSCALL_DEFINE1(s390_fadvise64_64, struct fadvise64_64_args __user *, args)
  420. {
  421. struct fadvise64_64_args a;
  422. if ( copy_from_user(&a, args, sizeof(a)) )
  423. return -EFAULT;
  424. if (a.advice == 4)
  425. a.advice = POSIX_FADV_DONTNEED;
  426. else if (a.advice == 5)
  427. a.advice = POSIX_FADV_NOREUSE;
  428. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  429. }
  430. COMPAT_SYSCALL_DEFINE6(s390_sync_file_range, int, fd, u32, offhigh, u32, offlow,
  431. u32, nhigh, u32, nlow, unsigned int, flags)
  432. {
  433. return sys_sync_file_range(fd, ((loff_t)offhigh << 32) + offlow,
  434. ((u64)nhigh << 32) + nlow, flags);
  435. }
  436. COMPAT_SYSCALL_DEFINE6(s390_fallocate, int, fd, int, mode, u32, offhigh, u32, offlow,
  437. u32, lenhigh, u32, lenlow)
  438. {
  439. return sys_fallocate(fd, mode, ((loff_t)offhigh << 32) + offlow,
  440. ((u64)lenhigh << 32) + lenlow);
  441. }