compat_linux.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * arch/s390x/kernel/linux32.c
  3. *
  4. * S390 version
  5. * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. * Gerhard Tonn (ton@de.ibm.com)
  8. * Thomas Spatzier (tspat@de.ibm.com)
  9. *
  10. * Conversion between 31bit and 64bit native syscalls.
  11. *
  12. * Heavily inspired by the 32-bit Sparc compat code which is
  13. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  14. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/fs.h>
  20. #include <linux/mm.h>
  21. #include <linux/file.h>
  22. #include <linux/signal.h>
  23. #include <linux/resource.h>
  24. #include <linux/times.h>
  25. #include <linux/utsname.h>
  26. #include <linux/smp.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/sem.h>
  29. #include <linux/msg.h>
  30. #include <linux/shm.h>
  31. #include <linux/slab.h>
  32. #include <linux/uio.h>
  33. #include <linux/nfs_fs.h>
  34. #include <linux/quota.h>
  35. #include <linux/module.h>
  36. #include <linux/sunrpc/svc.h>
  37. #include <linux/nfsd/nfsd.h>
  38. #include <linux/nfsd/cache.h>
  39. #include <linux/nfsd/xdr.h>
  40. #include <linux/nfsd/syscall.h>
  41. #include <linux/poll.h>
  42. #include <linux/personality.h>
  43. #include <linux/stat.h>
  44. #include <linux/filter.h>
  45. #include <linux/highmem.h>
  46. #include <linux/highuid.h>
  47. #include <linux/mman.h>
  48. #include <linux/ipv6.h>
  49. #include <linux/in.h>
  50. #include <linux/icmpv6.h>
  51. #include <linux/syscalls.h>
  52. #include <linux/sysctl.h>
  53. #include <linux/binfmts.h>
  54. #include <linux/capability.h>
  55. #include <linux/compat.h>
  56. #include <linux/vfs.h>
  57. #include <linux/ptrace.h>
  58. #include <linux/fadvise.h>
  59. #include <asm/types.h>
  60. #include <asm/ipc.h>
  61. #include <asm/uaccess.h>
  62. #include <asm/semaphore.h>
  63. #include <net/scm.h>
  64. #include <net/sock.h>
  65. #include "compat_linux.h"
  66. /* For this source file, we want overflow handling. */
  67. #undef high2lowuid
  68. #undef high2lowgid
  69. #undef low2highuid
  70. #undef low2highgid
  71. #undef SET_UID16
  72. #undef SET_GID16
  73. #undef NEW_TO_OLD_UID
  74. #undef NEW_TO_OLD_GID
  75. #undef SET_OLDSTAT_UID
  76. #undef SET_OLDSTAT_GID
  77. #undef SET_STAT_UID
  78. #undef SET_STAT_GID
  79. #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
  80. #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
  81. #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
  82. #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
  83. #define SET_UID16(var, uid) var = high2lowuid(uid)
  84. #define SET_GID16(var, gid) var = high2lowgid(gid)
  85. #define NEW_TO_OLD_UID(uid) high2lowuid(uid)
  86. #define NEW_TO_OLD_GID(gid) high2lowgid(gid)
  87. #define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  88. #define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  89. #define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
  90. #define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
  91. asmlinkage long sys32_chown16(const char __user * filename, u16 user, u16 group)
  92. {
  93. return sys_chown(filename, low2highuid(user), low2highgid(group));
  94. }
  95. asmlinkage long sys32_lchown16(const char __user * filename, u16 user, u16 group)
  96. {
  97. return sys_lchown(filename, low2highuid(user), low2highgid(group));
  98. }
  99. asmlinkage long sys32_fchown16(unsigned int fd, u16 user, u16 group)
  100. {
  101. return sys_fchown(fd, low2highuid(user), low2highgid(group));
  102. }
  103. asmlinkage long sys32_setregid16(u16 rgid, u16 egid)
  104. {
  105. return sys_setregid(low2highgid(rgid), low2highgid(egid));
  106. }
  107. asmlinkage long sys32_setgid16(u16 gid)
  108. {
  109. return sys_setgid((gid_t)gid);
  110. }
  111. asmlinkage long sys32_setreuid16(u16 ruid, u16 euid)
  112. {
  113. return sys_setreuid(low2highuid(ruid), low2highuid(euid));
  114. }
  115. asmlinkage long sys32_setuid16(u16 uid)
  116. {
  117. return sys_setuid((uid_t)uid);
  118. }
  119. asmlinkage long sys32_setresuid16(u16 ruid, u16 euid, u16 suid)
  120. {
  121. return sys_setresuid(low2highuid(ruid), low2highuid(euid),
  122. low2highuid(suid));
  123. }
  124. asmlinkage long sys32_getresuid16(u16 __user *ruid, u16 __user *euid, u16 __user *suid)
  125. {
  126. int retval;
  127. if (!(retval = put_user(high2lowuid(current->uid), ruid)) &&
  128. !(retval = put_user(high2lowuid(current->euid), euid)))
  129. retval = put_user(high2lowuid(current->suid), suid);
  130. return retval;
  131. }
  132. asmlinkage long sys32_setresgid16(u16 rgid, u16 egid, u16 sgid)
  133. {
  134. return sys_setresgid(low2highgid(rgid), low2highgid(egid),
  135. low2highgid(sgid));
  136. }
  137. asmlinkage long sys32_getresgid16(u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
  138. {
  139. int retval;
  140. if (!(retval = put_user(high2lowgid(current->gid), rgid)) &&
  141. !(retval = put_user(high2lowgid(current->egid), egid)))
  142. retval = put_user(high2lowgid(current->sgid), sgid);
  143. return retval;
  144. }
  145. asmlinkage long sys32_setfsuid16(u16 uid)
  146. {
  147. return sys_setfsuid((uid_t)uid);
  148. }
  149. asmlinkage long sys32_setfsgid16(u16 gid)
  150. {
  151. return sys_setfsgid((gid_t)gid);
  152. }
  153. static int groups16_to_user(u16 __user *grouplist, struct group_info *group_info)
  154. {
  155. int i;
  156. u16 group;
  157. for (i = 0; i < group_info->ngroups; i++) {
  158. group = (u16)GROUP_AT(group_info, i);
  159. if (put_user(group, grouplist+i))
  160. return -EFAULT;
  161. }
  162. return 0;
  163. }
  164. static int groups16_from_user(struct group_info *group_info, u16 __user *grouplist)
  165. {
  166. int i;
  167. u16 group;
  168. for (i = 0; i < group_info->ngroups; i++) {
  169. if (get_user(group, grouplist+i))
  170. return -EFAULT;
  171. GROUP_AT(group_info, i) = (gid_t)group;
  172. }
  173. return 0;
  174. }
  175. asmlinkage long sys32_getgroups16(int gidsetsize, u16 __user *grouplist)
  176. {
  177. int i;
  178. if (gidsetsize < 0)
  179. return -EINVAL;
  180. get_group_info(current->group_info);
  181. i = current->group_info->ngroups;
  182. if (gidsetsize) {
  183. if (i > gidsetsize) {
  184. i = -EINVAL;
  185. goto out;
  186. }
  187. if (groups16_to_user(grouplist, current->group_info)) {
  188. i = -EFAULT;
  189. goto out;
  190. }
  191. }
  192. out:
  193. put_group_info(current->group_info);
  194. return i;
  195. }
  196. asmlinkage long sys32_setgroups16(int gidsetsize, u16 __user *grouplist)
  197. {
  198. struct group_info *group_info;
  199. int retval;
  200. if (!capable(CAP_SETGID))
  201. return -EPERM;
  202. if ((unsigned)gidsetsize > NGROUPS_MAX)
  203. return -EINVAL;
  204. group_info = groups_alloc(gidsetsize);
  205. if (!group_info)
  206. return -ENOMEM;
  207. retval = groups16_from_user(group_info, grouplist);
  208. if (retval) {
  209. put_group_info(group_info);
  210. return retval;
  211. }
  212. retval = set_current_groups(group_info);
  213. put_group_info(group_info);
  214. return retval;
  215. }
  216. asmlinkage long sys32_getuid16(void)
  217. {
  218. return high2lowuid(current->uid);
  219. }
  220. asmlinkage long sys32_geteuid16(void)
  221. {
  222. return high2lowuid(current->euid);
  223. }
  224. asmlinkage long sys32_getgid16(void)
  225. {
  226. return high2lowgid(current->gid);
  227. }
  228. asmlinkage long sys32_getegid16(void)
  229. {
  230. return high2lowgid(current->egid);
  231. }
  232. /* 32-bit timeval and related flotsam. */
  233. static inline long get_tv32(struct timeval *o, struct compat_timeval __user *i)
  234. {
  235. return (!access_ok(VERIFY_READ, o, sizeof(*o)) ||
  236. (__get_user(o->tv_sec, &i->tv_sec) ||
  237. __get_user(o->tv_usec, &i->tv_usec)));
  238. }
  239. static inline long put_tv32(struct compat_timeval __user *o, struct timeval *i)
  240. {
  241. return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
  242. (__put_user(i->tv_sec, &o->tv_sec) ||
  243. __put_user(i->tv_usec, &o->tv_usec)));
  244. }
  245. /*
  246. * sys32_ipc() is the de-multiplexer for the SysV IPC calls in 32bit emulation.
  247. *
  248. * This is really horribly ugly.
  249. */
  250. asmlinkage long sys32_ipc(u32 call, int first, int second, int third, u32 ptr)
  251. {
  252. if (call >> 16) /* hack for backward compatibility */
  253. return -EINVAL;
  254. call &= 0xffff;
  255. switch (call) {
  256. case SEMTIMEDOP:
  257. return compat_sys_semtimedop(first, compat_ptr(ptr),
  258. second, compat_ptr(third));
  259. case SEMOP:
  260. /* struct sembuf is the same on 32 and 64bit :)) */
  261. return sys_semtimedop(first, compat_ptr(ptr),
  262. second, NULL);
  263. case SEMGET:
  264. return sys_semget(first, second, third);
  265. case SEMCTL:
  266. return compat_sys_semctl(first, second, third,
  267. compat_ptr(ptr));
  268. case MSGSND:
  269. return compat_sys_msgsnd(first, second, third,
  270. compat_ptr(ptr));
  271. case MSGRCV:
  272. return compat_sys_msgrcv(first, second, 0, third,
  273. 0, compat_ptr(ptr));
  274. case MSGGET:
  275. return sys_msgget((key_t) first, second);
  276. case MSGCTL:
  277. return compat_sys_msgctl(first, second, compat_ptr(ptr));
  278. case SHMAT:
  279. return compat_sys_shmat(first, second, third,
  280. 0, compat_ptr(ptr));
  281. case SHMDT:
  282. return sys_shmdt(compat_ptr(ptr));
  283. case SHMGET:
  284. return sys_shmget(first, (unsigned)second, third);
  285. case SHMCTL:
  286. return compat_sys_shmctl(first, second, compat_ptr(ptr));
  287. }
  288. return -ENOSYS;
  289. }
  290. asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
  291. {
  292. if ((int)high < 0)
  293. return -EINVAL;
  294. else
  295. return sys_truncate(path, (high << 32) | low);
  296. }
  297. asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
  298. {
  299. if ((int)high < 0)
  300. return -EINVAL;
  301. else
  302. return sys_ftruncate(fd, (high << 32) | low);
  303. }
  304. int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
  305. {
  306. compat_ino_t ino;
  307. int err;
  308. if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
  309. return -EOVERFLOW;
  310. ino = stat->ino;
  311. if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
  312. return -EOVERFLOW;
  313. err = put_user(old_encode_dev(stat->dev), &statbuf->st_dev);
  314. err |= put_user(stat->ino, &statbuf->st_ino);
  315. err |= put_user(stat->mode, &statbuf->st_mode);
  316. err |= put_user(stat->nlink, &statbuf->st_nlink);
  317. err |= put_user(high2lowuid(stat->uid), &statbuf->st_uid);
  318. err |= put_user(high2lowgid(stat->gid), &statbuf->st_gid);
  319. err |= put_user(old_encode_dev(stat->rdev), &statbuf->st_rdev);
  320. err |= put_user(stat->size, &statbuf->st_size);
  321. err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
  322. err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
  323. err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
  324. err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
  325. err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
  326. err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
  327. err |= put_user(stat->blksize, &statbuf->st_blksize);
  328. err |= put_user(stat->blocks, &statbuf->st_blocks);
  329. /* fixme
  330. err |= put_user(0, &statbuf->__unused4[0]);
  331. err |= put_user(0, &statbuf->__unused4[1]);
  332. */
  333. return err;
  334. }
  335. struct sysinfo32 {
  336. s32 uptime;
  337. u32 loads[3];
  338. u32 totalram;
  339. u32 freeram;
  340. u32 sharedram;
  341. u32 bufferram;
  342. u32 totalswap;
  343. u32 freeswap;
  344. unsigned short procs;
  345. unsigned short pads;
  346. u32 totalhigh;
  347. u32 freehigh;
  348. unsigned int mem_unit;
  349. char _f[8];
  350. };
  351. asmlinkage long sys32_sysinfo(struct sysinfo32 __user *info)
  352. {
  353. struct sysinfo s;
  354. int ret, err;
  355. mm_segment_t old_fs = get_fs ();
  356. set_fs (KERNEL_DS);
  357. ret = sys_sysinfo((struct sysinfo __user *) &s);
  358. set_fs (old_fs);
  359. err = put_user (s.uptime, &info->uptime);
  360. err |= __put_user (s.loads[0], &info->loads[0]);
  361. err |= __put_user (s.loads[1], &info->loads[1]);
  362. err |= __put_user (s.loads[2], &info->loads[2]);
  363. err |= __put_user (s.totalram, &info->totalram);
  364. err |= __put_user (s.freeram, &info->freeram);
  365. err |= __put_user (s.sharedram, &info->sharedram);
  366. err |= __put_user (s.bufferram, &info->bufferram);
  367. err |= __put_user (s.totalswap, &info->totalswap);
  368. err |= __put_user (s.freeswap, &info->freeswap);
  369. err |= __put_user (s.procs, &info->procs);
  370. err |= __put_user (s.totalhigh, &info->totalhigh);
  371. err |= __put_user (s.freehigh, &info->freehigh);
  372. err |= __put_user (s.mem_unit, &info->mem_unit);
  373. if (err)
  374. return -EFAULT;
  375. return ret;
  376. }
  377. asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
  378. struct compat_timespec __user *interval)
  379. {
  380. struct timespec t;
  381. int ret;
  382. mm_segment_t old_fs = get_fs ();
  383. set_fs (KERNEL_DS);
  384. ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
  385. set_fs (old_fs);
  386. if (put_compat_timespec(&t, interval))
  387. return -EFAULT;
  388. return ret;
  389. }
  390. asmlinkage long sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
  391. compat_sigset_t __user *oset, size_t sigsetsize)
  392. {
  393. sigset_t s;
  394. compat_sigset_t s32;
  395. int ret;
  396. mm_segment_t old_fs = get_fs();
  397. if (set) {
  398. if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
  399. return -EFAULT;
  400. switch (_NSIG_WORDS) {
  401. case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
  402. case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
  403. case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
  404. case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
  405. }
  406. }
  407. set_fs (KERNEL_DS);
  408. ret = sys_rt_sigprocmask(how,
  409. set ? (sigset_t __user *) &s : NULL,
  410. oset ? (sigset_t __user *) &s : NULL,
  411. sigsetsize);
  412. set_fs (old_fs);
  413. if (ret) return ret;
  414. if (oset) {
  415. switch (_NSIG_WORDS) {
  416. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  417. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  418. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  419. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  420. }
  421. if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
  422. return -EFAULT;
  423. }
  424. return 0;
  425. }
  426. asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
  427. size_t sigsetsize)
  428. {
  429. sigset_t s;
  430. compat_sigset_t s32;
  431. int ret;
  432. mm_segment_t old_fs = get_fs();
  433. set_fs (KERNEL_DS);
  434. ret = sys_rt_sigpending((sigset_t __user *) &s, sigsetsize);
  435. set_fs (old_fs);
  436. if (!ret) {
  437. switch (_NSIG_WORDS) {
  438. case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
  439. case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
  440. case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
  441. case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
  442. }
  443. if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
  444. return -EFAULT;
  445. }
  446. return ret;
  447. }
  448. asmlinkage long
  449. sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
  450. {
  451. siginfo_t info;
  452. int ret;
  453. mm_segment_t old_fs = get_fs();
  454. if (copy_siginfo_from_user32(&info, uinfo))
  455. return -EFAULT;
  456. set_fs (KERNEL_DS);
  457. ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *) &info);
  458. set_fs (old_fs);
  459. return ret;
  460. }
  461. /*
  462. * sys32_execve() executes a new program after the asm stub has set
  463. * things up for us. This should basically do what I want it to.
  464. */
  465. asmlinkage long
  466. sys32_execve(struct pt_regs regs)
  467. {
  468. int error;
  469. char * filename;
  470. filename = getname(compat_ptr(regs.orig_gpr2));
  471. error = PTR_ERR(filename);
  472. if (IS_ERR(filename))
  473. goto out;
  474. error = compat_do_execve(filename, compat_ptr(regs.gprs[3]),
  475. compat_ptr(regs.gprs[4]), &regs);
  476. if (error == 0)
  477. {
  478. task_lock(current);
  479. current->ptrace &= ~PT_DTRACE;
  480. task_unlock(current);
  481. current->thread.fp_regs.fpc=0;
  482. asm volatile("sfpc %0,0" : : "d" (0));
  483. }
  484. putname(filename);
  485. out:
  486. return error;
  487. }
  488. #ifdef CONFIG_MODULES
  489. asmlinkage long
  490. sys32_init_module(void __user *umod, unsigned long len,
  491. const char __user *uargs)
  492. {
  493. return sys_init_module(umod, len, uargs);
  494. }
  495. asmlinkage long
  496. sys32_delete_module(const char __user *name_user, unsigned int flags)
  497. {
  498. return sys_delete_module(name_user, flags);
  499. }
  500. #else /* CONFIG_MODULES */
  501. asmlinkage long
  502. sys32_init_module(void __user *umod, unsigned long len,
  503. const char __user *uargs)
  504. {
  505. return -ENOSYS;
  506. }
  507. asmlinkage long
  508. sys32_delete_module(const char __user *name_user, unsigned int flags)
  509. {
  510. return -ENOSYS;
  511. }
  512. #endif /* CONFIG_MODULES */
  513. /* Translations due to time_t size differences. Which affects all
  514. sorts of things, like timeval and itimerval. */
  515. extern struct timezone sys_tz;
  516. asmlinkage long sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  517. {
  518. if (tv) {
  519. struct timeval ktv;
  520. do_gettimeofday(&ktv);
  521. if (put_tv32(tv, &ktv))
  522. return -EFAULT;
  523. }
  524. if (tz) {
  525. if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  526. return -EFAULT;
  527. }
  528. return 0;
  529. }
  530. static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
  531. {
  532. long usec;
  533. if (!access_ok(VERIFY_READ, i, sizeof(*i)))
  534. return -EFAULT;
  535. if (__get_user(o->tv_sec, &i->tv_sec))
  536. return -EFAULT;
  537. if (__get_user(usec, &i->tv_usec))
  538. return -EFAULT;
  539. o->tv_nsec = usec * 1000;
  540. return 0;
  541. }
  542. asmlinkage long sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
  543. {
  544. struct timespec kts;
  545. struct timezone ktz;
  546. if (tv) {
  547. if (get_ts32(&kts, tv))
  548. return -EFAULT;
  549. }
  550. if (tz) {
  551. if (copy_from_user(&ktz, tz, sizeof(ktz)))
  552. return -EFAULT;
  553. }
  554. return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
  555. }
  556. /* These are here just in case some old sparc32 binary calls it. */
  557. asmlinkage long sys32_pause(void)
  558. {
  559. current->state = TASK_INTERRUPTIBLE;
  560. schedule();
  561. return -ERESTARTNOHAND;
  562. }
  563. asmlinkage long sys32_pread64(unsigned int fd, char __user *ubuf,
  564. size_t count, u32 poshi, u32 poslo)
  565. {
  566. if ((compat_ssize_t) count < 0)
  567. return -EINVAL;
  568. return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  569. }
  570. asmlinkage long sys32_pwrite64(unsigned int fd, const char __user *ubuf,
  571. size_t count, u32 poshi, u32 poslo)
  572. {
  573. if ((compat_ssize_t) count < 0)
  574. return -EINVAL;
  575. return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
  576. }
  577. asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
  578. {
  579. return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
  580. }
  581. asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, size_t count)
  582. {
  583. mm_segment_t old_fs = get_fs();
  584. int ret;
  585. off_t of;
  586. if (offset && get_user(of, offset))
  587. return -EFAULT;
  588. set_fs(KERNEL_DS);
  589. ret = sys_sendfile(out_fd, in_fd,
  590. offset ? (off_t __user *) &of : NULL, count);
  591. set_fs(old_fs);
  592. if (offset && put_user(of, offset))
  593. return -EFAULT;
  594. return ret;
  595. }
  596. asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
  597. compat_loff_t __user *offset, s32 count)
  598. {
  599. mm_segment_t old_fs = get_fs();
  600. int ret;
  601. loff_t lof;
  602. if (offset && get_user(lof, offset))
  603. return -EFAULT;
  604. set_fs(KERNEL_DS);
  605. ret = sys_sendfile64(out_fd, in_fd,
  606. offset ? (loff_t __user *) &lof : NULL, count);
  607. set_fs(old_fs);
  608. if (offset && put_user(lof, offset))
  609. return -EFAULT;
  610. return ret;
  611. }
  612. #ifdef CONFIG_SYSCTL_SYSCALL
  613. struct __sysctl_args32 {
  614. u32 name;
  615. int nlen;
  616. u32 oldval;
  617. u32 oldlenp;
  618. u32 newval;
  619. u32 newlen;
  620. u32 __unused[4];
  621. };
  622. asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
  623. {
  624. struct __sysctl_args32 tmp;
  625. int error;
  626. size_t oldlen;
  627. size_t __user *oldlenp = NULL;
  628. unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
  629. if (copy_from_user(&tmp, args, sizeof(tmp)))
  630. return -EFAULT;
  631. if (tmp.oldval && tmp.oldlenp) {
  632. /* Duh, this is ugly and might not work if sysctl_args
  633. is in read-only memory, but do_sysctl does indirectly
  634. a lot of uaccess in both directions and we'd have to
  635. basically copy the whole sysctl.c here, and
  636. glibc's __sysctl uses rw memory for the structure
  637. anyway. */
  638. if (get_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)) ||
  639. put_user(oldlen, (size_t __user *)addr))
  640. return -EFAULT;
  641. oldlenp = (size_t __user *)addr;
  642. }
  643. lock_kernel();
  644. error = do_sysctl(compat_ptr(tmp.name), tmp.nlen, compat_ptr(tmp.oldval),
  645. oldlenp, compat_ptr(tmp.newval), tmp.newlen);
  646. unlock_kernel();
  647. if (oldlenp) {
  648. if (!error) {
  649. if (get_user(oldlen, (size_t __user *)addr) ||
  650. put_user(oldlen, (u32 __user *)compat_ptr(tmp.oldlenp)))
  651. error = -EFAULT;
  652. }
  653. copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
  654. }
  655. return error;
  656. }
  657. #endif
  658. struct stat64_emu31 {
  659. unsigned long long st_dev;
  660. unsigned int __pad1;
  661. #define STAT64_HAS_BROKEN_ST_INO 1
  662. u32 __st_ino;
  663. unsigned int st_mode;
  664. unsigned int st_nlink;
  665. u32 st_uid;
  666. u32 st_gid;
  667. unsigned long long st_rdev;
  668. unsigned int __pad3;
  669. long st_size;
  670. u32 st_blksize;
  671. unsigned char __pad4[4];
  672. u32 __pad5; /* future possible st_blocks high bits */
  673. u32 st_blocks; /* Number 512-byte blocks allocated. */
  674. u32 st_atime;
  675. u32 __pad6;
  676. u32 st_mtime;
  677. u32 __pad7;
  678. u32 st_ctime;
  679. u32 __pad8; /* will be high 32 bits of ctime someday */
  680. unsigned long st_ino;
  681. };
  682. static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
  683. {
  684. struct stat64_emu31 tmp;
  685. memset(&tmp, 0, sizeof(tmp));
  686. tmp.st_dev = huge_encode_dev(stat->dev);
  687. tmp.st_ino = stat->ino;
  688. tmp.__st_ino = (u32)stat->ino;
  689. tmp.st_mode = stat->mode;
  690. tmp.st_nlink = (unsigned int)stat->nlink;
  691. tmp.st_uid = stat->uid;
  692. tmp.st_gid = stat->gid;
  693. tmp.st_rdev = huge_encode_dev(stat->rdev);
  694. tmp.st_size = stat->size;
  695. tmp.st_blksize = (u32)stat->blksize;
  696. tmp.st_blocks = (u32)stat->blocks;
  697. tmp.st_atime = (u32)stat->atime.tv_sec;
  698. tmp.st_mtime = (u32)stat->mtime.tv_sec;
  699. tmp.st_ctime = (u32)stat->ctime.tv_sec;
  700. return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  701. }
  702. asmlinkage long sys32_stat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  703. {
  704. struct kstat stat;
  705. int ret = vfs_stat(filename, &stat);
  706. if (!ret)
  707. ret = cp_stat64(statbuf, &stat);
  708. return ret;
  709. }
  710. asmlinkage long sys32_lstat64(char __user * filename, struct stat64_emu31 __user * statbuf)
  711. {
  712. struct kstat stat;
  713. int ret = vfs_lstat(filename, &stat);
  714. if (!ret)
  715. ret = cp_stat64(statbuf, &stat);
  716. return ret;
  717. }
  718. asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 __user * statbuf)
  719. {
  720. struct kstat stat;
  721. int ret = vfs_fstat(fd, &stat);
  722. if (!ret)
  723. ret = cp_stat64(statbuf, &stat);
  724. return ret;
  725. }
  726. asmlinkage long sys32_fstatat64(unsigned int dfd, char __user *filename,
  727. struct stat64_emu31 __user* statbuf, int flag)
  728. {
  729. struct kstat stat;
  730. int error = -EINVAL;
  731. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  732. goto out;
  733. if (flag & AT_SYMLINK_NOFOLLOW)
  734. error = vfs_lstat_fd(dfd, filename, &stat);
  735. else
  736. error = vfs_stat_fd(dfd, filename, &stat);
  737. if (!error)
  738. error = cp_stat64(statbuf, &stat);
  739. out:
  740. return error;
  741. }
  742. /*
  743. * Linux/i386 didn't use to be able to handle more than
  744. * 4 system call parameters, so these system calls used a memory
  745. * block for parameter passing..
  746. */
  747. struct mmap_arg_struct_emu31 {
  748. u32 addr;
  749. u32 len;
  750. u32 prot;
  751. u32 flags;
  752. u32 fd;
  753. u32 offset;
  754. };
  755. /* common code for old and new mmaps */
  756. static inline long do_mmap2(
  757. unsigned long addr, unsigned long len,
  758. unsigned long prot, unsigned long flags,
  759. unsigned long fd, unsigned long pgoff)
  760. {
  761. struct file * file = NULL;
  762. unsigned long error = -EBADF;
  763. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  764. if (!(flags & MAP_ANONYMOUS)) {
  765. file = fget(fd);
  766. if (!file)
  767. goto out;
  768. }
  769. down_write(&current->mm->mmap_sem);
  770. error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  771. if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
  772. /* Result is out of bounds. */
  773. do_munmap(current->mm, addr, len);
  774. error = -ENOMEM;
  775. }
  776. up_write(&current->mm->mmap_sem);
  777. if (file)
  778. fput(file);
  779. out:
  780. return error;
  781. }
  782. asmlinkage unsigned long
  783. old32_mmap(struct mmap_arg_struct_emu31 __user *arg)
  784. {
  785. struct mmap_arg_struct_emu31 a;
  786. int error = -EFAULT;
  787. if (copy_from_user(&a, arg, sizeof(a)))
  788. goto out;
  789. error = -EINVAL;
  790. if (a.offset & ~PAGE_MASK)
  791. goto out;
  792. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
  793. out:
  794. return error;
  795. }
  796. asmlinkage long
  797. sys32_mmap2(struct mmap_arg_struct_emu31 __user *arg)
  798. {
  799. struct mmap_arg_struct_emu31 a;
  800. int error = -EFAULT;
  801. if (copy_from_user(&a, arg, sizeof(a)))
  802. goto out;
  803. error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
  804. out:
  805. return error;
  806. }
  807. asmlinkage long sys32_read(unsigned int fd, char __user * buf, size_t count)
  808. {
  809. if ((compat_ssize_t) count < 0)
  810. return -EINVAL;
  811. return sys_read(fd, buf, count);
  812. }
  813. asmlinkage long sys32_write(unsigned int fd, char __user * buf, size_t count)
  814. {
  815. if ((compat_ssize_t) count < 0)
  816. return -EINVAL;
  817. return sys_write(fd, buf, count);
  818. }
  819. asmlinkage long sys32_clone(struct pt_regs regs)
  820. {
  821. unsigned long clone_flags;
  822. unsigned long newsp;
  823. int __user *parent_tidptr, *child_tidptr;
  824. clone_flags = regs.gprs[3] & 0xffffffffUL;
  825. newsp = regs.orig_gpr2 & 0x7fffffffUL;
  826. parent_tidptr = compat_ptr(regs.gprs[4]);
  827. child_tidptr = compat_ptr(regs.gprs[5]);
  828. if (!newsp)
  829. newsp = regs.gprs[15];
  830. return do_fork(clone_flags, newsp, &regs, 0,
  831. parent_tidptr, child_tidptr);
  832. }
  833. /*
  834. * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
  835. * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
  836. * because the 31 bit values differ from the 64 bit values.
  837. */
  838. asmlinkage long
  839. sys32_fadvise64(int fd, loff_t offset, size_t len, int advise)
  840. {
  841. if (advise == 4)
  842. advise = POSIX_FADV_DONTNEED;
  843. else if (advise == 5)
  844. advise = POSIX_FADV_NOREUSE;
  845. return sys_fadvise64(fd, offset, len, advise);
  846. }
  847. struct fadvise64_64_args {
  848. int fd;
  849. long long offset;
  850. long long len;
  851. int advice;
  852. };
  853. asmlinkage long
  854. sys32_fadvise64_64(struct fadvise64_64_args __user *args)
  855. {
  856. struct fadvise64_64_args a;
  857. if ( copy_from_user(&a, args, sizeof(a)) )
  858. return -EFAULT;
  859. if (a.advice == 4)
  860. a.advice = POSIX_FADV_DONTNEED;
  861. else if (a.advice == 5)
  862. a.advice = POSIX_FADV_NOREUSE;
  863. return sys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
  864. }