linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Alexander E. Patrakov" <patrakov@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	"Theodore Y. Ts'o" <tytso@mit.edu>
Cc: "Ahmed S. Darwish" <darwish.07@gmail.com>,
	Lennart Poettering <mzxreary@0pointer.de>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Michael Kerrisk <mtk.manpages@gmail.com>,
	lkml <linux-kernel@vger.kernel.org>,
	linux-ext4@vger.kernel.org, linux-man@vger.kernel.org
Subject: Re: [PATCH RFC v4 1/1] random: WARN on large getrandom() waits and introduce getrandom2()
Date: Fri, 20 Sep 2019 01:45:40 +0500	[thread overview]
Message-ID: <6adb02d4-c486-a945-7f51-d007d6de45b2@gmail.com> (raw)
In-Reply-To: <CAHk-=wjTbpcyVevsy3g-syB5v9gk_rR-yRFrUAvTL8NFuGfCrw@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 3608 bytes --]

20.09.2019 01:04, Linus Torvalds пишет:

> instead. Yeah, it still doesn't help on machines that don't even have
> a cycle counter, but it at least means that you don't have to have a
> CPU rdrand (or equivalent) but you do have a cycle counter, now the
> extraction of randomness from the pool doesn't just do the
> (predictable) mutation for the backtracking, but actually means that
> you have some very hard to predict timing effects.
> 
> Again, in this case a cycle counter really does add a small amount of
> entropy (everybody agrees that modern CPU's are simply too complex to
> be predictable at a cycle level), but that's not really the point. The
> point is that now doing the extraction really fundamentally changes
> the state in unpredictable ways, so that you don't have that "if I
> recognize a value, I know what the next value will be" kind of attack.

This already resembles in-kernel haveged (except that it doesn't credit 
entropy), and Willy Tarreau said "collect the small entropy where it is, 
period" today. So, too many people touched upon the topic in one day, 
and therefore I'll bite.

We already have user-space software (haveged and modern versions of 
rngd) that extract supposed entropy from clock jitter and feed it back 
to the kernel via /dev/random (crediting it). Indeed, at present, on 
some hardware this is the only way for distributions and users to 
collect enough entropy during boot and avoid stalls - all other 
suggestions are simply non-constructive. Also, Google's Fuchsia OS does 
use and credit jitter entropy.

For the record: I do not have a justifiable opinion whether haveged/rngd 
output (known as jitter entropy) actually contains any entropy. I 
understand that there are two possible viewpoints here. The rest of the 
email is written under the assumption that haveged does provide real 
entropy and not fake one.

The problem that I have with the current situation is that distributions 
and users, when they set up their systems to run haveged or rngd, often 
do it incorrectly (even, as mentioned, under the assumption that haveged 
is something valid and useful). The most common mistake is relying on 
systemd-provided default dependencies, thus not starting such software 
as early as possible. Even worse, no initramfs generator allows one to 
easily include haveged/rngd in the initramfs and run it there. And for 
me, the first urandom warning comes from the initramfs, so anything 
started from the main system is, arguably, already too late.

Therefore, I think, an in-kernel hwrng that exposes jitter entropy is 
something useful (for those who agree that jitter entropy is not fake), 
because it avoids the pitfall-ridden userspace setup. Just as an 
exercise, I have implemented a very simple driver (attached as a patch) 
that does just that. I am only half-serious here, the driver is only 
lightly tested in KVM without any devices except an unconnected virtio 
network card, not on any real hardware. Someone else can also find it 
useful as a test/fake hwrng driver.

I am aware that there was an earlier decision that jitter entropy should 
not be credited, i.e. effectively a pre-existing NAK from Theodore Ts'o. 
But, well, distributions are already overriding this decision in 
userspace, and do it badly, so in my viewpoint, the driver would be a 
net win if some mechanism is added that makes it a no-op by default even 
if the driver is built-in. E.g. an explicit "enable" parameter, but I am 
open to other suggestions, too.

-- 
Alexander E. Patrakov

[-- Attachment #1.2: 0001-hw_random-Add-jitterentropy_hwrng.patch --]
[-- Type: text/x-patch, Size: 4507 bytes --]

From 2836990aff5bc1dab6a4e927304247dae469c774 Mon Sep 17 00:00:00 2001
From: "Alexander E. Patrakov" <patrakov@gmail.com>
Date: Thu, 19 Sep 2019 01:18:39 +0500
Subject: [PATCH] hw_random: Add jitterentropy_hwrng

This re-exports the existing "jitterentropy_rng" cryptoapi RNG as a
hwrng. The use case is to replace haveged, which distributions
often misconfigure by running it too late, while it is really needed
even in the initramfs on some systems.

Signed-off-by: Alexander E. Patrakov <patrakov@gmail.com>
---
 drivers/char/hw_random/Kconfig               | 20 ++++++
 drivers/char/hw_random/Makefile              |  1 +
 drivers/char/hw_random/jitterentropy-hwrng.c | 70 ++++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 drivers/char/hw_random/jitterentropy-hwrng.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 59f25286befe..ff2102c0159c 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -35,6 +35,26 @@ config HW_RANDOM_TIMERIOMEM
 
 	  If unsure, say Y.
 
+config HW_RANDOM_JITTERENTROPY
+        tristate "Jitter Entropy HW Random Number Generator support"
+        select CRYPTO_JITTERENTROPY
+        ---help---
+          This driver provides kernel-side support for extracting entropy
+          from CPU and memory clock jitter.
+
+          jitterentropy-hwrng serves the same purpose as haveged, but is in
+          the kernel. So, if you otherwise would have to run haveged, build
+          this driver instead, it has an advantage of being available very
+          early in the boot process.
+
+          Note that it is still not known whether clock jitter provides any
+          actual entropy.
+
+          To compile this driver as a module, choose M here: the
+          module will be called jitterentropy-hwrng.
+
+          If unsure, say N.
+
 config HW_RANDOM_INTEL
 	tristate "Intel HW Random Number Generator support"
 	depends on (X86 || IA64) && PCI
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 7c9ef4a7667f..9c6d1d3626f6 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -6,6 +6,7 @@
 obj-$(CONFIG_HW_RANDOM) += rng-core.o
 rng-core-y := core.o
 obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
+obj-$(CONFIG_HW_RANDOM_JITTERENTROPY) += jitterentropy-hwrng.o
 obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
 obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
 obj-$(CONFIG_HW_RANDOM_ATMEL) += atmel-rng.o
diff --git a/drivers/char/hw_random/jitterentropy-hwrng.c b/drivers/char/hw_random/jitterentropy-hwrng.c
new file mode 100644
index 000000000000..b7aeefe4f47d
--- /dev/null
+++ b/drivers/char/hw_random/jitterentropy-hwrng.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2019 Alexander E. Patrakov <patrakov@gmail.com>
+ *
+ * Driver that exposes CPU clock jitter as a hardware random number generator
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/delay.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/hw_random.h>
+#include <crypto/rng.h>
+
+static struct crypto_rng *drng;
+
+static int jitterentropy_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+	int err;
+
+	/* Prevent the hwrng_fill thread from impeding progress of everything else */
+	if (wait)
+		schedule();
+
+	err = crypto_rng_get_bytes(drng, data, max);
+	if (err)
+		return err;
+	return max;
+}
+
+static struct hwrng jitterentropy_rng = {
+	.name		= KBUILD_MODNAME,
+	.read		= jitterentropy_rng_read,
+	.quality	= 4, /* minimum that guarantees progress in hwrng_fill thread */
+};
+
+static int __init mod_init(void)
+{
+	int ret;
+
+	pr_info("Registering the driver\n");
+	drng = crypto_alloc_rng("jitterentropy_rng", 0, 0);
+	if (!drng) {
+		pr_err("crypto_alloc_rng() failed\n");
+		return -ENODEV;
+	}
+
+	ret = hwrng_register(&jitterentropy_rng);
+	if (ret) {
+		crypto_free_rng(drng);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void __exit mod_exit(void)
+{
+	hwrng_unregister(&jitterentropy_rng);
+	crypto_free_rng(drng);
+}
+
+module_init(mod_init);
+module_exit(mod_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Alexander E. Patrakov <patrakov@gmail.com>");
+MODULE_DESCRIPTION("Exposes clock jitter as a hwrng");
+MODULE_SOFTDEP("pre: jitterentropy_rng");
-- 
2.23.0


[-- Attachment #2: Криптографическая подпись S/MIME --]
[-- Type: application/pkcs7-signature, Size: 4052 bytes --]

  reply	other threads:[~2019-09-19 20:45 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAHk-=whW_AB0pZ0u6P9uVSWpqeb5t2NCX_sMpZNGy8shPDyDNg@mail.gmail.com>
     [not found] ` <CAHk-=wi_yXK5KSmRhgNRSmJSD55x+2-pRdZZPOT8Fm1B8w6jUw@mail.gmail.com>
     [not found]   ` <20190911173624.GI2740@mit.edu>
     [not found]     ` <20190912034421.GA2085@darwi-home-pc>
     [not found]       ` <20190912082530.GA27365@mit.edu>
     [not found]         ` <CAHk-=wjyH910+JRBdZf_Y9G54c1M=LBF8NKXB6vJcm9XjLnRfg@mail.gmail.com>
     [not found]           ` <20190914122500.GA1425@darwi-home-pc>
     [not found]             ` <008f17bc-102b-e762-a17c-e2766d48f515@gmail.com>
     [not found]               ` <20190915052242.GG19710@mit.edu>
     [not found]                 ` <CAHk-=wgg2T=3KxrO-BY3nHJgMEyApjnO3cwbQb_0vxsn9qKN8Q@mail.gmail.com>
2019-09-18 21:15                   ` [PATCH RFC v4 0/1] random: WARN on large getrandom() waits and introduce getrandom2() Ahmed S. Darwish
2019-09-18 21:17                     ` [PATCH RFC v4 1/1] " Ahmed S. Darwish
2019-09-18 23:57                       ` Linus Torvalds
2019-09-19 14:34                         ` Theodore Y. Ts'o
2019-09-19 15:20                           ` Linus Torvalds
2019-09-19 15:50                             ` Linus Torvalds
2019-09-20 13:13                               ` Theodore Y. Ts'o
2019-09-19 20:04                             ` Linus Torvalds
2019-09-19 20:45                               ` Alexander E. Patrakov [this message]
2019-09-19 21:47                                 ` Linus Torvalds
2019-09-19 22:23                                   ` Alexander E. Patrakov
2019-09-19 23:44                                     ` Alexander E. Patrakov
2019-09-20 13:16                                     ` Theodore Y. Ts'o
2019-09-23 11:55                               ` David Laight
2019-09-20 13:08                             ` Theodore Y. Ts'o
2019-09-20 13:46                         ` Ahmed S. Darwish
2019-09-20 14:33                           ` Andy Lutomirski
2019-09-20 16:29                             ` Linus Torvalds
2019-09-20 17:52                               ` Andy Lutomirski
2019-09-20 18:09                                 ` Linus Torvalds
2019-09-20 18:16                                   ` Willy Tarreau
2019-09-20 19:12                                   ` Andy Lutomirski
2019-09-20 19:51                                     ` Linus Torvalds
2019-09-20 20:11                                       ` Alexander E. Patrakov
2019-09-20 20:17                                       ` Matthew Garrett
2019-09-20 20:51                                       ` Andy Lutomirski
2019-09-20 22:44                                         ` Linus Torvalds
2019-09-20 23:30                                           ` Andy Lutomirski
2019-09-21  3:05                                             ` Willy Tarreau
2019-09-21  6:07                                   ` Florian Weimer
2019-09-23 18:33                                     ` Andy Lutomirski
2019-09-26 21:11                                       ` Ahmed S. Darwish
2019-09-20 18:12                                 ` Willy Tarreau
2019-09-20 19:22                                   ` Andy Lutomirski
2019-09-20 19:37                                     ` Willy Tarreau
2019-09-20 19:52                                       ` Andy Lutomirski
2019-09-20 20:02                                     ` Linus Torvalds
2019-09-20 18:15                                 ` Alexander E. Patrakov
2019-09-20 18:29                                   ` Andy Lutomirski
2019-09-20 17:26                           ` Willy Tarreau
2019-09-20 17:56                             ` Ahmed S. Darwish
2019-09-26 20:42                         ` [PATCH v5 0/1] random: getrandom(2): warn on large CRNG waits, introduce new flags Ahmed S. Darwish
2019-09-26 20:44                           ` [PATCH v5 1/1] " Ahmed S. Darwish
2019-09-26 21:39                             ` Andy Lutomirski
2019-09-28  9:30                               ` Ahmed S. Darwish

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6adb02d4-c486-a945-7f51-d007d6de45b2@gmail.com \
    --to=patrakov@gmail.com \
    --cc=darwish.07@gmail.com \
    --cc=ebiederm@xmission.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=mzxreary@0pointer.de \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).