]>
Commit | Line | Data |
---|---|---|
d2d2340a SL |
1 | From 53a37cb948cd0bf77930aa65af70b88b1fc99143 Mon Sep 17 00:00:00 2001 |
2 | From: Sasha Levin <sashal@kernel.org> | |
3 | Date: Mon, 12 Feb 2024 12:15:52 +0100 | |
4 | Subject: kasan/test: avoid gcc warning for intentional overflow | |
5 | ||
6 | From: Arnd Bergmann <arnd@arndb.de> | |
7 | ||
8 | [ Upstream commit e10aea105e9ed14b62a11844fec6aaa87c6935a3 ] | |
9 | ||
10 | The out-of-bounds test allocates an object that is three bytes too short | |
11 | in order to validate the bounds checking. Starting with gcc-14, this | |
12 | causes a compile-time warning as gcc has grown smart enough to understand | |
13 | the sizeof() logic: | |
14 | ||
15 | mm/kasan/kasan_test.c: In function 'kmalloc_oob_16': | |
16 | mm/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 | ||
20 | Hide the actual computation behind a RELOC_HIDE() that ensures | |
21 | the compiler misses the intentional bug. | |
22 | ||
23 | Link: https://lkml.kernel.org/r/20240212111609.869266-1-arnd@kernel.org | |
24 | Fixes: 3f15801cdc23 ("lib: add kasan test module") | |
25 | Signed-off-by: Arnd Bergmann <arnd@arndb.de> | |
26 | Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> | |
27 | Cc: Alexander Potapenko <glider@google.com> | |
28 | Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> | |
29 | Cc: Arnd Bergmann <arnd@arndb.de> | |
30 | Cc: Dmitry Vyukov <dvyukov@google.com> | |
31 | Cc: Marco Elver <elver@google.com> | |
32 | Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> | |
33 | Cc: <stable@vger.kernel.org> | |
34 | Signed-off-by: Andrew Morton <akpm@linux-foundation.org> | |
35 | Signed-off-by: Sasha Levin <sashal@kernel.org> | |
36 | --- | |
37 | lib/test_kasan.c | 3 ++- | |
38 | 1 file changed, 2 insertions(+), 1 deletion(-) | |
39 | ||
40 | diff --git a/lib/test_kasan.c b/lib/test_kasan.c | |
41 | index 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 | -- | |
55 | 2.43.0 | |
56 |