Skip to content

Commit d899a7d

Browse files
thgarnieIngo Molnar
authored andcommitted
x86/mm: Refactor KASLR entropy functions
Move the KASLR entropy functions into arch/x86/lib to be used in early kernel boot for KASLR memory randomization. Signed-off-by: Thomas Garnier <thgarnie@google.com> Signed-off-by: Kees Cook <keescook@chromium.org> Cc: Alexander Kuleshov <kuleshovmail@gmail.com> Cc: Alexander Popov <alpopov@ptsecurity.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> Cc: Baoquan He <bhe@redhat.com> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Borislav Petkov <bp@suse.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Dave Young <dyoung@redhat.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Jan Beulich <JBeulich@suse.com> Cc: Joerg Roedel <jroedel@suse.de> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Juergen Gross <jgross@suse.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Lv Zheng <lv.zheng@intel.com> Cc: Mark Salter <msalter@redhat.com> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Matt Fleming <matt@codeblueprint.co.uk> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephen Smalley <sds@tycho.nsa.gov> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Toshi Kani <toshi.kani@hpe.com> Cc: Xiao Guangrong <guangrong.xiao@linux.intel.com> Cc: Yinghai Lu <yinghai@kernel.org> Cc: kernel-hardening@lists.openwall.com Cc: linux-doc@vger.kernel.org Link: http://lkml.kernel.org/r/1466556426-32664-2-git-send-email-keescook@chromium.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 9e7f7f5 commit d899a7d

File tree

4 files changed

+102
-71
lines changed

4 files changed

+102
-71
lines changed

arch/x86/boot/compressed/kaslr.c

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
#include "misc.h"
1313
#include "error.h"
1414

15-
#include <asm/msr.h>
16-
#include <asm/archrandom.h>
17-
#include <asm/e820.h>
18-
1915
#include <generated/compile.h>
2016
#include <linux/module.h>
2117
#include <linux/uts.h>
@@ -26,26 +22,6 @@
2622
static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
2723
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
2824

29-
#define I8254_PORT_CONTROL 0x43
30-
#define I8254_PORT_COUNTER0 0x40
31-
#define I8254_CMD_READBACK 0xC0
32-
#define I8254_SELECT_COUNTER0 0x02
33-
#define I8254_STATUS_NOTREADY 0x40
34-
static inline u16 i8254(void)
35-
{
36-
u16 status, timer;
37-
38-
do {
39-
outb(I8254_PORT_CONTROL,
40-
I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
41-
status = inb(I8254_PORT_COUNTER0);
42-
timer = inb(I8254_PORT_COUNTER0);
43-
timer |= inb(I8254_PORT_COUNTER0) << 8;
44-
} while (status & I8254_STATUS_NOTREADY);
45-
46-
return timer;
47-
}
48-
4925
static unsigned long rotate_xor(unsigned long hash, const void *area,
5026
size_t size)
5127
{
@@ -62,7 +38,7 @@ static unsigned long rotate_xor(unsigned long hash, const void *area,
6238
}
6339

6440
/* Attempt to create a simple but unpredictable starting entropy. */
65-
static unsigned long get_random_boot(void)
41+
static unsigned long get_boot_seed(void)
6642
{
6743
unsigned long hash = 0;
6844

@@ -72,50 +48,8 @@ static unsigned long get_random_boot(void)
7248
return hash;
7349
}
7450

75-
static unsigned long get_random_long(const char *purpose)
76-
{
77-
#ifdef CONFIG_X86_64
78-
const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
79-
#else
80-
const unsigned long mix_const = 0x3f39e593UL;
81-
#endif
82-
unsigned long raw, random = get_random_boot();
83-
bool use_i8254 = true;
84-
85-
debug_putstr(purpose);
86-
debug_putstr(" KASLR using");
87-
88-
if (has_cpuflag(X86_FEATURE_RDRAND)) {
89-
debug_putstr(" RDRAND");
90-
if (rdrand_long(&raw)) {
91-
random ^= raw;
92-
use_i8254 = false;
93-
}
94-
}
95-
96-
if (has_cpuflag(X86_FEATURE_TSC)) {
97-
debug_putstr(" RDTSC");
98-
raw = rdtsc();
99-
100-
random ^= raw;
101-
use_i8254 = false;
102-
}
103-
104-
if (use_i8254) {
105-
debug_putstr(" i8254");
106-
random ^= i8254();
107-
}
108-
109-
/* Circular multiply for better bit diffusion */
110-
asm("mul %3"
111-
: "=a" (random), "=d" (raw)
112-
: "a" (random), "rm" (mix_const));
113-
random += raw;
114-
115-
debug_putstr("...\n");
116-
117-
return random;
118-
}
51+
#define KASLR_COMPRESSED_BOOT
52+
#include "../../lib/kaslr.c"
11953

12054
struct mem_vector {
12155
unsigned long start;
@@ -349,7 +283,7 @@ static unsigned long slots_fetch_random(void)
349283
if (slot_max == 0)
350284
return 0;
351285

352-
slot = get_random_long("Physical") % slot_max;
286+
slot = kaslr_get_random_long("Physical") % slot_max;
353287

354288
for (i = 0; i < slot_area_index; i++) {
355289
if (slot >= slot_areas[i].num) {
@@ -479,7 +413,7 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
479413
slots = (KERNEL_IMAGE_SIZE - minimum - image_size) /
480414
CONFIG_PHYSICAL_ALIGN + 1;
481415

482-
random_addr = get_random_long("Virtual") % slots;
416+
random_addr = kaslr_get_random_long("Virtual") % slots;
483417

484418
return random_addr * CONFIG_PHYSICAL_ALIGN + minimum;
485419
}

arch/x86/include/asm/kaslr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _ASM_KASLR_H_
2+
#define _ASM_KASLR_H_
3+
4+
unsigned long kaslr_get_random_long(const char *purpose);
5+
6+
#endif

arch/x86/lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o
2424
lib-y += memcpy_$(BITS).o
2525
lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
2626
lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o
27+
lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
2728

2829
obj-y += msr.o msr-reg.o msr-reg-export.o
2930

arch/x86/lib/kaslr.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Entropy functions used on early boot for KASLR base and memory
3+
* randomization. The base randomization is done in the compressed
4+
* kernel and memory randomization is done early when the regular
5+
* kernel starts. This file is included in the compressed kernel and
6+
* normally linked in the regular.
7+
*/
8+
#include <asm/kaslr.h>
9+
#include <asm/msr.h>
10+
#include <asm/archrandom.h>
11+
#include <asm/e820.h>
12+
#include <asm/io.h>
13+
14+
/*
15+
* When built for the regular kernel, several functions need to be stubbed out
16+
* or changed to their regular kernel equivalent.
17+
*/
18+
#ifndef KASLR_COMPRESSED_BOOT
19+
#include <asm/cpufeature.h>
20+
#include <asm/setup.h>
21+
22+
#define debug_putstr(v) early_printk(v)
23+
#define has_cpuflag(f) boot_cpu_has(f)
24+
#define get_boot_seed() kaslr_offset()
25+
#endif
26+
27+
#define I8254_PORT_CONTROL 0x43
28+
#define I8254_PORT_COUNTER0 0x40
29+
#define I8254_CMD_READBACK 0xC0
30+
#define I8254_SELECT_COUNTER0 0x02
31+
#define I8254_STATUS_NOTREADY 0x40
32+
static inline u16 i8254(void)
33+
{
34+
u16 status, timer;
35+
36+
do {
37+
outb(I8254_PORT_CONTROL,
38+
I8254_CMD_READBACK | I8254_SELECT_COUNTER0);
39+
status = inb(I8254_PORT_COUNTER0);
40+
timer = inb(I8254_PORT_COUNTER0);
41+
timer |= inb(I8254_PORT_COUNTER0) << 8;
42+
} while (status & I8254_STATUS_NOTREADY);
43+
44+
return timer;
45+
}
46+
47+
unsigned long kaslr_get_random_long(const char *purpose)
48+
{
49+
#ifdef CONFIG_X86_64
50+
const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
51+
#else
52+
const unsigned long mix_const = 0x3f39e593UL;
53+
#endif
54+
unsigned long raw, random = get_boot_seed();
55+
bool use_i8254 = true;
56+
57+
debug_putstr(purpose);
58+
debug_putstr(" KASLR using");
59+
60+
if (has_cpuflag(X86_FEATURE_RDRAND)) {
61+
debug_putstr(" RDRAND");
62+
if (rdrand_long(&raw)) {
63+
random ^= raw;
64+
use_i8254 = false;
65+
}
66+
}
67+
68+
if (has_cpuflag(X86_FEATURE_TSC)) {
69+
debug_putstr(" RDTSC");
70+
raw = rdtsc();
71+
72+
random ^= raw;
73+
use_i8254 = false;
74+
}
75+
76+
if (use_i8254) {
77+
debug_putstr(" i8254");
78+
random ^= i8254();
79+
}
80+
81+
/* Circular multiply for better bit diffusion */
82+
asm("mul %3"
83+
: "=a" (random), "=d" (raw)
84+
: "a" (random), "rm" (mix_const));
85+
random += raw;
86+
87+
debug_putstr("...\n");
88+
89+
return random;
90+
}

0 commit comments

Comments
 (0)