fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()
PATCH RESEND v2 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().
---
v2:
- Remove stable CC tag since its just a kunit test
drivers/fpga/tests/fpga-mgr-test.c | 1 + 1 file changed, 1 insertion(+)
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 );
Signature · 1 line
2.39.5
Quoted 7 lines · Qasim Ijaz
fpga_mgr_test_img_load_sgt()allocates memory for sgt usingkunit_kzalloc()however it does not check if the allocation failed. It then passes sgt tosg_alloc_table(), which passes it to__sg_alloc_table(). This function callsmemset()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().Reviewed-by Marco Pagani <[email protected]>Fixes ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")Signed-off-by Qasim Ijaz <[email protected]>
Applied to for-next.
Quoted 23 lines
---
v2:
- Remove stable CC tag since its just a kunit testdrivers/fpga/tests/fpga-mgr-test.c | 1 + 1 file changed, 1 insertion(+)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 );Signature · 1 line
2.39.5