Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795942604; b=2wq74jUrjT2nca4QuDLtS3O1k0ulRrAUiNfeGCGjoi89SsnJiI9KDOLOfYzIXLXmDJY9 nGpqyaaw/ujzOC/cixwnAhsg/G3UXqCv1el6cEoqdZYRlwZF40qT2nR3lJNtTYKtMHvJu 1pytdOUOgMMC6fHdSNsfwGwwuKheo2x1EdMnCQkmSi02rEULYUeslbKwJyXyhiTYN96fu ZGt7Di5IFHPB227uGu8cbpxl7lJN+ubkpb7oummLdoSU26ZSIJgeqcIOM61KWBKxzmUHz w5EIjV/BMeP1djK1CPZjJ4pg/2CobU41oMSwO+5n1QLJRUlPXU0//VTc+vEeBkkw45A== 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-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=2+GciyyCztWfiADNTMOV7fzLlFrxn+FgYrXILoamZDhgMHsJVmMXE7EjDzIars7uo3PP xjhFI/7bCOMIJCDbs6k3iGhcYzcv4EAeDcUS9Gs8Vl9tHLmuZ7abFkf32M296nCts29Cr MdD9j/bDj4LvLJu0JW9RRBRlo34Ens9HNkkXK6qQBYQy/HgBm12DBzhEmTvumQZeGODWm h/YBEZmsvFpHuqHd4K817abOEHyRP5hBVOXD5wI8ZSLz13TbvHask0GSrYc6e00S/biGR TLSby82+lgZWnRw93FvvDXiunyMrcRE1GP2FIch52zkT4CBuFr2D3CnaY9BzdTMLcFQ== 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.169 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 3433B60A261B for ; Sun, 29 Nov 2026 08:56:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3433B60A261B 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-f169.google.com with SMTP id d9443c01a7336-2f03920c477so79391995ad.2 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=ocBXj6qnPafMvc1LjI/PhnFarvRA2iakr2HgAlMaWFArCjeiMt2Me5u54JUxQiggqf 02pP8HW6MLkfrzKt/Gl7AKD+yNHvpkHP2P67+jtaQ0hEiLd3VxDnYaNdua6OB6Q5wjro 2QbOMq8jE2JhzChkakjOMlLcwwnFabSiFY2WDSDGqCNvF2pqsGnsR++8oNR68Khi7NTe a7B1MZ7G7RlqIge1LV0VTbtzwwj/wcHL2SDzroNFENxaj+AkrN63AcDtdBKKtTwZlO4P DAGStjdoAeepSW+9sw5ZKhlANlNLjiLaOGSqjZnb8EQPQOQenG+PDV7ad3ezmrigPLK6 WSmw== X-Gm-Message-State: APuU4Z9b9OmEgwmfYYKcnKdCLQvxtFP9AbuMq0BVOPeP9/MuPpgxyFQx bdrwFxta3iy1wU0RXUpaakIsXLyaA6JHMSkpRyRQa7BZ71Kjxl6Bn1NiMAt6CYMfquo= X-Gm-Gg: AZFdTZcnbD8JV/k2lH1ri7OHMWwTsgx11UMJP3fjxQe8768xh5753prmHzxws+CVvhM Zzcoc5eeN5JVcN/+ol2OhheHiWDoDARDoGT3QlDPbSzL9KDRi91+T74plfySovKlOUxncFnhjgd /UkLcS4dxF0fctKfwf9WSh9BxxAOnPS1KhsvXS26cgsWdbklS/ovNPeIdGjq/BkKUxR4tE1Ajja UJOpEId55uIgyfjnu9Ow6RlTSJBuAkD0MoyCmzZrHcd7W3w0LLhmcCBiMjQEsue1TuVDpE5jYwr bzNDY3/GT5ERUZfextWusU/rl8KfYWzW1nUzwEasBBCak0hS6Tr3d9kXrnNDV4A+iG2VyV75SJy pGWMoiNolywguJhjPBDbPoKQ8tAaxntTJOj0Oql3p+bEIwWcM6N1SQEWjbhooZwz0KHPiN/Qf+v g0sTsn8VFBdrn6RkCQ4+h074jrNg2Ex/npk9/8uvynKTCoqfK2vk5UCSI9xTSSqvDE6hR9IToFn 1jlnX5Z7iCiMBBzItUkpXvgpGC1MzWiJwcDoM1adVaFyeSveiIPvCv1kEPwxyF3uwnJtkfKTYp+ X0VNhp5Evbc/fHLGzHXIAAWhlhyN0R//FhwGpdQ+sjmNC/XXSDlRknQ9LkE9Ab1Kc0vH2Z/Q22Y 76hWaRw0dYbt1fBVQqi2xmoPTRubd7wfuYJXQ03FhvwQijKMpiU22UlMUXmW0AtTX5YfCwS5Vqa EhEp/d 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-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 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