Skip to contentSkip to conversation

kasan: assorted fixes and improvements

PATCH Applied [email protected] 3 participants 13 messages Oct 6, 2023 – Oct 9, 2023

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

cover kasan: assorted fixes and improvements

From: Andrey Konovalov <[email protected]>

Andrey Konovalov (5):
arm64, kasan: update comment in kasan_init
kasan: unify printk prefixes
kasan: use unchecked __memset internally
kasan: fix and update KUNIT_EXPECT_KASAN_FAIL comment
Documentation: *san: drop "the" from article titles

 Documentation/dev-tools/kasan.rst |  7 +++++--
 Documentation/dev-tools/kcsan.rst |  4 ++--
 Documentation/dev-tools/kmsan.rst |  6 +++---
 arch/arm64/mm/kasan_init.c        |  6 +++++-
 mm/kasan/kasan_test.c             | 11 ++++++-----
 mm/kasan/kasan_test_module.c      |  2 +-
 mm/kasan/quarantine.c             |  4 +++-
 mm/kasan/report.c                 |  4 ++--
 mm/kasan/report_generic.c         |  6 +++---
 mm/kasan/shadow.c                 |  2 +-
 10 files changed, 31 insertions(+), 21 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/5 arm64, kasan: update comment in kasan_init

From: Andrey Konovalov <[email protected]>

Update the comment in kasan_init to also mention the Hardware Tag-Based KASAN mode.

---
 arch/arm64/mm/kasan_init.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm64/mm/kasan_init.c +5 -1
index f17d066e85eb..555285ebd5af 100644
@@ -300,7 +300,11 @@ void __init kasan_init(void)
 	kasan_init_shadow();
 	kasan_init_depth();
 #if defined(CONFIG_KASAN_GENERIC)
-	/* CONFIG_KASAN_SW_TAGS also requires kasan_init_sw_tags(). */
+	/*
+	 * Generic KASAN is now fully initialized.
+	 * Software and Hardware Tag-Based modes still require
+	 * kasan_init_sw_tags() and kasan_init_hw_tags() correspondingly.
+	 */
 	pr_info("KernelAddressSanitizer initialized (generic)\n");
 #endif
 }
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/5 kasan: unify printk prefixes

In reply to [email protected]

From: Andrey Konovalov <[email protected]>

Unify prefixes for printk messages in mm/kasan/.

---
 mm/kasan/kasan_test.c        | 2 +-
 mm/kasan/kasan_test_module.c | 2 +-
 mm/kasan/quarantine.c        | 4 +++-
 mm/kasan/report_generic.c    | 6 +++---
 4 files changed, 8 insertions(+), 6 deletions(-)
mm/kasan/kasan_test.c +1 -1
index b61cc6a42541..c707d6c6e019 100644
@@ -5,7 +5,7 @@
  * Author: Andrey Ryabinin <[email protected]>
  */
 
-#define pr_fmt(fmt) "kasan_test: " fmt
+#define pr_fmt(fmt) "kasan: test: " fmt
 
 #include <kunit/test.h>
 #include <linux/bitops.h>
mm/kasan/kasan_test_module.c +1 -1
index 7be7bed456ef..8b7b3ea2c74e 100644
@@ -5,7 +5,7 @@
  * Author: Andrey Ryabinin <[email protected]>
  */
 
-#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
+#define pr_fmt(fmt) "kasan: test: " fmt
 
 #include <linux/mman.h>
 #include <linux/module.h>
mm/kasan/quarantine.c +3 -1
index 152dca73f398..ca4529156735 100644
@@ -8,6 +8,8 @@
  * Based on code by Dmitry Chernenkov.
  */
 
+#define pr_fmt(fmt) "kasan: " fmt
+
 #include <linux/gfp.h>
 #include <linux/hash.h>
 #include <linux/kernel.h>
@@ -414,7 +416,7 @@ static int __init kasan_cpu_quarantine_init(void)
 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
 				kasan_cpu_online, kasan_cpu_offline);
 	if (ret < 0)
-		pr_err("kasan cpu quarantine register failed [%d]\n", ret);
+		pr_err("cpu quarantine register failed [%d]\n", ret);
 	return ret;
 }
 late_initcall(kasan_cpu_quarantine_init);
mm/kasan/report_generic.c +3 -3
index 51a1e8a8877f..99cbcd73cff7 100644
@@ -220,7 +220,7 @@ static bool __must_check tokenize_frame_descr(const char **frame_descr,
 		const size_t tok_len = sep - *frame_descr;
 
 		if (tok_len + 1 > max_tok_len) {
-			pr_err("KASAN internal error: frame description too long: %s\n",
+			pr_err("internal error: frame description too long: %s\n",
 			       *frame_descr);
 			return false;
 		}
@@ -233,7 +233,7 @@ static bool __must_check tokenize_frame_descr(const char **frame_descr,
 	*frame_descr = sep + 1;
 
 	if (value != NULL && kstrtoul(token, 10, value)) {
-		pr_err("KASAN internal error: not a valid number: %s\n", token);
+		pr_err("internal error: not a valid number: %s\n", token);
 		return false;
 	}
 
@@ -323,7 +323,7 @@ static bool __must_check get_address_stack_frame_info(const void *addr,
 
 	frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
 	if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
-		pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
+		pr_err("internal error: frame has invalid marker: %lu\n",
 		       frame[0]);
 		return false;
 	}
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH 2/5] kasan: unify printk prefixes

Quoted 6 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Unify prefixes for printk messages in mm/kasan/.

Quoted 89 lines
---
 mm/kasan/kasan_test.c        | 2 +-
 mm/kasan/kasan_test_module.c | 2 +-
 mm/kasan/quarantine.c        | 4 +++-
 mm/kasan/report_generic.c    | 6 +++---
 4 files changed, 8 insertions(+), 6 deletions(-)
mm/kasan/kasan_test.c +1 -1
index b61cc6a42541..c707d6c6e019 100644
@@ -5,7 +5,7 @@
  * Author: Andrey Ryabinin <[email protected]>
  */
 
-#define pr_fmt(fmt) "kasan_test: " fmt
+#define pr_fmt(fmt) "kasan: test: " fmt
 
 #include <kunit/test.h>
 #include <linux/bitops.h>
mm/kasan/kasan_test_module.c +1 -1
index 7be7bed456ef..8b7b3ea2c74e 100644
@@ -5,7 +5,7 @@
  * Author: Andrey Ryabinin <[email protected]>
  */
 
-#define pr_fmt(fmt) "kasan test: %s " fmt, __func__
+#define pr_fmt(fmt) "kasan: test: " fmt
 
 #include <linux/mman.h>
 #include <linux/module.h>
mm/kasan/quarantine.c +3 -1
index 152dca73f398..ca4529156735 100644
@@ -8,6 +8,8 @@
  * Based on code by Dmitry Chernenkov.
  */
 
+#define pr_fmt(fmt) "kasan: " fmt
+
 #include <linux/gfp.h>
 #include <linux/hash.h>
 #include <linux/kernel.h>
@@ -414,7 +416,7 @@ static int __init kasan_cpu_quarantine_init(void)
        ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online",
                                kasan_cpu_online, kasan_cpu_offline);
        if (ret < 0)
-               pr_err("kasan cpu quarantine register failed [%d]\n", ret);
+               pr_err("cpu quarantine register failed [%d]\n", ret);
        return ret;
 }
 late_initcall(kasan_cpu_quarantine_init);
mm/kasan/report_generic.c +3 -4
index 51a1e8a8877f..99cbcd73cff7 100644
@@ -220,7 +220,7 @@ static bool __must_check tokenize_frame_descr(const char **frame_descr,
                const size_t tok_len = sep - *frame_descr;
 
                if (tok_len + 1 > max_tok_len) {
-                       pr_err("KASAN internal error: frame description too long: %s\n",
+                       pr_err("internal error: frame description too long: %s\n",
                               *frame_descr);
                        return false;
                }
@@ -233,7 +233,7 @@ static bool __must_check tokenize_frame_descr(const char **frame_descr,
        *frame_descr = sep + 1;
 
        if (value != NULL && kstrtoul(token, 10, value)) {
-               pr_err("KASAN internal error: not a valid number: %s\n", token);
+               pr_err("internal error: not a valid number: %s\n", token);
                return false;
        }
 
@@ -323,7 +323,7 @@ static bool __must_check get_address_stack_frame_info(const void *addr,
 
        frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
        if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
-               pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
+               pr_err("internal error: frame has invalid marker: %lu\n",
                       frame[0]);
                return false;
        }
--

2.25.1

#4 lore raw

PATCH 3/5 kasan: use unchecked __memset internally

In reply to [email protected]

From: Andrey Konovalov <[email protected]>

KASAN code is supposed to use the unchecked __memset implementation when accessing its metadata.

Change uses of memset to __memset in mm/kasan/.

---
 mm/kasan/report.c | 4 ++--
 mm/kasan/shadow.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/kasan/report.c +2 -2
index ca4b6ff080a6..12557ffee90b 100644
@@ -538,7 +538,7 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty
 
 	start_report(&flags, true);
 
-	memset(&info, 0, sizeof(info));
+	__memset(&info, 0, sizeof(info));
 	info.type = type;
 	info.access_addr = ptr;
 	info.access_size = 0;
@@ -576,7 +576,7 @@ bool kasan_report(const void *addr, size_t size, bool is_write,
 
 	start_report(&irq_flags, true);
 
-	memset(&info, 0, sizeof(info));
+	__memset(&info, 0, sizeof(info));
 	info.type = KASAN_REPORT_ACCESS;
 	info.access_addr = addr;
 	info.access_size = size;
mm/kasan/shadow.c +1 -1
index dd772f9d0f08..d687f09a7ae3 100644
@@ -324,7 +324,7 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
 	if (!page)
 		return -ENOMEM;
 
-	memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
+	__memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
 	pte = pfn_pte(PFN_DOWN(__pa(page)), PAGE_KERNEL);
 
 	spin_lock(&init_mm.page_table_lock);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 3/5
#5 lore raw

Re: [PATCH 3/5] kasan: use unchecked __memset internally

Quoted 9 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

KASAN code is supposed to use the unchecked __memset implementation when accessing its metadata.

Change uses of memset to __memset in mm/kasan/.

Do we need a "Fixes" tag?

Quoted 43 lines
---
 mm/kasan/report.c | 4 ++--
 mm/kasan/shadow.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/kasan/report.c +2 -2
index ca4b6ff080a6..12557ffee90b 100644
@@ -538,7 +538,7 @@ void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_ty
 
        start_report(&flags, true);
 
-       memset(&info, 0, sizeof(info));
+       __memset(&info, 0, sizeof(info));
        info.type = type;
        info.access_addr = ptr;
        info.access_size = 0;
@@ -576,7 +576,7 @@ bool kasan_report(const void *addr, size_t size, bool is_write,
 
        start_report(&irq_flags, true);
 
-       memset(&info, 0, sizeof(info));
+       __memset(&info, 0, sizeof(info));
        info.type = KASAN_REPORT_ACCESS;
        info.access_addr = addr;
        info.access_size = size;
mm/kasan/shadow.c +1 -2
index dd772f9d0f08..d687f09a7ae3 100644
@@ -324,7 +324,7 @@ static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr,
        if (!page)
                return -ENOMEM;
 
-       memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
+       __memset((void *)page, KASAN_VMALLOC_INVALID, PAGE_SIZE);
        pte = pfn_pte(PFN_DOWN(__pa(page)), PAGE_KERNEL);
 
        spin_lock(&init_mm.page_table_lock);
--

2.25.1

#6 lore raw

Re: [PATCH 3/5] kasan: use unchecked __memset internally

Quoted 5 lines · Marco Elver
Quoted 3 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Quoted 8 lines
Quoted 6 lines

KASAN code is supposed to use the unchecked __memset implementation when accessing its metadata.

Change uses of memset to __memset in mm/kasan/.

Do we need a "Fixes" tag?

Good idea, let's add them:

Fixes 59e6e098d1c1 ("kasan: introduce kasan_complete_mode_report_info")
Fixes 3c5c3cfb9ef4 ("kasan: support backing vmalloc space with real

shadow memory")

Quoted 1 line

Thanks!

#7 lore raw

PATCH 4/5 kasan: fix and update KUNIT_EXPECT_KASAN_FAIL comment

In reply to [email protected]

From: Andrey Konovalov <[email protected]>

Update the comment for KUNIT_EXPECT_KASAN_FAIL to describe the parameters this macro accepts.

Also drop the mention of the "kasan_status" KUnit resource, as it no longer exists.

---
 mm/kasan/kasan_test.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/kasan/kasan_test.c +5 -4
index c707d6c6e019..2030c7ff7de9 100644
@@ -91,10 +91,11 @@ static void kasan_test_exit(struct kunit *test)
 }
 
 /**
- * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
- * KASAN report; causes a test failure otherwise. This relies on a KUnit
- * resource named "kasan_status". Do not use this name for KUnit resources
- * outside of KASAN tests.
+ * KUNIT_EXPECT_KASAN_FAIL - check that the executed expression produces a
+ * KASAN report; causes a KUnit test failure otherwise.
+ *
+ * @test: Currently executing KUnit test.
+ * @expression: Expression that must produce a KASAN report.
  *
  * For hardware tag-based KASAN, when a synchronous tag fault happens, tag
  * checking is auto-disabled. When this happens, this test handler reenables
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] kasan: fix and update KUNIT_EXPECT_KASAN_FAIL comment

Quoted 11 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Update the comment for KUNIT_EXPECT_KASAN_FAIL to describe the parameters this macro accepts.

Also drop the mention of the "kasan_status" KUnit resource, as it no longer exists.

"Closes" isn't a valid tag? Reported-by + Link should be enough to attribute.

Quoted 1 line
Quoted 27 lines
---
 mm/kasan/kasan_test.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/kasan/kasan_test.c +5 -5
index c707d6c6e019..2030c7ff7de9 100644
@@ -91,10 +91,11 @@ static void kasan_test_exit(struct kunit *test)
 }
 
 /**
- * KUNIT_EXPECT_KASAN_FAIL() - check that the executed expression produces a
- * KASAN report; causes a test failure otherwise. This relies on a KUnit
- * resource named "kasan_status". Do not use this name for KUnit resources
- * outside of KASAN tests.
+ * KUNIT_EXPECT_KASAN_FAIL - check that the executed expression produces a
+ * KASAN report; causes a KUnit test failure otherwise.
+ *
+ * @test: Currently executing KUnit test.
+ * @expression: Expression that must produce a KASAN report.
  *
  * For hardware tag-based KASAN, when a synchronous tag fault happens, tag
  * checking is auto-disabled. When this happens, this test handler reenables
--

2.25.1

#9 lore raw

Re: [PATCH 4/5] kasan: fix and update KUNIT_EXPECT_KASAN_FAIL comment

Quoted 8 lines · Marco Elver
Quoted 6 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Update the comment for KUNIT_EXPECT_KASAN_FAIL to describe the parameters this macro accepts.

Quoted 7 lines
Quoted 5 lines

Also drop the mention of the "kasan_status" KUnit resource, as it no longer exists.

"Closes" isn't a valid tag? Reported-by + Link should be enough to attribute.

I believe it is: the robot asks to use it, see the link. (I think this tag is also used by syzbot btw.)

Quoted 3 lines
Quoted 1 line

Thanks!

#10 lore raw

PATCH 5/5 Documentation: *san: drop "the" from article titles

In reply to [email protected]

From: Andrey Konovalov <[email protected]>

Drop "the" from the titles of documentation articles for KASAN, KCSAN, and KMSAN, as it is redundant.

Also add SPDX-License-Identifier for kasan.rst.

---
 Documentation/dev-tools/kasan.rst | 7 +++++--
 Documentation/dev-tools/kcsan.rst | 4 ++--
 Documentation/dev-tools/kmsan.rst | 6 +++---
 3 files changed, 10 insertions(+), 7 deletions(-)
Documentation/dev-tools/kasan.rst +5 -2
index 382818a7197a..858c77fe7dc4 100644
@@ -1,5 +1,8 @@
-The Kernel Address Sanitizer (KASAN)
-====================================
+.. SPDX-License-Identifier: GPL-2.0
+.. Copyright (C) 2023, Google LLC.
+
+Kernel Address Sanitizer (KASAN)
+================================
 
 Overview
 --------
Documentation/dev-tools/kcsan.rst +2 -2
index 3ae866dcc924..94b6802ab0ab 100644
@@ -1,8 +1,8 @@
 .. SPDX-License-Identifier: GPL-2.0
 .. Copyright (C) 2019, Google LLC.
 
-The Kernel Concurrency Sanitizer (KCSAN)
-========================================
+Kernel Concurrency Sanitizer (KCSAN)
+====================================
 
 The Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector, which
 relies on compile-time instrumentation, and uses a watchpoint-based sampling
Documentation/dev-tools/kmsan.rst +3 -3
index 55fa82212eb2..323eedad53cd 100644
@@ -1,9 +1,9 @@
 .. SPDX-License-Identifier: GPL-2.0
 .. Copyright (C) 2022, Google LLC.
 
-===================================
-The Kernel Memory Sanitizer (KMSAN)
-===================================
+===============================
+Kernel Memory Sanitizer (KMSAN)
+===============================
 
 KMSAN is a dynamic error detector aimed at finding uses of uninitialized
 values. It is based on compiler instrumentation, and is quite similar to the
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 5/5
#11 lore raw

Re: [PATCH 5/5] Documentation: *san: drop "the" from article titles

Quoted 6 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Drop "the" from the titles of documentation articles for KASAN, KCSAN, and KMSAN, as it is redundant.

Quoted 7 lines

Also add SPDX-License-Identifier for kasan.rst.

---
 Documentation/dev-tools/kasan.rst | 7 +++++--
 Documentation/dev-tools/kcsan.rst | 4 ++--
 Documentation/dev-tools/kmsan.rst | 6 +++---

UBSan also has it: https://docs.kernel.org/dev-tools/ubsan.html

Quoted 52 lines
 3 files changed, 10 insertions(+), 7 deletions(-)
Documentation/dev-tools/kasan.rst +5 -2
index 382818a7197a..858c77fe7dc4 100644
@@ -1,5 +1,8 @@
-The Kernel Address Sanitizer (KASAN)
-====================================
+.. SPDX-License-Identifier: GPL-2.0
+.. Copyright (C) 2023, Google LLC.
+
+Kernel Address Sanitizer (KASAN)
+================================
 
 Overview
 --------
Documentation/dev-tools/kcsan.rst +2 -2
index 3ae866dcc924..94b6802ab0ab 100644
@@ -1,8 +1,8 @@
 .. SPDX-License-Identifier: GPL-2.0
 .. Copyright (C) 2019, Google LLC.
 
-The Kernel Concurrency Sanitizer (KCSAN)
-========================================
+Kernel Concurrency Sanitizer (KCSAN)
+====================================
 
 The Kernel Concurrency Sanitizer (KCSAN) is a dynamic race detector, which
 relies on compile-time instrumentation, and uses a watchpoint-based sampling
Documentation/dev-tools/kmsan.rst +3 -4
index 55fa82212eb2..323eedad53cd 100644
@@ -1,9 +1,9 @@
 .. SPDX-License-Identifier: GPL-2.0
 .. Copyright (C) 2022, Google LLC.
 
-===================================
-The Kernel Memory Sanitizer (KMSAN)
-===================================
+===============================
+Kernel Memory Sanitizer (KMSAN)
+===============================
 
 KMSAN is a dynamic error detector aimed at finding uses of uninitialized
 values. It is based on compiler instrumentation, and is quite similar to the
--

2.25.1

#12 lore raw

Re: [PATCH 5/5] Documentation: *san: drop "the" from article titles

Quoted 10 lines · Marco Elver
Quoted 8 lines · On Fri, 6 Oct 2023 at 17:18, <[email protected]> wrote:

From: Andrey Konovalov <[email protected]>

Drop "the" from the titles of documentation articles for KASAN, KCSAN, and KMSAN, as it is redundant.

Also add SPDX-License-Identifier for kasan.rst.

Quoted 7 lines
Quoted 5 lines
---
 Documentation/dev-tools/kasan.rst | 7 +++++--
 Documentation/dev-tools/kcsan.rst | 4 ++--
 Documentation/dev-tools/kmsan.rst | 6 +++---

UBSan also has it: https://docs.kernel.org/dev-tools/ubsan.html

Ah, right, forgot that one. I'll send a fix up next week.

(Once again I wonder if we should rename UBSAN to KUBSAN. :)

Quoted 1 line

Thanks!