Skip to content

Commit 74df224

Browse files
committed
kunit/fortify: Fix replaced failure path to unbreak __alloc_size
The __alloc_size annotation for kmemdup() was getting disabled under KUnit testing because the replaced fortify_panic macro implementation was using "return NULL" as a way to survive the sanity checking. But having the chance to return NULL invalidated __alloc_size, so kmemdup was not passing the __builtin_dynamic_object_size() tests any more: [23:26:18] [PASSED] fortify_test_alloc_size_kmalloc_const [23:26:19] # fortify_test_alloc_size_kmalloc_dynamic: EXPECTATION FAILED at lib/fortify_kunit.c:265 [23:26:19] Expected __builtin_dynamic_object_size(p, 1) == expected, but [23:26:19] __builtin_dynamic_object_size(p, 1) == -1 (0xffffffffffffffff) [23:26:19] expected == 11 (0xb) [23:26:19] __alloc_size() not working with __bdos on kmemdup("hello there", len, gfp) [23:26:19] [FAILED] fortify_test_alloc_size_kmalloc_dynamic Normal builds were not affected: __alloc_size continued to work there. Use a zero-sized allocation instead, which allows __alloc_size to behave. Fixes: 4ce615e ("fortify: Provide KUnit counters for failure testing") Fixes: fa4a3f8 ("fortify: Add KUnit tests for runtime overflows") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent a284e43 commit 74df224

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

include/linux/fortify-string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
738738
if (__compiletime_lessthan(p_size, size))
739739
__read_overflow();
740740
if (p_size < size)
741-
fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size, NULL);
741+
fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size,
742+
__real_kmemdup(p, 0, gfp));
742743
return __real_kmemdup(p, size, gfp);
743744
}
744745

lib/fortify_kunit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,19 +1002,19 @@ static void fortify_test_kmemdup(struct kunit *test)
10021002

10031003
/* Out of bounds by 1 byte. */
10041004
copy = kmemdup(src, len + 1, GFP_KERNEL);
1005-
KUNIT_EXPECT_NULL(test, copy);
1005+
KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
10061006
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 1);
10071007
kfree(copy);
10081008

10091009
/* Way out of bounds. */
10101010
copy = kmemdup(src, len * 2, GFP_KERNEL);
1011-
KUNIT_EXPECT_NULL(test, copy);
1011+
KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
10121012
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 2);
10131013
kfree(copy);
10141014

10151015
/* Starting offset causing out of bounds. */
10161016
copy = kmemdup(src + 1, len, GFP_KERNEL);
1017-
KUNIT_EXPECT_NULL(test, copy);
1017+
KUNIT_EXPECT_PTR_EQ(test, copy, ZERO_SIZE_PTR);
10181018
KUNIT_EXPECT_EQ(test, fortify_read_overflows, 3);
10191019
kfree(copy);
10201020
}

0 commit comments

Comments
 (0)