2 * 32 bit compatibility code for System V IPC
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)
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
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>
32 #include <linux/mutex.h>
33 #include <asm/uaccess.h>
37 struct compat_msgbuf
{
42 struct compat_ipc_perm
{
52 struct compat_semid_ds
{
53 struct compat_ipc_perm sem_perm
;
54 compat_time_t sem_otime
;
55 compat_time_t sem_ctime
;
56 compat_uptr_t sem_base
;
57 compat_uptr_t sem_pending
;
58 compat_uptr_t sem_pending_last
;
60 unsigned short sem_nsems
;
63 struct compat_msqid_ds
{
64 struct compat_ipc_perm msg_perm
;
65 compat_uptr_t msg_first
;
66 compat_uptr_t msg_last
;
67 compat_time_t msg_stime
;
68 compat_time_t msg_rtime
;
69 compat_time_t msg_ctime
;
70 compat_ulong_t msg_lcbytes
;
71 compat_ulong_t msg_lqbytes
;
72 unsigned short msg_cbytes
;
73 unsigned short msg_qnum
;
74 unsigned short msg_qbytes
;
75 compat_ipc_pid_t msg_lspid
;
76 compat_ipc_pid_t msg_lrpid
;
79 struct compat_shmid_ds
{
80 struct compat_ipc_perm shm_perm
;
82 compat_time_t shm_atime
;
83 compat_time_t shm_dtime
;
84 compat_time_t shm_ctime
;
85 compat_ipc_pid_t shm_cpid
;
86 compat_ipc_pid_t shm_lpid
;
87 unsigned short shm_nattch
;
88 unsigned short shm_unused
;
89 compat_uptr_t shm_unused2
;
90 compat_uptr_t shm_unused3
;
93 struct compat_ipc_kludge
{
98 struct compat_shminfo64
{
99 compat_ulong_t shmmax
;
100 compat_ulong_t shmmin
;
101 compat_ulong_t shmmni
;
102 compat_ulong_t shmseg
;
103 compat_ulong_t shmall
;
104 compat_ulong_t __unused1
;
105 compat_ulong_t __unused2
;
106 compat_ulong_t __unused3
;
107 compat_ulong_t __unused4
;
110 struct compat_shm_info
{
111 compat_int_t used_ids
;
112 compat_ulong_t shm_tot
, shm_rss
, shm_swp
;
113 compat_ulong_t swap_attempts
, swap_successes
;
116 static inline int compat_ipc_parse_version(int *cmd
)
118 #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
119 int version
= *cmd
& IPC_64
;
121 /* this is tricky: architectures that have support for the old
122 * ipc structures in 64 bit binaries need to have IPC_64 set
123 * in cmd, the others need to have it cleared */
124 #ifndef ipc_parse_version
131 /* With the asm-generic APIs, we always use the 64-bit versions. */
136 static inline int __get_compat_ipc64_perm(struct ipc64_perm
*p64
,
137 struct compat_ipc64_perm __user
*up64
)
141 err
= __get_user(p64
->uid
, &up64
->uid
);
142 err
|= __get_user(p64
->gid
, &up64
->gid
);
143 err
|= __get_user(p64
->mode
, &up64
->mode
);
147 static inline int __get_compat_ipc_perm(struct ipc64_perm
*p
,
148 struct compat_ipc_perm __user
*up
)
152 err
= __get_user(p
->uid
, &up
->uid
);
153 err
|= __get_user(p
->gid
, &up
->gid
);
154 err
|= __get_user(p
->mode
, &up
->mode
);
158 static inline int __put_compat_ipc64_perm(struct ipc64_perm
*p64
,
159 struct compat_ipc64_perm __user
*up64
)
163 err
= __put_user(p64
->key
, &up64
->key
);
164 err
|= __put_user(p64
->uid
, &up64
->uid
);
165 err
|= __put_user(p64
->gid
, &up64
->gid
);
166 err
|= __put_user(p64
->cuid
, &up64
->cuid
);
167 err
|= __put_user(p64
->cgid
, &up64
->cgid
);
168 err
|= __put_user(p64
->mode
, &up64
->mode
);
169 err
|= __put_user(p64
->seq
, &up64
->seq
);
173 static inline int __put_compat_ipc_perm(struct ipc64_perm
*p
,
174 struct compat_ipc_perm __user
*up
)
180 err
= __put_user(p
->key
, &up
->key
);
182 err
|= __put_user(u
, &up
->uid
);
184 err
|= __put_user(g
, &up
->gid
);
186 err
|= __put_user(u
, &up
->cuid
);
188 err
|= __put_user(g
, &up
->cgid
);
189 err
|= __put_user(p
->mode
, &up
->mode
);
190 err
|= __put_user(p
->seq
, &up
->seq
);
194 static inline int get_compat_semid64_ds(struct semid64_ds
*s64
,
195 struct compat_semid64_ds __user
*up64
)
197 if (!access_ok(VERIFY_READ
, up64
, sizeof(*up64
)))
199 return __get_compat_ipc64_perm(&s64
->sem_perm
, &up64
->sem_perm
);
202 static inline int get_compat_semid_ds(struct semid64_ds
*s
,
203 struct compat_semid_ds __user
*up
)
205 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)))
207 return __get_compat_ipc_perm(&s
->sem_perm
, &up
->sem_perm
);
210 static inline int put_compat_semid64_ds(struct semid64_ds
*s64
,
211 struct compat_semid64_ds __user
*up64
)
215 if (!access_ok(VERIFY_WRITE
, up64
, sizeof(*up64
)))
217 err
= __put_compat_ipc64_perm(&s64
->sem_perm
, &up64
->sem_perm
);
218 err
|= __put_user(s64
->sem_otime
, &up64
->sem_otime
);
219 err
|= __put_user(s64
->sem_ctime
, &up64
->sem_ctime
);
220 err
|= __put_user(s64
->sem_nsems
, &up64
->sem_nsems
);
224 static inline int put_compat_semid_ds(struct semid64_ds
*s
,
225 struct compat_semid_ds __user
*up
)
229 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)))
231 err
= __put_compat_ipc_perm(&s
->sem_perm
, &up
->sem_perm
);
232 err
|= __put_user(s
->sem_otime
, &up
->sem_otime
);
233 err
|= __put_user(s
->sem_ctime
, &up
->sem_ctime
);
234 err
|= __put_user(s
->sem_nsems
, &up
->sem_nsems
);
238 static long do_compat_semctl(int first
, int second
, int third
, u32 pad
)
240 unsigned long fourth
;
242 struct semid64_ds s64
;
243 struct semid64_ds __user
*up64
;
244 int version
= compat_ipc_parse_version(&third
);
246 memset(&s64
, 0, sizeof(s64
));
248 if ((third
& (~IPC_64
)) == SETVAL
)
250 fourth
= (unsigned long)pad
<< 32;
255 fourth
= (unsigned long)compat_ptr(pad
);
256 switch (third
& (~IPC_64
)) {
267 err
= sys_semctl(first
, second
, third
, fourth
);
272 up64
= compat_alloc_user_space(sizeof(s64
));
273 fourth
= (unsigned long)up64
;
274 err
= sys_semctl(first
, second
, third
, fourth
);
277 if (copy_from_user(&s64
, up64
, sizeof(s64
)))
279 else if (version
== IPC_64
)
280 err2
= put_compat_semid64_ds(&s64
, compat_ptr(pad
));
282 err2
= put_compat_semid_ds(&s64
, compat_ptr(pad
));
288 if (version
== IPC_64
)
289 err
= get_compat_semid64_ds(&s64
, compat_ptr(pad
));
291 err
= get_compat_semid_ds(&s64
, compat_ptr(pad
));
293 up64
= compat_alloc_user_space(sizeof(s64
));
294 if (copy_to_user(up64
, &s64
, sizeof(s64
)))
299 fourth
= (unsigned long)up64
;
300 err
= sys_semctl(first
, second
, third
, fourth
);
310 static long compat_do_msg_fill(void __user
*dest
, struct msg_msg
*msg
, size_t bufsz
)
312 struct compat_msgbuf __user
*msgp
= dest
;
315 if (put_user(msg
->m_type
, &msgp
->mtype
))
318 msgsz
= (bufsz
> msg
->m_ts
) ? msg
->m_ts
: bufsz
;
319 if (store_msg(msgp
->mtext
, msg
, msgsz
))
324 #ifndef COMPAT_SHMLBA
325 #define COMPAT_SHMLBA SHMLBA
328 #ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
329 COMPAT_SYSCALL_DEFINE6(ipc
, u32
, call
, int, first
, int, second
,
330 u32
, third
, compat_uptr_t
, ptr
, u32
, fifth
)
335 version
= call
>> 16; /* hack for backward compatibility */
340 /* struct sembuf is the same on 32 and 64bit :)) */
341 return sys_semtimedop(first
, compat_ptr(ptr
), second
, NULL
);
343 return compat_sys_semtimedop(first
, compat_ptr(ptr
), second
,
346 return sys_semget(first
, second
, third
);
350 if (get_user(pad
, (u32 __user
*) compat_ptr(ptr
)))
352 return do_compat_semctl(first
, second
, third
, pad
);
355 struct compat_msgbuf __user
*up
= compat_ptr(ptr
);
358 if (first
< 0 || second
< 0)
361 if (get_user(type
, &up
->mtype
))
364 return do_msgsnd(first
, type
, up
->mtext
, second
, third
);
367 void __user
*uptr
= compat_ptr(ptr
);
369 if (first
< 0 || second
< 0)
373 struct compat_ipc_kludge ipck
;
376 if (copy_from_user(&ipck
, uptr
, sizeof(ipck
)))
378 uptr
= compat_ptr(ipck
.msgp
);
381 return do_msgrcv(first
, uptr
, second
, (s32
)fifth
, third
,
385 return sys_msgget(first
, second
);
387 return compat_sys_msgctl(first
, second
, compat_ptr(ptr
));
395 err
= do_shmat(first
, compat_ptr(ptr
), second
, &raddr
,
399 return put_user(raddr
, (compat_ulong_t
*)compat_ptr(third
));
402 return sys_shmdt(compat_ptr(ptr
));
404 return sys_shmget(first
, (unsigned)second
, third
);
406 return compat_sys_shmctl(first
, second
, compat_ptr(ptr
));
413 COMPAT_SYSCALL_DEFINE4(semctl
, int, semid
, int, semnum
, int, cmd
, int, arg
)
415 return do_compat_semctl(semid
, semnum
, cmd
, arg
);
418 COMPAT_SYSCALL_DEFINE4(msgsnd
, int, msqid
, compat_uptr_t
, msgp
,
419 compat_ssize_t
, msgsz
, int, msgflg
)
421 struct compat_msgbuf __user
*up
= compat_ptr(msgp
);
424 if (get_user(mtype
, &up
->mtype
))
426 return do_msgsnd(msqid
, mtype
, up
->mtext
, (ssize_t
)msgsz
, msgflg
);
429 COMPAT_SYSCALL_DEFINE5(msgrcv
, int, msqid
, compat_uptr_t
, msgp
,
430 compat_ssize_t
, msgsz
, compat_long_t
, msgtyp
, int, msgflg
)
432 return do_msgrcv(msqid
, compat_ptr(msgp
), (ssize_t
)msgsz
, (long)msgtyp
,
433 msgflg
, compat_do_msg_fill
);
436 static inline int get_compat_msqid64(struct msqid64_ds
*m64
,
437 struct compat_msqid64_ds __user
*up64
)
441 if (!access_ok(VERIFY_READ
, up64
, sizeof(*up64
)))
443 err
= __get_compat_ipc64_perm(&m64
->msg_perm
, &up64
->msg_perm
);
444 err
|= __get_user(m64
->msg_qbytes
, &up64
->msg_qbytes
);
448 static inline int get_compat_msqid(struct msqid64_ds
*m
,
449 struct compat_msqid_ds __user
*up
)
453 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)))
455 err
= __get_compat_ipc_perm(&m
->msg_perm
, &up
->msg_perm
);
456 err
|= __get_user(m
->msg_qbytes
, &up
->msg_qbytes
);
460 static inline int put_compat_msqid64_ds(struct msqid64_ds
*m64
,
461 struct compat_msqid64_ds __user
*up64
)
465 if (!access_ok(VERIFY_WRITE
, up64
, sizeof(*up64
)))
467 err
= __put_compat_ipc64_perm(&m64
->msg_perm
, &up64
->msg_perm
);
468 err
|= __put_user(m64
->msg_stime
, &up64
->msg_stime
);
469 err
|= __put_user(m64
->msg_rtime
, &up64
->msg_rtime
);
470 err
|= __put_user(m64
->msg_ctime
, &up64
->msg_ctime
);
471 err
|= __put_user(m64
->msg_cbytes
, &up64
->msg_cbytes
);
472 err
|= __put_user(m64
->msg_qnum
, &up64
->msg_qnum
);
473 err
|= __put_user(m64
->msg_qbytes
, &up64
->msg_qbytes
);
474 err
|= __put_user(m64
->msg_lspid
, &up64
->msg_lspid
);
475 err
|= __put_user(m64
->msg_lrpid
, &up64
->msg_lrpid
);
479 static inline int put_compat_msqid_ds(struct msqid64_ds
*m
,
480 struct compat_msqid_ds __user
*up
)
484 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)))
486 err
= __put_compat_ipc_perm(&m
->msg_perm
, &up
->msg_perm
);
487 err
|= __put_user(m
->msg_stime
, &up
->msg_stime
);
488 err
|= __put_user(m
->msg_rtime
, &up
->msg_rtime
);
489 err
|= __put_user(m
->msg_ctime
, &up
->msg_ctime
);
490 err
|= __put_user(m
->msg_cbytes
, &up
->msg_cbytes
);
491 err
|= __put_user(m
->msg_qnum
, &up
->msg_qnum
);
492 err
|= __put_user(m
->msg_qbytes
, &up
->msg_qbytes
);
493 err
|= __put_user(m
->msg_lspid
, &up
->msg_lspid
);
494 err
|= __put_user(m
->msg_lrpid
, &up
->msg_lrpid
);
498 COMPAT_SYSCALL_DEFINE3(msgctl
, int, first
, int, second
, void __user
*, uptr
)
501 struct msqid64_ds m64
;
502 int version
= compat_ipc_parse_version(&second
);
505 memset(&m64
, 0, sizeof(m64
));
507 switch (second
& (~IPC_64
)) {
511 err
= sys_msgctl(first
, second
, uptr
);
515 if (version
== IPC_64
)
516 err
= get_compat_msqid64(&m64
, uptr
);
518 err
= get_compat_msqid(&m64
, uptr
);
522 p
= compat_alloc_user_space(sizeof(m64
));
523 if (copy_to_user(p
, &m64
, sizeof(m64
)))
526 err
= sys_msgctl(first
, second
, p
);
531 p
= compat_alloc_user_space(sizeof(m64
));
532 err
= sys_msgctl(first
, second
, p
);
535 if (copy_from_user(&m64
, p
, sizeof(m64
)))
537 else if (version
== IPC_64
)
538 err2
= put_compat_msqid64_ds(&m64
, uptr
);
540 err2
= put_compat_msqid_ds(&m64
, uptr
);
552 COMPAT_SYSCALL_DEFINE3(shmat
, int, shmid
, compat_uptr_t
, shmaddr
, int, shmflg
)
557 err
= do_shmat(shmid
, compat_ptr(shmaddr
), shmflg
, &ret
, COMPAT_SHMLBA
);
560 force_successful_syscall_return();
564 static inline int get_compat_shmid64_ds(struct shmid64_ds
*s64
,
565 struct compat_shmid64_ds __user
*up64
)
567 if (!access_ok(VERIFY_READ
, up64
, sizeof(*up64
)))
569 return __get_compat_ipc64_perm(&s64
->shm_perm
, &up64
->shm_perm
);
572 static inline int get_compat_shmid_ds(struct shmid64_ds
*s
,
573 struct compat_shmid_ds __user
*up
)
575 if (!access_ok(VERIFY_READ
, up
, sizeof(*up
)))
577 return __get_compat_ipc_perm(&s
->shm_perm
, &up
->shm_perm
);
580 static inline int put_compat_shmid64_ds(struct shmid64_ds
*s64
,
581 struct compat_shmid64_ds __user
*up64
)
585 if (!access_ok(VERIFY_WRITE
, up64
, sizeof(*up64
)))
587 err
= __put_compat_ipc64_perm(&s64
->shm_perm
, &up64
->shm_perm
);
588 err
|= __put_user(s64
->shm_atime
, &up64
->shm_atime
);
589 err
|= __put_user(s64
->shm_dtime
, &up64
->shm_dtime
);
590 err
|= __put_user(s64
->shm_ctime
, &up64
->shm_ctime
);
591 err
|= __put_user(s64
->shm_segsz
, &up64
->shm_segsz
);
592 err
|= __put_user(s64
->shm_nattch
, &up64
->shm_nattch
);
593 err
|= __put_user(s64
->shm_cpid
, &up64
->shm_cpid
);
594 err
|= __put_user(s64
->shm_lpid
, &up64
->shm_lpid
);
598 static inline int put_compat_shmid_ds(struct shmid64_ds
*s
,
599 struct compat_shmid_ds __user
*up
)
603 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)))
605 err
= __put_compat_ipc_perm(&s
->shm_perm
, &up
->shm_perm
);
606 err
|= __put_user(s
->shm_atime
, &up
->shm_atime
);
607 err
|= __put_user(s
->shm_dtime
, &up
->shm_dtime
);
608 err
|= __put_user(s
->shm_ctime
, &up
->shm_ctime
);
609 err
|= __put_user(s
->shm_segsz
, &up
->shm_segsz
);
610 err
|= __put_user(s
->shm_nattch
, &up
->shm_nattch
);
611 err
|= __put_user(s
->shm_cpid
, &up
->shm_cpid
);
612 err
|= __put_user(s
->shm_lpid
, &up
->shm_lpid
);
616 static inline int put_compat_shminfo64(struct shminfo64
*smi
,
617 struct compat_shminfo64 __user
*up64
)
621 if (!access_ok(VERIFY_WRITE
, up64
, sizeof(*up64
)))
623 if (smi
->shmmax
> INT_MAX
)
624 smi
->shmmax
= INT_MAX
;
625 err
= __put_user(smi
->shmmax
, &up64
->shmmax
);
626 err
|= __put_user(smi
->shmmin
, &up64
->shmmin
);
627 err
|= __put_user(smi
->shmmni
, &up64
->shmmni
);
628 err
|= __put_user(smi
->shmseg
, &up64
->shmseg
);
629 err
|= __put_user(smi
->shmall
, &up64
->shmall
);
633 static inline int put_compat_shminfo(struct shminfo64
*smi
,
634 struct shminfo __user
*up
)
638 if (!access_ok(VERIFY_WRITE
, up
, sizeof(*up
)))
640 if (smi
->shmmax
> INT_MAX
)
641 smi
->shmmax
= INT_MAX
;
642 err
= __put_user(smi
->shmmax
, &up
->shmmax
);
643 err
|= __put_user(smi
->shmmin
, &up
->shmmin
);
644 err
|= __put_user(smi
->shmmni
, &up
->shmmni
);
645 err
|= __put_user(smi
->shmseg
, &up
->shmseg
);
646 err
|= __put_user(smi
->shmall
, &up
->shmall
);
650 static inline int put_compat_shm_info(struct shm_info __user
*ip
,
651 struct compat_shm_info __user
*uip
)
656 if (!access_ok(VERIFY_WRITE
, uip
, sizeof(*uip
)) ||
657 copy_from_user(&si
, ip
, sizeof(si
)))
659 err
= __put_user(si
.used_ids
, &uip
->used_ids
);
660 err
|= __put_user(si
.shm_tot
, &uip
->shm_tot
);
661 err
|= __put_user(si
.shm_rss
, &uip
->shm_rss
);
662 err
|= __put_user(si
.shm_swp
, &uip
->shm_swp
);
663 err
|= __put_user(si
.swap_attempts
, &uip
->swap_attempts
);
664 err
|= __put_user(si
.swap_successes
, &uip
->swap_successes
);
668 COMPAT_SYSCALL_DEFINE3(shmctl
, int, first
, int, second
, void __user
*, uptr
)
671 struct shmid64_ds s64
;
672 struct shminfo64 smi
;
674 int version
= compat_ipc_parse_version(&second
);
676 memset(&s64
, 0, sizeof(s64
));
678 switch (second
& (~IPC_64
)) {
682 err
= sys_shmctl(first
, second
, uptr
);
686 p
= compat_alloc_user_space(sizeof(smi
));
687 err
= sys_shmctl(first
, second
, p
);
690 if (copy_from_user(&smi
, p
, sizeof(smi
)))
692 else if (version
== IPC_64
)
693 err2
= put_compat_shminfo64(&smi
, uptr
);
695 err2
= put_compat_shminfo(&smi
, uptr
);
702 if (version
== IPC_64
)
703 err
= get_compat_shmid64_ds(&s64
, uptr
);
705 err
= get_compat_shmid_ds(&s64
, uptr
);
709 p
= compat_alloc_user_space(sizeof(s64
));
710 if (copy_to_user(p
, &s64
, sizeof(s64
)))
713 err
= sys_shmctl(first
, second
, p
);
718 p
= compat_alloc_user_space(sizeof(s64
));
719 err
= sys_shmctl(first
, second
, p
);
722 if (copy_from_user(&s64
, p
, sizeof(s64
)))
724 else if (version
== IPC_64
)
725 err2
= put_compat_shmid64_ds(&s64
, uptr
);
727 err2
= put_compat_shmid_ds(&s64
, uptr
);
733 p
= compat_alloc_user_space(sizeof(struct shm_info
));
734 err
= sys_shmctl(first
, second
, p
);
737 err2
= put_compat_shm_info(p
, uptr
);
749 COMPAT_SYSCALL_DEFINE4(semtimedop
, int, semid
, struct sembuf __user
*, tsems
,
751 const struct compat_timespec __user
*, timeout
)
753 struct timespec __user
*ts64
;
754 if (compat_convert_timespec(&ts64
, timeout
))
756 return sys_semtimedop(semid
, tsems
, nsops
, ts64
);