Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/boot: Remove run-time relocations from head_{32,64}.S

The BFD linker generates run-time relocations for z_input_len and
z_output_len, even though they are absolute symbols.

This is fixed for binutils-2.35 [1]. Work around this for earlier
versions by defining two variables input_len and output_len in addition
to the symbols, and use them via position-independent references.

This eliminates the last two run-time relocations in the head code and
allows us to drop the -z noreloc-overflow flag to the linker.

Move the -pie and --no-dynamic-linker LDFLAGS to LDFLAGS_vmlinux instead
of KBUILD_LDFLAGS. There shouldn't be anything else getting linked, but
this is the more logical location for these flags, and modversions might
call the linker if an EXPORT_SYMBOL is left over accidentally in one of
the decompressors.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=25754

Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Fangrui Song <maskray@google.com>
Link: https://lore.kernel.org/r/20200731230820.1742553-7-keescook@chromium.org

authored by

Arvind Sankar and committed by
Ingo Molnar
3f086189 a2c4fc4d

+18 -21
+2 -10
arch/x86/boot/compressed/Makefile
··· 52 52 KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE) 53 53 # Compressed kernel should be built as PIE since it may be loaded at any 54 54 # address by the bootloader. 55 - ifeq ($(CONFIG_X86_32),y) 56 - KBUILD_LDFLAGS += $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker) 57 - else 58 - # To build 64-bit compressed kernel as PIE, we disable relocation 59 - # overflow check to avoid relocation overflow error with a new linker 60 - # command-line option, -z noreloc-overflow. 61 - KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \ 62 - && echo "-z noreloc-overflow -pie --no-dynamic-linker") 63 - endif 64 - LDFLAGS_vmlinux := -T 55 + LDFLAGS_vmlinux := $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker) 56 + LDFLAGS_vmlinux += -T 65 57 66 58 hostprogs := mkpiggy 67 59 HOST_EXTRACFLAGS += -I$(srctree)/tools/include
+8 -9
arch/x86/boot/compressed/head_32.S
··· 178 178 /* 179 179 * Do the extraction, and jump to the new kernel.. 180 180 */ 181 - /* push arguments for extract_kernel: */ 182 - pushl $z_output_len /* decompressed length, end of relocs */ 181 + /* push arguments for extract_kernel: */ 183 182 184 - pushl %ebp /* output address */ 185 - 186 - pushl $z_input_len /* input_len */ 183 + pushl output_len@GOTOFF(%ebx) /* decompressed length, end of relocs */ 184 + pushl %ebp /* output address */ 185 + pushl input_len@GOTOFF(%ebx) /* input_len */ 187 186 leal input_data@GOTOFF(%ebx), %eax 188 - pushl %eax /* input_data */ 187 + pushl %eax /* input_data */ 189 188 leal boot_heap@GOTOFF(%ebx), %eax 190 - pushl %eax /* heap area */ 191 - pushl %esi /* real mode pointer */ 192 - call extract_kernel /* returns kernel location in %eax */ 189 + pushl %eax /* heap area */ 190 + pushl %esi /* real mode pointer */ 191 + call extract_kernel /* returns kernel location in %eax */ 193 192 addl $24, %esp 194 193 195 194 /*
+2 -2
arch/x86/boot/compressed/head_64.S
··· 534 534 movq %rsi, %rdi /* real mode address */ 535 535 leaq boot_heap(%rip), %rsi /* malloc area for uncompression */ 536 536 leaq input_data(%rip), %rdx /* input_data */ 537 - movl $z_input_len, %ecx /* input_len */ 537 + movl input_len(%rip), %ecx /* input_len */ 538 538 movq %rbp, %r8 /* output target address */ 539 - movl $z_output_len, %r9d /* decompressed length, end of relocs */ 539 + movl output_len(%rip), %r9d /* decompressed length, end of relocs */ 540 540 call extract_kernel /* returns kernel location in %rax */ 541 541 popq %rsi 542 542
+6
arch/x86/boot/compressed/mkpiggy.c
··· 60 60 printf(".incbin \"%s\"\n", argv[1]); 61 61 printf("input_data_end:\n"); 62 62 63 + printf(".section \".rodata\",\"a\",@progbits\n"); 64 + printf(".globl input_len\n"); 65 + printf("input_len:\n\t.long %lu\n", ilen); 66 + printf(".globl output_len\n"); 67 + printf("output_len:\n\t.long %lu\n", (unsigned long)olen); 68 + 63 69 retval = 0; 64 70 bail: 65 71 if (f)