compat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * 32 bit compatibility code for System V IPC
  3. *
  4. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  6. * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
  7. * Copyright (C) 2000 VA Linux Co
  8. * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
  9. * Copyright (C) 2000 Hewlett-Packard Co.
  10. * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
  11. * Copyright (C) 2000 Gerhard Tonn (ton@de.ibm.com)
  12. * Copyright (C) 2000-2002 Andi Kleen, SuSE Labs (x86-64 port)
  13. * Copyright (C) 2000 Silicon Graphics, Inc.
  14. * Copyright (C) 2001 IBM
  15. * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
  16. * Copyright (C) 2004 Arnd Bergmann (arnd@arndb.de)
  17. *
  18. * This code is collected from the versions for sparc64, mips64, s390x, ia64,
  19. * ppc64 and x86_64, all of which are based on the original sparc64 version
  20. * by Jakub Jelinek.
  21. *
  22. */
  23. #include <linux/compat.h>
  24. #include <linux/errno.h>
  25. #include <linux/highuid.h>
  26. #include <linux/init.h>
  27. #include <linux/msg.h>
  28. #include <linux/shm.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/mutex.h>
  32. #include <asm/uaccess.h>
  33. #include "util.h"
  34. struct compat_msgbuf {
  35. compat_long_t mtype;
  36. char mtext[1];
  37. };
  38. struct compat_ipc_perm {
  39. key_t key;
  40. __compat_uid_t uid;
  41. __compat_gid_t gid;
  42. __compat_uid_t cuid;
  43. __compat_gid_t cgid;
  44. compat_mode_t mode;
  45. unsigned short seq;
  46. };
  47. struct compat_semid_ds {
  48. struct compat_ipc_perm sem_perm;
  49. compat_time_t sem_otime;
  50. compat_time_t sem_ctime;
  51. compat_uptr_t sem_base;
  52. compat_uptr_t sem_pending;
  53. compat_uptr_t sem_pending_last;
  54. compat_uptr_t undo;
  55. unsigned short sem_nsems;
  56. };
  57. struct compat_msqid_ds {
  58. struct compat_ipc_perm msg_perm;
  59. compat_uptr_t msg_first;
  60. compat_uptr_t msg_last;
  61. compat_time_t msg_stime;
  62. compat_time_t msg_rtime;
  63. compat_time_t msg_ctime;
  64. compat_ulong_t msg_lcbytes;
  65. compat_ulong_t msg_lqbytes;
  66. unsigned short msg_cbytes;
  67. unsigned short msg_qnum;
  68. unsigned short msg_qbytes;
  69. compat_ipc_pid_t msg_lspid;
  70. compat_ipc_pid_t msg_lrpid;
  71. };
  72. struct compat_shmid_ds {
  73. struct compat_ipc_perm shm_perm;
  74. int shm_segsz;
  75. compat_time_t shm_atime;
  76. compat_time_t shm_dtime;
  77. compat_time_t shm_ctime;
  78. compat_ipc_pid_t shm_cpid;
  79. compat_ipc_pid_t shm_lpid;
  80. unsigned short shm_nattch;
  81. unsigned short shm_unused;
  82. compat_uptr_t shm_unused2;
  83. compat_uptr_t shm_unused3;
  84. };
  85. struct compat_ipc_kludge {
  86. compat_uptr_t msgp;
  87. compat_long_t msgtyp;
  88. };
  89. struct compat_shminfo64 {
  90. compat_ulong_t shmmax;
  91. compat_ulong_t shmmin;
  92. compat_ulong_t shmmni;
  93. compat_ulong_t shmseg;
  94. compat_ulong_t shmall;
  95. compat_ulong_t __unused1;
  96. compat_ulong_t __unused2;
  97. compat_ulong_t __unused3;
  98. compat_ulong_t __unused4;
  99. };
  100. struct compat_shm_info {
  101. compat_int_t used_ids;
  102. compat_ulong_t shm_tot, shm_rss, shm_swp;
  103. compat_ulong_t swap_attempts, swap_successes;
  104. };
  105. extern int sem_ctls[];
  106. #define sc_semopm (sem_ctls[2])
  107. static inline int compat_ipc_parse_version(int *cmd)
  108. {
  109. #ifdef __ARCH_WANT_COMPAT_IPC_PARSE_VERSION
  110. int version = *cmd & IPC_64;
  111. /* this is tricky: architectures that have support for the old
  112. * ipc structures in 64 bit binaries need to have IPC_64 set
  113. * in cmd, the others need to have it cleared */
  114. #ifndef ipc_parse_version
  115. *cmd |= IPC_64;
  116. #else
  117. *cmd &= ~IPC_64;
  118. #endif
  119. return version;
  120. #else
  121. /* With the asm-generic APIs, we always use the 64-bit versions. */
  122. return IPC_64;
  123. #endif
  124. }
  125. static inline int __get_compat_ipc64_perm(struct ipc64_perm *p64,
  126. struct compat_ipc64_perm __user *up64)
  127. {
  128. int err;
  129. err = __get_user(p64->uid, &up64->uid);
  130. err |= __get_user(p64->gid, &up64->gid);
  131. err |= __get_user(p64->mode, &up64->mode);
  132. return err;
  133. }
  134. static inline int __get_compat_ipc_perm(struct ipc64_perm *p,
  135. struct compat_ipc_perm __user *up)
  136. {
  137. int err;
  138. err = __get_user(p->uid, &up->uid);
  139. err |= __get_user(p->gid, &up->gid);
  140. err |= __get_user(p->mode, &up->mode);
  141. return err;
  142. }
  143. static inline int __put_compat_ipc64_perm(struct ipc64_perm *p64,
  144. struct compat_ipc64_perm __user *up64)
  145. {
  146. int err;
  147. err = __put_user(p64->key, &up64->key);
  148. err |= __put_user(p64->uid, &up64->uid);
  149. err |= __put_user(p64->gid, &up64->gid);
  150. err |= __put_user(p64->cuid, &up64->cuid);
  151. err |= __put_user(p64->cgid, &up64->cgid);
  152. err |= __put_user(p64->mode, &up64->mode);
  153. err |= __put_user(p64->seq, &up64->seq);
  154. return err;
  155. }
  156. static inline int __put_compat_ipc_perm(struct ipc64_perm *p,
  157. struct compat_ipc_perm __user *up)
  158. {
  159. int err;
  160. __compat_uid_t u;
  161. __compat_gid_t g;
  162. err = __put_user(p->key, &up->key);
  163. SET_UID(u, p->uid);
  164. err |= __put_user(u, &up->uid);
  165. SET_GID(g, p->gid);
  166. err |= __put_user(g, &up->gid);
  167. SET_UID(u, p->cuid);
  168. err |= __put_user(u, &up->cuid);
  169. SET_GID(g, p->cgid);
  170. err |= __put_user(g, &up->cgid);
  171. err |= __put_user(p->mode, &up->mode);
  172. err |= __put_user(p->seq, &up->seq);
  173. return err;
  174. }
  175. static inline int get_compat_semid64_ds(struct semid64_ds *s64,
  176. struct compat_semid64_ds __user *up64)
  177. {
  178. if (!access_ok (VERIFY_READ, up64, sizeof(*up64)))
  179. return -EFAULT;
  180. return __get_compat_ipc64_perm(&s64->sem_perm, &up64->sem_perm);
  181. }
  182. static inline int get_compat_semid_ds(struct semid64_ds *s,
  183. struct compat_semid_ds __user *up)
  184. {
  185. if (!access_ok (VERIFY_READ, up, sizeof(*up)))
  186. return -EFAULT;
  187. return __get_compat_ipc_perm(&s->sem_perm, &up->sem_perm);
  188. }
  189. static inline int put_compat_semid64_ds(struct semid64_ds *s64,
  190. struct compat_semid64_ds __user *up64)
  191. {
  192. int err;
  193. if (!access_ok (VERIFY_WRITE, up64, sizeof(*up64)))
  194. return -EFAULT;
  195. err = __put_compat_ipc64_perm(&s64->sem_perm, &up64->sem_perm);
  196. err |= __put_user(s64->sem_otime, &up64->sem_otime);
  197. err |= __put_user(s64->sem_ctime, &up64->sem_ctime);
  198. err |= __put_user(s64->sem_nsems, &up64->sem_nsems);
  199. return err;
  200. }
  201. static inline int put_compat_semid_ds(struct semid64_ds *s,
  202. struct compat_semid_ds __user *up)
  203. {
  204. int err;
  205. if (!access_ok (VERIFY_WRITE, up, sizeof(*up)))
  206. return -EFAULT;
  207. err = __put_compat_ipc_perm(&s->sem_perm, &up->sem_perm);
  208. err |= __put_user(s->sem_otime, &up->sem_otime);
  209. err |= __put_user(s->sem_ctime, &up->sem_ctime);
  210. err |= __put_user(s->sem_nsems, &up->sem_nsems);
  211. return err;
  212. }
  213. static long do_compat_semctl(int first, int second, int third, u32 pad)
  214. {
  215. union semun fourth;
  216. int err, err2;
  217. struct semid64_ds s64;
  218. struct semid64_ds __user *up64;
  219. int version = compat_ipc_parse_version(&third);
  220. memset(&s64, 0, sizeof(s64));
  221. if ((third & (~IPC_64)) == SETVAL)
  222. fourth.val = (int) pad;
  223. else
  224. fourth.__pad = compat_ptr(pad);
  225. switch (third & (~IPC_64)) {
  226. case IPC_INFO:
  227. case IPC_RMID:
  228. case SEM_INFO:
  229. case GETVAL:
  230. case GETPID:
  231. case GETNCNT:
  232. case GETZCNT:
  233. case GETALL:
  234. case SETVAL:
  235. case SETALL:
  236. err = sys_semctl(first, second, third, fourth);
  237. break;
  238. case IPC_STAT:
  239. case SEM_STAT:
  240. up64 = compat_alloc_user_space(sizeof(s64));
  241. fourth.__pad = up64;
  242. err = sys_semctl(first, second, third, fourth);
  243. if (err < 0)
  244. break;
  245. if (copy_from_user(&s64, up64, sizeof(s64)))
  246. err2 = -EFAULT;
  247. else if (version == IPC_64)
  248. err2 = put_compat_semid64_ds(&s64, compat_ptr(pad));
  249. else
  250. err2 = put_compat_semid_ds(&s64, compat_ptr(pad));
  251. if (err2)
  252. err = -EFAULT;
  253. break;
  254. case IPC_SET:
  255. if (version == IPC_64) {
  256. err = get_compat_semid64_ds(&s64, compat_ptr(pad));
  257. } else {
  258. err = get_compat_semid_ds(&s64, compat_ptr(pad));
  259. }
  260. up64 = compat_alloc_user_space(sizeof(s64));
  261. if (copy_to_user(up64, &s64, sizeof(s64)))
  262. err = -EFAULT;
  263. if (err)
  264. break;
  265. fourth.__pad = up64;
  266. err = sys_semctl(first, second, third, fourth);
  267. break;
  268. default:
  269. err = -EINVAL;
  270. break;
  271. }
  272. return err;
  273. }
  274. #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
  275. long compat_sys_semctl(int first, int second, int third, void __user *uptr)
  276. {
  277. u32 pad;
  278. if (!uptr)
  279. return -EINVAL;
  280. if (get_user(pad, (u32 __user *) uptr))
  281. return -EFAULT;
  282. return do_compat_semctl(first, second, third, pad);
  283. }
  284. long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
  285. {
  286. struct compat_msgbuf __user *up = uptr;
  287. long type;
  288. if (first < 0)
  289. return -EINVAL;
  290. if (second < 0)
  291. return -EINVAL;
  292. if (get_user(type, &up->mtype))
  293. return -EFAULT;
  294. return do_msgsnd(first, type, up->mtext, second, third);
  295. }
  296. long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
  297. int version, void __user *uptr)
  298. {
  299. struct compat_msgbuf __user *up;
  300. long type;
  301. int err;
  302. if (first < 0)
  303. return -EINVAL;
  304. if (second < 0)
  305. return -EINVAL;
  306. if (!version) {
  307. struct compat_ipc_kludge ipck;
  308. err = -EINVAL;
  309. if (!uptr)
  310. goto out;
  311. err = -EFAULT;
  312. if (copy_from_user (&ipck, uptr, sizeof(ipck)))
  313. goto out;
  314. uptr = compat_ptr(ipck.msgp);
  315. msgtyp = ipck.msgtyp;
  316. }
  317. up = uptr;
  318. err = do_msgrcv(first, &type, up->mtext, second, msgtyp, third);
  319. if (err < 0)
  320. goto out;
  321. if (put_user(type, &up->mtype))
  322. err = -EFAULT;
  323. out:
  324. return err;
  325. }
  326. #else
  327. long compat_sys_semctl(int semid, int semnum, int cmd, int arg)
  328. {
  329. return do_compat_semctl(semid, semnum, cmd, arg);
  330. }
  331. long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
  332. compat_ssize_t msgsz, int msgflg)
  333. {
  334. compat_long_t mtype;
  335. if (get_user(mtype, &msgp->mtype))
  336. return -EFAULT;
  337. return do_msgsnd(msqid, mtype, msgp->mtext, (ssize_t)msgsz, msgflg);
  338. }
  339. long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
  340. compat_ssize_t msgsz, long msgtyp, int msgflg)
  341. {
  342. long err, mtype;
  343. err = do_msgrcv(msqid, &mtype, msgp->mtext, (ssize_t)msgsz, msgtyp, msgflg);
  344. if (err < 0)
  345. goto out;
  346. if (put_user(mtype, &msgp->mtype))
  347. err = -EFAULT;
  348. out:
  349. return err;
  350. }
  351. #endif
  352. static inline int get_compat_msqid64(struct msqid64_ds *m64,
  353. struct compat_msqid64_ds __user *up64)
  354. {
  355. int err;
  356. if (!access_ok(VERIFY_READ, up64, sizeof(*up64)))
  357. return -EFAULT;
  358. err = __get_compat_ipc64_perm(&m64->msg_perm, &up64->msg_perm);
  359. err |= __get_user(m64->msg_qbytes, &up64->msg_qbytes);
  360. return err;
  361. }
  362. static inline int get_compat_msqid(struct msqid64_ds *m,
  363. struct compat_msqid_ds __user *up)
  364. {
  365. int err;
  366. if (!access_ok(VERIFY_READ, up, sizeof(*up)))
  367. return -EFAULT;
  368. err = __get_compat_ipc_perm(&m->msg_perm, &up->msg_perm);
  369. err |= __get_user(m->msg_qbytes, &up->msg_qbytes);
  370. return err;
  371. }
  372. static inline int put_compat_msqid64_ds(struct msqid64_ds *m64,
  373. struct compat_msqid64_ds __user *up64)
  374. {
  375. int err;
  376. if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
  377. return -EFAULT;
  378. err = __put_compat_ipc64_perm(&m64->msg_perm, &up64->msg_perm);
  379. err |= __put_user(m64->msg_stime, &up64->msg_stime);
  380. err |= __put_user(m64->msg_rtime, &up64->msg_rtime);
  381. err |= __put_user(m64->msg_ctime, &up64->msg_ctime);
  382. err |= __put_user(m64->msg_cbytes, &up64->msg_cbytes);
  383. err |= __put_user(m64->msg_qnum, &up64->msg_qnum);
  384. err |= __put_user(m64->msg_qbytes, &up64->msg_qbytes);
  385. err |= __put_user(m64->msg_lspid, &up64->msg_lspid);
  386. err |= __put_user(m64->msg_lrpid, &up64->msg_lrpid);
  387. return err;
  388. }
  389. static inline int put_compat_msqid_ds(struct msqid64_ds *m,
  390. struct compat_msqid_ds __user *up)
  391. {
  392. int err;
  393. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
  394. return -EFAULT;
  395. err = __put_compat_ipc_perm(&m->msg_perm, &up->msg_perm);
  396. err |= __put_user(m->msg_stime, &up->msg_stime);
  397. err |= __put_user(m->msg_rtime, &up->msg_rtime);
  398. err |= __put_user(m->msg_ctime, &up->msg_ctime);
  399. err |= __put_user(m->msg_cbytes, &up->msg_cbytes);
  400. err |= __put_user(m->msg_qnum, &up->msg_qnum);
  401. err |= __put_user(m->msg_qbytes, &up->msg_qbytes);
  402. err |= __put_user(m->msg_lspid, &up->msg_lspid);
  403. err |= __put_user(m->msg_lrpid, &up->msg_lrpid);
  404. return err;
  405. }
  406. long compat_sys_msgctl(int first, int second, void __user *uptr)
  407. {
  408. int err, err2;
  409. struct msqid64_ds m64;
  410. int version = compat_ipc_parse_version(&second);
  411. void __user *p;
  412. memset(&m64, 0, sizeof(m64));
  413. switch (second & (~IPC_64)) {
  414. case IPC_INFO:
  415. case IPC_RMID:
  416. case MSG_INFO:
  417. err = sys_msgctl(first, second, uptr);
  418. break;
  419. case IPC_SET:
  420. if (version == IPC_64) {
  421. err = get_compat_msqid64(&m64, uptr);
  422. } else {
  423. err = get_compat_msqid(&m64, uptr);
  424. }
  425. if (err)
  426. break;
  427. p = compat_alloc_user_space(sizeof(m64));
  428. if (copy_to_user(p, &m64, sizeof(m64)))
  429. err = -EFAULT;
  430. else
  431. err = sys_msgctl(first, second, p);
  432. break;
  433. case IPC_STAT:
  434. case MSG_STAT:
  435. p = compat_alloc_user_space(sizeof(m64));
  436. err = sys_msgctl(first, second, p);
  437. if (err < 0)
  438. break;
  439. if (copy_from_user(&m64, p, sizeof(m64)))
  440. err2 = -EFAULT;
  441. else if (version == IPC_64)
  442. err2 = put_compat_msqid64_ds(&m64, uptr);
  443. else
  444. err2 = put_compat_msqid_ds(&m64, uptr);
  445. if (err2)
  446. err = -EFAULT;
  447. break;
  448. default:
  449. err = -EINVAL;
  450. break;
  451. }
  452. return err;
  453. }
  454. #ifndef COMPAT_SHMLBA
  455. #define COMPAT_SHMLBA SHMLBA
  456. #endif
  457. #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
  458. long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
  459. void __user *uptr)
  460. {
  461. int err;
  462. unsigned long raddr;
  463. compat_ulong_t __user *uaddr;
  464. if (version == 1)
  465. return -EINVAL;
  466. err = do_shmat(first, uptr, second, &raddr, COMPAT_SHMLBA);
  467. if (err < 0)
  468. return err;
  469. uaddr = compat_ptr(third);
  470. return put_user(raddr, uaddr);
  471. }
  472. #else
  473. long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg)
  474. {
  475. unsigned long ret;
  476. long err;
  477. err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
  478. if (err)
  479. return err;
  480. force_successful_syscall_return();
  481. return (long)ret;
  482. }
  483. #endif
  484. static inline int get_compat_shmid64_ds(struct shmid64_ds *s64,
  485. struct compat_shmid64_ds __user *up64)
  486. {
  487. if (!access_ok(VERIFY_READ, up64, sizeof(*up64)))
  488. return -EFAULT;
  489. return __get_compat_ipc64_perm(&s64->shm_perm, &up64->shm_perm);
  490. }
  491. static inline int get_compat_shmid_ds(struct shmid64_ds *s,
  492. struct compat_shmid_ds __user *up)
  493. {
  494. if (!access_ok(VERIFY_READ, up, sizeof(*up)))
  495. return -EFAULT;
  496. return __get_compat_ipc_perm(&s->shm_perm, &up->shm_perm);
  497. }
  498. static inline int put_compat_shmid64_ds(struct shmid64_ds *s64,
  499. struct compat_shmid64_ds __user *up64)
  500. {
  501. int err;
  502. if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
  503. return -EFAULT;
  504. err = __put_compat_ipc64_perm(&s64->shm_perm, &up64->shm_perm);
  505. err |= __put_user(s64->shm_atime, &up64->shm_atime);
  506. err |= __put_user(s64->shm_dtime, &up64->shm_dtime);
  507. err |= __put_user(s64->shm_ctime, &up64->shm_ctime);
  508. err |= __put_user(s64->shm_segsz, &up64->shm_segsz);
  509. err |= __put_user(s64->shm_nattch, &up64->shm_nattch);
  510. err |= __put_user(s64->shm_cpid, &up64->shm_cpid);
  511. err |= __put_user(s64->shm_lpid, &up64->shm_lpid);
  512. return err;
  513. }
  514. static inline int put_compat_shmid_ds(struct shmid64_ds *s,
  515. struct compat_shmid_ds __user *up)
  516. {
  517. int err;
  518. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
  519. return -EFAULT;
  520. err = __put_compat_ipc_perm(&s->shm_perm, &up->shm_perm);
  521. err |= __put_user(s->shm_atime, &up->shm_atime);
  522. err |= __put_user(s->shm_dtime, &up->shm_dtime);
  523. err |= __put_user(s->shm_ctime, &up->shm_ctime);
  524. err |= __put_user(s->shm_segsz, &up->shm_segsz);
  525. err |= __put_user(s->shm_nattch, &up->shm_nattch);
  526. err |= __put_user(s->shm_cpid, &up->shm_cpid);
  527. err |= __put_user(s->shm_lpid, &up->shm_lpid);
  528. return err;
  529. }
  530. static inline int put_compat_shminfo64(struct shminfo64 *smi,
  531. struct compat_shminfo64 __user *up64)
  532. {
  533. int err;
  534. if (!access_ok(VERIFY_WRITE, up64, sizeof(*up64)))
  535. return -EFAULT;
  536. if (smi->shmmax > INT_MAX)
  537. smi->shmmax = INT_MAX;
  538. err = __put_user(smi->shmmax, &up64->shmmax);
  539. err |= __put_user(smi->shmmin, &up64->shmmin);
  540. err |= __put_user(smi->shmmni, &up64->shmmni);
  541. err |= __put_user(smi->shmseg, &up64->shmseg);
  542. err |= __put_user(smi->shmall, &up64->shmall);
  543. return err;
  544. }
  545. static inline int put_compat_shminfo(struct shminfo64 *smi,
  546. struct shminfo __user *up)
  547. {
  548. int err;
  549. if (!access_ok(VERIFY_WRITE, up, sizeof(*up)))
  550. return -EFAULT;
  551. if (smi->shmmax > INT_MAX)
  552. smi->shmmax = INT_MAX;
  553. err = __put_user(smi->shmmax, &up->shmmax);
  554. err |= __put_user(smi->shmmin, &up->shmmin);
  555. err |= __put_user(smi->shmmni, &up->shmmni);
  556. err |= __put_user(smi->shmseg, &up->shmseg);
  557. err |= __put_user(smi->shmall, &up->shmall);
  558. return err;
  559. }
  560. static inline int put_compat_shm_info(struct shm_info __user *ip,
  561. struct compat_shm_info __user *uip)
  562. {
  563. int err;
  564. struct shm_info si;
  565. if (!access_ok(VERIFY_WRITE, uip, sizeof(*uip)) ||
  566. copy_from_user(&si, ip, sizeof(si)))
  567. return -EFAULT;
  568. err = __put_user(si.used_ids, &uip->used_ids);
  569. err |= __put_user(si.shm_tot, &uip->shm_tot);
  570. err |= __put_user(si.shm_rss, &uip->shm_rss);
  571. err |= __put_user(si.shm_swp, &uip->shm_swp);
  572. err |= __put_user(si.swap_attempts, &uip->swap_attempts);
  573. err |= __put_user(si.swap_successes, &uip->swap_successes);
  574. return err;
  575. }
  576. long compat_sys_shmctl(int first, int second, void __user *uptr)
  577. {
  578. void __user *p;
  579. struct shmid64_ds s64;
  580. struct shminfo64 smi;
  581. int err, err2;
  582. int version = compat_ipc_parse_version(&second);
  583. memset(&s64, 0, sizeof(s64));
  584. switch (second & (~IPC_64)) {
  585. case IPC_RMID:
  586. case SHM_LOCK:
  587. case SHM_UNLOCK:
  588. err = sys_shmctl(first, second, uptr);
  589. break;
  590. case IPC_INFO:
  591. p = compat_alloc_user_space(sizeof(smi));
  592. err = sys_shmctl(first, second, p);
  593. if (err < 0)
  594. break;
  595. if (copy_from_user(&smi, p, sizeof(smi)))
  596. err2 = -EFAULT;
  597. else if (version == IPC_64)
  598. err2 = put_compat_shminfo64(&smi, uptr);
  599. else
  600. err2 = put_compat_shminfo(&smi, uptr);
  601. if (err2)
  602. err = -EFAULT;
  603. break;
  604. case IPC_SET:
  605. if (version == IPC_64) {
  606. err = get_compat_shmid64_ds(&s64, uptr);
  607. } else {
  608. err = get_compat_shmid_ds(&s64, uptr);
  609. }
  610. if (err)
  611. break;
  612. p = compat_alloc_user_space(sizeof(s64));
  613. if (copy_to_user(p, &s64, sizeof(s64)))
  614. err = -EFAULT;
  615. else
  616. err = sys_shmctl(first, second, p);
  617. break;
  618. case IPC_STAT:
  619. case SHM_STAT:
  620. p = compat_alloc_user_space(sizeof(s64));
  621. err = sys_shmctl(first, second, p);
  622. if (err < 0)
  623. break;
  624. if (copy_from_user(&s64, p, sizeof(s64)))
  625. err2 = -EFAULT;
  626. else if (version == IPC_64)
  627. err2 = put_compat_shmid64_ds(&s64, uptr);
  628. else
  629. err2 = put_compat_shmid_ds(&s64, uptr);
  630. if (err2)
  631. err = -EFAULT;
  632. break;
  633. case SHM_INFO:
  634. p = compat_alloc_user_space(sizeof(struct shm_info));
  635. err = sys_shmctl(first, second, p);
  636. if (err < 0)
  637. break;
  638. err2 = put_compat_shm_info(p, uptr);
  639. if (err2)
  640. err = -EFAULT;
  641. break;
  642. default:
  643. err = -EINVAL;
  644. break;
  645. }
  646. return err;
  647. }
  648. long compat_sys_semtimedop(int semid, struct sembuf __user *tsems,
  649. unsigned nsops, const struct compat_timespec __user *timeout)
  650. {
  651. struct timespec __user *ts64 = NULL;
  652. if (timeout) {
  653. struct timespec ts;
  654. ts64 = compat_alloc_user_space(sizeof(*ts64));
  655. if (get_compat_timespec(&ts, timeout))
  656. return -EFAULT;
  657. if (copy_to_user(ts64, &ts, sizeof(ts)))
  658. return -EFAULT;
  659. }
  660. return sys_semtimedop(semid, tsems, nsops, ts64);
  661. }