From patchwork Mon May 6 17:33:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163428 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:81:0:0:0:0 with SMTP id l1csp260593ilm; Mon, 6 May 2019 10:42:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqzxLmbcYtVuvejgZS9Bj620azpliiqXyzw5llVCRokH/lBamOlhmIt7CIQsxYOTuOvg9vZC X-Received: by 2002:adf:f68c:: with SMTP id v12mr19596486wrp.40.1557164565036; Mon, 06 May 2019 10:42:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557164565; cv=none; d=google.com; s=arc-20160816; b=KHjhxxRG4SSVUOz49073o8EVc5yhgMy61HaA6yAsvQRVTKKYzyoexV5nEfs2kU5Ada ztWdVuKxmgvJ+Zt20syWZMdJ7j6KQDjWHeAF3E54hLaQigF0wUs8vkD0faarNT+Ms9O0 YNpzGyBuGnpX+2jlOACzkfOS5QU4i6eRvENHQKBX90a5Uk9yD6OrE+CEw560ShY3jFps +TQ5Busvg/hymU+ugKWSi3xx9CnbRgnCGqn2+bmfbNDogHuiemsG98n3rMvgox8Sf07A 7j1mYOP3MKhcCxN8bHk/rvyIy7LVdgDQAhTABGo1hjyE+Bni4olIk+rUAxvY8mPblzdZ U2ow== 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=esNllaMlu2hXfZ1dTWICAYQAatBfGQUsBlaS7ORBn8s=; b=UyouThwMeIqwbMOX0tzwSGBqTJNTmL67LOXCD0NjYjIreMfQSJhTsmG0fKOx6Y9Ltp iffnnzxkxrZ3vvDA/o+Ck4k2HtxLPw0v2++VOQ4sVuc3i33lVepKepMO9/N4yc/WSggC ExO9tE7WokOtHiN3iUll/plL/NrO8JAROb7NiRaZWWsLdpNC10C9hTeiqy5+7W/0/5oj ueFqd+nIhd+yz8TdjeVkzKWVPLcJS63LRxdb2D4rFYseMOyBCxyjmNWbwnhx/oeUBKws pRjI4x4Py8NRYbOG8Bn/3QwFsg4Npr1Zt70vxa+38k+0j5OL6ZmFHVXj5hRzfKGcCW92 Gr5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=CblyidrE; 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 q16si7812745wrr.110.2019.05.06.10.42.44 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 06 May 2019 10:42:45 -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=CblyidrE; 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]:60011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhdU-0000t0-1F for patch@linaro.org; Mon, 06 May 2019 13:42:44 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39828) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhVA-00012F-HW for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNhV9-0001J2-DJ for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:08 -0400 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:42676) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hNhV9-0001G2-2p for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:07 -0400 Received: by mail-pf1-x442.google.com with SMTP id 13so6821932pfw.9 for ; Mon, 06 May 2019 10:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=esNllaMlu2hXfZ1dTWICAYQAatBfGQUsBlaS7ORBn8s=; b=CblyidrERYrSmUe/QNFM9pDuBnu2GOQWyCiYAJ1ahITmWOQMAhWl8z7DQQRlvnJ0EU iQemffNZvxh+sMsxlE6gU319NQvPhxI6oscrfJUe4pNu2e+bK/NvsNlGUezBzggClEPn pe/4qhwwvD8iklYbL3/0FUF6IEfMr7Lpner8Bx/vy6FQ3Lytt6kXQf7O9Qi2gTvVN79A meIXfSOl12vtTjO6Fz3sdZV/1ixL1av8QT7hkZp+62QsQfZHSvby54HrNZ4KdUhbSeZ6 CwPg2ih+X4WtuFSVZMqsZMbLIgd4PUna/4y1w4DppjzNoTtR1UBCvGHjbMknd6Bodat8 V9qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=esNllaMlu2hXfZ1dTWICAYQAatBfGQUsBlaS7ORBn8s=; b=CronMq5aCkp8InOU6uCBh/JYUc39zxocrF2JYgjXhq8elGN1dfJVyzEFrGf/piHqDD Eowx7durjiFH7njyCgOqJ1M6cnA07Wpl4mGGUVlAUUqkrNa46dw5/n6b00VKlvwWI26u nN+hBjxFlegfhLxLM7OYGh0TLbQjunjv8Qnj6p7a1bJJXzMomnpVz5CUW9iTyheizrdH Q4RWY6lThwcqUNIJ8UsKLdruAEOOBNZpqtc8k9En1HxyRq3ZhZyzV8OqVYAPGhmCkph6 P/KuaSn7/KwBWSTB1tUvO0OiWCW9sludWCq6y3OBnc/Ep3s7T6M4PJs5uZqpt4JjuzaD LgpA== X-Gm-Message-State: APjAAAWgcTo4nUaLYMeevjzBc8i4g5qLQThbINOEBQrKdBrRNhmVa2hu eRMx0XK1f4EJX7g///JImWI8MYpJ4jk= X-Received: by 2002:a63:e048:: with SMTP id n8mr33581286pgj.41.1557164042881; Mon, 06 May 2019 10:34:02 -0700 (PDT) Received: from localhost.localdomain (97-113-189-189.tukw.qwest.net. [97.113.189.189]) by smtp.gmail.com with ESMTPSA id k9sm2268839pfa.180.2019.05.06.10.34.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2019 10:34:02 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 6 May 2019 10:33:35 -0700 Message-Id: <20190506173353.32206-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190506173353.32206-1-richard.henderson@linaro.org> References: <20190506173353.32206-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::442 Subject: [Qemu-devel] [PATCH v4 06/24] 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. Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- v3: If getrandom is not present, fall back on /dev/(u)random. --- crypto/random-platform.c | 37 ++++++++++++++++++++++++++++++++----- configure | 18 +++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) -- 2.17.1 Reviewed-by: Laurent Vivier 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 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) == 0) { + /* Use getrandom() */ + fd = -1; + return 0; + } + /* Fall through to /dev/urandom case. */ +# endif fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); if (fd == -1 && errno == ENOENT) { fd = 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 = getrandom(buf, buflen, 0); + if (likely(got == buflen)) { + return 0; + } + if (got >= 0) { + buflen -= got; + buf += got; + } else if (errno != EINTR) { + error_setg_errno(errp, errno, "getrandom"); + return -1; + } + } + } + /* Fall through to /dev/urandom case. */ +# endif while (1) { ssize_t got = read(fd, buf, buflen); if (likely(got == buflen)) { diff --git a/configure b/configure index 0099e85a03..5138a1a45c 100755 --- a/configure +++ b/configure @@ -5806,6 +5806,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 @@ -7193,7 +7207,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