From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795942604; b=MPUi4tvcaQZMGTGPq86iyaRUkVRRJISObWaK78hEPZJk136We6XBfGlln6hdtK70r83p XqYzte6xYpm77Fy2u9IkYtXJm5t6GxarnJrgpbOSBUCI03hzHwnTY8n9IRbmQVWR+XDyM h087xpLeo4iwT7cYDM11JQ3icAEYR9K2Jaswxx0KxUPR4NI38x7O/HtpeiBce8fEmNASN i/lWK1tLub1Q1CQyyDXutnTjat/b2g8nrm6/FwRyjMkzKxQ5m6g16rJPbnyrePNMedGkz TkvAuBLRLCMC2l7HeotbsH/HasqZJwkhPxaiYKe10hocJk2U1w424hCXpgWPXkRfU5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795942604; 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=r8pYpsi3UX6NwFKbGdQmObtdpLJkj3oVh1o9pKvLrZ8=; b=sKG/MQOUYa1kiprODF1+NmkFh+gJikVl0pomL2bEJV/W4YydVY77BEuWrmVrH5NGnMdb dD3p1Y7E5wZ83i5e0GPlqOqyqZL37geaBkbtVFtiUMVpPjheHSsdRkdQ9mUMYJhaTz4EL PezJ6GRzWfY9eiJ8M9nLWO+Sd5j1v9nANUHED9FZNKM6vbZLvcM6JnZfHQjTOMnB4DJjJ JEhoSJXv61yYm/WBapwtmn0qUAMrXeAzk33L6fWbPdOWZlXuqC2XYiS60pqD5Tu2IK8w7 MWl/yOzc/y9MSrFoVFp3nidssC8Ylq0OioUUCnQ5ut5P+0u/wIab6eTdMUv5ygeASNw== 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=krINuobJ; arc=none smtp.remote-ip=209.85.214.172 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 204A2606A692 for ; Sun, 29 Nov 2026 08:56:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 204A2606A692 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=krINuobJ Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2f04244c3d0so73897715ad.3 for ; Sun, 29 Nov 2026 00:56:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795942604; x=1796547404; 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=r8pYpsi3UX6NwFKbGdQmObtdpLJkj3oVh1o9pKvLrZ8=; b=krINuobJbEeL/FuK/uUc+ugglLEooXcQBmR/cCGiKouNLMJ1zgD96BLKwpnjptrLZw ZVEsmmO/95kiyHscGnfFEfaWWjtaKKqegBxtNL4tK68jZ9nU6kFdHXjkieRWP1oFQbn3 Md2a4wCEu/K7rz72R/fk8+tSEuSzCA1dbNlAenDBG9kE9+RRNeFLqUa91nqgAI+cHRG8 cYIU95gn/CdA0yZFQvXszXz2DrszA1w6VR8FjtwTunkUaOEbsjiIu7JZFd1AkV/a4noY Zh4JbWgisAiU/fZTMMIHigvxM+j3g1KNie6HykS+dJZF/VdmRktmvARAPTsiXLciyiyP B19g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795942604; x=1796547404; 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=r8pYpsi3UX6NwFKbGdQmObtdpLJkj3oVh1o9pKvLrZ8=; b=fynq6qOabtQr8hGlb5N3pOIvVYSmgPjNIGT0w+uvea/WFv1fZuvV+puD/ABm2DOk5k Ik84j4+zWp+HidvuMAva94i+dvWa5pL0lPwt5EeQya7BqB255kiJ29ZTqtpvoZUz3JsB 5WIAO7vUp8R/fLRyWePX6TrAyN4qpPeyhNgTmZq7W4d+Ibsw+fBqHhgAINuq/5uDjesm /vKFKx1v9J7SW43N3cQxFcc2cL+QQtJCZif/A/8MxPrdZUpBa3IwJ/hYLCFYIOVA/liT R8+dAdv7xQzVeiMveN66yz2bs2MZ+RR5Yf2TQnIbcsveicyB6PSRWPoodNoGjuQvqRCw f20g== X-Forwarded-Encrypted: i=1; AL+hErG8JajBH0FmfhzJLUWiig6bgJr3sAsI8qC+wPyalsGBmFoaA+CoLcBeARaG5ChCj51rtpBE1LHeZpN5c3c=@vger.kernel.org X-Gm-Message-State: APuU4Z+rxSg2nYavwvJ9UR7H+H9xP993ZDY6eSlA7Yxm8prGjYH0TOlg ATX91bTHOTFEhcF9aIjzVvEUPrR3MVA41Y3leuTqHtDDdyVXwVsYGWvd X-Gm-Gg: AZFdTZdWjMpfLzlmG4krSCzzH64sV3bdAtiJraLWT4elaVNjFT9ntZgGXRyVMrjPOuZ oniMhhjCypAUSmBeTZwYjBH6GxgHzQkiupHstenpxbUgSONoTXjfwfg8KsKfTLd1Lmv6sB8QGWf YKYmykZrFyny4GQQnj/T6h7mF6FAF6Q8xCy2aParn98Dj2cpMOyc9WyqMFydHEvhd7pCptoA7XZ KxDAMBe4+y584OrWn5dvAlPRLsa5tqJZbKaJxXU+knmsdg4eYfKkvPbzHyY7qJ+wzuieME31ZU3 E/8P9iKjhESpnSBY0L785+VPiQidvvr+HHZTbNeEA/6YCfCLTnCuobhMtdiBhnzGptLi0msIZiX pr+8z54GUjbr/SU3QMye0lz9iBNMxjh1fuUBfBcSfMMryM7YFrVhaxRCKUGHqHg+ivZVWLAaqQe tyFEctnNbtYXlzU4iP5SrWIUjtEDcjdDAUtJowR/1yJ+inKRQATLALX5M5y2D8S3I+xHPaeVLOV ZLBGO1LqhATyV5lY2buoSh3Mn8t7n2DnJ8fCv5bYydDS4pvpjztyEb26RcBCSt7L/XYZ/8TxyQb YMC50/7R0Eaq2C8K4GzH2n3qMMXfX5ijU9PgTI4biA4i1BlBnNuzpAk1Y+qjll+9LbIwqKdkWc2 R0bHcJPRc1B6iknidTSio4w60oglJMaSJDcptbBuJ43/0+hLLGn2etxQ67GwJSt4c18YGMV5zpU NG4Kbn X-Received: by 2002:a17:903:b4b:b0:2df:991b:4368 with SMTP id d9443c01a7336-2f03f1817ddmr327228435ad.53.1795942603536; Sun, 29 Nov 2026 00:56:43 -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.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 00:56:43 -0800 (PST) From: Jack Wallner To: Kees Cook Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/8] kunit/fortify: Add PTR_ALIGN() object size test Date: Sun, 29 Nov 2026 00:56:10 -0800 Message-ID: <20261129085612.22266-9-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 Check that the compiler still knows the size of the object behind a pointer after PTR_ALIGN(), for a member of a stack object and for a kmalloc() allocation, and that an out of bounds read through the aligned pointer is caught. The test fails without "vdso/align: Preserve object size through PTR_ALIGN()" and is skipped on compilers without __builtin_dynamic_object_size(). 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 --- lib/tests/fortify_kunit.c | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib/tests/fortify_kunit.c b/lib/tests/fortify_kunit.c index 413cdbf3d..4df55eed3 100644 --- a/lib/tests/fortify_kunit.c +++ b/lib/tests/fortify_kunit.c @@ -43,6 +43,7 @@ void fortify_add_kunit_error(int write); #include #include #include +#include #include #include #include @@ -1011,6 +1012,67 @@ static void fortify_test_kmemdup(struct kunit *test) kfree(copy); } +struct fortify_ptr_align { + unsigned long bytes_before; + char buf[40]; + unsigned long bytes_after; +}; + +/* + * PTR_ALIGN() must not hide which object a pointer points into, or the + * bounds checking loses track of it. + */ +static void fortify_test_ptr_align(struct kunit *test) +{ +#if !__has_builtin(__builtin_dynamic_object_size) + kunit_skip(test, "Compiler is missing __builtin_dynamic_object_size() support\n"); +#else + struct fortify_ptr_align pad = { }; + size_t size = unknown_size; + size_t offset = 11; + char *heap, *up, *copy; + size_t avail; + + /* Keep the compiler from knowing the starting offset. */ + OPTIMIZER_HIDE_VAR(offset); + + /* Into a member of a stack object. */ + up = PTR_ALIGN(&pad.buf[offset], 8); + KUNIT_EXPECT_TRUE(test, IS_ALIGNED((unsigned long)up, 8)); + KUNIT_EXPECT_LT(test, up - &pad.buf[offset], 8); + KUNIT_EXPECT_EQ(test, __struct_size(up), + sizeof(pad) - (up - (char *)&pad)); + /* + * Compilers differ in whether they track the member (buf) or only + * the object (pad) here, so accept either bound. + */ + KUNIT_EXPECT_GE(test, __member_size(up), + sizeof(pad.buf) - (up - pad.buf)); + KUNIT_EXPECT_LE(test, __member_size(up), __struct_size(up)); + + /* Into a heap object. */ + heap = kzalloc(size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, heap); + up = PTR_ALIGN(heap + offset, 8); + avail = size - (up - heap); + KUNIT_EXPECT_EQ(test, __struct_size(up), avail); + + /* Reading what is left after aligning is within bounds. */ + copy = kmemdup(up, avail, GFP_KERNEL); + KUNIT_EXPECT_NOT_NULL(test, copy); + KUNIT_EXPECT_EQ(test, fortify_read_overflows, 0); + kfree(copy); + + /* Out of bounds by 1 byte is caught, and nothing is read. */ + copy = kmemdup(up, avail + 1, GFP_KERNEL); + KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR); + KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1); + kfree(copy); + + kfree(heap); +#endif +} + static int fortify_test_init(struct kunit *test) { if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE)) @@ -1054,6 +1116,7 @@ static struct kunit_case fortify_test_cases[] = { KUNIT_CASE(fortify_test_memchr_inv), KUNIT_CASE(fortify_test_memcmp), KUNIT_CASE(fortify_test_kmemdup), + KUNIT_CASE(fortify_test_ptr_align), {} }; -- 2.43.0