From 2415092cd2eb5f17fa4acc0cea53c4fdc4bf2b61 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Mon, 17 Aug 2020 15:02:09 -0700 Subject: [PATCH] Makefile: add -fno-builtin-stpcpy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LLVM implemented a recent "libcall optimization" that lowers calls to `sprintf(dest, "%s", str)` where the return value is used to `stpcpy(dest, str) - dest`. This generally avoids the machinery involved in parsing format strings. This optimization was introduced into clang-12. Because the kernel does not provide an implementation of stpcpy, we observe linkage failures for almost all targets when building with ToT clang. The interface is unsafe as it does not perform any bounds checking. Disable this "libcall optimization" via `-fno-builtin-stpcpy`. Unlike commit 5f074f3e192f ("lib/string.c: implement a basic bcmp") which cited failures with `-fno-builtin-*` flags being retained in LLVM LTO, that bug seems to have been fixed by https://reviews.llvm.org/D71193, so the above sha can now be reverted in favor of `-fno-builtin-bcmp`. Reported-by: Sami Tolvanen Suggested-by: Dávid Bolvanský Suggested-by: Kees Cook Signed-off-by: Nick Desaulniers Cc: stable@vger.kernel.org # 4.4 Link: https://bugs.llvm.org/show_bug.cgi?id=47162 Link: https://github.com/ClangBuiltLinux/linux/issues/1126 Link: https://reviews.llvm.org/D85963 --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index b9072fe3c4927..f90d41602a1d6 100644 --- a/Makefile +++ b/Makefile @@ -698,6 +698,12 @@ KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \ # Tell gcc to never replace conditional load with a non-conditional one KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0) +# The compiler may "libcall optimize" certain function calls into the below +# functions, for architectures that don't use -ffreestanding. If we don't plan +# to provide implementations of these routines, then prevent the compiler from +# emitting calls to what will be undefined symbols. +KBUILD_CFLAGS += -fno-builtin-stpcpy + # check for 'asm goto' ifeq ($(call shell-cached,$(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC) $(KBUILD_CFLAGS)), y) KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO