From patchwork Wed May 22 18:42:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164877 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp1245992ili; Wed, 22 May 2019 12:20:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHt8EU7ARFuLYMEwmbNPZ/hlyGqsmoAkrAUd2fBsEgY7/ldTzvz4MQfVsxrm+ZR0lZyx9A X-Received: by 2002:a17:906:6857:: with SMTP id a23mr64919061ejs.280.1558552813079; Wed, 22 May 2019 12:20:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558552813; cv=none; d=google.com; s=arc-20160816; b=BS2tJLY+hEk23ZzDrRwbwxhHw/laWDpd5otqx9P7+3ZdIxv8JvgJPsLnM5Xk9pAJ1P dfatJRu5oMpTbuwbr4brUqNHQryeb4ut8SNhjeLZgyiSZe0n6VRl30HLfbxTmqe1BoTu BBF5wBF87CdNbwDPP+WlReJtQXf8J5nG4ht3ZuDSvYa29DZ0pSdxeBI5roielkcCPeJ6 m8VFQ47FATsuj3uhoUVcNuFmldrTXg6ps+FzBRFYPZtPwrm6Qwg3QRgKrqusCohxznGE fRMmwOmZxykEAxO/PTIdmrm9U/MSgKa/OffQPdKPzxDO+NMjHvS43zZaspcfWZofFA1V LFCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=4MrsM8HWkl5HnptGJNmid/oZc3Q7MB8Exr59LxaGIMY=; b=e7WyYOaZ48GzHt3MwtTcprlHJOe1bMPTSt8DtpWYvT6MDCUIfbV74My0wLrhJjsfTT 5hbTGKAl3fs18CF1MqbhN/vhXiUB78OuhwWPuQhBjX1IZuko2f6eFeLRtA0Czip6UYtk SpjvEn/x8IAElAJ5/MptWlAixQCSdC8anvjU6+xt6WcOi+rEkDH41lwRWidZCImRZStJ bdflUBDQ9X/F5hlyGLPAY+9Ga3G9ZXjiXBJWytueUJHfr8gVvW3tpqbM83WYygh+lsrU oaUnH8SsdQxsZb8dCqZiWStFFL4yphPWQM9Dy/hxPLKybtpYX+CpdaX972PN+xcVIJL3 AfRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=GWEK4DzL; 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 s32si16483860eda.374.2019.05.22.12.20.12 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 22 May 2019 12:20:13 -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=GWEK4DzL; 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]:49987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWmZ-00055U-UF for patch@linaro.org; Wed, 22 May 2019 15:20:11 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34796) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWOp-00086f-Ql for qemu-devel@nongnu.org; Wed, 22 May 2019 14:55:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTWCH-00085r-QZ for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:43 -0400 Received: from mail-yw1-xc41.google.com ([2607:f8b0:4864:20::c41]:42440) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hTWCG-000850-E5 for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:41 -0400 Received: by mail-yw1-xc41.google.com with SMTP id s5so1236681ywd.9 for ; Wed, 22 May 2019 11:42:39 -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=4MrsM8HWkl5HnptGJNmid/oZc3Q7MB8Exr59LxaGIMY=; b=GWEK4DzLuwRLDSn4ojJp4HgoMS+aCUenx69mtZ9UAPaDFezyZWQQE5toJEykcVwt51 yb4+JhiRiPn09WrsTsq2aWtMlgGY4FBKiCLJSzz2SLvJHn2Rf0GPqlM1cjmBGb2+iMrC KIdjat/vwGiLX012TEJuiRmUM1eFngZuPeCrGupstP30HL8sgMiHZB223TkR4RIR9GDz 2qDSnzaqbRy1eRE+TGFRjecjGlsFGLr+a36b7akQceZyPtXf63+9Ip3aMkAXLOeEBXqG BS12ksV8qubrTFk11ugxerh4VuScgAFE1e7U97RlaXCnZLS1O9NemwCi2AHONfYDC53+ 1sRA== 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=4MrsM8HWkl5HnptGJNmid/oZc3Q7MB8Exr59LxaGIMY=; b=ApNiOvRAbDlc23/pVOAeH7An7yPMKh1QOWcDQJILtObseTKgFEKMCYS5SRwrCTkzgf bhwJWRsCMGr5KtrCzJce27r7GxuwbdZZVAem7D3pAQCZLSAVTQkqOqYiQ1U15qjdBMp4 HDI38dyFa9zzMhSYRTx/IN4ZlLCKxEBlhwaq2EmeC0RV3cHorix/NeDX3DHIl3N+PEkJ 9JVMyhB2YkGyKuhcBawH21yhxxSxTXvmowG3EzIZRmMNP3rfkJv80M0Qeh/HawlVAxDm qMs/INPtnMhID8SZWFsKOq5L2fq+ASDwLsQ+GH3vpZgx+rapepwxzTC4kIMcrRiIf2c3 RNBg== X-Gm-Message-State: APjAAAV8TyjnK4G2CSumobOArcS7FTKV3OuqpM4JUrnOyMTwGOi8QXWr Y7ulZxckEVL+r3rFmbaTY6heAzE27NM= X-Received: by 2002:a0d:eb90:: with SMTP id u138mr22470698ywe.196.1558550559046; Wed, 22 May 2019 11:42:39 -0700 (PDT) Received: from localhost.localdomain ([71.46.56.17]) by smtp.gmail.com with ESMTPSA id 206sm1299445ywq.74.2019.05.22.11.42.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2019 11:42:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 22 May 2019 14:42:11 -0400 Message-Id: <20190522184226.17871-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190522184226.17871-1-richard.henderson@linaro.org> References: <20190522184226.17871-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::c41 Subject: [Qemu-devel] [PULL 10/25] 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: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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: Laurent Vivier Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- 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 -- 2.17.1 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 Free + * Software Foundation; either version 2 of the License, or (at your option) + * 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 Free + * Software Foundation; either version 2 of the License, or (at your option) + * 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 = thread_rand; + size_t i; + uint32_t x; + + if (unlikely(rand == NULL)) { + /* Thread not initialized for a cpu, or main w/o -seed. */ + thread_rand = rand = g_rand_new(); + } + + for (i = 0; i + 4 <= len; i += 4) { + x = g_rand_int(rand); + __builtin_memcpy(buf + i, &x, 4); + } + if (i < len) { + x = 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 == NULL); + if (deterministic) { + thread_rand = + 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 = 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 += iova-tree.o util-obj-$(CONFIG_INOTIFY1) += filemonitor-inotify.o util-obj-$(CONFIG_LINUX) += vfio-helpers.o util-obj-$(CONFIG_OPENGL) += drm.o +util-obj-y += guest-random.o stub-obj-y += filemonitor-stub.o