sys_oabi-compat.c 12 KB

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