Skip to content

RFC x86: Build the core kernel using PIC codegen

Ard Biesheuvel bpf linux-arch lkml llvm 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest5 patches 3 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5kallsyms: Avoid weak references for kallsyms symbolsinclude/asm-generic/vmlinux.lds.h kernel/kallsyms.c kernel/kallsyms_internal.h+31 -240 replies
2/5vmlinux: Avoid weak reference to notes sectionkernel/ksysfs.c lib/buildid.c+4 -40 replies
3/5btf: Avoid weak external referencesinclude/asm-generic/vmlinux.lds.h kernel/bpf/btf.c+4 -20 replies
4/5x86/head64: Replace pointer fixups with PIE codegenarch/x86/Makefile arch/x86/boot/compressed/Makefile arch/x86/include/asm/init.h +3 more+60 -783 replies
5/5x86: Build the core kernel with position independent codegenarch/x86/Makefile arch/x86/entry/vdso/Makefile arch/x86/kernel/Makefile +2 more+9 -70 replies

Reviews trailers per patch

1 of 5 patches has a Reviewed-by or Acked-by.

PatchNick DesaulniersKees Cook
1/5T-b R-b R-b
2/5
3/5
4/5
5/5

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [RFC PATCH 0/5] x86: Build the core kernel using PIC codegen

From: Ard Biesheuvel <[email protected]>

Originally, only arch/x86/kernel/head64.c had some code that required special care because it executes very early from the 1:1 mapping of the kernel rather than the ordinary kernel virtual mapping.

This is no longer the case, and there is a lot of SEV related code that is reachable from the primary startup path, with no guarantees that the toolchain will produce code that runs correctly. This is especially problematic when it comes to things like string literals, which are emitted by the compiler as data objects, and subsequently referenced via an absolute address that is not mapped yet this early in the boot [0].

Kevin has been looking into failures resulting from the fact that Clang behaves slightly differently from GCC in this regard, by selectively applying PIC codegen to the objects in question. However, while this fixes the observed issues, it does not offer any guarantees, given that the set of reachable code from startup_64() does not appear to be bounded when running on SEV hardware.

Instead of applying this change piecemeal to objects that happen to have caused issues in the past, this series convert the core kernel to PIC codegen entirely.

Note that this does not entirely solve the problem of the unbounded set of reachable code from the early SEV entrypoint: there might be code that attempts to access global objects via their kernel virtual address (which is not mapped yet). But at least all implicit accesses will be made via the same translation that the code is running from.

This does result in a slight increase in code size (see below) but it also reduces the size of the KASLR relocation table (applied by the decompressor) by roughly half.

Before

$ size -x vmlinux
   text	   data	    bss	    dec	    hex	filename
0x1b78ec1	0xdde145	0x381000	47022086	2cd8006	vmlinux

After

$ size -x vmlinux
   text	   data	    bss	    dec	    hex	filename
0x1b8371b	0xde0d1d	0x370000	47006776	2cd4438	vmlinux

[0] arch/x86/mm/mem_encrypt_identity.c has some nice examples of this, where RIP-relative references are emitted using inline asm.

[1] https://lkml.kernel.org/r/20240111223650.3502633-1-kevinloughlin%40google.com

Cc Kevin Loughlin <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Dionna Glaze <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Justin Stitt <[email protected]>

Ard Biesheuvel (5):
kallsyms: Avoid weak references for kallsyms symbols
vmlinux: Avoid weak reference to notes section
btf: Avoid weak external references
x86/head64: Replace pointer fixups with PIE codegen
x86: Build the core kernel with position independent codegen

 arch/x86/Makefile                 |  18 ++-
 arch/x86/boot/compressed/Makefile |   2 +-
 arch/x86/entry/vdso/Makefile      |   2 +-
 arch/x86/include/asm/init.h       |   2 -
 arch/x86/include/asm/setup.h      |   2 +-
 arch/x86/kernel/head64.c          | 117 +++++++-------------
 arch/x86/realmode/rm/Makefile     |   1 +
 include/asm-generic/vmlinux.lds.h |  23 ++++
 kernel/bpf/btf.c                  |   4 +-
 kernel/kallsyms.c                 |   6 -
 kernel/kallsyms_internal.h        |  30 ++---
 kernel/ksysfs.c                   |   4 +-
 lib/buildid.c                     |   4 +-
 13 files changed, 104 insertions(+), 111 deletions(-)
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog