Browse code

kernels: Improve error-handling of rdrand-rng kernel driver

Improve the rdrand-rng driver to be robust in the face of various
errors and edge cases. Specifically,

- arch_get_random_long() and arch_get_random_seed_long() can fail.
Check their return values to deal with this appropriately.

- The number of random bytes requested can be arbitrary; so fill in
the supplied buffer carefully.

- Prefer rdseed over rdrand whenever possible.

Thanks to Joao Lima and Lonnie Abelbeck (@abelbeck) for suggesting
some of these improvements.

Change-Id: I4612215c3f4c66d216c875cea7ae09ab6aedaace
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/5841
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Srinidhi Rao <srinidhir@vmware.com>
Reviewed-by: Srivatsa S. Bhat <srivatsab@vmware.com>

Srivatsa S. Bhat authored on 2018/10/03 07:21:20
Showing 5 changed files
... ...
@@ -12,14 +12,15 @@ entropy-starved virtual machines.
12 12
 
13 13
 Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
14 14
 ---
15
- drivers/char/hw_random/Kconfig      | 14 +++++++++
15
+
16
+ drivers/char/hw_random/Kconfig      | 14 ++++++++
16 17
  drivers/char/hw_random/Makefile     |  1 +
17
- drivers/char/hw_random/rdrand-rng.c | 61 +++++++++++++++++++++++++++++++++++++
18
- 3 files changed, 76 insertions(+)
18
+ drivers/char/hw_random/rdrand-rng.c | 72 +++++++++++++++++++++++++++++++++++++
19
+ 3 files changed, 87 insertions(+)
19 20
  create mode 100644 drivers/char/hw_random/rdrand-rng.c
20 21
 
21 22
 diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
22
-index 200dab5..cc3a67d 100644
23
+index 200dab5..8b6d236 100644
23 24
 --- a/drivers/char/hw_random/Kconfig
24 25
 +++ b/drivers/char/hw_random/Kconfig
25 26
 @@ -62,6 +62,20 @@ config HW_RANDOM_AMD
... ...
@@ -32,7 +33,7 @@ index 200dab5..cc3a67d 100644
32 32
 +	default HW_RANDOM
33 33
 +	---help---
34 34
 +	  This driver provides kernel-side support for a Random Number
35
-+	  Generator that uses the 'rdrand' instruction on modern Intel
35
++	  Generator that uses the RDRAND/RDSEED instructions on modern Intel
36 36
 +	  and AMD CPUs.
37 37
 +
38 38
 +	  To compile this driver as a module, choose M here: the
... ...
@@ -57,13 +58,13 @@ index 5f52b1e..5b92c8e 100644
57 57
  obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
58 58
 diff --git a/drivers/char/hw_random/rdrand-rng.c b/drivers/char/hw_random/rdrand-rng.c
59 59
 new file mode 100644
60
-index 0000000..e1cf7f3
60
+index 0000000..ba017f3
61 61
 --- /dev/null
62 62
 +++ b/drivers/char/hw_random/rdrand-rng.c
63
-@@ -0,0 +1,61 @@
63
+@@ -0,0 +1,72 @@
64 64
 +// SPDX-License-Identifier: GPL-2.0
65 65
 +/*
66
-+ * RNG driver that uses the 'rdrand' instruction (found on modern
66
++ * RNG driver that uses the RDRAND/RDSEED instructions (found on modern
67 67
 + * Intel and AMD CPUs).
68 68
 + *
69 69
 + * Author: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
... ...
@@ -77,18 +78,27 @@ index 0000000..e1cf7f3
77 77
 +
78 78
 +#define PFX	KBUILD_MODNAME ": "
79 79
 +
80
-+static int rdrand_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
80
++static int rdrand_rng_read(struct hwrng *rng, void *buf, size_t max_bytes, bool wait)
81 81
 +{
82
-+	unsigned long *data = buf;
83
-+	size_t read = 0;
84
-+
85
-+	while (read < max) {
86
-+		arch_get_random_long(data);
87
-+		data++;
88
-+		read += sizeof(unsigned long);
82
++	char *p = buf;
83
++	size_t read_bytes = 0;
84
++
85
++	while (max_bytes) {
86
++		unsigned long v;
87
++		size_t chunk = min(max_bytes, (int)sizeof(unsigned long));
88
++
89
++		if (unlikely(!arch_get_random_seed_long(&v)) &&
90
++		    unlikely(!arch_get_random_long(&v))) {
91
++			break;
92
++		}
93
++
94
++		memcpy(p, &v, chunk);
95
++		p += chunk;
96
++		max_bytes -= chunk;
97
++		read_bytes += chunk;
89 98
 +	}
90 99
 +
91
-+	return read;
100
++	return read_bytes;
92 101
 +}
93 102
 +
94 103
 +static struct hwrng rdrand_rng = {
... ...
@@ -101,8 +111,10 @@ index 0000000..e1cf7f3
101 101
 +{
102 102
 +	int err = -ENODEV;
103 103
 +
104
-+	if (!arch_has_random())
105
-+		return err; /* rdrand not available. */
104
++	if (!arch_has_random_seed() && !arch_has_random()) {
105
++		pr_err(PFX "Neither RDSEED nor RDRAND is available.\n");
106
++		return err;
107
++	}
106 108
 +
107 109
 +	err = hwrng_register(&rdrand_rng);
108 110
 +	if (err)
... ...
@@ -120,7 +132,7 @@ index 0000000..e1cf7f3
120 120
 +module_exit(mod_exit);
121 121
 +
122 122
 +MODULE_AUTHOR("Srivatsa S. Bhat <srivatsa@csail.mit.edu>");
123
-+MODULE_DESCRIPTION("H/W RNG driver for x86 CPUs that support rdrand");
123
++MODULE_DESCRIPTION("H/W RNG driver for x86 CPUs that support RDRAND/RDSEED");
124 124
 +MODULE_LICENSE("GPL");
125 125
 -- 
126 126
 2.7.4
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-aws
4 4
 Version:        4.9.130
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
... ...
@@ -443,6 +443,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
443 443
 /usr/share/doc/*
444 444
 
445 445
 %changelog
446
+*   Tue Oct 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.130-2
447
+-   Improve error-handling of rdrand-rng kernel driver.
446 448
 *   Mon Oct 01 2018 srinidhira0 <srinidhir@vmware.com> 4.9.130-1
447 449
 -   Update to version 4.9.130
448 450
 *   Mon Sep 10 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.124-2
... ...
@@ -461,7 +463,7 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
461 461
 -   Update to version 4.9.114
462 462
 *   Thu Jul 19 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.111-5
463 463
 -   Apply out-of-tree patches needed for AppArmor.
464
-*   Thu Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-4
464
+*   Tue Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-4
465 465
 -   Fix CVE-2018-10322
466 466
 *   Thu Jul 12 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-3
467 467
 -   Fix CVE-2017-18232, CVE-2017-18249 and CVE-2018-10323
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-esx
4 4
 Version:        4.9.130
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
... ...
@@ -236,6 +236,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
236 236
 /usr/src/linux-headers-%{uname_r}
237 237
 
238 238
 %changelog
239
+*   Tue Oct 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.130-2
240
+-   Improve error-handling of rdrand-rng kernel driver.
239 241
 *   Mon Oct 01 2018 srinidhira0 <srinidhir@vmware.com> 4.9.130-1
240 242
 -   Update to version 4.9.130
241 243
 *   Mon Sep 10 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.124-2
... ...
@@ -252,7 +254,7 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
252 252
 -   Update to version 4.9.116 and clear stack on fork.
253 253
 *   Mon Jul 23 2018 srinidhira0 <srinidhir@vmware.com> 4.9.114-1
254 254
 -   Update to version 4.9.114
255
-*   Thu Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-3
255
+*   Tue Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-3
256 256
 -   Fix CVE-2018-10322
257 257
 *   Thu Jul 12 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-2
258 258
 -   Fix CVE-2017-18232, CVE-2017-18249 and CVE-2018-10323
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux-secure
4 4
 Version:        4.9.130
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
... ...
@@ -334,6 +334,8 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
334 334
 /usr/src/linux-headers-%{uname_r}
335 335
 
336 336
 %changelog
337
+*   Tue Oct 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.130-2
338
+-   Improve error-handling of rdrand-rng kernel driver.
337 339
 *   Mon Oct 01 2018 srinidhira0 <srinidhir@vmware.com> 4.9.130-1
338 340
 -   Update to version 4.9.130
339 341
 *   Mon Sep 10 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.124-2
... ...
@@ -352,7 +354,7 @@ ln -sf linux-%{uname_r}.cfg /boot/photon.cfg
352 352
 -   Update to version 4.9.114
353 353
 *   Thu Jul 19 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.111-4
354 354
 -   Apply out-of-tree patches needed for AppArmor.
355
-*   Thu Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-3
355
+*   Tue Jul 17 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-3
356 356
 -   Fix CVE-2018-10322
357 357
 *   Thu Jul 12 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.111-2
358 358
 -   Fix CVE-2017-18232, CVE-2017-18249 and CVE-2018-10323
... ...
@@ -2,7 +2,7 @@
2 2
 Summary:        Kernel
3 3
 Name:           linux
4 4
 Version:        4.9.130
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
... ...
@@ -366,6 +366,8 @@ ln -sf %{name}-%{uname_r}.cfg /boot/photon.cfg
366 366
 /usr/share/doc/*
367 367
 
368 368
 %changelog
369
+*   Tue Oct 02 2018 Srivatsa S. Bhat <srivatsa@csail.mit.edu> 4.9.130-2
370
+-   Improve error-handling of rdrand-rng kernel driver.
369 371
 *   Mon Oct 01 2018 srinidhira0 <srinidhir@vmware.com> 4.9.130-1
370 372
 -   Update to version 4.9.130
371 373
 *   Mon Sep 10 2018 Srinidhi Rao <srinidhir@vmware.com> 4.9.124-2