Skip to contentSkip to conversation

posix message queues

DISCUSSION Andrew Morton 4 participants 10 messages Apr 7, 2004 – Apr 10, 2004

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Manfred, if/when this gets merged up the long-suffering arch maintainers might appreciate a little test app to exercise the syscalls which must be added. Do you have something suitable at hand?

Also, before we go too far the 64-bit guys may like to comment on the
syscall interface. For example, this:

struct mq_attr {
long mq_flags; /* message queue flags */
long mq_maxmsg; /* maximum number of messages */
long mq_msgsize; /* maximum message size */
long mq_curmsgs; /* number of messages currently queued */
long __reserved[4]; /* ignored for input, zeroed for output */
};

looks like it will require emulation for 32-bit apps. But if we were to make these __u32 perhaps that could be avoided?

The patches are at

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-01-codemove.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-02-syscalls.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-03-core-update.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-03-core.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-04-linuxext-poll.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-05-linuxext-mount.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-security-fix.patch ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.5/2.6.5-mc2/broken-out/mq-update-01.patch

#1 lore raw
Quoted 5 lines · Andrew Morton

Manfred, if/when this gets merged up the long-suffering arch maintainers might appreciate a little test app to exercise the syscalls which must be added. Do you have something suitable at hand?

I have a few separate apps, I'll merge them into one app and post it to lkml. Probably tomorrow or Friday.

Quoted 12 lines

Also, before we go too far the 64-bit guys may like to comment on the
syscall interface. For example, this:

struct mq_attr {
long mq_flags; /* message queue flags */
long mq_maxmsg; /* maximum number of messages */
long mq_msgsize; /* maximum message size */
long mq_curmsgs; /* number of messages currently queued */
long __reserved[4]; /* ignored for input, zeroed for output */
};

Quoted 4 lines

looks like it will require emulation for 32-bit apps. But if we were to make these __u32 perhaps that could be avoided?

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

--
Manfred

maintainer
#2 lore raw
Quoted 9 lines · you
Quoted 4 lines

looks like it will require emulation for 32-bit apps.  But if we were to make these __u32 perhaps that could be avoided?

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

Coincidentally, I have just finished writing handlers for them. I have tested the code with the open posix test suite and found the same four failures for both 64-bit and compat mode, most tests pass. The patch is against -mc1, but I guess it also applies to the other trees around.

What worries me more than mq_attr compatibility is the conversion of struct sigevent, which might turn out really hard when more fields in there are used. AFAICS, the only other part in the kernel ABI is sys_timer_create(), so maybe it's not too late to deprecate the current structure and create a structure that can be used properly for compat syscalls.

	Arnd <><

http://www.arndb.de/patches/linux/2.6.5-mc1/compat_mq.diff

include/linux/compat.h      |   16 +++
include/linux/mqueue.h      |    4 
include/linux/posix_types.h |    1 
include/linux/syscalls.h    |    1 
include/linux/types.h       |    1 
ipc/Makefile                |    3 
ipc/compat_mq.c             |  185 ++++++++++++++++++++++++++++++++++++++++++++
kernel/sys.c                |    5 +
  8 files changed, 212 insertions(+), 4 deletions(-)
include/linux/compat.h +16 -0
diff -Nru a/include/linux/compat.h b/include/linux/compat.h
@@ -90,6 +90,22 @@
 	compat_uptr_t	sival_ptr;
 } compat_sigval_t;
 
+typedef struct compat_sigevent {
+	compat_sigval_t sigev_value;
+	compat_int_t sigev_signo;
+	compat_int_t sigev_notify;
+	union {
+		compat_int_t _pad[SIGEV_PAD_SIZE];
+		compat_int_t _tid;
+
+		struct {
+			compat_uptr_t _function;
+			compat_uptr_t _attribute;
+		} _sigev_thread;
+	} _sigev_un;
+} compat_sigevent_t;
+
+
 long compat_sys_semctl(int first, int second, int third, void __user *uptr);
 long compat_sys_msgsnd(int first, int second, int third, void __user *uptr);
 long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
include/linux/mqueue.h +2 -2
diff -Nru a/include/linux/mqueue.h b/include/linux/mqueue.h
@@ -18,9 +18,9 @@
 #ifndef _LINUX_MQUEUE_H
 #define _LINUX_MQUEUE_H
 
-#define MQ_PRIO_MAX 	32768
+#include <linux/types.h>
 
-typedef int mqd_t;
+#define MQ_PRIO_MAX 	32768
 
 struct mq_attr {
 	long	mq_flags;	/* message queue flags			*/
include/linux/posix_types.h +1 -0
diff -Nru a/include/linux/posix_types.h b/include/linux/posix_types.h
@@ -42,6 +42,7 @@
 
 /* Type of a SYSV IPC key.  */
 typedef int __kernel_key_t;
+typedef int __kernel_mqd_t;
 
 #include <asm/posix_types.h>
 
include/linux/syscalls.h +0 -1
diff -Nru a/include/linux/syscalls.h b/include/linux/syscalls.h
@@ -48,7 +48,6 @@
 struct timezone;
 struct tms;
 struct utimbuf;
-typedef int mqd_t;
 struct mq_attr;
 
 #include <linux/config.h>
include/linux/types.h +1 -0
diff -Nru a/include/linux/types.h b/include/linux/types.h
@@ -31,6 +31,7 @@
 typedef __kernel_suseconds_t	suseconds_t;
 typedef __kernel_timer_t	timer_t;
 typedef __kernel_clockid_t	clockid_t;
+typedef __kernel_mqd_t		mqd_t;
 
 #ifdef __KERNEL__
 typedef __kernel_uid32_t	uid_t;
ipc/Makefile +2 -1
diff -Nru a/ipc/Makefile b/ipc/Makefile
@@ -4,5 +4,6 @@
 
 obj-$(CONFIG_SYSVIPC_COMPAT) += compat.o
 obj-$(CONFIG_SYSVIPC) += util.o msgutil.o msg.o sem.o shm.o
-obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o
+obj_mq-$(CONFIG_COMPAT) += compat_mq.o
+obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o $(obj_mq-y)
 
ipc/compat_mq.c new file +185 -0

185 lines not on this page · raw message

kernel/sys.c +5 -0
diff -Nru a/kernel/sys.c b/kernel/sys.c
@@ -266,6 +266,11 @@
 cond_syscall(sys_mq_timedreceive)
 cond_syscall(sys_mq_notify)
 cond_syscall(sys_mq_getsetattr)
+cond_syscall(compat_sys_mq_open)
+cond_syscall(compat_sys_mq_timedsend)
+cond_syscall(compat_sys_mq_timedreceive)
+cond_syscall(compat_sys_mq_notify)
+cond_syscall(compat_sys_mq_getsetattr)
 
 /* arch-specific weak syscall entries */
 cond_syscall(sys_pciconfig_read)
#3 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 6 lines

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

Coincidentally, I have just finished writing handlers for them.

In file included from kernel/sys.c:8:
include/linux/compat.h:98: error: `SIGEV_PAD_SIZE' undeclared here (not in a function)

---

 25-akpm/include/linux/compat.h |    1 +
 1 files changed, 1 insertion(+)
include/linux/compat.h renamed from include/linux/compat.h~compat_mq-fix +1 -0
diff -puN include/linux/compat.h~compat_mq-fix include/linux/compat.h
@@ -13,6 +13,7 @@
 #include <linux/sem.h>
 
 #include <asm/compat.h>
+#include <asm/siginfo.h>
 
 #define compat_jiffies_to_clock_t(x)	\
 		(((unsigned long)(x) * COMPAT_USER_HZ) / HZ)

_

#4 lore raw
In reply to Arnd Bergmann
Quoted 4 lines · Arnd Bergmann

What worries me more than mq_attr compatibility is the conversion of struct sigevent, which might turn out really hard when more fields in there are used.

What is the problem for the wrappers with sigevent?

Quoted 6 lines
ipc/compat_mq.c new file +5 -0
+
+static int get_compat_sigevent(struct sigevent *event,
+		const struct compat_sigevent __user *u_event)
+{
+	if (verify_area(VERIFY_READ, u_event, sizeof(*u_event)))

ipc/compat_mq.c line 136

Quoted 10 lines
ipc/compat_mq.c new file +10 -0
+		return -EFAULT;
+
+	return __get_user(event->sigev_value.sival_int,
+			  &u_event->sigev_value.sival_int)
+	     | __get_user(event->sigev_signo, &u_event->sigev_signo)
+	     | __get_user(event->sigev_notify, &u_event->sigev_notify)
+	     | __get_user(event->sigev_notify_thread_id,
+			  &u_event->sigev_notify_thread_id);
+}
+

The dangerous case for the wrappers is mq_notify for SIGEV_THREAD: The kernel will read (with copy_from_user()) from the pointer u_event->sigev_value.sival_ptr. Fixed 32-byte buffer. The data is opaque - the kernel copies it back to the same process without any interpretation. Would that work on all archs?

--
Manfred

#5 lore raw
In reply to Arnd Bergmann
Quoted 8 lines · Arnd Bergmann
Quoted 6 lines

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

Coincidentally, I have just finished writing handlers for them.

It broke ppc64. Please check other architectures for the same pattern.

In file included from arch/ppc64/kernel/signal.c:29:
include/asm/ppc32.h:144: redefinition of struct compat_sigevent' include/asm/ppc32.h:156: redefinition of compat_sigevent_t' include/linux/compat.h:107: `compat_sigevent_t' previously declared here

---

 25-power4-akpm/include/asm-ppc64/ppc32.h |   14 --------------
 1 files changed, 14 deletions(-)
include/asm-ppc64/ppc32.h renamed from include/asm-ppc64/ppc32.h~compat_mq-ppc-fix +0 -14
diff -puN include/asm-ppc64/ppc32.h~compat_mq-ppc-fix include/asm-ppc64/ppc32.h
@@ -141,20 +141,6 @@ struct ucontext32 { 
 	struct mcontext32	uc_mcontext;
 };
 
-typedef struct compat_sigevent {
-	compat_sigval_t sigev_value;
-	int sigev_signo;
-	int sigev_notify;
-	union {
-		int _pad[SIGEV_PAD_SIZE];
-		int _tid;
-		struct {
-			compat_uptr_t _function;
-			compat_uptr_t _attribute;
-		} _sigev_thread;
-	} _sigev_un;
-} compat_sigevent_t;
-
 struct ipc_kludge_32 {
 	unsigned int msgp;
 	int msgtyp;

_

#6 lore raw
In reply to Manfred Spraul
Quoted 5 lines · On Wed, 07 Apr 2004 21:15:44 +0200 Manfred Spraul <[email protected]>…

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

We need to support this stuff when running 32-bit apps, you cannot avoid this Manfred. So we'll need kernel compat layer code to translate these structures containing 'long'. Please code up the necessary compat.c code for this posix message queues feature.

Thanks.

#7 lore raw
Quoted 5 lines · David S. Miller

On Wed, 07 Apr 2004 21:15:44 +0200
Manfred Spraul <[email protected]> wrote:

Quoted 7 lines
Quoted 3 lines

No. The unix spec mandates 'long' for the final C-library api. Using u32 would mean that glibc would have to add an emulation layer in user space for 64-bit archs. In the long run, 64-bit kernels running 64-bit apps will be the common case. I don't want to add an emulation layer to the common case.

We need to support this stuff when running 32-bit apps, you cannot avoid this Manfred.

Of course. But the compat stuff is only used if a 32-bit app runs on a 64-bit kernel. If I'd use u32 for the kernel interface, then a compat wrappers would be necessary if a 64-bit app runs on a 64-bit kernel, and that would be the wrong thing.

Quoted 5 lines

So we'll need kernel compat layer code to
translate these structures containing 'long'. Please code up the necessary compat.c code for this posix message queues feature.

Arndt did 90% of the coding, it's already in 2.6.5-mm3. The only missing part was the SIGEV_THREAD handling, I've attached a patch that adds this, together a small user space test app.

--
Manfred

/*
* Test app for Linux Kernel posix message queues.
*
* Copyright (C) 2004 Manfred Spraul
*
* Redistribution of this file is permitted under the terms of the GNU
* Public License (GPL)
* $Header: /home/manfred/cvs-tree/mq/mq_test.c,v 1.3 2004/04/10 11:17:31 manfred Exp $ */
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>

/*
* Kernel-interface based on the Mqueue library:
*
* Copyright (C) 2003 Krzysztof Benedyczak & Michal Wronski

This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

/****************** implementation *****************/

#define __NR_mq_open 277

#define __NR_mq_unlink (__NR_mq_open+1)
#define __NR_mq_timedsend (__NR_mq_open+2)
#define __NR_mq_timedreceive (__NR_mq_open+3)
#define __NR_mq_notify (__NR_mq_open+4)
#define __NR_mq_getsetattr (__NR_mq_open+5)

#define MQ_PRIO_MAX 32768

typedef int mqd_t;

struct mq_attr {
long mq_flags; /* message queue flags */
long mq_maxmsg; /* maximum number of messages */
long mq_msgsize; /* maximum message size */
long mq_curmsgs; /* number of messages currently queued */
long __reserved[4]; /* unused */
};

#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>

mqd_t mq_open(const char *name, int oflag, /* mode_t mode, struct mq_attr *attr */ ...);
int mq_close(mqd_t mqdes);
int mq_unlink(const char *name);
int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned int msg_prio);
int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout);
ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio);
ssize_t mq_timedreceive(mqd_t mqdes, char *__restrict msg_ptr, size_t msg_len, unsigned int *__restrict msg_prio, const struct timespec *__restrict abs_timeout); int mq_notify(mqd_t mqdes, const struct sigevent *notification);
int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);
int mq_setattr(mqd_t mqdes, const struct mq_attr *__restrict mqstat, struct mq_attr *__restrict omqstat);

/*
* kernel interfaces. We use glibc's syscall(3) instead of the macros
* _syscall1, _syscall2, etc, as the macros generate compilation errors
* when mqueue.c is built as a dynamic shared library.
*/
static inline mqd_t __mq_open(const char *name,
int oflag, mode_t mode, struct mq_attr* attr)

{
	return syscall(__NR_mq_open, name, oflag, mode, attr);
}

static inline int __mq_unlink(const char *name)

{
	return syscall(__NR_mq_unlink, name);
}

static inline int __mq_notify(mqd_t mqdes, const struct sigevent *notification)

{
	return syscall(__NR_mq_notify, mqdes, notification);
}

static inline int __mq_getsetattr(mqd_t mqdes, const struct mq_attr *mqstat, struct mq_attr *omqstat)

{
	return syscall(__NR_mq_getsetattr, mqdes, mqstat, omqstat);
}

static inline int __mq_timedsend(mqd_t mqdes, const char *msg_ptr,
size_t msg_len, unsigned int msg_prio, const struct timespec *abs_timeout)

{
	return syscall(__NR_mq_timedsend, mqdes, msg_ptr, msg_len,
		msg_prio, abs_timeout);
}

static inline ssize_t __mq_timedreceive(mqd_t mqdes, char *msg_ptr,
size_t msg_len, unsigned int *msg_prio, const struct timespec *abs_timeout)

{
	return syscall(__NR_mq_timedreceive, mqdes, msg_ptr, msg_len,
		msg_prio, abs_timeout);
}

static inline int is_valid_path(const char *name)

{
	if (name[0] != '/') {
		errno = EINVAL;
		return -1;
	}
	return 0;
}

/*
* application-visible wrappers around the kernel interfaces */

mqd_t mq_open(const char *name, int oflag, ...)

{
	unsigned long 	mode;
	struct mq_attr 	*attr;
	va_list 	ap;

	va_start(ap, oflag);
	mode = va_arg(ap, unsigned long);
	attr = va_arg(ap, struct mq_attr *);
	va_end(ap);

	if (is_valid_path(name) < 0)
		return (mqd_t)-1;

	/* omit leading slash */
	return __mq_open(name + 1, oflag, mode, attr);
}

int mq_close(mqd_t mqdes)

{
	return close(mqdes);
}

int mq_unlink(const char *name)

{
	if (is_valid_path(name) < 0)
		return -1;
	
	return __mq_unlink(name+1);
}

int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned int msg_prio)

{
	return __mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, NULL);
}

ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned int *msg_prio)

{
	return __mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio, NULL);
}

int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned int msg_prio, const struct timespec *abs_timeout)

{
	return __mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout);
}

ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned int *msg_prio, const struct timespec *abs_timeout)

{
	return __mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio,
		abs_timeout);
}

int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)

{
	return __mq_getsetattr(mqdes, NULL, mqstat);
}

int mq_setattr( mqd_t mqdes, const struct mq_attr *mqstat,
struct mq_attr *omqstat)

{
	return __mq_getsetattr(mqdes, mqstat, omqstat);
}

int mq_notify(mqd_t mqdes, const struct sigevent *notification)

{
	return __mq_notify(mqdes, notification);
}

int got_signal = 0;
void usr1_handler(int unused)

{
	printf("usr1 signal caught.\n");
	got_signal = 1;
}

char buffer[131072];

int main(int argc, char **argv)
{

static const char qname[] = "/test_123";
        int err;
        mqd_t m, m2;
	struct mq_attr ma;

	printf("Test 1: mq_open()\n");
	m = mq_open(qname, O_CREAT|O_RDWR, S_IRUSR | S_IWUSR, NULL);
	if (m == -1) {
	        printf("mq_open() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 2: check exclusive open()\n");
	m2 = mq_open(qname, O_EXCL|O_CREAT|O_RDWR, S_IRUSR | S_IWUSR, NULL);
	if (m2 != -1 || errno != EEXIST) {
	        printf("mq_open() succeeded or wrong errno (%d).\n", errno);
		return 1;
	}

	printf("Test 3: close handle\n");
	err = mq_close(m);

	if (err) {
		printf("mq_close() failed.\n");
	}

	printf("Test 4: mq_unlink\n");
	err = mq_unlink(qname);
	if (err) {
	        printf("mq_unlink() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 4: mq_unlink on nonexisting file\n");
	err = mq_unlink(qname);
	if (err != -1 || errno != ENOENT) {
	        printf("mq_unlink() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 5: mq_open()\n");
	m = mq_open(qname, O_CREAT|O_EXCL|O_RDWR, S_IRUSR | S_IWUSR, NULL);
	if (m == -1) {
	        printf("mq_open() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 6: mq_getattr()\n");
	err = mq_getattr(m, &ma);
	if (err || ma.mq_curmsgs != 0) {
	        printf("mq_getattr() failed,  errno %d, curmsg %ld.\n", errno, ma.mq_curmsgs);
		return 1;
	}

	printf("Test 7: setup signal based notify().\n");

	signal (SIGUSR1,usr1_handler);

	struct sigevent event;
	event.sigev_notify = SIGEV_SIGNAL;
	event.sigev_signo = SIGUSR1;
	err = mq_notify(m, &event);
	if (err) {
	        printf("mq_notify() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 8: mq_send()\n");
	err = mq_send(m, qname, strlen(qname), 1);
	if (err) {
	        printf("mq_send() failed,  errno %d.\n", errno);
		return 1;
	}
	if (!got_signal) {
		printf("No signal received.\n");
		return 1;
	}

	printf("Test 9: mq_getattr() with one pending message\n");
	err = mq_getattr(m, &ma);
	if (err || ma.mq_curmsgs != 1) {
	        printf("mq_getattr() failed,  errno %d, curmsg %ld.\n", errno, ma.mq_curmsgs);
		return 1;
	}

	printf("Test 10: mq_receive()\n");
	err = mq_receive(m, buffer, sizeof(buffer), NULL);
	if (err < 0) {
	        printf("mq_receive() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 11: data comparison\n");
	if (strcmp(qname, buffer)) {
		printf("Data mismatch!!!.\n");
		return 1;
	}

	printf("Test 12: request notification cookie.\n");
	{
		int fd, i;
		char inc[32], outc[64];


		fd = socket(PF_NETLINK, SOCK_RAW, 0);
		if (fd == -1) {
			printf("opening netlink socket failed, errno %d.\n", errno);
			return 1;
		}

		for (i=0;i<32;i++)
			inc[i] = 64+2*i;

		event.sigev_notify = SIGEV_THREAD;
		event.sigev_signo = fd;
		event.sigev_value.sival_ptr = inc;
		err = mq_notify(m, &event);
		if (err < 0) {
			 printf("mq_notify() failed,  errno %d.\n", errno);
			return 1;
		}
		err = mq_send(m, qname, strlen(qname), 1);
		if (err) {
		        printf("mq_send() failed,  errno %d.\n", errno);
			return 1;
		}
		err = recv(fd, outc, sizeof(outc), MSG_NOSIGNAL|MSG_DONTWAIT);
		if (err != 32) {
			printf("recv unexpected result %d, errno %d.\n", err, errno);
			return 1;
		}
		for (i=0;i<31;i++) {
			if (outc[i] != 64+2*i) {
				printf("Data mismatch in cookie at offset %d: %d.\n",
						i, outc[i]);
				return 1;
			}
		}
		if (outc[31] != 1) {
			printf("state mismatch in cookie: got %d.\n", outc[31]);
		}
	}

	printf("Test 13: another mq_unlink\n");
	err = mq_unlink(qname);
	if (err) {
	        printf("mq_unlink() failed,  errno %d.\n", errno);
		return 1;
	}

	printf("Test 14: mq_close()\n");
	err = mq_close(m);
	if (err) {
		printf("mq_close() failed, errno %d.\n", errno);
		return 1;
	}
	return 0;
}
// $Header$
// Kernel Version:
//  VERSION = 2
//  PATCHLEVEL = 6
//  SUBLEVEL = 5
//  EXTRAVERSION = -mm3
ipc/compat_mq.c +9 -0
@@ -139,6 +139,7 @@
 {
 	mm_segment_t oldfs;
 	struct sigevent notification;
+	char cookie[NOTIFY_COOKIE_LEN];
 	long ret;
 
 	if (!u_notification)
@@ -147,6 +148,14 @@
 	if (get_compat_sigevent(&notification, u_notification))
 		return -EFAULT;
 
+	if (notification.sigev_notify == SIGEV_THREAD) {
+		if (copy_from_user(cookie, u_notification.sigev_value.sival_ptr,
+					NOTIFY_COOKIE_LEN)) {
+			return -EFAULT;
+		}
+		notification.sigev_value.sival_ptr = cookie;
+	}
+
 	oldfs = get_fs();
 	set_fs(KERNEL_DS);
 	ret = sys_mq_notify(mqdes, &notification);
#8 lore raw

Review

Quoted 4 lines · Manfred Spraul
(quoted diff) +2 -0
+if (notification.sigev_notify == SIGEV_THREAD) {
+	if (copy_from_user(cookie, u_notification.sigev_value.sival_ptr,

notification, not u_notification: sival_ptr is a union on sival_int, and sival_int was copied to kernel space by get_compat_sigevent. Updated patch attached, sorry.

--
Manfred

// $Header$
// Kernel Version:

//  VERSION = 2
//  PATCHLEVEL = 6
//  SUBLEVEL = 5
//  EXTRAVERSION = -mm3
ipc/compat_mq.c +9 -0
@@ -139,6 +139,7 @@
 {
 	mm_segment_t oldfs;
 	struct sigevent notification;
+	char cookie[NOTIFY_COOKIE_LEN];
 	long ret;
 
 	if (!u_notification)
@@ -147,6 +148,14 @@
 	if (get_compat_sigevent(&notification, u_notification))
 		return -EFAULT;
 
+	if (notification.sigev_notify == SIGEV_THREAD) {
+		if (copy_from_user(cookie, notification.sigev_value.sival_ptr,
+					NOTIFY_COOKIE_LEN)) {
+			return -EFAULT;
+		}
+		notification.sigev_value.sival_ptr = cookie;
+	}
+
 	oldfs = get_fs();
 	set_fs(KERNEL_DS);
 	ret = sys_mq_notify(mqdes, &notification);
maintainer
#9 lore raw
Quoted 9 lines · Manfred Spraul
Quoted 4 lines · Manfred Spraul

+if (notification.sigev_notify == SIGEV_THREAD) {
+      if (copy_from_user(cookie, u_notification.sigev_value.sival_ptr,

notification, not u_notification: sival_ptr is a union on sival_int, and sival_int was copied to kernel space by get_compat_sigevent. Updated patch attached, sorry.

I'm afraid that is still incorrect, at least on big-endian machines:

Quoted 11 lines
(quoted diff) +0 -0
@@ -147,6 +148,14 @@
         if (get_compat_sigevent(&notification, u_notification))
                 return -EFAULT;


+       if (notification.sigev_notify == SIGEV_THREAD) {
+               if (copy_from_user(cookie, notification.sigev_value.sival_ptr, +                                       NOTIFY_COOKIE_LEN)) {
+                       return -EFAULT;
+               }
+               notification.sigev_value.sival_ptr = cookie;
+       }

sival_ptr is not valid when passed through get_compat_sigevent, because the 32 bit pointer might be copied to the upper half of the kernel pointer. Even worse, the infamous s390 31 bit pointer conversion is missing. This patch fixes that problem and two others reported by Jakub.

Quoted 1 line
Quoted 1 line

Arndt did 90% of the coding, it's already in 2.6.5-mm3.

BTW, my name is 'Arnd', not 'Arndt'. I hope we can stop this now before this becomes some 'Russel' vs. 'Russell' problem ;-)

	Arnd <><

--
[PATCH] More fixups for compat_mq
- handle SIGEV_THREAD
- don't try to convert u_attr in sys_mq_open if !O_CREAT
- handle __SI_MESGQ in copy_siginfo_to_user32 (still missing for ppc64 and parisc)
===== ipc/compat_mq.c 1.1 vs edited =====

ipc/compat_mq.c +12 -1
@@ -55,7 +55,7 @@
 	char *name;
 	long ret;
 
-	if (!u_attr)
+	if ((oflag & O_CREAT) == 0 || !u_attr)
 		return sys_mq_open(u_name, oflag, mode, 0);
 
 	if (get_compat_mq_attr(&attr, u_attr))
@@ -139,6 +139,8 @@
 {
 	mm_segment_t oldfs;
 	struct sigevent notification;
+	char cookie[NOTIFY_COOKIE_LEN];
+	compat_uptr_t u_cookie;
 	long ret;
 
 	if (!u_notification)
@@ -146,6 +148,15 @@
 
 	if (get_compat_sigevent(&notification, u_notification))
 		return -EFAULT;
+
+	if (notification.sigev_notify == SIGEV_THREAD) {
+		u_cookie = (compat_uptr_t)notification.sigev_value.sival_int;
+		if (copy_from_user(cookie, compat_ptr(u_cookie),
+						NOTIFY_COOKIE_LEN)) {
+			return -EFAULT;
+		}
+		notification.sigev_value.sival_ptr = cookie;
+	}
 
 	oldfs = get_fs();
 	set_fs(KERNEL_DS);

===== arch/ia64/ia32/ia32_signal.c 1.24 vs edited =====

arch/ia64/ia32/ia32_signal.c +6 -1
@@ -114,7 +114,12 @@
 			err |= __get_user(to->si_band, &from->si_band);
 			err |= __get_user(to->si_fd, &from->si_fd);
 			break;
-			/* case __SI_RT: This is not generated by the kernel as of now.  */
+		      case __SI_RT: /* This is not generated by the kernel as of now.  */
+		      case __SI_MESGQ:
+			err |= __get_user(to->si_pid, &from->si_pid);
+			err |= __get_user(to->si_uid, &from->si_uid);
+			err |= __get_user(to->si_int, &from->si_int);
+			break;
 		}
 	}
 	return err;

===== arch/mips/kernel/signal32.c 1.13 vs edited =====

arch/mips/kernel/signal32.c +6 -1
@@ -358,7 +358,12 @@
 			err |= __put_user(from->si_band, &to->si_band);
 			err |= __put_user(from->si_fd, &to->si_fd);
 			break;
-		/* case __SI_RT: This is not generated by the kernel as of now.  */
+		case __SI_RT: /* This is not generated by the kernel as of now.  */
+		case __SI_MESGQ:
+			err |= __put_user(from->si_pid, &to->si_pid);
+			err |= __put_user(from->si_uid, &to->si_uid);
+			err |= __put_user(from->si_int, &to->si_int);
+			break;
 		}
 	}
 	return err;

===== arch/s390/kernel/compat_signal.c 1.7 vs edited =====

arch/s390/kernel/compat_signal.c +4 -1
@@ -74,6 +74,10 @@
 		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
 	else {
 		switch (from->si_code >> 16) {
+		case __SI_RT: /* This is not generated by the kernel as of now.  */
+		case __SI_MESGQ:
+			err |= __put_user(from->si_int, &to->si_int);
+			/* fallthrough */
 		case __SI_KILL >> 16:
 			err |= __put_user(from->si_pid, &to->si_pid);
 			err |= __put_user(from->si_uid, &to->si_uid);
@@ -96,7 +100,6 @@
 			break;
 		default:
 			break;
-		/* case __SI_RT: This is not generated by the kernel as of now.  */
 		}
 	}
 	return err;

===== arch/sparc64/kernel/signal32.c 1.32 vs edited =====

arch/sparc64/kernel/signal32.c +6 -1
@@ -129,7 +129,12 @@
 			err |= __put_user(from->si_trapno, &to->si_trapno);
 			err |= __put_user((long)from->si_addr, &to->si_addr);
 			break;
-		/* case __SI_RT: This is not generated by the kernel as of now.  */
+		case __SI_RT: /* This is not generated by the kernel as of now.  */
+		case __SI_MESGQ:
+			err |= __put_user(from->si_pid, &to->si_pid);
+			err |= __put_user(from->si_uid, &to->si_uid);
+			err |= __put_user(from->si_int, &to->si_int);
+			break;
 		}
 	}
 	return err;

===== arch/x86_64/ia32/ia32_signal.c 1.19 vs edited =====

arch/x86_64/ia32/ia32_signal.c +5 -1
@@ -85,7 +85,11 @@
 			err |= __put_user(from->si_overrun, &to->si_overrun); 
 			err |= __put_user((u32)(u64)from->si_ptr, &to->si_ptr);
 			break;
-		/* case __SI_RT: This is not generated by the kernel as of now.  */
+		case __SI_RT: /* This is not generated by the kernel as of now.  */
+		case __SI_MESGQ:
+			err |= __put_user(from->si_uid, &to->si_uid);
+			err |= __put_user(from->si_int, &to->si_int);
+			break;
 		}
 	}
 	return err;