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: I3196ddfe3561c1a15276d08a1fdba73981bfe6b0
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/5309
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/29 05:49:03
Showing 7 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,127 @@
0
+From c3e6a24425bc9986da0d0a5a8c92e0b9ed7f18fa Mon Sep 17 00:00:00 2001
1
+From: "Srivatsa S. Bhat" <srivatsa@csail.mit.edu>
2
+Date: Thu, 28 Jun 2018 08:51:18 -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 dbf2271..b4e558d 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 5ad3976..c186ddb 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
+
... ...
@@ -2298,6 +2298,7 @@ CONFIG_HW_RANDOM=m
2298 2298
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2299 2299
 CONFIG_HW_RANDOM_INTEL=m
2300 2300
 CONFIG_HW_RANDOM_AMD=m
2301
+CONFIG_HW_RANDOM_RDRAND=m
2301 2302
 CONFIG_HW_RANDOM_VIA=m
2302 2303
 CONFIG_HW_RANDOM_VIRTIO=m
2303 2304
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -1788,7 +1788,12 @@ CONFIG_SERIAL_CORE_CONSOLE=y
1788 1788
 # CONFIG_SERIAL_FSL_LPUART is not set
1789 1789
 # CONFIG_TTY_PRINTK is not set
1790 1790
 # CONFIG_IPMI_HANDLER is not set
1791
-# CONFIG_HW_RANDOM is not set
1791
+CONFIG_HW_RANDOM=m
1792
+# CONFIG_HW_RANDOM_TIMERIOMEM is not set
1793
+# CONFIG_HW_RANDOM_INTEL is not set
1794
+# CONFIG_HW_RANDOM_AMD is not set
1795
+CONFIG_HW_RANDOM_RDRAND=m
1796
+# CONFIG_HW_RANDOM_VIA is not set
1792 1797
 # CONFIG_NVRAM is not set
1793 1798
 # CONFIG_R3964 is not set
1794 1799
 # CONFIG_APPLICOM is not set
... ...
@@ -2322,6 +2327,7 @@ CONFIG_USB_STORAGE=m
2322 2322
 # CONFIG_USB_EZUSB_FX2 is not set
2323 2323
 # CONFIG_USB_HSIC_USB3503 is not set
2324 2324
 # CONFIG_USB_LINK_LAYER_TEST is not set
2325
+# CONFIG_USB_CHAOSKEY is not set
2325 2326
 
2326 2327
 #
2327 2328
 # USB Physical Layer drivers
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:       Kernel
3 3
 Name:          linux-esx
4 4
 Version:       4.4.138
5
-Release:       1%{?dist}
5
+Release:       2%{?dist}
6 6
 License:       GPLv2
7 7
 URL:           http://www.kernel.org/
8 8
 Group:         System Environment/Kernel
... ...
@@ -46,6 +46,8 @@ Patch30:       0001-net-phy-mdio-bcm-unimac-fix-potential-NULL-dereferen.patch
46 46
 Patch31:       0001-ocfs2-subsystem.su_mutex-is-required-while-accessing.patch
47 47
 # Fix for CVE-2017-18241
48 48
 Patch33:       0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
49
+Patch34:       0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
50
+
49 51
 
50 52
 # For Spectre
51 53
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -234,6 +236,7 @@ The Linux package contains the Linux kernel doc files
234 234
 %patch30 -p1
235 235
 %patch31 -p1
236 236
 %patch33 -p1
237
+%patch34 -p1
237 238
 
238 239
 %patch52 -p1
239 240
 %patch55 -p1
... ...
@@ -439,6 +442,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
439 439
 /usr/src/linux-headers-%{uname_r}
440 440
 
441 441
 %changelog
442
+*   Thu Jun 28 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.138-2
443
+-   Add rdrand-based RNG driver to enhance kernel entropy.
442 444
 *   Mon Jun 25 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.138-1
443 445
 -   Update to version 4.4.138
444 446
 *   Thu Jun 14 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.137-2
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux
4 4
 Version:    	4.4.138
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
... ...
@@ -46,6 +46,7 @@ Patch23:        0001-ocfs2-subsystem.su_mutex-is-required-while-accessing.patch
46 46
 # Fix for CVE-2017-18241
47 47
 Patch25:        0001-f2fs-fix-a-panic-caused-by-NULL-flush_cmd_control.patch
48 48
 Patch26:        Implement-the-f-xattrat-family-of-functions.patch
49
+Patch27:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
49 50
 
50 51
 # For Spectre
51 52
 Patch52: 0141-locking-barriers-introduce-new-observable-speculatio.patch
... ...
@@ -266,6 +267,7 @@ This package contains the 'perf' performance analysis tools for Linux kernel.
266 266
 %patch23 -p1
267 267
 %patch25 -p1
268 268
 %patch26 -p1
269
+%patch27 -p1
269 270
 
270 271
 %patch52 -p1
271 272
 %patch55 -p1
... ...
@@ -539,6 +541,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
539 539
 /usr/share/perf-core
540 540
 
541 541
 %changelog
542
+*   Thu Jun 28 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.138-2
543
+-   Add rdrand-based RNG driver to enhance kernel entropy.
542 544
 *   Mon Jun 25 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.138-1
543 545
 -   Update to version 4.4.138
544 546
 *   Thu Jun 14 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.4.137-2
545 547
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-228
2 2
 Name:             systemd
3 3
 Version:          228
4
-Release:          45%{?dist}
4
+Release:          46%{?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
... ...
@@ -12,6 +12,7 @@ Source0:          %{name}-%{version}.tar.gz
12 12
 Source1:          99-vmware-hotplug.rules
13 13
 Source2:          50-security-hardening.conf
14 14
 Source3:          filesystem.conf
15
+Source4:          10-rdrand-rng.conf
15 16
 #patch for ostree
16 17
 Patch0:           systemd-228-mount.patch
17 18
 Patch1:           01-enoX-uses-instance-number-for-vmware-hv.patch
... ...
@@ -170,6 +171,7 @@ find %{buildroot}%{_libdir} -name '*.la' -delete
170 170
 install -Dm 0644 %{SOURCE1} %{buildroot}/%{_sysconfdir}/udev/rules.d
171 171
 install -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/sysctl.d
172 172
 install -m 0644 %{SOURCE3} %{buildroot}/usr/lib/tmpfiles.d/
173
+install -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/modules-load.d
173 174
 rm %{buildroot}/lib/systemd/system/default.target
174 175
 ln -sfv multi-user.target %{buildroot}/lib/systemd/system/default.target
175 176
 install -vdm 755 %{buildroot}/%{_sysconfdir}/systemd/network
... ...
@@ -221,6 +223,7 @@ rm -rf %{buildroot}/*
221 221
 %config(noreplace) %{_sysconfdir}/systemd/timesyncd.conf
222 222
 %config(noreplace) %{_sysconfdir}/systemd/bootchart.conf
223 223
 %config(noreplace) %{_sysconfdir}/pam.d/systemd-user
224
+%config(noreplace) %{_sysconfdir}/modules-load.d/10-rdrand-rng.conf
224 225
 %dir %{_sysconfdir}/systemd/network
225 226
 %config(noreplace) %{_sysconfdir}/systemd/network/99-dhcp-en.network
226 227
 %dir %{_sysconfdir}/udev
... ...
@@ -246,7 +249,8 @@ rm -rf %{buildroot}/*
246 246
 
247 247
 
248 248
 %changelog
249
-%changelog
249
+*    Thu Jun 28 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 228-46
250
+-    Automatically load rdrand-rng kernel module on every boot.
250 251
 *    Thu Mar 15 2018 Xiaolin Li <xiaolinl@vmware.com>  228-45
251 252
 -    Fix CVE-2017-18078.
252 253
 *    Wed Nov 29 2017 Anish Swaminathan <anishs@vmware.com> 228-44