From nobody Wed May 14 20:42:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1527582337694853.6863791705855; Tue, 29 May 2018 01:25:37 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C09FC0D265D; Tue, 29 May 2018 08:25:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1612E1BBC4; Tue, 29 May 2018 08:25:36 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B23A74CA89; Tue, 29 May 2018 08:25:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4T8PG9L005794 for ; Tue, 29 May 2018 04:25:16 -0400 Received: by smtp.corp.redhat.com (Postfix) id 17475422BA; Tue, 29 May 2018 08:25:16 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 92A3464002; Tue, 29 May 2018 08:25:15 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 29 May 2018 10:24:42 +0200 Message-Id: <0d96fdb2bd21d374c24a4cb057ed9e4b01c2c9d9.1527581861.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 06/10] virRandomBytes: Report error X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 29 May 2018 08:25:36 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of having each caller report error move it into the function. This way we can produce more accurate error messages too. Signed-off-by: Michal Privoznik --- src/util/vircrypto.c | 6 ++---- src/util/virrandom.c | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c index 673e1648e8..e5f2319720 100644 --- a/src/util/vircrypto.c +++ b/src/util/vircrypto.c @@ -330,9 +330,9 @@ int virCryptoGenerateRandom(unsigned char *buf, size_t buflen) { +#if WITH_GNUTLS int rv; =20 -#if WITH_GNUTLS /* Generate the byte stream using gnutls_rnd() if possible */ if ((rv =3D gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -344,10 +344,8 @@ virCryptoGenerateRandom(unsigned char *buf, /* If we don't have gnutls_rnd(), we will generate a less cryptographi= cally * strong master buf from /dev/urandom. */ - if ((rv =3D virRandomBytes(buf, buflen)) < 0) { - virReportSystemError(-rv, "%s", _("failed to generate byte stream"= )); + if (virRandomBytes(buf, buflen) < 0) return -1; - } #endif =20 return 0; diff --git a/src/util/virrandom.c b/src/util/virrandom.c index ea55fe654d..230745d311 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -43,6 +43,8 @@ =20 VIR_LOG_INIT("util.random"); =20 +#define RANDOM_SOURCE "/dev/urandom" + /* The algorithm of virRandomBits relies on gnulib's guarantee that * 'random_r' matches the POSIX requirements on 'random' of being * evenly distributed among exactly [0, 2**31) (that is, we always get @@ -107,7 +109,6 @@ uint64_t virRandomBits(int nbits) if (virRandomInitialize() < 0) { /* You're already hosed, so this particular non-random value * isn't any worse. */ - VIR_WARN("random number generation is broken"); return 0; } =20 @@ -165,10 +166,10 @@ uint32_t virRandomInt(uint32_t max) * @buf: Pointer to location to store bytes * @buflen: Number of bytes to store * - * Generate a stream of random bytes from /dev/urandom + * Generate a stream of random bytes from RANDOM_SOURCE * into @buf of size @buflen * - * Returns 0 on success or an -errno on failure + * Returns 0 on success or -1 (with error reported) */ int virRandomBytes(unsigned char *buf, @@ -176,13 +177,20 @@ virRandomBytes(unsigned char *buf, { int fd; =20 - if ((fd =3D open("/dev/urandom", O_RDONLY)) < 0) - return -errno; + if ((fd =3D open(RANDOM_SOURCE, O_RDONLY)) < 0) { + virReportSystemError(errno, + _("unable to open %s"), + RANDOM_SOURCE); + return -1; + } =20 while (buflen > 0) { ssize_t n; =20 if ((n =3D saferead(fd, buf, buflen)) <=3D 0) { + virReportSystemError(errno, + _("unable to read from %s"), + RANDOM_SOURCE); VIR_FORCE_CLOSE(fd); return n < 0 ? -errno : -ENODATA; } --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list