linux-user: NULL and zero-length buffer fixes
cover linux-user: NULL and zero-length buffer fixes
Hi all,
two places where qemu-user answers differently from the kernel for NULL buffer pointers. I found them by running the same static binary natively and under qemu-user and comparing what each call returned.
1/2: getrandom() with a zero-length request. The kernel never looks at the buffer, so getrandom(NULL, 0, 0) returns 0; qemu-user locks the buffer first and fails with EFAULT even though nothing is copied. read(), write(), pread64() and pwrite64() already special-case the zero-length request, so do the same here.
2/2: sysinfo() and getitimer() skip the copy-out when the guest pointer is zero, so a NULL pointer returns success and leaves the result untouched. The kernel reports EFAULT for a NULL address, and so should we. The other users of the same "if (!is_error(ret) && argN)" idiom in this file guard results that really are optional (rt_sigaction's old action, the old signal mask, sendfile's offset, the old rlimit, setitimer's old value), so these two were the only ones skipping a required copy-out.
Both are covered by a new test, tests/tcg/multiarch/linux/linux-null-buffer.c, which asserts the kernel behaviour: it fails before the series and passes after it. The NULL-pointer cases in the test go through syscall() rather than the libc wrappers, because some of those wrappers copy the result through a local object and would follow the NULL pointer themselves; on 32-bit x86 getitimer() is exactly such a case.
Covered so far, with the test built per target and run under the patched
qemu: aarch64 (64-bit LE), x86_64 (64-bit LE), i386 (32-bit LE), s390x (64-bit BE) and ppc (32-bit BE). On each of them the test fails against 10.2.1 and passes with the series.
Measured on aarch64 (Ubuntu 26.04, Linux 7.0.0-1013-oracle) with static binaries:
call native qemu 10.2.1 with this series getrandom(NULL, 0, 0) 0 -1 EFAULT 0 sysinfo(NULL) -1 EFAULT 0 -1 EFAULT getitimer(REAL, NULL) -1 EFAULT 0 -1 EFAULT
The ordinary calls (getrandom(buf, n, 0), sysinfo(&si), getitimer(w, &it)) and the flag checking of getrandom() are unaffected.
Reproducers: https://github.com/takano32/qemu-user-null-args
Takano
TAKANO Mitsuhiro (2):
linux-user: allow a zero-length getrandom with a NULL buffer
linux-user: report EFAULT for a NULL sysinfo/getitimer buffer
linux-user/syscall.c | 12 +++++++-- tests/tcg/multiarch/linux/linux-null-buffer.c | 48 +++++++++++++++++++++++++++ tests/tcg/multiarch/meson.build | 1 + 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/multiarch/linux/linux-null-buffer.c
Signature · 1 line
2.53.0
PATCH 1/2 linux-user: allow a zero-length getrandom with a NULL buffer
The kernel never looks at the buffer of a zero-length request, so getrandom(NULL, 0, 0) returns 0. qemu-user hands the request to lock_user() unconditionally, and a NULL or otherwise unusable address fails there with EFAULT even though nothing is copied.
read(), write(), pread64() and pwrite64() already special-case the zero-length request; do the same here. The address plays no part in it:
with a zero length it is never dereferenced, whatever value the caller passes.
--- linux-user/syscall.c | 8 ++++++++ tests/tcg/multiarch/linux/linux-null-buffer.c | 19 +++++++++++++++++++ tests/tcg/multiarch/meson.build | 1 + 3 files changed, 28 insertions(+) create mode 100644 tests/tcg/multiarch/linux/linux-null-buffer.c
linux-user/syscall.c +8 -0
@@ -11456,6 +11456,14 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,#endif #if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)case TARGET_NR_getrandom: +if (arg2 ==0 ) { +/* +* A zero-length request has no buffer to lock, and the kernel +* never looks at the pointer for it, so pass the call through +* instead of failing on a NULL or otherwise unusable address. +*/ +return get_errno (getrandom (NULL ,0 , arg3)); + } p =lock_user (VERIFY_WRITE , arg1, arg2,0 );if (!p) {return -TARGET_EFAULT ;
tests/tcg/multiarch/linux/linux-null-buffer.c new file +19 -0
@@ -0,0 +1,19 @@ +/* +* A zero-length getrandom() needs no valid buffer: the kernel never looks at +* the pointer, so getrandom(NULL, 0, 0) succeeds. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <stdlib.h> +#include <sys/random.h> + +int main (void ) +{ + errno =0 ; +assert (getrandom (NULL ,0 ,0 ) ==0 ); + +return EXIT_SUCCESS ; +}
tests/tcg/multiarch/meson.build +1 -0
@@ -57,6 +57,7 @@ tests += { # Linux tests tests += { multiarch/'linux/linux-madvise.c': {}, + multiarch/'linux/linux-null-buffer.c': {}, multiarch/'linux/linux-shmat-maps.c': {}, multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {},
Signature · 1 line
2.53.0
PATCH 2/2 linux-user: report EFAULT for a NULL sysinfo/getitimer buffer
In reply to TAKANO MitsuhiroBoth handlers skip the copy-out when the guest pointer is zero and return success, so a caller that forgets the buffer gets an uninitialised result instead of the EFAULT the kernel reports for a NULL address.
Copy unconditionally: a NULL pointer then fails in lock_user_struct() and in copy_to_user_timeval() like any other unusable address.
--- linux-user/syscall.c | 4 +-- tests/tcg/multiarch/linux/linux-null-buffer.c | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-)
linux-user/syscall.c +2 -2
@@ -11551,7 +11551,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,struct itimerval value; ret =get_errno (getitimer (arg1, &value)); - if (!is_error(ret) && arg2) { + if (!is_error(ret)) { if (copy_to_user_timeval(arg2, &value.it_interval) || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
@@ -11666,7 +11666,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,struct target_sysinfo *target_value;struct sysinfo value; ret =get_errno (sysinfo (&value)); -if (!is_error (ret) && arg1) +if (!is_error (ret)) {if (!lock_user_struct (VERIFY_WRITE , target_value, arg1,0 ))return -TARGET_EFAULT ;
tests/tcg/multiarch/linux/linux-null-buffer.c +32 -3
@@ -1,6 +1,15 @@/* -* A zero-length getrandom() needs no valid buffer: the kernel never looks at -* the pointer, so getrandom(NULL, 0, 0) succeeds. +* Degenerate buffer pointers must behave like they do in the kernel. +* +* A zero-length getrandom() needs no valid buffer: the pointer is never +* used, so getrandom(NULL, 0, 0) succeeds. A required output buffer, on +* the other hand, must not be silently skipped: sysinfo() and getitimer() +* have to report EFAULT for a NULL pointer. +* +* The NULL cases go through syscall() rather than the libc wrappers: on +* 32-bit x86, for example, getitimer() hands the kernel a local structure +* and copies the result out itself, so it would follow the NULL pointer +* before the kernel's answer could be seen. * * SPDX-License-Identifier: GPL-2.0-or-later */
@@ -9,11 +18,31 @@#include <errno.h> #include <stdlib.h> #include <sys/random.h> +#include <sys/syscall.h> +#include <sys/sysinfo.h> +#include <sys/time.h> +#include <unistd.h> int main (void ) { +char buf[8 ]; +struct itimerval it; +struct sysinfo si; + +/* A zero-length request is not rejected for a NULL buffer. */ errno =0 ; -assert (getrandom (NULL ,0 ,0 ) ==0 ); +assert (syscall (SYS_getrandom,NULL ,0 ,0 ) ==0 ); + +/* A NULL output pointer is reported, not silently skipped. */ + errno =0 ; +assert (syscall (SYS_sysinfo,NULL ) ==-1 && errno ==EFAULT ); + errno =0 ; +assert (syscall (SYS_getitimer,ITIMER_REAL ,NULL ) ==-1 && errno ==EFAULT ); + +/* The ordinary calls still work. */ +assert (getrandom (buf,sizeof (buf),0 ) ==sizeof (buf)); +assert (sysinfo (&si) ==0 ); +assert (getitimer (ITIMER_REAL , &it) ==0 );return EXIT_SUCCESS ; }
Signature · 1 line
2.53.0