Browse code

kernels: Add rdrand-based RNG driver to enhance kernel entropy

Virtualized environments are often entropy-starved, due to the lack of
hardware RNGs or events that can be used as sources of randomness.
To alleviate this problem, add a Random Number Generator driver that
utilizes the 'rdrand' instruction (available on modern Intel and AMD
CPUs), and feeds the kernel's entropy pool.

Set this rdrand-rng driver to autoload on every boot, by adding it to
modules-load.d

This helps address issues such as slow boot due to lack of hardware
entropy (eg: Github issue #774).

Change-Id: Id0644715b782066df5cd8383c9c84684b6d5cd6d
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/5304
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Alexey Makhalov <amakhalov@vmware.com>
Reviewed-by: Sharath George

Srivatsa S. Bhat authored on 2018/06/28 10:36:11
Showing 11 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,127 @@
0
+From 48e775c9449017499b113ff6af45dd6ddf5e0aac Mon Sep 17 00:00:00 2001
1
+From: "Srivatsa S. Bhat" <srivatsa@csail.mit.edu>
2
+Date: Wed, 27 Jun 2018 13:11:40 -0700
3
+Subject: [PATCH] hwrng: rdrand - Add RNG driver based on x86 rdrand
4
+ instruction
5
+
6
+Add a Hardware Random Number Generator driver, which uses the
7
+'rdrand' instruction available on modern Intel and AMD CPUs.
8
+
9
+This can be used to feed the kernel's entropy pool on
10
+entropy-starved virtual machines.
11
+
12
+Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
13
+---
14
+ drivers/char/hw_random/Kconfig      | 14 +++++++++
15
+ drivers/char/hw_random/Makefile     |  1 +
16
+ drivers/char/hw_random/rdrand-rng.c | 61 +++++++++++++++++++++++++++++++++++++
17
+ 3 files changed, 76 insertions(+)
18
+ create mode 100644 drivers/char/hw_random/rdrand-rng.c
19
+
20
+diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
21
+index 200dab5..cc3a67d 100644
22
+--- a/drivers/char/hw_random/Kconfig
23
+@@ -62,6 +62,20 @@ config HW_RANDOM_AMD
24
+ 
25
+ 	  If unsure, say Y.
26
+ 
27
++config HW_RANDOM_RDRAND
28
++	tristate "x86 rdrand Random Number Generator support"
29
++	depends on (X86_32 || X86_64) && ARCH_RANDOM
30
++	default HW_RANDOM
31
++	---help---
32
++	  This driver provides kernel-side support for a Random Number
33
++	  Generator that uses the 'rdrand' instruction on modern Intel
34
++	  and AMD CPUs.
35
++
36
++	  To compile this driver as a module, choose M here: the
37
++	  module will be called rdrand-rng.
38
++
39
++	  If unsure, say N.
40
++
41
+ config HW_RANDOM_ATMEL
42
+ 	tristate "Atmel Random Number Generator support"
43
+ 	depends on ARCH_AT91 && HAVE_CLK && OF
44
+diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
45
+index 5f52b1e..5b92c8e 100644
46
+--- a/drivers/char/hw_random/Makefile
47
+@@ -7,6 +7,7 @@ rng-core-y := core.o
48
+ obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
49
+ obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
50
+ obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
51
++obj-$(CONFIG_HW_RANDOM_RDRAND) += rdrand-rng.o
52
+ obj-$(CONFIG_HW_RANDOM_ATMEL) += atmel-rng.o
53
+ obj-$(CONFIG_HW_RANDOM_BCM63XX)	+= bcm63xx-rng.o
54
+ obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
55
+diff --git a/drivers/char/hw_random/rdrand-rng.c b/drivers/char/hw_random/rdrand-rng.c
56
+new file mode 100644
57
+index 0000000..e1cf7f3
58
+--- /dev/null
59
+@@ -0,0 +1,61 @@
60
++// SPDX-License-Identifier: GPL-2.0
61
++/*
62
++ * RNG driver that uses the 'rdrand' instruction (found on modern
63
++ * Intel and AMD CPUs).
64
++ *
65
++ * Author: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
66
++ *
67
++ */
68
++
69
++#include <linux/hw_random.h>
70
++#include <linux/kernel.h>
71
++#include <linux/module.h>
72
++#include <asm/archrandom.h>
73
++
74
++#define PFX	KBUILD_MODNAME ": "
75
++
76
++static int rdrand_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
77
++{
78
++	unsigned long *data = buf;
79
++	size_t read = 0;
80
++
81
++	while (read < max) {
82
++		arch_get_random_long(data);
83
++		data++;
84
++		read += sizeof(unsigned long);
85
++	}
86
++
87
++	return read;
88
++}
89
++
90
++static struct hwrng rdrand_rng = {
91
++	.name		= KBUILD_MODNAME,
92
++	.quality	= 1000,
93
++	.read		= rdrand_rng_read,
94
++};
95
++
96
++static int __init mod_init(void)
97
++{
98
++	int err = -ENODEV;
99
++
100
++	if (!arch_has_random())
101
++		return err; /* rdrand not available. */
102
++
103
++	err = hwrng_register(&rdrand_rng);
104
++	if (err)
105
++		pr_err(PFX "RNG registration failed (%d)\n", err);
106
++
107
++	return err;
108
++}
109
++
110
++static void __exit mod_exit(void)
111
++{
112
++	hwrng_unregister(&rdrand_rng);
113
++}
114
++
115
++module_init(mod_init);
116
++module_exit(mod_exit);
117
++
118
++MODULE_AUTHOR("Srivatsa S. Bhat <srivatsa@csail.mit.edu>");
119
++MODULE_DESCRIPTION("H/W RNG driver for x86 CPUs that support rdrand");
120
++MODULE_LICENSE("GPL");
121
+-- 
122
+2.7.4
123
+
... ...
@@ -2400,6 +2400,7 @@ CONFIG_HW_RANDOM=m
2400 2400
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2401 2401
 CONFIG_HW_RANDOM_INTEL=m
2402 2402
 CONFIG_HW_RANDOM_AMD=m
2403
+CONFIG_HW_RANDOM_RDRAND=m
2403 2404
 CONFIG_HW_RANDOM_VIA=m
2404 2405
 CONFIG_HW_RANDOM_VIRTIO=m
2405 2406
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -2226,6 +2226,7 @@ CONFIG_HW_RANDOM=m
2226 2226
 # CONFIG_HW_RANDOM_TIMERIOMEM is not set
2227 2227
 CONFIG_HW_RANDOM_INTEL=m
2228 2228
 CONFIG_HW_RANDOM_AMD=m
2229
+CONFIG_HW_RANDOM_RDRAND=m
2229 2230
 CONFIG_HW_RANDOM_VIA=m
2230 2231
 CONFIG_HW_RANDOM_VIRTIO=m
2231 2232
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -1865,7 +1865,12 @@ CONFIG_SERIAL_CORE_CONSOLE=y
1865 1865
 # CONFIG_SERIAL_FSL_LPUART is not set
1866 1866
 # CONFIG_TTY_PRINTK is not set
1867 1867
 # CONFIG_IPMI_HANDLER is not set
1868
-# CONFIG_HW_RANDOM is not set
1868
+CONFIG_HW_RANDOM=m
1869
+# CONFIG_HW_RANDOM_TIMERIOMEM is not set
1870
+# CONFIG_HW_RANDOM_INTEL is not set
1871
+# CONFIG_HW_RANDOM_AMD is not set
1872
+CONFIG_HW_RANDOM_RDRAND=m
1873
+# CONFIG_HW_RANDOM_VIA is not set
1869 1874
 # CONFIG_NVRAM is not set
1870 1875
 # CONFIG_R3964 is not set
1871 1876
 # CONFIG_APPLICOM is not set
... ...
@@ -2474,6 +2479,7 @@ CONFIG_USB_SERIAL_FTDI_SIO=m
2474 2474
 # CONFIG_USB_HSIC_USB3503 is not set
2475 2475
 # CONFIG_USB_HSIC_USB4604 is not set
2476 2476
 # CONFIG_USB_LINK_LAYER_TEST is not set
2477
+# CONFIG_USB_CHAOSKEY is not set
2477 2478
 # CONFIG_UCSI is not set
2478 2479
 
2479 2480
 #
... ...
@@ -2361,6 +2361,7 @@ CONFIG_HW_RANDOM=m
2361 2361
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2362 2362
 CONFIG_HW_RANDOM_INTEL=m
2363 2363
 CONFIG_HW_RANDOM_AMD=m
2364
+CONFIG_HW_RANDOM_RDRAND=m
2364 2365
 CONFIG_HW_RANDOM_VIA=m
2365 2366
 CONFIG_HW_RANDOM_VIRTIO=m
2366 2367
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-aws
4 4
 Version:        4.9.109
5
-Release:        1%{?kat_build:.%kat_build}%{?dist}
5
+Release:        2%{?kat_build:.%kat_build}%{?dist}
6 6
 License:    	GPLv2
7 7
 URL:        	http://www.kernel.org/
8 8
 Group:        	System Environment/Kernel
... ...
@@ -58,6 +58,7 @@ Patch38:        0001-net-phy-mdio-bcm-unimac-fix-potential-NULL-dereferen.patch
58 58
 Patch40:        0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
59 59
 # Fix for CVE-2017-18224
60 60
 Patch41:        0001-ocfs2-ip_alloc_sem-should-be-taken-in-ocfs2_get_bloc.patch
61
+Patch42:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
61 62
 
62 63
 # For Spectre
63 64
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -229,6 +230,7 @@ This package contains the 'perf' performance analysis tools for Linux kernel.
229 229
 %patch38 -p1
230 230
 %patch40 -p1
231 231
 %patch41 -p1
232
+%patch42 -p1
232 233
 
233 234
 %patch52 -p1
234 235
 %patch53 -p1
... ...
@@ -449,6 +451,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
449 449
 /usr/share/doc/*
450 450
 
451 451
 %changelog
452
+*   Wed Jun 27 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-2
453
+-   Add rdrand-based RNG driver to enhance kernel entropy.
452 454
 *   Thu Jun 21 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-1
453 455
 -   Update to version 4.9.109
454 456
 *   Mon May 21 2018 Alexey Makhalov <amakhalov@vmware.com> 4.9.101-2
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-esx
4 4
 Version:        4.9.109
5
-Release:        2%{?dist}
5
+Release:        3%{?dist}
6 6
 License:        GPLv2
7 7
 URL:            http://www.kernel.org/
8 8
 Group:          System Environment/Kernel
... ...
@@ -55,6 +55,7 @@ Patch38:        0001-net-phy-mdio-bcm-unimac-fix-potential-NULL-dereferen.patch
55 55
 Patch40:        0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
56 56
 # Fix for CVE-2017-18224
57 57
 Patch41:        0001-ocfs2-ip_alloc_sem-should-be-taken-in-ocfs2_get_bloc.patch
58
+Patch42:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
58 59
 
59 60
 # For Spectre
60 61
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -144,6 +145,7 @@ The Linux package contains the Linux kernel doc files
144 144
 %patch38 -p1
145 145
 %patch40 -p1
146 146
 %patch41 -p1
147
+%patch42 -p1
147 148
 
148 149
 %patch52 -p1
149 150
 %patch53 -p1
... ...
@@ -256,6 +258,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
256 256
 /usr/src/linux-headers-%{uname_r}
257 257
 
258 258
 %changelog
259
+*   Wed Jun 27 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-3
260
+-   Add rdrand-based RNG driver to enhance kernel entropy.
259 261
 *   Mon Jun 25 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-2
260 262
 -   Enable USB_SERIAL support in the config.
261 263
 *   Thu Jun 21 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-1
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-secure
4 4
 Version:        4.9.109
5
-Release:        1%{?kat_build:.%kat_build}%{?dist}
5
+Release:        2%{?kat_build:.%kat_build}%{?dist}
6 6
 License:        GPLv2
7 7
 URL:            http://www.kernel.org/
8 8
 Group:          System Environment/Kernel
... ...
@@ -64,6 +64,7 @@ Patch40:        0001-net-phy-mdio-bcm-unimac-fix-potential-NULL-dereferen.patch
64 64
 Patch42:        0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
65 65
 # Fix for CVE-2017-18224
66 66
 Patch43:        0001-ocfs2-ip_alloc_sem-should-be-taken-in-ocfs2_get_bloc.patch
67
+Patch44:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
67 68
 
68 69
 # For Spectre
69 70
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -197,6 +198,7 @@ EOF
197 197
 %patch40 -p1
198 198
 %patch42 -p1
199 199
 %patch43 -p1
200
+%patch44 -p1
200 201
 
201 202
 # spectre
202 203
 %patch52 -p1
... ...
@@ -344,6 +346,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
344 344
 /usr/src/linux-headers-%{uname_r}
345 345
 
346 346
 %changelog
347
+*   Wed Jun 27 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-2
348
+-   Add rdrand-based RNG driver to enhance kernel entropy.
347 349
 *   Thu Jun 21 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-1
348 350
 -   Update to version 4.9.109
349 351
 *   Mon May 21 2018 Alexey Makhalov <amakhalov@vmware.com> 4.9.101-2
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux
4 4
 Version:        4.9.109
5
-Release:        1%{?kat_build:.%kat_build}%{?dist}
5
+Release:        2%{?kat_build:.%kat_build}%{?dist}
6 6
 License:    	GPLv2
7 7
 URL:        	http://www.kernel.org/
8 8
 Group:        	System Environment/Kernel
... ...
@@ -62,6 +62,7 @@ Patch38:        0001-net-phy-mdio-bcm-unimac-fix-potential-NULL-dereferen.patch
62 62
 Patch40:        0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
63 63
 # Fix for CVE-2017-18224
64 64
 Patch41:        0001-ocfs2-ip_alloc_sem-should-be-taken-in-ocfs2_get_bloc.patch
65
+Patch42:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
65 66
 
66 67
 # For Spectre
67 68
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -188,6 +189,7 @@ This package contains the 'perf' performance analysis tools for Linux kernel.
188 188
 %patch38 -p1
189 189
 %patch40 -p1
190 190
 %patch41 -p1
191
+%patch42 -p1
191 192
 
192 193
 %patch52 -p1
193 194
 %patch53 -p1
... ...
@@ -371,6 +373,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
371 371
 /usr/share/doc/*
372 372
 
373 373
 %changelog
374
+*   Wed Jun 27 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-2
375
+-   Add rdrand-based RNG driver to enhance kernel entropy.
374 376
 *   Thu Jun 21 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.109-1
375 377
 -   Update to version 4.9.109
376 378
 *   Mon May 21 2018 Alexey Makhalov <amakhalov@vmware.com> 4.9.101-2
377 379
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+# Automatically load the following kernel modules on every boot.
1
+rdrand-rng
... ...
@@ -1,7 +1,7 @@
1 1
 Summary:          Systemd-233
2 2
 Name:             systemd
3 3
 Version:          233
4
-Release:          13%{?dist}
4
+Release:          14%{?dist}
5 5
 License:          LGPLv2+ and GPLv2+ and MIT
6 6
 URL:              http://www.freedesktop.org/wiki/Software/systemd/
7 7
 Group:            System Environment/Security
... ...
@@ -13,6 +13,7 @@ Source1:          99-vmware-hotplug.rules
13 13
 Source2:          50-security-hardening.conf
14 14
 Source3:          systemd.cfg
15 15
 Source4:          99-dhcp-en.network
16
+Source5:          10-rdrand-rng.conf
16 17
 
17 18
 Patch0:           01-enoX-uses-instance-number-for-vmware-hv.patch
18 19
 Patch1:           02-install-general-aliases.patch
... ...
@@ -151,6 +152,7 @@ rm %{buildroot}/lib/systemd/system/default.target
151 151
 ln -sfv multi-user.target %{buildroot}/lib/systemd/system/default.target
152 152
 install -dm 0755 %{buildroot}/%{_sysconfdir}/systemd/network
153 153
 install -m 0644 %{SOURCE4} %{buildroot}/%{_sysconfdir}/systemd/network
154
+install -m 0644 %{SOURCE5} %{buildroot}/%{_sysconfdir}/modules-load.d
154 155
 %find_lang %{name}
155 156
 
156 157
 %post
... ...
@@ -196,6 +198,7 @@ rm -rf %{buildroot}/*
196 196
 %dir %{_sysconfdir}/udev/hwdb.d
197 197
 %{_sysconfdir}/udev/rules.d/99-vmware-hotplug.rules
198 198
 %config(noreplace) %{_sysconfdir}/udev/udev.conf
199
+%config(noreplace) %{_sysconfdir}/modules-load.d/10-rdrand-rng.conf
199 200
 %config(noreplace) /boot/systemd.cfg
200 201
 %{_sysconfdir}/systemd/system/*
201 202
 /lib/udev/*
... ...
@@ -247,6 +250,8 @@ rm -rf %{buildroot}/*
247 247
 %files lang -f %{name}.lang
248 248
 
249 249
 %changelog
250
+*    Wed Jun 27 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu>  233-14
251
+-    Automatically load rdrand-rng kernel module on every boot.
250 252
 *    Wed Apr 11 2018 Xiaolin Li <xiaolinl@vmware.com>  233-13
251 253
 -    Build systemd with util-linux 2.32.
252 254
 *    Wed Mar 14 2018 Xiaolin Li <xiaolinl@vmware.com>  233-12