linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Ethan Graham <ethan.w.s.graham@gmail.com>
To: ethangraham@google.com, glider@google.com
Cc: andreyknvl@gmail.com, brendan.higgins@linux.dev,
	davidgow@google.com, dvyukov@google.com, jannh@google.com,
	elver@google.com, rmoar@google.com, shuah@kernel.org,
	tarasmadan@google.com, kasan-dev@googlegroups.com,
	kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, dhowells@redhat.com, lukas@wunner.de,
	ignat@cloudflare.com, herbert@gondor.apana.org.au,
	davem@davemloft.net, linux-crypto@vger.kernel.org
Subject: [PATCH v2 RFC 1/7] mm/kasan: implement kasan_poison_range
Date: Mon,  1 Sep 2025 16:42:06 +0000	[thread overview]
Message-ID: <20250901164212.460229-2-ethan.w.s.graham@gmail.com> (raw)
In-Reply-To: <20250901164212.460229-1-ethan.w.s.graham@gmail.com>

From: Ethan Graham <ethangraham@google.com>

Introduce a new helper function, kasan_poison_range(), to encapsulate
the logic for poisoning an arbitrary memory range of a given size, and
expose it publically in <include/linux/kasan.h>.

This is a preparatory change for the upcoming KFuzzTest patches, which
requires the ability to poison the inter-region padding in its input
buffers.

No functional change to any other subsystem is intended by this commit.

Signed-off-by: Ethan Graham <ethangraham@google.com>
---
 include/linux/kasan.h | 16 ++++++++++++++++
 mm/kasan/shadow.c     | 31 +++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 890011071f2b..09baeb6c9f4d 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -102,6 +102,21 @@ static inline bool kasan_has_integrated_init(void)
 }
 
 #ifdef CONFIG_KASAN
+
+/**
+ * kasan_poison_range - poison the memory range [start, start + size)
+ *
+ * The exact behavior is subject to alignment with KASAN_GRANULE_SIZE, defined
+ * in <mm/kasan/kasan.h>.
+ *
+ * - If @start is unaligned, the initial partial granule at the beginning
+ *	of the range is only poisoned if CONFIG_KASAN_GENERIC is enabled.
+ * - The poisoning of the range only extends up to the last full granule before
+ *	the end of the range. Any remaining bytes in a final partial granule are
+ *	ignored.
+ */
+void kasan_poison_range(const void *start, size_t size);
+
 void __kasan_unpoison_range(const void *addr, size_t size);
 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
 {
@@ -402,6 +417,7 @@ static __always_inline bool kasan_check_byte(const void *addr)
 
 #else /* CONFIG_KASAN */
 
+static inline void kasan_poison_range(const void *start, size_t size) {}
 static inline void kasan_unpoison_range(const void *address, size_t size) {}
 static inline void kasan_poison_pages(struct page *page, unsigned int order,
 				      bool init) {}
diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
index d2c70cd2afb1..a1b6bfb35f07 100644
--- a/mm/kasan/shadow.c
+++ b/mm/kasan/shadow.c
@@ -147,6 +147,37 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 }
 EXPORT_SYMBOL_GPL(kasan_poison);
 
+void kasan_poison_range(const void *start, size_t size)
+{
+	void *end = (char *)start + size;
+	uintptr_t start_addr = (uintptr_t)start;
+	uintptr_t head_granule_start;
+	uintptr_t poison_body_start;
+	uintptr_t poison_body_end;
+	size_t head_prefix_size;
+	uintptr_t end_addr;
+
+	end_addr = ALIGN_DOWN((uintptr_t)end, KASAN_GRANULE_SIZE);
+	if (start_addr >= end_addr)
+		return;
+
+	head_granule_start = ALIGN_DOWN(start_addr, KASAN_GRANULE_SIZE);
+	head_prefix_size = start_addr - head_granule_start;
+
+	if (IS_ENABLED(CONFIG_KASAN_GENERIC) && head_prefix_size > 0)
+		kasan_poison_last_granule((void *)head_granule_start,
+					  head_prefix_size);
+
+	poison_body_start = ALIGN(start_addr, KASAN_GRANULE_SIZE);
+	poison_body_end = ALIGN_DOWN(end_addr, KASAN_GRANULE_SIZE);
+
+	if (poison_body_start < poison_body_end)
+		kasan_poison((void *)poison_body_start,
+			     poison_body_end - poison_body_start,
+			     KASAN_SLAB_REDZONE, false);
+}
+EXPORT_SYMBOL(kasan_poison_range);
+
 #ifdef CONFIG_KASAN_GENERIC
 void kasan_poison_last_granule(const void *addr, size_t size)
 {
-- 
2.51.0.318.gd7df087d1a-goog



  reply	other threads:[~2025-09-01 16:43 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 16:42 [PATCH v2 RFC 0/7] KFuzzTest: a new kernel fuzzing framework Ethan Graham
2025-09-01 16:42 ` Ethan Graham [this message]
2025-09-05  8:32   ` [PATCH v2 RFC 1/7] mm/kasan: implement kasan_poison_range Alexander Potapenko
2025-09-05  8:46     ` Ethan Graham
2025-09-05  9:32       ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 2/7] kfuzztest: add user-facing API and data structures Ethan Graham
2025-09-02 10:37   ` Marco Elver
2025-09-03  8:40   ` Alexander Potapenko
2025-09-03 10:15   ` Alexander Potapenko
2025-09-03 11:35   ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 3/7] kfuzztest: implement core module and input processing Ethan Graham
2025-09-03  9:53   ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 4/7] tools: add kfuzztest-bridge utility Ethan Graham
2025-09-03 14:07   ` Alexander Potapenko
2025-09-05 10:43   ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 5/7] kfuzztest: add ReST documentation Ethan Graham
2025-09-04  8:53   ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 6/7] kfuzztest: add KFuzzTest sample fuzz targets Ethan Graham
2025-09-04  9:59   ` Alexander Potapenko
2025-09-01 16:42 ` [PATCH v2 RFC 7/7] crypto: implement KFuzzTest targets for PKCS7 and RSA parsing Ethan Graham
2025-09-03  8:58   ` Ignat Korchagin
2025-09-04 20:20     ` Ethan Graham
2025-09-04  9:11 ` [PATCH v2 RFC 0/7] KFuzzTest: a new kernel fuzzing framework David Gow
2025-09-04 20:08   ` Ethan Graham
2025-09-08 13:11 ` Johannes Berg
2025-09-10 10:40   ` Alexander Potapenko
2025-09-10 15:59     ` Johannes Berg
2025-09-11 13:59       ` Johannes Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250901164212.460229-2-ethan.w.s.graham@gmail.com \
    --to=ethan.w.s.graham@gmail.com \
    --cc=andreyknvl@gmail.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davem@davemloft.net \
    --cc=davidgow@google.com \
    --cc=dhowells@redhat.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=ethangraham@google.com \
    --cc=glider@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=ignat@cloudflare.com \
    --cc=jannh@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lukas@wunner.de \
    --cc=rmoar@google.com \
    --cc=shuah@kernel.org \
    --cc=tarasmadan@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox