Fix CFI failures with GCOV_PROFILE_ALL
cover Fix CFI failures with GCOV_PROFILE_ALL
Hi folks,
The following two patches fix CFI failures with GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.o
init/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
base-commit: 007034977130b49b618a5206aad54f634d9f169c
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
PATCH 1/2 kbuild: Fix CFI failures with GCOV
With GCOV_PROFILE_ALL, Clang injects __llvm_gcov_* functions to each object file, and the functions are indirectly called during boot. However, when code is injected to object files that are not part of vmlinux.o, it's also not processed by objtool, which breaks CFI hash randomization as the hashes in these files won't be included in the .cfi_sites section and thus won't be randomized.
Similarly to commit 42633ed852de ("kbuild: Fix CFI hash randomization with KASAN"), disable GCOV for .vmlinux.export.o and init/version-timestamp.o to avoid emitting unnecessary functions to object files that don't otherwise have executable code.
--- init/Makefile | 1 + scripts/Makefile.vmlinux | 1 + 2 files changed, 2 insertions(+)
init/Makefile +1 -0
@@ -60,3 +60,4 @@ include/generated/utsversion.h: FORCE $(obj)/version-timestamp.o: include/generated/utsversion.h CFLAGS_version-timestamp.o := -include include/generated/utsversion.h KASAN_SANITIZE_version-timestamp.o := n +GCOV_PROFILE_version-timestamp.o := n
scripts/Makefile.vmlinux +1 -0
@@ -19,6 +19,7 @@ quiet_cmd_cc_o_c = CC $@ ifdef CONFIG_MODULES KASAN_SANITIZE_.vmlinux.export.o := n +GCOV_PROFILE_.vmlinux.export.o := n targets += .vmlinux.export.o vmlinux: .vmlinux.export.o endif
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
PATCH 2/2 kbuild: Disable GCOV for *.mod.o
In reply to Sami TolvanenWith GCOV_PROFILE_ALL, Clang injects __llvm_gcov_* functions to each object file, including the *.mod.o. As we filter out CC_FLAGS_CFI for *.mod.o, the compiler won't generate type hashes for the injected functions, and therefore indirectly calling them during module loading trips indirect call checking.
Enabling CFI for *.mod.o isn't sufficient to fix this issue after commit 0c3e806ec0f9 ("x86/cfi: Add boot time hash randomization"), as *.mod.o aren't processed by objtool, which means any hashes emitted there won't be randomized. Therefore, in addition to disabling CFI for *.mod.o, also disable GCOV, as the object files don't otherwise contain any executable code.
--- scripts/Makefile.modfinal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.modfinal +1 -1
@@ -23,7 +23,7 @@ modname = $(notdir $(@:.mod.o=)) part-of-module = y quiet_cmd_cc_o_c = CC [M] $@ - cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI), $(c_flags)) -c -o $@ $< + cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI) $(CFLAGS_GCOV), $(c_flags)) -c -o $@ $< %.mod.o: %.mod.c FORCE $(call if_changed_dep,cc_o_c)
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Quoted 9 lines · Sami Tolvanen
Hi folks,
The following two patches fix CFI failures with
GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.
Quoted 9 lines
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.oinit/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
Urgh, tricky stuff this.
And yes, objtool essentially assumes vmlinux.o is complete and does LTO like passes. Is there something kbuild can do to ensure noting else gets linked in after this?
Quoted 9 lines · Sami Tolvanen
Hi folks,
The following two patches fix CFI failures with
GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.
Quoted 8 lines
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.oinit/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
Nice hunting!
Should these get Cc: stable tags maybe?
Signature · 1 line
Kees Cook
Quoted 20 lines · Kees Cook
Quoted 9 lines · Sami Tolvanen
Hi folks,
The following two patches fix CFI failures with
GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.Quoted 8 lines
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.oinit/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
Quoted 5 lines
Nice hunting!
Reviewed-by Kees Cook <[email protected]>Should these get Cc: stable tags maybe?
I was under the impression that Fixes: tags would be sufficient these days, but agreed, explicit Cc: probably wouldn't hurt.
Sami
Quoted 24 lines · Sami Tolvanen
Quoted 22 lines · Kees Cook
Quoted 9 lines · Sami Tolvanen
Hi folks,
The following two patches fix CFI failures with
GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.Quoted 8 lines
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.oinit/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)Nice hunting!
Quoted 8 lines
Quoted 3 lines
Reviewed-by Kees Cook <[email protected]>Should these get Cc: stable tags maybe?
I was under the impression that Fixes: tags would be sufficient these days, but agreed, explicit Cc: probably wouldn't hurt.
Sami
Both applied to linux-kbuild.
I also think Fixes: tags would be enough
to make them back-ported.
Signature · 2 lines
Best Regards Masahiro Yamada
Quoted 10 lines · Sami Tolvanen
Hi folks,
The following two patches fix CFI failures with
GCOV_PROFILE_ALL, where the compiler injects indirectly called functions to object files that otherwise contain no executable code, and are not processed by objtool or don't have CFI enabled. This results in missing or incorrect type hashes during boot and when modules are loaded.
Quoted 8 lines
Sami Tolvanen (2):
kbuild: Fix CFI failures with GCOV
kbuild: Disable GCOV for *.mod.oinit/Makefile | 1 + scripts/Makefile.modfinal | 2 +- scripts/Makefile.vmlinux | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
Thanks for the patches!
Quoted 7 lines
base-commit:
007034977130b49b618a5206aad54f634d9f169c--
2.41.0.162.gfafddb0af9-goog
Signature · 2 lines
Thanks, ~Nick Desaulniers