sys_oabi-compat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * arch/arm/kernel/sys_oabi-compat.c
  3. *
  4. * Compatibility wrappers for syscalls that are used from
  5. * old ABI user space binaries with an EABI kernel.
  6. *
  7. * Author: Nicolas Pitre
  8. * Created: Oct 7, 2005
  9. * Copyright: MontaVista Software, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. /*
  16. * The legacy ABI and the new ARM EABI have different rules making some
  17. * syscalls incompatible especially with structure arguments.
  18. * Most notably, Eabi says 64-bit members should be 64-bit aligned instead of
  19. * simply word aligned. EABI also pads structures to the size of the largest
  20. * member it contains instead of the invariant 32-bit.
  21. *
  22. * The following syscalls are affected:
  23. *
  24. * sys_stat64:
  25. * sys_lstat64:
  26. * sys_fstat64:
  27. *
  28. * struct stat64 has different sizes and some members are shifted
  29. * Compatibility wrappers are needed for them and provided below.
  30. *
  31. * sys_fcntl64:
  32. *
  33. * struct flock64 has different sizes and some members are shifted
  34. * A compatibility wrapper is needed and provided below.
  35. *
  36. * sys_statfs64:
  37. * sys_fstatfs64:
  38. *
  39. * struct statfs64 has extra padding with EABI growing its size from
  40. * 84 to 88. This struct is now __attribute__((packed,aligned(4)))
  41. * with a small assembly wrapper to force the sz argument to 84 if it is 88
  42. * to avoid copying the extra padding over user space unexpecting it.
  43. *
  44. * sys_newuname:
  45. *
  46. * struct new_utsname has no padding with EABI. No problem there.
  47. *
  48. * sys_epoll_ctl:
  49. * sys_epoll_wait:
  50. *
  51. * struct epoll_event has its second member shifted also affecting the
  52. * structure size. Compatibility wrappers are needed and provided below.
  53. *
  54. * sys_ipc:
  55. * sys_semop:
  56. * sys_semtimedop:
  57. *
  58. * struct sembuf loses its padding with EABI. Since arrays of them are
  59. * used they have to be copyed to remove the padding. Compatibility wrappers
  60. * provided below.
  61. *
  62. * sys_bind:
  63. * sys_connect:
  64. * sys_sendmsg:
  65. * sys_sendto:
  66. * sys_socketcall:
  67. *
  68. * struct sockaddr_un loses its padding with EABI. Since the size of the
  69. * structure is used as a validation test in unix_mkname(), we need to
  70. * change the length argument to 110 whenever it is 112. Compatibility
  71. * wrappers provided below.
  72. */
  73. #include <linux/syscalls.h>
  74. #include <linux/errno.h>
  75. #include <linux/fs.h>
  76. #include <linux/fcntl.h>
  77. #include <linux/eventpoll.h>
  78. #include <linux/sem.h>
  79. #include <linux/socket.h>
  80. #include <linux/net.h>
  81. #include <asm/ipc.h>
  82. #include <asm/uaccess.h>
  83. struct oldabi_stat64 {
  84. unsigned long long st_dev;
  85. unsigned int __pad1;
  86. unsigned long __st_ino;
  87. unsigned int st_mode;
  88. unsigned int st_nlink;
  89. unsigned long st_uid;
  90. unsigned long st_gid;
  91. unsigned long long st_rdev;
  92. unsigned int __pad2;
  93. long long st_size;
  94. unsigned long st_blksize;
  95. unsigned long long st_blocks;
  96. unsigned long st_atime;
  97. unsigned long st_atime_nsec;
  98. unsigned long st_mtime;
  99. unsigned long st_mtime_nsec;
  100. unsigned long st_ctime;
  101. unsigned long st_ctime_nsec;
  102. unsigned long long st_ino;
  103. } __attribute__ ((packed,aligned(4)));
  104. static long cp_oldabi_stat64(struct kstat *stat,
  105. struct oldabi_stat64 __user *statbuf)
  106. {
  107. struct oldabi_stat64 tmp;
  108. tmp.st_dev = huge_encode_dev(stat->dev);
  109. tmp.__pad1 = 0;
  110. tmp.__st_ino = stat->ino;
  111. tmp.st_mode = stat->mode;
  112. tmp.st_nlink = stat->nlink;
  113. tmp.st_uid = stat->uid;
  114. tmp.st_gid = stat->gid;
  115. tmp.st_rdev = huge_encode_dev(stat->rdev);
  116. tmp.st_size = stat->size;
  117. tmp.st_blocks = stat->blocks;
  118. tmp.__pad2 = 0;
  119. tmp.st_blksize = stat->blksize;
  120. tmp.st_atime = stat->atime.tv_sec;
  121. tmp.st_atime_nsec = stat->atime.tv_nsec;
  122. tmp.st_mtime = stat->mtime.tv_sec;
  123. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  124. tmp.st_ctime = stat->ctime.tv_sec;
  125. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  126. tmp.st_ino = stat->ino;
  127. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  128. }
  129. asmlinkage long sys_oabi_stat64(char __user * filename,
  130. struct oldabi_stat64 __user * statbuf)
  131. {
  132. struct kstat stat;
  133. int error = vfs_stat(filename, &stat);
  134. if (!error)
  135. error = cp_oldabi_stat64(&stat, statbuf);
  136. return error;
  137. }
  138. asmlinkage long sys_oabi_lstat64(char __user * filename,
  139. struct oldabi_stat64 __user * statbuf)
  140. {
  141. struct kstat stat;
  142. int error = vfs_lstat(filename, &stat);
  143. if (!error)
  144. error = cp_oldabi_stat64(&stat, statbuf);
  145. return error;
  146. }
  147. asmlinkage long sys_oabi_fstat64(unsigned long fd,
  148. struct oldabi_stat64 __user * statbuf)
  149. {
  150. struct kstat stat;
  151. int error = vfs_fstat(fd, &stat);
  152. if (!error)
  153. error = cp_oldabi_stat64(&stat, statbuf);
  154. return error;
  155. }
  156. struct oabi_flock64 {
  157. short l_type;
  158. short l_whence;
  159. loff_t l_start;
  160. loff_t l_len;
  161. pid_t l_pid;
  162. } __attribute__ ((packed,aligned(4)));
  163. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  164. unsigned long arg)
  165. {
  166. struct oabi_flock64 user;
  167. struct flock64 kernel;
  168. mm_segment_t fs = USER_DS; /* initialized to kill a warning */
  169. unsigned long local_arg = arg;
  170. int ret;
  171. switch (cmd) {
  172. case F_GETLK64:
  173. case F_SETLK64:
  174. case F_SETLKW64:
  175. if (copy_from_user(&user, (struct oabi_flock64 __user *)arg,
  176. sizeof(user)))
  177. return -EFAULT;
  178. kernel.l_type = user.l_type;
  179. kernel.l_whence = user.l_whence;
  180. kernel.l_start = user.l_start;
  181. kernel.l_len = user.l_len;
  182. kernel.l_pid = user.l_pid;
  183. local_arg = (unsigned long)&kernel;
  184. fs = get_fs();
  185. set_fs(KERNEL_DS);
  186. }
  187. ret = sys_fcntl64(fd, cmd, local_arg);
  188. switch (cmd) {
  189. case F_GETLK64:
  190. if (!ret) {
  191. user.l_type = kernel.l_type;
  192. user.l_whence = kernel.l_whence;
  193. user.l_start = kernel.l_start;
  194. user.l_len = kernel.l_len;
  195. user.l_pid = kernel.l_pid;
  196. if (copy_to_user((struct oabi_flock64 __user *)arg,
  197. &user, sizeof(user)))
  198. ret = -EFAULT;
  199. }
  200. case F_SETLK64:
  201. case F_SETLKW64:
  202. set_fs(fs);
  203. }
  204. return ret;
  205. }
  206. struct oabi_epoll_event {
  207. __u32 events;
  208. __u64 data;
  209. } __attribute__ ((packed,aligned(4)));
  210. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  211. struct oabi_epoll_event __user *event)
  212. {
  213. struct oabi_epoll_event user;
  214. struct epoll_event kernel;
  215. mm_segment_t fs;
  216. long ret;
  217. if (op == EPOLL_CTL_DEL)
  218. return sys_epoll_ctl(epfd, op, fd, NULL);
  219. if (copy_from_user(&user, event, sizeof(user)))
  220. return -EFAULT;
  221. kernel.events = user.events;
  222. kernel.data = user.data;
  223. fs = get_fs();
  224. set_fs(KERNEL_DS);
  225. ret = sys_epoll_ctl(epfd, op, fd, &kernel);
  226. set_fs(fs);
  227. return ret;
  228. }
  229. asmlinkage long sys_oabi_epoll_wait(int epfd,
  230. struct oabi_epoll_event __user *events,
  231. int maxevents, int timeout)
  232. {
  233. struct epoll_event *kbuf;
  234. mm_segment_t fs;
  235. long ret, err, i;
  236. if (maxevents <= 0 || maxevents > (INT_MAX/sizeof(struct epoll_event)))
  237. return -EINVAL;
  238. kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
  239. if (!kbuf)
  240. return -ENOMEM;
  241. fs = get_fs();
  242. set_fs(KERNEL_DS);
  243. ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
  244. set_fs(fs);
  245. err = 0;
  246. for (i = 0; i < ret; i++) {
  247. __put_user_error(kbuf[i].events, &events->events, err);
  248. __put_user_error(kbuf[i].data, &events->data, err);
  249. events++;
  250. }
  251. kfree(kbuf);
  252. return err ? -EFAULT : ret;
  253. }
  254. struct oabi_sembuf {
  255. unsigned short sem_num;
  256. short sem_op;
  257. short sem_flg;
  258. unsigned short __pad;
  259. };
  260. asmlinkage long sys_oabi_semtimedop(int semid,
  261. struct oabi_sembuf __user *tsops,
  262. unsigned nsops,
  263. const struct timespec __user *timeout)
  264. {
  265. struct sembuf *sops;
  266. struct timespec local_timeout;
  267. long err;
  268. int i;
  269. if (nsops < 1)
  270. return -EINVAL;
  271. sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
  272. if (!sops)
  273. return -ENOMEM;
  274. err = 0;
  275. for (i = 0; i < nsops; i++) {
  276. __get_user_error(sops[i].sem_num, &tsops->sem_num, err);
  277. __get_user_error(sops[i].sem_op, &tsops->sem_op, err);
  278. __get_user_error(sops[i].sem_flg, &tsops->sem_flg, err);
  279. tsops++;
  280. }
  281. if (timeout) {
  282. /* copy this as well before changing domain protection */
  283. err |= copy_from_user(&local_timeout, timeout, sizeof(*timeout));
  284. timeout = &local_timeout;
  285. }
  286. if (err) {
  287. err = -EFAULT;
  288. } else {
  289. mm_segment_t fs = get_fs();
  290. set_fs(KERNEL_DS);
  291. err = sys_semtimedop(semid, sops, nsops, timeout);
  292. set_fs(fs);
  293. }
  294. kfree(sops);
  295. return err;
  296. }
  297. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  298. unsigned nsops)
  299. {
  300. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  301. }
  302. extern asmlinkage int sys_ipc(uint call, int first, int second, int third,
  303. void __user *ptr, long fifth);
  304. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  305. void __user *ptr, long fifth)
  306. {
  307. switch (call & 0xffff) {
  308. case SEMOP:
  309. return sys_oabi_semtimedop(first,
  310. (struct oabi_sembuf __user *)ptr,
  311. second, NULL);
  312. case SEMTIMEDOP:
  313. return sys_oabi_semtimedop(first,
  314. (struct oabi_sembuf __user *)ptr,
  315. second,
  316. (const struct timespec __user *)fifth);
  317. default:
  318. return sys_ipc(call, first, second, third, ptr, fifth);
  319. }
  320. }
  321. asmlinkage long sys_oabi_bind(int fd, struct sockaddr __user *addr, int addrlen)
  322. {
  323. sa_family_t sa_family;
  324. if (addrlen == 112 &&
  325. get_user(sa_family, &addr->sa_family) == 0 &&
  326. sa_family == AF_UNIX)
  327. addrlen = 110;
  328. return sys_bind(fd, addr, addrlen);
  329. }
  330. asmlinkage long sys_oabi_connect(int fd, struct sockaddr __user *addr, int addrlen)
  331. {
  332. sa_family_t sa_family;
  333. if (addrlen == 112 &&
  334. get_user(sa_family, &addr->sa_family) == 0 &&
  335. sa_family == AF_UNIX)
  336. addrlen = 110;
  337. return sys_connect(fd, addr, addrlen);
  338. }
  339. asmlinkage long sys_oabi_sendto(int fd, void __user *buff,
  340. size_t len, unsigned flags,
  341. struct sockaddr __user *addr,
  342. int addrlen)
  343. {
  344. sa_family_t sa_family;
  345. if (addrlen == 112 &&
  346. get_user(sa_family, &addr->sa_family) == 0 &&
  347. sa_family == AF_UNIX)
  348. addrlen = 110;
  349. return sys_sendto(fd, buff, len, flags, addr, addrlen);
  350. }
  351. asmlinkage long sys_oabi_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  352. {
  353. struct sockaddr __user *addr;
  354. int msg_namelen;
  355. sa_family_t sa_family;
  356. if (msg &&
  357. get_user(msg_namelen, &msg->msg_namelen) == 0 &&
  358. msg_namelen == 112 &&
  359. get_user(addr, &msg->msg_name) == 0 &&
  360. get_user(sa_family, &addr->sa_family) == 0 &&
  361. sa_family == AF_UNIX)
  362. {
  363. /*
  364. * HACK ALERT: there is a limit to how much backward bending
  365. * we should do for what is actually a transitional
  366. * compatibility layer. This already has known flaws with
  367. * a few ioctls that we don't intend to fix. Therefore
  368. * consider this blatent hack as another one... and take care
  369. * to run for cover. In most cases it will "just work fine".
  370. * If it doesn't, well, tough.
  371. */
  372. put_user(110, &msg->msg_namelen);
  373. }
  374. return sys_sendmsg(fd, msg, flags);
  375. }
  376. asmlinkage long sys_oabi_socketcall(int call, unsigned long __user *args)
  377. {
  378. unsigned long r = -EFAULT, a[6];
  379. switch (call) {
  380. case SYS_BIND:
  381. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  382. r = sys_oabi_bind(a[0], (struct sockaddr __user *)a[1], a[2]);
  383. break;
  384. case SYS_CONNECT:
  385. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  386. r = sys_oabi_connect(a[0], (struct sockaddr __user *)a[1], a[2]);
  387. break;
  388. case SYS_SENDTO:
  389. if (copy_from_user(a, args, 6 * sizeof(long)) == 0)
  390. r = sys_oabi_sendto(a[0], (void __user *)a[1], a[2], a[3],
  391. (struct sockaddr __user *)a[4], a[5]);
  392. break;
  393. case SYS_SENDMSG:
  394. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  395. r = sys_oabi_sendmsg(a[0], (struct msghdr __user *)a[1], a[2]);
  396. break;
  397. default:
  398. r = sys_socketcall(call, args);
  399. }
  400. return r;
  401. }