From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795973904; b=2N+zsiFXuXxu7/3hOdPOwhn611Ea0nrzYxIP3JuwQOz5RFjKCMIGvBaxJGdOQtGRxKhI /0yfZDQfxhDKOkuz2U2qlaUqS5Tx5raZdDEbLOFImf4jeAqJRy4X9uGQG/2DYQBr7mEke RLTJSOSmAWqQeo44dl5B9sfJB3Z30vR0iNnZlUmLQb/lAVxsqbGBw1o6XWMRKpVp/kuK+ EIZZrMqTY2E2veIs9r8H6G9wM5n6IiGsC4Utc1RKkEev9aKmo/l3AwrOp7VZKrTQqWyRN fuH6NYuEAmYmDDlfGn90Wzk4AUtWOoUmrqB8Avj/oGlKulW2l4HFDYUCQvFDBsP0cjw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795973904; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References:X-Mailer: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=0BkpJ//pg85/5PkwmT9dRFMAcSx0+NUCLXZNYYaM0ac=; b=ZzIXLQxSvCEzGp9GT1ZpzVqYtrjUjCsVPnAy/Ovt0C5vGyOzPYW+54n47B6nw2K9K34y VXyMiECKkzUAzEcr/0NwMV/I2pJCcz92D2xHiYcDDByaWdyhGTUZV4dcMEpx1TxWTXAZR fxbJ7LjhuOrC9jrX7jSZeKuujVZHgxkAsQJZlsYJ5+Aza3NyfBcDJOMbF2ix1lEB8kDXa d9nSD0AO0WlPaWmRKn5nPLcXFmiMdW4zfasKNmNOaiHmltzskK1kP8nyAstz1vpJ1VN6L JAqe4jpPQv17x8/btQ5taqFTOpf3IaAte44XMw8GL09FUjZXe6HCGRrQHKoJMRrazfA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZYFCQaiC; arc=none smtp.remote-ip=209.85.221.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 510A2606A323 for ; Sun, 29 Nov 2026 17:38:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 510A2606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZYFCQaiC Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4c4b8e58cfeso307622f8f.3 for ; Sun, 29 Nov 2026 09:38:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795973903; x=1796578703; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=0BkpJ//pg85/5PkwmT9dRFMAcSx0+NUCLXZNYYaM0ac=; b=ZYFCQaiC48jbxRp04IyjIPpk3PpFIFUen97fJqpTeiWoAmyQ1IBN+143PxwT1Gd3Ag 6dZyVpV7nycG+Iqt1zx214OVFGfef/s/JN9fmWToSpM7aBxvuQS1nHAn5/YSHO59+NJR aGPqPjHymMI2wA25LB7ld+xwE/eRFuq3u/M+cqoN5KcVPeh/STNrPTofSD+SFACtPX/9 Nic6qNZckLiGJ4iBofKZXdrdzRVgZpdUwY4Ze0yTMyXqqNTK6HYj/M4awx6GhR5HgnZ4 6ypaC7v/1Gnp8fOopspUMioUT72QMSHUX3TmiG2m3mn3urmPVwuZb2lYvjF9CULLQ8jW ZT1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795973903; x=1796578703; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=0BkpJ//pg85/5PkwmT9dRFMAcSx0+NUCLXZNYYaM0ac=; b=PzuhZz7GjPStBYZ4vKOCYkl/OFBV2/+Ho3XRl76sjdb7XA7IwstlWJDyIZAQsJSA3G tmkgAFA3trPB57psMNP9XQK2Z4d5sq6jK88fzutTgNGR6BOacw0zHcHDuHXazMmc+IWe U6YMFjszJl8QU/Q+ZabnDoFnqa+m2HSAk4MwVKDcNPsWPFkoFCodr/O3s89OoRIUjK4l jR4Pu2tmXfQUNUsSWXRnJ4RXJfmRxtpAgglpmp/f0uP1f/gMNscLE8mRird1C2cV1sm7 KRLAgzqUj1Q2EORJAwvKIj8KTG11CXKd89gswp8mCwjMH5FvdS8+JyaeXKQdyRYGC5WQ sgnQ== X-Forwarded-Encrypted: i=1; AL+hErHoE8Aqs/Vgu4HWHJh7c3pjYM2LhstOkCCABAtSNleqgaxuvCc135Z423z5X8+jNpQyMjz8rTEO8XgZHa0=@vger.kernel.org X-Gm-Message-State: APuU4Z9bNcmAvsrkWQA+ZqsxCiYogNmgWZgHmnspNVmg5N5AF2zZMmux MH4KBSronpRhXl5osURzAgwfjqH+3xrCIi59zPF/JJlxkNEn8oDCpWVmysGcpxvs X-Gm-Gg: AZFdTZeMtaNEZOnKUT6d0eZS/kDe29v6w7UwYMcGXaGuuh4ucarWZrGZBkyooEtaynY YXAhzJjY+Qt6WGrayeT3FHKM98fY8nSBy4hXnZDGjYOrK8pwbWtA4oK5R3dI46xBm5VZfDMTHUF bPmSSYfYy9lY2mJ4MVQUY/cm4EvuuwwzC3n6h+Z9EbKieo1yBrVsErGWgyupYvdItBhfXs+cycc 59lyPeYtmdfQcTAODcWWMrzTYg7OrIak/8GZGm6cEXYxlIuRkKPZgasg5s3IvFXCpKYOJofd0+j tInHGQwYkZTlsbRAECZ4G1hq1u9OtVDj0qJ0OioJni9x2B7U9mPGxwlkw30IGCnVfzvl9zx+iQq 2k0LjnNU1brpoxtw2n/6dffjaT0LOmMNsGINP4xgx0tKYaPLcqM1TdCKgPCL3Vi7K8N3rlokYbL nQGbD16LieiAFKVn4Rcq+MZRHtjSuALVVSI8TJS8t9Yzy85VmBlOku+0auLuiqDiTI6PQhc4gOw OXwPHpcmMTypV1F9N9AIZ45jQpyvAkZgoiLL6VeXrqTqt1fXmTq6Jrg4jsMGU2WRR2wTZstTzPI g6jRBDbtbW+GxJFXT47FXFPTjuGl1pFy+uI14NlJS0UoSJscePgDPixjJFayeIiZc7EWrJ5iG4T VJ8PvBPTNjGE= X-Received: by 2002:a05:600c:45d4:b0:4bb:5df4:685b with SMTP id 5b1f17b1804b1-4bb88926da4mr631949495e9.23.1795973902408; Sun, 29 Nov 2026 09:38:22 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6baa3c4dsm50354158f8f.46.2026.11.29.09.38.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 09:38:22 -0800 (PST) Date: Sun, 29 Nov 2026 17:38:16 +0000 From: David Laight 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: <20261129173816.1e27aa56@pumpkin> In-Reply-To: <20261129085612.22266-8-jackwallner@gmail.com> References: <20261129085612.22266-1-jackwallner@gmail.com> <20261129085612.22266-8-jackwallner@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 29 Nov 2026 00:56:09 -0800 Jack Wallner wrote: > 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. scripts/bloat-o-meter will tell you exactly where the changes are. It might be that the code is actually larger in a few places. > > 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 > --- > 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)) How did that happen? Why isn't it just ((x) & ~((a) - 1)) (One of the three versions that were merged when ALIGN_DOWN() was created did do that.) > #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. Too long. > + * > + * 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. Doesn't belong here at all. David > + */ > +#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) >