PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/arm/kernel/sys_oabi-compat.c

http://github.com/torvalds/linux
C | 467 lines | 346 code | 41 blank | 80 comment | 56 complexity | d83542f4c95422872fd3befae9c740f2 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/kernel/sys_oabi-compat.c
  4. *
  5. * Compatibility wrappers for syscalls that are used from
  6. * old ABI user space binaries with an EABI kernel.
  7. *
  8. * Author: Nicolas Pitre
  9. * Created: Oct 7, 2005
  10. * Copyright: MontaVista Software, Inc.
  11. */
  12. /*
  13. * The legacy ABI and the new ARM EABI have different rules making some
  14. * syscalls incompatible especially with structure arguments.
  15. * Most notably, Eabi says 64-bit members should be 64-bit aligned instead of
  16. * simply word aligned. EABI also pads structures to the size of the largest
  17. * member it contains instead of the invariant 32-bit.
  18. *
  19. * The following syscalls are affected:
  20. *
  21. * sys_stat64:
  22. * sys_lstat64:
  23. * sys_fstat64:
  24. * sys_fstatat64:
  25. *
  26. * struct stat64 has different sizes and some members are shifted
  27. * Compatibility wrappers are needed for them and provided below.
  28. *
  29. * sys_fcntl64:
  30. *
  31. * struct flock64 has different sizes and some members are shifted
  32. * A compatibility wrapper is needed and provided below.
  33. *
  34. * sys_statfs64:
  35. * sys_fstatfs64:
  36. *
  37. * struct statfs64 has extra padding with EABI growing its size from
  38. * 84 to 88. This struct is now __attribute__((packed,aligned(4)))
  39. * with a small assembly wrapper to force the sz argument to 84 if it is 88
  40. * to avoid copying the extra padding over user space unexpecting it.
  41. *
  42. * sys_newuname:
  43. *
  44. * struct new_utsname has no padding with EABI. No problem there.
  45. *
  46. * sys_epoll_ctl:
  47. * sys_epoll_wait:
  48. *
  49. * struct epoll_event has its second member shifted also affecting the
  50. * structure size. Compatibility wrappers are needed and provided below.
  51. *
  52. * sys_ipc:
  53. * sys_semop:
  54. * sys_semtimedop:
  55. *
  56. * struct sembuf loses its padding with EABI. Since arrays of them are
  57. * used they have to be copyed to remove the padding. Compatibility wrappers
  58. * provided below.
  59. *
  60. * sys_bind:
  61. * sys_connect:
  62. * sys_sendmsg:
  63. * sys_sendto:
  64. * sys_socketcall:
  65. *
  66. * struct sockaddr_un loses its padding with EABI. Since the size of the
  67. * structure is used as a validation test in unix_mkname(), we need to
  68. * change the length argument to 110 whenever it is 112. Compatibility
  69. * wrappers provided below.
  70. */
  71. #include <linux/syscalls.h>
  72. #include <linux/errno.h>
  73. #include <linux/fs.h>
  74. #include <linux/cred.h>
  75. #include <linux/fcntl.h>
  76. #include <linux/eventpoll.h>
  77. #include <linux/sem.h>
  78. #include <linux/socket.h>
  79. #include <linux/net.h>
  80. #include <linux/ipc.h>
  81. #include <linux/uaccess.h>
  82. #include <linux/slab.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 = from_kuid_munged(current_user_ns(), stat->uid);
  114. tmp.st_gid = from_kgid_munged(current_user_ns(), 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(const 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(const 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. asmlinkage long sys_oabi_fstatat64(int dfd,
  157. const char __user *filename,
  158. struct oldabi_stat64 __user *statbuf,
  159. int flag)
  160. {
  161. struct kstat stat;
  162. int error;
  163. error = vfs_fstatat(dfd, filename, &stat, flag);
  164. if (error)
  165. return error;
  166. return cp_oldabi_stat64(&stat, statbuf);
  167. }
  168. struct oabi_flock64 {
  169. short l_type;
  170. short l_whence;
  171. loff_t l_start;
  172. loff_t l_len;
  173. pid_t l_pid;
  174. } __attribute__ ((packed,aligned(4)));
  175. static long do_locks(unsigned int fd, unsigned int cmd,
  176. unsigned long arg)
  177. {
  178. struct flock64 kernel;
  179. struct oabi_flock64 user;
  180. mm_segment_t fs;
  181. long ret;
  182. if (copy_from_user(&user, (struct oabi_flock64 __user *)arg,
  183. sizeof(user)))
  184. return -EFAULT;
  185. kernel.l_type = user.l_type;
  186. kernel.l_whence = user.l_whence;
  187. kernel.l_start = user.l_start;
  188. kernel.l_len = user.l_len;
  189. kernel.l_pid = user.l_pid;
  190. fs = get_fs();
  191. set_fs(KERNEL_DS);
  192. ret = sys_fcntl64(fd, cmd, (unsigned long)&kernel);
  193. set_fs(fs);
  194. if (!ret && (cmd == F_GETLK64 || cmd == F_OFD_GETLK)) {
  195. user.l_type = kernel.l_type;
  196. user.l_whence = kernel.l_whence;
  197. user.l_start = kernel.l_start;
  198. user.l_len = kernel.l_len;
  199. user.l_pid = kernel.l_pid;
  200. if (copy_to_user((struct oabi_flock64 __user *)arg,
  201. &user, sizeof(user)))
  202. ret = -EFAULT;
  203. }
  204. return ret;
  205. }
  206. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  207. unsigned long arg)
  208. {
  209. switch (cmd) {
  210. case F_OFD_GETLK:
  211. case F_OFD_SETLK:
  212. case F_OFD_SETLKW:
  213. case F_GETLK64:
  214. case F_SETLK64:
  215. case F_SETLKW64:
  216. return do_locks(fd, cmd, arg);
  217. default:
  218. return sys_fcntl64(fd, cmd, arg);
  219. }
  220. }
  221. struct oabi_epoll_event {
  222. __u32 events;
  223. __u64 data;
  224. } __attribute__ ((packed,aligned(4)));
  225. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  226. struct oabi_epoll_event __user *event)
  227. {
  228. struct oabi_epoll_event user;
  229. struct epoll_event kernel;
  230. mm_segment_t fs;
  231. long ret;
  232. if (op == EPOLL_CTL_DEL)
  233. return sys_epoll_ctl(epfd, op, fd, NULL);
  234. if (copy_from_user(&user, event, sizeof(user)))
  235. return -EFAULT;
  236. kernel.events = user.events;
  237. kernel.data = user.data;
  238. fs = get_fs();
  239. set_fs(KERNEL_DS);
  240. ret = sys_epoll_ctl(epfd, op, fd, &kernel);
  241. set_fs(fs);
  242. return ret;
  243. }
  244. asmlinkage long sys_oabi_epoll_wait(int epfd,
  245. struct oabi_epoll_event __user *events,
  246. int maxevents, int timeout)
  247. {
  248. struct epoll_event *kbuf;
  249. struct oabi_epoll_event e;
  250. mm_segment_t fs;
  251. long ret, err, i;
  252. if (maxevents <= 0 ||
  253. maxevents > (INT_MAX/sizeof(*kbuf)) ||
  254. maxevents > (INT_MAX/sizeof(*events)))
  255. return -EINVAL;
  256. if (!access_ok(events, sizeof(*events) * maxevents))
  257. return -EFAULT;
  258. kbuf = kmalloc_array(maxevents, sizeof(*kbuf), GFP_KERNEL);
  259. if (!kbuf)
  260. return -ENOMEM;
  261. fs = get_fs();
  262. set_fs(KERNEL_DS);
  263. ret = sys_epoll_wait(epfd, kbuf, maxevents, timeout);
  264. set_fs(fs);
  265. err = 0;
  266. for (i = 0; i < ret; i++) {
  267. e.events = kbuf[i].events;
  268. e.data = kbuf[i].data;
  269. err = __copy_to_user(events, &e, sizeof(e));
  270. if (err)
  271. break;
  272. events++;
  273. }
  274. kfree(kbuf);
  275. return err ? -EFAULT : ret;
  276. }
  277. struct oabi_sembuf {
  278. unsigned short sem_num;
  279. short sem_op;
  280. short sem_flg;
  281. unsigned short __pad;
  282. };
  283. asmlinkage long sys_oabi_semtimedop(int semid,
  284. struct oabi_sembuf __user *tsops,
  285. unsigned nsops,
  286. const struct old_timespec32 __user *timeout)
  287. {
  288. struct sembuf *sops;
  289. struct old_timespec32 local_timeout;
  290. long err;
  291. int i;
  292. if (nsops < 1 || nsops > SEMOPM)
  293. return -EINVAL;
  294. if (!access_ok(tsops, sizeof(*tsops) * nsops))
  295. return -EFAULT;
  296. sops = kmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
  297. if (!sops)
  298. return -ENOMEM;
  299. err = 0;
  300. for (i = 0; i < nsops; i++) {
  301. struct oabi_sembuf osb;
  302. err |= __copy_from_user(&osb, tsops, sizeof(osb));
  303. sops[i].sem_num = osb.sem_num;
  304. sops[i].sem_op = osb.sem_op;
  305. sops[i].sem_flg = osb.sem_flg;
  306. tsops++;
  307. }
  308. if (timeout) {
  309. /* copy this as well before changing domain protection */
  310. err |= copy_from_user(&local_timeout, timeout, sizeof(*timeout));
  311. timeout = &local_timeout;
  312. }
  313. if (err) {
  314. err = -EFAULT;
  315. } else {
  316. mm_segment_t fs = get_fs();
  317. set_fs(KERNEL_DS);
  318. err = sys_semtimedop_time32(semid, sops, nsops, timeout);
  319. set_fs(fs);
  320. }
  321. kfree(sops);
  322. return err;
  323. }
  324. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  325. unsigned nsops)
  326. {
  327. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  328. }
  329. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  330. void __user *ptr, long fifth)
  331. {
  332. switch (call & 0xffff) {
  333. case SEMOP:
  334. return sys_oabi_semtimedop(first,
  335. (struct oabi_sembuf __user *)ptr,
  336. second, NULL);
  337. case SEMTIMEDOP:
  338. return sys_oabi_semtimedop(first,
  339. (struct oabi_sembuf __user *)ptr,
  340. second,
  341. (const struct old_timespec32 __user *)fifth);
  342. default:
  343. return sys_ipc(call, first, second, third, ptr, fifth);
  344. }
  345. }
  346. asmlinkage long sys_oabi_bind(int fd, struct sockaddr __user *addr, int addrlen)
  347. {
  348. sa_family_t sa_family;
  349. if (addrlen == 112 &&
  350. get_user(sa_family, &addr->sa_family) == 0 &&
  351. sa_family == AF_UNIX)
  352. addrlen = 110;
  353. return sys_bind(fd, addr, addrlen);
  354. }
  355. asmlinkage long sys_oabi_connect(int fd, struct sockaddr __user *addr, int addrlen)
  356. {
  357. sa_family_t sa_family;
  358. if (addrlen == 112 &&
  359. get_user(sa_family, &addr->sa_family) == 0 &&
  360. sa_family == AF_UNIX)
  361. addrlen = 110;
  362. return sys_connect(fd, addr, addrlen);
  363. }
  364. asmlinkage long sys_oabi_sendto(int fd, void __user *buff,
  365. size_t len, unsigned flags,
  366. struct sockaddr __user *addr,
  367. int addrlen)
  368. {
  369. sa_family_t sa_family;
  370. if (addrlen == 112 &&
  371. get_user(sa_family, &addr->sa_family) == 0 &&
  372. sa_family == AF_UNIX)
  373. addrlen = 110;
  374. return sys_sendto(fd, buff, len, flags, addr, addrlen);
  375. }
  376. asmlinkage long sys_oabi_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
  377. {
  378. struct sockaddr __user *addr;
  379. int msg_namelen;
  380. sa_family_t sa_family;
  381. if (msg &&
  382. get_user(msg_namelen, &msg->msg_namelen) == 0 &&
  383. msg_namelen == 112 &&
  384. get_user(addr, &msg->msg_name) == 0 &&
  385. get_user(sa_family, &addr->sa_family) == 0 &&
  386. sa_family == AF_UNIX)
  387. {
  388. /*
  389. * HACK ALERT: there is a limit to how much backward bending
  390. * we should do for what is actually a transitional
  391. * compatibility layer. This already has known flaws with
  392. * a few ioctls that we don't intend to fix. Therefore
  393. * consider this blatent hack as another one... and take care
  394. * to run for cover. In most cases it will "just work fine".
  395. * If it doesn't, well, tough.
  396. */
  397. put_user(110, &msg->msg_namelen);
  398. }
  399. return sys_sendmsg(fd, msg, flags);
  400. }
  401. asmlinkage long sys_oabi_socketcall(int call, unsigned long __user *args)
  402. {
  403. unsigned long r = -EFAULT, a[6];
  404. switch (call) {
  405. case SYS_BIND:
  406. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  407. r = sys_oabi_bind(a[0], (struct sockaddr __user *)a[1], a[2]);
  408. break;
  409. case SYS_CONNECT:
  410. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  411. r = sys_oabi_connect(a[0], (struct sockaddr __user *)a[1], a[2]);
  412. break;
  413. case SYS_SENDTO:
  414. if (copy_from_user(a, args, 6 * sizeof(long)) == 0)
  415. r = sys_oabi_sendto(a[0], (void __user *)a[1], a[2], a[3],
  416. (struct sockaddr __user *)a[4], a[5]);
  417. break;
  418. case SYS_SENDMSG:
  419. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  420. r = sys_oabi_sendmsg(a[0], (struct user_msghdr __user *)a[1], a[2]);
  421. break;
  422. default:
  423. r = sys_socketcall(call, args);
  424. }
  425. return r;
  426. }