Skip to contentSkip to conversation

x86, kaslr: mix entropy sources together as needed

PATCH New Kees Cook 2 participants 3 messages Nov 11, 2013 – Nov 12, 2013 x86

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v4 1/1 x86, kaslr: mix entropy sources together as needed

Depending on availability, mix the RDRAND and RDTSC entropy together with XOR. Only when neither is available should the i8254 be used. Update the Kconfig documentation to reflect this. Additionally, since bits used for entropy is masked elsewhere, drop the needless masking in the get_random_long(). Similarly, use the entire TSC, not just the low 32 bits.

Finally, to improve the starting entropy, do a simple hashing of a build-time versions string and the boot-time boot_params structure for some additional level of unpredictability.

---
v4:
- rotate by odd bit count; HPA.
v3:
- do not limit ourself to the low 32 bits of TSC; Ingo Molnar.
v2:
- added build-time string to add to starting entropy; Ingo Molnar.

---
 arch/x86/Kconfig                |   14 +++++---
 arch/x86/boot/compressed/aslr.c |   73 ++++++++++++++++++++++++++++++---------
 2 files changed, 65 insertions(+), 22 deletions(-)
arch/x86/Kconfig +9 -5
index ee3b38363063..119455802d57 100644
@@ -1736,13 +1736,17 @@ config RANDOMIZE_BASE
 	   deters exploit attempts relying on knowledge of the location
 	   of kernel internals.
 
-	   Entropy is generated using the RDRAND instruction if it
-	   is supported.  If not, then RDTSC is used, if supported. If
-	   neither RDRAND nor RDTSC are supported, then no randomness
-	   is introduced.
+	   Entropy is generated using the RDRAND instruction if it is
+	   supported. If RDTSC is supported, it is used as well. If
+	   neither RDRAND nor RDTSC are supported, then randomness is
+	   read from the i8254 timer.
 
 	   The kernel will be offset by up to RANDOMIZE_BASE_MAX_OFFSET,
-	   and aligned according to PHYSICAL_ALIGN.
+	   and aligned according to PHYSICAL_ALIGN. Since the kernel is
+	   built using 2GiB addressing, and PHYSICAL_ALGIN must be at a
+	   minimum of 2MiB, only 10 bits of entropy is theoretically
+	   possible. At best, due to page table layouts, 64-bit can use
+	   9 bits of entropy and 32-bit uses 8 bits.
 
 config RANDOMIZE_BASE_MAX_OFFSET
 	hex "Maximum ASLR offset allowed"
arch/x86/boot/compressed/aslr.c +56 -17

96 lines not on this page · raw message

Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw

[tip:x86/kaslr] x86, kaslr: Mix entropy sources together as needed

x86, kaslr: Mix entropy sources together as needed
Commit-ID:  a653f3563c51c7bb7de63d607bef09d3baddaeb8
Gitweb:     http://git.kernel.org/tip/a653f3563c51c7bb7de63d607bef09d3baddaeb8
Author:     Kees Cook <[email protected]>
AuthorDate: Mon, 11 Nov 2013 14:28:39 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 11 Nov 2013 22:29:44 -0800

x86, kaslr: Mix entropy sources together as needed

Depending on availability, mix the RDRAND and RDTSC entropy together with XOR. Only when neither is available should the i8254 be used. Update the Kconfig documentation to reflect this. Additionally, since bits used for entropy is masked elsewhere, drop the needless masking in the get_random_long(). Similarly, use the entire TSC, not just the low 32 bits.

Finally, to improve the starting entropy, do a simple hashing of a build-time versions string and the boot-time boot_params structure for some additional level of unpredictability.

---
 arch/x86/Kconfig                | 14 +++++---
 arch/x86/boot/compressed/aslr.c | 73 +++++++++++++++++++++++++++++++----------
 2 files changed, 65 insertions(+), 22 deletions(-)
arch/x86/Kconfig +9 -5
index 51f4399..596cd9e 100644
@@ -1735,13 +1735,17 @@ config RANDOMIZE_BASE
 	   deters exploit attempts relying on knowledge of the location
 	   of kernel internals.
 
-	   Entropy is generated using the RDRAND instruction if it
-	   is supported.  If not, then RDTSC is used, if supported. If
-	   neither RDRAND nor RDTSC are supported, then no randomness
-	   is introduced.
+	   Entropy is generated using the RDRAND instruction if it is
+	   supported. If RDTSC is supported, it is used as well. If
+	   neither RDRAND nor RDTSC are supported, then randomness is
+	   read from the i8254 timer.
 
 	   The kernel will be offset by up to RANDOMIZE_BASE_MAX_OFFSET,
-	   and aligned according to PHYSICAL_ALIGN.
+	   and aligned according to PHYSICAL_ALIGN. Since the kernel is
+	   built using 2GiB addressing, and PHYSICAL_ALGIN must be at a
+	   minimum of 2MiB, only 10 bits of entropy is theoretically
+	   possible. At best, due to page table layouts, 64-bit can use
+	   9 bits of entropy and 32-bit uses 8 bits.
 
 config RANDOMIZE_BASE_MAX_OFFSET
 	hex "Maximum ASLR offset allowed"
arch/x86/boot/compressed/aslr.c +56 -17

96 lines not on this page · raw message

#2 lore raw

[tip:x86/kaslr] x86, kaslr: Add a circular multiply for better bit diffusion

In reply to Kees Cook
x86, kaslr: Add a circular multiply for better bit diffusion
Commit-ID:  e8236c4d9338d52d0f2fcecc0b792ac0542e4ee9
Gitweb:     http://git.kernel.org/tip/e8236c4d9338d52d0f2fcecc0b792ac0542e4ee9
Author:     H. Peter Anvin <[email protected]>
AuthorDate: Mon, 11 Nov 2013 22:45:20 -0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 11 Nov 2013 23:05:49 -0800

x86, kaslr: Add a circular multiply for better bit diffusion

If we don't have RDRAND (in which case nothing else *should* matter), most sources have a highly biased entropy distribution. Use a circular multiply to diffuse the entropic bits. A circular multiply is a good operation for this: it is cheap on standard hardware and because it is symmetric (unlike an ordinary multiply) it doesn't introduce its own bias.

---
 arch/x86/boot/compressed/aslr.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/boot/compressed/aslr.c +11 -0
index 8746487..38a07cc 100644
@@ -64,6 +64,11 @@ static unsigned long get_random_boot(void)
 
 static unsigned long get_random_long(void)
 {
+#ifdef CONFIG_X86_64
+	const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
+#else
+	const unsigned long mix_const = 0x3f39e593UL;
+#endif
 	unsigned long raw, random = get_random_boot();
 	bool use_i8254 = true;
 
@@ -90,6 +95,12 @@ static unsigned long get_random_long(void)
 		random ^= i8254();
 	}
 
+	/* Circular multiply for better bit diffusion */
+	asm("mul %3"
+	    : "=a" (random), "=d" (raw)
+	    : "a" (random), "rm" (mix_const));
+	random += raw;
+
 	debug_putstr("...\n");
 
 	return random;