From nobody Mon Nov 23 04:34:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1793919412; cv=none; d=zohomail.com; s=zohoarc; b=e0E4o3YHQf8aPCjeGKZfweR5k4Ww6piFj3xNI1NR2spj+ifUOUFdN1eNk+F+8GFL8pNwe2wozIcwOaEHv3mdsaSMACVXLZ4k+RiQcHAy3D4QvBYsXPJ1m6cxycWpCLmLRfCL5FoJe3Ak/5gJywjm3r/2Ru6tACDwKC27K6B0nfs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793919412; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=OT5xRW8A58o2SKIBu0eKtKZMV1p43VIrNvyL5ye7+h0=; b=hJacemMPmAl4lZQZBwO03VqLidl0/lLfqy0E8/F5oYaM6b+PS0wzOfcdcgg09gIi1990s1WpQCbhCHDGJXYd6brl6ActKwOV1DmAgFoEpABZO62EeT2RQnb2/F5HP1cI8jMG4BurQptRPbC2DFHsKv1hvlGaf8V9I+nuy+j8I7Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793919412460397.86247082962973; Thu, 5 Nov 2026 14:56:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xP6NT-0001Jw-Bw; Thu, 05 Nov 2026 17:56:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xP6NB-0001IG-8x for qemu-devel@nongnu.org; Thu, 05 Nov 2026 17:55:59 -0500 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xP6N8-0004W7-1R for qemu-devel@nongnu.org; Thu, 05 Nov 2026 17:55:55 -0500 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 64973413CE; Thu, 5 Nov 2026 22:55:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 489F41F0089F; Thu, 5 Nov 2026 22:55:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793919351; bh=OT5xRW8A58o2SKIBu0eKtKZMV1p43VIrNvyL5ye7+h0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YXSc9LR8Yc+heEgU40lOSqABtKhbq2XFVZBanvARkFK6/Jq49aNqnw2BeGG+5aits O/r7AubSZa1SolQS4K9UOR5C6SerUgOL+cFfWzjOkvgdyt5kUH4qHcw6+0InMWXGb2 ghS43C4RyxoH3DWkGS7HsAArXf5zy4tQL0yyJD3B1Hot3ljQoLWzv0LHDxCu9VUTB4 5H1I5RXY/Qacx4V1oxDhkbE57IW4I+G2pKf95FthqnWl7bFzSqhnbOzOmOfqqlEv3H OnIjh7gSi37aEZAanI8fY3mEp3mwmxmzlwaD6br/o8AZiLUGPfB8ieqybnnSeQleeI avjbOK91lCB3Q== From: Helge Deller To: qemu-devel@nongnu.org Cc: Pierrick Bouvier , Laurent Vivier , Helge Deller Subject: [PULL 1/2] linux-user: allow a zero-length getrandom with a NULL buffer Date: Thu, 5 Nov 2026 23:55:45 +0100 Message-ID: <20261105225547.12848-2-deller@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261105225547.12848-1-deller@kernel.org> References: <20261105225547.12848-1-deller@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2600:3c0a:e001:78e:0:1991:8:25; envelope-from=deller@kernel.org; helo=sea.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1793919412961158500 Content-Type: text/plain; charset="utf-8" From: TAKANO Mitsuhiro 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. Fixes: f894efd19917 ("linux-user: add getrandom() syscall") Signed-off-by: TAKANO Mitsuhiro Signed-off-by: Helge Deller --- 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 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 9d85a193ef..59cbf5feaa 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -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 =3D=3D 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 =3D lock_user(VERIFY_WRITE, arg1, arg2, 0); if (!p) { return -TARGET_EFAULT; diff --git a/tests/tcg/multiarch/linux/linux-null-buffer.c b/tests/tcg/mult= iarch/linux/linux-null-buffer.c new file mode 100644 index 0000000000..6246e47707 --- /dev/null +++ b/tests/tcg/multiarch/linux/linux-null-buffer.c @@ -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 +#include +#include +#include + +int main(void) +{ + errno =3D 0; + assert(getrandom(NULL, 0, 0) =3D=3D 0); + + return EXIT_SUCCESS; +} diff --git a/tests/tcg/multiarch/meson.build b/tests/tcg/multiarch/meson.bu= ild index e60a052700..8f2f57d9ab 100644 --- a/tests/tcg/multiarch/meson.build +++ b/tests/tcg/multiarch/meson.build @@ -57,6 +57,7 @@ tests +=3D { # Linux tests tests +=3D { 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': {}, --=20 2.54.0 From nobody Mon Nov 23 04:34:04 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1793919405; cv=none; d=zohomail.com; s=zohoarc; b=UjZMpwrsQa0MUpyQ3fpUaEjaBIYEDkglj1yhXJiv6hT8Y4mWBzFdTW0dY3wVNfqXE40Z0FunA6lxzoNPPxj4+u4JSsUXB+1rJrbgiikLhMYAQCdSZkYiX9AF2AFMDC+HVcqAZjjOhWxmX/3WbFXDxd41x44Y57YY0yKvnXphFPA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793919405; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=S4MYz3HmpV3zuE4set2Zxrehvqdp40xYuvdeqdddayM=; b=WEmcHZa7agtnKQ4Q7/huB9vDIob7XGHtHXviZ6Du9+b+U/VI91A0RCjqEr13FRD9+4VhqbCew45atpHChLiL2IysdwQW0hZz6nY/WEblfoXTIVt+Qm7UnKpq2JWB8c75lUf/8/jIy8BSYXwHBNKf1oO9/WLQlOuLhS759TfUOso= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179391940483198.0876556611048; Thu, 5 Nov 2026 14:56:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xP6NT-0001Jg-0e; Thu, 05 Nov 2026 17:56:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xP6NB-0001IH-8s for qemu-devel@nongnu.org; Thu, 05 Nov 2026 17:56:00 -0500 Received: from sea.source.kernel.org ([172.234.252.31]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xP6N8-0004WC-3V for qemu-devel@nongnu.org; Thu, 05 Nov 2026 17:55:55 -0500 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id CB6C9401BC; Thu, 5 Nov 2026 22:55:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0E471F0089F; Thu, 5 Nov 2026 22:55:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793919352; bh=S4MYz3HmpV3zuE4set2Zxrehvqdp40xYuvdeqdddayM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=M4GBTQXIaUX3NSYRsQ0e79gE194mJrlmDdzDTPRd6t5i9M59InijGDrkNBqlm5pim mllrbS9Yx+Se+SoGNy2e6Ih/1eZnDA/IfSBJ9u1cNHMEsn+LfrH1Yc8phqZprxkegH JDjWg+w5rawVJ4o7SbzdyAgvHvQfrwIJASCEL5/nO6mVn9oNgq1wMzdwQQoW3jEEPs 1z8Ffm/jEJMqlfM8+7UdfKrpBCq0nhVSDEyRwr7aRfg6u89iQFSyDAH0IzPERU9OrB jhovCEejEgC9NhIgeH2vImz4waAboHd1qGIsLzKyWH2qc4YtCrQxWVWvLVnJiEfy1l RGGyduDtktfLw== From: Helge Deller To: qemu-devel@nongnu.org Cc: Pierrick Bouvier , Laurent Vivier , Helge Deller Subject: [PULL 2/2] linux-user: report EFAULT for a NULL sysinfo/getitimer buffer Date: Thu, 5 Nov 2026 23:55:46 +0100 Message-ID: <20261105225547.12848-3-deller@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261105225547.12848-1-deller@kernel.org> References: <20261105225547.12848-1-deller@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=172.234.252.31; envelope-from=deller@kernel.org; helo=sea.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1793919406938158500 Content-Type: text/plain; charset="utf-8" From: TAKANO Mitsuhiro 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 Signed-off-by: Helge Deller --- 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; =20 ret =3D 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_ti= meval), @@ -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 =3D 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/mult= iarch/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 #include #include +#include +#include +#include +#include =20 int main(void) { + char buf[8]; + struct itimerval it; + struct sysinfo si; + + /* A zero-length request is not rejected for a NULL buffer. */ errno =3D 0; - assert(getrandom(NULL, 0, 0) =3D=3D 0); + assert(syscall(SYS_getrandom, NULL, 0, 0) =3D=3D 0); + + /* A NULL output pointer is reported, not silently skipped. */ + errno =3D 0; + assert(syscall(SYS_sysinfo, NULL) =3D=3D -1 && errno =3D=3D EFAULT); + errno =3D 0; + assert(syscall(SYS_getitimer, ITIMER_REAL, NULL) =3D=3D -1 && errno = =3D=3D EFAULT); + + /* The ordinary calls still work. */ + assert(getrandom(buf, sizeof(buf), 0) =3D=3D sizeof(buf)); + assert(sysinfo(&si) =3D=3D 0); + assert(getitimer(ITIMER_REAL, &it) =3D=3D 0); =20 return EXIT_SUCCESS; } --=20 2.54.0