[PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN()
From: Jack Wallner
Date: Sun Nov 29 2026 - 03:57:24 EST
PTR_ALIGN() rounds a pointer up by casting it to unsigned long, aligning
the integer and casting the result back. Compilers do not track which
object a pointer points into across that round trip, so
__builtin_object_size() and __builtin_dynamic_object_size() report
"unknown" for the result. FORTIFY_SOURCE then cannot bound a memcpy() or
similar call made through an aligned pointer.
Compute only the padding as an integer and add it to the original
pointer instead. The resulting address is the same, and both GCC and
Clang keep the object size. With
char *heap = kmalloc(200, GFP_KERNEL);
char *p = PTR_ALIGN(heap + 3, 64);
__builtin_dynamic_object_size(p, 0) is SIZE_MAX before this patch and
the number of bytes left in the allocation after it (136 if kmalloc()
returned a 64 byte aligned address), on GCC 13.3 and on Clang 18. The
same holds for pointers into stack objects and into struct members.
The argument has to be evaluated once and used twice, so PTR_ALIGN()
becomes a statement expression. Every current user is inside a function.
The casts use __force so that sparse stays quiet for the callers that
align __user and __iomem pointers.
The alignment itself compiles to the same instructions as before: at
-O2, GCC 13.3 on x86-64 and Clang 18 on x86-64, i386, arm, arm64,
riscv64 and powerpc64le emit identical code for the old and the new
form. In allmodconfig builds the text of the roughly 100 objects that
use PTR_ALIGN() grows by 0.09 to 0.15 percent in total on x86-64, i386,
arm64 and powerpc and by 0.35 to 0.5 percent on arm and riscv, the new
test case included. I attribute that to the compiler now carrying an
object size to more call sites.
PTR_ALIGN_DOWN() is left alone on purpose. Rounding down commonly gives
a pointer to before the start of the object the argument points into,
such as the start of the page holding it, and that pointer must not be
bounds checked against the smaller object. It has 25 users, most of them
page table and page start arithmetic, and can be looked at separately.
One thing does change for callers. Where GCC can see that the argument
points into a member of a structure, the result is now bounded by that
member as well (__builtin_dynamic_object_size(p, 1)), and FORTIFY_SOURCE
warns about a memcpy() through it that runs past the end of the member.
Two callers in crypto/ stepped past a member on purpose. The first patch
of this series changes them.
The same goes for Clang's -fsanitize=local-bounds
(CONFIG_UBSAN_LOCAL_BOUNDS, which UBSAN_BOUNDS with UBSAN_TRAP enables):
an access through the result into an object whose size Clang can see is
now bounds checked, where the cast used to hide it. A kernel built with
Clang 18, UBSAN_BOUNDS and UBSAN_TRAP passes the fortify KUnit suite and
the crypto self-tests with and without this series. I did not test that
configuration beyond that.
To find the callers that could get a new FORTIFY report I had the
compiler list them. I built allmodconfig with a temporary PTR_ALIGN()
that calls a function declared with __attribute__((warning)) whenever
__builtin_dynamic_object_size() of the result is known, which reports
each call site after inlining. That covered the C files that use
PTR_ALIGN() and the directories that reach it through header helpers
(crypto/, lib/crypto/, drivers/crypto/, block/, drivers/md/, fs/crypto/,
security/keys/ and the net and driver directories that use those
helpers), with GCC 13.3 for x86-64, i386 and arm64 and with Clang 18 for
x86-64 and arm64. Code that builds only for other architectures is not
covered.
A size is visible at 69 call sites. At 62 of them it is the size of the
whole allocation or stack buffer being aligned, so a report there would
be a real overflow. At 7, with GCC only, it is the size of a struct
member: the two crypto callers above, and drivers/dma/ste_dma40.c (2),
drivers/i2c/busses/i2c-ismt.c (2) and
drivers/net/ethernet/broadcom/bnxt/bnxt.c (1), where the member is the
buffer being aligned. Of those three drivers only i2c-ismt.c passes the
result to a fortified function, and its copies fit: at most 32 bytes at
offset 1 of a buffer with at least 33 bytes left after alignment.
Link: https://github.com/KSPP/linux/issues/191
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>
---
include/vdso/align.h | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/include/vdso/align.h b/include/vdso/align.h
index 02dd8626b..0b9fbac98 100644
--- a/include/vdso/align.h
+++ b/include/vdso/align.h
@@ -8,7 +8,28 @@
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))
#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
-#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
+
+/*
+ * Only the distance to the boundary is computed as an integer. Applying it
+ * to the original pointer, instead of casting the aligned integer back to a
+ * pointer, lets the compiler keep track of the object being pointed into, so
+ * __builtin_object_size() and __builtin_dynamic_object_size() still work on
+ * the result. @p must be a pointer: use ALIGN() for integers such as
+ * dma_addr_t.
+ *
+ * PTR_ALIGN_DOWN() keeps the integer round trip on purpose: its result often
+ * points before the start of the object that @p points into (the start of
+ * the page holding it, for example), which must not be bounds checked
+ * against that object.
+ */
+#define PTR_ALIGN(p, a) \
+({ \
+ typeof(p) __ptr_align_p = (p); \
+ unsigned long __ptr_align_v = (unsigned long)__ptr_align_p; \
+ \
+ (__force typeof(p))((void __force *)__ptr_align_p + \
+ (ALIGN(__ptr_align_v, (a)) - __ptr_align_v)); \
+})
#define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
--
2.43.0