kasan: add kunit tests for kmalloc_track_caller, kmalloc_node_track_caller
Subsystems (2)
PATCH v3 1/1 kasan: add kunit tests for kmalloc_track_caller, kmalloc_node_track_caller
The Kunit tests for kmalloc_track_caller and kmalloc_node_track_caller were missing in kasan_test_c.c, which check that these functions poison the memory properly.
Add a Kunit test:
-> kmalloc_tracker_caller_oob_right(): This includes out-of-bounds
access test for kmalloc_track_caller and kmalloc_node_track_caller.
---v1->v2: Simplified the three separate out-of-bounds tests to a single test for kmalloc_track_caller.
v2->v3: Used the same size for both the test cases.
Link to v1: https://lore.kernel.org/all/[email protected]/
Link to v2: https://lore.kernel.org/all/[email protected]/
mm/kasan/kasan_test_c.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
mm/kasan/kasan_test_c.c +31 -0
@@ -213,6 +213,36 @@ static void kmalloc_node_oob_right(struct kunit *test)kfree (ptr); } +static void kmalloc_track_caller_oob_right (struct kunit *test) +{ +char *ptr; +size_t size =128 -KASAN_GRANULE_SIZE ; + +/* +* Check that KASAN detects out-of-bounds access for object allocated via +* kmalloc_track_caller(). +*/ + ptr =kmalloc_track_caller (size,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr); + +OPTIMIZER_HIDE_VAR (ptr); +KUNIT_EXPECT_KASAN_FAIL (test, ptr[size] ='y' ); + +kfree (ptr); + +/* +* Check that KASAN detects out-of-bounds access for object allocated via +* kmalloc_node_track_caller(). +*/ + ptr =kmalloc_node_track_caller (size,GFP_KERNEL ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr); + +OPTIMIZER_HIDE_VAR (ptr); +KUNIT_EXPECT_KASAN_FAIL (test, ptr[size] ='y' ); + +kfree (ptr); +} +/* * Check that KASAN detects an out-of-bounds access for a big object allocated * via kmalloc(). But not as big as to trigger the page_alloc fallback.
@@ -1958,6 +1988,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_oob_right),KUNIT_CASE (kmalloc_oob_left),KUNIT_CASE (kmalloc_node_oob_right), +KUNIT_CASE (kmalloc_track_caller_oob_right),KUNIT_CASE (kmalloc_big_oob_right),KUNIT_CASE (kmalloc_large_oob_right), KUNIT_CASE(kmalloc_large_uaf),
Signature · 1 line
2.34.1
Quoted 73 lines · Nihar Chaithanya
The Kunit tests for
kmalloc_track_callerandkmalloc_node_track_callerwere missing inkasan_test_c.c, which check that these functions poison the memory properly.Add a Kunit test:
->kmalloc_tracker_caller_oob_right(): This includes out-of-bounds
access test forkmalloc_track_callerandkmalloc_node_track_caller.Signed-off-by Nihar Chaithanya <[email protected]>---
v1->v2: Simplified the three separate out-of-bounds tests to a single test forkmalloc_track_caller.
v2->v3: Used the same size for both the test cases.Link to v1: https://lore.kernel.org/all/[email protected]/
Link to v2: https://lore.kernel.org/all/[email protected]/mm/kasan/kasan_test_c.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)mm/kasan/kasan_test_c.c +31
-1@@ -213,6 +213,36 @@ static void kmalloc_node_oob_right(struct kunit *test)kfree (ptr); } +static void kmalloc_track_caller_oob_right (struct kunit *test) +{ +char *ptr; +size_t size =128 -KASAN_GRANULE_SIZE ; + +/* +* Check that KASAN detects out-of-bounds access for object allocated via +* kmalloc_track_caller(). +*/ + ptr =kmalloc_track_caller (size,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr); + +OPTIMIZER_HIDE_VAR (ptr); +KUNIT_EXPECT_KASAN_FAIL (test, ptr[size] ='y' ); + +kfree (ptr); + +/* +* Check that KASAN detects out-of-bounds access for object allocated via +* kmalloc_node_track_caller(). +*/ + ptr =kmalloc_node_track_caller (size,GFP_KERNEL ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, ptr); + +OPTIMIZER_HIDE_VAR (ptr); +KUNIT_EXPECT_KASAN_FAIL (test, ptr[size] ='y' ); + +kfree (ptr); +} +/* * Check that KASAN detects an out-of-bounds access for a big object allocated * via kmalloc(). But not as big as to trigger the page_alloc fallback. @@ -1958,6 +1988,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_oob_right),KUNIT_CASE (kmalloc_oob_left),KUNIT_CASE (kmalloc_node_oob_right), +KUNIT_CASE (kmalloc_track_caller_oob_right),KUNIT_CASE (kmalloc_big_oob_right),KUNIT_CASE (kmalloc_large_oob_right), KUNIT_CASE(kmalloc_large_uaf), --
Quoted 2 lines
2.34.1
Thank you!