From nobody Tue Feb 10 13:37:35 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=1557164700; cv=none; d=zoho.com; s=zohoarc; b=OkoR42Qjd+7Wys30qMhrrurG9/93PV6FuPm/oCgJJA3uLjNJ3civMNhRSLZr4fQ9iHsVpeS+GsFbxsoLzV0mbGCQ3PKK9EWj+gbp/siLFuYS8/uogm/W3iIizu9Gb7tfy7c6al7+/RwQvuwvHo01fX5KVPaUm09Mn+w2RNTG5QI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1557164700; h=Content-Type:Content-Transfer-Encoding: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=lbaopiF28XTSe0kOgA43uLo7FEzHh7vNLBWI3VxuLJg=; b=SQ9tbMltzMDf+4IL6Cv9HcMUZD3fULrk77+rIETYI0fbs6oyG6gU5mNs9BsPOVp8RCTp1j68ryAsKB/rSvdMZsi3cILQYnRSX3U1wcUUc3QT8N/s6DeC1WYe6I5eJbMeDBKKBeIW41XLy/UzGTMY5okb8hxbGopxEkhQFBXwlDA= 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1557164700764469.74372196304364; Mon, 6 May 2019 10:45:00 -0700 (PDT) Received: from localhost ([127.0.0.1]:60027 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhfR-0002B4-LC for importer@patchew.org; Mon, 06 May 2019 13:44:45 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39880) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhVE-00016R-U4 for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNhVB-0001LM-MI for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:12 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]:36746) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hNhVB-0001Kp-E7 for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:09 -0400 Received: by mail-pg1-x533.google.com with SMTP id 85so6797447pgc.3 for ; Mon, 06 May 2019 10:34:09 -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.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2019 10:34:07 -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=lbaopiF28XTSe0kOgA43uLo7FEzHh7vNLBWI3VxuLJg=; b=yHrF05c493lMI8Ycww/BdgbvwfwJzJL5/wDqNnvOANcNr7P16SfM4ZjBV2sWr7/88s zVcxZGB6bK1EN+NT6V7Brj2A9qpWDeX+kCgoCSEyTtyLsgUMcPpWkRayX8bZeVoSke+x TnX8ktXtmpSacFwJYp2rhRn3X/kITXHTKRSOxZbNfNqcL4h9rWBxoeJ2XlkLZDYSPC28 TA86sZVN1Ku25AmdK1dYjzYLSZGk+UisA7C4DRFGuAqJzlq5ywFs+WSDh+QUUl7c7OKz njDSrjusjlM55A+ONe6V3c8qFxQMM2EcgPGCUymS7NV3Ok+vx4RV+OqSzAygDpqKnCOe BALg== 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=lbaopiF28XTSe0kOgA43uLo7FEzHh7vNLBWI3VxuLJg=; b=cgNWJNzXLFbdaMk39hHh0kurklsqSIEKVzJ+iJaufFQNC/cX5rLm+HjI1+L08U7mIX /l/1OlnfjrJJpTSe9fyuwNLfQjpLS5vIJ0PtyjoGYEQvkjYqUlnAE0rCBwbAkx/PPA2h BIfgCY//G7KNG9Vmes7WlfHt7z+rh4tDkkj+EEYGR12c3K8x4Evg3l4Lp17Ta5rrtjCN wh9iaEmggktat7Zuw9GgfRRNld27l6FlEd5Uo6ajQh05iAiJnRz/v1M+VQ/Gu32pTvxA 0VU8Vo0KDtohA5S6kPoSVP6iPs0Js36Cfnr0OgZ6eAvJDh0IX58TYQgqP3qHgRA1q6lD XCAQ== X-Gm-Message-State: APjAAAU7JXYuewPUxNxSIy+xx7MuTmP8f1pkdGVAH7Q+95N05aBzrzgr ry6/Yf+AL2/Zv4Ob9TrKoqG55NGeAt0= X-Google-Smtp-Source: APXvYqyrTW/k7r5/751BY/ZTsictQZbHuKTbQIQC6o2pSclEWdrBjvRdjFQwVhAw5M1zhEdQhMxkKw== X-Received: by 2002:a65:66d2:: with SMTP id c18mr3106926pgw.427.1557164048174; Mon, 06 May 2019 10:34:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 6 May 2019 10:33:39 -0700 Message-Id: <20190506173353.32206-11-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 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::533 Subject: [Qemu-devel] [PATCH v4 10/24] util: Add qemu_guest_getrandom and associated routines 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) This routine is intended to produce high-quality random numbers to the guest. Normally, such numbers are crypto quality from the host, but a command-line option can force the use of a fully deterministic sequence for use while debugging. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Richard Henderson Reviewed-by: Laurent Vivier --- include/qemu/guest-random.h | 68 +++++++++++++++++++++++++++ util/guest-random.c | 93 +++++++++++++++++++++++++++++++++++++ util/Makefile.objs | 1 + 3 files changed, 162 insertions(+) create mode 100644 include/qemu/guest-random.h create mode 100644 util/guest-random.c diff --git a/include/qemu/guest-random.h b/include/qemu/guest-random.h new file mode 100644 index 0000000000..09ff9c2236 --- /dev/null +++ b/include/qemu/guest-random.h @@ -0,0 +1,68 @@ +/* + * QEMU guest-visible random functions + * + * Copyright 2019 Linaro, Ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the F= ree + * Software Foundation; either version 2 of the License, or (at your optio= n) + * any later version. + */ + +#ifndef QEMU_GUEST_RANDOM_H +#define QEMU_GUEST_RANDOM_H + +/** + * qemu_guest_random_seed_main(const char *optarg, Error **errp) + * @optarg: a non-NULL pointer to a C string + * @errp: an error indicator + * + * The @optarg value is that which accompanies the -seed argument. + * This forces qemu_guest_getrandom into deterministic mode. + * + * Returns 0 on success, < 0 on failure while setting *errp. + */ +int qemu_guest_random_seed_main(const char *optarg, Error **errp); + +/** + * qemu_guest_random_seed_thread_part1(void) + * + * If qemu_getrandom is in deterministic mode, returns an + * independent seed for the new thread. Otherwise returns 0. + */ +uint64_t qemu_guest_random_seed_thread_part1(void); + +/** + * qemu_guest_random_seed_thread_part2(uint64_t seed) + * @seed: a value for the new thread. + * + * If qemu_guest_getrandom is in deterministic mode, this stores an + * independent seed for the new thread. Otherwise a no-op. + */ +void qemu_guest_random_seed_thread_part2(uint64_t seed); + +/** + * qemu_guest_getrandom(void *buf, size_t len, Error **errp) + * @buf: a buffer of bytes to be written + * @len: the number of bytes in @buf + * @errp: an error indicator + * + * Fills len bytes in buf with random data. This should only be used + * for data presented to the guest. Host-side crypto services should + * use qcrypto_random_bytes. + * + * Returns 0 on success, < 0 on failure while setting *errp. + */ +int qemu_guest_getrandom(void *buf, size_t len, Error **errp); + +/** + * qemu_guest_getrandom_nofail(void *buf, size_t len) + * @buf: a buffer of bytes to be written + * @len: the number of bytes in @buf + * + * Like qemu_guest_getrandom, but will assert for failure. + * Use this when there is no reasonable recovery. + */ +void qemu_guest_getrandom_nofail(void *buf, size_t len); + +#endif /* QEMU_GUEST_RANDOM_H */ diff --git a/util/guest-random.c b/util/guest-random.c new file mode 100644 index 0000000000..e8124a3cad --- /dev/null +++ b/util/guest-random.c @@ -0,0 +1,93 @@ +/* + * QEMU guest-visible random functions + * + * Copyright 2019 Linaro, Ltd. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the F= ree + * Software Foundation; either version 2 of the License, or (at your optio= n) + * any later version. + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/cutils.h" +#include "qapi/error.h" +#include "qemu/guest-random.h" +#include "crypto/random.h" + + +static __thread GRand *thread_rand; +static bool deterministic; + + +static int glib_random_bytes(void *buf, size_t len) +{ + GRand *rand =3D thread_rand; + size_t i; + uint32_t x; + + if (unlikely(rand =3D=3D NULL)) { + /* Thread not initialized for a cpu, or main w/o -seed. */ + thread_rand =3D rand =3D g_rand_new(); + } + + for (i =3D 0; i + 4 <=3D len; i +=3D 4) { + x =3D g_rand_int(rand); + __builtin_memcpy(buf + i, &x, 4); + } + if (i < len) { + x =3D g_rand_int(rand); + __builtin_memcpy(buf + i, &x, i - len); + } + return 0; +} + +int qemu_guest_getrandom(void *buf, size_t len, Error **errp) +{ + if (unlikely(deterministic)) { + /* Deterministic implementation using Glib's Mersenne Twister. */ + return glib_random_bytes(buf, len); + } else { + /* Non-deterministic implementation using crypto routines. */ + return qcrypto_random_bytes(buf, len, errp); + } +} + +void qemu_guest_getrandom_nofail(void *buf, size_t len) +{ + qemu_guest_getrandom(buf, len, &error_fatal); +} + +uint64_t qemu_guest_random_seed_thread_part1(void) +{ + if (deterministic) { + uint64_t ret; + glib_random_bytes(&ret, sizeof(ret)); + return ret; + } + return 0; +} + +void qemu_guest_random_seed_thread_part2(uint64_t seed) +{ + g_assert(thread_rand =3D=3D NULL); + if (deterministic) { + thread_rand =3D + g_rand_new_with_seed_array((const guint32 *)&seed, + sizeof(seed) / sizeof(guint32)); + } +} + +int qemu_guest_random_seed_main(const char *optarg, Error **errp) +{ + unsigned long long seed; + if (parse_uint_full(optarg, &seed, 0)) { + error_setg(errp, "Invalid seed number: %s", optarg); + return -1; + } else { + deterministic =3D true; + qemu_guest_random_seed_thread_part2(seed); + return 0; + } +} diff --git a/util/Makefile.objs b/util/Makefile.objs index 9206878dec..c27a923dbe 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -54,5 +54,6 @@ util-obj-y +=3D iova-tree.o util-obj-$(CONFIG_INOTIFY1) +=3D filemonitor-inotify.o util-obj-$(CONFIG_LINUX) +=3D vfio-helpers.o util-obj-$(CONFIG_OPENGL) +=3D drm.o +util-obj-y +=3D guest-random.o =20 stub-obj-y +=3D filemonitor-stub.o --=20 2.17.1