x86/boot/compressed/64: Validate trampoline placement against e820
PATCH 1/1 x86/boot/compressed/64: Validate trampoline placement against e820
There were two report of boot failure cased by trampoline placed into a reserved memory region. It can happen on machines that don't report EBDA correctly.
Fix the problem by re-validating the found address against e820 table. If the address is in a reserved area, find the next usable region below the initial address.
--- arch/x86/boot/compressed/pgtable_64.c | 73 ++++++++++++++++++++------- 1 file changed, 55 insertions(+), 18 deletions(-)
arch/x86/boot/compressed/pgtable_64.c +55 -18
@@ -1,3 +1,4 @@ +#include <asm/e820/types.h> #include <asm/processor.h> #include "pgtable.h" #include "../string.h"
@@ -34,10 +35,62 @@ unsigned long *trampoline_32bit __section(.data);extern struct boot_params *boot_params;int cmdline_find_option_bool (const char *option); +static unsigned long find_trampoline_placement (void ) +{ +unsigned long bios_start, ebda_start; +unsigned long trampoline_start; +struct boot_e820_entry *entry; +int i; + +/* +* Find a suitable spot for the trampoline. +* This code is based on reserve_bios_regions(). +*/ + + ebda_start = *(unsigned short *)0x40e <<4 ; + bios_start = *(unsigned short *)0x413 <<10 ; + +if (bios_start <BIOS_START_MIN || bios_start >BIOS_START_MAX ) + bios_start =BIOS_START_MAX ; + +if (ebda_start >BIOS_START_MIN && ebda_start < bios_start) + bios_start = ebda_start; + + bios_start =round_down (bios_start,PAGE_SIZE ); + +/* Find the first usable memory region under bios_start. */ +for (i = boot_params->e820_entries -1 ; i >=0 ; i--) { + entry = &boot_params->e820_table[i]; + +/* Skip all entries above bios_start. */ +if (bios_start <= entry->addr) +continue ; + +/* Skip non-RAM entries. */ +if (entry->type !=E820_TYPE_RAM ) +continue ; + +/* Adjust bios_start to the end of the entry if needed. */ +if (bios_start > entry->addr + entry->size) + bios_start = entry->addr + entry->size; + +/* Keep bios_start page-aligned. */ + bios_start =round_down (bios_start,PAGE_SIZE ); + +/* Skip the entry if it's too small. */ +if (bios_start -TRAMPOLINE_32BIT_SIZE < entry->addr) +continue ; + +break ; + } + +/* Place the trampoline just below the end of low memory */ +return bios_start -TRAMPOLINE_32BIT_SIZE ; +} +struct paging_config paging_prepare (void *rmode) {struct paging_config paging_config = {}; -unsigned long bios_start, ebda_start;/* Initialize boot_params. Required for cmdline_find_option_bool(). */ boot_params = rmode;
@@ -61,23 +114,7 @@ struct paging_config paging_prepare(void *rmode) paging_config.l5_required =1 ; } -/* -* Find a suitable spot for the trampoline. -* This code is based on reserve_bios_regions(). -*/ - - ebda_start = *(unsigned short *)0x40e <<4 ; - bios_start = *(unsigned short *)0x413 <<10 ; - -if (bios_start <BIOS_START_MIN || bios_start >BIOS_START_MAX ) - bios_start =BIOS_START_MAX ; - -if (ebda_start >BIOS_START_MIN && ebda_start < bios_start) - bios_start = ebda_start; - -/* Place the trampoline just below the end of low memory, aligned to 4k */ - paging_config.trampoline_start = bios_start -TRAMPOLINE_32BIT_SIZE ; - paging_config.trampoline_start =round_down (paging_config.trampoline_start,PAGE_SIZE ); + paging_config.trampoline_start =find_trampoline_placement (); trampoline_32bit = (unsigned long *)paging_config.trampoline_start;
Signature · 1 line
2.18.0
tip-bot for Kirill A. Shutemov
merge bot[tip:x86/urgent] x86/boot/compressed/64: Validate trampoline placement against E820
x86/boot/compressed/64: Validate trampoline placement against E820
Commit-ID: 1b3a62643660020cdc68e6139a010c06e8fc96c7 Gitweb: https://git.kernel.org/tip/1b3a62643660020cdc68e6139a010c06e8fc96c7 Author: Kirill A. Shutemov <[email protected]> AuthorDate: Wed, 1 Aug 2018 16:32:25 +0300 Committer: Thomas Gleixner <[email protected]> CommitDate: Thu, 2 Aug 2018 14:22:22 +0200
x86/boot/compressed/64: Validate trampoline placement against E820
There were two report of boot failure cased by trampoline placed into a reserved memory region. It can happen on machines that don't report EBDA correctly.
Fix the problem by re-validating the found address against the E820 table. If the address is in a reserved area, find the next usable region below the initial address.
--- arch/x86/boot/compressed/pgtable_64.c | 73 ++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 18 deletions(-)
arch/x86/boot/compressed/pgtable_64.c +55 -18
@@ -1,3 +1,4 @@ +#include <asm/e820/types.h> #include <asm/processor.h> #include "pgtable.h" #include "../string.h"
@@ -34,10 +35,62 @@ unsigned long *trampoline_32bit __section(.data);extern struct boot_params *boot_params;int cmdline_find_option_bool (const char *option); +static unsigned long find_trampoline_placement (void ) +{ +unsigned long bios_start, ebda_start; +unsigned long trampoline_start; +struct boot_e820_entry *entry; +int i; + +/* +* Find a suitable spot for the trampoline. +* This code is based on reserve_bios_regions(). +*/ + + ebda_start = *(unsigned short *)0x40e <<4 ; + bios_start = *(unsigned short *)0x413 <<10 ; + +if (bios_start <BIOS_START_MIN || bios_start >BIOS_START_MAX ) + bios_start =BIOS_START_MAX ; + +if (ebda_start >BIOS_START_MIN && ebda_start < bios_start) + bios_start = ebda_start; + + bios_start =round_down (bios_start,PAGE_SIZE ); + +/* Find the first usable memory region under bios_start. */ +for (i = boot_params->e820_entries -1 ; i >=0 ; i--) { + entry = &boot_params->e820_table[i]; + +/* Skip all entries above bios_start. */ +if (bios_start <= entry->addr) +continue ; + +/* Skip non-RAM entries. */ +if (entry->type !=E820_TYPE_RAM ) +continue ; + +/* Adjust bios_start to the end of the entry if needed. */ +if (bios_start > entry->addr + entry->size) + bios_start = entry->addr + entry->size; + +/* Keep bios_start page-aligned. */ + bios_start =round_down (bios_start,PAGE_SIZE ); + +/* Skip the entry if it's too small. */ +if (bios_start -TRAMPOLINE_32BIT_SIZE < entry->addr) +continue ; + +break ; + } + +/* Place the trampoline just below the end of low memory */ +return bios_start -TRAMPOLINE_32BIT_SIZE ; +} +struct paging_config paging_prepare (void *rmode) {struct paging_config paging_config = {}; -unsigned long bios_start, ebda_start;/* Initialize boot_params. Required for cmdline_find_option_bool(). */ boot_params = rmode;
@@ -61,23 +114,7 @@ struct paging_config paging_prepare(void *rmode) paging_config.l5_required =1 ; } -/* -* Find a suitable spot for the trampoline. -* This code is based on reserve_bios_regions(). -*/ - - ebda_start = *(unsigned short *)0x40e <<4 ; - bios_start = *(unsigned short *)0x413 <<10 ; - -if (bios_start <BIOS_START_MIN || bios_start >BIOS_START_MAX ) - bios_start =BIOS_START_MAX ; - -if (ebda_start >BIOS_START_MIN && ebda_start < bios_start) - bios_start = ebda_start; - -/* Place the trampoline just below the end of low memory, aligned to 4k */ - paging_config.trampoline_start = bios_start -TRAMPOLINE_32BIT_SIZE ; - paging_config.trampoline_start =round_down (paging_config.trampoline_start,PAGE_SIZE ); + paging_config.trampoline_start =find_trampoline_placement (); trampoline_32bit = (unsigned long *)paging_config.trampoline_start;