linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kasan: don't emit builtin calls when sanitization is off
@ 2018-01-19 14:54 Andrey Konovalov
  2018-01-19 15:57 ` Andrey Ryabinin
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Konovalov @ 2018-01-19 14:54 UTC (permalink / raw)
  To: Masahiro Yamada, Michal Marek, Andrey Ryabinin,
	Alexander Potapenko, Dmitry Vyukov, linux-kbuild, linux-kernel,
	kasan-dev
  Cc: Kostya Serebryany, Evgeniy Stepanov, Nick Desaulniers,
	Stephen Hines, Pirama Arumuga Nainar, Manoj Gupta, Greg Hackmann,
	Andrey Konovalov

With KASAN enabled the kernel has two different memset() functions, one
with KASAN checks (memset) and one without (__memset). KASAN uses some
macro tricks to use the proper version where required. For example memset()
calls in mm/slub.c are without KASAN checks, since they operate on poisoned
slab object metadata.

The issue is that clang emits memset() calls even when there is no memset()
in the source code. They get linked with improper memset() implementation
and the kernel fails to boot due to a huge amount of KASAN reports during
early boot stages.

The solution is to add -fno-builtin flag for files with KASAN_SANITIZE := n
marker.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---

This patch should be applied on top of the v4 "kasan: support alloca, LLVM"
patchset from Paul Lawrence.

 Makefile               | 3 ++-
 scripts/Makefile.kasan | 3 +++
 scripts/Makefile.lib   | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index bf5b8cbb9469..d45e31b293d0 100644
--- a/Makefile
+++ b/Makefile
@@ -432,7 +432,8 @@ export MAKE AWK GENKSYMS INSTALLKERNEL PERL PYTHON UTS_MACHINE
 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
-export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE CFLAGS_KASAN CFLAGS_UBSAN
+export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN
 export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
 export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
 export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index dbbd4382f15a..db473309f136 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -39,4 +39,7 @@ else
 endif
 
 CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
+
+CFLAGS_KASAN_NOSANITIZE := $(call cc-option, -fno-builtin)
+
 endif
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 1ca4dcd2d500..015aa9dbad86 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -121,7 +121,7 @@ endif
 ifeq ($(CONFIG_KASAN),y)
 _c_flags += $(if $(patsubst n%,, \
 		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
-		$(CFLAGS_KASAN))
+		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
 endif
 
 ifeq ($(CONFIG_UBSAN),y)
-- 
2.16.0.rc1.238.g530d649a79-goog

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] kasan: don't emit builtin calls when sanitization is off
  2018-01-19 14:54 [PATCH] kasan: don't emit builtin calls when sanitization is off Andrey Konovalov
@ 2018-01-19 15:57 ` Andrey Ryabinin
  2018-01-19 17:58   ` Andrey Konovalov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Ryabinin @ 2018-01-19 15:57 UTC (permalink / raw)
  To: Andrey Konovalov, Masahiro Yamada, Michal Marek,
	Alexander Potapenko, Dmitry Vyukov, linux-kbuild, linux-kernel,
	kasan-dev
  Cc: Kostya Serebryany, Evgeniy Stepanov, Nick Desaulniers,
	Stephen Hines, Pirama Arumuga Nainar, Manoj Gupta, Greg Hackmann,
	Andrew Morton



On 01/19/2018 05:54 PM, Andrey Konovalov wrote:

> diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
> index dbbd4382f15a..db473309f136 100644
> --- a/scripts/Makefile.kasan
> +++ b/scripts/Makefile.kasan
> @@ -39,4 +39,7 @@ else
>  endif
>  
>  CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
> +
> +CFLAGS_KASAN_NOSANITIZE := $(call cc-option, -fno-builtin)
> +

I think it should be fine to drop cc-option here. Even ancient versions of GCC have -fno-builtin.

>  endif
> diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
> index 1ca4dcd2d500..015aa9dbad86 100644
> --- a/scripts/Makefile.lib
> +++ b/scripts/Makefile.lib
> @@ -121,7 +121,7 @@ endif
>  ifeq ($(CONFIG_KASAN),y)
>  _c_flags += $(if $(patsubst n%,, \
>  		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
> -		$(CFLAGS_KASAN))
> +		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
>  endif
>  
>  ifeq ($(CONFIG_UBSAN),y)
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kasan: don't emit builtin calls when sanitization is off
  2018-01-19 15:57 ` Andrey Ryabinin
@ 2018-01-19 17:58   ` Andrey Konovalov
  0 siblings, 0 replies; 3+ messages in thread
From: Andrey Konovalov @ 2018-01-19 17:58 UTC (permalink / raw)
  To: Andrey Ryabinin
  Cc: Masahiro Yamada, Michal Marek, Alexander Potapenko,
	Dmitry Vyukov, linux-kbuild, LKML, kasan-dev, Kostya Serebryany,
	Evgeniy Stepanov, Nick Desaulniers, Stephen Hines,
	Pirama Arumuga Nainar, Manoj Gupta, Greg Hackmann, Andrew Morton

On Fri, Jan 19, 2018 at 4:57 PM, Andrey Ryabinin
<aryabinin@virtuozzo.com> wrote:
>
>
> On 01/19/2018 05:54 PM, Andrey Konovalov wrote:
>
>> diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
>> index dbbd4382f15a..db473309f136 100644
>> --- a/scripts/Makefile.kasan
>> +++ b/scripts/Makefile.kasan
>> @@ -39,4 +39,7 @@ else
>>  endif
>>
>>  CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
>> +
>> +CFLAGS_KASAN_NOSANITIZE := $(call cc-option, -fno-builtin)
>> +
>
> I think it should be fine to drop cc-option here. Even ancient versions of GCC have -fno-builtin.
>

Mailed v2.

Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-01-19 17:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-19 14:54 [PATCH] kasan: don't emit builtin calls when sanitization is off Andrey Konovalov
2018-01-19 15:57 ` Andrey Ryabinin
2018-01-19 17:58   ` Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).