From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795942603; b=hJq21u2rgrBb+UN5JCPyUvxtlp7+cdJf07Em5qFoItTrkSuh8pQFwq34jFHUOR2YItEz aG/q+8VQJtGAfOHePDOO0LySQtAg7ZzzAu0Nl4/72oZ6Z4mg0z4QDT23Mi8i0/QviX5r2 Y5vhIbhJwEPTyNqnBlxQdjCfl7ZtGmYodNLrDGgGkopoyUkSGpH3/W3o5fg6raYa5W4VQ AflDEZr2YD2oeQ9RyArjeXf16Mxkv7Xh2SlYZ0nSjPZKwrPZcKwVOGlBdRWIbzPz6iQK5 zRevYbknvdatrAcNBsJM02Qh152nLBQZeIIpsHRJK7U6u8OVv3SMLnFa1rTzTcoDO4Q== 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-Forwarded-Encrypted: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=qPKOvtig6/V/VdOwd85hpwjhWDQXT31MOlM3KNWw408V8o4TkFxFl+P2hohDJjp7qNQA Qj9DZMqzr6Q341V0xUkVVDnPlTmVIDjdTwvsiX1LMh29Kj4GcwawcClBHQwvAYN4jo0b+ fM+ihm0CKWqiM1GBguiKguBBd9VjAOfElQ8QUlJPFbuwQLinMQKOrAA9XCmvauQehdmzK L4wFiIcEQ35I1CKU0I82RYfObbJOn4+F4SmqUgvqJc1N6q65WBRiMN4S6FZ3wMU6U3Kri p7YigW878XLh5CbA53RPT2DZFtDWIMiLQGgiBOH7Txi8MyrKZpIoto4P8pBGrc6TzPg== 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.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 D594B606A68E for ; Sun, 29 Nov 2026 08:56:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D594B606A68E 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-f173.google.com with SMTP id d9443c01a7336-2f03eeda984so51433415ad.2 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=kt65EQ+B2ijW0IqQNKJ8Ng7l1LWSsfRljm6ik6xtDHO2AUkDOyI8CpEyvmLKHcpgHu vtA6UxtT5scb4aVUgHQE1Rl3nZbs1Q16BkOnXemGUgFvC4BoWRcwcP9iQEEp1+pEXDTY 8hxSoKZ5UlDkOZsujzBcnQJP1mqaLqSijYBN0/hWntEeWeFhjd92SRLKLx7nIxnWfeSm 0HNK2YnaSgy6JD6vLUaWAgqmBlxUfcL/uIAPl8G4TpyZJr/5w+ZDWNKL2GPCc5E2ocuN ISNVjGIQbxGYiIwxC4l5RgxJNEOK8suZst2oQRc2jbTLfJhQZEPI8N95sAVr4A8M/7h8 zsow== X-Forwarded-Encrypted: i=1; AL+hErG72y16gv9DiUg6xcm9fSFaOX5RueA95RSPMzLpszqZyAFLsfxsc2Btzr8pVW8yBUkVE9qrQnX1TdciWE0=@vger.kernel.org X-Gm-Message-State: APuU4Z/x9zf7cD8iC6D/bsuSqHsklqIE6Rossv+VpluVj+Zuj8h/PlVE /kgyFLmciOliynkulasRN/Fvzp1aAsnjBWM4IiZNEhQqFQiuTKx5WKU6 X-Gm-Gg: AZFdTZdkQ3BSXKw8V5zJiJ0dvflsTKZn9ZD2hjcdsZLE5G9Ef1oIV0f7doCojG7kDUF e0VaXZNGJC9IJYO+M023lciVbmsm/koPaZpS8OysRkaiDmah+QXgLzGHj8G0WdCx5O9Hdg4LGoM JNbLNTFXZJVrv30RumKnJTG30K+t3fOXghWZna1rz0TJoeWGsP0Vk+g1qGOdmEV5VHj/sdHxhKW b4PFpDm+D8XeGu3fFao5D6P1r0SIa0Cf0K4Knt0uwcYnkjk7p0HOBS31JGYXfFcZ4xLhpCqinvF OhptOmwENJ8oMw14qwWnNqK2YKP4+pkiymqySaQpphjw6rtaCWiCqCef5QzmbSZxpzjRAE9gKFV oCvkELNnrfdCr4/mjA5jOQIMFNuVa+DJBnLzrEHsMQvTbeEGxIMGhZAktqERXjfn1U7TVV7HBQ3 pX4S6n4zTAci9JlGBDqqwJSIV1ZPv9Mlh+7VIzXI87NRbqtDofW/pcVDmYpThas4ZtmpoDUjH5T aOypsRdHCma0FdhYEi8nJ153SfksIqdvVWcMCmi8TZwlZL1jophJVGa6xokWT0Uv9bldPcrUKJx lcCxLd7/5BtVG1YVnCwoHO0d+Ex5mq9zANCJr7lHRxCbvZyf975jluDhEVQCE0+QJYYoh4QrUrd ZnVXguYRSs0i2a6oyODpFPFjCcoodnP1St+a7Whz3bu3IcpcfTTt3HIz8sfcmOgpyI7FARfPysk Buqfpo 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-kernel@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