diff mbox

linux-user: Don't overrun guest buffer in sched_getaffinity

Message ID 1400161223-30451-1-git-send-email-peter.maydell@linaro.org
State Superseded
Headers show

Commit Message

Peter Maydell May 15, 2014, 1:40 p.m. UTC
If the guest's "long" type is smaller than the host's, then
our sched_getaffinity wrapper needs to round the buffer size
up to a multiple of the host sizeof(long). This means that when
we copy the data back from the host buffer to the guest's
buffer there might be more than we can fit. Rather than
overflowing the guest's buffer, handle this case by returning
EINVAL or ignoring the unused extra space, as appropriate.

Note that only guests using the syscall interface directly might
run into this bug -- the glibc wrappers around it will always
use a buffer whose size is a multiple of 8 regardless of guest
architecture.

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

Comments

Peter Maydell May 28, 2014, 10:55 a.m. UTC | #1
On 15 May 2014 14:40, Peter Maydell <peter.maydell@linaro.org> wrote:
> If the guest's "long" type is smaller than the host's, then
> our sched_getaffinity wrapper needs to round the buffer size
> up to a multiple of the host sizeof(long). This means that when
> we copy the data back from the host buffer to the guest's
> buffer there might be more than we can fit. Rather than
> overflowing the guest's buffer, handle this case by returning
> EINVAL or ignoring the unused extra space, as appropriate.
>
> Note that only guests using the syscall interface directly might
> run into this bug -- the glibc wrappers around it will always
> use a buffer whose size is a multiple of 8 regardless of guest
> architecture.

In fact it turns out that gcc 4.9 compiled ARM binaries will
run into this, because libgomp does direct syscalls as part
of its initialization:

https://bugs.launchpad.net/bugs/1311614

So:

Cc: qemu-stable@nongnu.org

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Also: ping! :-)

thanks
-- PMM
diff mbox

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 6efeeff..840ced1 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7438,6 +7438,22 @@  abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
             ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
 
             if (!is_error(ret)) {
+                if (ret > arg2) {
+                    /* More data returned than the caller's buffer will fit.
+                     * This only happens if sizeof(abi_long) < sizeof(long)
+                     * and the caller passed us a buffer holding an odd number
+                     * of abi_longs. If the host kernel is actually using the
+                     * extra 4 bytes then fail EINVAL; otherwise we can just
+                     * ignore them and only copy the interesting part.
+                     */
+                    int numcpus = sysconf(_SC_NPROCESSORS_CONF);
+                    if (numcpus > arg2 * 8) {
+                        ret = -TARGET_EINVAL;
+                        break;
+                    }
+                    ret = arg2;
+                }
+
                 if (copy_to_user(arg3, mask, ret)) {
                     goto efault;
                 }