Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795950230; b=n26Cv46n87ywecS7taRv8rhUr8sFlGKwxbK3QqIDZvcnIBLvPERYK/Ey3D7zLr/0Pw3J fUpRziKFriHMq/aDnVjj5O+By35M6LiM50/DEVHPuwrK0TKyD8wSodhyNEfph8XG+ViPA fHJjDv8Fb7fj8Wap/gYVp7vF0IY2xZm4m5Arq0vU6474clgk1Bs1U/6FoEygPRAEzXpHs qxqr4wGietnfjy4vIvt4mZWCPoe7iaUpcFPCNT/9KgDYJBDE/szQGWod4bJp/Hto3t7JR rv6DceyQ5bUSfKDW6/qzMuZcouN8M9VZki1iWRDP2J42/zK+nBeoY3+OYav0tgOBCHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795950230; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=b4L6CkyrEmpLkapu1rilKx3rLni41kTtMNt9LUbY1k8=; b=Oo/pZdU5eH3JSNkfKUXVzncolsZhK8/nviasYHUr0SWE6jvtBFpRp5pMcsirSGq0NO8h yVAvivXqi3p5oLAgYNeSrZSxK982f8EMovB+bx62RED5diMp15KaUUAJfgQ74Pmd6mu5B JaDojYU6+HYf/6zQMK50OEpuOorg4OUqJKGHPW9bzVZMlhU3IT9HqtS/mQ6Uu9CipxNUh +r0VQkWIx8XA6DBQMkf1K1fHJ4XnlU85uTQqGbRhJs6nMfgk+tMvKOafxp81kLNJ6Rgc4 AoMFZom9NxTAWJErJnLFxnvpS+1btaZ2yHLKhYhUocbuWHZ6YeaFZPfhnyX1o6vkfGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mJFILKpk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5885F606A31C; Sun, 29 Nov 2026 11:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=mJFILKpk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5987F1F00897; Sun, 29 Nov 2026 11:03:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795950229; bh=b4L6CkyrEmpLkapu1rilKx3rLni41kTtMNt9LUbY1k8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=mJFILKpktHi2Qbhzk6V0Md7++0vdobtBsc2k0ziHWqGyzMwt4tBdm8rYfHZa0efKc Z0BNwzmXbqN87N+Bll/UxFSn1T4wre6PGHB0V2Ng9fVXDbHd+7fNNJVBcJVwNbwER3 bJNZcrT2Pphzzlt3BaU09avn4mMfdVKAokXR8qi4fZI3japKEWyqqc5UzYs6dVN+r/ 2p5ZZF7qlsJUVElKhVWWEJG3mpAV+lMX2hnGtlOnaGJchJpotgbaVnojbpodRjIrZm 4c8wT2D5yRreWy0o9Up7eYGMhGrfgWFfanq0vVl72GOohxeQGecb2RqFI+TjbrzVRt QToyK10t2WFYQ== Date: Sun, 29 Nov 2026 12:03:45 +0100 From: Alejandro Colomar To: Jack Wallner Cc: Kees Cook , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Thomas Gleixner , Vincenzo Frascino Subject: Re: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Message-ID: References: <20261129085612.22266-1-jackwallner@gmail.com> <20261129085612.22266-8-jackwallner@gmail.com> X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="kyhn5sud5zmscrzx" Content-Disposition: inline In-Reply-To: <20261129085612.22266-8-jackwallner@gmail.com> --kyhn5sud5zmscrzx Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable From: Alejandro Colomar To: Jack Wallner Cc: Kees Cook , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Thomas Gleixner , Vincenzo Frascino Subject: Re: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Message-ID: References: <20261129085612.22266-1-jackwallner@gmail.com> <20261129085612.22266-8-jackwallner@gmail.com> MIME-Version: 1.0 In-Reply-To: <20261129085612.22266-8-jackwallner@gmail.com> Hi Jack, > Date: 2026-11-29 00:56:09-0800 > From: Jack Wallner > > 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. >=20 > 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 >=20 > char *heap =3D kmalloc(200, GFP_KERNEL); > char *p =3D PTR_ALIGN(heap + 3, 64); >=20 > __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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > The same goes for Clang's -fsanitize=3Dlocal-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. >=20 > 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. >=20 > 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. >=20 > Link: https://github.com/KSPP/linux/issues/191 >=20 > 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. >=20 > Signed-off-by: Jack Wallner > --- > include/vdso/align.h | 23 ++++++++++++++++++++++- > 1 file changed, 22 insertions(+), 1 deletion(-) >=20 > 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 int= o, so > + * __builtin_object_size() and __builtin_dynamic_object_size() still wor= k 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 =3D (p); \ This 'typeof(p)' could be replaced with just 'auto', right? > + unsigned long __ptr_align_v =3D (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)) =3D=3D 0) > =20 > --=20 > 2.43.0 >=20 >=20 --=20 --kyhn5sud5zmscrzx Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmsMBoYACgkQ64mZXMKQ wqksoQ//aq3mQGnG5V5YlxFwdkMuEY1uZiWAo4i+PXWy9+pAYEs34cQAWg+7wKQA GIa0VZo3yLrjkwowEGmQaM1b+nn1Nys/zJMirR6hDKnLRQHjFowsKFX7wl1GYA3I HlgVrUGkGlgT5ZkVW3zkTETOUoAWTn7VrlyQZm0Uqac13SNvcTGJ1OoYyfs+ygSn Bx0o0a+1GE/6uxoY5TX3lPCdN2t7sHQOZNxnk+1/7ofFh2lHMYcUVTmxeEPavoCu LK7slgdScRH46Zk1s+cQ1ub2dswP6k/qz/r0LQMBzVBi168kPAAYaI32Hoj0KUZj XXDLLFv4JXpbwCHdammsfzvxzPEXDRSTEyaft13VXkk8DI13rMqnzoRAUD5beMCE PnyltPKeWYFo1V+/m0vGf/HAF3UsgMF+hEdR3AFtAprMRogVsqXhd9udjiLshf7/ 6gy6Df440bMGBuv/8GyWfDXFIC8pN1kYcrezXuIFcw90OdavwySlNRIsnkPAMDqp zkQCmqmpAwDmDKbN6k0xqs8N/x4rx3Q5IdLPbbj7EkAuQ1FvXdkX8By3Diht6q4Y ofSlvXyKEoYhbR/mF8Xd9Z6eNLTPx48yF/avYDsscu2Ny7pJzwpcimCo3NepsEWb eH+oOlc71y3tBaW4PaqbPjrgYYVOhUWv8su2DcBoE8KigeriiKg= =ciqD -----END PGP SIGNATURE----- --kyhn5sud5zmscrzx--