From patchwork Thu Mar 14 04:55:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160292 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp15077594jad; Wed, 13 Mar 2019 22:04:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqwFmBtc3orlgPsiPDyr/lEfTPyisou/N4nHSEp5UqhXHdzWV/m2hF9kIRFhDnR/VVgdRxPF X-Received: by 2002:a0d:dbd2:: with SMTP id d201mr37549584ywe.484.1552539872207; Wed, 13 Mar 2019 22:04:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552539872; cv=none; d=google.com; s=arc-20160816; b=k7JedxlHStzBTEradLKXmLpxeS6ZBiJJPEh3bRsLNL9YdOAIN/+a3O1jAWhQEM9slv L/ymYmsWnGxfsAgWqSie9N4I3mAudGf5GTwPfdpIcwBTR6MqB33JOEoC36NmKWM+ttex 13PhAXEyWwTUfeqKYQL23HjY2MvqIJrrMQgK0vlE3M19ktJuiqItWS4MjhfkZVpqXQoO g8Y7N3P285HDlWlOx4xQL0hTH3Ttmb5SvEuUpvgeDSI9CteNHyg+hRdhOo2NFwz23YeQ ffnOswHA4xtx7geUhouGN979jwPmcIxGqUFc2eQHfvSj7qffsvhSI+AUEGjXnfUZrEMD D9ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=CoYKWtcbjnkf8ec29DwDvyGtEftw+7AvJg2LPmquNfI=; b=ZVaD6pCqcewmVK/m6r2eBnzYWqlhyUpp/kxW6ND+pJyLOylWdeAWV9aRpR8v7EJRSu 7NFgucaBH4vDc344IzbOEv/NK4TaB12M3kQ28vJmCmA9p4WNNA0dACRzEKuiMvRCILeS fYV876Glj9Sp088ZvEAkzC6OEiMkeNPn0bq56C85idrU/7NSdXRBBXsOq7BzGaMShoZP xXhcsKqhiDblIsy+RyDkWVa+/15fNqRJ6Knj+d+rd+nBdl68FhXu0onFcmd0E5FYim1D lLMgglw1v1LYFYamNaLTxjCR8prqxlss0B/3ihk7+HmaOwf7rZmymooBQThweAS+A/+Q rDbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=wxlZzNoJ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 206si7890454ywo.44.2019.03.13.22.04.32 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 13 Mar 2019 22:04:32 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=wxlZzNoJ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:56948 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IXf-0002nV-N3 for patch@linaro.org; Thu, 14 Mar 2019 01:04:31 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56149) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IP3-0004Vq-J9 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4IP2-0007Ns-K8 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:37 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]:35829) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h4IP2-0007NO-CJ for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:36 -0400 Received: by mail-pf1-x432.google.com with SMTP id j5so3019677pfa.2 for ; Wed, 13 Mar 2019 21:55:36 -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=CoYKWtcbjnkf8ec29DwDvyGtEftw+7AvJg2LPmquNfI=; b=wxlZzNoJBsCaR2oNNS1aUsFy7iv2g/iyxGCts959DJO0xVRFNZC8o5GQb9UJfWbqgL KJhZsSFYl07vr4oriN2AphT2WuTHqzaSbSt+N1tpd1OhqTXK6vIh0djo1CvPOIeVBOwV PqtmVo9bcVDeruzoUx06+GH9r5JaUfgkxFpUR7K4aeifyMCWyHBSDJ0w0TIgDSiJUlEG UMQb57RPVVcG5TnHNBrS1+fOLCbnu87JowSPTzGCeuK8xosTilw6z7L5yLDTdm76hKzE snFgOoi61dQJh5pLaE0aqzDSGH5xjeK0iYucFxH4v7WiHb0FSBB4Of3yfM92AIjxlGr4 6tvA== 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=CoYKWtcbjnkf8ec29DwDvyGtEftw+7AvJg2LPmquNfI=; b=IDhbWgTfx+AupL1zigsn/0X2u8ihP54lWu8z1RcSCZVr6+q+7PLWGY4Kw5NEegHknT SlOk4mHWBvnJOnBaa5c0eCz+Zn06/WZcmddieLR8xYq/1lKCyLnorinDVgA88Cv+saNf VhiP+rTNQCG6Qw/XqooMTWQ/sO4ZD3yt844ik53hklER7x+IO61O7GspjLkl3LxmaBte dCoOaLn4clG68bOvoD5oa15urFLVH3hPbCYc3v1Qd2W7YZY54zNUWM0icyLFs7W09qjL nJdb4IT1J/r47Fq+6BkG0mYn3nIquWaypvxk7j9h6cllGdA/5297v/JEAYIWYc5upx2D jtKQ== X-Gm-Message-State: APjAAAXiSnU0q/ixB3FW0DKF52K+Stbjy/MzdZzUJai/w55tDK/Uvb+M tf2WWhaskkNRM9TwSsWKJIjAgDZdrY4= X-Received: by 2002:a63:4c13:: with SMTP id z19mr17730178pga.71.1552539334932; Wed, 13 Mar 2019 21:55:34 -0700 (PDT) Received: from localhost.localdomain (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id d9sm16185027pgq.6.2019.03.13.21.55.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 21:55:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 13 Mar 2019 21:55:17 -0700 Message-Id: <20190314045526.13342-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190314045526.13342-1-richard.henderson@linaro.org> References: <20190314045526.13342-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::432 Subject: [Qemu-devel] [PATCH v2 05/14] 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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Prefer it to direct use of /dev/urandom. Cc: Daniel P. Berrangé Signed-off-by: Richard Henderson --- crypto/random-platform.c | 21 +++++++++++++++++++++ configure | 18 +++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/crypto/random-platform.c b/crypto/random-platform.c index 8bfce99a65..bdaa8fbbfb 100644 --- a/crypto/random-platform.c +++ b/crypto/random-platform.c @@ -26,6 +26,8 @@ #ifdef _WIN32 #include static HCRYPTPROV hCryptProv; +#elif defined(CONFIG_GETRANDOM) +#include #else static int fd; /* a file handle to either /dev/urandom or /dev/random */ #endif @@ -39,6 +41,12 @@ int qcrypto_random_init(Error **errp) "Unable to create cryptographic provider"); return -1; } +#elif defined(CONFIG_GETRANDOM) + errno = 0; + if (getrandom(NULL, 0, 0) < 0 && errno == ENOSYS) { + error_setg_errno(errp, errno, "getrandom"); + return -1; + } #else /* TBD perhaps also add support for BSD getentropy / Linux * getrandom syscalls directly */ @@ -65,6 +73,19 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, "Unable to read random bytes"); return -1; } +#elif defined(CONFIG_GETRANDOM) + while (buflen > 0) { + ssize_t got = getrandom(buf, buflen, 0); + if (unlikely(got < 0)) { + if (errno != EINTR) { + error_setg_errno(errp, errno, "getrandom"); + return -1; + } + } else { + buflen -= got; + buf += got; + } + } #else while (buflen > 0) { ssize_t got = read(fd, buf, buflen); diff --git a/configure b/configure index 8992b3aade..6a32284d26 100755 --- a/configure +++ b/configure @@ -5783,6 +5783,20 @@ if compile_prog "" "" ; then have_utmpx=yes fi +########################################## +# check for getrandom() + +have_getrandom=no +cat > $TMPC << EOF +#include +int main(void) { + return getrandom(0, 0, GRND_NONBLOCK); +} +EOF +if compile_prog "" "" ; then + have_getrandom=yes +fi + ########################################## # checks for sanitizers @@ -7170,7 +7184,9 @@ fi if test "$have_utmpx" = "yes" ; then echo "HAVE_UTMPX=y" >> $config_host_mak fi - +if test "$have_getrandom" = "yes" ; then + echo "CONFIG_GETRANDOM=y" >> $config_host_mak +fi if test "$ivshmem" = "yes" ; then echo "CONFIG_IVSHMEM=y" >> $config_host_mak fi