Skip to contentSkip to conversation

x86, kaslr: mix entropy sources together as needed

PATCH Superseded Kees Cook 2 participants 3 messages Nov 11, 2013 x86

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 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().

Finally, to improve the starting entropy, do a simple hashing of the boot_params structure for some additional level of unpredictability.

---
 arch/x86/Kconfig                |   14 +++++++----
 arch/x86/boot/compressed/aslr.c |   52 ++++++++++++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 20 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 +37 -15

71 lines not on this page · raw message

Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 24 lines · Kees Cook

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().

Finally, to improve the starting entropy, do a simple hashing of the boot_params structure for some additional level of unpredictability.

---
 arch/x86/Kconfig                |   14 +++++++----
 arch/x86/boot/compressed/aslr.c |   52 ++++++++++++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 20 deletions(-)
arch/x86/Kconfig +1 -0
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

arch/x86/Kconfig config RANDOMIZE_BASE · line 1749

Quoted 10 lines
arch/x86/Kconfig +8 -0
+	   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. 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.

Perfect!

arch/x86/Kconfig config RANDOMIZE_BASE · line 1751

Quoted 2 lines
arch/x86/Kconfig +2 -0
+/* Simple way to create an alternate starting entropy. */
+static unsigned long get_boot_hash(void)

s/get_random_boot, signifying that we want to get some initial randomness?

Btw., could we also add some build build time source of randomness as well? That won't help distro kernels which all share the same build but it would be a nice touch for self-built kernels and Gentoo systems.

arch/x86/Kconfig config RANDOMIZE_BASE

Quoted 12 lines
arch/x86/Kconfig +12 -0
+{
+	int i;
+	unsigned long hash = 0;
+	unsigned long *ptr = (unsigned long *)real_mode;
+
+	for (i = 0; i < sizeof(*real_mode) / sizeof(hash); i++) {
+		/* Rotate and XOR */
+		hash = (hash << ((sizeof(hash) - 1) * 8)) | (hash >> 8);
+		hash ^= ptr[i];
+	}
+
+	return hash;

Looks mostly good, but I'm too tired to ack this bit now, maybe hpa will have a look :-)

Quoted 17 lines
arch/x86/Kconfig +13 -0
+}
+
 static unsigned long get_random_long(void)
 {
+	unsigned long random = get_boot_hash();
+	bool use_i8254 = true;
+
+	debug_putstr("KASLR using");
 
 	if (has_cpuflag(X86_FEATURE_RDRAND)) {
+		unsigned long raw;
+
+		debug_putstr(" RDRAND");
+		if (rdrand_long(&raw)) {
+			random ^= raw;
+			use_i8254 = false;
+		}

arch/x86/Kconfig

Quoted 10 lines
arch/x86/Kconfig +3 -0
 	}
 
 	if (has_cpuflag(X86_FEATURE_TSC)) {
 		uint32_t raw;
 
+		debug_putstr(" RDTSC");
 		rdtscl(raw);
 
+		random ^= raw;
+		use_i8254 = false;

The TSC is 64-bits. The high bits will most likely be 0, but just in case there's some dirt up there or BIOS bootup takes more than 4G cycles we might as well use the high portion as well!

arch/x86/Kconfig

Quoted 11 lines
arch/x86/Kconfig +7 -0
 	}
 
+	if (use_i8254) {
+		debug_putstr(" i8254");
+		random ^= i8254();
+	}
+
+	debug_putstr("...\n");
+
 	return random;
 }

Looks good otherwise.

Thanks,

Ingo

author
#2 lore raw
Quoted 40 lines · Ingo Molnar
Quoted 24 lines · Kees Cook

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().

Finally, to improve the starting entropy, do a simple hashing of the boot_params structure for some additional level of unpredictability.

---
 arch/x86/Kconfig                |   14 +++++++----
 arch/x86/boot/compressed/aslr.c |   52 ++++++++++++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 20 deletions(-)
arch/x86/Kconfig +1 -0
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

arch/x86/Kconfig config RANDOMIZE_BASE · line 1749

Quoted 10 lines
arch/x86/Kconfig +8 -0
+        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. 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.

Perfect!

Quoted 8 lines

arch/x86/Kconfig config RANDOMIZE_BASE · line 1751

Quoted 2 lines
arch/x86/Kconfig +2 -0
+/* Simple way to create an alternate starting entropy. */
+static unsigned long get_boot_hash(void)

s/get_random_boot, signifying that we want to get some initial randomness?

Btw., could we also add some build build time source of randomness as well? That won't help distro kernels which all share the same build but it would be a nice touch for self-built kernels and Gentoo systems.

Sure thing. Now sent.

Quoted 42 lines

arch/x86/Kconfig config RANDOMIZE_BASE

Quoted 13 lines
arch/x86/Kconfig +12 -0
+{
+     int i;
+     unsigned long hash = 0;
+     unsigned long *ptr = (unsigned long *)real_mode;
+
+     for (i = 0; i < sizeof(*real_mode) / sizeof(hash); i++) {
+             /* Rotate and XOR */
+             hash = (hash << ((sizeof(hash) - 1) * 8)) | (hash >> 8);
+             hash ^= ptr[i];
+     }
+
+     return hash;

Looks mostly good, but I'm too tired to ack this bit now, maybe hpa will have a look :-)

Quoted 25 lines
arch/x86/Kconfig +14 -0
+}
+
 static unsigned long get_random_long(void)
 {
+     unsigned long random = get_boot_hash();
+     bool use_i8254 = true;
+
+     debug_putstr("KASLR using");
 
      if (has_cpuflag(X86_FEATURE_RDRAND)) {
+             unsigned long raw;
+
+             debug_putstr(" RDRAND");
+             if (rdrand_long(&raw)) {
+                     random ^= raw;
+                     use_i8254 = false;
+             }
      }
 
      if (has_cpuflag(X86_FEATURE_TSC)) {
              uint32_t raw;
 
+             debug_putstr(" RDTSC");
              rdtscl(raw);
Quoted 6 lines

arch/x86/Kconfig

Quoted 2 lines
arch/x86/Kconfig +2 -0
+             random ^= raw;
+             use_i8254 = false;

The TSC is 64-bits. The high bits will most likely be 0, but just in case there's some dirt up there or BIOS bootup takes more than 4G cycles we might as well use the high portion as well!

Oops, I missed this suggestion entirely when I first read this email. :)

v3 on it's way!

Quoted 14 lines

arch/x86/Kconfig

Quoted 12 lines
      }
arch/x86/Kconfig +7 -0
+     if (use_i8254) {
+             debug_putstr(" i8254");
+             random ^= i8254();
+     }
+
+     debug_putstr("...\n");
+
      return random;
 }

Looks good otherwise.

Thanks!

-Kees

Quoted 4 lines

Thanks,

Ingo

Signature · 2 lines
Kees Cook
Chrome OS Security