qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] linux-user: Handle short lengths in host_to_target_sockaddr()
@ 2016-07-07 14:44 Peter Maydell
  2016-07-07 19:23 ` Riku Voipio
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Maydell @ 2016-07-07 14:44 UTC (permalink / raw)
  To: qemu-devel; +Cc: patches, Riku Voipio

If userspace specifies a short buffer for a target sockaddr,
the kernel will only copy in as much as it has space for
(or none at all if the length is zero) -- see the kernel
move_addr_to_user() function. Mimic this in QEMU's
host_to_target_sockaddr() routine.

In particular, this fixes a segfault running the LTP
recvfrom01 test, where the guest makes a recvfrom()
call with a bad buffer pointer and other parameters which
cause the kernel to set the addrlen to zero; because we
did not skip the attempt to swap the sa_family field we
segfaulted on the bad address.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/syscall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 165fd06..6e77d34 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1374,12 +1374,19 @@ static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
 {
     struct target_sockaddr *target_saddr;
 
+    if (len == 0) {
+        return 0;
+    }
+
     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
     if (!target_saddr)
         return -TARGET_EFAULT;
     memcpy(target_saddr, addr, len);
-    target_saddr->sa_family = tswap16(addr->sa_family);
-    if (addr->sa_family == AF_NETLINK) {
+    if (len >= offsetof(struct target_sockaddr, sa_family) +
+        sizeof(target_saddr->sa_family)) {
+        target_saddr->sa_family = tswap16(addr->sa_family);
+    }
+    if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
         struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
         target_nl->nl_pid = tswap32(target_nl->nl_pid);
         target_nl->nl_groups = tswap32(target_nl->nl_groups);
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] linux-user: Handle short lengths in host_to_target_sockaddr()
  2016-07-07 14:44 [Qemu-devel] [PATCH] linux-user: Handle short lengths in host_to_target_sockaddr() Peter Maydell
@ 2016-07-07 19:23 ` Riku Voipio
  0 siblings, 0 replies; 2+ messages in thread
From: Riku Voipio @ 2016-07-07 19:23 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel, patches

On Thu, Jul 07, 2016 at 03:44:43PM +0100, Peter Maydell wrote:
> If userspace specifies a short buffer for a target sockaddr,
> the kernel will only copy in as much as it has space for
> (or none at all if the length is zero) -- see the kernel
> move_addr_to_user() function. Mimic this in QEMU's
> host_to_target_sockaddr() routine.
> 
> In particular, this fixes a segfault running the LTP
> recvfrom01 test, where the guest makes a recvfrom()
> call with a bad buffer pointer and other parameters which
> cause the kernel to set the addrlen to zero; because we
> did not skip the attempt to swap the sa_family field we
> segfaulted on the bad address.
 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

All your patches up to this one applied to linux-user, thanks!

Riku

> ---
>  linux-user/syscall.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 165fd06..6e77d34 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -1374,12 +1374,19 @@ static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
>  {
>      struct target_sockaddr *target_saddr;
>  
> +    if (len == 0) {
> +        return 0;
> +    }
> +
>      target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
>      if (!target_saddr)
>          return -TARGET_EFAULT;
>      memcpy(target_saddr, addr, len);
> -    target_saddr->sa_family = tswap16(addr->sa_family);
> -    if (addr->sa_family == AF_NETLINK) {
> +    if (len >= offsetof(struct target_sockaddr, sa_family) +
> +        sizeof(target_saddr->sa_family)) {
> +        target_saddr->sa_family = tswap16(addr->sa_family);
> +    }
> +    if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
>          struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
>          target_nl->nl_pid = tswap32(target_nl->nl_pid);
>          target_nl->nl_groups = tswap32(target_nl->nl_groups);
> -- 
> 1.9.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-07-07 19:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-07 14:44 [Qemu-devel] [PATCH] linux-user: Handle short lengths in host_to_target_sockaddr() Peter Maydell
2016-07-07 19:23 ` Riku Voipio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).