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

-- 
2.53.0

Reply via email to