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

CFI: Move function_nocfi() into compiler.h

Currently the common definition of function_nocfi() is provided by
<linux/mm.h>, and architectures are expected to provide a definition in
<asm/memory.h>. Due to header dependencies, this can make it hard to use
function_nocfi() in low-level headers.

As function_nocfi() has no dependency on any mm code, nor on any memory
definitions, it doesn't need to live in <linux/mm.h> or <asm/memory.h>.
Generally, it would make more sense for it to live in
<linux/compiler.h>, where an architecture can override it in
<asm/compiler.h>.

Move the definitions accordingly.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20210602153701.35957-1-mark.rutland@arm.com

authored by

Mark Rutland and committed by
Kees Cook
590e8a08 3f1639f8

+26 -26
+16
arch/arm64/include/asm/compiler.h
··· 23 23 #define __builtin_return_address(val) \ 24 24 (void *)(ptrauth_clear_pac((unsigned long)__builtin_return_address(val))) 25 25 26 + #ifdef CONFIG_CFI_CLANG 27 + /* 28 + * With CONFIG_CFI_CLANG, the compiler replaces function address 29 + * references with the address of the function's CFI jump table 30 + * entry. The function_nocfi macro always returns the address of the 31 + * actual function instead. 32 + */ 33 + #define function_nocfi(x) ({ \ 34 + void *addr; \ 35 + asm("adrp %0, " __stringify(x) "\n\t" \ 36 + "add %0, %0, :lo12:" __stringify(x) \ 37 + : "=r" (addr)); \ 38 + addr; \ 39 + }) 40 + #endif 41 + 26 42 #endif /* __ASM_COMPILER_H */
-16
arch/arm64/include/asm/memory.h
··· 323 323 #define virt_to_pfn(x) __phys_to_pfn(__virt_to_phys((unsigned long)(x))) 324 324 #define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x)) 325 325 326 - #ifdef CONFIG_CFI_CLANG 327 - /* 328 - * With CONFIG_CFI_CLANG, the compiler replaces function address 329 - * references with the address of the function's CFI jump table 330 - * entry. The function_nocfi macro always returns the address of the 331 - * actual function instead. 332 - */ 333 - #define function_nocfi(x) ({ \ 334 - void *addr; \ 335 - asm("adrp %0, " __stringify(x) "\n\t" \ 336 - "add %0, %0, :lo12:" __stringify(x) \ 337 - : "=r" (addr)); \ 338 - addr; \ 339 - }) 340 - #endif 341 - 342 326 /* 343 327 * virt_to_page(x) convert a _valid_ virtual address to struct page * 344 328 * virt_addr_valid(x) indicates whether a virtual address is valid
+10
include/linux/compiler.h
··· 213 213 __v; \ 214 214 }) 215 215 216 + /* 217 + * With CONFIG_CFI_CLANG, the compiler replaces function addresses in 218 + * instrumented C code with jump table addresses. Architectures that 219 + * support CFI can define this macro to return the actual function address 220 + * when needed. 221 + */ 222 + #ifndef function_nocfi 223 + #define function_nocfi(x) (x) 224 + #endif 225 + 216 226 #endif /* __KERNEL__ */ 217 227 218 228 /*
-10
include/linux/mm.h
··· 125 125 #endif 126 126 127 127 /* 128 - * With CONFIG_CFI_CLANG, the compiler replaces function addresses in 129 - * instrumented C code with jump table addresses. Architectures that 130 - * support CFI can define this macro to return the actual function address 131 - * when needed. 132 - */ 133 - #ifndef function_nocfi 134 - #define function_nocfi(x) (x) 135 - #endif 136 - 137 - /* 138 128 * To prevent common memory management code establishing 139 129 * a zero page mapping on a read fault. 140 130 * This macro should be defined within <asm/pgtable.h>.