sys_oabi-compat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. #include <asm/syscalls.h>
  13. /*
  14. * The legacy ABI and the new ARM EABI have different rules making some
  15. * syscalls incompatible especially with structure arguments.
  16. * Most notably, Eabi says 64-bit members should be 64-bit aligned instead of
  17. * simply word aligned. EABI also pads structures to the size of the largest
  18. * member it contains instead of the invariant 32-bit.
  19. *
  20. * The following syscalls are affected:
  21. *
  22. * sys_stat64:
  23. * sys_lstat64:
  24. * sys_fstat64:
  25. * sys_fstatat64:
  26. *
  27. * struct stat64 has different sizes and some members are shifted
  28. * Compatibility wrappers are needed for them and provided below.
  29. *
  30. * sys_fcntl64:
  31. *
  32. * struct flock64 has different sizes and some members are shifted
  33. * A compatibility wrapper is needed and provided below.
  34. *
  35. * sys_statfs64:
  36. * sys_fstatfs64:
  37. *
  38. * struct statfs64 has extra padding with EABI growing its size from
  39. * 84 to 88. This struct is now __attribute__((packed,aligned(4)))
  40. * with a small assembly wrapper to force the sz argument to 84 if it is 88
  41. * to avoid copying the extra padding over user space unexpecting it.
  42. *
  43. * sys_newuname:
  44. *
  45. * struct new_utsname has no padding with EABI. No problem there.
  46. *
  47. * sys_epoll_ctl:
  48. * sys_epoll_wait:
  49. *
  50. * struct epoll_event has its second member shifted also affecting the
  51. * structure size. Compatibility wrappers are needed and provided below.
  52. *
  53. * sys_ipc:
  54. * sys_semop:
  55. * sys_semtimedop:
  56. *
  57. * struct sembuf loses its padding with EABI. Since arrays of them are
  58. * used they have to be copyed to remove the padding. Compatibility wrappers
  59. * provided below.
  60. *
  61. * sys_bind:
  62. * sys_connect:
  63. * sys_sendmsg:
  64. * sys_sendto:
  65. * sys_socketcall:
  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/filelock.h>
  76. #include <linux/cred.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/ipc_namespace.h>
  84. #include <linux/uaccess.h>
  85. #include <linux/slab.h>
  86. #include <asm/syscall.h>
  87. struct oldabi_stat64 {
  88. unsigned long long st_dev;
  89. unsigned int __pad1;
  90. unsigned long __st_ino;
  91. unsigned int st_mode;
  92. unsigned int st_nlink;
  93. unsigned long st_uid;
  94. unsigned long st_gid;
  95. unsigned long long st_rdev;
  96. unsigned int __pad2;
  97. long long st_size;
  98. unsigned long st_blksize;
  99. unsigned long long st_blocks;
  100. unsigned long st_atime;
  101. unsigned long st_atime_nsec;
  102. unsigned long st_mtime;
  103. unsigned long st_mtime_nsec;
  104. unsigned long st_ctime;
  105. unsigned long st_ctime_nsec;
  106. unsigned long long st_ino;
  107. } __attribute__ ((packed,aligned(4)));
  108. static long cp_oldabi_stat64(struct kstat *stat,
  109. struct oldabi_stat64 __user *statbuf)
  110. {
  111. struct oldabi_stat64 tmp;
  112. tmp.st_dev = huge_encode_dev(stat->dev);
  113. tmp.__pad1 = 0;
  114. tmp.__st_ino = stat->ino;
  115. tmp.st_mode = stat->mode;
  116. tmp.st_nlink = stat->nlink;
  117. tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
  118. tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
  119. tmp.st_rdev = huge_encode_dev(stat->rdev);
  120. tmp.st_size = stat->size;
  121. tmp.st_blocks = stat->blocks;
  122. tmp.__pad2 = 0;
  123. tmp.st_blksize = stat->blksize;
  124. tmp.st_atime = stat->atime.tv_sec;
  125. tmp.st_atime_nsec = stat->atime.tv_nsec;
  126. tmp.st_mtime = stat->mtime.tv_sec;
  127. tmp.st_mtime_nsec = stat->mtime.tv_nsec;
  128. tmp.st_ctime = stat->ctime.tv_sec;
  129. tmp.st_ctime_nsec = stat->ctime.tv_nsec;
  130. tmp.st_ino = stat->ino;
  131. return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
  132. }
  133. asmlinkage long sys_oabi_stat64(const char __user * filename,
  134. struct oldabi_stat64 __user * statbuf)
  135. {
  136. struct kstat stat;
  137. int error = vfs_stat(filename, &stat);
  138. if (!error)
  139. error = cp_oldabi_stat64(&stat, statbuf);
  140. return error;
  141. }
  142. asmlinkage long sys_oabi_lstat64(const char __user * filename,
  143. struct oldabi_stat64 __user * statbuf)
  144. {
  145. struct kstat stat;
  146. int error = vfs_lstat(filename, &stat);
  147. if (!error)
  148. error = cp_oldabi_stat64(&stat, statbuf);
  149. return error;
  150. }
  151. asmlinkage long sys_oabi_fstat64(unsigned long fd,
  152. struct oldabi_stat64 __user * statbuf)
  153. {
  154. struct kstat stat;
  155. int error = vfs_fstat(fd, &stat);
  156. if (!error)
  157. error = cp_oldabi_stat64(&stat, statbuf);
  158. return error;
  159. }
  160. asmlinkage long sys_oabi_fstatat64(int dfd,
  161. const char __user *filename,
  162. struct oldabi_stat64 __user *statbuf,
  163. int flag)
  164. {
  165. struct kstat stat;
  166. int error;
  167. error = vfs_fstatat(dfd, filename, &stat, flag);
  168. if (error)
  169. return error;
  170. return cp_oldabi_stat64(&stat, statbuf);
  171. }
  172. struct oabi_flock64 {
  173. short l_type;
  174. short l_whence;
  175. loff_t l_start;
  176. loff_t l_len;
  177. pid_t l_pid;
  178. } __attribute__ ((packed,aligned(4)));
  179. static int get_oabi_flock(struct flock64 *kernel, struct oabi_flock64 __user *arg)
  180. {
  181. struct oabi_flock64 user;
  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. return 0;
  191. }
  192. static int put_oabi_flock(struct flock64 *kernel, struct oabi_flock64 __user *arg)
  193. {
  194. struct oabi_flock64 user;
  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. return -EFAULT;
  203. return 0;
  204. }
  205. asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
  206. unsigned long arg)
  207. {
  208. void __user *argp = (void __user *)arg;
  209. struct fd f = fdget_raw(fd);
  210. struct flock64 flock;
  211. long err = -EBADF;
  212. if (!fd_file(f))
  213. goto out;
  214. switch (cmd) {
  215. case F_GETLK64:
  216. case F_OFD_GETLK:
  217. err = security_file_fcntl(fd_file(f), cmd, arg);
  218. if (err)
  219. break;
  220. err = get_oabi_flock(&flock, argp);
  221. if (err)
  222. break;
  223. err = fcntl_getlk64(fd_file(f), cmd, &flock);
  224. if (!err)
  225. err = put_oabi_flock(&flock, argp);
  226. break;
  227. case F_SETLK64:
  228. case F_SETLKW64:
  229. case F_OFD_SETLK:
  230. case F_OFD_SETLKW:
  231. err = security_file_fcntl(fd_file(f), cmd, arg);
  232. if (err)
  233. break;
  234. err = get_oabi_flock(&flock, argp);
  235. if (err)
  236. break;
  237. err = fcntl_setlk64(fd, fd_file(f), cmd, &flock);
  238. break;
  239. default:
  240. err = sys_fcntl64(fd, cmd, arg);
  241. break;
  242. }
  243. fdput(f);
  244. out:
  245. return err;
  246. }
  247. struct oabi_epoll_event {
  248. __poll_t events;
  249. __u64 data;
  250. } __attribute__ ((packed,aligned(4)));
  251. #ifdef CONFIG_EPOLL
  252. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  253. struct oabi_epoll_event __user *event)
  254. {
  255. struct oabi_epoll_event user;
  256. struct epoll_event kernel;
  257. if (ep_op_has_event(op) &&
  258. copy_from_user(&user, event, sizeof(user)))
  259. return -EFAULT;
  260. kernel.events = user.events;
  261. kernel.data = user.data;
  262. return do_epoll_ctl(epfd, op, fd, &kernel, false);
  263. }
  264. #else
  265. asmlinkage long sys_oabi_epoll_ctl(int epfd, int op, int fd,
  266. struct oabi_epoll_event __user *event)
  267. {
  268. return -EINVAL;
  269. }
  270. #endif
  271. struct epoll_event __user *
  272. epoll_put_uevent(__poll_t revents, __u64 data,
  273. struct epoll_event __user *uevent)
  274. {
  275. if (in_oabi_syscall()) {
  276. struct oabi_epoll_event __user *oevent = (void __user *)uevent;
  277. if (__put_user(revents, &oevent->events) ||
  278. __put_user(data, &oevent->data))
  279. return NULL;
  280. return (void __user *)(oevent+1);
  281. }
  282. if (__put_user(revents, &uevent->events) ||
  283. __put_user(data, &uevent->data))
  284. return NULL;
  285. return uevent+1;
  286. }
  287. struct oabi_sembuf {
  288. unsigned short sem_num;
  289. short sem_op;
  290. short sem_flg;
  291. unsigned short __pad;
  292. };
  293. #define sc_semopm sem_ctls[2]
  294. #ifdef CONFIG_SYSVIPC
  295. asmlinkage long sys_oabi_semtimedop(int semid,
  296. struct oabi_sembuf __user *tsops,
  297. unsigned nsops,
  298. const struct old_timespec32 __user *timeout)
  299. {
  300. struct ipc_namespace *ns;
  301. struct sembuf *sops;
  302. long err;
  303. int i;
  304. ns = current->nsproxy->ipc_ns;
  305. if (nsops > ns->sc_semopm)
  306. return -E2BIG;
  307. if (nsops < 1 || nsops > SEMOPM)
  308. return -EINVAL;
  309. sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
  310. if (!sops)
  311. return -ENOMEM;
  312. err = 0;
  313. for (i = 0; i < nsops; i++) {
  314. struct oabi_sembuf osb;
  315. err |= copy_from_user(&osb, tsops, sizeof(osb));
  316. sops[i].sem_num = osb.sem_num;
  317. sops[i].sem_op = osb.sem_op;
  318. sops[i].sem_flg = osb.sem_flg;
  319. tsops++;
  320. }
  321. if (err) {
  322. err = -EFAULT;
  323. goto out;
  324. }
  325. if (timeout) {
  326. struct timespec64 ts;
  327. err = get_old_timespec32(&ts, timeout);
  328. if (err)
  329. goto out;
  330. err = __do_semtimedop(semid, sops, nsops, &ts, ns);
  331. goto out;
  332. }
  333. err = __do_semtimedop(semid, sops, nsops, NULL, ns);
  334. out:
  335. kvfree(sops);
  336. return err;
  337. }
  338. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  339. unsigned nsops)
  340. {
  341. return sys_oabi_semtimedop(semid, tsops, nsops, NULL);
  342. }
  343. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  344. void __user *ptr, long fifth)
  345. {
  346. switch (call & 0xffff) {
  347. case SEMOP:
  348. return sys_oabi_semtimedop(first,
  349. (struct oabi_sembuf __user *)ptr,
  350. second, NULL);
  351. case SEMTIMEDOP:
  352. return sys_oabi_semtimedop(first,
  353. (struct oabi_sembuf __user *)ptr,
  354. second,
  355. (const struct old_timespec32 __user *)fifth);
  356. default:
  357. return sys_ipc(call, first, second, third, ptr, fifth);
  358. }
  359. }
  360. #else
  361. asmlinkage long sys_oabi_semtimedop(int semid,
  362. struct oabi_sembuf __user *tsops,
  363. unsigned nsops,
  364. const struct old_timespec32 __user *timeout)
  365. {
  366. return -ENOSYS;
  367. }
  368. asmlinkage long sys_oabi_semop(int semid, struct oabi_sembuf __user *tsops,
  369. unsigned nsops)
  370. {
  371. return -ENOSYS;
  372. }
  373. asmlinkage int sys_oabi_ipc(uint call, int first, int second, int third,
  374. void __user *ptr, long fifth)
  375. {
  376. return -ENOSYS;
  377. }
  378. #endif
  379. asmlinkage long sys_oabi_bind(int fd, struct sockaddr __user *addr, int addrlen)
  380. {
  381. sa_family_t sa_family;
  382. if (addrlen == 112 &&
  383. get_user(sa_family, &addr->sa_family) == 0 &&
  384. sa_family == AF_UNIX)
  385. addrlen = 110;
  386. return sys_bind(fd, addr, addrlen);
  387. }
  388. asmlinkage long sys_oabi_connect(int fd, struct sockaddr __user *addr, int addrlen)
  389. {
  390. sa_family_t sa_family;
  391. if (addrlen == 112 &&
  392. get_user(sa_family, &addr->sa_family) == 0 &&
  393. sa_family == AF_UNIX)
  394. addrlen = 110;
  395. return sys_connect(fd, addr, addrlen);
  396. }
  397. asmlinkage long sys_oabi_sendto(int fd, void __user *buff,
  398. size_t len, unsigned flags,
  399. struct sockaddr __user *addr,
  400. int addrlen)
  401. {
  402. sa_family_t sa_family;
  403. if (addrlen == 112 &&
  404. get_user(sa_family, &addr->sa_family) == 0 &&
  405. sa_family == AF_UNIX)
  406. addrlen = 110;
  407. return sys_sendto(fd, buff, len, flags, addr, addrlen);
  408. }
  409. asmlinkage long sys_oabi_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
  410. {
  411. struct sockaddr __user *addr;
  412. int msg_namelen;
  413. sa_family_t sa_family;
  414. if (msg &&
  415. get_user(msg_namelen, &msg->msg_namelen) == 0 &&
  416. msg_namelen == 112 &&
  417. get_user(addr, &msg->msg_name) == 0 &&
  418. get_user(sa_family, &addr->sa_family) == 0 &&
  419. sa_family == AF_UNIX)
  420. {
  421. /*
  422. * HACK ALERT: there is a limit to how much backward bending
  423. * we should do for what is actually a transitional
  424. * compatibility layer. This already has known flaws with
  425. * a few ioctls that we don't intend to fix. Therefore
  426. * consider this blatent hack as another one... and take care
  427. * to run for cover. In most cases it will "just work fine".
  428. * If it doesn't, well, tough.
  429. */
  430. put_user(110, &msg->msg_namelen);
  431. }
  432. return sys_sendmsg(fd, msg, flags);
  433. }
  434. asmlinkage long sys_oabi_socketcall(int call, unsigned long __user *args)
  435. {
  436. unsigned long r = -EFAULT, a[6];
  437. switch (call) {
  438. case SYS_BIND:
  439. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  440. r = sys_oabi_bind(a[0], (struct sockaddr __user *)a[1], a[2]);
  441. break;
  442. case SYS_CONNECT:
  443. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  444. r = sys_oabi_connect(a[0], (struct sockaddr __user *)a[1], a[2]);
  445. break;
  446. case SYS_SENDTO:
  447. if (copy_from_user(a, args, 6 * sizeof(long)) == 0)
  448. r = sys_oabi_sendto(a[0], (void __user *)a[1], a[2], a[3],
  449. (struct sockaddr __user *)a[4], a[5]);
  450. break;
  451. case SYS_SENDMSG:
  452. if (copy_from_user(a, args, 3 * sizeof(long)) == 0)
  453. r = sys_oabi_sendmsg(a[0], (struct user_msghdr __user *)a[1], a[2]);
  454. break;
  455. default:
  456. r = sys_socketcall(call, args);
  457. }
  458. return r;
  459. }