From nobody Tue Feb 10 07:57:40 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1558553180; cv=none; d=zoho.com; s=zohoarc; b=fNMGhIvnvxldB/UD0trrW7mMP0rgLYh1Q6zMZJZZTTAz56FELhQOvNUsEK8oCxnELTSb+DUewf7+cIogsqTrdU6YI9KMpMdxBXPSl88BIg1B6zt50CUsE9r6E54ZQh1/5GxjH1wI6pkD/eTH1NwTejJU7n1u54Pqc2mrfU7aTrI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1558553180; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=C5yUHyNINPBjASzQcl4pblPDsyyBX7i3Ae47r1N2nnw=; b=ZMWmnYYbi+NU+4z7g5WDhlCtY13HnUgZc5e6nL/OhOQUxRW/JdAzZEFbhrY6Gy9FWe5mqBbpe4YJbbP6k/kshhdcsfVru38G/+2Mh9s92DKtyphx4X87XgIRPagmYujVa2fWwCoQeQxhI0RKETtnu3pcpPSzbqah915xNDCIogs= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1558553180662699.5508453590434; Wed, 22 May 2019 12:26:20 -0700 (PDT) Received: from localhost ([127.0.0.1]:50098 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWsQ-0001sh-8u for importer@patchew.org; Wed, 22 May 2019 15:26:14 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35163) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWOr-0008Ln-AP for qemu-devel@nongnu.org; Wed, 22 May 2019 14:55:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTWCB-00083L-Mj for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:37 -0400 Received: from mail-yw1-xc2d.google.com ([2607:f8b0:4864:20::c2d]:44154) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hTWCB-000831-Fw for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:35 -0400 Received: by mail-yw1-xc2d.google.com with SMTP id e74so1231708ywe.11 for ; Wed, 22 May 2019 11:42:35 -0700 (PDT) Received: from localhost.localdomain ([71.46.56.17]) by smtp.gmail.com with ESMTPSA id 206sm1299445ywq.74.2019.05.22.11.42.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2019 11:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C5yUHyNINPBjASzQcl4pblPDsyyBX7i3Ae47r1N2nnw=; b=MO9ov4iCjqTqWRSLRa+MMwhyhfNR8J5CBshBSM5RXUHwcsALhRWX98E3nGvBo9ftIR pzkUS6p6DRHN0OOGS1rjffopEnc3uPbHnrmQuZn/lu/HFcK6fdkKn1CP4lWTtvsP8ISb nDOn25cLJMB9WWompAIarJ8XnHwuxKggFDnkRCMmrz9o6q/hrToATasb3VxYRz8choc8 G4EYHNBIyQ+5f1oDNWsAjJAw6yt7vSijU6jmdEi6nvw7EtG8GWYC2zFkGICS9cXI4B2g UrmI9Hfn+0F0aUZyE2ZndPZuAlhuDw2twcABIgo3wWssj/BsoPyW3kVr3dXMBtks+Yay y45g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C5yUHyNINPBjASzQcl4pblPDsyyBX7i3Ae47r1N2nnw=; b=rD2uCK3OSdzIw0PggGYZl6yH0MzY2QeFmrx2P62X9i2rP5N+4STxmgGZvk1NV2cj3n Lw/0tHkqnPN/ECYponP+xSfs2iKun/WTg5LDS74ib0xi8wA9UBwHg+ckY3H916+/uEDu jgrc0VNFkTj0O+pPX82pReUF20R8di2s4LuD5fljxTbyjYVFg+P693X1CEch/lcP62jy /u6lOQCsI2UlvcVuot/VjmFe0hn1e+51LeWRvP1dxjGUGHVJMW2NOCQSAh1jWl37oWus TGc5j9kxQEaGd0xDfl2W030W45riGc+sTVluwv5HxVa3dbpkxXbAXQ+z1uz2/DfveTIJ 1/dg== X-Gm-Message-State: APjAAAXLpiFCsIPk4EbPZ20BtzHwc1A1lVqNtIwpKZEeEdWuBc9zbeoT nZqIwf+tGNi7uBeAN7fV3HwJewJ6JCI= X-Google-Smtp-Source: APXvYqwhdJetAXM6PCwdhzYNSGRxZVmZip0OVj2tBq2ddChQuUScEheq1Sx2eurnFoxGCBRzcweWRA== X-Received: by 2002:a81:7246:: with SMTP id n67mr15937514ywc.423.1558550554455; Wed, 22 May 2019 11:42:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 22 May 2019 14:42:07 -0400 Message-Id: <20190522184226.17871-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190522184226.17871-1-richard.henderson@linaro.org> References: <20190522184226.17871-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::c2d Subject: [Qemu-devel] [PULL 06/25] crypto: Use getrandom for qcrypto_random_bytes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Prefer it to direct use of /dev/urandom. Reviewed-by: Laurent Vivier Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Richard Henderson --- crypto/random-platform.c | 37 ++++++++++++++++++++++++++++++++----- configure | 18 +++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/crypto/random-platform.c b/crypto/random-platform.c index 6df40744c7..cb3ca1bc09 100644 --- a/crypto/random-platform.c +++ b/crypto/random-platform.c @@ -27,7 +27,11 @@ #include static HCRYPTPROV hCryptProv; #else -static int fd; /* a file handle to either /dev/urandom or /dev/random */ +# ifdef CONFIG_GETRANDOM +# include +# endif +/* This is -1 for getrandom(), or a file handle for /dev/{u,}random. */ +static int fd; #endif =20 int qcrypto_random_init(Error **errp) @@ -40,15 +44,20 @@ int qcrypto_random_init(Error **errp) return -1; } #else - /* TBD perhaps also add support for BSD getentropy / Linux - * getrandom syscalls directly */ +# ifdef CONFIG_GETRANDOM + if (getrandom(NULL, 0, 0) =3D=3D 0) { + /* Use getrandom() */ + fd =3D -1; + return 0; + } + /* Fall through to /dev/urandom case. */ +# endif fd =3D open("/dev/urandom", O_RDONLY | O_CLOEXEC); if (fd =3D=3D -1 && errno =3D=3D ENOENT) { fd =3D open("/dev/random", O_RDONLY | O_CLOEXEC); } - if (fd < 0) { - error_setg(errp, "No /dev/urandom or /dev/random found"); + error_setg_errno(errp, errno, "No /dev/urandom or /dev/random"); return -1; } #endif @@ -66,6 +75,24 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, return -1; } #else +# ifdef CONFIG_GETRANDOM + if (likely(fd < 0)) { + while (1) { + ssize_t got =3D getrandom(buf, buflen, 0); + if (likely(got =3D=3D buflen)) { + return 0; + } + if (got >=3D 0) { + buflen -=3D got; + buf +=3D got; + } else if (errno !=3D EINTR) { + error_setg_errno(errp, errno, "getrandom"); + return -1; + } + } + } + /* Fall through to /dev/urandom case. */ +# endif while (1) { ssize_t got =3D read(fd, buf, buflen); if (likely(got =3D=3D buflen)) { diff --git a/configure b/configure index 54fa78c4dc..f3526a5bca 100755 --- a/configure +++ b/configure @@ -5815,6 +5815,20 @@ if compile_prog "" "" ; then have_utmpx=3Dyes fi =20 +########################################## +# check for getrandom() + +have_getrandom=3Dno +cat > $TMPC << EOF +#include +int main(void) { + return getrandom(0, 0, GRND_NONBLOCK); +} +EOF +if compile_prog "" "" ; then + have_getrandom=3Dyes +fi + ########################################## # checks for sanitizers =20 @@ -7204,7 +7218,9 @@ fi if test "$have_utmpx" =3D "yes" ; then echo "HAVE_UTMPX=3Dy" >> $config_host_mak fi - +if test "$have_getrandom" =3D "yes" ; then + echo "CONFIG_GETRANDOM=3Dy" >> $config_host_mak +fi if test "$ivshmem" =3D "yes" ; then echo "CONFIG_IVSHMEM=3Dy" >> $config_host_mak fi --=20 2.17.1