[PATCH] ext4: Fix potential null dereference in ext4 test

Charles Han posted 1 patch 11 months, 1 week ago
fs/ext4/mballoc-test.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] ext4: Fix potential null dereference in ext4 test
Posted by Charles Han 11 months, 1 week ago
kunit_kzalloc() may return a NULL pointer, dereferencing it without
NULL check may lead to NULL dereference.
Add a NULL check for test_state

Fixes: b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
Fixes: ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
Signed-off-by: Charles Han <hanchunchao@inspur.com>
---
 fs/ext4/mballoc-test.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/ext4/mballoc-test.c b/fs/ext4/mballoc-test.c
index bb2a223b207c..d634c12f1984 100644
--- a/fs/ext4/mballoc-test.c
+++ b/fs/ext4/mballoc-test.c
@@ -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);
-- 
2.43.0
Re: [PATCH] ext4: Fix potential null dereference in ext4 test
Posted by Theodore Ts'o 10 months, 3 weeks ago
On Fri, Mar 07, 2025 at 07:54:31PM +0800, Charles Han wrote:
> kunit_kzalloc() may return a NULL pointer, dereferencing it without
> NULL check may lead to NULL dereference.
> Add a NULL check for test_state

Thanks, others have submitted the same fix.  It's now fixed in my tree
and linux-next.

	       	    	      	       - Ted
Re: [PATCH] ext4: Fix potential null dereference in ext4 test
Posted by Kemeng Shi 11 months ago

on 3/7/2025 7:54 PM, Charles Han wrote:
> kunit_kzalloc() may return a NULL pointer, dereferencing it without
> NULL check may lead to NULL dereference.
> Add a NULL check for test_state
> 
> Fixes: b7098e1fa7bc ("ext4: Add unit test for mb_free_blocks")
> Fixes: ac96b56a2fbd ("ext4: Add unit test for mb_mark_used")
> Signed-off-by: Charles Han <hanchunchao@inspur.com>
> ---
>  fs/ext4/mballoc-test.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/ext4/mballoc-test.c b/fs/ext4/mballoc-test.c
> index bb2a223b207c..d634c12f1984 100644
> --- a/fs/ext4/mballoc-test.c
> +++ b/fs/ext4/mballoc-test.c
> @@ -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);
> 
Good catch, looks good to me.

Reviewed-by: Kemeng Shi <shikemeng@huaweicloud.com>