Skip to contentSkip to conversation

kasan: hw_tags: Add option to tag only at allocation time

RFC PATCH In review Dev Jain 6 participants 22 messages Jun 12 – Jun 24

Subsystems (4)
lore
Share & tools
lore

Dev Jain

author
#0 lore raw

cover kasan: hw_tags: Add option to tag only at allocation time

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/2 kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning

In preparation for adding "tag only on alloc" boot time option, use KASAN_PAGE_REDZONE instead of KASAN_TAG_INVALID for poisoning the tail end of the vmalloc allocation.

Although both values are the same for hw tags, KASAN_SLAB_REDZONE is used for poisoning the tail end of a kmalloc object allocation, so maintain the pattern.

---
 mm/kasan/hw_tags.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/kasan/hw_tags.c +1 -1
index cbef5e450954e..c1a2b48808ed7 100644
@@ -375,7 +375,7 @@ void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
 	redzone_start = round_up((unsigned long)start + size,
 				 KASAN_GRANULE_SIZE);
 	redzone_size = round_up(redzone_start, PAGE_SIZE) - redzone_start;
-	kasan_poison((void *)redzone_start, redzone_size, KASAN_TAG_INVALID,
+	kasan_poison((void *)redzone_start, redzone_size, KASAN_PAGE_REDZONE,
 		     flags & KASAN_VMALLOC_INIT);
 
 	/*
Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 2/2 kasan: hw_tags: Add boot option to elide free time poisoning

In reply to Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Keep this as a boot time feature to prevent building two kernel images.

To implement the feature, we need to effectively render kasan_poison() redundant for hw tags case, but keep it working in the case where it is used not in an object-freeing code path, but the redzoning path (which means, poisoning the tail end of a vmalloc or kmalloc allocation).

We achieve this by overloading the poison values for the hw tags case: we define the four poison values as 0x0E, 0x1E, 0x2E, 0x3E. In kasan_poison(), if we arrive with KASAN_SLAB_REDZONE or KASAN_PAGE_REDZONE, do a bitwise OR on the value of the tag to make it equal to KASAN_TAG_INVALID.

If not, then, if init is true, zero out the memory and bail out.

---
 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 43 ++++++++++++++++++++++++++++++-
 mm/kasan/kasan.h                  | 23 ++++++++++++++++-
 3 files changed, 68 insertions(+), 2 deletions(-)
Documentation/dev-tools/kasan.rst +4 -0
index 4968b2aa60c80..b0c30584b5062 100644
@@ -146,6 +146,10 @@ disabling KASAN altogether or controlling its features:
 - ``kasan.vmalloc=off`` or ``=on`` disables or enables tagging of vmalloc
   allocations (default: ``on``).
 
+- ``kasan.tag_only_on_alloc=off`` or ``=on`` disables or enables skipping
+  free-time tagging (poisoning) while keeping allocation-time tagging enabled
+  (default: ``off``).
+
 - ``kasan.page_alloc.sample=<sampling interval>`` makes KASAN tag only every
   Nth page_alloc allocation with the order equal or greater than
   ``kasan.page_alloc.sample.order``, where N is the value of the ``sample``
mm/kasan/hw_tags.c +42 -1

77 lines not on this page · raw message

mm/kasan/kasan.h +22 -1
index fc9169a547662..4fa8abb312faa 100644
@@ -33,6 +33,7 @@ static inline bool kasan_stack_collection_enabled(void)
 #include "../slab.h"
 
 DECLARE_STATIC_KEY_TRUE(kasan_flag_vmalloc);
+DECLARE_STATIC_KEY_FALSE(kasan_flag_tag_only_on_alloc);
 
 enum kasan_mode {
 	KASAN_MODE_SYNC,
@@ -52,6 +53,11 @@ static inline bool kasan_vmalloc_enabled(void)
 	return static_branch_likely(&kasan_flag_vmalloc);
 }
 
+static inline bool kasan_tag_only_on_alloc_enabled(void)
+{
+	return static_branch_unlikely(&kasan_flag_tag_only_on_alloc);
+}
+
 static inline bool kasan_async_fault_possible(void)
 {
 	return kasan_mode == KASAN_MODE_ASYNC || kasan_mode == KASAN_MODE_ASYMM;
@@ -145,12 +151,17 @@ static inline bool kasan_requires_meta(void)
 #define KASAN_SLAB_REDZONE	0xFC  /* redzone for slab object */
 #define KASAN_SLAB_FREE		0xFB  /* freed slab object */
 #define KASAN_VMALLOC_INVALID	0xF8  /* inaccessible space in vmap area */
+#elif defined(CONFIG_KASAN_HW_TAGS)
+#define KASAN_PAGE_FREE		0x0E
+#define KASAN_PAGE_REDZONE	0x1E
+#define KASAN_SLAB_REDZONE	0x2E
+#define KASAN_SLAB_FREE		0x3E
 #else
 #define KASAN_PAGE_FREE		KASAN_TAG_INVALID
 #define KASAN_PAGE_REDZONE	KASAN_TAG_INVALID
 #define KASAN_SLAB_REDZONE	KASAN_TAG_INVALID
 #define KASAN_SLAB_FREE		KASAN_TAG_INVALID
-#define KASAN_VMALLOC_INVALID	KASAN_TAG_INVALID /* only used for SW_TAGS */
+#define KASAN_VMALLOC_INVALID	KASAN_TAG_INVALID
 #endif
 
 #ifdef CONFIG_KASAN_GENERIC
@@ -478,6 +489,16 @@ static inline u8 kasan_random_tag(void) { return 0; }
 
 static inline void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {
+	if (kasan_tag_only_on_alloc_enabled()) {
+		if ((value != KASAN_SLAB_REDZONE) && (value != KASAN_PAGE_REDZONE)) {
+			if (init)
+				memset((void *)kasan_reset_tag(addr), 0, size);
+			return;
+		}
+	}
+
+	value |= 0xF0;
+
 	if (WARN_ON((unsigned long)addr & KASAN_GRANULE_MASK))
 		return;
 	if (WARN_ON(size & KASAN_GRANULE_MASK))
Signature · 1 line
2.43.0
#5 lore raw
In reply to Dev Jain
Quoted 19 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Quoted 11 lines

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0

I tested out this series on one of our devices that has MTE support, and didn't see any functional issues.

One thing I did notice though, and it's independent of this patch, is that the vmalloc_oob is failing, but that happens even if these patches aren't present.

Thanks,
Isaac

#3 lore raw

Re: [RFC PATCH 2/2] kasan: hw_tags: Add boot option to elide free time poisoning

In reply to Dev Jain
Quoted 9 lines · Dev Jain
mm/kasan/kasan.h +0 -0
index fc9169a547662..4fa8abb312faa 100644
 #ifdef CONFIG_KASAN_GENERIC
@@ -478,6 +489,16 @@ static inline u8 kasan_random_tag(void) { return 0; }
 
 static inline void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {

mm/kasan/kasan.h static inline u8 kasan_random_tag(void) { return 0; } · line 501

Quoted 10 lines
mm/kasan/kasan.h +10 -0
+	if (kasan_tag_only_on_alloc_enabled()) {
+		if ((value != KASAN_SLAB_REDZONE) && (value != KASAN_PAGE_REDZONE)) {
+			if (init)
+				memset((void *)kasan_reset_tag(addr), 0, size);
+			return;
+		}
+	}
+
+	value |= 0xF0;
+

I wonder if it would make more sense to have this as:

if (kasan_tag_only_on_alloc_enabled() && (value == KASAN_SLAB_FREE ||

    value == KASAN_PAGE_FREE)) {
	if (init)
		memset((void *)kasan_reset_tag(addr), 0, size);
	return;
}

That seems a bit clearer to me as to what it is that you're doing, and also makes it so that you don't have to do any bit manipulation on the value when you're filling in the redzones.

Thanks,
Isaac

#7 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Hmm ... do we also need to teach the KASAN KUnit tests about this mode?

With kasan.tag_only_on_alloc=on, free-time poisoning is skipped, so some UAF and double-free reports are skipped on purpose, but the tests still expect them :)

Cheers, Lance

#9 lore raw
In reply to Dev Jain

Hi Dev,

Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

Quoted 7 lines

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

Quoted 16 lines

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
Signature · 2 lines
Cheers,
Harry / Hyeonggon
#20 lore raw
In reply to Dev Jain
Quoted 11 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Does standard KASAN with HW_TAGS really detect double-free? How does it do that? I could imagine it testing the tags of memory being freed to see if they are set to the poison tag, but that would lead to false positives for the GFP_SKIP_KASAN case, surely?

If I'm right, then the only downgrade this new mode causes is that if freed-but-not-yet-reallocated memory is accessed via it's dangling pointer, then that bad access is not detected. I think that would be benign in all the cases I can think of, so while it would be a problem for a debugging use case, it would unlikely be a problem for security enforcement?

Thanks,
Ryan

Quoted 17 lines

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
#10 lore raw
In reply to Harry Yoo
Quoted 11 lines · Harry Yoo

Hi Dev,

Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

Is there any reason not to use STGM instead of STG + DC GVA when setting/clearing tags for large sizes when we know they are properly aligned?

Quoted 32 lines
Quoted 7 lines

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

Quoted 16 lines

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
Signature · 2 lines
Cheers,
Harry / Hyeonggon
author
#4 lore raw

Re: [RFC PATCH 2/2] kasan: hw_tags: Add boot option to elide free time poisoning

In reply to Isaac Manjarres
Quoted 30 lines · Isaac Manjarres
Quoted 9 lines · Dev Jain
mm/kasan/kasan.h +0 -0
index fc9169a547662..4fa8abb312faa 100644
 #ifdef CONFIG_KASAN_GENERIC
@@ -478,6 +489,16 @@ static inline u8 kasan_random_tag(void) { return 0; }
 
 static inline void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {

mm/kasan/kasan.h static inline u8 kasan_random_tag(void) { return 0; } · line 501

Quoted 10 lines
mm/kasan/kasan.h +10 -0
+	if (kasan_tag_only_on_alloc_enabled()) {
+		if ((value != KASAN_SLAB_REDZONE) && (value != KASAN_PAGE_REDZONE)) {
+			if (init)
+				memset((void *)kasan_reset_tag(addr), 0, size);
+			return;
+		}
+	}
+
+	value |= 0xF0;
+

I wonder if it would make more sense to have this as:

if (kasan_tag_only_on_alloc_enabled() && (value == KASAN_SLAB_FREE ||

    value == KASAN_PAGE_FREE)) {
	if (init)
		memset((void *)kasan_reset_tag(addr), 0, size);
	return;
}
Quoted 3 lines

That seems a bit clearer to me as to what it is that you're doing, and also makes it so that you don't have to do any bit manipulation on the value when you're filling in the redzones.

Ah so you mean, we can define KASAN_SLAB_FREE and KASAN_PAGE_FREE to be different values, leaving KASAN_SLAB_REDZONE and KASAN_PAGE_REDZONE to be 0xFE, the poison value. Yep I'll do that.

Quoted 3 lines

Thanks,
Isaac

author
#8 lore raw
In reply to Lance Yang
Quoted 11 lines · Lance Yang
Quoted 9 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Quoted 8 lines
Quoted 2 lines

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Hmm ... do we also need to teach the KASAN KUnit tests about this mode?

With kasan.tag_only_on_alloc=on, free-time poisoning is skipped, so some UAF and double-free reports are skipped on purpose, but the tests still expect them :)

Yeah my opinion is that we shouldn't bother - but if we go ahead with this patch in some shape or form then I'll see how to make kasan_test work with this.

Quoted 3 lines

Cheers, Lance

author
#21 lore raw
In reply to Ryan Roberts
Quoted 10 lines · Ryan Roberts
Quoted 9 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Quoted 7 lines
Quoted 2 lines

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Does standard KASAN with HW_TAGS really detect double-free? How does it do that? I could imagine it testing the tags of memory being freed to see if they are set to the poison tag, but that would lead to false positives for the GFP_SKIP_KASAN case, surely?

Should have mentioned, the double-free check is only for slab objects, see __kasan_slab_pre_free. So we won't be able to catch double-free here.

Quoted 6 lines

If I'm right, then the only downgrade this new mode causes is that if freed-but-not-yet-reallocated memory is accessed via it's dangling pointer, then that bad access is not detected. I think that would be benign in all the cases I can think of, so while it would be a problem for a debugging use case, it would unlikely be a problem for security enforcement?

Okay so you are saying that we won't catch the bug, but there is no security problem because the dangling pointer is accessing memory which isn't in use by anyone else.

Quoted 23 lines

Thanks,
Ryan

Quoted 17 lines

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
#11 lore raw
In reply to Harry Yoo
Quoted 6 lines · Harry Yoo
Quoted 5 lines · Harry Yoo
Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Quoted 7 lines
Quoted 3 lines

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

Is there any reason not to use STGM instead of STG + DC GVA when setting/clearing tags for large sizes when we know they are properly aligned?

STGM is intended for copying tags when paired with LDGM. Have you seen hardware where STGM is faster than STG or DC GVA? For properly aligned buffers, I'd expect DC GVA to behave at least on par with STGM.

Signature · 1 line
Catalin
#13 lore raw
In reply to Harry Yoo
Quoted 8 lines · Harry Yoo
Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

I guess it depends on the workload. Lots of allocations for short-lived buffers (e.g. network traffic) may notice the additional tagging more than the actual tag checking.

Of course, it would be nice to get some numbers from those who have access to MTE capable hardware.

Quoted 8 lines
Quoted 7 lines

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free. The buffer will be re-tagged later so use-after-realloc should be caught, especially if we ensure that a different tag will be used (I don't think Dev's patches do this).

Of course, if you want MTE as a debug/bug-finding feature, tagging on both allocation and freeing is highly recommended. This patchset is aimed for those wanting to run MTE in production and squeeze a bit more performance out of it (with the compromise of not detecting use-after-free, only prevent access after re-allocation).

Signature · 1 line
Catalin
author
#6 lore raw
In reply to Isaac Manjarres
Quoted 29 lines · Isaac Manjarres
Quoted 28 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

This is an RFC because we are not clear about the performance benefit.

Android folks, please help with testing!

---
Applies on Linus master (9716c086c8e8).

Dev Jain (2):
kasan: hw_tags: Use KASAN_PAGE_REDZONE for vmalloc redzoning
kasan: hw_tags: Add boot option to elide free time poisoning

 Documentation/dev-tools/kasan.rst |  4 +++
 mm/kasan/hw_tags.c                | 45 +++++++++++++++++++++++++++++--
 mm/kasan/kasan.h                  | 23 +++++++++++++++-
 3 files changed, 69 insertions(+), 3 deletions(-)
Quoted 5 lines
Quoted 2 lines
Signature · 1 line
2.43.0

I tested out this series on one of our devices that has MTE support, and didn't see any functional issues.

Thanks for testing Isaac! Do you have the bandwidth to carry out some performance tests? We want to see whether this patchset gets perf boost on some HW.

Quoted 7 lines

One thing I did notice though, and it's independent of this patch, is that the vmalloc_oob is failing, but that happens even if these patches aren't present.

Thanks,
Isaac

#14 lore raw
In reply to Catalin Marinas

Hi Catalin,

Quoted 6 lines · Catalin Marinas
Quoted 5 lines · Harry Yoo
Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

Quoted 7 lines
Quoted 3 lines

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

I guess it depends on the workload. Lots of allocations for short-lived buffers (e.g. network traffic) may notice the additional tagging more than the actual tag checking.

Agreed. Likely depends on lifetime and size of objects.

Quoted 2 lines

Of course, it would be nice to get some numbers from those who have access to MTE capable hardware.

Agreed! (I don't have one, unfortunately. It's pretty new hardware feature)

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 8 lines
Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free.

I think it depends... OH, WAIT! I see what you mean.

You mean use-after-free before reallocation does not lead to much compromise on security because objects are initialized after allocation?

You're probably right.

Hmm, but stores to e.g.) free pointer, fields initialized by constructor or accessed by SLAB_TYPESAFE_BY_RCU semantics after free will be undiscovered if they happen before reallocation.

Not sure what are security implications of that,
but sounds worth discussing.

Quoted 3 lines

The buffer will be re-tagged later so use-after-realloc should be caught, especially if we ensure that a different tag will be used (I don't think Dev's patches do this).

Agreed that it'll be nice to ensure that.

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#12 lore raw
In reply to Catalin Marinas
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Harry Yoo
Quoted 8 lines · Harry Yoo
Quoted 3 lines · Dev Jain

Introduce a boot option to tag only at allocation time of the objects. This reduces KASAN MTE overhead, the tradeoff being reduced ability of catching bugs.

I think most of overhead when enabling MTE comes from loading and validing tags for every memory access (either in SYNC or ASYNC mode), rather than from storing tags.

Quoted 6 lines
Quoted 3 lines

Is there any reason not to use STGM instead of STG + DC GVA when setting/clearing tags for large sizes when we know they are properly aligned?

STGM is intended for copying tags when paired with LDGM. Have you seen hardware where STGM is faster than STG or DC GVA?

No, I haven't. It was a question I had after learning that there are multiple ways to store tags ;)

Quoted 2 lines

For properly aligned
buffers, I'd expect DC GVA to behave at least on par with STGM.

Thanks for answering!

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#15 lore raw
In reply to Harry Yoo

Hi Harry,

Quoted 22 lines · Harry Yoo
Quoted 10 lines · Catalin Marinas
Quoted 9 lines · Harry Yoo
Quoted 7 lines · Dev Jain

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 8 lines
Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free.

I think it depends... OH, WAIT! I see what you mean.

Quoted 8 lines

You mean use-after-free before reallocation does not lead to much compromise on security because objects are initialized after allocation?

You're probably right.

Hmm, but stores to e.g.) free pointer, fields initialized by constructor or accessed by SLAB_TYPESAFE_BY_RCU semantics after free will be undiscovered if they happen before reallocation.

Even with SLAB_TYPESAFE_BY_RCU, the object isn't tagged on free either (or realloc, only if the actual slab page ends up freed). But we don't get type confusion for such slab.

However, without tagging on free, one could argue that it reduces security for cases where the page is re-allocated as untagged - e.g. all user pages mapped without PROT_MTE. Currently we have a deterministic tag check fault if the page is coloured as KASAN_TAG_INVALID. I think for this patch, it might be better to only do such skip on free in kasan_poison_slab() rather than kasan_poison(). Freed pages would then be tagged.

An alternative would be tagging on free only with a new tag and skipping it on re-alloc. But we'd need to track when it's a completely new allocation or a reused object (I haven't looked I'm pretty sure it's doable).

Signature · 1 line
Catalin
author
#16 lore raw
Quoted 34 lines · Catalin Marinas

Hi Harry,

Quoted 22 lines · Harry Yoo
Quoted 10 lines · Catalin Marinas
Quoted 9 lines · Harry Yoo
Quoted 7 lines · Dev Jain

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 8 lines
Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free.

I think it depends... OH, WAIT! I see what you mean.

Quoted 8 lines

You mean use-after-free before reallocation does not lead to much compromise on security because objects are initialized after allocation?

You're probably right.

Hmm, but stores to e.g.) free pointer, fields initialized by constructor or accessed by SLAB_TYPESAFE_BY_RCU semantics after free will be undiscovered if they happen before reallocation.

Quoted 8 lines

Even with SLAB_TYPESAFE_BY_RCU, the object isn't tagged on free either (or realloc, only if the actual slab page ends up freed). But we don't get type confusion for such slab.

However, without tagging on free, one could argue that it reduces security for cases where the page is re-allocated as untagged - e.g. all user pages mapped without PROT_MTE. Currently we have a deterministic tag check fault if the page is coloured as KASAN_TAG_INVALID. I think

So you are saying that a stale kernel pointer can continue to use the reallocated page, because for non-PROT_MTE case the page does not get a new tag. Makes sense.

Quoted 3 lines

for this patch, it might be better to only do such skip on free in kasan_poison_slab() rather than kasan_poison(). Freed pages would then be tagged.

I think you mean to say, "skip tag on free when freeing pages into buddy"? So that would mean, kasan_poison() will do the poisoning also in the case of value == KASAN_PAGE_FREE.

Quoted 5 lines

An alternative would be tagging on free only with a new tag and skipping it on re-alloc. But we'd need to track when it's a completely new allocation or a reused object (I haven't looked I'm pretty sure it's doable).

That was our original approach, and IIRC we had concluded there was no security compromise. However it is difficult to implement - it has cases like, what happens when two differently tagged pages are coalesced by buddy and someone gets that large page as an allocation.

Quoted 1 line
#17 lore raw
Quoted 37 lines · Dev Jain
Quoted 36 lines · Catalin Marinas
Quoted 22 lines · Harry Yoo
Quoted 10 lines · Catalin Marinas
Quoted 9 lines · Harry Yoo
Quoted 7 lines · Dev Jain

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 8 lines
Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free.

I think it depends... OH, WAIT! I see what you mean.

Quoted 8 lines

You mean use-after-free before reallocation does not lead to much compromise on security because objects are initialized after allocation?

You're probably right.

Hmm, but stores to e.g.) free pointer, fields initialized by constructor or accessed by SLAB_TYPESAFE_BY_RCU semantics after free will be undiscovered if they happen before reallocation.

Even with SLAB_TYPESAFE_BY_RCU, the object isn't tagged on free either (or realloc, only if the actual slab page ends up freed). But we don't get type confusion for such slab.

Quoted 8 lines
Quoted 4 lines

However, without tagging on free, one could argue that it reduces security for cases where the page is re-allocated as untagged - e.g. all user pages mapped without PROT_MTE. Currently we have a deterministic tag check fault if the page is coloured as KASAN_TAG_INVALID. I think

So you are saying that a stale kernel pointer can continue to use the reallocated page, because for non-PROT_MTE case the page does not get a new tag. Makes sense.

Yes.

Quoted 5 lines
Quoted 3 lines

for this patch, it might be better to only do such skip on free in kasan_poison_slab() rather than kasan_poison(). Freed pages would then be tagged.

I think you mean to say, "skip tag on free when freeing pages into buddy"?

No, I meant always poison via kasan_poison_pages(), as we currently do with KASAN_PAGE_FREE being set.

Quoted 8 lines

So that would mean, kasan_poison() will do the poisoning also in the case of value == KASAN_PAGE_FREE.

Quoted 4 lines

An alternative would be tagging on free only with a new tag and skipping it on re-alloc. But we'd need to track when it's a completely new allocation or a reused object (I haven't looked I'm pretty sure it's doable).

Quoted 4 lines

That was our original approach, and IIRC we had concluded there was no security compromise. However it is difficult to implement - it has cases like, what happens when two differently tagged pages are coalesced by buddy and someone gets that large page as an allocation.

Yeah, it's fairly complex.

I think the more problematic case is when we can't detect use-after-reallocation and this happens when a page is reused untagged (probabilistically, also when the page is reused with the old tag). As an optimisation, it might be sufficient to skip poisoning when freeing an object into the slab but keep the poisoning when freeing a slab page into the buddy allocator. That's where the page may end up in a place untagged.

Also for your optimisation to only tag on reallocation, do you have any code to read the current tag and avoid reusing it? That's useful for kmalloc caches or other merged kmem caches where we can have type confusion.

Signature · 1 line
Catalin
author
#18 lore raw
Quoted 49 lines · Catalin Marinas
Quoted 37 lines · Dev Jain
Quoted 36 lines · Catalin Marinas
Quoted 22 lines · Harry Yoo
Quoted 10 lines · Catalin Marinas
Quoted 9 lines · Harry Yoo
Quoted 7 lines · Dev Jain

Now, when a memory object will be freed, it will retain the random tag it had at allocation time. This compromises on catching UAF bugs, till the time the object is not reallocated, at which point it will have a new random tag.

Hence, not catching "use-after-free-before-reallocation" and not catching "double-free" will be the compromise for reduced KASAN overhead.

Quoted 8 lines
Quoted 6 lines

I doubt users who care about security enough to enable HW_TAGS KASAN are willing to compromise on security just to save a few instructions to store tags in the free path.

To me, it looks like too much of a compromise on security for little performance gain.

I don't think there's much compromise on security for use-after-free.

I think it depends... OH, WAIT! I see what you mean.

Quoted 8 lines

You mean use-after-free before reallocation does not lead to much compromise on security because objects are initialized after allocation?

You're probably right.

Hmm, but stores to e.g.) free pointer, fields initialized by constructor or accessed by SLAB_TYPESAFE_BY_RCU semantics after free will be undiscovered if they happen before reallocation.

Even with SLAB_TYPESAFE_BY_RCU, the object isn't tagged on free either (or realloc, only if the actual slab page ends up freed). But we don't get type confusion for such slab.

Quoted 8 lines
Quoted 4 lines

However, without tagging on free, one could argue that it reduces security for cases where the page is re-allocated as untagged - e.g. all user pages mapped without PROT_MTE. Currently we have a deterministic tag check fault if the page is coloured as KASAN_TAG_INVALID. I think

So you are saying that a stale kernel pointer can continue to use the reallocated page, because for non-PROT_MTE case the page does not get a new tag. Makes sense.

Yes.

Quoted 11 lines
Quoted 5 lines
Quoted 3 lines

for this patch, it might be better to only do such skip on free in kasan_poison_slab() rather than kasan_poison(). Freed pages would then be tagged.

I think you mean to say, "skip tag on free when freeing pages into buddy"?

No, I meant always poison via kasan_poison_pages(), as we currently do with KASAN_PAGE_FREE being set.

Quoted 2 lines

So that would mean, kasan_poison() will do the poisoning also in the case of value == KASAN_PAGE_FREE.

Yeah sorry I wrote two contradictory things above, that's what I meant too.

Quoted 21 lines
Quoted 10 lines
Quoted 5 lines

An alternative would be tagging on free only with a new tag and skipping it on re-alloc. But we'd need to track when it's a completely new allocation or a reused object (I haven't looked I'm pretty sure it's doable).

That was our original approach, and IIRC we had concluded there was no security compromise. However it is difficult to implement - it has cases like, what happens when two differently tagged pages are coalesced by buddy and someone gets that large page as an allocation.

Yeah, it's fairly complex.

I think the more problematic case is when we can't detect use-after-reallocation and this happens when a page is reused untagged (probabilistically, also when the page is reused with the old tag). As an optimisation, it might be sufficient to skip poisoning when freeing an object into the slab but keep the poisoning when freeing a slab page into the buddy allocator. That's where the page may end up in a place untagged.

Quoted 4 lines

Also for your optimisation to only tag on reallocation, do you have any code to read the current tag and avoid reusing it? That's useful for kmalloc caches or other merged kmem caches where we can have type confusion.

I don't have it, but should be fairly simple I guess. I just wanted to keep it simple for now.

Anyhow someone needs to first test the current patchset to get some numbers, we would be wasting time on this if no one gets an improvement.

Quoted 1 line
#19 lore raw
Quoted 2 lines · Dev Jain

Anyhow someone needs to first test the current patchset to get some numbers, we would be wasting time on this if no one gets an improvement.

I agree. Something like iperf3 would be interesting, lots of skb allocations.

Signature · 1 line
Catalin