sys_oabi-compat.c 12 KB

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