Skip to contentSkip to conversation

fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()

PATCH Superseded Qasim Ijaz 4 participants 4 messages Mar 11, 2025 – Mar 16, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()

fpga_mgr_test_img_load_sgt() allocates memory for sgt using kunit_kzalloc() however it does not check if the allocation failed. It then passes sgt to sg_alloc_table(), which passes it to __sg_alloc_table(). This function calls memset() on sgt in an attempt to zero it out. If the allocation fails then sgt will be NULL and the memset will trigger a NULL pointer dereference.

Fix this by checking the allocation with KUNIT_ASSERT_NOT_ERR_OR_NULL().

Fixes ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")
---
 drivers/fpga/tests/fpga-mgr-test.c | 1 +
 1 file changed, 1 insertion(+)
drivers/fpga/tests/fpga-mgr-test.c +1 -0
index 9cb37aefbac4..1902ebf5a298 100644
@@ -263,6 +263,7 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test)
 	img_buf = init_test_buffer(test, IMAGE_SIZE);
 
 	sgt = kunit_kzalloc(test, sizeof(*sgt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, sgt);
 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 	sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE);
Signature · 1 line
2.39.5
#1 lore raw

…

Quoted 1 line

zero it out. If the allocation fails then sgt will be NULL and the

…
failed?

Can a summary phrase like “Prevent null pointer dereference in fpga_mgr_test_img_load_sgt()” be nicer?

Regards,
Markus

maintainer
#2 lore raw
Quoted 6 lines · Markus Elfring

…

Quoted 1 line

zero it out. If the allocation fails then sgt will be NULL and the

…
failed?

Quoted 6 lines

Can a summary phrase like “Prevent null pointer dereference in fpga_mgr_test_img_load_sgt()” be nicer?

Regards,
Markus

Hi,

This is the semi-friendly patch-bot of Greg Kroah-Hartman.

Markus, you seem to have sent a nonsensical or otherwise pointless review comment to a patch submission on a Linux kernel developer mailing list. I strongly suggest that you not do this anymore. Please do not bother developers who are actively working to produce patches and features with comments that, in the end, are a waste of time.

Patch submitter, please ignore Markus's suggestion; you do not need to follow it at all. The person/bot/AI that sent it is being ignored by almost all Linux kernel maintainers for having a persistent pattern of behavior of producing distracting and pointless commentary, and inability to adapt to feedback. Please feel free to also ignore emails from them.

thanks,

greg k-h's patch email bot

Reviewed-by on v1 1/1
#3 lore raw
In reply to Qasim Ijaz
Quoted 7 lines · Qasim Ijaz

fpga_mgr_test_img_load_sgt() allocates memory for sgt using kunit_kzalloc() however it does not check if the allocation failed. It then passes sgt to sg_alloc_table(), which passes it to __sg_alloc_table(). This function calls memset() on sgt in an attempt to zero it out. If the allocation fails then sgt will be NULL and the memset will trigger a NULL pointer dereference.

Quoted 5 lines

Fix this by checking the allocation with KUNIT_ASSERT_NOT_ERR_OR_NULL().

Fixes ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")
Quoted 9 lines
---
 drivers/fpga/tests/fpga-mgr-test.c | 1 +
 1 file changed, 1 insertion(+)
drivers/fpga/tests/fpga-mgr-test.c +0 -0
index 9cb37aefbac4..1902ebf5a298 100644

drivers/fpga/tests/fpga-mgr-test.c static void fpga_mgr_test_img_load_sgt(struct kunit *test) · line 269

Quoted 7 lines
drivers/fpga/tests/fpga-mgr-test.c +1 -0
@@ -263,6 +263,7 @@ static void fpga_mgr_test_img_load_sgt(struct kunit *test)
 	img_buf = init_test_buffer(test, IMAGE_SIZE);
 
 	sgt = kunit_kzalloc(test, sizeof(*sgt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, sgt);
 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
 	KUNIT_ASSERT_EQ(test, ret, 0);
 	sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE);

Thanks,
Marco