compat_linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/poll.h>
  30. #include <linux/personality.h>
  31. #include <linux/stat.h>
  32. #include <linux/filter.h>
  33. #include <linux/highmem.h>
  34. #include <linux/highuid.h>
  35. #include <linux/mman.h>
  36. #include <linux/ipv6.h>
  37. #include <linux/in.h>
  38. #include <linux/icmpv6.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/sysctl.h>
  41. #include <linux/binfmts.h>
  42. #include <linux/capability.h>
  43. #include <linux/compat.h>
  44. #include <linux/vfs.h>
  45. #include <linux/ptrace.h>
  46. #include <linux/fadvise.h>
  47. #include <linux/ipc.h>
  48. #include <linux/slab.h>
  49. #include <asm/types.h>
  50. #include <linux/uaccess.h>
  51. #include <net/scm.h>
  52. #include <net/sock.h>
  53. #include "compat_linux.h"
  54. /* For this source file, we want overflow handling. */
  55. #undef high2lowuid
  56. #undef high2lowgid
  57. #undef low2highuid
  58. #undef low2highgid
  59. #undef SET_UID16
  60. #undef SET_GID16
  61. #undef NEW_TO_OLD_UID
  62. #undef NEW_TO_OLD_GID
  63. #undef SET_OLDSTAT_UID
  64. #undef SET_OLDSTAT_GID
  65. #undef SET_STAT_UID
  66. #undef SET_STAT_GID
  67. #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
  68. #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
  69. #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
  70. #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
  71. #define SET_UID16(var, uid) var = high2lowuid(uid)
  72. #define SET_GID16(var, gid) var = high2lowgid(gid)
  73. #define NEW_TO_OLD_UID(uid) high2lowuid(uid)
  74. #define NEW_TO_OLD_GID(gid) high2lowgid(gid)
  75. #define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  76. #define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  77. #define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  78. #define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  79. COMPAT_SYSCALL_DEFINE3(s390_chown16, const char __user *, filename,
  80. u16, user, u16, group)
  81. {
  82. return sys_chown(filename, low2highuid(user), low2highgid(group));
  83. }
  84. COMPAT_SYSCALL_DEFINE3(s390_lchown16, const char __user *,
  85. filename, u16, user, u16, group)
  86. {
  87. return sys_lchown(filename, low2highuid(user), low2highgid(group));
  88. }
  89. COMPAT_SYSCALL_DEFINE3(s390_fchown16, unsigned int, fd, u16, user, u16, group)
  90. {
  91. return sys_fchown(fd, low2highuid(user), low2highgid(group));
  92. }
  93. COMPAT_SYSCALL_DEFINE2(s390_setregid16, u16, rgid, u16, egid)
  94. {
  95. return sys_setregid(low2highgid(rgid), low2highgid(egid));
  96. }
  97. COMPAT_SYSCALL_DEFINE1(s390_setgid16, u16, gid)
  98. {
  99. return sys_setgid((gid_t)gid);
  100. }
  101. COMPAT_SYSCALL_DEFINE2(s390_setreuid16, u16, ruid, u16, euid)
  102. {
  103. return sys_setreuid(low2highuid(ruid), low2highuid(euid));
  104. }
  105. COMPAT_SYSCALL_DEFINE1(s390_setuid16, u16, uid)
  106. {
  107. return sys_setuid((uid_t)uid);
  108. }
  109. COMPAT_SYSCALL_DEFINE3(s390_setresuid16, u16, ruid, u16, euid, u16, suid)
  110. {
  111. return sys_setresuid(low2highuid(ruid), low2highuid(euid),
  112. low2highuid(suid));
  113. }
  114. COMPAT_SYSCALL_DEFINE3(s390_getresuid16, u16 __user *, ruidp,
  115. u16 __user *, euidp, u16 __user *, suidp)
  116. {
  117. const struct cred *cred = current_cred();
  118. int retval;
  119. u16 ruid, euid, suid;
  120. ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
  121. euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
  122. suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
  123. if (!(retval = put_user(ruid, ruidp)) &&
  124. !(retval = put_user(euid, euidp)))
  125. retval = put_user(suid, suidp);
  126. return retval;
  127. }
  128. COMPAT_SYSCALL_DEFINE3(s390_setresgid16, u16, rgid, u16, egid, u16, sgid)
  129. {
  130. return sys_setresgid(low2highgid(rgid), low2highgid(egid),
  131. low2highgid(sgid));
  132. }
  133. COMPAT_SYSCALL_DEFINE3(s390_getresgid16, u16 __user *, rgidp,
  134. 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. COMPAT_SYSCALL_DEFINE1(s390_setfsuid16, u16, uid)
  148. {
  149. return sys_setfsuid((uid_t)uid);
  150. }
  151. COMPAT_SYSCALL_DEFINE1(s390_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_info->gid[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_info->gid[i] = kgid;
  182. }
  183. return 0;
  184. }
  185. COMPAT_SYSCALL_DEFINE2(s390_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. COMPAT_SYSCALL_DEFINE2(s390_setgroups16, int, gidsetsize, u16 __user *, grouplist)
  208. {
  209. struct group_info *group_info;
  210. int retval;
  211. if (!may_setgroups())
  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. COMPAT_SYSCALL_DEFINE0(s390_getuid16)
  228. {
  229. return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
  230. }
  231. COMPAT_SYSCALL_DEFINE0(s390_geteuid16)
  232. {
  233. return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
  234. }
  235. COMPAT_SYSCALL_DEFINE0(s390_getgid16)
  236. {
  237. return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
  238. }
  239. COMPAT_SYSCALL_DEFINE0(s390_getegid16)
  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, compat_ulong_t, second,
  245. compat_ulong_t, 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. COMPAT_SYSCALL_DEFINE3(s390_truncate64, const char __user *, path, u32, high, u32, low)
  253. {
  254. return sys_truncate(path, (unsigned long)high << 32 | low);
  255. }
  256. COMPAT_SYSCALL_DEFINE3(s390_ftruncate64, unsigned int, fd, u32, high, u32, low)
  257. {
  258. return sys_ftruncate(fd, (unsigned long)high << 32 | low);
  259. }
  260. COMPAT_SYSCALL_DEFINE5(s390_pread64, unsigned int, fd, char __user *, ubuf,
  261. compat_size_t, count, u32, high, u32, low)
  262. {
  263. if ((compat_ssize_t) count < 0)
  264. return -EINVAL;
  265. return sys_pread64(fd, ubuf, count, (unsigned long)high << 32 | low);
  266. }
  267. COMPAT_SYSCALL_DEFINE5(s390_pwrite64, unsigned int, fd, const char __user *, ubuf,
  268. compat_size_t, count, u32, high, u32, low)
  269. {
  270. if ((compat_ssize_t) count < 0)
  271. return -EINVAL;
  272. return sys_pwrite64(fd, ubuf, count, (unsigned long)high << 32 | low);
  273. }
  274. COMPAT_SYSCALL_DEFINE4(s390_readahead, int, fd, u32, high, u32, low, s32, count)
  275. {
  276. return sys_readahead(fd, (unsigned long)high << 32 | low, count);
  277. }
  278. struct stat64_emu31 {
  279. unsigned long long st_dev;
  280. unsigned int __pad1;
  281. #define STAT64_HAS_BROKEN_ST_INO 1
  282. u32 __st_ino;
  283. unsigned int st_mode;
  284. unsigned int st_nlink;
  285. u32 st_uid;
  286. u32 st_gid;
  287. unsigned long long st_rdev;
  288. unsigned int __pad3;
  289. long st_size;
  290. u32 st_blksize;
  291. unsigned char __pad4[4];
  292. u32 __pad5; /* future possible st_blocks high bits */
  293. u32 st_blocks; /* Number 512-byte blocks allocated. */
  294. u32 st_atime;
  295. u32 __pad6;
  296. u32 st_mtime;
  297. u32 __pad7;
  298. u32 st_ctime;
  299. u32 __pad8; /* will be high 32 bits of ctime someday */
  300. unsigned long st_ino;
  301. };
  302. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  303. {
  304. struct stat64_emu31 tmp;
  305. memset(&tmp, 0, sizeof(tmp));
  306. tmp.st_dev = huge_encode_dev(stat->dev);
  307. tmp.st_ino = stat->ino;
  308. tmp.__st_ino = (u32)stat->ino;
  309. tmp.st_mode = stat->mode;
  310. tmp.st_nlink = (unsigned int)stat->nlink;
  311. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  312. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  313. tmp.st_rdev = huge_encode_dev(stat->rdev);
  314. tmp.st_size = stat->size;
  315. tmp.st_blksize = (u32)stat->blksize;
  316. tmp.st_blocks = (u32)stat->blocks;
  317. tmp.st_atime = (u32)stat->atime.tv_sec;
  318. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  319. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  320. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  321. }
  322. COMPAT_SYSCALL_DEFINE2(s390_stat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  323. {
  324. struct kstat stat;
  325. int ret = vfs_stat(filename, &stat);
  326. if (!ret)
  327. ret = cp_stat64(statbuf, &stat);
  328. return ret;
  329. }
  330. COMPAT_SYSCALL_DEFINE2(s390_lstat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
  331. {
  332. struct kstat stat;
  333. int ret = vfs_lstat(filename, &stat);
  334. if (!ret)
  335. ret = cp_stat64(statbuf, &stat);
  336. return ret;
  337. }
  338. COMPAT_SYSCALL_DEFINE2(s390_fstat64, unsigned int, fd, struct stat64_emu31 __user *, statbuf)
  339. {
  340. struct kstat stat;
  341. int ret = vfs_fstat(fd, &stat);
  342. if (!ret)
  343. ret = cp_stat64(statbuf, &stat);
  344. return ret;
  345. }
  346. COMPAT_SYSCALL_DEFINE4(s390_fstatat64, unsigned int, dfd, const char __user *, filename,
  347. struct stat64_emu31 __user *, statbuf, int, flag)
  348. {
  349. struct kstat stat;
  350. int error;
  351. error = vfs_fstatat(dfd, filename, &stat, flag);
  352. if (error)
  353. return error;
  354. return cp_stat64(statbuf, &stat);
  355. }
  356. /*
  357. * Linux/i386 didn't use to be able to handle more than
  358. * 4 system call parameters, so these system calls used a memory
  359. * block for parameter passing..
  360. */
  361. struct mmap_arg_struct_emu31 {
  362. compat_ulong_t addr;
  363. compat_ulong_t len;
  364. compat_ulong_t prot;
  365. compat_ulong_t flags;
  366. compat_ulong_t fd;
  367. compat_ulong_t offset;
  368. };
  369. COMPAT_SYSCALL_DEFINE1(s390_old_mmap, struct mmap_arg_struct_emu31 __user *, arg)
  370. {
  371. struct mmap_arg_struct_emu31 a;
  372. if (copy_from_user(&a, arg, sizeof(a)))
  373. return -EFAULT;
  374. if (a.offset & ~PAGE_MASK)
  375. return -EINVAL;
  376. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  377. a.offset >> PAGE_SHIFT);
  378. }
  379. COMPAT_SYSCALL_DEFINE1(s390_mmap2, struct mmap_arg_struct_emu31 __user *, arg)
  380. {
  381. struct mmap_arg_struct_emu31 a;
  382. if (copy_from_user(&a, arg, sizeof(a)))
  383. return -EFAULT;
  384. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  385. }
  386. COMPAT_SYSCALL_DEFINE3(s390_read, unsigned int, fd, char __user *, buf, compat_size_t, count)
  387. {
  388. if ((compat_ssize_t) count < 0)
  389. return -EINVAL;
  390. return sys_read(fd, buf, count);
  391. }
  392. COMPAT_SYSCALL_DEFINE3(s390_write, unsigned int, fd, const char __user *, buf, compat_size_t, count)
  393. {
  394. if ((compat_ssize_t) count < 0)
  395. return -EINVAL;
  396. return sys_write(fd, buf, count);
  397. }
  398. /*
  399. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  400. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  401. * because the 31 bit values differ from the 64 bit values.
  402. */
  403. COMPAT_SYSCALL_DEFINE5(s390_fadvise64, int, fd, u32, high, u32, low, compat_size_t, len, int, advise)
  404. {
  405. if (advise == 4)
  406. advise = POSIX_FADV_DONTNEED;
  407. else if (advise == 5)
  408. advise = POSIX_FADV_NOREUSE;
  409. return sys_fadvise64(fd, (unsigned long)high << 32 | low, len, advise);
  410. }
  411. struct fadvise64_64_args {
  412. int fd;
  413. long long offset;
  414. long long len;
  415. int advice;
  416. };
  417. COMPAT_SYSCALL_DEFINE1(s390_fadvise64_64, struct fadvise64_64_args __user *, args)
  418. {
  419. struct fadvise64_64_args a;
  420. if ( copy_from_user(&a, args, sizeof(a)) )
  421. return -EFAULT;
  422. if (a.advice == 4)
  423. a.advice = POSIX_FADV_DONTNEED;
  424. else if (a.advice == 5)
  425. a.advice = POSIX_FADV_NOREUSE;
  426. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  427. }
  428. COMPAT_SYSCALL_DEFINE6(s390_sync_file_range, int, fd, u32, offhigh, u32, offlow,
  429. u32, nhigh, u32, nlow, unsigned int, flags)
  430. {
  431. return sys_sync_file_range(fd, ((loff_t)offhigh << 32) + offlow,
  432. ((u64)nhigh << 32) + nlow, flags);
  433. }
  434. COMPAT_SYSCALL_DEFINE6(s390_fallocate, int, fd, int, mode, u32, offhigh, u32, offlow,
  435. u32, lenhigh, u32, lenlow)
  436. {
  437. return sys_fallocate(fd, mode, ((loff_t)offhigh << 32) + offlow,
  438. ((u64)lenhigh << 32) + lenlow);
  439. }