mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: linux-kernel@vger.kernel.org
Cc: kernel-hardening@lists.openwall.com,
	"H. Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>,
	x86@kernel.org, Jarkko Sakkinen <jarkko.sakkinen@intel.com>,
	Matthew Garrett <mjg@redhat.com>,
	Matt Fleming <matt.fleming@intel.com>,
	Eric Northup <digitaleric@google.com>,
	Dan Rosenberg <drosenberg@vsecurity.com>,
	Julien Tinnes <jln@google.com>, Will Drewry <wad@chromium.org>,
	Kees Cook <keescook@chromium.org>
Subject: [PATCH 5/6] x86: kaslr: select memory region from e820 maps
Date: Thu, 25 Apr 2013 14:54:19 -0700	[thread overview]
Message-ID: <1366926860-26776-6-git-send-email-keescook@chromium.org> (raw)
In-Reply-To: <1366926860-26776-1-git-send-email-keescook@chromium.org>

This chooses the largest contiguous RAM region for the KASLR offset
to live in.

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 arch/x86/boot/compressed/aslr.c |   43 +++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/arch/x86/boot/compressed/aslr.c b/arch/x86/boot/compressed/aslr.c
index 11a91c6..8f21ebe 100644
--- a/arch/x86/boot/compressed/aslr.c
+++ b/arch/x86/boot/compressed/aslr.c
@@ -1,6 +1,7 @@
 #include "misc.h"
 
 #ifdef CONFIG_RANDOMIZE_BASE
+#include <asm/e820.h>
 
 #include <asm/archrandom.h>
 static inline int rdrand(unsigned long *v)
@@ -56,28 +57,58 @@ static unsigned long get_random_long(void)
 	return 0;
 }
 
+int largest_ram_region(unsigned long *start, unsigned long *size)
+{
+	int i, rc = 0;
+
+	*size = 0;
+	for (i = 0; i < real_mode->e820_entries; i++) {
+		struct e820entry *entry = &real_mode->e820_map[i];
+
+		if (entry->type != E820_RAM)
+			continue;
+
+		if (entry->size > *size) {
+			*size = entry->size;
+			*start = entry->addr;
+			rc = 1;
+		}
+	}
+	return rc;
+}
+
 unsigned char *choose_kernel_location(unsigned char *hint, unsigned long size)
 {
 	unsigned char *choice = hint;
 	unsigned long random, mask;
+	unsigned long addr, length;
 
 	if (cmdline_find_option_bool("noaslr")) {
 		debug_putstr("KASLR disabled...\n");
 		goto out;
 	}
 
+	/* Find an appropriate E820 entry. */
+	if (!largest_ram_region(&addr, &length)) {
+		debug_putstr("KASLR could not find suitable E820 region...\n");
+		goto out;
+	}
+
 	random = get_random_long();
 
-	/* Clip off top of the range. */
+	/* XXX: Rework page tables to handle arbitrary physical location. */
 	mask = CONFIG_RANDOMIZE_BASE_MAX_OFFSET - 1;
 	random &= mask;
 
-	/* XXX: Find an appropriate E820 hole, instead of adding hint. */
-	random += (unsigned long)hint;
+	/* Clip to E820 entry size. */
+	while (random > length)
+		random >>= 1;
+
+	/* Offset the target. */
+	random += addr;
 
-	/* XXX: Clip to E820 hole, instead of just using hint. */
-	mask = (unsigned long)hint + CONFIG_RANDOMIZE_BASE_MAX_OFFSET;
-	while (random + size > mask)
+	/* Clip end to E820 entry size. */
+	while (random + size > addr + length)
 		random >>= 1;
 
 	/* Clip off bottom of range (via alignment). */
-- 
1.7.9.5


  parent reply	other threads:[~2013-04-25 21:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-25 21:54 [PATCH v3 0/6] kernel ASLR Kees Cook
2013-04-25 21:54 ` [PATCH 1/6] x86: kaslr: move ELF relocation handling to C Kees Cook
2013-04-25 21:54 ` [PATCH 2/6] x86: kaslr: move CPU flags out of cpucheck Kees Cook
2013-04-25 22:00   ` H. Peter Anvin
2013-04-26 15:28     ` Kees Cook
2013-04-25 21:54 ` [PATCH 3/6] x86: kaslr: return location from decompress_kernel Kees Cook
2013-04-25 21:54 ` [PATCH 4/6] x86: kaslr: select random base offset Kees Cook
2013-04-26  6:13   ` [kernel-hardening] " Mathias Krause
2013-04-26 15:30     ` Kees Cook
2013-04-25 21:54 ` Kees Cook [this message]
2013-04-25 21:54 ` [PATCH 6/6] x86: kaslr: report kernel offset on panic Kees Cook
2013-04-25 22:01 ` [PATCH v3 0/6] kernel ASLR H. Peter Anvin
2013-04-26 18:53   ` Kees Cook
2013-04-26 19:03 [PATCH v4 " Kees Cook
2013-04-26 19:03 ` [PATCH 5/6] x86: kaslr: select memory region from e820 maps Kees Cook
2013-04-26 21:51   ` Yinghai Lu
2013-04-26 22:01     ` H. Peter Anvin
2013-04-26 22:01     ` Kees Cook
2013-04-26 20:07 George Spelvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1366926860-26776-6-git-send-email-keescook@chromium.org \
    --to=keescook@chromium.org \
    --cc=digitaleric@google.com \
    --cc=drosenberg@vsecurity.com \
    --cc=hpa@zytor.com \
    --cc=jarkko.sakkinen@intel.com \
    --cc=jln@google.com \
    --cc=kernel-hardening@lists.openwall.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt.fleming@intel.com \
    --cc=mingo@redhat.com \
    --cc=mjg@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=wad@chromium.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®