sys_oabi-compat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. *
  67. * struct sockaddr_un loses its padding with EABI. Since the size of the
  68. * structure is used as a validation test in unix_mkname(), we need to
  69. * change the length argument to 110 whenever it is 112. Compatibility
  70. * wrappers provided below.
  71. */
  72. #include <linux/syscalls.h>
  73. #include <linux/errno.h>
  74. #include <linux/fs.h>
  75. #include <linux/fcntl.h>
  76. #include <linux/eventpoll.h>
  77. #include <linux/sem.h>
  78. #include <linux/socket.h>
  79. #include <asm/ipc.h>
  80. #include <asm/uaccess.h>
  81. struct oldabi_stat64 {
  82. unsigned long long st_dev;
  83. unsigned int __pad1;
  84. unsigned long __st_ino;
  85. unsigned int st_mode;
  86. unsigned int st_nlink;
  87. unsigned long st_uid;
  88. unsigned long st_gid;
  89. unsigned long long st_rdev;
  90. unsigned int __pad2;
  91. long long st_size;
  92. unsigned long st_blksize;
  93. unsigned long long st_blocks;
  94. unsigned long st_atime;
  95. unsigned long st_atime_nsec;
  96. unsigned long st_mtime;
  97. unsigned long st_mtime_nsec;
  98. unsigned long st_ctime;
  99. unsigned long st_ctime_nsec;
  100. unsigned long long st_ino;
  101. } __attribute__ ((packed,aligned(4)));
  102. static long cp_oldabi_stat64(struct kstat *stat,
  103. struct oldabi_stat64 __user *statbuf)
  104. {
  105. struct oldabi_stat64 tmp;
  106. tmp.st_dev = huge_encode_dev(stat->dev);
  107. tmp.__pad1 = 0;
  108. tmp.__st_ino = stat->ino;
  109. tmp.st_mode = stat->mode;
  110. tmp.st_nlink = stat->nlink;
  111. tmp.st_uid = stat->uid;
  112. tmp.st_gid = stat->gid;
  113. tmp.st_rdev = huge_encode_dev(stat->rdev);
  114. tmp.st_size = stat->size;
  115. tmp.st_blocks = stat->blocks;
  116. tmp.__pad2 = 0;
  117. tmp.st_blksize = stat->blksize;
  118. tmp.st_atime = stat->atime.tv_sec;
  119. tmp.st_atime_nsec = stat->atime.tv_nsec;
  120. tmp.st_mtime = stat->mtime.tv_sec;
  121. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  122. tmp.st_ctime = stat->ctime.tv_sec;
  123. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  124. tmp.st_ino = stat->ino;
  125. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  126. }
  127. asmlinkage long sys_oabi_stat64(char __user * filename,
  128. struct oldabi_stat64 __user * statbuf)
  129. {
  130. struct kstat stat;
  131. int error = vfs_stat(filename, &stat);
  132. if (!error)
  133. error = cp_oldabi_stat64(&stat, statbuf);
  134. return error;
  135. }
  136. asmlinkage long sys_oabi_lstat64(char __user * filename,
  137. struct oldabi_stat64 __user * statbuf)
  138. {
  139. struct kstat stat;
  140. int error = vfs_lstat(filename, &stat);
  141. if (!error)
  142. error = cp_oldabi_stat64(&stat, statbuf);
  143. return error;
  144. }
  145. asmlinkage long sys_oabi_fstat64(unsigned long fd,
  146. struct oldabi_stat64 __user * statbuf)
  147. {
  148. struct kstat stat;
  149. int error = vfs_fstat(fd, &stat);
  150. if (!error)
  151. error = cp_oldabi_stat64(&stat, statbuf);
  152. return error;
  153. }
  154. struct oabi_flock64 {
  155. short l_type;
  156. short l_whence;
  157. loff_t l_start;
  158. loff_t l_len;
  159. pid_t l_pid;
  160. } __attribute__ ((packed,aligned(4)));
  161. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  162. unsigned long arg)
  163. {
  164. struct oabi_flock64 user;
  165. struct flock64 kernel;
  166. mm_segment_t fs = USER_DS; /* initialized to kill a warning */
  167. unsigned long local_arg = arg;
  168. int ret;
  169. switch (cmd) {
  170. case F_GETLK64:
  171. case F_SETLK64:
  172. case F_SETLKW64:
  173. if (copy_from_user(&user, (struct oabi_flock64 __user *)arg,
  174. sizeof(user)))
  175. return -EFAULT;
  176. kernel.l_type = user.l_type;
  177. kernel.l_whence = user.l_whence;
  178. kernel.l_start = user.l_start;
  179. kernel.l_len = user.l_len;
  180. kernel.l_pid = user.l_pid;
  181. local_arg = (unsigned long)&kernel;
  182. fs = get_fs();
  183. set_fs(KERNEL_DS);
  184. }
  185. ret = sys_fcntl64(fd, cmd, local_arg);
  186. switch (cmd) {
  187. case F_GETLK64:
  188. if (!ret) {
  189. user.l_type = kernel.l_type;
  190. user.l_whence = kernel.l_whence;
  191. user.l_start = kernel.l_start;
  192. user.l_len = kernel.l_len;
  193. user.l_pid = kernel.l_pid;
  194. if (copy_to_user((struct oabi_flock64 __user *)arg,
  195. &user, sizeof(user)))
  196. ret = -EFAULT;
  197. }
  198. case F_SETLK64:
  199. case F_SETLKW64:
  200. set_fs(fs);
  201. }
  202. return ret;
  203. }
  204. struct oabi_epoll_event {
  205. __u32 events;
  206. __u64 data;
  207. } __attribute__ ((packed,aligned(4)));
  208. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  209. struct oabi_epoll_event __user *event)
  210. {
  211. struct oabi_epoll_event user;
  212. struct epoll_event kernel;
  213. mm_segment_t fs;
  214. long ret;
  215. if (op == EPOLL_CTL_DEL)
  216. return sys_epoll_ctl(epfd, op, fd, NULL);
  217. if (copy_from_user(&user, event, sizeof(user)))
  218. return -EFAULT;
  219. kernel.events = user.events;
  220. kernel.data = user.data;
  221. fs = get_fs();
  222. set_fs(KERNEL_DS);
  223. ret = sys_epoll_ctl(epfd, op, fd, &kernel);
  224. set_fs(fs);
  225. return ret;
  226. }
  227. asmlinkage long sys_oabi_epoll_wait(int epfd,
  228. struct oabi_epoll_event __user *events,
  229. int maxevents, int timeout)
  230. {
  231. struct epoll_event *kbuf;
  232. mm_segment_t fs;
  233. long ret, err, i;
  234. if (maxevents <= 0 || maxevents > (INT_MAX/sizeof(struct epoll_event)))
  235. return -EINVAL;
  236. kbuf = kmalloc(sizeof(*kbuf) * maxevents, GFP_KERNEL);
  237. if (!kbuf)
  238. return -ENOMEM;
  239. fs = get_fs();
  240. set_fs(KERNEL_DS);
  241. ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
  242. set_fs(fs);
  243. err = 0;
  244. for (i = 0; i < ret; i++) {
  245. __put_user_error(kbuf[i].events, &events->events, err);
  246. __put_user_error(kbuf[i].data, &events->data, err);
  247. events++;
  248. }
  249. kfree(kbuf);
  250. return err ? -EFAULT : ret;
  251. }
  252. struct oabi_sembuf {
  253. unsigned short sem_num;
  254. short sem_op;
  255. short sem_flg;
  256. unsigned short __pad;
  257. };
  258. asmlinkage long sys_oabi_semtimedop(int semid,
  259. struct oabi_sembuf __user *tsops,
  260. unsigned nsops,
  261. const struct timespec __user *timeout)
  262. {
  263. struct sembuf *sops;
  264. struct timespec local_timeout;
  265. long err;
  266. int i;
  267. if (nsops < 1)
  268. return -EINVAL;
  269. sops = kmalloc(sizeof(*sops) * nsops, GFP_KERNEL);
  270. if (!sops)
  271. return -ENOMEM;
  272. err = 0;
  273. for (i = 0; i < nsops; i++) {
  274. __get_user_error(sops[i].sem_num, &tsops->sem_num, err);
  275. __get_user_error(sops[i].sem_op, &tsops->sem_op, err);
  276. __get_user_error(sops[i].sem_flg, &tsops->sem_flg, err);
  277. tsops++;
  278. }
  279. if (timeout) {
  280. /* copy this as well before changing domain protection */
  281. err |= copy_from_user(&local_timeout, timeout, sizeof(*timeout));
  282. timeout = &local_timeout;
  283. }
  284. if (err) {
  285. err = -EFAULT;
  286. } else {
  287. mm_segment_t fs = get_fs();
  288. set_fs(KERNEL_DS);
  289. err = sys_semtimedop(semid, sops, nsops, timeout);
  290. set_fs(fs);
  291. }
  292. kfree(sops);
  293. return err;
  294. }
  295. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  296. unsigned nsops)
  297. {
  298. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  299. }
  300. extern asmlinkage int sys_ipc(uint call, int first, int second, int third,
  301. void __user *ptr, long fifth);
  302. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  303. void __user *ptr, long fifth)
  304. {
  305. switch (call & 0xffff) {
  306. case SEMOP:
  307. return sys_oabi_semtimedop(first,
  308. (struct oabi_sembuf __user *)ptr,
  309. second, NULL);
  310. case SEMTIMEDOP:
  311. return sys_oabi_semtimedop(first,
  312. (struct oabi_sembuf __user *)ptr,
  313. second,
  314. (const struct timespec __user *)fifth);
  315. default:
  316. return sys_ipc(call, first, second, third, ptr, fifth);
  317. }
  318. }
  319. asmlinkage long sys_oabi_bind(int fd, struct sockaddr __user *addr, int addrlen)
  320. {
  321. sa_family_t sa_family;
  322. if (addrlen == 112 &&
  323. get_user(sa_family, &addr->sa_family) == 0 &&
  324. sa_family == AF_UNIX)
  325. addrlen = 110;
  326. return sys_bind(fd, addr, addrlen);
  327. }
  328. asmlinkage long sys_oabi_connect(int fd, struct sockaddr __user *addr, int addrlen)
  329. {
  330. sa_family_t sa_family;
  331. if (addrlen == 112 &&
  332. get_user(sa_family, &addr->sa_family) == 0 &&
  333. sa_family == AF_UNIX)
  334. addrlen = 110;
  335. return sys_connect(fd, addr, addrlen);
  336. }
  337. asmlinkage long sys_oabi_sendto(int fd, void __user *buff,
  338. size_t len, unsigned flags,
  339. struct sockaddr __user *addr,
  340. int addrlen)
  341. {
  342. sa_family_t sa_family;
  343. if (addrlen == 112 &&
  344. get_user(sa_family, &addr->sa_family) == 0 &&
  345. sa_family == AF_UNIX)
  346. addrlen = 110;
  347. return sys_sendto(fd, buff, len, flags, addr, addrlen);
  348. }
  349. asmlinkage long sys_oabi_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  350. {
  351. struct sockaddr __user *addr;
  352. int msg_namelen;
  353. sa_family_t sa_family;
  354. if (msg &&
  355. get_user(msg_namelen, &msg->msg_namelen) == 0 &&
  356. msg_namelen == 112 &&
  357. get_user(addr, &msg->msg_name) == 0 &&
  358. get_user(sa_family, &addr->sa_family) == 0 &&
  359. sa_family == AF_UNIX)
  360. {
  361. /*
  362. * HACK ALERT: there is a limit to how much backward bending
  363. * we should do for what is actually a transitional
  364. * compatibility layer. This already has known flaws with
  365. * a few ioctls that we don't intend to fix. Therefore
  366. * consider this blatent hack as another one... and take care
  367. * to run for cover. In most cases it will "just work fine".
  368. * If it doesn't, well, tough.
  369. */
  370. put_user(110, &msg->msg_namelen);
  371. }
  372. return sys_sendmsg(fd, msg, flags);
  373. }