Skip to contentSkip to conversation

ext4: Fix potential NULL pointer dereferences in test_mb_mark_used() and test_mb_free_blocks()

PATCH RESEND In review Qasim Ijaz 4 participants 4 messages Mar 13, 2025 – Mar 21, 2025

Subsystems (2)
lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH RESEND 1/1 ext4: Fix potential NULL pointer dereferences in test_mb_mark_used() and test_mb_free_blocks()

test_mb_mark_used() and test_mb_free_blocks() call kunit_kzalloc() to allocate memory, however both fail to ensure that the allocations succeeded. If kunit_kzalloc() returns NULL, then dereferencing the corresponding pointer without checking for NULL will lead to a NULL pointer dereference.

To fix this call KUNIT_ASSERT_NOT_ERR_OR_NULL() to ensure the allocation succeeded.

Fixes ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
Fixes b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
---
 fs/ext4/mballoc-test.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/mballoc-test.c +2 -0
index bb2a223b207c..d634c12f1984 100644
@@ -796,6 +796,7 @@ static void test_mb_mark_used(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
@@ -860,6 +861,7 @@ static void test_mb_free_blocks(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Qasim Ijaz

test_mb_mark_used() and test_mb_free_blocks() call kunit_kzalloc() to allocate memory, however both fail to ensure that the allocations succeeded. If kunit_kzalloc() returns NULL, then dereferencing the corresponding pointer without checking for NULL will lead to a NULL pointer dereference.

To fix this call KUNIT_ASSERT_NOT_ERR_OR_NULL() to ensure the allocation succeeded.

Quoted 28 lines
Fixes ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
Fixes b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
---
 fs/ext4/mballoc-test.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/mballoc-test.c +2 -0
index bb2a223b207c..d634c12f1984 100644
@@ -796,6 +796,7 @@ static void test_mb_mark_used(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
@@ -860,6 +861,7 @@ static void test_mb_free_blocks(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Qasim Ijaz
Quoted 8 lines · Qasim Ijaz

test_mb_mark_used() and test_mb_free_blocks() call kunit_kzalloc() to allocate memory, however both fail to ensure that the allocations succeeded. If kunit_kzalloc() returns NULL, then dereferencing the corresponding pointer without checking for NULL will lead to a NULL pointer dereference.

To fix this call KUNIT_ASSERT_NOT_ERR_OR_NULL() to ensure the allocation succeeded.

Quoted 30 lines
Fixes ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
Fixes b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
---
 fs/ext4/mballoc-test.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/mballoc-test.c +2 -0
index bb2a223b207c..d634c12f1984 100644
@@ -796,6 +796,7 @@ static void test_mb_mark_used(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
@@ -860,6 +861,7 @@ static void test_mb_free_blocks(struct kunit *test)
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buddy);
 	grp = kunit_kzalloc(test, offsetof(struct ext4_group_info,
 				bb_counters[MB_NUM_ORDERS(sb)]), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, grp);
 
 	ret = ext4_mb_load_buddy(sb, TEST_GOAL_GROUP, &e4b);
 	KUNIT_ASSERT_EQ(test, ret, 0);
maintainer
#3 lore raw
In reply to Qasim Ijaz
Quoted 8 lines · Qasim Ijaz

test_mb_mark_used() and test_mb_free_blocks() call kunit_kzalloc() to allocate memory, however both fail to ensure that the allocations succeeded. If kunit_kzalloc() returns NULL, then dereferencing the corresponding pointer without checking for NULL will lead to a NULL pointer dereference.

To fix this call KUNIT_ASSERT_NOT_ERR_OR_NULL() to ensure the allocation succeeded.

Thanks; other folks have sent this fix.

- Ted