Skip to contentSkip to conversation

kasan: fix kasan_check_read/write definitions

PATCH In review Arnd Bergmann 5 participants 8 messages Dec 11, 2018 – Jan 11, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kasan: fix kasan_check_read/write definitions

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -0
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
Signature · 1 line
2.20.0
reviewer
#1 lore raw
Quoted 8 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

Quoted 7 lines

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Alexander, I think you are doing a similar thing for similar reasons in KMSAN patch (see KMSAN_CHECK_ATOMIC_PARAMS):
https://github.com/google/kmsan/commit/17ebbfe19624c84adf79b0e5a74fd258c49ff12b
Namely, non-KMSAN-instrumented files must not get KMSAN callbacks from atomics too.

Arnd patch does it the other way around: non-instrumented files need to opt-in instead of opt-out.
Let's settle on a common way to do this, so that we can use it consistently across all tools.

Quoted 37 lines
Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -1
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
--

2.20.0

#2 lore raw
Quoted 19 lines · Dmitry Vyukov
Quoted 8 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

Quoted 7 lines

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Quoted 5 lines

Alexander, I think you are doing a similar thing for similar reasons in KMSAN patch (see KMSAN_CHECK_ATOMIC_PARAMS):
https://github.com/google/kmsan/commit/17ebbfe19624c84adf79b0e5a74fd258c49ff12b
Namely, non-KMSAN-instrumented files must not get KMSAN callbacks from atomics too.

I'll need to double-check, but it occurs to me that we won't need additional hooks for atomics in KMSAN - the compiler instrumentation should suffice.

Quoted 2 lines

Arnd patch does it the other way around: non-instrumented files need to opt-in instead of opt-out.

Shouldn't we put __SANITIZE_ADDRESS__ somewhere into mm/kasan/kasan.h then?

Quoted 42 lines

Let's settle on a common way to do this, so that we can use it consistently across all tools.

Quoted 37 lines
Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -1
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
--

2.20.0

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
reviewer
#3 lore raw
Quoted 16 lines · Alexander Potapenko
Quoted 16 lines
Quoted 14 lines

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Quoted 8 lines
Quoted 5 lines

Alexander, I think you are doing a similar thing for similar reasons in KMSAN patch (see KMSAN_CHECK_ATOMIC_PARAMS):
https://github.com/google/kmsan/commit/17ebbfe19624c84adf79b0e5a74fd258c49ff12b
Namely, non-KMSAN-instrumented files must not get KMSAN callbacks from atomics too.

I'll need to double-check, but it occurs to me that we won't need additional hooks for atomics in KMSAN - the compiler instrumentation should suffice.

Compiler asm instrumentation will only insert conservative initialization, but not checks of arguments, right?
I mean these checks are optional in the sense that it's only false negatives, but since we already have them and they don't seem to lead to false positives, why do we want to remove them?

Quoted 59 lines
Quoted 2 lines

Arnd patch does it the other way around: non-instrumented files need to opt-in instead of opt-out.

Shouldn't we put __SANITIZE_ADDRESS__ somewhere into mm/kasan/kasan.h then?

Quoted 34 lines

Let's settle on a common way to do this, so that we can use it consistently across all tools.

Quoted 29 lines
Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +0 -0
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
Quoted 8 lines
Quoted 8 lines
mm/kasan/common.c +2 -1
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
--

2.20.0

--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg

#4 lore raw
In reply to Arnd Bergmann
Quoted 52 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -0
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
Signature · 1 line
2.20.0

Hi all,

Was there any other movement on this patch? I am noticing this fail as well and I have applied this patch in the meantime; it would be nice for it to be merged so I could drop it from my stack.

Thanks,
Nathan

reviewer
#5 lore raw
Quoted 57 lines · Nathan Chancellor
Quoted 50 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -1
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
--
Quoted 2 lines

2.20.0

Hi all,

Quoted 3 lines

Was there any other movement on this patch? I am noticing this fail as well and I have applied this patch in the meantime; it would be nice for it to be merged so I could drop it from my stack.

Alexander, ping, you wanted to double-check re KMSAN asm instrumentation and then decide on a common approach for KASAN and KMSAN.

reviewer Reviewed-by
#6 lore raw
Quoted 15 lines · Dmitry Vyukov
Quoted 12 lines · Nathan Chancellor
Quoted 10 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Fixes b1864b828644 ("locking/atomics: build atomic headers as required")
Quoted 37 lines
Quoted 37 lines
Quoted 32 lines
---
 include/linux/kasan-checks.h | 2 +-
 mm/kasan/common.c            | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/kasan-checks.h +1 -1
index d314150658a4..a61dc075e2ce 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_KASAN_CHECKS_H
 #define _LINUX_KASAN_CHECKS_H
 
-#ifdef CONFIG_KASAN
+#if defined(__SANITIZE_ADDRESS__) || defined(__KASAN_INTERNAL)
 void kasan_check_read(const volatile void *p, unsigned int size);
 void kasan_check_write(const volatile void *p, unsigned int size);
 #else
mm/kasan/common.c +2 -1
index 03d5d1374ca7..51a7932c33a3 100644
@@ -14,6 +14,8 @@
  *
  */
 
+#define __KASAN_INTERNAL
+
 #include <linux/export.h>
 #include <linux/interrupt.h>
 #include <linux/init.h>
--
Quoted 2 lines

2.20.0

Hi all,

Quoted 7 lines
Quoted 3 lines

Was there any other movement on this patch? I am noticing this fail as well and I have applied this patch in the meantime; it would be nice for it to be merged so I could drop it from my stack.

Alexander, ping, you wanted to double-check re KMSAN asm instrumentation and then decide on a common approach for KASAN and KMSAN.

I like Arnd's approach and will do the same for KMSAN. Arnd, please go ahead submitting your patch.
The only possible issue I'm anticipating is that in the future we may want to disable the checks in non-KASAN code (e.g. in arch/ or mm/), so __KASAN_INTERNAL may not be the best name, but that's up to you.

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Acked-by on v1 1/1
#7 lore raw
In reply to Arnd Bergmann
Quoted 15 lines · Arnd Bergmann

Building little-endian allmodconfig kernels on arm64 started failing with the generated atomic.h implementation, since we now try to call kasan helpers from the EFI stub:

aarch64-linux-gnu-ld: drivers/firmware/efi/libstub/arm-stub.stub.o: in function `atomic_set':
include/generated/atomic-instrumented.h:44: undefined reference to `__efistub_kasan_check_write'

I suspect that we get similar problems in other files that explicitly disable KASAN for some reason but call atomic_t based helper functions.

We can fix this by checking the predefined __SANITIZE_ADDRESS__ macro that the compiler sets instead of checking CONFIG_KASAN, but this in turn requires a small hack in mm/kasan/common.c so we do see the extern declaration there instead of the inline function.

Quoted 4 lines
Fixes b1864b828644 ("locking/atomics: build atomic headers as required")

---