From: laurentiu.tudor@nxp.com (Laurentiu Tudor)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH][RFC] arm64: kaslr: add pseudo-RNG in kernel
Date: Fri, 26 Feb 2016 11:01:57 +0000 [thread overview]
Message-ID: <56D030A4.5050205@nxp.com> (raw)
In case the bootloader doesn't provide randomness
(through device tree or by uefi protocol) generate
a pseudo-random seed based on the timer counter.
People might find this "week rng" approach convenient
as it gets rid of the bootloader dependency.
The patch tries to mimic the x86's rdtsc
based implementation authored by Kees Cook.
Signed-off-by: Laurentiu Tudor <laurentiu.tudor@nxp.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Kees Cook <keescook@chromium.org>
---
based on http://git.linaro.org/people/ard.biesheuvel/linux-arm.git,
branch arm64-kaslr-v6
arch/arm64/kernel/kaslr.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 42 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kernel/kaslr.c b/arch/arm64/kernel/kaslr.c
index 5829839..41e9cbd 100644
--- a/arch/arm64/kernel/kaslr.c
+++ b/arch/arm64/kernel/kaslr.c
@@ -13,6 +13,7 @@
#include <linux/sched.h>
#include <linux/types.h>
+#include <asm/arch_timer.h>
#include <asm/fixmap.h>
#include <asm/kernel-pgtable.h>
#include <asm/memory.h>
@@ -20,9 +21,30 @@
#include <asm/pgtable.h>
#include <asm/sections.h>
+#include <generated/compile.h>
+#include <generated/utsrelease.h>
+
+/* Simplified build-specific string for starting entropy. */
+static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
+ LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
+
u64 __read_mostly module_alloc_base;
u16 __initdata memstart_offset_seed;
+static u64 rotate_xor(u64 hash, const void *area, size_t size)
+{
+ size_t i;
+ unsigned long *ptr = (unsigned long *)area;
+
+ for (i = 0; i < size / sizeof(hash); i++) {
+ /* Rotate by odd number of bits and XOR. */
+ hash = (hash << ((sizeof(hash) * 8) - 7)) | (hash >> 7);
+ hash ^= ptr[i];
+ }
+
+ return hash;
+}
+
static __init u64 get_kaslr_seed(void *fdt)
{
int node, len;
@@ -101,8 +123,26 @@ u64 __init kaslr_early_init(u64 dt_phys)
* Retrieve (and wipe) the seed from the FDT
*/
seed = get_kaslr_seed(fdt);
- if (!seed)
- return 0;
+ if (!seed) {
+ u64 raw;
+ const u64 mix_const = 0x5d6008cbf3848dd3UL;
+
+ /*
+ * Attempt to create a simple but unpredictable starting
+ * entropy.
+ */
+ seed = rotate_xor(seed, build_str, sizeof(build_str));
+ seed = rotate_xor(seed, fdt, size);
+
+ raw = arch_counter_get_cntvct();
+ seed ^= raw;
+
+ /* Circular multiply for better bit diffusion */
+ asm("mul %1, %2, %3; umulh %0, %2, %3"
+ : "=&r" (raw), "=&r" (seed)
+ : "r" (seed), "r" (mix_const));
+ seed += raw;
+ }
/*
* Check if 'nokaslr' appears on the command line, and
--
1.8.3.1
next reply other threads:[~2016-02-26 11:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-26 11:01 Laurentiu Tudor [this message]
2016-02-26 11:51 ` [PATCH][RFC] arm64: kaslr: add pseudo-RNG in kernel Ard Biesheuvel
2016-02-26 12:18 ` Mark Rutland
2016-02-26 12:37 ` Ard Biesheuvel
2016-02-26 18:56 ` Kees Cook
2016-02-26 19:07 ` Ard Biesheuvel
2016-02-29 12:47 ` Laurentiu Tudor
2016-02-29 12:54 ` Ard Biesheuvel
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=56D030A4.5050205@nxp.com \
--to=laurentiu.tudor@nxp.com \
--cc=linux-arm-kernel@lists.infradead.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