Skip to contentSkip to conversation

kasan: add memory corruption identification for hw tag-based kasan

PATCH In review Kuan-Ying Lee 3 participants 9 messages Jun 12, 2021 – Jun 19, 2021

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

v2 cover kasan: add memory corruption identification for hw tag-based kasan

Add memory corruption identification for hardware tag-based KASAN mode.

Changes since v2:
- Thanks for Marco's Suggestion
- Rename the CONFIG_KASAN_SW_TAGS_IDENTIFY
- Integrate tag-based kasan common part
- Rebase to latest linux-next

Kuan-Ying Lee (3):
kasan: rename CONFIG_KASAN_SW_TAGS_IDENTIFY to
CONFIG_KASAN_TAGS_IDENTIFY
kasan: integrate the common part of two KASAN tag-based modes
kasan: add memory corruption identification support for hardware
tag-based mode

 lib/Kconfig.kasan         |  4 +--
 mm/kasan/Makefile         |  4 +--
 mm/kasan/hw_tags.c        | 22 ---------------
 mm/kasan/kasan.h          |  4 +--
 mm/kasan/report_hw_tags.c |  6 +---
 mm/kasan/report_sw_tags.c | 46 +------------------------------
 mm/kasan/report_tags.h    | 56 +++++++++++++++++++++++++++++++++++++
 mm/kasan/sw_tags.c        | 41 ---------------------------
 mm/kasan/tags.c           | 58 +++++++++++++++++++++++++++++++++++++++
 9 files changed, 122 insertions(+), 119 deletions(-)
 create mode 100644 mm/kasan/report_tags.h
 create mode 100644 mm/kasan/tags.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 kasan: rename CONFIG_KASAN_SW_TAGS_IDENTIFY to CONFIG_KASAN_TAGS_IDENTIFY

This patch renames CONFIG_KASAN_SW_TAGS_IDENTIFY to CONFIG_KASAN_TAGS_IDENTIFY in order to be compatible with hardware tag-based mode.

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 lib/Kconfig.kasan         | 2 +-
 mm/kasan/kasan.h          | 4 ++--
 mm/kasan/report_sw_tags.c | 2 +-
 mm/kasan/sw_tags.c        | 4 ++--
 4 files changed, 6 insertions(+), 6 deletions(-)
lib/Kconfig.kasan +1 -1
index cffc2ebbf185..6f5d48832139 100644
@@ -155,7 +155,7 @@ config KASAN_STACK
 	  CONFIG_COMPILE_TEST.	On gcc it is assumed to always be safe
 	  to use and enabled by default.
 
-config KASAN_SW_TAGS_IDENTIFY
+config KASAN_TAGS_IDENTIFY
 	bool "Enable memory corruption identification"
 	depends on KASAN_SW_TAGS
 	help
mm/kasan/kasan.h +2 -2
index 8f450bc28045..b0fc9a1eb7e3 100644
@@ -153,7 +153,7 @@ struct kasan_track {
 	depot_stack_handle_t stack;
 };
 
-#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
 #define KASAN_NR_FREE_STACKS 5
 #else
 #define KASAN_NR_FREE_STACKS 1
@@ -170,7 +170,7 @@ struct kasan_alloc_meta {
 #else
 	struct kasan_track free_track[KASAN_NR_FREE_STACKS];
 #endif
-#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
 	u8 free_pointer_tag[KASAN_NR_FREE_STACKS];
 	u8 free_track_idx;
 #endif
mm/kasan/report_sw_tags.c +1 -1
index 3d20d3451d9e..821a14a19a92 100644
@@ -31,7 +31,7 @@
 
 const char *kasan_get_bug_type(struct kasan_access_info *info)
 {
-#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
 	struct kasan_alloc_meta *alloc_meta;
 	struct kmem_cache *cache;
 	struct page *page;
mm/kasan/sw_tags.c +2 -2
index 9362938abbfa..dd05e6c801fa 100644
@@ -177,7 +177,7 @@ void kasan_set_free_info(struct kmem_cache *cache,
 	if (!alloc_meta)
 		return;
 
-#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
 	idx = alloc_meta->free_track_idx;
 	alloc_meta->free_pointer_tag[idx] = tag;
 	alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS;
@@ -196,7 +196,7 @@ struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
 	if (!alloc_meta)
 		return NULL;
 
-#ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
 	for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
 		if (alloc_meta->free_pointer_tag[i] == tag)
 			break;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/3 kasan: integrate the common part of two KASAN tag-based modes

In reply to Kuan-Ying Lee

1. Move kasan_get_free_track() and kasan_set_free_info() into tags.c
2. Move kasan_get_bug_type() to header file

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/kasan/Makefile         |  4 +--
 mm/kasan/hw_tags.c        | 22 ---------------
 mm/kasan/report_hw_tags.c |  6 +---
 mm/kasan/report_sw_tags.c | 46 +------------------------------
 mm/kasan/report_tags.h    | 56 +++++++++++++++++++++++++++++++++++++
 mm/kasan/sw_tags.c        | 41 ---------------------------
 mm/kasan/tags.c           | 58 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 115 deletions(-)
 create mode 100644 mm/kasan/report_tags.h
 create mode 100644 mm/kasan/tags.c
mm/kasan/Makefile +2 -2
index 9fe39a66388a..634de6c1da9b 100644
@@ -37,5 +37,5 @@ CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
 
 obj-$(CONFIG_KASAN) := common.o report.o
 obj-$(CONFIG_KASAN_GENERIC) += init.o generic.o report_generic.o shadow.o quarantine.o
-obj-$(CONFIG_KASAN_HW_TAGS) += hw_tags.o report_hw_tags.o
-obj-$(CONFIG_KASAN_SW_TAGS) += init.o report_sw_tags.o shadow.o sw_tags.o
+obj-$(CONFIG_KASAN_HW_TAGS) += hw_tags.o report_hw_tags.o tags.o
+obj-$(CONFIG_KASAN_SW_TAGS) += init.o report_sw_tags.o shadow.o sw_tags.o tags.o
mm/kasan/hw_tags.c +0 -22
index ed5e5b833d61..4ea8c368b5b8 100644
@@ -216,28 +216,6 @@ void __init kasan_init_hw_tags(void)
 	pr_info("KernelAddressSanitizer initialized\n");
 }
 
-void kasan_set_free_info(struct kmem_cache *cache,
-				void *object, u8 tag)
-{
-	struct kasan_alloc_meta *alloc_meta;
-
-	alloc_meta = kasan_get_alloc_meta(cache, object);
-	if (alloc_meta)
-		kasan_set_track(&alloc_meta->free_track[0], GFP_NOWAIT);
-}
-
-struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
-				void *object, u8 tag)
-{
-	struct kasan_alloc_meta *alloc_meta;
-
-	alloc_meta = kasan_get_alloc_meta(cache, object);
-	if (!alloc_meta)
-		return NULL;
-
-	return &alloc_meta->free_track[0];
-}
-
 void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags)
 {
 	/*
mm/kasan/report_hw_tags.c +1 -5
index 42b2168755d6..ef5e7378f3aa 100644
@@ -14,11 +14,7 @@
 #include <linux/types.h>
 
 #include "kasan.h"
-
-const char *kasan_get_bug_type(struct kasan_access_info *info)
-{
-	return "invalid-access";
-}
+#include "report_tags.h"
 
 void *kasan_find_first_bad_addr(void *addr, size_t size)
 {
mm/kasan/report_sw_tags.c +1 -45

52 lines not on this page · raw message

mm/kasan/report_tags.h new file +56 -0

56 lines not on this page · raw message

mm/kasan/sw_tags.c +0 -41

47 lines not on this page · raw message

mm/kasan/tags.c new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v2 2/3] kasan: integrate the common part of two KASAN tag-based modes

Quoted 22 lines · Kuan-Ying Lee

1. Move kasan_get_free_track() and kasan_set_free_info() into tags.c
2. Move kasan_get_bug_type() to header file

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/kasan/Makefile         |  4 +--
 mm/kasan/hw_tags.c        | 22 ---------------
 mm/kasan/report_hw_tags.c |  6 +---
 mm/kasan/report_sw_tags.c | 46 +------------------------------
 mm/kasan/report_tags.h    | 56 +++++++++++++++++++++++++++++++++++++
 mm/kasan/sw_tags.c        | 41 ---------------------------
 mm/kasan/tags.c           | 58 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 115 deletions(-)
 create mode 100644 mm/kasan/report_tags.h
 create mode 100644 mm/kasan/tags.c

[...]

Quoted 123 lines
mm/kasan/hw_tags.c +0 -22
index ed5e5b833d61..4ea8c368b5b8 100644
@@ -216,28 +216,6 @@ void __init kasan_init_hw_tags(void)
        pr_info("KernelAddressSanitizer initialized\n");
 }
 
-void kasan_set_free_info(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (alloc_meta)
-               kasan_set_track(&alloc_meta->free_track[0], GFP_NOWAIT);
-}
-
-struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return NULL;
-
-       return &alloc_meta->free_track[0];
-}
-
 void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags)
 {
        /*
mm/kasan/report_hw_tags.c +1 -5
index 42b2168755d6..ef5e7378f3aa 100644
@@ -14,11 +14,7 @@
 #include <linux/types.h>
 
 #include "kasan.h"
-
-const char *kasan_get_bug_type(struct kasan_access_info *info)
-{
-       return "invalid-access";
-}
+#include "report_tags.h"
 
 void *kasan_find_first_bad_addr(void *addr, size_t size)
 {
mm/kasan/report_sw_tags.c +1 -45
index 821a14a19a92..d965a170083e 100644
@@ -26,51 +26,7 @@
 
 #include <asm/sections.h>
 
-#include "kasan.h"
-#include "../slab.h"
-
-const char *kasan_get_bug_type(struct kasan_access_info *info)
-{
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       struct kasan_alloc_meta *alloc_meta;
-       struct kmem_cache *cache;
-       struct page *page;
-       const void *addr;
-       void *object;
-       u8 tag;
-       int i;
-
-       tag = get_tag(info->access_addr);
-       addr = kasan_reset_tag(info->access_addr);
-       page = kasan_addr_to_page(addr);
-       if (page && PageSlab(page)) {
-               cache = page->slab_cache;
-               object = nearest_obj(cache, page, (void *)addr);
-               alloc_meta = kasan_get_alloc_meta(cache, object);
-
-               if (alloc_meta) {
-                       for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
-                               if (alloc_meta->free_pointer_tag[i] == tag)
-                                       return "use-after-free";
-                       }
-               }
-               return "out-of-bounds";
-       }
-
-#endif
-       /*
-        * If access_size is a negative number, then it has reason to be
-        * defined as out-of-bounds bug type.
-        *
-        * Casting negative numbers to size_t would indeed turn up as
-        * a large size_t and its value will be larger than ULONG_MAX/2,
-        * so that this can qualify as out-of-bounds.
-        */
-       if (info->access_addr + info->access_size < info->access_addr)
-               return "out-of-bounds";
-
-       return "invalid-access";
-}
+#include "report_tags.h"
 
 void *kasan_find_first_bad_addr(void *addr, size_t size)
 {
mm/kasan/report_tags.h new file +10 -0
new file mode 100644
index 000000000000..4f740d4d99ee
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_KASAN_REPORT_TAGS_H
+#define __MM_KASAN_REPORT_TAGS_H
+
+#include "kasan.h"
+#include "../slab.h"
+
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{

[...]

mm/kasan/report_tags.h line 45

Quoted 10 lines
mm/kasan/report_tags.h new file +10 -0
+       /*
+        * If access_size is a negative number, then it has reason to be
+        * defined as out-of-bounds bug type.
+        *
+        * Casting negative numbers to size_t would indeed turn up as
+        * a large size_t and its value will be larger than ULONG_MAX/2,
+        * so that this can qualify as out-of-bounds.
+        */
+       if (info->access_addr + info->access_size < info->access_addr)
+               return "out-of-bounds";

This seems to change behaviour for SW_TAGS because it was there even if !CONFIG_KASAN_TAGS_IDENTIFY. Does it still work as before?

Quoted 69 lines
mm/kasan/report_tags.h new file +11 -0
+
+       return "invalid-access";
+}
+#else
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{
+       return "invalid-access";
+}
+#endif
+
+#endif
mm/kasan/sw_tags.c +0 -41
index dd05e6c801fa..bd3f540feb47 100644
@@ -167,47 +167,6 @@ void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
 }
 EXPORT_SYMBOL(__hwasan_tag_memory);
 
-void kasan_set_free_info(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-       u8 idx = 0;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return;
-
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       idx = alloc_meta->free_track_idx;
-       alloc_meta->free_pointer_tag[idx] = tag;
-       alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS;
-#endif
-
-       kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT);
-}
-
-struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-       int i = 0;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return NULL;
-
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
-               if (alloc_meta->free_pointer_tag[i] == tag)
-                       break;
-       }
-       if (i == KASAN_NR_FREE_STACKS)
-               i = alloc_meta->free_track_idx;
-#endif
-
-       return &alloc_meta->free_track[i];
-}
-
 void kasan_tag_mismatch(unsigned long addr, unsigned long access_info,
                        unsigned long ret_ip)
 {
mm/kasan/tags.c new file +0 -0
new file mode 100644
index 000000000000..9c33c0ebe1d1

mm/kasan/tags.c line 5

Quoted 5 lines
mm/kasan/tags.c new file +5 -0
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains common tag-based KASAN code.
+ *
+ * Author: Kuan-Ying Lee <[email protected]>

We appreciate your work on this, but this is misleading. Because you merely copied/moved the code, have a look what sw_tags.c says -- that should either be preserved, or we add nothing here.

I prefer to add nothing or the bare minimum (e.g. if the company requires a Copyright line) for non-substantial additions because this stuff becomes out-of-date fast and just isn't useful at all. 'git log' is the source of truth.

Cc'ing Greg for process advice. For moved code, does it have to preserve the original Copyright line if there was one?

Thanks,
-- Marco

#4 lore raw

Re: [PATCH v2 2/3] kasan: integrate the common part of two KASAN tag-based modes

Quoted 159 lines · Marco Elver
Quoted 22 lines · Kuan-Ying Lee

1. Move kasan_get_free_track() and kasan_set_free_info() into tags.c
2. Move kasan_get_bug_type() to header file

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/kasan/Makefile         |  4 +--
 mm/kasan/hw_tags.c        | 22 ---------------
 mm/kasan/report_hw_tags.c |  6 +---
 mm/kasan/report_sw_tags.c | 46 +------------------------------
 mm/kasan/report_tags.h    | 56 +++++++++++++++++++++++++++++++++++++
 mm/kasan/sw_tags.c        | 41 ---------------------------
 mm/kasan/tags.c           | 58 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 115 deletions(-)
 create mode 100644 mm/kasan/report_tags.h
 create mode 100644 mm/kasan/tags.c

[...]

Quoted 123 lines
mm/kasan/hw_tags.c +0 -22
index ed5e5b833d61..4ea8c368b5b8 100644
@@ -216,28 +216,6 @@ void __init kasan_init_hw_tags(void)
        pr_info("KernelAddressSanitizer initialized\n");
 }
 
-void kasan_set_free_info(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (alloc_meta)
-               kasan_set_track(&alloc_meta->free_track[0], GFP_NOWAIT);
-}
-
-struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return NULL;
-
-       return &alloc_meta->free_track[0];
-}
-
 void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags)
 {
        /*
mm/kasan/report_hw_tags.c +1 -5
index 42b2168755d6..ef5e7378f3aa 100644
@@ -14,11 +14,7 @@
 #include <linux/types.h>
 
 #include "kasan.h"
-
-const char *kasan_get_bug_type(struct kasan_access_info *info)
-{
-       return "invalid-access";
-}
+#include "report_tags.h"
 
 void *kasan_find_first_bad_addr(void *addr, size_t size)
 {
mm/kasan/report_sw_tags.c +1 -45
index 821a14a19a92..d965a170083e 100644
@@ -26,51 +26,7 @@
 
 #include <asm/sections.h>
 
-#include "kasan.h"
-#include "../slab.h"
-
-const char *kasan_get_bug_type(struct kasan_access_info *info)
-{
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       struct kasan_alloc_meta *alloc_meta;
-       struct kmem_cache *cache;
-       struct page *page;
-       const void *addr;
-       void *object;
-       u8 tag;
-       int i;
-
-       tag = get_tag(info->access_addr);
-       addr = kasan_reset_tag(info->access_addr);
-       page = kasan_addr_to_page(addr);
-       if (page && PageSlab(page)) {
-               cache = page->slab_cache;
-               object = nearest_obj(cache, page, (void *)addr);
-               alloc_meta = kasan_get_alloc_meta(cache, object);
-
-               if (alloc_meta) {
-                       for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
-                               if (alloc_meta->free_pointer_tag[i] == tag)
-                                       return "use-after-free";
-                       }
-               }
-               return "out-of-bounds";
-       }
-
-#endif
-       /*
-        * If access_size is a negative number, then it has reason to be
-        * defined as out-of-bounds bug type.
-        *
-        * Casting negative numbers to size_t would indeed turn up as
-        * a large size_t and its value will be larger than ULONG_MAX/2,
-        * so that this can qualify as out-of-bounds.
-        */
-       if (info->access_addr + info->access_size < info->access_addr)
-               return "out-of-bounds";
-
-       return "invalid-access";
-}
+#include "report_tags.h"
 
 void *kasan_find_first_bad_addr(void *addr, size_t size)
 {
mm/kasan/report_tags.h new file +10 -0
new file mode 100644
index 000000000000..4f740d4d99ee
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_KASAN_REPORT_TAGS_H
+#define __MM_KASAN_REPORT_TAGS_H
+
+#include "kasan.h"
+#include "../slab.h"
+
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{

[...]

mm/kasan/report_tags.h line 45

Quoted 10 lines
mm/kasan/report_tags.h new file +10 -0
+       /*
+        * If access_size is a negative number, then it has reason to be
+        * defined as out-of-bounds bug type.
+        *
+        * Casting negative numbers to size_t would indeed turn up as
+        * a large size_t and its value will be larger than ULONG_MAX/2,
+        * so that this can qualify as out-of-bounds.
+        */
+       if (info->access_addr + info->access_size < info->access_addr)
+               return "out-of-bounds";
Quoted 3 lines

This seems to change behaviour for SW_TAGS because it was there even if !CONFIG_KASAN_TAGS_IDENTIFY. Does it still work as before?

You are right. It will change the behavior.
However, I think that if !CONFIG_KASAN_TAG_IDENTIFY, it should be reported "invalid-access".

Or is it better to keep it in both conditions?

Quoted 79 lines
Quoted 69 lines
mm/kasan/report_tags.h new file +11 -0
+
+       return "invalid-access";
+}
+#else
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{
+       return "invalid-access";
+}
+#endif
+
+#endif
mm/kasan/sw_tags.c +0 -41
index dd05e6c801fa..bd3f540feb47 100644
@@ -167,47 +167,6 @@ void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
 }
 EXPORT_SYMBOL(__hwasan_tag_memory);
 
-void kasan_set_free_info(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-       u8 idx = 0;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return;
-
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       idx = alloc_meta->free_track_idx;
-       alloc_meta->free_pointer_tag[idx] = tag;
-       alloc_meta->free_track_idx = (idx + 1) % KASAN_NR_FREE_STACKS;
-#endif
-
-       kasan_set_track(&alloc_meta->free_track[idx], GFP_NOWAIT);
-}
-
-struct kasan_track *kasan_get_free_track(struct kmem_cache *cache,
-                               void *object, u8 tag)
-{
-       struct kasan_alloc_meta *alloc_meta;
-       int i = 0;
-
-       alloc_meta = kasan_get_alloc_meta(cache, object);
-       if (!alloc_meta)
-               return NULL;
-
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
-       for (i = 0; i < KASAN_NR_FREE_STACKS; i++) {
-               if (alloc_meta->free_pointer_tag[i] == tag)
-                       break;
-       }
-       if (i == KASAN_NR_FREE_STACKS)
-               i = alloc_meta->free_track_idx;
-#endif
-
-       return &alloc_meta->free_track[i];
-}
-
 void kasan_tag_mismatch(unsigned long addr, unsigned long access_info,
                        unsigned long ret_ip)
 {
mm/kasan/tags.c new file +0 -0
new file mode 100644
index 000000000000..9c33c0ebe1d1

mm/kasan/tags.c line 5

Quoted 5 lines
mm/kasan/tags.c new file +5 -0
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains common tag-based KASAN code.
+ *
+ * Author: Kuan-Ying Lee <[email protected]>

We appreciate your work on this, but this is misleading. Because you merely copied/moved the code, have a look what sw_tags.c says -- that should either be preserved, or we add nothing here.

Quoted 4 lines

I prefer to add nothing or the bare minimum (e.g. if the company requires a Copyright line) for non-substantial additions because this stuff becomes out-of-date fast and just isn't useful at all. 'git log' is the source of truth.

This was my first time to upload a new file.
Thanks for the suggestions. :)
I will remove this author tag and wait for Greg's process advice.

Quoted 6 lines

Cc'ing Greg for process advice. For moved code, does it have to preserve the original Copyright line if there was one?

Thanks,
-- Marco

#5 lore raw

Re: [PATCH v2 2/3] kasan: integrate the common part of two KASAN tag-based modes

On Sat, 12 Jun 2021 at 17:51, Kuan-Ying Lee <[email protected]> wrote:
[...]

Quoted 28 lines
Quoted 28 lines
Quoted 16 lines
mm/kasan/report_tags.h new file +10 -0
new file mode 100644
index 000000000000..4f740d4d99ee
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_KASAN_REPORT_TAGS_H
+#define __MM_KASAN_REPORT_TAGS_H
+
+#include "kasan.h"
+#include "../slab.h"
+
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{

[...]

Quoted 10 lines
mm/kasan/report_tags.h new file +10 -0
+       /*
+        * If access_size is a negative number, then it has reason to be
+        * defined as out-of-bounds bug type.
+        *
+        * Casting negative numbers to size_t would indeed turn up as
+        * a large size_t and its value will be larger than ULONG_MAX/2,
+        * so that this can qualify as out-of-bounds.
+        */
+       if (info->access_addr + info->access_size < info->access_addr)
+               return "out-of-bounds";
Quoted 7 lines
Quoted 3 lines

This seems to change behaviour for SW_TAGS because it was there even if !CONFIG_KASAN_TAGS_IDENTIFY. Does it still work as before?

You are right. It will change the behavior.
However, I think that if !CONFIG_KASAN_TAG_IDENTIFY, it should be reported "invalid-access".

There's no reason that if !CONFIG_KASAN_TAG_IDENTIFY it should be reported as "invalid-acces" if we can do better without the additional state that the config option introduces.

It's trivial to give a slightly better report without additional state, see the comment explaining why it's reasonable to infer out-of-bounds here.

Quoted 1 line

Or is it better to keep it in both conditions?

We want to make this patch a non-functional change.

[...]

Quoted 16 lines
Quoted 16 lines

mm/kasan/tags.c line 5

Quoted 11 lines
mm/kasan/tags.c new file +5 -0
new file mode 100644
index 000000000000..9c33c0ebe1d1
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains common tag-based KASAN code.
+ *
+ * Author: Kuan-Ying Lee <[email protected]>

We appreciate your work on this, but this is misleading. Because you merely copied/moved the code, have a look what sw_tags.c says -- that should either be preserved, or we add nothing here.

Quoted 12 lines
Quoted 4 lines

I prefer to add nothing or the bare minimum (e.g. if the company requires a Copyright line) for non-substantial additions because this stuff becomes out-of-date fast and just isn't useful at all. 'git log' is the source of truth.

This was my first time to upload a new file.
Thanks for the suggestions. :)
I will remove this author tag and wait for Greg's process advice.

Quoted 3 lines

Cc'ing Greg for process advice. For moved code, does it have to preserve the original Copyright line if there was one?

Greg responded, see his emails. Please preserve the original header from the file the code was moved from (hw_tags.c/sw_tags.c).

Thanks,
-- Marco

#6 lore raw

Re: [PATCH v2 2/3] kasan: integrate the common part of two KASAN tag-based modes

Quoted 42 lines · Marco Elver

On Sat, 12 Jun 2021 at 17:51, Kuan-Ying Lee <[email protected]> wrote:
[...]

Quoted 28 lines
Quoted 28 lines
Quoted 16 lines
mm/kasan/report_tags.h new file +10 -0
new file mode 100644
index 000000000000..4f740d4d99ee
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_KASAN_REPORT_TAGS_H
+#define __MM_KASAN_REPORT_TAGS_H
+
+#include "kasan.h"
+#include "../slab.h"
+
+#ifdef CONFIG_KASAN_TAGS_IDENTIFY
+const char *kasan_get_bug_type(struct kasan_access_info *info)
+{

[...]

Quoted 10 lines
mm/kasan/report_tags.h new file +10 -0
+       /*
+        * If access_size is a negative number, then it has reason to be
+        * defined as out-of-bounds bug type.
+        *
+        * Casting negative numbers to size_t would indeed turn up as
+        * a large size_t and its value will be larger than ULONG_MAX/2,
+        * so that this can qualify as out-of-bounds.
+        */
+       if (info->access_addr + info->access_size < info->access_addr)
+               return "out-of-bounds";
Quoted 7 lines
Quoted 3 lines

This seems to change behaviour for SW_TAGS because it was there even if !CONFIG_KASAN_TAGS_IDENTIFY. Does it still work as before?

You are right. It will change the behavior.
However, I think that if !CONFIG_KASAN_TAG_IDENTIFY, it should be reported "invalid-access".

There's no reason that if !CONFIG_KASAN_TAG_IDENTIFY it should be reported as "invalid-acces" if we can do better without the additional state that the config option introduces.

Quoted 8 lines

It's trivial to give a slightly better report without additional state, see the comment explaining why it's reasonable to infer out-of-bounds here.

Quoted 1 line

Or is it better to keep it in both conditions?

We want to make this patch a non-functional change.

Got it.

Quoted 27 lines

[...]

Quoted 26 lines
Quoted 16 lines

mm/kasan/tags.c line 5

Quoted 11 lines
mm/kasan/tags.c new file +5 -0
new file mode 100644
index 000000000000..9c33c0ebe1d1
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains common tag-based KASAN code.
+ *
+ * Author: Kuan-Ying Lee <[email protected]>

We appreciate your work on this, but this is misleading. Because you merely copied/moved the code, have a look what sw_tags.c says -- that should either be preserved, or we add nothing here.

Quoted 4 lines

I prefer to add nothing or the bare minimum (e.g. if the company requires a Copyright line) for non-substantial additions because this stuff becomes out-of-date fast and just isn't useful at all. 'git log' is the source of truth.

This was my first time to upload a new file.
Thanks for the suggestions. :)
I will remove this author tag and wait for Greg's process advice.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Cc'ing Greg for process advice. For moved code, does it have to preserve the original Copyright line if there was one?

Greg responded, see his emails. Please preserve the original header from the file the code was moved from (hw_tags.c/sw_tags.c).

Ok. I will do it in v3.
Thanks.

Quoted 3 lines

Thanks,
-- Marco

#7 lore raw

Re: [PATCH v2 2/3] kasan: integrate the common part of two KASAN tag-based modes

In reply to Marco Elver
Quoted 17 lines · Marco Elver

mm/kasan/tags.c line 5

Quoted 11 lines · Kuan-Ying Lee
mm/kasan/tags.c new file +5 -0
new file mode 100644
index 000000000000..9c33c0ebe1d1
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains common tag-based KASAN code.
+ *
+ * Author: Kuan-Ying Lee <[email protected]>

We appreciate your work on this, but this is misleading. Because you merely copied/moved the code, have a look what sw_tags.c says -- that should either be preserved, or we add nothing here.

Quoted 7 lines

I prefer to add nothing or the bare minimum (e.g. if the company requires a Copyright line) for non-substantial additions because this stuff becomes out-of-date fast and just isn't useful at all. 'git log' is the source of truth.

Cc'ing Greg for process advice. For moved code, does it have to preserve the original Copyright line if there was one?

Yes, it does have to. Unless you want to talk to a lot of lawyers about the issues involved here and can defend the removal of the copyright lines to them.

So please keep them. Unless you can get your corporate lawyer to sign off on the patch that does the removal.

thanks,

greg k-h

#8 lore raw

PATCH v2 3/3 kasan: add memory corruption identification support for hardware tag-based mode

In reply to Kuan-Ying Lee

Add memory corruption identification support for hardware tag-based mode. We store one old free pointer tag and free backtrace.

Cc Andrey Ryabinin <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrey Konovalov <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Andrew Morton <[email protected]>
---
 lib/Kconfig.kasan | 2 +-
 mm/kasan/kasan.h  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/Kconfig.kasan +1 -1
index 6f5d48832139..2cc25792bc2f 100644
@@ -157,7 +157,7 @@ config KASAN_STACK
 
 config KASAN_TAGS_IDENTIFY
 	bool "Enable memory corruption identification"
-	depends on KASAN_SW_TAGS
+	depends on KASAN_SW_TAGS || KASAN_HW_TAGS
 	help
 	  This option enables best-effort identification of bug type
 	  (use-after-free or out-of-bounds) at the cost of increased
mm/kasan/kasan.h +1 -1
index b0fc9a1eb7e3..d6f982b8a84e 100644
@@ -153,7 +153,7 @@ struct kasan_track {
 	depot_stack_handle_t stack;
 };
 
-#ifdef CONFIG_KASAN_TAGS_IDENTIFY
+#if defined(CONFIG_KASAN_TAGS_IDENTIFY) && defined(CONFIG_KASAN_SW_TAGS)
 #define KASAN_NR_FREE_STACKS 5
 #else
 #define KASAN_NR_FREE_STACKS 1
Signature · 1 line
2.25.1