]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - queue-5.15/kasan-test-avoid-gcc-warning-for-intentional-overflo.patch
6.6-stable patches
[thirdparty/kernel/stable-queue.git] / queue-5.15 / kasan-test-avoid-gcc-warning-for-intentional-overflo.patch
CommitLineData
d2d2340a
SL
1From 53a37cb948cd0bf77930aa65af70b88b1fc99143 Mon Sep 17 00:00:00 2001
2From: Sasha Levin <sashal@kernel.org>
3Date: Mon, 12 Feb 2024 12:15:52 +0100
4Subject: kasan/test: avoid gcc warning for intentional overflow
5
6From: Arnd Bergmann <arnd@arndb.de>
7
8[ Upstream commit e10aea105e9ed14b62a11844fec6aaa87c6935a3 ]
9
10The out-of-bounds test allocates an object that is three bytes too short
11in order to validate the bounds checking. Starting with gcc-14, this
12causes a compile-time warning as gcc has grown smart enough to understand
13the sizeof() logic:
14
15mm/kasan/kasan_test.c: In function 'kmalloc_oob_16':
16mm/kasan/kasan_test.c:443:14: error: allocation of insufficient size '13' for type 'struct <anonymous>' with size '16' [-Werror=alloc-size]
17 443 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
18 | ^
19
20Hide the actual computation behind a RELOC_HIDE() that ensures
21the compiler misses the intentional bug.
22
23Link: https://lkml.kernel.org/r/20240212111609.869266-1-arnd@kernel.org
24Fixes: 3f15801cdc23 ("lib: add kasan test module")
25Signed-off-by: Arnd Bergmann <arnd@arndb.de>
26Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
27Cc: Alexander Potapenko <glider@google.com>
28Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
29Cc: Arnd Bergmann <arnd@arndb.de>
30Cc: Dmitry Vyukov <dvyukov@google.com>
31Cc: Marco Elver <elver@google.com>
32Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
33Cc: <stable@vger.kernel.org>
34Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
35Signed-off-by: Sasha Levin <sashal@kernel.org>
36---
37 lib/test_kasan.c | 3 ++-
38 1 file changed, 2 insertions(+), 1 deletion(-)
39
40diff --git a/lib/test_kasan.c b/lib/test_kasan.c
41index f0b8b05ccf194..ffedc34714ba7 100644
42--- a/lib/test_kasan.c
43+++ b/lib/test_kasan.c
44@@ -403,7 +403,8 @@ static void kmalloc_oob_16(struct kunit *test)
45 /* This test is specifically crafted for the generic mode. */
46 KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
47
48- ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
49+ /* RELOC_HIDE to prevent gcc from warning about short alloc */
50+ ptr1 = RELOC_HIDE(kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL), 0);
51 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
52
53 ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
54--
552.43.0
56