Skip to contentSkip to conversation

kasan: add clang support

PATCH In review Greg Hackmann 3 participants 17 messages Jul 6, 2017 – Jul 14, 2017

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

cover kasan: add clang support

This patch series adds support for building KASAN-enabled kernels with clang. This mostly involves adding callbacks for a couple of new features in LLVM's AddressSanitizer implementation. We also need to probe for the (slightly different) CFLAGS used to configure ASAN with clang.

*** BLURB HERE ***

Alexander Potapenko (1):
kasan: added functions for unpoisoning stack variables

Greg Hackmann (3):
kasan: support alloca() poisoning
kasan: support LLVM-style asan parameters
kasan: add compiler support for clang

 include/linux/compiler-clang.h | 10 ++++++++++
 lib/test_kasan.c               | 22 ++++++++++++++++++++++
 mm/kasan/kasan.c               | 41 +++++++++++++++++++++++++++++++++++++++++
 mm/kasan/kasan.h               |  8 ++++++++
 mm/kasan/report.c              |  3 +++
 scripts/Makefile.kasan         | 10 +++++++++-
 6 files changed, 93 insertions(+), 1 deletion(-)
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#1 lore raw

PATCH 1/4 kasan: support alloca() poisoning

clang's AddressSanitizer implementation adds redzones on either side of alloca()ed buffers. These redzones are 32-byte aligned and at least 32 bytes long.

__asan_alloca_poison() is passed the size and address of the allocated buffer, *excluding* the redzones on either side. The left redzone will always be to the immediate left of this buffer; but AddressSanitizer may need to add padding between the end of the buffer and the right redzone. If there are any 8-byte chunks inside this padding, we should poison those too.

__asan_allocas_unpoison() is just passed the top and bottom of the dynamic stack area, so unpoisoning is simpler.

---
 lib/test_kasan.c  | 22 ++++++++++++++++++++++
 mm/kasan/kasan.c  | 26 ++++++++++++++++++++++++++
 mm/kasan/kasan.h  |  8 ++++++++
 mm/kasan/report.c |  3 +++
 4 files changed, 59 insertions(+)
lib/test_kasan.c +22 -0
index a25c9763fce1..f774fcafb696 100644
@@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void)
 	p[1023] = 1;
 }
 
+static noinline void __init kasan_alloca_oob_left(void)
+{
+	volatile int i = 10;
+	char alloca_array[i];
+	char *p = alloca_array - 1;
+
+	pr_info("out-of-bounds to left on alloca\n");
+	*(volatile char *)p;
+}
+
+static noinline void __init kasan_alloca_oob_right(void)
+{
+	volatile int i = 10;
+	char alloca_array[i];
+	char *p = alloca_array + round_up(i, 8);
+
+	pr_info("out-of-bounds to right on alloca\n");
+	*(volatile char *)p;
+}
+
 static int __init kmalloc_tests_init(void)
 {
 	/*
@@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void)
 	memcg_accounted_kmem_cache();
 	kasan_stack_oob();
 	kasan_global_oob();
+	kasan_alloca_oob_left();
+	kasan_alloca_oob_right();
 	ksize_unpoisons_memory();
 	copy_user_test();
 	use_after_scope_test();
mm/kasan/kasan.c +26 -0
index c81549d5c833..892b626f564b 100644
@@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size)
 }
 EXPORT_SYMBOL(__asan_unpoison_stack_memory);
 
+/* Emitted by compiler to poison alloca()ed objects. */
+void __asan_alloca_poison(unsigned long addr, size_t size)
+{
+	size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
+	size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
+			round_up(size, KASAN_SHADOW_SCALE_SIZE);
+
+	const void *left_redzone = (const void *)(addr -
+			KASAN_ALLOCA_REDZONE_SIZE);
+	const void *right_redzone = (const void *)(addr + rounded_up_size);
+
+	kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
+			KASAN_ALLOCA_LEFT);
+	kasan_poison_shadow(right_redzone,
+			padding_size + KASAN_ALLOCA_REDZONE_SIZE,
+			KASAN_ALLOCA_RIGHT);
+}
+EXPORT_SYMBOL(__asan_alloca_poison);
+
+/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */
+void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
+{
+	kasan_unpoison_shadow(stack_top, stack_bottom - stack_top);
+}
+EXPORT_SYMBOL(__asan_allocas_unpoison);
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 static int kasan_mem_notifier(struct notifier_block *nb,
 			unsigned long action, void *data)
mm/kasan/kasan.h +8 -0
index 1229298cce64..b857dc70d6a2 100644
@@ -23,6 +23,14 @@
 #define KASAN_STACK_PARTIAL     0xF4
 #define KASAN_USE_AFTER_SCOPE   0xF8
 
+/*
+ * alloca redzone shadow values
+ */
+#define KASAN_ALLOCA_LEFT	0xCA
+#define KASAN_ALLOCA_RIGHT	0xCB
+
+#define KASAN_ALLOCA_REDZONE_SIZE	32
+
 /* Don't break randconfig/all*config builds */
 #ifndef KASAN_ABI_VERSION
 #define KASAN_ABI_VERSION 1
mm/kasan/report.c +3 -0
index beee0e980e2d..c6a5b7ab9e3a 100644
@@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)
 		break;
 	case KASAN_USE_AFTER_SCOPE:
 		bug_type = "use-after-scope";
+	case KASAN_ALLOCA_LEFT:
+	case KASAN_ALLOCA_RIGHT:
+		bug_type = "alloca-out-of-bounds";
 		break;
 	}
 
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
#2 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

mm/kasan/report.c static const char *get_shadow_bug_type(struct kasan_access_info *info) · line 108

Quoted 9 lines · Greg Hackmann
mm/kasan/report.c +3 -0
@@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)
  		break;
  	case KASAN_USE_AFTER_SCOPE:
  		bug_type = "use-after-scope";
+	case KASAN_ALLOCA_LEFT:
+	case KASAN_ALLOCA_RIGHT:
+		bug_type = "alloca-out-of-bounds";
  		break;
  	}

There needs to be a "break" above the new case statements. I'll wait to see if there's any other feedback, then send out a V2 patch that fixes this.

reviewer
#3 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

In reply to Greg Hackmann
Quoted 67 lines · Greg Hackmann

clang's AddressSanitizer implementation adds redzones on either side of alloca()ed buffers. These redzones are 32-byte aligned and at least 32 bytes long.

__asan_alloca_poison() is passed the size and address of the allocated buffer, *excluding* the redzones on either side. The left redzone will always be to the immediate left of this buffer; but AddressSanitizer may need to add padding between the end of the buffer and the right redzone. If there are any 8-byte chunks inside this padding, we should poison those too.

__asan_allocas_unpoison() is just passed the top and bottom of the dynamic stack area, so unpoisoning is simpler.

---
 lib/test_kasan.c  | 22 ++++++++++++++++++++++
 mm/kasan/kasan.c  | 26 ++++++++++++++++++++++++++
 mm/kasan/kasan.h  |  8 ++++++++
 mm/kasan/report.c |  3 +++
 4 files changed, 59 insertions(+)
lib/test_kasan.c +22 -0
index a25c9763fce1..f774fcafb696 100644
@@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void)
        p[1023] = 1;
 }
 
+static noinline void __init kasan_alloca_oob_left(void)
+{
+       volatile int i = 10;
+       char alloca_array[i];
+       char *p = alloca_array - 1;
+
+       pr_info("out-of-bounds to left on alloca\n");
+       *(volatile char *)p;
+}
+
+static noinline void __init kasan_alloca_oob_right(void)
+{
+       volatile int i = 10;
+       char alloca_array[i];
+       char *p = alloca_array + round_up(i, 8);
+
+       pr_info("out-of-bounds to right on alloca\n");
+       *(volatile char *)p;
+}
+
 static int __init kmalloc_tests_init(void)
 {
        /*
@@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void)
        memcg_accounted_kmem_cache();
        kasan_stack_oob();
        kasan_global_oob();
+       kasan_alloca_oob_left();
+       kasan_alloca_oob_right();
        ksize_unpoisons_memory();
        copy_user_test();
        use_after_scope_test();
mm/kasan/kasan.c +0 -0
index c81549d5c833..892b626f564b 100644

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 810

Quoted 9 lines
mm/kasan/kasan.c +6 -0
@@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size)
 }
 EXPORT_SYMBOL(__asan_unpoison_stack_memory);
 
+/* Emitted by compiler to poison alloca()ed objects. */
+void __asan_alloca_poison(unsigned long addr, size_t size)
+{
+       size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
+       size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
+                       round_up(size, KASAN_SHADOW_SCALE_SIZE);

Perhaps s/round_up(size, KASAN_SHADOW_SCALE_SIZE)/rounded_up_size/ because we already calculated that.

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 814

Quoted 4 lines
mm/kasan/kasan.c +4 -0
+
+       const void *left_redzone = (const void *)(addr -
+                       KASAN_ALLOCA_REDZONE_SIZE);
+       const void *right_redzone = (const void *)(addr + rounded_up_size);

Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's the expectation, right? That can change is clang silently.

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 820

Quoted 5 lines
mm/kasan/kasan.c +5 -0
+       kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_LEFT);
+       kasan_poison_shadow(right_redzone,
+                       padding_size + KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_RIGHT);

We also need to poison the unaligned part at the end of the object from size to rounded_up_size. You can see how we do it for heap objects.

Quoted 46 lines
mm/kasan/kasan.c +9 -0
+}
+EXPORT_SYMBOL(__asan_alloca_poison);
+/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */
+void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
+{
+       kasan_unpoison_shadow(stack_top, stack_bottom - stack_top);
+}
+EXPORT_SYMBOL(__asan_allocas_unpoison);
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 static int kasan_mem_notifier(struct notifier_block *nb,
                        unsigned long action, void *data)
mm/kasan/kasan.h +8 -0
index 1229298cce64..b857dc70d6a2 100644
@@ -23,6 +23,14 @@
 #define KASAN_STACK_PARTIAL     0xF4
 #define KASAN_USE_AFTER_SCOPE   0xF8
 
+/*
+ * alloca redzone shadow values
+ */
+#define KASAN_ALLOCA_LEFT      0xCA
+#define KASAN_ALLOCA_RIGHT     0xCB
+
+#define KASAN_ALLOCA_REDZONE_SIZE      32
+
 /* Don't break randconfig/all*config builds */
 #ifndef KASAN_ABI_VERSION
 #define KASAN_ABI_VERSION 1
mm/kasan/report.c +3 -1
index beee0e980e2d..c6a5b7ab9e3a 100644
@@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info)
                break;
        case KASAN_USE_AFTER_SCOPE:
                bug_type = "use-after-scope";
+       case KASAN_ALLOCA_LEFT:
+       case KASAN_ALLOCA_RIGHT:
+               bug_type = "alloca-out-of-bounds";
                break;
        }
 
--
Quoted 2 lines

2.13.2.725.g09c95d1e9-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

Hi,

Thanks for taking a look at this patchstack. I apologize for the delay in responding.

Quoted 14 lines · Dmitry Vyukov

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 814

Quoted 4 lines
mm/kasan/kasan.c +4 -0
+
+       const void *left_redzone = (const void *)(addr -
+                       KASAN_ALLOCA_REDZONE_SIZE);
+       const void *right_redzone = (const void *)(addr + rounded_up_size);

Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's the expectation, right? That can change is clang silently.

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 820

Quoted 5 lines
mm/kasan/kasan.c +5 -0
+       kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_LEFT);
+       kasan_poison_shadow(right_redzone,
+                       padding_size + KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_RIGHT);
Quoted 3 lines

We also need to poison the unaligned part at the end of the object from size to rounded_up_size. You can see how we do it for heap objects.

The expectation is that size' is the exact size of the alloca()ed object. rounded_up_size' then adds the 0-7 bytes needed to adjust the size to the ASAN shadow scale. So `addr + rounded_up_size' should be the correct place to start poisoning.

In retrospect this part of the code was pretty confusing. How about this? I think its intent is clearer, plus it's a closer match for the description in my commit message:

	unsigned long left_redzone_start;
	unsigned long object_end;
	unsigned long right_redzone_start, right_redzone_end;

	left_redzone_start = addr - KASAN_ALLOCA_REDZONE_SIZE;
	kasan_poison_shadow((const void *)left_redzone_start,
			KASAN_ALLOCA_REDZONE_SIZE,
			KASAN_ALLOCA_LEFT);

	object_end = round_up(addr + size, KASAN_SHADOW_SCALE_SIZE);
	right_redzone_start = round_up(object_end, KASAN_ALLOCA_REDZONE_SIZE);
	right_redzone_end = right_redzone_start + KASAN_ALLOCA_REDZONE_SIZE;
	kasan_poison_shadow((const void *)object_end,
			right_redzone_end - object_end,
			KASAN_ALLOCA_RIGHT);
reviewer
#5 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

Quoted 29 lines · Greg Hackmann

Hi,

Thanks for taking a look at this patchstack. I apologize for the delay in responding.

Quoted 18 lines · Dmitry Vyukov
Quoted 6 lines
mm/kasan/kasan.c +4 -0
+
+       const void *left_redzone = (const void *)(addr -
+                       KASAN_ALLOCA_REDZONE_SIZE);
+       const void *right_redzone = (const void *)(addr +

rounded_up_size);

Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's the expectation, right? That can change is clang silently.

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 820

Quoted 5 lines
mm/kasan/kasan.c +5 -0
+       kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_LEFT);
+       kasan_poison_shadow(right_redzone,
+                       padding_size + KASAN_ALLOCA_REDZONE_SIZE,
+                       KASAN_ALLOCA_RIGHT);
Quoted 3 lines

We also need to poison the unaligned part at the end of the object from size to rounded_up_size. You can see how we do it for heap objects.

Quoted 4 lines

The expectation is that size' is the exact size of the alloca()ed object. rounded_up_size' then adds the 0-7 bytes needed to adjust the size to the ASAN shadow scale. So `addr + rounded_up_size' should be the correct place to start poisoning.

We need to start poisoning at addr+size exactly.
Asan shadow scheme supports this. It's not possible to poison beginning of an aligned 8-byte block, but leave tail unpoisoned. But it is possible to poison tail of an aligned 8-byte block and leave beginning unpoisoned. Look at what we do for kmalloc.

Quoted 20 lines

In retrospect this part of the code was pretty confusing. How about this? I think its intent is clearer, plus it's a closer match for the description in my commit message:

        unsigned long left_redzone_start;
        unsigned long object_end;
        unsigned long right_redzone_start, right_redzone_end;

        left_redzone_start = addr - KASAN_ALLOCA_REDZONE_SIZE;
        kasan_poison_shadow((const void *)left_redzone_start,
                        KASAN_ALLOCA_REDZONE_SIZE,
                        KASAN_ALLOCA_LEFT);

        object_end = round_up(addr + size, KASAN_SHADOW_SCALE_SIZE);
        right_redzone_start = round_up(object_end,

KASAN_ALLOCA_REDZONE_SIZE);
right_redzone_end = right_redzone_start + KASAN_ALLOCA_REDZONE_SIZE; kasan_poison_shadow((const void *)object_end,
right_redzone_end - object_end, KASAN_ALLOCA_RIGHT);

#6 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

In reply to Greg Hackmann
Quoted 3 lines · Greg Hackmann

clang's AddressSanitizer implementation adds redzones on either side of alloca()ed buffers. These redzones are 32-byte aligned and at least 32 bytes long.

gcc now supports this too. So I think this patch should enable it.
It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags to make it work

Quoted 8 lines

__asan_alloca_poison() is passed the size and address of the allocated buffer, *excluding* the redzones on either side. The left redzone will always be to the immediate left of this buffer; but AddressSanitizer may need to add padding between the end of the buffer and the right redzone. If there are any 8-byte chunks inside this padding, we should poison those too.

Quoted 6 lines

__asan_allocas_unpoison() is just passed the top and bottom of the dynamic stack area, so unpoisoning is simpler.

---
 lib/test_kasan.c  | 22 ++++++++++++++++++++++

Tests would be better as a separate patch.

Quoted 18 lines
 mm/kasan/kasan.c  | 26 ++++++++++++++++++++++++++
 mm/kasan/kasan.h  |  8 ++++++++
 mm/kasan/report.c |  3 +++
 4 files changed, 59 insertions(+)
lib/test_kasan.c +5 -0
index a25c9763fce1..f774fcafb696 100644
@@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void)
 	p[1023] = 1;
 }
 
+static noinline void __init kasan_alloca_oob_left(void)
+{
+	volatile int i = 10;
+	char alloca_array[i];
+	char *p = alloca_array - 1;

lib/test_kasan.c static noinline void __init use_after_scope_test(void) · line 490

Quoted 10 lines
lib/test_kasan.c +10 -0
+
+	pr_info("out-of-bounds to left on alloca\n");
+	*(volatile char *)p;
+}
+
+static noinline void __init kasan_alloca_oob_right(void)
+{
+	volatile int i = 10;
+	char alloca_array[i];
+	char *p = alloca_array + round_up(i, 8);

Why round_up() ?

Quoted 31 lines
lib/test_kasan.c +7 -0
+
+	pr_info("out-of-bounds to right on alloca\n");
+	*(volatile char *)p;
+}
+
 static int __init kmalloc_tests_init(void)
 {
 	/*
@@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void)
 	memcg_accounted_kmem_cache();
 	kasan_stack_oob();
 	kasan_global_oob();
+	kasan_alloca_oob_left();
+	kasan_alloca_oob_right();
 	ksize_unpoisons_memory();
 	copy_user_test();
 	use_after_scope_test();
mm/kasan/kasan.c +6 -0
index c81549d5c833..892b626f564b 100644
@@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size)
 }
 EXPORT_SYMBOL(__asan_unpoison_stack_memory);
 
+/* Emitted by compiler to poison alloca()ed objects. */
+void __asan_alloca_poison(unsigned long addr, size_t size)
+{
+	size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
+	size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) -
+			round_up(size, KASAN_SHADOW_SCALE_SIZE);

mm/kasan/kasan.c void __asan_unpoison_stack_memory(const void *addr, size_t size) · line 820

Quoted 10 lines
mm/kasan/kasan.c +10 -0
+
+	const void *left_redzone = (const void *)(addr -
+			KASAN_ALLOCA_REDZONE_SIZE);
+	const void *right_redzone = (const void *)(addr + rounded_up_size);
+
+	kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
+			KASAN_ALLOCA_LEFT);
+	kasan_poison_shadow(right_redzone,
+			padding_size + KASAN_ALLOCA_REDZONE_SIZE,
+			KASAN_ALLOCA_RIGHT);

As Dmitry pointed out, the memory between [addr+size, addr+rounded_up_size) is left unpoisoned. kasan_alloca_oob_right() without round_up() would have caught this.

#7 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

Quoted 3 lines · Andrey Ryabinin

gcc now supports this too. So I think this patch should enable it.
It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags to make it work

Thanks, will fix. For now, it looks like I'll need to build gcc from git to test this?

Quoted 3 lines
Quoted 1 line
  lib/test_kasan.c  | 22 ++++++++++++++++++++++

Tests would be better as a separate patch.

I was following the precedent in 828347f8f9a5 ("kasan: support use-after-scope detection") which added both at the same time. But I can split the test off into a separate patch if you feel really strongly about it.

#8 lore raw

Re: [PATCH 1/4] kasan: support alloca() poisoning

Quoted 7 lines · Greg Hackmann
Quoted 3 lines · Andrey Ryabinin

gcc now supports this too. So I think this patch should enable it.
It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags to make it work

Thanks, will fix. For now, it looks like I'll need to build gcc from git to test this?

Right, you'll need quite fresh revision >= 250032

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
  lib/test_kasan.c  | 22 ++++++++++++++++++++++

Tests would be better as a separate patch.

I was following the precedent in 828347f8f9a5 ("kasan: support use-after-scope detection") which added both at the same time. But I can split the test off into a separate patch if you feel really strongly about it.

Please, do the split.

#9 lore raw

PATCH 2/4 kasan: added functions for unpoisoning stack variables

In reply to Greg Hackmann

From: Alexander Potapenko <[email protected]>

As a code-size optimization, LLVM builds since r279383 may bulk-manipulate the shadow region when (un)poisoning large memory blocks. This requires new callbacks that simply do an uninstrumented memset().

This fixes linking the Clang-built kernel when using KASAN.

[[email protected]: fix memset() parameters, and tweak
commit message to describe new callbacks]

---
 mm/kasan/kasan.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
mm/kasan/kasan.c +15 -0
index 892b626f564b..89911e5c69f9 100644
@@ -828,6 +828,21 @@ void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
 }
 EXPORT_SYMBOL(__asan_allocas_unpoison);
 
+/* Emitted by the compiler to [un]poison local variables. */
+#define DEFINE_ASAN_SET_SHADOW(byte) \
+	void __asan_set_shadow_##byte(const void *addr, size_t size)	\
+	{								\
+		__memset((void *)addr, 0x##byte, size);			\
+	}								\
+	EXPORT_SYMBOL(__asan_set_shadow_##byte)
+
+DEFINE_ASAN_SET_SHADOW(00);
+DEFINE_ASAN_SET_SHADOW(f1);
+DEFINE_ASAN_SET_SHADOW(f2);
+DEFINE_ASAN_SET_SHADOW(f3);
+DEFINE_ASAN_SET_SHADOW(f5);
+DEFINE_ASAN_SET_SHADOW(f8);
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 static int kasan_mem_notifier(struct notifier_block *nb,
 			unsigned long action, void *data)
Signature · 7 lines
2.13.2.725.g09c95d1e9-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
reviewer Reviewed-by on v1 2/4
#10 lore raw

Re: [PATCH 2/4] kasan: added functions for unpoisoning stack variables

Quoted 33 lines · Greg Hackmann

From: Alexander Potapenko <[email protected]>

As a code-size optimization, LLVM builds since r279383 may bulk-manipulate the shadow region when (un)poisoning large memory blocks. This requires new callbacks that simply do an uninstrumented memset().

This fixes linking the Clang-built kernel when using KASAN.

[[email protected]: fix memset() parameters, and tweak
commit message to describe new callbacks]

---
 mm/kasan/kasan.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
mm/kasan/kasan.c +8 -0
index 892b626f564b..89911e5c69f9 100644
@@ -828,6 +828,21 @@ void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
 }
 EXPORT_SYMBOL(__asan_allocas_unpoison);
 
+/* Emitted by the compiler to [un]poison local variables. */
+#define DEFINE_ASAN_SET_SHADOW(byte) \
+       void __asan_set_shadow_##byte(const void *addr, size_t size)    \
+       {                                                               \
+               __memset((void *)addr, 0x##byte, size);                 \
+       }                                                               \
+       EXPORT_SYMBOL(__asan_set_shadow_##byte)
+

mm/kasan/kasan.c void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) · line 848

Quoted 10 lines
mm/kasan/kasan.c +7 -0
+DEFINE_ASAN_SET_SHADOW(00);
+DEFINE_ASAN_SET_SHADOW(f1);
+DEFINE_ASAN_SET_SHADOW(f2);
+DEFINE_ASAN_SET_SHADOW(f3);
+DEFINE_ASAN_SET_SHADOW(f5);
+DEFINE_ASAN_SET_SHADOW(f8);
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 static int kasan_mem_notifier(struct notifier_block *nb,
                        unsigned long action, void *data)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw

Re: [PATCH 2/4] kasan: added functions for unpoisoning stack variables

In reply to Greg Hackmann
Quoted 29 lines · Greg Hackmann

From: Alexander Potapenko <[email protected]>

As a code-size optimization, LLVM builds since r279383 may bulk-manipulate the shadow region when (un)poisoning large memory blocks. This requires new callbacks that simply do an uninstrumented memset().

This fixes linking the Clang-built kernel when using KASAN.

[[email protected]: fix memset() parameters, and tweak
commit message to describe new callbacks]

---
 mm/kasan/kasan.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
mm/kasan/kasan.c +4 -0
index 892b626f564b..89911e5c69f9 100644
@@ -828,6 +828,21 @@ void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
 }
 EXPORT_SYMBOL(__asan_allocas_unpoison);
 
+/* Emitted by the compiler to [un]poison local variables. */
+#define DEFINE_ASAN_SET_SHADOW(byte) \
+	void __asan_set_shadow_##byte(const void *addr, size_t size)	\
+	{								\

mm/kasan/kasan.c void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) · line 844

Quoted 10 lines
mm/kasan/kasan.c +10 -0
+		__memset((void *)addr, 0x##byte, size);			\
+	}								\
+	EXPORT_SYMBOL(__asan_set_shadow_##byte)
+
+DEFINE_ASAN_SET_SHADOW(00);
+DEFINE_ASAN_SET_SHADOW(f1);
+DEFINE_ASAN_SET_SHADOW(f2);
+DEFINE_ASAN_SET_SHADOW(f3);
+DEFINE_ASAN_SET_SHADOW(f5);
+DEFINE_ASAN_SET_SHADOW(f8);

I think we can remove f8 as it should be used only by use-after-return instrumentation. We don't use it in the kernel

Quoted 5 lines
mm/kasan/kasan.c +1 -0
+
 #ifdef CONFIG_MEMORY_HOTPLUG
 static int kasan_mem_notifier(struct notifier_block *nb,
 			unsigned long action, void *data)
#12 lore raw

PATCH 3/4 kasan: support LLVM-style asan parameters

In reply to Greg Hackmann

Use cc-option to figure out whether the compiler's sanitizer uses LLVM-style parameters ("-mllvm -asan-foo=bar") or GCC-style parameters ("--param asan-foo=bar").

---
 scripts/Makefile.kasan | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
scripts/Makefile.kasan +9 -1
index 9576775a86f6..b66ae4b4546b 100644
@@ -9,11 +9,19 @@ KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
 
 CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
-CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
+CFLAGS_KASAN_GCC := $(call cc-option, -fsanitize=kernel-address \
 		-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET) \
 		--param asan-stack=1 --param asan-globals=1 \
 		--param asan-instrumentation-with-call-threshold=$(call_threshold))
 
+CFLAGS_KASAN_LLVM := $(call cc-option, -fsanitize=kernel-address \
+		-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET) \
+		-mllvm -asan-stack=1 -mllvm -asan-globals=1 \
+		-mllvm -asan-use-after-scope=1 \
+		-mllvm -asan-instrumentation-with-call-threshold=$(call_threshold))
+
+CFLAGS_KASAN := $(CFLAGS_KASAN_GCC) $(CFLAGS_KASAN_LLVM)
+
 ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
    ifneq ($(CONFIG_COMPILE_TEST),y)
         $(warning Cannot use CONFIG_KASAN: \
Signature · 7 lines
2.13.2.725.g09c95d1e9-goog

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
reviewer Reviewed-by on v1 3/4
#13 lore raw

Re: [PATCH 3/4] kasan: support LLVM-style asan parameters

Quoted 24 lines · Greg Hackmann

Use cc-option to figure out whether the compiler's sanitizer uses LLVM-style parameters ("-mllvm -asan-foo=bar") or GCC-style parameters ("--param asan-foo=bar").

---
 scripts/Makefile.kasan | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
scripts/Makefile.kasan +2 -1
index 9576775a86f6..b66ae4b4546b 100644
@@ -9,11 +9,19 @@ KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
 
 CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
-CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
+CFLAGS_KASAN_GCC := $(call cc-option, -fsanitize=kernel-address \
                -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET) \
                --param asan-stack=1 --param asan-globals=1 \
                --param asan-instrumentation-with-call-threshold=$(call_threshold))
 
+CFLAGS_KASAN_LLVM := $(call cc-option, -fsanitize=kernel-address \

scripts/Makefile.kasan KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET) · line 27

Quoted 10 lines
scripts/Makefile.kasan +7 -0
+               -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET) \
+               -mllvm -asan-stack=1 -mllvm -asan-globals=1 \
+               -mllvm -asan-use-after-scope=1 \
+               -mllvm -asan-instrumentation-with-call-threshold=$(call_threshold))
+
+CFLAGS_KASAN := $(CFLAGS_KASAN_GCC) $(CFLAGS_KASAN_LLVM)
+
 ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
    ifneq ($(CONFIG_COMPILE_TEST),y)
         $(warning Cannot use CONFIG_KASAN: \

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

PATCH 4/4 kasan: add compiler support for clang

In reply to Greg Hackmann

For now we can hard-code ASAN ABI level 5, since historical clang builds can't build the kernel anyway. We also need to emulate gcc's __SANITIZE_ADDRESS__ flag, or memset() calls won't be instrumented.

---
 include/linux/compiler-clang.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/compiler-clang.h +10 -0
index d614c5ea1b5e..8153f793b22a 100644
@@ -23,3 +23,13 @@
  */
 #undef inline
 #define inline inline __attribute__((unused)) notrace
+
+/* all clang versions usable with the kernel support KASAN ABI version 5
+ */
+#define KASAN_ABI_VERSION 5
+
+/* emulate gcc's __SANITIZE_ADDRESS__ flag
+ */
+#if __has_feature(address_sanitizer)
+#define __SANITIZE_ADDRESS__
+#endif
Signature · 1 line
2.13.2.725.g09c95d1e9-goog
reviewer Reviewed-by on v1 4/4
#15 lore raw

Re: [PATCH 4/4] kasan: add compiler support for clang

Quoted 17 lines · Greg Hackmann

For now we can hard-code ASAN ABI level 5, since historical clang builds can't build the kernel anyway. We also need to emulate gcc's __SANITIZE_ADDRESS__ flag, or memset() calls won't be instrumented.

---
 include/linux/compiler-clang.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/compiler-clang.h +0 -0
index d614c5ea1b5e..8153f793b22a 100644
@@ -23,3 +23,13 @@
  */
 #undef inline
 #define inline inline __attribute__((unused)) notrace

include/linux/compiler-clang.h line 35

Quoted 10 lines
include/linux/compiler-clang.h +10 -0
+
+/* all clang versions usable with the kernel support KASAN ABI version 5
+ */
+#define KASAN_ABI_VERSION 5
+
+/* emulate gcc's __SANITIZE_ADDRESS__ flag
+ */
+#if __has_feature(address_sanitizer)
+#define __SANITIZE_ADDRESS__
+#endif

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [PATCH 4/4] kasan: add compiler support for clang

In reply to Greg Hackmann
Quoted 14 lines · Greg Hackmann

For now we can hard-code ASAN ABI level 5, since historical clang builds can't build the kernel anyway. We also need to emulate gcc's __SANITIZE_ADDRESS__ flag, or memset() calls won't be instrumented.

---
 include/linux/compiler-clang.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/compiler-clang.h +0 -0
index d614c5ea1b5e..8153f793b22a 100644

include/linux/compiler-clang.h line 28

Quoted 6 lines
include/linux/compiler-clang.h +3 -0
@@ -23,3 +23,13 @@
  */
 #undef inline
 #define inline inline __attribute__((unused)) notrace
+
+/* all clang versions usable with the kernel support KASAN ABI version 5
+ */

Enclosing */ should be on the same line for single-line comments.

include/linux/compiler-clang.h line 32

Quoted 4 lines
include/linux/compiler-clang.h +4 -0
+#define KASAN_ABI_VERSION 5
+
+/* emulate gcc's __SANITIZE_ADDRESS__ flag
+ */

Ditto.

Quoted 4 lines
include/linux/compiler-clang.h +3 -0
+#if __has_feature(address_sanitizer)
+#define __SANITIZE_ADDRESS__
+#endif