x86/alternatives: Use C int3 selftest but disable KASAN
PATCH 1/1 x86/alternatives: Use C int3 selftest but disable KASAN
Instead of using inline asm for the int3 selftest (which confuses the Clang's ThinLTO pass), this restores the C function but disables KASAN (and tracing for good measure) to keep the things simple and avoid unexpected side-effects. This attempts to keep the fix from commit ecc606103837 ("x86/alternatives: Fix int3_emulate_call() selftest stack corruption") without using inline asm.
--- arch/x86/kernel/alternative.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
arch/x86/kernel/alternative.c +4 -17
@@ -625,23 +625,10 @@ extern struct paravirt_patch_site __start_parainstructions[],* * See entry_{32,64}.S for more details. */ - -/* -* We define the int3_magic() function in assembly to control the calling -* convention such that we can 'call' it from assembly. -*/ - -extern void int3_magic (unsigned int *ptr);/* defined in asm */ - -asm ( -" .pushsection .init.text, \"ax\", @progbits\n" -" .type int3_magic, @function\n" -"int3_magic:\n" -" movl $1, (%" _ASM_ARG1")\n" -" ret\n" -" .size int3_magic, .-int3_magic\n" -" .popsection\n" -); +static void __init __no_sanitize_address notrace int3_magic(unsigned int *ptr) +{ + *ptr =1 ; +}extern __initdata unsigned long int3_selftest_ip;/* defined in asm below */
Signature · 5 lines
2.17.1 -- Kees Cook
Quoted 2 lines · Kees Cook
Instead of using inline asm for the int3 selftest (which confuses the Clang's ThinLTO pass),
What is that and why do we care?
Quoted 5 lines
this restores the C function but disables KASAN
(and tracing for good measure) to keep the things simple and avoid unexpected side-effects. This attempts to keep the fix from commitecc606103837("x86/alternatives: Fixint3_emulate_call()selftest stack corruption") without using inline asm.
See, I don't much like that. The selftest basically does a naked CALL and hard relies on the callee saving everything if required, which is very much against the C calling convention.
Sure, by disabling KASAN and all the other crap the compiler probably does the right thing by accident, but it is still a C ABI violation.
We use ASM all over the kernel, why is this one a problem?
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Kees Cook
Instead of using inline asm for the int3 selftest (which confuses the Clang's ThinLTO pass),
What is that and why do we care?
This was breaking my build when using Clang and the LLVM linker with Link Time Optimization (LTO) enabled, which is a prerequisite for enabling Clang's Control Flow Integrity (CFI) feature that seeks to protect indirect function calls from intentional (or accidental) manipulation. Adding CFI to kernel builds is an ongoing project to further defend the kernel from attacks[1], which many system builders are interested in deploying.
Quoted 6 lines
Quoted 5 lines
this restores the C function but disables KASAN
(and tracing for good measure) to keep the things simple and avoid unexpected side-effects. This attempts to keep the fix from commitecc606103837("x86/alternatives: Fixint3_emulate_call()selftest stack corruption") without using inline asm.
Quoted 6 lines
See, I don't much like that. The selftest basically does a naked CALL and hard relies on the callee saving everything if required, which is very much against the C calling convention.
Sure, by disabling KASAN and all the other crap the compiler probably does the right thing by accident, but it is still a C ABI violation.
Okay, fair enough. I thought the patch seemed like a reasonable middle ground, but I'll revisit it.
Quoted 1 line
We use ASM all over the kernel, why is this one a problem?
There seem to be a lot of weird visibility differences between GCC and Clang with respect to asm. This is just declared differently from the other many cases.
I'll see if I can find a better work-around.
-Kees
[1] https://android-developers.googleblog.com/2018/10/control-flow-integrity-in-android-kernel.html
Signature · 1 line
Kees Cook