From: Laurent Vivier <laurent@vivier.eu>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Laurent Vivier <laurent@vivier.eu>
Subject: [Qemu-devel] [PULL 03/13] linux-user: Fix length calculations in host_to_target_cmsg()
Date: Tue, 23 Jan 2018 15:47:57 +0100 [thread overview]
Message-ID: <20180123144807.5618-4-laurent@vivier.eu> (raw)
In-Reply-To: <20180123144807.5618-1-laurent@vivier.eu>
From: Peter Maydell <peter.maydell@linaro.org>
The handling of length calculations in host_to_target_cmsg()
was rather confused:
* when checking for whether the target cmsg header fit in
the remaining buffer, we were using the host struct size,
not the target size
* we were setting tgt_len to "target payload + header length"
but then using it as if it were the target payload length alone
* in various message type cases we weren't handling the possibility
that host or target buffers were truncated
Fix these problems. The second one in particular is liable
to result in us overrunning the guest provided buffer,
since we will try to convert more data than is actually
present.
Fixes: https://bugs.launchpad.net/qemu/+bug/1701808
Reported-by: Bruno Haible <bruno@clisp.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <1513345976-22958-2-git-send-email-peter.maydell@linaro.org>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
linux-user/syscall.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 11c9116c4a..a1b9772a85 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1782,7 +1782,7 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
* to the guest via the CTRUNC bit), unlike truncation
* in target_to_host_cmsg, which is a QEMU bug.
*/
- if (msg_controllen < sizeof(struct cmsghdr)) {
+ if (msg_controllen < sizeof(struct target_cmsghdr)) {
target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
break;
}
@@ -1794,8 +1794,6 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
}
target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
- tgt_len = TARGET_CMSG_LEN(len);
-
/* Payload types which need a different size of payload on
* the target must adjust tgt_len here.
*/
@@ -1809,12 +1807,13 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
break;
}
default:
+ tgt_len = len;
break;
}
- if (msg_controllen < tgt_len) {
+ if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
- tgt_len = msg_controllen;
+ tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
}
/* We must now copy-and-convert len bytes of payload
@@ -1875,6 +1874,10 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
uint32_t *v = (uint32_t *)data;
uint32_t *t_int = (uint32_t *)target_data;
+ if (len != sizeof(uint32_t) ||
+ tgt_len != sizeof(uint32_t)) {
+ goto unimplemented;
+ }
__put_user(*v, t_int);
break;
}
@@ -1888,6 +1891,10 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
struct errhdr_t *target_errh =
(struct errhdr_t *)target_data;
+ if (len != sizeof(struct errhdr_t) ||
+ tgt_len != sizeof(struct errhdr_t)) {
+ goto unimplemented;
+ }
__put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
__put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
__put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
@@ -1911,6 +1918,10 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
uint32_t *v = (uint32_t *)data;
uint32_t *t_int = (uint32_t *)target_data;
+ if (len != sizeof(uint32_t) ||
+ tgt_len != sizeof(uint32_t)) {
+ goto unimplemented;
+ }
__put_user(*v, t_int);
break;
}
@@ -1924,6 +1935,10 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
struct errhdr6_t *target_errh =
(struct errhdr6_t *)target_data;
+ if (len != sizeof(struct errhdr6_t) ||
+ tgt_len != sizeof(struct errhdr6_t)) {
+ goto unimplemented;
+ }
__put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
__put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
__put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
@@ -1950,8 +1965,8 @@ static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
}
}
- target_cmsg->cmsg_len = tswapal(tgt_len);
- tgt_space = TARGET_CMSG_SPACE(len);
+ target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
+ tgt_space = TARGET_CMSG_SPACE(tgt_len);
if (msg_controllen < tgt_space) {
tgt_space = msg_controllen;
}
--
2.14.3
next prev parent reply other threads:[~2018-01-23 14:48 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-23 14:47 [Qemu-devel] [PULL 00/13] Linux user for 2.12 patches Laurent Vivier
2018-01-23 14:47 ` [Qemu-devel] [PULL 01/13] linux-user: Fix locking order in fork_start() Laurent Vivier
2018-01-23 14:47 ` [Qemu-devel] [PULL 02/13] linux-user: wrap fork() in a start/end exclusive section Laurent Vivier
2018-01-23 14:47 ` Laurent Vivier [this message]
2018-01-23 14:47 ` [Qemu-devel] [PULL 04/13] linux-user: Don't use CMSG_ALIGN(sizeof struct cmsghdr) Laurent Vivier
2018-01-23 14:47 ` [Qemu-devel] [PULL 05/13] linux-user: Translate flags argument to dup3 syscall Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 06/13] linux-user/mmap.c: Avoid choosing NULL as start address Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 07/13] linux-user: Fix sched_get/setaffinity conversion Laurent Vivier
2018-01-26 18:23 ` Peter Maydell
2018-01-26 18:33 ` Samuel Thibault
2018-01-23 14:48 ` [Qemu-devel] [PULL 08/13] linux-user: Add AT_SECURE auxval Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 09/13] linux-user: Add getcpu() support Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 10/13] linux-user: remove nmi.c and fw-path-provider.c Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 11/13] linux-user: Propagate siginfo_t through to handle_cpu_signal() Laurent Vivier
2018-01-23 14:48 ` [Qemu-devel] [PULL 12/13] page_unprotect(): handle calls to pages that are PAGE_WRITE Laurent Vivier
2018-03-22 1:52 ` Laurent Vivier
2018-03-22 10:36 ` Laurent Vivier
2018-03-22 11:05 ` Peter Maydell
2018-03-22 11:07 ` Peter Maydell
2018-03-22 11:13 ` Laurent Vivier
2018-03-22 16:47 ` Laurent Vivier
2018-03-22 11:07 ` Laurent Vivier
2018-03-22 11:10 ` Peter Maydell
2018-01-23 14:48 ` [Qemu-devel] [PULL 13/13] linux-user: implement renameat2 Laurent Vivier
2018-01-23 19:13 ` Palmer Dabbelt
2018-01-23 20:13 ` Laurent Vivier
2018-01-23 20:55 ` Palmer Dabbelt
2018-01-25 11:37 ` [Qemu-devel] [PULL 00/13] Linux user for 2.12 patches Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180123144807.5618-4-laurent@vivier.eu \
--to=laurent@vivier.eu \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).