From: Helge Deller <deller@kernel.org>
To: qemu-devel@nongnu.org
Cc: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>,
Laurent Vivier <laurent@vivier.eu>, Helge Deller <deller@gmx.de>
Subject: [PULL 2/2] linux-user: report EFAULT for a NULL sysinfo/getitimer buffer
Date: Thu, 5 Nov 2026 23:55:46 +0100 [thread overview]
Message-ID: <20261105225547.12848-3-deller@kernel.org> (raw)
In-Reply-To: <20261105225547.12848-1-deller@kernel.org>
From: TAKANO Mitsuhiro <takano32@gmail.com>
Both 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.
Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
Signed-off-by: Helge Deller <deller@gmx.de>
---
linux-user/syscall.c | 4 +--
tests/tcg/multiarch/linux/linux-null-buffer.c | 35 +++++++++++++++++--
2 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 59cbf5feaa..2fc61b3eab 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -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;
diff --git a/tests/tcg/multiarch/linux/linux-null-buffer.c b/tests/tcg/multiarch/linux/linux-null-buffer.c
index 6246e47707..b099803c57 100644
--- a/tests/tcg/multiarch/linux/linux-null-buffer.c
+++ b/tests/tcg/multiarch/linux/linux-null-buffer.c
@@ -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;
}
--
2.54.0
next prev parent reply other threads:[~2026-11-05 22:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 22:55 [PULL 0/2] Linux user next patches Helge Deller
2026-11-05 22:55 ` [PULL 1/2] linux-user: allow a zero-length getrandom with a NULL buffer Helge Deller
2026-11-05 22:55 ` Helge Deller [this message]
2026-11-06 20:25 ` [PULL 0/2] Linux user next patches Richard Henderson
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=20261105225547.12848-3-deller@kernel.org \
--to=deller@kernel.org \
--cc=deller@gmx.de \
--cc=laurent@vivier.eu \
--cc=pierrick.bouvier@oss.qualcomm.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.