linux-fpga.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 RESEND] fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()
@ 2025-04-08 17:35 Qasim Ijaz
  0 siblings, 0 replies; 3+ messages in thread
From: Qasim Ijaz @ 2025-04-08 17:35 UTC (permalink / raw)
  To: mdf, hao.wu, yilun.xu, trix, marpagan, russ.weight
  Cc: linux-fpga, linux-kernel, Marco Pagani

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().

Reviewed-by: Marco Pagani <marco.pagani@linux.dev>
Fixes: ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")
Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
v2:
- Remove stable CC tag since its just a kunit test

 drivers/fpga/tests/fpga-mgr-test.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c
index 9cb37aefbac4..1902ebf5a298 100644
--- a/drivers/fpga/tests/fpga-mgr-test.c
+++ b/drivers/fpga/tests/fpga-mgr-test.c
@@ -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);
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 RESEND] fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()
@ 2025-04-22 15:37 Qasim Ijaz
  2025-04-25  4:30 ` Xu Yilun
  0 siblings, 1 reply; 3+ messages in thread
From: Qasim Ijaz @ 2025-04-22 15:37 UTC (permalink / raw)
  To: mdf, hao.wu, yilun.xu, trix, akpm, marpagan, arnd
  Cc: linux-fpga, linux-kernel, Marco Pagani

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().

Reviewed-by: Marco Pagani <marco.pagani@linux.dev>
Fixes: ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")
Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
v2:
- Remove stable CC tag since its just a kunit test

 drivers/fpga/tests/fpga-mgr-test.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c
index 9cb37aefbac4..1902ebf5a298 100644
--- a/drivers/fpga/tests/fpga-mgr-test.c
+++ b/drivers/fpga/tests/fpga-mgr-test.c
@@ -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);
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 RESEND] fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt()
  2025-04-22 15:37 Qasim Ijaz
@ 2025-04-25  4:30 ` Xu Yilun
  0 siblings, 0 replies; 3+ messages in thread
From: Xu Yilun @ 2025-04-25  4:30 UTC (permalink / raw)
  To: Qasim Ijaz
  Cc: mdf, hao.wu, yilun.xu, trix, akpm, marpagan, arnd, linux-fpga,
	linux-kernel, Marco Pagani

On Tue, Apr 22, 2025 at 04:37:37PM +0100, Qasim Ijaz wrote:
> 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().
> 
> Reviewed-by: Marco Pagani <marco.pagani@linux.dev>
> Fixes: ccbc1c302115 ("fpga: add an initial KUnit suite for the FPGA Manager")
> Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>

Acked-by: Xu Yilun <yilun.xu@intel.com>

Applied to for-next.

> ---
> v2:
> - Remove stable CC tag since its just a kunit test
> 
>  drivers/fpga/tests/fpga-mgr-test.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c
> index 9cb37aefbac4..1902ebf5a298 100644
> --- a/drivers/fpga/tests/fpga-mgr-test.c
> +++ b/drivers/fpga/tests/fpga-mgr-test.c
> @@ -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);
> -- 
> 2.39.5
> 
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-04-25  4:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 17:35 [PATCH v2 RESEND] fpga: fix potential null pointer deref in fpga_mgr_test_img_load_sgt() Qasim Ijaz
  -- strict thread matches above, loose matches on Subject: below --
2025-04-22 15:37 Qasim Ijaz
2025-04-25  4:30 ` Xu Yilun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).