2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* misc.c
|
2008-01-30 12:33:38 +00:00
|
|
|
*
|
2016-04-18 16:42:13 +00:00
|
|
|
* This is a collection of several routines used to extract the kernel
|
|
|
|
* which includes KASLR relocation, decompression, ELF parsing, and
|
|
|
|
* relocation processing. Additionally included are the screen and serial
|
|
|
|
* output functions and related debugging support functions.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
|
|
|
|
* puts by Nick Holloway 1993, better puts by Martin Mares 1995
|
|
|
|
* High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
|
|
|
|
*/
|
|
|
|
|
2010-08-02 23:21:22 +00:00
|
|
|
#include "misc.h"
|
2016-05-02 22:51:00 +00:00
|
|
|
#include "error.h"
|
2014-03-18 19:26:38 +00:00
|
|
|
#include "../string.h"
|
x86/boot: Fix "run_size" calculation
Currently, the "run_size" variable holds the total kernel size
(size of code plus brk and bss) and is calculated via the shell script
arch/x86/tools/calc_run_size.sh. It gets the file offset and mem size
of the .bss and .brk sections from the vmlinux, and adds them as follows:
run_size = $(( $offsetA + $sizeA + $sizeB ))
However, this is not correct (it is too large). To illustrate, here's
a walk-through of the script's calculation, compared to the correct way
to find it.
First, offsetA is found as the starting address of the first .bss or
.brk section seen in the ELF file. The sizeA and sizeB values are the
respective section sizes.
[bhe@x1 linux]$ objdump -h vmlinux
vmlinux: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
27 .bss 00170000 ffffffff81ec8000 0000000001ec8000 012c8000 2**12
ALLOC
28 .brk 00027000 ffffffff82038000 0000000002038000 012c8000 2**0
ALLOC
Here, offsetA is 0x012c8000, with sizeA at 0x00170000 and sizeB at
0x00027000. The resulting run_size is 0x145f000:
0x012c8000 + 0x00170000 + 0x00027000 = 0x145f000
However, if we instead examine the ELF LOAD program headers, we see a
different picture.
[bhe@x1 linux]$ readelf -l vmlinux
Elf file type is EXEC (Executable file)
Entry point 0x1000000
There are 5 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000200000 0xffffffff81000000 0x0000000001000000
0x0000000000b5e000 0x0000000000b5e000 R E 200000
LOAD 0x0000000000e00000 0xffffffff81c00000 0x0000000001c00000
0x0000000000145000 0x0000000000145000 RW 200000
LOAD 0x0000000001000000 0x0000000000000000 0x0000000001d45000
0x0000000000018158 0x0000000000018158 RW 200000
LOAD 0x000000000115e000 0xffffffff81d5e000 0x0000000001d5e000
0x000000000016a000 0x0000000000301000 RWE 200000
NOTE 0x000000000099bcac 0xffffffff8179bcac 0x000000000179bcac
0x00000000000001bc 0x00000000000001bc 4
Section to Segment mapping:
Segment Sections...
00 .text .notes __ex_table .rodata __bug_table .pci_fixup .tracedata
__ksymtab __ksymtab_gpl __ksymtab_strings __init_rodata __param
__modver
01 .data .vvar
02 .data..percpu
03 .init.text .init.data .x86_cpu_dev.init .parainstructions
.altinstructions .altinstr_replacement .iommu_table .apicdrivers
.exit.text .smp_locks .bss .brk
04 .notes
As mentioned, run_size needs to be the size of the running kernel
including .bss and .brk. We can see from the Section/Segment mapping
above that .bss and .brk are included in segment 03 (which corresponds
to the final LOAD program header). To find the run_size, we calculate
the end of the LOAD segment from its PhysAddr start (0x0000000001d5e000)
and its MemSiz (0x0000000000301000), minus the physical load address of
the kernel (the first LOAD segment's PhysAddr: 0x0000000001000000). The
resulting run_size is 0x105f000:
0x0000000001d5e000 + 0x0000000000301000 - 0x0000000001000000 = 0x105f000
So, from this we can see that the existing run_size calculation is
0x400000 too high. And, as it turns out, the correct run_size is
actually equal to VO_end - VO_text, which is certainly easier to calculate.
_end: 0xffffffff8205f000
_text:0xffffffff81000000
0xffffffff8205f000 - 0xffffffff81000000 = 0x105f000
As a result, run_size is a simple constant, so we don't need to pass it
around; we already have voffset.h for such things. We can share voffset.h
between misc.c and header.S instead of getting run_size in other ways.
This patch moves voffset.h creation code to boot/compressed/Makefile,
and switches misc.c to use the VO_end - VO_text calculation for run_size.
Dependence before:
boot/header.S ==> boot/voffset.h ==> vmlinux
boot/header.S ==> compressed/vmlinux ==> compressed/misc.c
Dependence after:
boot/header.S ==> compressed/vmlinux ==> compressed/misc.c ==> boot/voffset.h ==> vmlinux
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
Signed-off-by: Baoquan He <bhe@redhat.com>
[ Rewrote the changelog. ]
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Josh Triplett <josh@joshtriplett.org>
Cc: Junjie Mao <eternal.n08@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: lasse.collin@tukaani.org
Fixes: e6023367d779 ("x86, kaslr: Prevent .bss from overlaping initrd")
Link: http://lkml.kernel.org/r/1461888548-32439-5-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-29 00:09:06 +00:00
|
|
|
#include "../voffset.h"
|
2006-12-07 01:14:04 +00:00
|
|
|
|
|
|
|
/*
|
x86/KASLR: Update description for decompressor worst case size
The comment that describes the analysis for the size of the decompressor
code only took gzip into account (there are currently 6 other decompressors
that could be used). The actual z_extract_offset calculation in code was
already handling the correct maximum size, but this documentation hadn't
been updated. This updates the documentation, fixes several typos, moves
the comment to header.S, updates references, and adds a note at the end
of the decompressor include list to remind us about updating the comment
in the future.
(Instead of moving the comment to mkpiggy.c, where the calculation
is currently happening, it is being moved to header.S because
the calculations in mkpiggy.c will be removed in favor of header.S
calculations in a following patch, and it seemed like overkill to move
the giant comment twice, especially when there's already reference to
z_extract_offset in header.S.)
Signed-off-by: Baoquan He <bhe@redhat.com>
[ Rewrote changelog, cleaned up comment style, moved comments around. ]
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: H.J. Lu <hjl.tools@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1461185746-8017-2-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-20 20:55:42 +00:00
|
|
|
* WARNING!!
|
|
|
|
* This code is compiled with -fPIC and it is relocated dynamically at
|
|
|
|
* run time, but no relocation processing is performed. This means that
|
|
|
|
* it is not safe to place pointers in static structures.
|
2006-12-07 01:14:04 +00:00
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2016-04-20 20:55:44 +00:00
|
|
|
/* Macros used by the included decompressor code below. */
|
2008-02-21 04:03:48 +00:00
|
|
|
#define STATIC static
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-03-18 19:26:40 +00:00
|
|
|
/*
|
2016-04-20 20:55:44 +00:00
|
|
|
* Use normal definitions of mem*() from string.c. There are already
|
2014-03-18 19:26:40 +00:00
|
|
|
* included header files which expect a definition of memset() and by
|
|
|
|
* the time we define memset macro, it is too late.
|
|
|
|
*/
|
2016-04-20 20:55:44 +00:00
|
|
|
#undef memcpy
|
2014-03-18 19:26:40 +00:00
|
|
|
#undef memset
|
2008-02-21 04:03:48 +00:00
|
|
|
#define memzero(s, n) memset((s), 0, (n))
|
2016-04-26 21:46:06 +00:00
|
|
|
#define memmove memmove
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2016-04-20 20:55:44 +00:00
|
|
|
/* Functions used by the included decompressor code below. */
|
2016-04-26 21:46:06 +00:00
|
|
|
void *memmove(void *dest, const void *src, size_t n);
|
2008-02-20 23:19:10 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* This is set up by the setup-routine at boot-time
|
|
|
|
*/
|
2016-04-18 16:42:12 +00:00
|
|
|
struct boot_params *boot_params;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-10-11 00:18:16 +00:00
|
|
|
memptr free_mem_ptr;
|
|
|
|
memptr free_mem_end_ptr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-04-06 12:47:00 +00:00
|
|
|
static char *vidmem;
|
2005-04-16 22:20:36 +00:00
|
|
|
static int vidport;
|
|
|
|
static int lines, cols;
|
|
|
|
|
2009-01-04 21:46:17 +00:00
|
|
|
#ifdef CONFIG_KERNEL_GZIP
|
|
|
|
#include "../../../../lib/decompress_inflate.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_KERNEL_BZIP2
|
|
|
|
#include "../../../../lib/decompress_bunzip2.c"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_KERNEL_LZMA
|
|
|
|
#include "../../../../lib/decompress_unlzma.c"
|
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-01-13 01:01:24 +00:00
|
|
|
#ifdef CONFIG_KERNEL_XZ
|
|
|
|
#include "../../../../lib/decompress_unxz.c"
|
|
|
|
#endif
|
|
|
|
|
2010-01-08 22:42:45 +00:00
|
|
|
#ifdef CONFIG_KERNEL_LZO
|
|
|
|
#include "../../../../lib/decompress_unlzo.c"
|
|
|
|
#endif
|
|
|
|
|
2013-07-08 23:01:48 +00:00
|
|
|
#ifdef CONFIG_KERNEL_LZ4
|
|
|
|
#include "../../../../lib/decompress_unlz4.c"
|
|
|
|
#endif
|
x86/KASLR: Update description for decompressor worst case size
The comment that describes the analysis for the size of the decompressor
code only took gzip into account (there are currently 6 other decompressors
that could be used). The actual z_extract_offset calculation in code was
already handling the correct maximum size, but this documentation hadn't
been updated. This updates the documentation, fixes several typos, moves
the comment to header.S, updates references, and adds a note at the end
of the decompressor include list to remind us about updating the comment
in the future.
(Instead of moving the comment to mkpiggy.c, where the calculation
is currently happening, it is being moved to header.S because
the calculations in mkpiggy.c will be removed in favor of header.S
calculations in a following patch, and it seemed like overkill to move
the giant comment twice, especially when there's already reference to
z_extract_offset in header.S.)
Signed-off-by: Baoquan He <bhe@redhat.com>
[ Rewrote changelog, cleaned up comment style, moved comments around. ]
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: H.J. Lu <hjl.tools@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Yinghai Lu <yinghai@kernel.org>
Link: http://lkml.kernel.org/r/1461185746-8017-2-git-send-email-keescook@chromium.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-20 20:55:42 +00:00
|
|
|
/*
|
|
|
|
* NOTE: When adding a new decompressor, please update the analysis in
|
|
|
|
* ../header.S.
|
|
|
|
*/
|
2013-07-08 23:01:48 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void scroll(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-04-26 21:46:06 +00:00
|
|
|
memmove(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
|
2008-02-20 23:19:10 +00:00
|
|
|
for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
|
2005-04-16 22:20:36 +00:00
|
|
|
vidmem[i] = ' ';
|
|
|
|
}
|
|
|
|
|
2010-08-02 23:21:22 +00:00
|
|
|
#define XMTRDY 0x20
|
|
|
|
|
|
|
|
#define TXR 0 /* Transmit register (WRITE) */
|
|
|
|
#define LSR 5 /* Line Status */
|
|
|
|
static void serial_putchar(int ch)
|
|
|
|
{
|
|
|
|
unsigned timeout = 0xffff;
|
|
|
|
|
|
|
|
while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
|
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
outb(ch, early_serial_base + TXR);
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:04:39 +00:00
|
|
|
void __putstr(const char *s)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-20 23:19:10 +00:00
|
|
|
int x, y, pos;
|
2005-04-16 22:20:36 +00:00
|
|
|
char c;
|
|
|
|
|
2010-08-02 23:21:22 +00:00
|
|
|
if (early_serial_base) {
|
|
|
|
const char *str = s;
|
|
|
|
while (*str) {
|
|
|
|
if (*str == '\n')
|
|
|
|
serial_putchar('\r');
|
|
|
|
serial_putchar(*str++);
|
|
|
|
}
|
|
|
|
}
|
2008-06-18 18:04:35 +00:00
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
if (boot_params->screen_info.orig_video_mode == 0 &&
|
2008-05-29 22:31:14 +00:00
|
|
|
lines == 0 && cols == 0)
|
2007-10-21 23:41:35 +00:00
|
|
|
return;
|
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
x = boot_params->screen_info.orig_x;
|
|
|
|
y = boot_params->screen_info.orig_y;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-20 23:19:10 +00:00
|
|
|
while ((c = *s++) != '\0') {
|
|
|
|
if (c == '\n') {
|
2005-04-16 22:20:36 +00:00
|
|
|
x = 0;
|
2008-02-20 23:19:10 +00:00
|
|
|
if (++y >= lines) {
|
2005-04-16 22:20:36 +00:00
|
|
|
scroll();
|
|
|
|
y--;
|
|
|
|
}
|
|
|
|
} else {
|
2008-08-02 19:23:36 +00:00
|
|
|
vidmem[(x + cols * y) * 2] = c;
|
2008-02-20 23:19:10 +00:00
|
|
|
if (++x >= cols) {
|
2005-04-16 22:20:36 +00:00
|
|
|
x = 0;
|
2008-02-20 23:19:10 +00:00
|
|
|
if (++y >= lines) {
|
2005-04-16 22:20:36 +00:00
|
|
|
scroll();
|
|
|
|
y--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
boot_params->screen_info.orig_x = x;
|
|
|
|
boot_params->screen_info.orig_y = y;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
pos = (x + cols * y) * 2; /* Update cursor position */
|
2008-01-30 12:30:05 +00:00
|
|
|
outb(14, vidport);
|
|
|
|
outb(0xff & (pos >> 9), vidport+1);
|
|
|
|
outb(15, vidport);
|
|
|
|
outb(0xff & (pos >> 1), vidport+1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 23:06:20 +00:00
|
|
|
void __puthex(unsigned long value)
|
|
|
|
{
|
|
|
|
char alpha[2] = "0";
|
|
|
|
int bits;
|
|
|
|
|
|
|
|
for (bits = sizeof(value) * 8 - 4; bits >= 0; bits -= 4) {
|
|
|
|
unsigned long digit = (value >> bits) & 0xf;
|
|
|
|
|
|
|
|
if (digit < 0xA)
|
|
|
|
alpha[0] = '0' + digit;
|
|
|
|
else
|
|
|
|
alpha[0] = 'a' + (digit - 0xA);
|
|
|
|
|
|
|
|
__putstr(alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 16:15:17 +00:00
|
|
|
#if CONFIG_X86_NEED_RELOCS
|
2016-05-25 22:45:32 +00:00
|
|
|
static void handle_relocations(void *output, unsigned long output_len,
|
|
|
|
unsigned long virt_addr)
|
2013-07-08 16:15:17 +00:00
|
|
|
{
|
|
|
|
int *reloc;
|
|
|
|
unsigned long delta, map, ptr;
|
|
|
|
unsigned long min_addr = (unsigned long)output;
|
2016-04-29 00:09:08 +00:00
|
|
|
unsigned long max_addr = min_addr + (VO___bss_start - VO__text);
|
2013-07-08 16:15:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the delta between where vmlinux was linked to load
|
|
|
|
* and where it was actually loaded.
|
|
|
|
*/
|
|
|
|
delta = min_addr - LOAD_PHYSICAL_ADDR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The kernel contains a table of relocation addresses. Those
|
|
|
|
* addresses have the final load address of the kernel in virtual
|
|
|
|
* memory. We are currently working in the self map. So we need to
|
|
|
|
* create an adjustment for kernel memory addresses to the self map.
|
|
|
|
* This will involve subtracting out the base address of the kernel.
|
|
|
|
*/
|
|
|
|
map = delta - __START_KERNEL_map;
|
|
|
|
|
2016-05-25 22:45:32 +00:00
|
|
|
/*
|
|
|
|
* 32-bit always performs relocations. 64-bit relocations are only
|
|
|
|
* needed if KASLR has chosen a different starting address offset
|
|
|
|
* from __START_KERNEL_map.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_X86_64))
|
|
|
|
delta = virt_addr - LOAD_PHYSICAL_ADDR;
|
|
|
|
|
|
|
|
if (!delta) {
|
|
|
|
debug_putstr("No relocation needed... ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug_putstr("Performing relocations... ");
|
|
|
|
|
2013-07-08 16:15:17 +00:00
|
|
|
/*
|
|
|
|
* Process relocations: 32 bit relocations first then 64 bit after.
|
2014-11-04 08:50:18 +00:00
|
|
|
* Three sets of binary relocations are added to the end of the kernel
|
2013-07-08 16:15:17 +00:00
|
|
|
* before compression. Each relocation table entry is the kernel
|
|
|
|
* address of the location which needs to be updated stored as a
|
|
|
|
* 32-bit value which is sign extended to 64 bits.
|
|
|
|
*
|
|
|
|
* Format is:
|
|
|
|
*
|
|
|
|
* kernel bits...
|
|
|
|
* 0 - zero terminator for 64 bit relocations
|
|
|
|
* 64 bit relocation repeated
|
2014-11-04 08:50:18 +00:00
|
|
|
* 0 - zero terminator for inverse 32 bit relocations
|
|
|
|
* 32 bit inverse relocation repeated
|
2013-07-08 16:15:17 +00:00
|
|
|
* 0 - zero terminator for 32 bit relocations
|
|
|
|
* 32 bit relocation repeated
|
|
|
|
*
|
|
|
|
* So we work backwards from the end of the decompressed image.
|
|
|
|
*/
|
|
|
|
for (reloc = output + output_len - sizeof(*reloc); *reloc; reloc--) {
|
2016-04-29 00:09:03 +00:00
|
|
|
long extended = *reloc;
|
2013-07-08 16:15:17 +00:00
|
|
|
extended += map;
|
|
|
|
|
|
|
|
ptr = (unsigned long)extended;
|
|
|
|
if (ptr < min_addr || ptr > max_addr)
|
|
|
|
error("32-bit relocation outside of kernel!\n");
|
|
|
|
|
|
|
|
*(uint32_t *)ptr += delta;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_X86_64
|
2014-11-04 08:50:18 +00:00
|
|
|
while (*--reloc) {
|
|
|
|
long extended = *reloc;
|
|
|
|
extended += map;
|
|
|
|
|
|
|
|
ptr = (unsigned long)extended;
|
|
|
|
if (ptr < min_addr || ptr > max_addr)
|
|
|
|
error("inverse 32-bit relocation outside of kernel!\n");
|
|
|
|
|
|
|
|
*(int32_t *)ptr -= delta;
|
|
|
|
}
|
2013-07-08 16:15:17 +00:00
|
|
|
for (reloc--; *reloc; reloc--) {
|
|
|
|
long extended = *reloc;
|
|
|
|
extended += map;
|
|
|
|
|
|
|
|
ptr = (unsigned long)extended;
|
|
|
|
if (ptr < min_addr || ptr > max_addr)
|
|
|
|
error("64-bit relocation outside of kernel!\n");
|
|
|
|
|
|
|
|
*(uint64_t *)ptr += delta;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
2016-05-25 22:45:32 +00:00
|
|
|
static inline void handle_relocations(void *output, unsigned long output_len,
|
|
|
|
unsigned long virt_addr)
|
2013-07-08 16:15:17 +00:00
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
|
2008-02-13 20:54:58 +00:00
|
|
|
static void parse_elf(void *output)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
Elf64_Ehdr ehdr;
|
|
|
|
Elf64_Phdr *phdrs, *phdr;
|
|
|
|
#else
|
|
|
|
Elf32_Ehdr ehdr;
|
|
|
|
Elf32_Phdr *phdrs, *phdr;
|
|
|
|
#endif
|
|
|
|
void *dest;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memcpy(&ehdr, output, sizeof(ehdr));
|
2008-02-20 23:19:10 +00:00
|
|
|
if (ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
|
2008-02-13 20:54:58 +00:00
|
|
|
ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
|
|
|
|
ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
|
2008-02-20 23:19:10 +00:00
|
|
|
ehdr.e_ident[EI_MAG3] != ELFMAG3) {
|
2008-02-13 20:54:58 +00:00
|
|
|
error("Kernel is not a valid ELF file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-20 01:04:37 +00:00
|
|
|
debug_putstr("Parsing ELF... ");
|
2008-02-13 20:54:58 +00:00
|
|
|
|
|
|
|
phdrs = malloc(sizeof(*phdrs) * ehdr.e_phnum);
|
|
|
|
if (!phdrs)
|
|
|
|
error("Failed to allocate space for phdrs");
|
|
|
|
|
|
|
|
memcpy(phdrs, output + ehdr.e_phoff, sizeof(*phdrs) * ehdr.e_phnum);
|
|
|
|
|
2008-02-20 23:19:10 +00:00
|
|
|
for (i = 0; i < ehdr.e_phnum; i++) {
|
2008-02-13 20:54:58 +00:00
|
|
|
phdr = &phdrs[i];
|
|
|
|
|
|
|
|
switch (phdr->p_type) {
|
|
|
|
case PT_LOAD:
|
|
|
|
#ifdef CONFIG_RELOCATABLE
|
|
|
|
dest = output;
|
|
|
|
dest += (phdr->p_paddr - LOAD_PHYSICAL_ADDR);
|
|
|
|
#else
|
2008-02-20 23:19:10 +00:00
|
|
|
dest = (void *)(phdr->p_paddr);
|
2008-02-13 20:54:58 +00:00
|
|
|
#endif
|
2016-04-26 21:46:06 +00:00
|
|
|
memmove(dest, output + phdr->p_offset, phdr->p_filesz);
|
2008-02-13 20:54:58 +00:00
|
|
|
break;
|
|
|
|
default: /* Ignore other PT_* */ break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-23 22:34:59 +00:00
|
|
|
|
|
|
|
free(phdrs);
|
2008-02-13 20:54:58 +00:00
|
|
|
}
|
|
|
|
|
x86/boot: Move compressed kernel to the end of the decompression buffer
This change makes later calculations about where the kernel is located
easier to reason about. To better understand this change, we must first
clarify what 'VO' and 'ZO' are. These values were introduced in commits
by hpa:
77d1a4999502 ("x86, boot: make symbols from the main vmlinux available")
37ba7ab5e33c ("x86, boot: make kernel_alignment adjustable; new bzImage fields")
Specifically:
All names prefixed with 'VO_':
- relate to the uncompressed kernel image
- the size of the VO image is: VO__end-VO__text ("VO_INIT_SIZE" define)
All names prefixed with 'ZO_':
- relate to the bootable compressed kernel image (boot/compressed/vmlinux),
which is composed of the following memory areas:
- head text
- compressed kernel (VO image and relocs table)
- decompressor code
- the size of the ZO image is: ZO__end - ZO_startup_32 ("ZO_INIT_SIZE" define, though see below)
The 'INIT_SIZE' value is used to find the larger of the two image sizes:
#define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_extract_offset)
#define VO_INIT_SIZE (VO__end - VO__text)
#if ZO_INIT_SIZE > VO_INIT_SIZE
# define INIT_SIZE ZO_INIT_SIZE
#else
# define INIT_SIZE VO_INIT_SIZE
#endif
The current code uses extract_offset to decide where to position the
copied ZO (i.e. ZO starts at extract_offset). (This is why ZO_INIT_SIZE
currently includes the extract_offset.)
Why does z_extract_offset exist? It's needed because we are trying to minimize
the amount of RAM used for the whole act of creating an uncompressed, executable,
properly relocation-linked kernel image in system memory. We do this so that
kernels can be booted on even very small systems.
To achieve the goal of minimal memory consumption we have implemented an in-place
decompression strategy: instead of cleanly separating the VO and ZO images and
also allocating some memory for the decompression code's runtime needs, we instead
create this elaborate layout of memory buffers where the output (decompressed)
stream, as it progresses, overlaps with and destroys the input (compressed)
stream. This can only be done safely if the ZO image is placed to the end of the
VO range, plus a certain amount of safety distance to make sure that when the last
bytes of the VO range are decompressed, the compressed stream pointer is safely
beyond the end of the VO range.
z_extract_offset is calculated in arch/x86/boot/compressed/mkpiggy.c during
the build process, at a point when we know the exact compressed and
uncompressed size of the kernel images and can calculate this safe minimum
offset value. (Note that the mkpiggy.c calculation is not perfect, because
we don't know the decompressor used at that stage, so the z_extract_offset
calculation is necessarily imprecise and is mostly based on gzip internals -
we'll improve that in the next patch.)
When INIT_SIZE is bigger than VO_INIT_SIZE (uncommon but possible),
the copied ZO occupies the memory from extract_offset to the end of
decompression buffer. It overlaps with the soon-to-be-uncompressed kernel
like this:
|-----compressed kernel image------|
V V
0 extract_offset +INIT_SIZE
|-----------|---------------|-------------------------|--------|
| | | |
VO__text startup_32 of ZO VO__end ZO__end
^ ^
|-------uncompressed kernel image---------|
When INIT_SIZE is equal to VO_INIT_SIZE (likely) there's still space
left from end of ZO to the end of decompressing buffer, like below.
|-compressed kernel image-|
V V
0 extract_offset +INIT_SIZE
|-----------|---------------|-------------------------|--------|
| | | |
VO__text startup_32 of ZO ZO__end VO__end
^ ^
|------------uncompressed kernel image-------------|
To simplify calculations and avoid special cases, it is cleaner to
always place the compressed kernel image in memory so that ZO__end
is at the end of the decompression buffer, instead of placing t at
the start of extract_offset as is currently done.
This patch adds BP_init_size (which is the INIT_SIZE as passed in from
the boot_params) into asm-offsets.c to make it visible to the assembly
code.
Then when moving the ZO, it calculates the starting position of
the copied ZO (via BP_init_size and the ZO run size) so that the VO__end
will be at the end of the decompression buffer. To make the position
calculation safe, the end of ZO is page aligned (and a comment is added
to the existing VO alignment for good measure).
Signed-off-by: Yinghai Lu <yinghai@kernel.org>
[ Rewrote changelog and comments. ]
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: lasse.collin@tukaani.org
Link: http://lkml.kernel.org/r/1461888548-32439-3-git-send-email-keescook@chromium.org
[ Rewrote the changelog some more. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-04-29 00:09:04 +00:00
|
|
|
/*
|
|
|
|
* The compressed kernel image (ZO), has been moved so that its position
|
|
|
|
* is against the end of the buffer used to hold the uncompressed kernel
|
|
|
|
* image (VO) and the execution environment (.bss, .brk), which makes sure
|
|
|
|
* there is room to do the in-place decompression. (See header.S for the
|
|
|
|
* calculations.)
|
|
|
|
*
|
|
|
|
* |-----compressed kernel image------|
|
|
|
|
* V V
|
|
|
|
* 0 extract_offset +INIT_SIZE
|
|
|
|
* |-----------|---------------|-------------------------|--------|
|
|
|
|
* | | | |
|
|
|
|
* VO__text startup_32 of ZO VO__end ZO__end
|
|
|
|
* ^ ^
|
|
|
|
* |-------uncompressed kernel image---------|
|
|
|
|
*
|
|
|
|
*/
|
2016-04-18 16:42:13 +00:00
|
|
|
asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
|
2008-02-21 04:03:48 +00:00
|
|
|
unsigned char *input_data,
|
|
|
|
unsigned long input_len,
|
2013-07-08 16:15:17 +00:00
|
|
|
unsigned char *output,
|
2016-04-29 00:09:07 +00:00
|
|
|
unsigned long output_len)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2016-04-29 00:09:07 +00:00
|
|
|
const unsigned long kernel_total_size = VO__end - VO__text;
|
2017-06-27 12:39:06 +00:00
|
|
|
unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
|
2015-01-16 00:51:46 +00:00
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
/* Retain x86 boot parameters pointer passed from startup_32/64. */
|
|
|
|
boot_params = rmode;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
/* Clear flags intended for solely in-kernel use. */
|
|
|
|
boot_params->hdr.loadflags &= ~KASLR_FLAG;
|
2015-04-01 10:49:52 +00:00
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
sanitize_boot_params(boot_params);
|
2013-01-29 09:05:24 +00:00
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
if (boot_params->screen_info.orig_video_mode == 7) {
|
2005-04-16 22:20:36 +00:00
|
|
|
vidmem = (char *) 0xb0000;
|
|
|
|
vidport = 0x3b4;
|
|
|
|
} else {
|
|
|
|
vidmem = (char *) 0xb8000;
|
|
|
|
vidport = 0x3d4;
|
|
|
|
}
|
|
|
|
|
2016-04-18 16:42:12 +00:00
|
|
|
lines = boot_params->screen_info.orig_video_lines;
|
|
|
|
cols = boot_params->screen_info.orig_video_cols;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-08-02 23:21:22 +00:00
|
|
|
console_init();
|
2016-04-18 16:42:13 +00:00
|
|
|
debug_putstr("early console in extract_kernel\n");
|
2010-08-02 23:21:22 +00:00
|
|
|
|
2008-01-30 12:33:38 +00:00
|
|
|
free_mem_ptr = heap; /* Heap */
|
2008-04-08 10:54:30 +00:00
|
|
|
free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
|
2006-12-07 01:14:04 +00:00
|
|
|
|
2015-07-06 23:06:20 +00:00
|
|
|
/* Report initial kernel position details. */
|
|
|
|
debug_putaddr(input_data);
|
|
|
|
debug_putaddr(input_len);
|
|
|
|
debug_putaddr(output);
|
|
|
|
debug_putaddr(output_len);
|
2016-04-29 00:09:07 +00:00
|
|
|
debug_putaddr(kernel_total_size);
|
2015-07-06 23:06:20 +00:00
|
|
|
|
2014-10-31 13:40:38 +00:00
|
|
|
/*
|
|
|
|
* The memory hole needed for the kernel is the larger of either
|
|
|
|
* the entire decompressed kernel plus relocation table, or the
|
|
|
|
* entire decompressed kernel plus .bss and .brk sections.
|
|
|
|
*/
|
2016-05-25 22:45:32 +00:00
|
|
|
choose_random_location((unsigned long)input_data, input_len,
|
|
|
|
(unsigned long *)&output,
|
|
|
|
max(output_len, kernel_total_size),
|
|
|
|
&virt_addr);
|
2013-10-11 00:18:14 +00:00
|
|
|
|
|
|
|
/* Validate memory location choices. */
|
2009-05-12 18:33:08 +00:00
|
|
|
if ((unsigned long)output & (MIN_KERNEL_ALIGN - 1))
|
2016-05-25 22:45:32 +00:00
|
|
|
error("Destination physical address inappropriately aligned");
|
|
|
|
if (virt_addr & (MIN_KERNEL_ALIGN - 1))
|
|
|
|
error("Destination virtual address inappropriately aligned");
|
2008-01-30 12:33:38 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2009-05-12 18:33:08 +00:00
|
|
|
if (heap > 0x3fffffffffffUL)
|
2008-01-30 12:33:38 +00:00
|
|
|
error("Destination address too large");
|
2017-06-27 12:39:05 +00:00
|
|
|
if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
|
|
|
|
error("Destination virtual address is beyond the kernel mapping area");
|
2008-01-30 12:33:38 +00:00
|
|
|
#else
|
2010-12-17 03:11:09 +00:00
|
|
|
if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
|
2006-12-07 01:14:04 +00:00
|
|
|
error("Destination address too large");
|
2009-05-12 18:33:08 +00:00
|
|
|
#endif
|
2006-12-07 01:14:04 +00:00
|
|
|
#ifndef CONFIG_RELOCATABLE
|
2009-05-12 18:33:08 +00:00
|
|
|
if ((unsigned long)output != LOAD_PHYSICAL_ADDR)
|
2016-05-25 22:45:32 +00:00
|
|
|
error("Destination address does not match LOAD_PHYSICAL_ADDR");
|
2017-06-27 12:39:06 +00:00
|
|
|
if (virt_addr != LOAD_PHYSICAL_ADDR)
|
2016-05-25 22:45:32 +00:00
|
|
|
error("Destination virtual address changed when not relocatable");
|
2006-12-07 01:14:04 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-07-20 01:04:37 +00:00
|
|
|
debug_putstr("\nDecompressing Linux... ");
|
2015-09-09 22:39:12 +00:00
|
|
|
__decompress(input_data, input_len, NULL, NULL, output, output_len,
|
|
|
|
NULL, error);
|
2008-02-13 20:54:58 +00:00
|
|
|
parse_elf(output);
|
2016-05-25 22:45:32 +00:00
|
|
|
handle_relocations(output, output_len, virt_addr);
|
2012-07-20 01:04:37 +00:00
|
|
|
debug_putstr("done.\nBooting the kernel.\n");
|
2013-10-11 00:18:14 +00:00
|
|
|
return output;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|