From: Alejandro Colomar <alx+linux-hardening@kernel.org>
To: Jack Wallner <jackwallner@gmail.com>
Cc: Kees Cook <kees@kernel.org>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
Andy Lutomirski <luto@kernel.org>,
Thomas Gleixner <tglx@kernel.org>,
Vincenzo Frascino <vincenzo.frascino@arm.com>
Subject: Re: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN()
Date: Sun, 29 Nov 2026 12:03:45 +0100 [thread overview]
Message-ID: <awwD4Mnb1qtKSfHx@devuan> (raw)
In-Reply-To: <20261129085612.22266-8-jackwallner@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 6835 bytes --]
Hi Jack,
> Date: 2026-11-29 00:56:09-0800
> From: Jack Wallner <jackwallner@gmail.com>
>
> 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@gmail.com>
> ---
> 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); \
This 'typeof(p)' could be replaced with just 'auto', right?
> + unsigned long __ptr_align_v = (unsigned long)__ptr_align_p; \
> + \
> + (__force typeof(p))((void __force *)__ptr_align_p + \
The attribute makes more sense before the type, not in between.
I see both uses already existing in the kernel, but attributes shouldn't
go there.
(__force void *)
Have a lovely day!
Alex
> + (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
>
>
--
<https://www.alejandro-colomar.es>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2026-11-29 11:03 UTC|newest]
Thread overview: 17+ 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-30 4:14 ` Ratheesh Kannoth
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 11:03 ` Alejandro Colomar [this message]
2026-11-29 17:32 ` Jack Wallner
2026-11-29 17:38 ` David Laight
2026-11-29 20:47 ` Jack Wallner
2026-11-30 10:46 ` David Laight
2026-11-30 19:40 ` Jack Wallner
2026-11-29 8:56 ` [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test Jack Wallner
2026-11-29 9:03 ` [PATCH 0/8] Preserve object size through PTR_ALIGN() sashiko-bot
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=awwD4Mnb1qtKSfHx@devuan \
--to=alx+linux-hardening@kernel.org \
--cc=jackwallner@gmail.com \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=tglx@kernel.org \
--cc=vincenzo.frascino@arm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.