Skip to contentSkip to conversation

um: fix address-of CMSG_DATA() rvalue in stub

PATCH New Marcel W. Wysocki 1 participant 2 messages Feb 15

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

PATCH 1/2 um: fix address-of CMSG_DATA() rvalue in stub

The UML stub takes the address of CMSG_DATA(fd_msg):

    fd_map = (void *)&CMSG_DATA(fd_msg);

CMSG_DATA() is specified by POSIX to return unsigned char *. Taking its address is semantically wrong -- the intent is to get a pointer to the control message data, which is exactly what CMSG_DATA() already returns.

This happens to compile with glibc because glibc's primary CMSG_DATA definition accesses a flexible array member:

    #define CMSG_DATA(cmsg) ((cmsg)->__cmsg_data)

An array lvalue can have its address taken, and &array yields the same address as array. However, glibc also has an alternative definition that uses pointer arithmetic (returning an rvalue), and musl's definition always uses pointer arithmetic:

    /* musl */
    #define CMSG_DATA(cmsg) \
        ((unsigned char *)(((struct cmsghdr *)(cmsg)) + 1))

Taking the address of an rvalue is a hard error in C, so the current code fails to compile with musl libc.

Remove the erroneous & operator. The resulting code is correct regardless of the CMSG_DATA implementation -- it simply assigns the data pointer, which is what the subsequent code (fd_map[--num_fds]) expects.

No functional change with glibc; fixes the build with musl.

---
 arch/um/kernel/skas/stub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/um/kernel/skas/stub.c +1 -1
@@ -146,7 +146,7 @@
 		/* Receive the FDs */
 		num_fds = 0;
 		fd_msg = msghdr.msg_control;
-		fd_map = (void *)&CMSG_DATA(fd_msg);
+		fd_map = (void *)CMSG_DATA(fd_msg);
 		if (res == iov.iov_len && msghdr.msg_controllen > sizeof(struct cmsghdr))
 			num_fds = (fd_msg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
 
#1 lore raw

PATCH 2/2 um: avoid struct sigcontext redefinition with musl

mcontext.c includes both <sys/ucontext.h> and <asm/sigcontext.h>. With musl libc, this causes a struct sigcontext redefinition error:

<sys/ucontext.h> pulls in musl's <bits/signal.h>, which defines struct sigcontext directly. The kernel's <asm/sigcontext.h> then provides a second, conflicting definition of the same struct.

With glibc this does not conflict because glibc's signal headers source their struct sigcontext from the kernel's own UAPI headers, so the include guard in <asm/sigcontext.h> makes the second inclusion a no-op.

mcontext.c does not actually use struct sigcontext by name -- it only needs the FP-state types (_fpstate, _xstate, etc.) that are defined in <asm/sigcontext.h> independently of the sigcontext struct.

Temporarily rename sigcontext to __kernel_sigcontext during the inclusion of <asm/sigcontext.h> so that the kernel's definition does not collide with musl's. The #undef restores normal name resolution immediately afterward.

No functional change with glibc; fixes the build with musl.

---
 arch/x86/um/os-Linux/mcontext.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/um/os-Linux/mcontext.c +6 -0
@@ -4,7 +4,13 @@
 #include <linux/string.h>
 #include <sys/ucontext.h>
 #include <asm/ptrace.h>
+/*
+ * musl defines struct sigcontext in <bits/signal.h>.  Rename the kernel's
+ * copy to avoid redefinition while keeping the FP-state types available.
+ */
+#define sigcontext __kernel_sigcontext
 #include <asm/sigcontext.h>
+#undef sigcontext
 #include <sysdep/ptrace.h>
 #include <sysdep/mcontext.h>
 #include <arch.h>