Skip to content

Commit 98f7852

Browse files
keesIngo Molnar
authored and
Ingo Molnar
committed
x86/boot: Refuse to build with data relocations
The compressed kernel is built with -fPIC/-fPIE so that it can run in any location a bootloader happens to put it. However, since ELF relocation processing is not happening (and all the relocation information has already been stripped at link time), none of the code can use data relocations (e.g. static assignments of pointers). This is already noted in a warning comment at the top of misc.c, but this adds an explicit check for the condition during the linking stage to block any such bugs from appearing. If this was in place with the earlier bug in pagetable.c, the build would fail like this: ... CC arch/x86/boot/compressed/pagetable.o DATAREL arch/x86/boot/compressed/vmlinux error: arch/x86/boot/compressed/pagetable.o has data relocations! make[2]: *** [arch/x86/boot/compressed/vmlinux] Error 1 ... A clean build shows: ... CC arch/x86/boot/compressed/pagetable.o DATAREL arch/x86/boot/compressed/vmlinux LD arch/x86/boot/compressed/vmlinux ... Suggested-by: Ingo Molnar <[email protected]> Signed-off-by: Kees Cook <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Baoquan He <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Brian Gerst <[email protected]> Cc: Denys Vlasenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: H.J. Lu <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Yinghai Lu <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 65fe935 commit 98f7852

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

arch/x86/boot/compressed/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,25 @@ vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o $(obj)/efi_stub_$(BITS).o \
8585
$(objtree)/drivers/firmware/efi/libstub/lib.a
8686
vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
8787

88+
# The compressed kernel is built with -fPIC/-fPIE so that a boot loader
89+
# can place it anywhere in memory and it will still run. However, since
90+
# it is executed as-is without any ELF relocation processing performed
91+
# (and has already had all relocation sections stripped from the binary),
92+
# none of the code can use data relocations (e.g. static assignments of
93+
# pointer values), since they will be meaningless at runtime. This check
94+
# will refuse to link the vmlinux if any of these relocations are found.
95+
quiet_cmd_check_data_rel = DATAREL $@
96+
define cmd_check_data_rel
97+
for obj in $(filter %.o,$^); do \
98+
readelf -S $$obj | grep -qF .rel.local && { \
99+
echo "error: $$obj has data relocations!" >&2; \
100+
exit 1; \
101+
} || true; \
102+
done
103+
endef
104+
88105
$(obj)/vmlinux: $(vmlinux-objs-y) FORCE
106+
$(call if_changed,check_data_rel)
89107
$(call if_changed,ld)
90108

91109
OBJCOPYFLAGS_vmlinux.bin := -R .comment -S

0 commit comments

Comments
 (0)