Commit a267b0a3 authored by Ingo Molnar's avatar Ingo Molnar

Merge branch 'tip-x86-kaslr' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp into x86/urgent

Pull ASLR and kASLR fixes from Borislav Petkov:

  - Add a global flag announcing KASLR state so that relevant code can do
    informed decisions based on its setting. (Jiri Kosina)

  - Fix a stack randomization entropy decrease bug. (Hector Marco-Gisbert)
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
parents ee408b42 4e7c22d4
...@@ -14,6 +14,13 @@ ...@@ -14,6 +14,13 @@
static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@" static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION; LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
struct kaslr_setup_data {
__u64 next;
__u32 type;
__u32 len;
__u8 data[1];
} kaslr_setup_data;
#define I8254_PORT_CONTROL 0x43 #define I8254_PORT_CONTROL 0x43
#define I8254_PORT_COUNTER0 0x40 #define I8254_PORT_COUNTER0 0x40
#define I8254_CMD_READBACK 0xC0 #define I8254_CMD_READBACK 0xC0
...@@ -295,7 +302,29 @@ static unsigned long find_random_addr(unsigned long minimum, ...@@ -295,7 +302,29 @@ static unsigned long find_random_addr(unsigned long minimum,
return slots_fetch_random(); return slots_fetch_random();
} }
unsigned char *choose_kernel_location(unsigned char *input, static void add_kaslr_setup_data(struct boot_params *params, __u8 enabled)
{
struct setup_data *data;
kaslr_setup_data.type = SETUP_KASLR;
kaslr_setup_data.len = 1;
kaslr_setup_data.next = 0;
kaslr_setup_data.data[0] = enabled;
data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
while (data && data->next)
data = (struct setup_data *)(unsigned long)data->next;
if (data)
data->next = (unsigned long)&kaslr_setup_data;
else
params->hdr.setup_data = (unsigned long)&kaslr_setup_data;
}
unsigned char *choose_kernel_location(struct boot_params *params,
unsigned char *input,
unsigned long input_size, unsigned long input_size,
unsigned char *output, unsigned char *output,
unsigned long output_size) unsigned long output_size)
...@@ -306,14 +335,17 @@ unsigned char *choose_kernel_location(unsigned char *input, ...@@ -306,14 +335,17 @@ unsigned char *choose_kernel_location(unsigned char *input,
#ifdef CONFIG_HIBERNATION #ifdef CONFIG_HIBERNATION
if (!cmdline_find_option_bool("kaslr")) { if (!cmdline_find_option_bool("kaslr")) {
debug_putstr("KASLR disabled by default...\n"); debug_putstr("KASLR disabled by default...\n");
add_kaslr_setup_data(params, 0);
goto out; goto out;
} }
#else #else
if (cmdline_find_option_bool("nokaslr")) { if (cmdline_find_option_bool("nokaslr")) {
debug_putstr("KASLR disabled by cmdline...\n"); debug_putstr("KASLR disabled by cmdline...\n");
add_kaslr_setup_data(params, 0);
goto out; goto out;
} }
#endif #endif
add_kaslr_setup_data(params, 1);
/* Record the various known unsafe memory ranges. */ /* Record the various known unsafe memory ranges. */
mem_avoid_init((unsigned long)input, input_size, mem_avoid_init((unsigned long)input, input_size,
......
...@@ -401,7 +401,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap, ...@@ -401,7 +401,8 @@ asmlinkage __visible void *decompress_kernel(void *rmode, memptr heap,
* the entire decompressed kernel plus relocation table, or the * the entire decompressed kernel plus relocation table, or the
* entire decompressed kernel plus .bss and .brk sections. * entire decompressed kernel plus .bss and .brk sections.
*/ */
output = choose_kernel_location(input_data, input_len, output, output = choose_kernel_location(real_mode, input_data, input_len,
output,
output_len > run_size ? output_len output_len > run_size ? output_len
: run_size); : run_size);
......
...@@ -56,7 +56,8 @@ int cmdline_find_option_bool(const char *option); ...@@ -56,7 +56,8 @@ int cmdline_find_option_bool(const char *option);
#if CONFIG_RANDOMIZE_BASE #if CONFIG_RANDOMIZE_BASE
/* aslr.c */ /* aslr.c */
unsigned char *choose_kernel_location(unsigned char *input, unsigned char *choose_kernel_location(struct boot_params *params,
unsigned char *input,
unsigned long input_size, unsigned long input_size,
unsigned char *output, unsigned char *output,
unsigned long output_size); unsigned long output_size);
...@@ -64,7 +65,8 @@ unsigned char *choose_kernel_location(unsigned char *input, ...@@ -64,7 +65,8 @@ unsigned char *choose_kernel_location(unsigned char *input,
bool has_cpuflag(int flag); bool has_cpuflag(int flag);
#else #else
static inline static inline
unsigned char *choose_kernel_location(unsigned char *input, unsigned char *choose_kernel_location(struct boot_params *params,
unsigned char *input,
unsigned long input_size, unsigned long input_size,
unsigned char *output, unsigned char *output,
unsigned long output_size) unsigned long output_size)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <linux/const.h> #include <linux/const.h>
#include <linux/types.h> #include <linux/types.h>
#include <asm/bootparam.h>
/* PAGE_SHIFT determines the page size */ /* PAGE_SHIFT determines the page size */
#define PAGE_SHIFT 12 #define PAGE_SHIFT 12
...@@ -51,6 +52,8 @@ extern int devmem_is_allowed(unsigned long pagenr); ...@@ -51,6 +52,8 @@ extern int devmem_is_allowed(unsigned long pagenr);
extern unsigned long max_low_pfn_mapped; extern unsigned long max_low_pfn_mapped;
extern unsigned long max_pfn_mapped; extern unsigned long max_pfn_mapped;
extern bool kaslr_enabled;
static inline phys_addr_t get_max_mapped(void) static inline phys_addr_t get_max_mapped(void)
{ {
return (phys_addr_t)max_pfn_mapped << PAGE_SHIFT; return (phys_addr_t)max_pfn_mapped << PAGE_SHIFT;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#define SETUP_DTB 2 #define SETUP_DTB 2
#define SETUP_PCI 3 #define SETUP_PCI 3
#define SETUP_EFI 4 #define SETUP_EFI 4
#define SETUP_KASLR 5
/* ram_size flags */ /* ram_size flags */
#define RAMDISK_IMAGE_START_MASK 0x07FF #define RAMDISK_IMAGE_START_MASK 0x07FF
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <asm/page.h> #include <asm/page.h>
#include <asm/pgtable.h> #include <asm/pgtable.h>
#include <asm/page_types.h>
#if 0 #if 0
#define DEBUGP(fmt, ...) \ #define DEBUGP(fmt, ...) \
...@@ -46,21 +47,13 @@ do { \ ...@@ -46,21 +47,13 @@ do { \
#ifdef CONFIG_RANDOMIZE_BASE #ifdef CONFIG_RANDOMIZE_BASE
static unsigned long module_load_offset; static unsigned long module_load_offset;
static int randomize_modules = 1;
/* Mutex protects the module_load_offset. */ /* Mutex protects the module_load_offset. */
static DEFINE_MUTEX(module_kaslr_mutex); static DEFINE_MUTEX(module_kaslr_mutex);
static int __init parse_nokaslr(char *p)
{
randomize_modules = 0;
return 0;
}
early_param("nokaslr", parse_nokaslr);
static unsigned long int get_module_load_offset(void) static unsigned long int get_module_load_offset(void)
{ {
if (randomize_modules) { if (kaslr_enabled) {
mutex_lock(&module_kaslr_mutex); mutex_lock(&module_kaslr_mutex);
/* /*
* Calculate the module_load_offset the first time this * Calculate the module_load_offset the first time this
......
...@@ -121,6 +121,8 @@ ...@@ -121,6 +121,8 @@
unsigned long max_low_pfn_mapped; unsigned long max_low_pfn_mapped;
unsigned long max_pfn_mapped; unsigned long max_pfn_mapped;
bool __read_mostly kaslr_enabled = false;
#ifdef CONFIG_DMI #ifdef CONFIG_DMI
RESERVE_BRK(dmi_alloc, 65536); RESERVE_BRK(dmi_alloc, 65536);
#endif #endif
...@@ -424,6 +426,11 @@ static void __init reserve_initrd(void) ...@@ -424,6 +426,11 @@ static void __init reserve_initrd(void)
} }
#endif /* CONFIG_BLK_DEV_INITRD */ #endif /* CONFIG_BLK_DEV_INITRD */
static void __init parse_kaslr_setup(u64 pa_data, u32 data_len)
{
kaslr_enabled = (bool)(pa_data + sizeof(struct setup_data));
}
static void __init parse_setup_data(void) static void __init parse_setup_data(void)
{ {
struct setup_data *data; struct setup_data *data;
...@@ -449,6 +456,9 @@ static void __init parse_setup_data(void) ...@@ -449,6 +456,9 @@ static void __init parse_setup_data(void)
case SETUP_EFI: case SETUP_EFI:
parse_efi_setup(pa_data, data_len); parse_efi_setup(pa_data, data_len);
break; break;
case SETUP_KASLR:
parse_kaslr_setup(pa_data, data_len);
break;
default: default:
break; break;
} }
...@@ -831,10 +841,14 @@ static void __init trim_low_memory_range(void) ...@@ -831,10 +841,14 @@ static void __init trim_low_memory_range(void)
static int static int
dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p) dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
{ {
pr_emerg("Kernel Offset: 0x%lx from 0x%lx " if (kaslr_enabled)
"(relocation range: 0x%lx-0x%lx)\n", pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
(unsigned long)&_text - __START_KERNEL, __START_KERNEL, (unsigned long)&_text - __START_KERNEL,
__START_KERNEL_map, MODULES_VADDR-1); __START_KERNEL,
__START_KERNEL_map,
MODULES_VADDR-1);
else
pr_emerg("Kernel Offset: disabled\n");
return 0; return 0;
} }
......
...@@ -35,12 +35,12 @@ struct va_alignment __read_mostly va_align = { ...@@ -35,12 +35,12 @@ struct va_alignment __read_mostly va_align = {
.flags = -1, .flags = -1,
}; };
static unsigned int stack_maxrandom_size(void) static unsigned long stack_maxrandom_size(void)
{ {
unsigned int max = 0; unsigned long max = 0;
if ((current->flags & PF_RANDOMIZE) && if ((current->flags & PF_RANDOMIZE) &&
!(current->personality & ADDR_NO_RANDOMIZE)) { !(current->personality & ADDR_NO_RANDOMIZE)) {
max = ((-1U) & STACK_RND_MASK) << PAGE_SHIFT; max = ((-1UL) & STACK_RND_MASK) << PAGE_SHIFT;
} }
return max; return max;
......
...@@ -645,11 +645,12 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, ...@@ -645,11 +645,12 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
static unsigned long randomize_stack_top(unsigned long stack_top) static unsigned long randomize_stack_top(unsigned long stack_top)
{ {
unsigned int random_variable = 0; unsigned long random_variable = 0;
if ((current->flags & PF_RANDOMIZE) && if ((current->flags & PF_RANDOMIZE) &&
!(current->personality & ADDR_NO_RANDOMIZE)) { !(current->personality & ADDR_NO_RANDOMIZE)) {
random_variable = get_random_int() & STACK_RND_MASK; random_variable = (unsigned long) get_random_int();
random_variable &= STACK_RND_MASK;
random_variable <<= PAGE_SHIFT; random_variable <<= PAGE_SHIFT;
} }
#ifdef CONFIG_STACK_GROWSUP #ifdef CONFIG_STACK_GROWSUP
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment