[PATCH] kasan: fix clang compilation warning due to stack protector

6 views
Skip to first unread message

Andrey Konovalov

unread,
May 28, 2020, 1:20:33 PM5/28/20
to Andrew Morton, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, kasa...@googlegroups.com, linu...@kvack.org, linux-...@vger.kernel.org, Qian Cai, Andrey Konovalov
KASAN uses a single cc-option invocation to disable both conserve-stack
and stack-protector flags. The former flag is not present in Clang, which
causes cc-option to fail, and results in stack-protector being enabled.

Fix by using separate cc-option calls for each flag. Also collect all
flags in a variable to avoid calling cc-option multiple times for
different files.

Reported-by: Qian Cai <c...@lca.pw>
Signed-off-by: Andrey Konovalov <andre...@google.com>
---
mm/kasan/Makefile | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
index de3121848ddf..bf6f7b1f6b18 100644
--- a/mm/kasan/Makefile
+++ b/mm/kasan/Makefile
@@ -15,14 +15,19 @@ CFLAGS_REMOVE_tags_report.o = $(CC_FLAGS_FTRACE)

# Function splitter causes unnecessary splits in __asan_load1/__asan_store1
# see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
-CFLAGS_common.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_generic.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_generic_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_init.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_quarantine.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_tags.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_tags_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
+CC_FLAGS_KASAN_CONFLICT := $(call cc-option, -fno-conserve-stack)
+CC_FLAGS_KASAN_CONFLICT += $(call cc-option, -fno-stack-protector)
+# Disable branch tracing to avoid recursion.
+CC_FLAGS_KASAN_CONFLICT += -DDISABLE_BRANCH_PROFILING
+
+CFLAGS_common.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_generic.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_generic_report.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_init.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_quarantine.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_report.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_tags.o := $(CC_FLAGS_KASAN_CONFLICT)
+CFLAGS_tags_report.o := $(CC_FLAGS_KASAN_CONFLICT)

obj-$(CONFIG_KASAN) := common.o init.o report.o
obj-$(CONFIG_KASAN_GENERIC) += generic.o generic_report.o quarantine.o
--
2.27.0.rc0.183.gde8f92d652-goog

Marco Elver

unread,
May 29, 2020, 10:56:22 AM5/29/20
to Andrey Konovalov, Andrew Morton, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, kasan-dev, Linux Memory Management List, LKML, Qian Cai
On Thu, 28 May 2020 at 19:20, 'Andrey Konovalov' via kasan-dev
<kasa...@googlegroups.com> wrote:
>
> KASAN uses a single cc-option invocation to disable both conserve-stack
> and stack-protector flags. The former flag is not present in Clang, which
> causes cc-option to fail, and results in stack-protector being enabled.
>
> Fix by using separate cc-option calls for each flag. Also collect all
> flags in a variable to avoid calling cc-option multiple times for
> different files.
>
> Reported-by: Qian Cai <c...@lca.pw>
> Signed-off-by: Andrey Konovalov <andre...@google.com>
> ---

Thank you! I was about to send an almost identical patch, as I
encountered this when using clang.

Reviewed-by: Marco Elver <el...@google.com>

> mm/kasan/Makefile | 21 +++++++++++++--------
> 1 file changed, 13 insertions(+), 8 deletions(-)
>
> diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
> index de3121848ddf..bf6f7b1f6b18 100644
> --- a/mm/kasan/Makefile
> +++ b/mm/kasan/Makefile
> @@ -15,14 +15,19 @@ CFLAGS_REMOVE_tags_report.o = $(CC_FLAGS_FTRACE)
>
> # Function splitter causes unnecessary splits in __asan_load1/__asan_store1
> # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
> -CFLAGS_common.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_generic.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_generic_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_init.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_quarantine.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_tags.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> -CFLAGS_tags_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
> +CC_FLAGS_KASAN_CONFLICT := $(call cc-option, -fno-conserve-stack)
> +CC_FLAGS_KASAN_CONFLICT += $(call cc-option, -fno-stack-protector)
> +# Disable branch tracing to avoid recursion.
> +CC_FLAGS_KASAN_CONFLICT += -DDISABLE_BRANCH_PROFILING

Note that maybe CC_FLAGS_KASAN_RUNTIME could be a better name, because
other flags added in future might not be conflict-related. But until
that future, it doesn't really matter.

> +CFLAGS_common.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_generic.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_generic_report.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_init.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_quarantine.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_report.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_tags.o := $(CC_FLAGS_KASAN_CONFLICT)
> +CFLAGS_tags_report.o := $(CC_FLAGS_KASAN_CONFLICT)
>
> obj-$(CONFIG_KASAN) := common.o init.o report.o
> obj-$(CONFIG_KASAN_GENERIC) += generic.o generic_report.o quarantine.o
> --
> 2.27.0.rc0.183.gde8f92d652-goog
>
> --
> You received this message because you are subscribed to the Google Groups "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/ced83584eec86a1a9ce264013cf6c0da5e0add6a.1590686292.git.andreyknvl%40google.com.

Andrey Konovalov

unread,
May 29, 2020, 3:12:31 PM5/29/20
to Andrew Morton, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver, kasa...@googlegroups.com, linu...@kvack.org, linux-...@vger.kernel.org, Qian Cai, Andrey Konovalov
KASAN uses a single cc-option invocation to disable both conserve-stack
and stack-protector flags. The former flag is not present in Clang, which
causes cc-option to fail, and results in stack-protector being enabled.

Fix by using separate cc-option calls for each flag. Also collect all
flags in a variable to avoid calling cc-option multiple times for
different files.

Reported-by: Qian Cai <c...@lca.pw>
Signed-off-by: Andrey Konovalov <andre...@google.com>
---

Changes v1 -> v2:
- Renamed CC_FLAGS_KASAN_CONFLICT to CC_FLAGS_KASAN_RUNTIME.

---
mm/kasan/Makefile | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
index de3121848ddf..d532c2587731 100644
--- a/mm/kasan/Makefile
+++ b/mm/kasan/Makefile
@@ -15,14 +15,19 @@ CFLAGS_REMOVE_tags_report.o = $(CC_FLAGS_FTRACE)

# Function splitter causes unnecessary splits in __asan_load1/__asan_store1
# see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533
-CFLAGS_common.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_generic.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_generic_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_init.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_quarantine.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_tags.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
-CFLAGS_tags_report.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -DDISABLE_BRANCH_PROFILING
+CC_FLAGS_KASAN_RUNTIME := $(call cc-option, -fno-conserve-stack)
+CC_FLAGS_KASAN_RUNTIME += $(call cc-option, -fno-stack-protector)
+# Disable branch tracing to avoid recursion.
+CC_FLAGS_KASAN_RUNTIME += -DDISABLE_BRANCH_PROFILING
+
+CFLAGS_common.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_generic.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_generic_report.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_init.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_quarantine.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_report.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
+CFLAGS_tags_report.o := $(CC_FLAGS_KASAN_RUNTIME)

Andrey Konovalov

unread,
May 29, 2020, 3:13:38 PM5/29/20
to Marco Elver, Andrew Morton, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, kasan-dev, Linux Memory Management List, LKML, Qian Cai
CC_FLAGS_KASAN_RUNTIME is a better name, sent v2, thanks!

Marco Elver

unread,
May 29, 2020, 3:16:58 PM5/29/20
to Andrey Konovalov, Andrew Morton, Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, kasan-dev, Linux Memory Management List, LKML, Qian Cai
On Fri, 29 May 2020 at 21:12, Andrey Konovalov <andre...@google.com> wrote:
>
> KASAN uses a single cc-option invocation to disable both conserve-stack
> and stack-protector flags. The former flag is not present in Clang, which
> causes cc-option to fail, and results in stack-protector being enabled.
>
> Fix by using separate cc-option calls for each flag. Also collect all
> flags in a variable to avoid calling cc-option multiple times for
> different files.
>
> Reported-by: Qian Cai <c...@lca.pw>
> Signed-off-by: Andrey Konovalov <andre...@google.com>
> ---
>
> Changes v1 -> v2:
> - Renamed CC_FLAGS_KASAN_CONFLICT to CC_FLAGS_KASAN_RUNTIME.

Reviewed-by: Marco Elver <el...@google.com>

Thanks!
Reply all
Reply to author
Forward
0 new messages