From nobody Mon Nov 23 04:26:32 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793480130; cv=none; d=zohomail.com; s=zohoarc; b=fiwZUHBw7XQTw4mE6dkBdps7VbFa/oa8I4VLaajg/eWftJpL19VBm8tBvfjPdE20t9OocbH9wWTL/awLaBpcnDZ7a+dvlMTHJTGxhjOE0BiVTbCO96WFwH/6/fdEkZqXzyRXXytTl70BGEklmDSBtmLCQsnLBUujuAU61XXgVbg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793480130; 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=khyefkDvxu9elgvS2Ym6JQ3MKBgTRdap+wvNkxs3UHw=; b=Lx0dI8HTRnZny1mToKHqlp4SVmSawDWmpLs2aEmqh8fvkrM9aLrhGeRGZrcJe1ih1LQGsnLJDgxeh3ZdOaLsy7pjPFWVp8Q5qcGOJrzklO533Td52kMvbkLagRB0mFVxbLjlq0flGo/qjWWvOXZc6YbHoJvQKQLAfzbVzFUe3dw= 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=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793480130583938.5221475370134; Sat, 31 Oct 2026 13:55:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNG6b-0000lk-Di; Sat, 31 Oct 2026 16:55:13 -0400 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 1xNG6Z-0000kq-Ax for qemu-devel@nongnu.org; Sat, 31 Oct 2026 16:55:11 -0400 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xNG6X-0004ig-LQ for qemu-devel@nongnu.org; Sat, 31 Oct 2026 16:55:11 -0400 Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-3ab752f6244so2534730a91.2 for ; Sat, 31 Oct 2026 13:55:09 -0700 (PDT) Received: from a1-free ([161.33.196.121]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ae87ee7765sm4772952a91.2.2026.10.31.13.55.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Oct 2026 13:55:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793480108; x=1794084908; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=khyefkDvxu9elgvS2Ym6JQ3MKBgTRdap+wvNkxs3UHw=; b=TEP2Iv1qhka7sPzOhy8zbQ1zDXJbbjJNsZg3EGeamo/zbxmdsRC7ljllfZ3wnL1xh3 aR+zzIUSHkIHxXIKp/ucgwdOMm15q393HKfrf+u+CgEpZoQgZGzzibqVpVTkjfyy6UAP a/7+/652wpXGMXkSu49ZMutMqDxrGEJpYcbvSrBy8t1NtdaeHpxhioNLHe/ghUSyC2FQ qXgkf3FGyroAqKV80AlC3UkhHDYUfqIKO3I7gwlgjCw7IILKKyGJNBFqD0v+SKhn8EtD pQN4RCAdzYxQ1YyV0UXyxxPtqUJarj3BiQrz/8UjpVE7/ZA0erNBP8K+53DiIj66LGnN W6pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793480108; x=1794084908; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=khyefkDvxu9elgvS2Ym6JQ3MKBgTRdap+wvNkxs3UHw=; b=0t2AyyLGizDv2mkEid7pWS2OKLTt5ZcNvoMVLeTs0p81qNwJF+mY4YJQFuFTtopR0i whpcow4c7DdZtF0Oi55gPkcDdn1IyHG4cwfG5YYR17rrMvCzDAgQigoT1Gq7UGFOd7MM zri7hqkbl2OAenkCR9i7jwIAMN2StUscqwnk1qVOt3ScVKVc7qdxw18rQwl5HSk2gMR5 +/HOR9GprmeGMe6se+Vw+a9cKT2WgE4PACwhjjsgrkCsugyL8ePnrj9vDnWxf7dQqABt 734TFWSCcIwElnkHnEqTexFr9L4ZnFXW34HVlrl/ZzTxigNkfqyPdfuQzy6RpLSNklSq J41A== X-Gm-Message-State: AFq9FYIHZpqLnd6OjPnB5SNY7MP32121tTzkTZttUt6YSKIJz/7GL3lP 2og4Y/00hWA7Jz6N/UfZrfJ7FfslalmVb4bd/Zv60TPJf/SYNM3n4Q14BLO+UQ== X-Gm-Gg: AYBFou2JhzkajCIjSR4gZvmMmaKoXwPSR6FE0C1iQgTVpqxkWqVZGlOJNqjHZmo3D77 ApsIYQvbNaz4a4r50JKkZ+MYmCz4PW/dSl9ZNj7id/IRoqTpw2Y+8yyHSoq/kCLpeDqhLOcsW9M BLRAqVx9ueXJkw11/LL7LTvrM9OWK6nzGaJP1soeiypXNa++HbxJ7aXh2Pv2K3022w+ovWN23dC o894JeIZaCYx8xW35xsVOXBbbgmsjkXw1kmWwHXPNqvYabqEedT+cHKRVeaOYMNCZfVCNi03maz 0NjqkDBPMpAgcx1gRBZb+CTYlMh0pq9vW6VQszI9vrKcX9zfG2Q2M1Cz5gMOCbFRx0aZWmVzuWL rOmlwe3vv3Djzesbq5sk1gE1ruLbk5JBrD4ArsLaEqdbIRggk9Q/OXpFP+GptBy01zKFV0COQ+g JcBoTrl89heefHz73MKi2eowCtNz7AMb73Idn0IjTHO6tevekh/l/mUoFJbft4wOK5i18xQK5nx 8/XkEnUo2cAe3K0b0en533w8Vk3gblvKk3g9kM8wi9qLAtP8moCApync2JVZJUVjK3pQPSGYssD CTNJbZ7nQdjhFfG+9AVm X-Received: by 2002:a17:90b:3d4b:b0:3ad:eb4c:26fb with SMTP id 98e67ed59e1d1-3ae897c87acmr5003008a91.23.1793480108162; Sat, 31 Oct 2026 13:55:08 -0700 (PDT) From: TAKANO Mitsuhiro To: qemu-devel@nongnu.org Cc: Laurent Vivier , Helge Deller , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH 1/2] linux-user: allow a zero-length getrandom with a NULL buffer Date: Sat, 31 Oct 2026 20:55:00 +0000 Message-ID: <20261031205501.1988785-2-takano32@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261031205501.1988785-1-takano32@gmail.com> References: <20261031205501.1988785-1-takano32@gmail.com> 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=2607:f8b0:4864:20::102d; envelope-from=takano32@gmail.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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 @gmail.com) X-ZM-MESSAGEID: 1793480132166158500 Content-Type: text/plain; charset="utf-8" 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 --- 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 9d85a193efe..59cbf5feaad 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 00000000000..6246e477070 --- /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 e60a0527007..8f2f57d9ab4 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.53.0 From nobody Mon Nov 23 04:26:32 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793480149; cv=none; d=zohomail.com; s=zohoarc; b=TGPGThHrfuqL82IZVWJQd+ukDDFT+1rNk07pzj3LXXy3EPm0xxSnNky/EeQ/tYri9xyx/LWuQpB9Z2gmtUB579+wTs8R/5qiXrhfM0xOB8+xCs+EDes/7MCDkyH90OGVww3UTu6PKQncv2upVAtczFkIGL9x5BcbVcHHv0bxK3s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793480149; 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=gFVk3oih9DS0LLY5asZ/G/Ce34H8tbZKr/F/Tvew0YQ=; b=Ae5Bk6K7h83qHDgf03ri7LVktkkBe9yjs/1M5xr6vOT0TjnX+aDLkUHc3niEssOrk2uLPsVKzv1l6pUMnQQswbyemdZovJveGYl8kidfMGWrCvBwd9cgROEnh1zClDtGAD+oQrGz+mjpnNnYkA3zjQ6oRifcDEHn111b4tV89xE= 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=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793480149009792.2839412232536; Sat, 31 Oct 2026 13:55:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNG6e-0000ma-0M; Sat, 31 Oct 2026 16:55:16 -0400 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 1xNG6c-0000ls-74 for qemu-devel@nongnu.org; Sat, 31 Oct 2026 16:55:14 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xNG6a-0004ix-Gn for qemu-devel@nongnu.org; Sat, 31 Oct 2026 16:55:13 -0400 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-3ad41b47541so1525290a91.1 for ; Sat, 31 Oct 2026 13:55:11 -0700 (PDT) Received: from a1-free ([161.33.196.121]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ae87ee7765sm4772952a91.2.2026.10.31.13.55.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Oct 2026 13:55:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793480111; x=1794084911; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=gFVk3oih9DS0LLY5asZ/G/Ce34H8tbZKr/F/Tvew0YQ=; b=FTM336SasaH5oHfS242n9fyaMA8U3yxUSLxiUYomMvJ5X619xO3TNlCcR4zczHr2Kp EevFPv/C7v7ZQeYQVBiHpZzhzbodjABy7WGZAvLTShu1FqV92nXIQfAukU5xgxKZoNzO uOoCwGAl1oJR7+nKGub6OmdqqP2H96Oon6yvEY47AEDzlX2DqEBT9g/pWYyEpqCW9VNU N0w6wypZtmFr3BdOiO/QIT8Z2IJ/0LYfZJp2n767dZkGNEob3+jWPk+SvVFSixH/MDru n9bMp34ot2WQj3T2SkUbe6dfZ9mzD9BKSn4zX7qjhiZ3w+/SBp6zqAVsZq3hYOvZE3Nd IjIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793480111; x=1794084911; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gFVk3oih9DS0LLY5asZ/G/Ce34H8tbZKr/F/Tvew0YQ=; b=rEHHkab3dCBGYJJIfnJcl09khJmy+b6gKRcjBj4mzrqhxm39TjlaNhWWKxMZWPfP+L nHaXa/P9LmyTg42NVfIo4StTFo/8LtAfWlKtqvtOHdtlNOSCVyhn+qQYIfCIby/GZ6s6 8iVnlHb/xtzXPtB1tfr9qFSW1iq+izkepk7T4A27xkOHVamOLepuxWRv467r7evJnQGE mK2ju67JccMGr71ICpE8OPM14zDX6DKFBkxSCpHYiVq7Jm/zl/6Gf6S63MQScxJB82KQ YdhiqxsdTJJgMnS6mY0AmtWsfyThfHijUAINd2DFKsrvLHKuwcDP817RYili/n5QqLAp IVDg== X-Gm-Message-State: AFq9FYKrurNAK3Yy29QEtKQjsi+31jElUrx0QsTTeHEktOtWzVfkER2E yUc1AC5lpg6tYJ3btdHX3kulP2yxmGRfoA0zbvKYF0iD1UIx+21u8CIIe8mKWQ== X-Gm-Gg: AYBFou02q4iIRdRn0YyjhrS2418ekQCGOUlgHbaka2gWEPEa/znyTxCi2nfhCwmIkY3 5UtZ0ACbLqUJjNchR3A9Dwq7vYPvlGagskBYLdk6vB9E6sBkgbosGckncJe56avtPJ01WwETTrM HOHmia5NzIJy7Cbfl93Z/zWYmQacSoh3mSif+8AgQkMrdDPa8w098RANrv1a2F7WGs1VDkaxkrR mV60Xeh4g08IGIa60y6ADHrQTDGH60+cjSZKlgekkUvbyNCcgPl7YLHNvxz8/y4KqpiEeK94caB hmFRFuUA0mi4r+LgVvNdIixe5XviAawUAp/9UTxURMsIhQt4GruGY6Pup2gGfVfw7922utH8eUt LUvNysBpY3fUklDCl8dh5tZaHNQ8pzd7hnw922Ln4QitONBIptnqceLxtpcjBpfcyl0bN84Z8Kg /c7J3wLWxPHZFtsjMUjjShntdcnz0z1Xs1NBS1KlmFJgmmoaZBfH+6qNELGGbZpc/cbWufX92b5 eehd6fH/YodeBhqh7+3KWFP5v9lRbfi6Jhmdsy+QADrYmSwHHIEpgZbY5TTxbKs0vHbbMUt7jRI kz38Lm0XqvT6brkpCNP6shFTcUbfiyvF5GoA+pNuiA== X-Received: by 2002:a17:90b:3b85:b0:3ab:188b:c5b1 with SMTP id 98e67ed59e1d1-3ae897e4182mr5100681a91.56.1793480110720; Sat, 31 Oct 2026 13:55:10 -0700 (PDT) From: TAKANO Mitsuhiro To: qemu-devel@nongnu.org Cc: Laurent Vivier , Helge Deller , Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH 2/2] linux-user: report EFAULT for a NULL sysinfo/getitimer buffer Date: Sat, 31 Oct 2026 20:55:01 +0000 Message-ID: <20261031205501.1988785-3-takano32@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261031205501.1988785-1-takano32@gmail.com> References: <20261031205501.1988785-1-takano32@gmail.com> 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=2607:f8b0:4864:20::1030; envelope-from=takano32@gmail.com; helo=mail-pj1-x1030.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, 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 @gmail.com) X-ZM-MESSAGEID: 1793480152279154100 Content-Type: text/plain; charset="utf-8" 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 --- 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 59cbf5feaad..2fc61b3eabc 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 6246e477070..b099803c574 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.53.0