[PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test
From: Jack Wallner
Date: Sun Nov 29 2026 - 03:57:27 EST
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@xxxxxxxxx>
---
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