From: Jack Wallner <jackwallner@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test
Date: Sun, 29 Nov 2026 00:56:10 -0800 [thread overview]
Message-ID: <20261129085612.22266-9-jackwallner@gmail.com> (raw)
In-Reply-To: <20261129085612.22266-1-jackwallner@gmail.com>
Check that the compiler still knows the size of the object behind a
pointer after PTR_ALIGN(), for a member of a stack object and for a
kmalloc() allocation, and that an out of bounds read through the aligned
pointer is caught.
The test fails without "vdso/align: Preserve object size through
PTR_ALIGN()" and is skipped on compilers without
__builtin_dynamic_object_size().
The change and this changelog were written by an AI coding assistant
(Claude, run in Claude Code) at my direction. See the cover letter for
how it was used and tested.
Signed-off-by: Jack Wallner <jackwallner@gmail.com>
---
lib/tests/fortify_kunit.c | 63 +++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/lib/tests/fortify_kunit.c b/lib/tests/fortify_kunit.c
index 413cdbf3d..4df55eed3 100644
--- a/lib/tests/fortify_kunit.c
+++ b/lib/tests/fortify_kunit.c
@@ -43,6 +43,7 @@ void fortify_add_kunit_error(int write);
#include <kunit/device.h>
#include <kunit/test.h>
#include <kunit/test-bug.h>
+#include <linux/align.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/string.h>
@@ -1011,6 +1012,67 @@ static void fortify_test_kmemdup(struct kunit *test)
kfree(copy);
}
+struct fortify_ptr_align {
+ unsigned long bytes_before;
+ char buf[40];
+ unsigned long bytes_after;
+};
+
+/*
+ * PTR_ALIGN() must not hide which object a pointer points into, or the
+ * bounds checking loses track of it.
+ */
+static void fortify_test_ptr_align(struct kunit *test)
+{
+#if !__has_builtin(__builtin_dynamic_object_size)
+ kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n");
+#else
+ struct fortify_ptr_align pad = { };
+ size_t size = unknown_size;
+ size_t offset = 11;
+ char *heap, *up, *copy;
+ size_t avail;
+
+ /* Keep the compiler from knowing the starting offset. */
+ OPTIMIZER_HIDE_VAR(offset);
+
+ /* Into a member of a stack object. */
+ up = PTR_ALIGN(&pad.buf[offset], 8);
+ KUNIT_EXPECT_TRUE(test, IS_ALIGNED((unsigned long)up, 8));
+ KUNIT_EXPECT_LT(test, up - &pad.buf[offset], 8);
+ KUNIT_EXPECT_EQ(test, __struct_size(up),
+ sizeof(pad) - (up - (char *)&pad));
+ /*
+ * Compilers differ in whether they track the member (buf) or only
+ * the object (pad) here, so accept either bound.
+ */
+ KUNIT_EXPECT_GE(test, __member_size(up),
+ sizeof(pad.buf) - (up - pad.buf));
+ KUNIT_EXPECT_LE(test, __member_size(up), __struct_size(up));
+
+ /* Into a heap object. */
+ heap = kzalloc(size, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, heap);
+ up = PTR_ALIGN(heap + offset, 8);
+ avail = size - (up - heap);
+ KUNIT_EXPECT_EQ(test, __struct_size(up), avail);
+
+ /* Reading what is left after aligning is within bounds. */
+ copy = kmemdup(up, avail, GFP_KERNEL);
+ KUNIT_EXPECT_NOT_NULL(test, copy);
+ KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0);
+ kfree(copy);
+
+ /* Out of bounds by 1 byte is caught, and nothing is read. */
+ copy = kmemdup(up, avail + 1, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
+ KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
+ kfree(copy);
+
+ kfree(heap);
+#endif
+}
+
static int fortify_test_init(struct kunit *test)
{
if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE))
@@ -1054,6 +1116,7 @@ static struct kunit_case fortify_test_cases[] = {
KUNIT_CASE(fortify_test_memchr_inv),
KUNIT_CASE(fortify_test_memcmp),
KUNIT_CASE(fortify_test_kmemdup),
+ KUNIT_CASE(fortify_test_ptr_align),
{}
};
--
2.43.0
prev parent reply other threads:[~2026-11-29 8:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 8:56 [PATCH 0/8] Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29 8:56 ` [PATCH 1/8] crypto: gcm, ccm - Derive the rfc4106/rfc4309 IV from the request context Jack Wallner
2026-11-29 8:56 ` [PATCH 2/8] crypto: nitrox, octeontx, octeontx2 - Use ALIGN() on DMA addresses Jack Wallner
2026-11-29 8:56 ` [PATCH 3/8] misc: pci_endpoint_test: " Jack Wallner
2026-11-29 8:56 ` [PATCH 4/8] be2net: Use ALIGN() on a DMA address Jack Wallner
2026-11-29 8:56 ` [PATCH 5/8] octeontx2-pf: " Jack Wallner
2026-11-29 8:56 ` [PATCH 6/8] scsi: be2iscsi: " Jack Wallner
2026-11-29 8:56 ` [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Jack Wallner
2026-11-29 8:56 ` Jack Wallner [this message]
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=20261129085612.22266-9-jackwallner@gmail.com \
--to=jackwallner@gmail.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/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