From nobody Fri May 3 07:56:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487334272187793.6974017143639; Fri, 17 Feb 2017 04:24:32 -0800 (PST) Received: from localhost ([::1]:53142 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cehaQ-0004gW-00 for importer@patchew.org; Fri, 17 Feb 2017 07:24:30 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47346) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cehYm-0003lC-BS for qemu-devel@nongnu.org; Fri, 17 Feb 2017 07:22:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cehYl-0005SR-5A for qemu-devel@nongnu.org; Fri, 17 Feb 2017 07:22:48 -0500 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:48547) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cehYi-0005Mk-0Y; Fri, 17 Feb 2017 07:22:44 -0500 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1cehYe-0003e0-Cz; Fri, 17 Feb 2017 12:22:40 +0000 From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Date: Fri, 17 Feb 2017 12:22:39 +0000 Message-Id: <1487334159-19664-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH] bcm2835_rng: Use qcrypto_random_bytes() rather than rand() 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: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Marcin Chojnacki , patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Switch to using qcrypto_random_bytes() rather than rand() as our source of randomness for the BCM2835 RNG. If qcrypto_random_bytes() fails, we don't want to return the guest a non-random value in case they're really using it for cryptographic purposes, so the best we can do is a fatal error. This shouldn't happen unless something's broken, though. In theory we could implement this device's full FIFO and interrupt semantics and then just stop filling the FIFO. That's a lot of work, though, and doesn't really give a very nice diagnostic to the user since the guest will just seem to hang. Signed-off-by: Peter Maydell Reviewed-by: Daniel P. Berrange --- This patch sits on top of http://patchwork.ozlabs.org/patch/726744/ (though for review purposes I think it's pretty self explanatory). The interesting question here is the failure case handling, where we're a bit between a rock and a hard place because we don't have a nice way to report it to the guest, but we don't want to return a non-random value either... We should probably improve crypto/random-platform.c to use getentropy() if available, which would fix the "BSD or OSX host and not using gcrypt or gnutls" case which I think is the most likely cause of qcrypto_random_bytes() failing. hw/misc/bcm2835_rng.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/hw/misc/bcm2835_rng.c b/hw/misc/bcm2835_rng.c index 2242bc5..bbe903d 100644 --- a/hw/misc/bcm2835_rng.c +++ b/hw/misc/bcm2835_rng.c @@ -9,8 +9,32 @@ =20 #include "qemu/osdep.h" #include "qemu/log.h" +#include "qapi/error.h" +#include "crypto/random.h" #include "hw/misc/bcm2835_rng.h" =20 +static uint32_t get_random_bytes(void) +{ + uint32_t res; + Error *err =3D NULL; + + if (qcrypto_random_bytes((uint8_t *)&res, sizeof(res), &err) < 0) { + /* On failure we don't want to return the guest a non-random + * value in case they're really using it for cryptographic + * purposes, so the best we can do is die here. + * This shouldn't happen unless something's broken. + * In theory we could implement this device's full FIFO + * and interrupt semantics and then just stop filling the + * FIFO. That's a lot of work, though, so we assume any + * errors are systematic problems and trust that the check + * on init is sufficient. + */ + error_report_err(err); + exit(1); + } + return res; +} + static uint64_t bcm2835_rng_read(void *opaque, hwaddr offset, unsigned size) { @@ -27,7 +51,7 @@ static uint64_t bcm2835_rng_read(void *opaque, hwaddr off= set, res =3D s->rng_status | (1 << 24); break; case 0x8: /* rng_data */ - res =3D rand(); + res =3D get_random_bytes(); break; =20 default: --=20 2.7.4