Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795942603; b=Jin6Hp0HWwTavFhf3eWjxvcM/CTBh/HYiL5e0Kk83F50niNr3i4PC+ugr7JTru5GeTz+ +zXmHxqU0eLObmUnUf2J1v3J/w80tbpa/rVUNVEClue+spLsR4eLJNQ/nsPj4y82Puk8B t0fxJcOfmgoXRqBDjxulR3Lwo+MUBoCC3Zcii+/fA0w3oXqAGWVoB96L0WQ8ke6iESh+p tdAC8DVmQuL2Xaga2fMAYe8Od0WuUxfGt9Kh3zJE+hgVeVEkNpUV5lN413wo93sDqn/et aeV+dNDKlVR/Sd5xq8qhFnLpXzdAqIMdAeDncRrx1Rcl2H0i3KYDOVBn9xRUtXRj0qw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795942603; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=KAMDi626+eths51iqxbZxok/KdMHBqw0c9+jtXXdXaM=; b=ZLrSNenJVCdELMbGz9xs1vpGNCXFAa02AhN3lBPQAS3E0RBZUROkmM+dupdttiVCEkmY V1UT29IlmfxT+zdfhFUwkS1+x2KhWs6kwplktgS8HAL6DWD3mOvLr3ch5nRB/L+o3l/Mc hAVWiAA4lKlqQRkpMyUtmYzMJCHK2iTg9P/DPm99Ve4jlflOv0FoW/mgIWtwjBZiRyFyA VaOleKwoUnl4fTDk60VvALolhRQr30sYL5e35bhxcrpacKKmwyCrlEy13EyBkmWxOVZY7 bT/XZ5dBgNjSujfc5MrxpChwer3rI+QIaoKQ9la1dLSN11Rl38iGIdkoW32ZazgS/GA== 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=en2JbNGt; arc=none smtp.remote-ip=209.85.214.178 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 E46A3606A68F for ; Sun, 29 Nov 2026 08:56:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E46A3606A68F 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=en2JbNGt Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2f0458cc9easo73874275ad.3 for ; Sun, 29 Nov 2026 00:56:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795942602; x=1796547402; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KAMDi626+eths51iqxbZxok/KdMHBqw0c9+jtXXdXaM=; b=en2JbNGtLmDnih23XtfcNhFa6xslA/bVgTFGhUdbFhrOlIBDT3afYunzBe9aYG2quU +OO9ckMOUJencXnMDLfzlS98grV00QkfdlJq5hlZGe1eCTNME540OuT18SnmnFG21Cye XXXb+X2picRvc7jKy0R1ljU4+quWhKT9mXH8fWHKYO347864n7koURbbsc7WJSqLka/m uD71Cx1mp7OOU6/UXlj5/MrpQ7TcFaDTsbLPYVaVkBmOqqdtjJQsNdECJI0ISw6z48SU EhkoNFZ1NOWhqLLEUD8G7yiZZnNTIAsbItOGg93LYUK6flUvziWly1zeJ1sSQRqbR1aW Cxiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795942602; x=1796547402; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=KAMDi626+eths51iqxbZxok/KdMHBqw0c9+jtXXdXaM=; b=NHL8GUYh6I2yMQW0u09jQr36sMOn+ArfoqIitqEGKBOi5QZVK8FixiFjyAdBxG6byC Ce2fxuij/rVHfFe1USAFH7gs9qtcd8DRcU2L4Ljs0jfpNKu0I13NCO48HctE02Tlc3DQ uwOQ5peWAbpES+6R6stjZObV+2HnkPErZce4ysH6cRx7shsdr3O2OBEl5yslqsu+4+cA M5LvESMrCnBUZobhws4Gl6qwrC8oL4Xik5rpvT6LCeweiCz3CkMoglZMCeKaO5ucM0Ky gjIMoAUzS/0IMc0HJYqRV5NC536ixK/U3DhkpoDwp0mocGhyl14+1kJk/+0KOUpMPasW rT8A== X-Gm-Message-State: APuU4Z/EjFy+8sEXVTHZGwD3lCJ5x0lIKa3ZZ/uwTIAekjAX8glYffU6 0oZzB4t59qHrN1Z6XpsdnGg5jBBg10dYkz5uVn5ZyTG3cfESMb42gKf1 X-Gm-Gg: AZFdTZeeSqPsh1jS+NW0R1yPbkcrOgirMiqN+1zrQ2K+ZYaXv3jBK+MJGtF8Ri+uRfx Y4rSA1iOVhrd31+JNo3S0pHD/06kpFbGDmkvm6LqgSBuzWj09kjxuagg29wrmqC5Cu0k0HtuOHQ nftOzfX9lce/joruaYdMwc8HpdODZsgc9VIQSy1hxxqRo1WSsvh7F0d/QR/iWxDQDukN8pTGkGB X9rcB1c8FcNH87P3JccqRZl6yheC+KY/Aq4sh01YqYUy33IKXEuAQJBf4JhU9QeY568ltlKgtys Xv4SblPBfW6Hvj/x/VdEMHqj3Ev4Lit16B/utjfPVl11PbxTG2K/t9bFPU+WtJPwPdu+dqrrd+/ CjLMD98ff4jnRbRDcqUAquh/UNM3YsTx302yjfYxCVXeGK8vtyhkYEqZ4JGf4XbshIH4JwAU7WP +FtnyWgVgfM+nA6CwSK2J1xf1vIkmYiIfRUduSoe+8KRw/mJuDAr0Y+NiSKekAYCoPGhAkI/IAO dSYhcIgCunSGTa2bVhEoNMxSe+aYSkYyk9Zt/BlWGUUpFo7nSY0RZmXKLWyeUPqr/P5/zo1rMZT gZfdzxn3/enEPP4FDA9YnQNoYin1kFOWxhtHiZk5jdg0OClw32v8hM4bIu7eAH6u6Kr0e7CXcHd UeMgigOoiyULQws6zoRCTgsmLxjzT6jv8M9xDvhPPbmnb7Cyqo/jCh/LsEWyNO2bF8o5B3HQb9Z rrBHkM X-Received: by 2002:a17:903:32d0:b0:2f1:6bb:55e4 with SMTP id d9443c01a7336-2f106bb7b12mr102602115ad.44.1795942602181; Sun, 29 Nov 2026 00:56:42 -0800 (PST) Received: from localhost.localdomain ([2601:1c2:4100:453:29cf:2f71:51d4:4a57]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f0c2091sm144767735ad.20.2026.11.29.00.56.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 00:56:41 -0800 (PST) From: Jack Wallner To: Kees Cook Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Lutomirski , Thomas Gleixner , Vincenzo Frascino Subject: [PATCH 7/8] vdso/align: Preserve object size through PTR_ALIGN() Date: Sun, 29 Nov 2026 00:56:09 -0800 Message-ID: <20261129085612.22266-8-jackwallner@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261129085612.22266-1-jackwallner@gmail.com> References: <20261129085612.22266-1-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-Transfer-Encoding: 8bit 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 --- 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