sys_oabi-compat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/cred.h>
  78. #include <linux/fcntl.h>
  79. #include <linux/eventpoll.h>
  80. #include <linux/sem.h>
  81. #include <linux/socket.h>
  82. #include <linux/net.h>
  83. #include <linux/ipc.h>
  84. #include <linux/uaccess.h>
  85. #include <linux/slab.h>
  86. struct oldabi_stat64 {
  87. unsigned long long st_dev;
  88. unsigned int __pad1;
  89. unsigned long __st_ino;
  90. unsigned int st_mode;
  91. unsigned int st_nlink;
  92. unsigned long st_uid;
  93. unsigned long st_gid;
  94. unsigned long long st_rdev;
  95. unsigned int __pad2;
  96. long long st_size;
  97. unsigned long st_blksize;
  98. unsigned long long st_blocks;
  99. unsigned long st_atime;
  100. unsigned long st_atime_nsec;
  101. unsigned long st_mtime;
  102. unsigned long st_mtime_nsec;
  103. unsigned long st_ctime;
  104. unsigned long st_ctime_nsec;
  105. unsigned long long st_ino;
  106. } __attribute__ ((packed,aligned(4)));
  107. static long cp_oldabi_stat64(struct kstat *stat,
  108. struct oldabi_stat64 __user *statbuf)
  109. {
  110. struct oldabi_stat64 tmp;
  111. tmp.st_dev = huge_encode_dev(stat->dev);
  112. tmp.__pad1 = 0;
  113. tmp.__st_ino = stat->ino;
  114. tmp.st_mode = stat->mode;
  115. tmp.st_nlink = stat->nlink;
  116. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  117. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  118. tmp.st_rdev = huge_encode_dev(stat->rdev);
  119. tmp.st_size = stat->size;
  120. tmp.st_blocks = stat->blocks;
  121. tmp.__pad2 = 0;
  122. tmp.st_blksize = stat->blksize;
  123. tmp.st_atime = stat->atime.tv_sec;
  124. tmp.st_atime_nsec = stat->atime.tv_nsec;
  125. tmp.st_mtime = stat->mtime.tv_sec;
  126. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  127. tmp.st_ctime = stat->ctime.tv_sec;
  128. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  129. tmp.st_ino = stat->ino;
  130. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  131. }
  132. asmlinkage long sys_oabi_stat64(const char __user * filename,
  133. struct oldabi_stat64 __user * statbuf)
  134. {
  135. struct kstat stat;
  136. int error = vfs_stat(filename, &stat);
  137. if (!error)
  138. error = cp_oldabi_stat64(&stat, statbuf);
  139. return error;
  140. }
  141. asmlinkage long sys_oabi_lstat64(const char __user * filename,
  142. struct oldabi_stat64 __user * statbuf)
  143. {
  144. struct kstat stat;
  145. int error = vfs_lstat(filename, &stat);
  146. if (!error)
  147. error = cp_oldabi_stat64(&stat, statbuf);
  148. return error;
  149. }
  150. asmlinkage long sys_oabi_fstat64(unsigned long fd,
  151. struct oldabi_stat64 __user * statbuf)
  152. {
  153. struct kstat stat;
  154. int error = vfs_fstat(fd, &stat);
  155. if (!error)
  156. error = cp_oldabi_stat64(&stat, statbuf);
  157. return error;
  158. }
  159. asmlinkage long sys_oabi_fstatat64(int dfd,
  160. const char __user *filename,
  161. struct oldabi_stat64 __user *statbuf,
  162. int flag)
  163. {
  164. struct kstat stat;
  165. int error;
  166. error = vfs_fstatat(dfd, filename, &stat, flag);
  167. if (error)
  168. return error;
  169. return cp_oldabi_stat64(&stat, statbuf);
  170. }
  171. struct oabi_flock64 {
  172. short l_type;
  173. short l_whence;
  174. loff_t l_start;
  175. loff_t l_len;
  176. pid_t l_pid;
  177. } __attribute__ ((packed,aligned(4)));
  178. static long do_locks(unsigned int fd, unsigned int cmd,
  179. unsigned long arg)
  180. {
  181. struct flock64 kernel;
  182. struct oabi_flock64 user;
  183. mm_segment_t fs;
  184. long ret;
  185. if (copy_from_user(&user, (struct oabi_flock64 __user *)arg,
  186. sizeof(user)))
  187. return -EFAULT;
  188. kernel.l_type = user.l_type;
  189. kernel.l_whence = user.l_whence;
  190. kernel.l_start = user.l_start;
  191. kernel.l_len = user.l_len;
  192. kernel.l_pid = user.l_pid;
  193. fs = get_fs();
  194. set_fs(KERNEL_DS);
  195. ret = sys_fcntl64(fd, cmd, (unsigned long)&kernel);
  196. set_fs(fs);
  197. if (!ret && (cmd == F_GETLK64 || cmd == F_OFD_GETLK)) {
  198. user.l_type = kernel.l_type;
  199. user.l_whence = kernel.l_whence;
  200. user.l_start = kernel.l_start;
  201. user.l_len = kernel.l_len;
  202. user.l_pid = kernel.l_pid;
  203. if (copy_to_user((struct oabi_flock64 __user *)arg,
  204. &user, sizeof(user)))
  205. ret = -EFAULT;
  206. }
  207. return ret;
  208. }
  209. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  210. unsigned long arg)
  211. {
  212. switch (cmd) {
  213. case F_OFD_GETLK:
  214. case F_OFD_SETLK:
  215. case F_OFD_SETLKW:
  216. case F_GETLK64:
  217. case F_SETLK64:
  218. case F_SETLKW64:
  219. return do_locks(fd, cmd, arg);
  220. default:
  221. return sys_fcntl64(fd, cmd, arg);
  222. }
  223. }
  224. struct oabi_epoll_event {
  225. __u32 events;
  226. __u64 data;
  227. } __attribute__ ((packed,aligned(4)));
  228. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  229. struct oabi_epoll_event __user *event)
  230. {
  231. struct oabi_epoll_event user;
  232. struct epoll_event kernel;
  233. mm_segment_t fs;
  234. long ret;
  235. if (op == EPOLL_CTL_DEL)
  236. return sys_epoll_ctl(epfd, op, fd, NULL);
  237. if (copy_from_user(&user, event, sizeof(user)))
  238. return -EFAULT;
  239. kernel.events = user.events;
  240. kernel.data = user.data;
  241. fs = get_fs();
  242. set_fs(KERNEL_DS);
  243. ret = sys_epoll_ctl(epfd, op, fd, &kernel);
  244. set_fs(fs);
  245. return ret;
  246. }
  247. asmlinkage long sys_oabi_epoll_wait(int epfd,
  248. struct oabi_epoll_event __user *events,
  249. int maxevents, int timeout)
  250. {
  251. struct epoll_event *kbuf;
  252. struct oabi_epoll_event e;
  253. mm_segment_t fs;
  254. long ret, err, i;
  255. if (maxevents <= 0 ||
  256. maxevents > (INT_MAX/sizeof(*kbuf)) ||
  257. maxevents > (INT_MAX/sizeof(*events)))
  258. return -EINVAL;
  259. if (!access_ok(VERIFY_WRITE, events, sizeof(*events) * maxevents))
  260. return -EFAULT;
  261. kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
  262. if (!kbuf)
  263. return -ENOMEM;
  264. fs = get_fs();
  265. set_fs(KERNEL_DS);
  266. ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
  267. set_fs(fs);
  268. err = 0;
  269. for (i = 0; i < ret; i++) {
  270. e.events = kbuf[i].events;
  271. e.data = kbuf[i].data;
  272. err = __copy_to_user(events, &e, sizeof(e));
  273. if (err)
  274. break;
  275. events++;
  276. }
  277. kfree(kbuf);
  278. return err ? -EFAULT : ret;
  279. }
  280. struct oabi_sembuf {
  281. unsigned short sem_num;
  282. short sem_op;
  283. short sem_flg;
  284. unsigned short __pad;
  285. };
  286. asmlinkage long sys_oabi_semtimedop(int semid,
  287. struct oabi_sembuf __user *tsops,
  288. unsigned nsops,
  289. const struct timespec __user *timeout)
  290. {
  291. struct sembuf *sops;
  292. struct timespec local_timeout;
  293. long err;
  294. int i;
  295. if (nsops < 1 || nsops > SEMOPM)
  296. return -EINVAL;
  297. if (!access_ok(VERIFY_READ, tsops, sizeof(*tsops) * nsops))
  298. return -EFAULT;
  299. sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
  300. if (!sops)
  301. return -ENOMEM;
  302. err = 0;
  303. for (i = 0; i < nsops; i++) {
  304. struct oabi_sembuf osb;
  305. err |= __copy_from_user(&osb, tsops, sizeof(osb));
  306. sops[i].sem_num = osb.sem_num;
  307. sops[i].sem_op = osb.sem_op;
  308. sops[i].sem_flg = osb.sem_flg;
  309. tsops++;
  310. }
  311. if (timeout) {
  312. /* copy this as well before changing domain protection */
  313. err |= copy_from_user(&local_timeout, timeout, sizeof(*timeout));
  314. timeout = &local_timeout;
  315. }
  316. if (err) {
  317. err = -EFAULT;
  318. } else {
  319. mm_segment_t fs = get_fs();
  320. set_fs(KERNEL_DS);
  321. err = sys_semtimedop(semid, sops, nsops, timeout);
  322. set_fs(fs);
  323. }
  324. kfree(sops);
  325. return err;
  326. }
  327. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  328. unsigned nsops)
  329. {
  330. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  331. }
  332. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  333. void __user *ptr, long fifth)
  334. {
  335. switch (call & 0xffff) {
  336. case SEMOP:
  337. return sys_oabi_semtimedop(first,
  338. (struct oabi_sembuf __user *)ptr,
  339. second, NULL);
  340. case SEMTIMEDOP:
  341. return sys_oabi_semtimedop(first,
  342. (struct oabi_sembuf __user *)ptr,
  343. second,
  344. (const struct timespec __user *)fifth);
  345. default:
  346. return sys_ipc(call, first, second, third, ptr, fifth);
  347. }
  348. }
  349. asmlinkage long sys_oabi_bind(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_bind(fd, addr, addrlen);
  357. }
  358. asmlinkage long sys_oabi_connect(int fd, struct sockaddr __user *addr, int addrlen)
  359. {
  360. sa_family_t sa_family;
  361. if (addrlen == 112 &&
  362. get_user(sa_family, &addr->sa_family) == 0 &&
  363. sa_family == AF_UNIX)
  364. addrlen = 110;
  365. return sys_connect(fd, addr, addrlen);
  366. }
  367. asmlinkage long sys_oabi_sendto(int fd, void __user *buff,
  368. size_t len, unsigned flags,
  369. struct sockaddr __user *addr,
  370. int addrlen)
  371. {
  372. sa_family_t sa_family;
  373. if (addrlen == 112 &&
  374. get_user(sa_family, &addr->sa_family) == 0 &&
  375. sa_family == AF_UNIX)
  376. addrlen = 110;
  377. return sys_sendto(fd, buff, len, flags, addr, addrlen);
  378. }
  379. asmlinkage long sys_oabi_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
  380. {
  381. struct sockaddr __user *addr;
  382. int msg_namelen;
  383. sa_family_t sa_family;
  384. if (msg &&
  385. get_user(msg_namelen, &msg->msg_namelen) == 0 &&
  386. msg_namelen == 112 &&
  387. get_user(addr, &msg->msg_name) == 0 &&
  388. get_user(sa_family, &addr->sa_family) == 0 &&
  389. sa_family == AF_UNIX)
  390. {
  391. /*
  392. * HACK ALERT: there is a limit to how much backward bending
  393. * we should do for what is actually a transitional
  394. * compatibility layer. This already has known flaws with
  395. * a few ioctls that we don't intend to fix. Therefore
  396. * consider this blatent hack as another one... and take care
  397. * to run for cover. In most cases it will "just work fine".
  398. * If it doesn't, well, tough.
  399. */
  400. put_user(110, &msg->msg_namelen);
  401. }
  402. return sys_sendmsg(fd, msg, flags);
  403. }
  404. asmlinkage long sys_oabi_socketcall(int call, unsigned long __user *args)
  405. {
  406. unsigned long r = -EFAULT, a[6];
  407. switch (call) {
  408. case SYS_BIND:
  409. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  410. r = sys_oabi_bind(a[0], (struct sockaddr __user *)a[1], a[2]);
  411. break;
  412. case SYS_CONNECT:
  413. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  414. r = sys_oabi_connect(a[0], (struct sockaddr __user *)a[1], a[2]);
  415. break;
  416. case SYS_SENDTO:
  417. if (copy_from_user(a, args, 6 * sizeof(long)) == 0)
  418. r = sys_oabi_sendto(a[0], (void __user *)a[1], a[2], a[3],
  419. (struct sockaddr __user *)a[4], a[5]);
  420. break;
  421. case SYS_SENDMSG:
  422. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  423. r = sys_oabi_sendmsg(a[0], (struct user_msghdr __user *)a[1], a[2]);
  424. break;
  425. default:
  426. r = sys_socketcall(call, args);
  427. }
  428. return r;
  429. }