Skip to contentSkip to conversation

linux-user/syscall: zero-init msghdr in do_sendrecvmsg_locked

PATCH In review Kenta Iwasaki 2 participants 7 messages May 16, 2021 – Jun 20, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/syscall: zero-init msghdr in do_sendrecvmsg_locked

The mixing of libc and kernel versions of the layout of the msghdr struct causes EMSGSIZE to be returned by sendmsg if the msghdr struct is not zero-initialized (such that padding bytes comprise of uninitialized memory).

Other parts of the QEMU codebase appear to zero-initialize the msghdr struct to workaround these struct layout issues, except for do_sendrecvmsg_locked in linux-user/syscall.c.

This patch zero-initializes the msghdr struct in do_sendrecvmsg_locked.

---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +1 -1
index 95d79ddc43..f60b7e04d5 100644
@@ -3337,7 +3337,7 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
                                       int flags, int send)
 {
     abi_long ret, len;
-    struct msghdr msg;
+    struct msghdr msg = { 0 };
     abi_ulong count;
     struct iovec *vec;
     abi_ulong target_vec;
Signature · 1 line
2.31.0
#1 lore raw

Le 16/05/2021 à 11:15, Kenta Iwasaki a écrit :

Quoted 23 lines

The mixing of libc and kernel versions of the layout of the msghdr struct causes EMSGSIZE to be returned by sendmsg if the msghdr struct is not zero-initialized (such that padding bytes comprise of uninitialized memory).

Other parts of the QEMU codebase appear to zero-initialize the msghdr struct to workaround these struct layout issues, except for do_sendrecvmsg_locked in linux-user/syscall.c.

This patch zero-initializes the msghdr struct in do_sendrecvmsg_locked.

---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 95d79ddc43..f60b7e04d5 100644

linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3343

Quoted 8 lines
linux-user/syscall.c +1 -1
@@ -3337,7 +3337,7 @@ static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
                                       int flags, int send)
 {
     abi_long ret, len;
-    struct msghdr msg;
+    struct msghdr msg = { 0 };
     abi_ulong count;
     struct iovec *vec;
     abi_ulong target_vec;

It seems do_sendrecvmsg_locked() initializes all the fields of the structure, I don't see why we need to clear it before use.

Could you explain more?

Thanks,
Laurent

#2 lore raw

Sure,

The bytes of msghdr need to be cleared because the msghdr struct layout specified in QEMU appears to generalize between the definitions of msghdr across different libc's and kernels. To appropriately generalize msghdr across libc's and kernels would either:

1. require specializing code in do_sendrecvmsg_locked() for each individual libc and kernel version, or
2. zeroing out all bytes of msghdr, b/c certain libc or kernel versions may misinterpret the undefined padding bytes that come from misalignment in the struct as actual syscall params.

The patch I provided would be going for route #2, given that it's a simpler fix for the underlying problem for the short term.

What I believe is the background behind why the struct layout has been a problem is because, since the beginning, the Linux kernel has always specified the layout of msghdr differently from POSIX. Given that this implies incompatibility between kernels on how msghdr is specified, different libc projects such as musl and glibc provide different workarounds by laying out msghdr differently amongst one another.

A few projects running tests/applications through QEMU have been bitten by this, and a solution that one of the projects discovered was that patching QEMU to zero-initialize the bytes msghdr the same way my patch does allow for compatibility between different msghdr layouts across glibc, musl, and the Linux kernel:
https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360

For some additional useful context, here's a link pointing changes musl libc made to laying out msghdr b/c of Linux incorrectly laying out msghdr against the POSIX standard:
http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64

Also, here is a link to the msghdr struct layout in musl libc's bleeding edge branch as of now:
https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22

As for my rationale for sending in this patch, it is because I'm currently implementing cross-platform networking in the standard library for the Zig programming language, and have run into this exact same problem with EMSGSIZE being returned by sendmsg() when tests are run through QEMU on x86_64-linux-musl.

My discussions with the Zig community about it alongside debug logs regarding the exact parameters being fed to the sendmsg() syscall can be found here: https://github.com/ziglang/zig/pull/8750#issuecomment-841641576

Hope this gives enough context about the problem and patch, but please do let me know if there is any more information that I could provide which would help.

Best regards,
Kenta Iwasaki

Quoted 43 lines · Laurent Vivier

linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd,

Quoted 22 lines · Kenta Iwasaki

The mixing of libc and kernel versions of the layout of the msghdr struct causes EMSGSIZE to be returned by sendmsg if the msghdr struct is not zero-initialized (such that padding bytes comprise of uninitialized memory).

Other parts of the QEMU codebase appear to zero-initialize the msghdr struct to workaround these struct layout issues, except for do_sendrecvmsg_locked in linux-user/syscall.c.

This patch zero-initializes the msghdr struct in do_sendrecvmsg_locked.

---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 95d79ddc43..f60b7e04d5 100644
@@ -3337,7 +3337,7 @@ static abi_long do_sendrecvmsg_locked(int fd,

struct target_msghdr *msgp,

linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3343

Quoted 9 lines
linux-user/syscall.c +1 -1
                                       int flags, int send)
 {
     abi_long ret, len;
-    struct msghdr msg;
+    struct msghdr msg = { 0 };
     abi_ulong count;
     struct iovec *vec;
     abi_ulong target_vec;

It seems do_sendrecvmsg_locked() initializes all the fields of the structure, I don't see why we
need to clear it before use.

Could you explain more?

Thanks,
Laurent

#3 lore raw

Doing a ping for this patch.
https://patchew.org/QEMU/[email protected]/

Best regards,
Kenta Iwasaki

Quoted 104 lines · Kenta Iwasaki

Sure,

The bytes of msghdr need to be cleared because the msghdr struct layout specified in QEMU appears to generalize between the definitions of msghdr across different libc's and kernels. To appropriately generalize msghdr across libc's and kernels would either:

1. require specializing code in do_sendrecvmsg_locked() for each individual libc and kernel version, or
2. zeroing out all bytes of msghdr, b/c certain libc or kernel versions may misinterpret the undefined padding bytes that come from misalignment in the struct as actual syscall params.

The patch I provided would be going for route #2, given that it's a simpler fix for the underlying problem for the short term.

What I believe is the background behind why the struct layout has been a problem is because, since the beginning, the Linux kernel has always specified the layout of msghdr differently from POSIX. Given that this implies incompatibility between kernels on how msghdr is specified, different libc projects such as musl and glibc provide different workarounds by laying out msghdr differently amongst one another.

A few projects running tests/applications through QEMU have been bitten by this, and a solution that one of the projects discovered was that patching QEMU to zero-initialize the bytes msghdr the same way my patch does allow for compatibility between different msghdr layouts across glibc, musl, and the Linux kernel:
https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360

For some additional useful context, here's a link pointing changes musl libc made to laying out msghdr b/c of Linux incorrectly laying out msghdr against the POSIX standard:
http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64

Also, here is a link to the msghdr struct layout in musl libc's bleeding edge branch as of now:
https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22

As for my rationale for sending in this patch, it is because I'm currently implementing cross-platform networking in the standard library for the Zig programming language, and have run into this exact same problem with EMSGSIZE being returned by sendmsg() when tests are run through QEMU on x86_64-linux-musl.

My discussions with the Zig community about it alongside debug logs regarding the exact parameters being fed to the sendmsg() syscall can be found here:
https://github.com/ziglang/zig/pull/8750#issuecomment-841641576

Hope this gives enough context about the problem and patch, but please do let me know if there is any more information that I could provide which would help.

Best regards,
Kenta Iwasaki

Quoted 43 lines · Laurent Vivier

linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd,

Quoted 22 lines · Kenta Iwasaki

The mixing of libc and kernel versions of the layout of the msghdr struct causes EMSGSIZE to be returned by sendmsg if the msghdr struct is not zero-initialized (such that padding bytes comprise of uninitialized memory).

Other parts of the QEMU codebase appear to zero-initialize the msghdr struct to workaround these struct layout issues, except for do_sendrecvmsg_locked in linux-user/syscall.c.

This patch zero-initializes the msghdr struct in do_sendrecvmsg_locked.

---
 linux-user/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 95d79ddc43..f60b7e04d5 100644
@@ -3337,7 +3337,7 @@ static abi_long do_sendrecvmsg_locked(int fd,

struct target_msghdr *msgp,

linux-user/syscall.c static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp, · line 3343

Quoted 9 lines
linux-user/syscall.c +1 -1
                                       int flags, int send)
 {
     abi_long ret, len;
-    struct msghdr msg;
+    struct msghdr msg = { 0 };
     abi_ulong count;
     struct iovec *vec;
     abi_ulong target_vec;

It seems do_sendrecvmsg_locked() initializes all the fields of the structure, I don't see why we
need to clear it before use.

Could you explain more?

Thanks,
Laurent

#4 lore raw
In reply to Kenta Iwasaki

Le 16/05/2021 à 14:57, Kenta Iwasaki a écrit :

Quoted 39 lines

Sure,

The bytes of msghdr need to be cleared because the msghdr struct layout specified in QEMU appears to generalize between the definitions of msghdr across different libc's and kernels. To appropriately generalize msghdr across libc's and kernels would either:

1. require specializing code in do_sendrecvmsg_locked() for each individual libc and kernel version, or
2. zeroing out all bytes of msghdr, b/c certain libc or kernel versions may misinterpret the undefined padding bytes that come from misalignment in the struct as actual syscall params.

The patch I provided would be going for route #2, given that it's a simpler fix for the underlying problem for the short term.

What I believe is the background behind why the struct layout has been a problem is because, since the beginning, the Linux kernel has always specified the layout of msghdr differently from POSIX. Given that this implies incompatibility between kernels on how msghdr is specified, different libc projects such as musl and glibc provide different workarounds by laying out msghdr differently amongst one another.

A few projects running tests/applications through QEMU have been bitten by this, and a solution that one of the projects discovered was that patching QEMU to zero-initialize the bytes msghdr the same way my patch does allow for compatibility between different msghdr layouts across glibc, musl, and the Linux kernel: https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360
<https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360>

For some additional useful context, here's a link pointing changes musl libc made to laying out msghdr b/c of Linux incorrectly laying out msghdr against the POSIX standard:
http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64
<http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64>

Also, here is a link to the msghdr struct layout in musl libc's bleeding edge branch as of now: https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22
<https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22>

As for my rationale for sending in this patch, it is because I'm currently implementing cross-platform networking in the standard library for the Zig programming language, and have run into this exact same problem with EMSGSIZE being returned by sendmsg() when tests are run through QEMU on x86_64-linux-musl.

Quoted 7 lines

My discussions with the Zig community about it alongside debug logs regarding the exact parameters being fed to the sendmsg() syscall can be found
here: https://github.com/ziglang/zig/pull/8750#issuecomment-841641576
<https://github.com/ziglang/zig/pull/8750#issuecomment-841641576>

Hope this gives enough context about the problem and patch, but please do let me know if there is any more information that I could provide which would help.

Thank you for the explanation and sorry for the delay.

As we use directly the kernel syscall rather than the libc wrapper we should not use here the msghdr definition from the libc but the one from the kernel.

The one we receive is also the one from the kernel as we trap the kernel syscall not the libc call.

So the code should be libc-agnostic...

The reference implementation in our case is 'struct user_msghdr' from the kernel, and we need to duplicate it in QEMU to be able to use it.

Thanks,
LAurent

#5 lore raw

No worries, though I would also like to apologize as I find that the explanation I gave in my last email was a little all over the place.

To clarify my last e-mail, I believe the current msghdr struct layout in QEMU is libc-agnostic, but not kernel agnostic. Rather, the current msghdr struct layout defined in QEMU follows the definition of the Linux kernel's own struct layout of msghdr, which isn't compliant with POSIX.

Zeroing the bytes in the patch I provided, which is a trick used in musl libc as well to be both kernel-agnostic and libc-agnostic, would make QEMU's struct layout definition of msghdr be both kernel-agnostic to all kernels adhering to the POSIX standard, and libc-agnostic as well.

Best regards,
Kenta Iwasaki

Quoted 94 lines · Laurent Vivier
Quoted 3 lines · Kenta Iwasaki

Sure,

The bytes of msghdr need to be cleared because the msghdr struct

layout specified in QEMU

Quoted 1 line

appears to generalize between the definitions of msghdr across

different libc's and kernels. To

Quoted 3 lines

appropriately generalize msghdr across libc's and kernels would either:

1. require specializing code in do_sendrecvmsg_locked() for each

individual libc and kernel version, or

Quoted 1 line

2. zeroing out all bytes of msghdr, b/c certain libc or kernel

versions may misinterpret the

Quoted 1 line

undefined padding bytes that come from misalignment in the struct as

actual syscall params.

Quoted 2 lines

The patch I provided would be going for route #2, given that it's a

simpler fix for the underlying

Quoted 3 lines

problem for the short term.

What I believe is the background behind why the struct layout has been a

problem is because, since

Quoted 1 line

the beginning, the Linux kernel has always specified the layout of

msghdr differently from POSIX.

Quoted 1 line

Given that this implies incompatibility between kernels on how msghdr

is specified, different libc

Quoted 1 line

projects such as musl and glibc provide different workarounds by laying

out msghdr differently

Quoted 3 lines

amongst one another.

A few projects running tests/applications through QEMU have been bitten

by this, and a solution that

Quoted 1 line

one of the projects discovered was that patching QEMU to zero-initialize

the bytes msghdr the same

Quoted 1 line

way my patch does allow for compatibility between different msghdr

layouts across glibc, musl, and

Quoted 1 line

the Linux kernel:

https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360

Quoted 1 line

<

https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360

Quoted 3 lines

For some additional useful context, here's a link pointing changes musl

libc made to laying out

Quoted 1 line

msghdr b/c of Linux incorrectly laying out msghdr against the POSIX

standard:

Quoted 1 line

http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64

Quoted 1 line

<

http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64

Quoted 3 lines

Also, here is a link to the msghdr struct layout in musl libc's

bleeding edge branch as of

Quoted 4 lines

now: https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22
<https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22>

As for my rationale for sending in this patch, it is because I'm

currently implementing

Quoted 1 line

cross-platform networking in the standard library for the Zig

programming language, and have run

Quoted 1 line

into this exact same problem with EMSGSIZE being returned by sendmsg()

when tests are run through

Quoted 3 lines

QEMU on x86_64-linux-musl.

My discussions with the Zig community about it alongside debug logs

regarding the exact parameters

Quoted 5 lines

being fed to the sendmsg() syscall can be found
here: https://github.com/ziglang/zig/pull/8750#issuecomment-841641576
<https://github.com/ziglang/zig/pull/8750#issuecomment-841641576>

Hope this gives enough context about the problem and patch, but please

do let me know if there is

Quoted 1 line

any more information that I could provide which would help.

Thank you for the explanation and sorry for the delay.

As we use directly the kernel syscall rather than the libc wrapper we should not use here the msghdr
definition from the libc but the one from the kernel.

The one we receive is also the one from the kernel as we trap the kernel syscall not the libc call.

So the code should be libc-agnostic...

The reference implementation in our case is 'struct user_msghdr' from the kernel, and we need to
duplicate it in QEMU to be able to use it.

Thanks,
LAurent

#6 lore raw

Le 20/06/2021 à 17:09, Kenta Iwasaki a écrit :

Quoted 10 lines

No worries, though I would also like to apologize as I find that the explanation I gave in my last email was a little all over the place.

To clarify my last e-mail, I believe the current msghdr struct layout in QEMU is libc-agnostic, but not kernel agnostic. Rather, the current msghdr struct layout defined in QEMU follows the definition of the Linux kernel's own struct layout of msghdr, which isn't compliant with POSIX.

Zeroing the bytes in the patch I provided, which is a trick used in musl libc as well to be both kernel-agnostic and libc-agnostic, would make QEMU's struct layout definition of msghdr be both kernel-agnostic to all kernels adhering to the POSIX standard, and libc-agnostic as well.

Yes, linux-user is not kernel agnostic. It's why I would prefer to define a "host_msghdr" and use it as a parameter to the host outgoing syscall (we already define "target_msghdr" for target incoming syscall). host_msghdr would be a copy of kernel user_msghdr.

But what do you mean by "all kernels": do we have linux kernels that use a different msghdr definition (POSIX one)?

Thanks,
Laurent

Quoted 82 lines

Best regards,
Kenta Iwasaki

On Sun, 20 Jun 2021 at 23:56, Laurent Vivier <[email protected] <mailto:[email protected]>> wrote:

Le 16/05/2021 à 14:57, Kenta Iwasaki a écrit :
> Sure,
>
> The bytes of msghdr need to be cleared because the msghdr struct layout specified in QEMU > appears to generalize between the definitions of msghdr across different libc's and kernels. To > appropriately generalize msghdr across libc's and kernels would either:
>
> 1. require specializing code in do_sendrecvmsg_locked() for each individual libc and kernel version, or
> 2. zeroing out all bytes of msghdr, b/c certain libc or kernel versions may misinterpret the > undefined padding bytes that come from misalignment in the struct as actual syscall params. >
> The patch I provided would be going for route #2, given that it's a simpler fix for the underlying > problem for the short term.
>
> What I believe is the background behind why the struct layout has been a problem is because, since > the beginning, the Linux kernel has always specified the layout of msghdr differently from POSIX.
> Given that this implies incompatibility between kernels on how msghdr is specified, different libc
> projects such as musl and glibc provide different workarounds by laying out msghdr differently > amongst one another.
>
> A few projects running tests/applications through QEMU have been bitten by this, and a solution that
> one of the projects discovered was that patching QEMU to zero-initialize the bytes msghdr the same > way my patch does allow for compatibility between different msghdr layouts across glibc, musl, and
> the Linux kernel:
https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360
<https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360>
> <https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360
<https://github.com/void-linux/void-packages/issues/23557#issuecomment-718392360>>
>
> For some additional useful context, here's a link pointing changes musl libc made to laying out > msghdr b/c of Linux incorrectly laying out msghdr against the POSIX standard:
> http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64
<http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64>
> <http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64
<http://git.musl-libc.org/cgit/musl/commit/?id=7168790763cdeb794df52be6e3b39fbb021c5a64>>
>
> Also, here is a link to the msghdr struct layout in musl libc's bleeding edge branch as of > now: https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22
<https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22>
> <https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22
<https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h#n22>>
>
> As for my rationale for sending in this patch, it is because I'm currently implementing > cross-platform networking in the standard library for the Zig programming language, and have run > into this exact same problem with EMSGSIZE being returned by sendmsg() when tests are run through > QEMU on x86_64-linux-musl.
>
> My discussions with the Zig community about it alongside debug logs regarding the exact parameters > being fed to the sendmsg() syscall can be found
> here: https://github.com/ziglang/zig/pull/8750#issuecomment-841641576
<https://github.com/ziglang/zig/pull/8750#issuecomment-841641576>
> <https://github.com/ziglang/zig/pull/8750#issuecomment-841641576
<https://github.com/ziglang/zig/pull/8750#issuecomment-841641576>>
>
> Hope this gives enough context about the problem and patch, but please do let me know if there is > any more information that I could provide which would help.

Thank you for the explanation and sorry for the delay.

As we use directly the kernel syscall rather than the libc wrapper we should not use here the msghdr definition from the libc but the one from the kernel.

The one we receive is also the one from the kernel as we trap the kernel syscall not the libc call.

So the code should be libc-agnostic...

The reference implementation in our case is 'struct user_msghdr' from the kernel, and we need to duplicate it in QEMU to be able to use it.

Thanks,
LAurent