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: I8efb0c8fa02d5adef26383edfbea673e5561bc08
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/5728
Reviewed-by: Sharath George
Tested-by: Sharath George

Srivatsa S. Bhat authored on 2018/09/19 10:58:35
Showing 11 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,138 @@
0
+From 0fd09a9a3418124e59cded88699d4c6bcdb811b2 Mon Sep 17 00:00:00 2001
1
+From: "Srivatsa S. Bhat" <srivatsa@csail.mit.edu>
2
+Date: Tue, 18 Sep 2018 18:33:06 -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/rdseed instructions available on modern Intel and AMD CPUs.
8
+
9
+This can be used to feed the kernel's entropy pool on entropy-starved
10
+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 | 72 +++++++++++++++++++++++++++++++++++++
17
+ 3 files changed, 87 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 95a031e..c7e7ce7 100644
22
+--- a/drivers/char/hw_random/Kconfig
23
+@@ -60,6 +60,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/RDSEED instructions 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 f3728d0..c677d3a 100644
46
+--- a/drivers/char/hw_random/Makefile
47
+@@ -8,6 +8,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..ba017f3
58
+--- /dev/null
59
+@@ -0,0 +1,72 @@
60
++// SPDX-License-Identifier: GPL-2.0
61
++/*
62
++ * RNG driver that uses the RDRAND/RDSEED instructions (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_bytes, bool wait)
77
++{
78
++	char *p = buf;
79
++	size_t read_bytes = 0;
80
++
81
++	while (max_bytes) {
82
++		unsigned long v;
83
++		size_t chunk = min(max_bytes, (int)sizeof(unsigned long));
84
++
85
++		if (unlikely(!arch_get_random_seed_long(&v)) &&
86
++		    unlikely(!arch_get_random_long(&v))) {
87
++			break;
88
++		}
89
++
90
++		memcpy(p, &v, chunk);
91
++		p += chunk;
92
++		max_bytes -= chunk;
93
++		read_bytes += chunk;
94
++	}
95
++
96
++	return read_bytes;
97
++}
98
++
99
++static struct hwrng rdrand_rng = {
100
++	.name		= KBUILD_MODNAME,
101
++	.quality	= 1000,
102
++	.read		= rdrand_rng_read,
103
++};
104
++
105
++static int __init mod_init(void)
106
++{
107
++	int err = -ENODEV;
108
++
109
++	if (!arch_has_random_seed() && !arch_has_random()) {
110
++		pr_err(PFX "Neither RDSEED nor RDRAND is available.\n");
111
++		return err;
112
++	}
113
++
114
++	err = hwrng_register(&rdrand_rng);
115
++	if (err)
116
++		pr_err(PFX "RNG registration failed (%d)\n", err);
117
++
118
++	return err;
119
++}
120
++
121
++static void __exit mod_exit(void)
122
++{
123
++	hwrng_unregister(&rdrand_rng);
124
++}
125
++
126
++module_init(mod_init);
127
++module_exit(mod_exit);
128
++
129
++MODULE_AUTHOR("Srivatsa S. Bhat <srivatsa@csail.mit.edu>");
130
++MODULE_DESCRIPTION("H/W RNG driver for x86 CPUs that support RDRAND/RDSEED");
131
++MODULE_LICENSE("GPL");
132
+-- 
133
+2.7.4
134
+
... ...
@@ -2738,6 +2738,7 @@ CONFIG_HW_RANDOM=m
2738 2738
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2739 2739
 CONFIG_HW_RANDOM_INTEL=m
2740 2740
 CONFIG_HW_RANDOM_AMD=m
2741
+CONFIG_HW_RANDOM_RDRAND=m
2741 2742
 CONFIG_HW_RANDOM_VIA=m
2742 2743
 CONFIG_HW_RANDOM_VIRTIO=m
2743 2744
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -2510,6 +2510,7 @@ CONFIG_HW_RANDOM=m
2510 2510
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2511 2511
 CONFIG_HW_RANDOM_INTEL=m
2512 2512
 CONFIG_HW_RANDOM_AMD=m
2513
+CONFIG_HW_RANDOM_RDRAND=m
2513 2514
 CONFIG_HW_RANDOM_VIA=m
2514 2515
 CONFIG_HW_RANDOM_VIRTIO=m
2515 2516
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -1976,7 +1976,12 @@ CONFIG_SERIAL_CORE_CONSOLE=y
1976 1976
 # CONFIG_SERIAL_DEV_BUS is not set
1977 1977
 # CONFIG_TTY_PRINTK is not set
1978 1978
 # CONFIG_IPMI_HANDLER is not set
1979
-# CONFIG_HW_RANDOM is not set
1979
+CONFIG_HW_RANDOM=m
1980
+# CONFIG_HW_RANDOM_TIMERIOMEM is not set
1981
+# CONFIG_HW_RANDOM_INTEL is not set
1982
+# CONFIG_HW_RANDOM_AMD is not set
1983
+CONFIG_HW_RANDOM_RDRAND=m
1984
+# CONFIG_HW_RANDOM_VIA is not set
1980 1985
 # CONFIG_NVRAM is not set
1981 1986
 # CONFIG_R3964 is not set
1982 1987
 # CONFIG_APPLICOM is not set
... ...
@@ -2551,6 +2556,7 @@ CONFIG_USB_STORAGE=m
2551 2551
 # CONFIG_USB_HSIC_USB3503 is not set
2552 2552
 # CONFIG_USB_HSIC_USB4604 is not set
2553 2553
 # CONFIG_USB_LINK_LAYER_TEST is not set
2554
+# CONFIG_USB_CHAOSKEY is not set
2554 2555
 
2555 2556
 #
2556 2557
 # USB Physical Layer drivers
... ...
@@ -2468,6 +2468,7 @@ CONFIG_HW_RANDOM=m
2468 2468
 CONFIG_HW_RANDOM_TIMERIOMEM=m
2469 2469
 CONFIG_HW_RANDOM_INTEL=m
2470 2470
 CONFIG_HW_RANDOM_AMD=m
2471
+CONFIG_HW_RANDOM_RDRAND=m
2471 2472
 CONFIG_HW_RANDOM_VIA=m
2472 2473
 CONFIG_HW_RANDOM_VIRTIO=m
2473 2474
 CONFIG_HW_RANDOM_TPM=m
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-aws
4 4
 Version:        4.14.54
5
-Release:        3%{?kat_build:.%kat_build}%{?dist}
5
+Release:        4%{?kat_build:.%kat_build}%{?dist}
6 6
 License:    	GPLv2
7 7
 URL:        	http://www.kernel.org/
8 8
 Group:        	System Environment/Kernel
... ...
@@ -36,6 +36,7 @@ Patch28:        kvm-dont-accept-wrong-gsi-values.patch
36 36
 # Out-of-tree patches from AppArmor:
37 37
 Patch29:        0001-apparmor-add-base-infastructure-for-socket-mediation.patch
38 38
 Patch30:        0002-apparmor-af_unix-mediation.patch
39
+Patch31:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
39 40
 
40 41
 %if 0%{?kat_build:1}
41 42
 Patch1000:	%{kat_build}.patch
... ...
@@ -124,6 +125,7 @@ This package contains the 'perf' performance analysis tools for Linux kernel.
124 124
 %patch28 -p1
125 125
 %patch29 -p1
126 126
 %patch30 -p1
127
+%patch31 -p1
127 128
 
128 129
 %if 0%{?kat_build:1}
129 130
 %patch1000 -p1
... ...
@@ -311,6 +313,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
311 311
 /usr/share/doc/*
312 312
 
313 313
 %changelog
314
+*   Tue Sep 18 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-4
315
+-   Add rdrand-based RNG driver to enhance kernel entropy.
314 316
 *   Sun Sep 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-3
315 317
 -   Add full retpoline support by building with retpoline-enabled gcc.
316 318
 *   Thu Aug 30 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-2
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-esx
4 4
 Version:        4.14.54
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
... ...
@@ -35,6 +35,7 @@ Patch21:        vmware-balloon-late-initcall.patch
35 35
 Patch22:        add-sysctl-to-disallow-unprivileged-CLONE_NEWUSER-by-default.patch
36 36
 # Fix CVE-2017-1000252
37 37
 Patch24:        kvm-dont-accept-wrong-gsi-values.patch
38
+Patch25:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
38 39
 
39 40
 
40 41
 BuildRequires: bc
... ...
@@ -90,6 +91,7 @@ The Linux package contains the Linux kernel doc files
90 90
 %patch21 -p1
91 91
 %patch22 -p1
92 92
 %patch24 -p1
93
+%patch25 -p1
93 94
 
94 95
 %build
95 96
 # patch vmw_balloon driver
... ...
@@ -186,6 +188,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
186 186
 /usr/src/linux-headers-%{uname_r}
187 187
 
188 188
 %changelog
189
+*   Tue Sep 18 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-3
190
+-   Add rdrand-based RNG driver to enhance kernel entropy.
189 191
 *   Sun Sep 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-2
190 192
 -   Add full retpoline support by building with retpoline-enabled gcc.
191 193
 *   Mon Jul 09 2018 Him Kalyan Bordoloi <bordoloih@vmware.com> 4.14.54-1
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-secure
4 4
 Version:        4.14.54
5
-Release:        3%{?kat_build:.%kat_build}%{?dist}
5
+Release:        4%{?kat_build:.%kat_build}%{?dist}
6 6
 License:        GPLv2
7 7
 URL:            http://www.kernel.org/
8 8
 Group:          System Environment/Kernel
... ...
@@ -35,6 +35,7 @@ Patch31:        kvm-dont-accept-wrong-gsi-values.patch
35 35
 # Out-of-tree patches from AppArmor:
36 36
 Patch32:        0001-apparmor-add-base-infastructure-for-socket-mediation.patch
37 37
 Patch33:        0002-apparmor-af_unix-mediation.patch
38
+Patch34:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
38 39
 # NSX requirements (should be removed)
39 40
 Patch99:        LKCM.patch
40 41
 
... ...
@@ -100,6 +101,7 @@ The Linux package contains the Linux kernel doc files
100 100
 %patch31 -p1
101 101
 %patch32 -p1
102 102
 %patch33 -p1
103
+%patch34 -p1
103 104
 
104 105
 pushd ..
105 106
 %patch99 -p0
... ...
@@ -227,6 +229,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
227 227
 /usr/src/linux-headers-%{uname_r}
228 228
 
229 229
 %changelog
230
+*   Tue Sep 18 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-4
231
+-   Add rdrand-based RNG driver to enhance kernel entropy.
230 232
 *   Sun Sep 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-3
231 233
 -   Add full retpoline support by building with retpoline-enabled gcc.
232 234
 *   Thu Aug 30 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-2
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux
4 4
 Version:        4.14.54
5
-Release:        6%{?kat_build:.%kat_build}%{?dist}
5
+Release:        7%{?kat_build:.%kat_build}%{?dist}
6 6
 License:    	GPLv2
7 7
 URL:        	http://www.kernel.org/
8 8
 Group:        	System Environment/Kernel
... ...
@@ -44,6 +44,8 @@ Patch28:        kvm-dont-accept-wrong-gsi-values.patch
44 44
 # Out-of-tree patches from AppArmor:
45 45
 Patch29:        0001-apparmor-add-base-infastructure-for-socket-mediation.patch
46 46
 Patch30:        0002-apparmor-af_unix-mediation.patch
47
+Patch31:        0001-hwrng-rdrand-Add-RNG-driver-based-on-x86-rdrand-inst.patch
48
+
47 49
 
48 50
 %if 0%{?kat_build:1}
49 51
 Patch1000:	%{kat_build}.patch
... ...
@@ -146,6 +148,7 @@ Kernel Device Tree Blob files for Raspberry Pi3
146 146
 %patch28 -p1
147 147
 %patch29 -p1
148 148
 %patch30 -p1
149
+%patch31 -p1
149 150
 
150 151
 %if 0%{?kat_build:1}
151 152
 %patch1000 -p1
... ...
@@ -366,6 +369,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
366 366
 %endif
367 367
 
368 368
 %changelog
369
+*   Tue Sep 18 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-7
370
+-   Add rdrand-based RNG driver to enhance kernel entropy.
369 371
 *   Sun Sep 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-6
370 372
 -   Add full retpoline support by building with retpoline-enabled gcc.
371 373
 *   Thu Aug 30 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.14.54-5
372 374
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-239
2 2
 Name:             systemd
3 3
 Version:          239
4
-Release:          1%{?dist}
4
+Release:          2%{?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
... ...
@@ -133,6 +134,7 @@ rm %{buildroot}/lib/systemd/system/default.target
133 133
 ln -sfv multi-user.target %{buildroot}/lib/systemd/system/default.target
134 134
 install -dm 0755 %{buildroot}/%{_sysconfdir}/systemd/network
135 135
 install -m 0644 %{SOURCE4} %{buildroot}/%{_sysconfdir}/systemd/network
136
+install -m 0644 %{SOURCE5} %{buildroot}%{_sysconfdir}/modules-load.d
136 137
 %find_lang %{name} ../%{name}.lang
137 138
 
138 139
 %post
... ...
@@ -173,6 +175,7 @@ rm -rf %{buildroot}/*
173 173
 %config(noreplace) %{_sysconfdir}/systemd/coredump.conf
174 174
 %config(noreplace) %{_sysconfdir}/systemd/timesyncd.conf
175 175
 %config(noreplace) %{_sysconfdir}/pam.d/systemd-user
176
+%config(noreplace) %{_sysconfdir}/modules-load.d/10-rdrand-rng.conf
176 177
 %config(noreplace) %{_sysconfdir}/systemd/network/99-dhcp-en.network
177 178
 
178 179
 %dir %{_sysconfdir}/udev
... ...
@@ -237,6 +240,8 @@ rm -rf %{buildroot}/*
237 237
 %files lang -f %{name}.lang
238 238
 
239 239
 %changelog
240
+*    Tue Sep 18 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 239-2
241
+-    Automatically load rdrand-rng kernel module on every boot.
240 242
 *    Tue Aug 28 2018 Anish Swaminathan <anishs@vmware.com>  239-1
241 243
 -    Update systemd to 239
242 244
 *    Wed Apr 11 2018 Xiaolin Li <xiaolinl@vmware.com>  236-3