Skip to content

Commit 48b25c4

Browse files
[PATCH v3] ipc: provide generic compat versions of IPC syscalls
When using the "compat" APIs, architectures will generally want to be able to make direct syscalls to msgsnd(), shmctl(), etc., and in the kernel we would want them to be handled directly by compat_sys_xxx() functions, as is true for other compat syscalls. However, for historical reasons, several of the existing compat IPC syscalls do not do this. semctl() expects a pointer to the fourth argument, instead of the fourth argument itself. msgsnd(), msgrcv() and shmat() expect arguments in different order. This change adds an ARCH_WANT_OLD_COMPAT_IPC config option that can be set to preserve this behavior for ports that use it (x86, sparc, powerpc, s390, and mips). No actual semantics are changed for those architectures, and there is only a minimal amount of code refactoring in ipc/compat.c. Newer architectures like tile (and perhaps future architectures such as arm64 and unicore64) should not select this option, and thus can avoid having any IPC-specific code at all in their architecture-specific compat layer. In the same vein, if this option is not selected, IPC_64 mode is assumed, since that's what the <asm-generic> headers expect. The workaround code in "tile" for msgsnd() and msgrcv() is removed with this change; it also fixes the bug that shmat() and semctl() were not being properly handled. Reviewed-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
1 parent fde7d90 commit 48b25c4

10 files changed

Lines changed: 83 additions & 61 deletions

File tree

‎arch/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,7 @@ config HAVE_CMPXCHG_LOCAL
199199
config HAVE_CMPXCHG_DOUBLE
200200
bool
201201

202+
config ARCH_WANT_OLD_COMPAT_IPC
203+
bool
204+
202205
source "kernel/gcov/Kconfig"

‎arch/mips/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,7 @@ config MIPS32_COMPAT
24562456
config COMPAT
24572457
bool
24582458
depends on MIPS32_COMPAT
2459+
select ARCH_WANT_OLD_COMPAT_IPC
24592460
default y
24602461

24612462
config SYSVIPC_COMPAT

‎arch/powerpc/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ config COMPAT
152152
bool
153153
default y if PPC64
154154
select COMPAT_BINFMT_ELF
155+
select ARCH_WANT_OLD_COMPAT_IPC
155156

156157
config SYSVIPC_COMPAT
157158
bool

‎arch/s390/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ config COMPAT
218218
prompt "Kernel support for 31 bit emulation"
219219
depends on 64BIT
220220
select COMPAT_BINFMT_ELF
221+
select ARCH_WANT_OLD_COMPAT_IPC
221222
help
222223
Select this option if you want to enable your system kernel to
223224
handle system-calls from ELF binaries for 31 bit ESA. This option

‎arch/sparc/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ config COMPAT
576576
depends on SPARC64
577577
default y
578578
select COMPAT_BINFMT_ELF
579+
select ARCH_WANT_OLD_COMPAT_IPC
579580

580581
config SYSVIPC_COMPAT
581582
bool

‎arch/tile/include/asm/compat.h‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,6 @@ long compat_sys_fallocate(int fd, int mode,
242242
long compat_sys_sched_rr_get_interval(compat_pid_t pid,
243243
struct compat_timespec __user *interval);
244244

245-
/* Versions of compat functions that differ from generic Linux. */
246-
struct compat_msgbuf;
247-
long tile_compat_sys_msgsnd(int msqid,
248-
struct compat_msgbuf __user *msgp,
249-
size_t msgsz, int msgflg);
250-
long tile_compat_sys_msgrcv(int msqid,
251-
struct compat_msgbuf __user *msgp,
252-
size_t msgsz, long msgtyp, int msgflg);
253-
long tile_compat_sys_ptrace(compat_long_t request, compat_long_t pid,
254-
compat_long_t addr, compat_long_t data);
255-
256245
/* Tilera Linux syscalls that don't have "compat" versions. */
257246
#define compat_sys_flush_cache sys_flush_cache
258247

‎arch/tile/kernel/compat.c‎

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define __SYSCALL_COMPAT
1717

1818
#include <linux/compat.h>
19-
#include <linux/msg.h>
2019
#include <linux/syscalls.h>
2120
#include <linux/kdev_t.h>
2221
#include <linux/fs.h>
@@ -95,52 +94,10 @@ long compat_sys_sched_rr_get_interval(compat_pid_t pid,
9594
return ret;
9695
}
9796

98-
/*
99-
* The usual compat_sys_msgsnd() and _msgrcv() seem to be assuming
100-
* some different calling convention than our normal 32-bit tile code.
101-
*/
102-
103-
/* Already defined in ipc/compat.c, but we need it here. */
104-
struct compat_msgbuf {
105-
compat_long_t mtype;
106-
char mtext[1];
107-
};
108-
109-
long tile_compat_sys_msgsnd(int msqid,
110-
struct compat_msgbuf __user *msgp,
111-
size_t msgsz, int msgflg)
112-
{
113-
compat_long_t mtype;
114-
115-
if (get_user(mtype, &msgp->mtype))
116-
return -EFAULT;
117-
return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
118-
}
119-
120-
long tile_compat_sys_msgrcv(int msqid,
121-
struct compat_msgbuf __user *msgp,
122-
size_t msgsz, long msgtyp, int msgflg)
123-
{
124-
long err, mtype;
125-
126-
err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
127-
if (err < 0)
128-
goto out;
129-
130-
if (put_user(mtype, &msgp->mtype))
131-
err = -EFAULT;
132-
out:
133-
return err;
134-
}
135-
13697
/* Provide the compat syscall number to call mapping. */
13798
#undef __SYSCALL
13899
#define __SYSCALL(nr, call) [nr] = (call),
139100

140-
/* The generic versions of these don't work for Tile. */
141-
#define compat_sys_msgrcv tile_compat_sys_msgrcv
142-
#define compat_sys_msgsnd tile_compat_sys_msgsnd
143-
144101
/* See comments in sys.c */
145102
#define compat_sys_fadvise64_64 sys32_fadvise64_64
146103
#define compat_sys_readahead sys32_readahead

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,7 @@ config IA32_AOUT
21782178
config COMPAT
21792179
def_bool y
21802180
depends on IA32_EMULATION
2181+
select ARCH_WANT_OLD_COMPAT_IPC
21812182

21822183
config COMPAT_FOR_U64_ALIGNMENT
21832184
def_bool COMPAT

‎include/linux/compat.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct compat_sysinfo;
224224
struct compat_sysctl_args;
225225
struct compat_kexec_segment;
226226
struct compat_mq_attr;
227+
struct compat_msgbuf;
227228

228229
extern void compat_exit_robust_list(struct task_struct *curr);
229230

@@ -234,13 +235,22 @@ asmlinkage long
234235
compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
235236
compat_size_t __user *len_ptr);
236237

238+
#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
237239
long compat_sys_semctl(int first, int second, int third, void __user *uptr);
238240
long compat_sys_msgsnd(int first, int second, int third, void __user *uptr);
239241
long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
240242
int version, void __user *uptr);
241-
long compat_sys_msgctl(int first, int second, void __user *uptr);
242243
long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
243244
void __user *uptr);
245+
#else
246+
long compat_sys_semctl(int semid, int semnum, int cmd, int arg);
247+
long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
248+
size_t msgsz, int msgflg);
249+
long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
250+
size_t msgsz, long msgtyp, int msgflg);
251+
long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg);
252+
#endif
253+
long compat_sys_msgctl(int first, int second, void __user *uptr);
244254
long compat_sys_shmctl(int first, int second, void __user *uptr);
245255
long compat_sys_semtimedop(int semid, struct sembuf __user *tsems,
246256
unsigned nsems, const struct compat_timespec __user *timeout);

‎ipc/compat.c‎

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/msg.h>
2828
#include <linux/shm.h>
2929
#include <linux/syscalls.h>
30+
#include <linux/ptrace.h>
3031

3132
#include <linux/mutex.h>
3233
#include <asm/uaccess.h>
@@ -117,6 +118,7 @@ extern int sem_ctls[];
117118

118119
static inline int compat_ipc_parse_version(int *cmd)
119120
{
121+
#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
120122
int version = *cmd & IPC_64;
121123

122124
/* this is tricky: architectures that have support for the old
@@ -128,6 +130,10 @@ static inline int compat_ipc_parse_version(int *cmd)
128130
*cmd &= ~IPC_64;
129131
#endif
130132
return version;
133+
#else
134+
/* With the asm-generic APIs, we always use the 64-bit versions. */
135+
return IPC_64;
136+
#endif
131137
}
132138

133139
static inline int __get_compat_ipc64_perm(struct ipc64_perm *p64,
@@ -232,21 +238,16 @@ static inline int put_compat_semid_ds(struct semid64_ds *s,
232238
return err;
233239
}
234240

235-
long compat_sys_semctl(int first, int second, int third, void __user *uptr)
241+
static long do_compat_semctl(int first, int second, int third, u32 pad)
236242
{
237243
union semun fourth;
238-
u32 pad;
239244
int err, err2;
240245
struct semid64_ds s64;
241246
struct semid64_ds __user *up64;
242247
int version = compat_ipc_parse_version(&third);
243248

244249
memset(&s64, 0, sizeof(s64));
245250

246-
if (!uptr)
247-
return -EINVAL;
248-
if (get_user(pad, (u32 __user *) uptr))
249-
return -EFAULT;
250251
if ((third & (~IPC_64)) == SETVAL)
251252
fourth.val = (int) pad;
252253
else
@@ -305,6 +306,18 @@ long compat_sys_semctl(int first, int second, int third, void __user *uptr)
305306
return err;
306307
}
307308

309+
#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
310+
long compat_sys_semctl(int first, int second, int third, void __user *uptr)
311+
{
312+
u32 pad;
313+
314+
if (!uptr)
315+
return -EINVAL;
316+
if (get_user(pad, (u32 __user *) uptr))
317+
return -EFAULT;
318+
return do_compat_semctl(first, second, third, pad);
319+
}
320+
308321
long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
309322
{
310323
struct compat_msgbuf __user *up = uptr;
@@ -353,6 +366,37 @@ long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
353366
out:
354367
return err;
355368
}
369+
#else
370+
long compat_sys_semctl(int semid, int semnum, int cmd, int arg)
371+
{
372+
return do_compat_semctl(semid, semnum, cmd, arg);
373+
}
374+
375+
long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
376+
size_t msgsz, int msgflg)
377+
{
378+
compat_long_t mtype;
379+
380+
if (get_user(mtype, &msgp->mtype))
381+
return -EFAULT;
382+
return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
383+
}
384+
385+
long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
386+
size_t msgsz, long msgtyp, int msgflg)
387+
{
388+
long err, mtype;
389+
390+
err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
391+
if (err < 0)
392+
goto out;
393+
394+
if (put_user(mtype, &msgp->mtype))
395+
err = -EFAULT;
396+
out:
397+
return err;
398+
}
399+
#endif
356400

357401
static inline int get_compat_msqid64(struct msqid64_ds *m64,
358402
struct compat_msqid64_ds __user *up64)
@@ -470,6 +514,7 @@ long compat_sys_msgctl(int first, int second, void __user *uptr)
470514
return err;
471515
}
472516

517+
#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
473518
long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
474519
void __user *uptr)
475520
{
@@ -485,6 +530,19 @@ long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
485530
uaddr = compat_ptr(third);
486531
return put_user(raddr, uaddr);
487532
}
533+
#else
534+
long compat_sys_shmat(int shmid, compat_uptr_t shmaddr, int shmflg)
535+
{
536+
unsigned long ret;
537+
long err;
538+
539+
err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret);
540+
if (err)
541+
return err;
542+
force_successful_syscall_return();
543+
return (long)ret;
544+
}
545+
#endif
488546

489547
static inline int get_compat_shmid64_ds(struct shmid64_ds *s64,
490548
struct compat_shmid64_ds __user *up64)

0 commit comments

Comments
 (0)