All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bnx2x: fix NULL pointer dereference in bnx2x_free_mem_bp()
@ 2026-08-17  3:33 Jiangshan Yi
  2026-08-17 13:11 ` Vadim Fedorenko
  0 siblings, 1 reply; 3+ messages in thread
From: Jiangshan Yi @ 2026-08-17  3:33 UTC (permalink / raw)
  To: skalluru, manishc, andrew+netdev, davem, edumazet, kuba, pabeni
  Cc: yuvalmin, dmitry, ariele, eilong, netdev, linux-kernel,
	13667453960, Jiangshan Yi, Sashiko, stable

bnx2x_alloc_mem_bp() sets bp->fp_array_size before allocating bp->fp.
If the fp allocation fails, the error path calls bnx2x_free_mem_bp(),
which dereferences bp->fp in a loop bounded by the non-zero
bp->fp_array_size, causing a NULL pointer dereference.

Move the bp->fp_array_size assignment to after bp->fp is set, and
add a NULL guard in bnx2x_free_mem_bp().

Fixes: c3146eb676e7c ("bnx2x: Correct memory preparation and release")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260815122149.951215-1-yijiangshan%40kylinos.cn
Cc: stable@vger.kernel.org
Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
---
 drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
index 5b2640bd31c3..d84d1845a096 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
@@ -4712,8 +4712,10 @@ void bnx2x_free_mem_bp(struct bnx2x *bp)
 {
 	int i;
 
-	for (i = 0; i < bp->fp_array_size; i++)
-		kfree(bp->fp[i].tpa_info);
+	if (bp->fp) {
+		for (i = 0; i < bp->fp_array_size; i++)
+			kfree(bp->fp[i].tpa_info);
+	}
 	kfree(bp->fp);
 	kfree(bp->sp_objs);
 	kfree(bp->fp_stats);
@@ -4742,13 +4744,13 @@ int bnx2x_alloc_mem_bp(struct bnx2x *bp)
 
 	/* fp array: RSS plus CNIC related L2 queues */
 	fp_array_size = BNX2X_MAX_RSS_COUNT(bp) + CNIC_SUPPORT(bp);
-	bp->fp_array_size = fp_array_size;
-	BNX2X_DEV_INFO("fp_array_size %d\n", bp->fp_array_size);
+	BNX2X_DEV_INFO("fp_array_size %d\n", fp_array_size);
 
-	fp = kzalloc_objs(*fp, bp->fp_array_size);
+	fp = kzalloc_objs(*fp, fp_array_size);
 	if (!fp)
 		goto alloc_err;
 	bp->fp = fp;
+	bp->fp_array_size = fp_array_size;
 	for (i = 0; i < bp->fp_array_size; i++) {
 		fp[i].tpa_info =
 			kzalloc_objs(struct bnx2x_agg_info,
-- 
2.25.1


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

* Re: [PATCH] bnx2x: fix NULL pointer dereference in bnx2x_free_mem_bp()
  2026-08-17  3:33 [PATCH] bnx2x: fix NULL pointer dereference in bnx2x_free_mem_bp() Jiangshan Yi
@ 2026-08-17 13:11 ` Vadim Fedorenko
  2026-08-17 15:08   ` Jiangshan Yi
  0 siblings, 1 reply; 3+ messages in thread
From: Vadim Fedorenko @ 2026-08-17 13:11 UTC (permalink / raw)
  To: Jiangshan Yi, skalluru, manishc, andrew+netdev, davem, edumazet,
	kuba, pabeni
  Cc: yuvalmin, dmitry, ariele, eilong, netdev, linux-kernel,
	13667453960, Sashiko, stable

On 17/08/2026 04:33, Jiangshan Yi wrote:
> bnx2x_alloc_mem_bp() sets bp->fp_array_size before allocating bp->fp.
> If the fp allocation fails, the error path calls bnx2x_free_mem_bp(),
> which dereferences bp->fp in a loop bounded by the non-zero
> bp->fp_array_size, causing a NULL pointer dereference.
> 
> Move the bp->fp_array_size assignment to after bp->fp is set, and
> add a NULL guard in bnx2x_free_mem_bp().
> 
> Fixes: c3146eb676e7c ("bnx2x: Correct memory preparation and release")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260815122149.951215-1-yijiangshan%40kylinos.cn
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiangshan Yi <yijiangshan@kylinos.cn>
> ---
>   drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
> index 5b2640bd31c3..d84d1845a096 100644
> --- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
> +++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_cmn.c
> @@ -4712,8 +4712,10 @@ void bnx2x_free_mem_bp(struct bnx2x *bp)
>   {
>   	int i;
>   
> -	for (i = 0; i < bp->fp_array_size; i++)
> -		kfree(bp->fp[i].tpa_info);
> +	if (bp->fp) {

there is no need to put this defensive code ...

> +		for (i = 0; i < bp->fp_array_size; i++)
> +			kfree(bp->fp[i].tpa_info);
> +	}
>   	kfree(bp->fp);
>   	kfree(bp->sp_objs);
>   	kfree(bp->fp_stats);
> @@ -4742,13 +4744,13 @@ int bnx2x_alloc_mem_bp(struct bnx2x *bp)
>   
>   	/* fp array: RSS plus CNIC related L2 queues */
>   	fp_array_size = BNX2X_MAX_RSS_COUNT(bp) + CNIC_SUPPORT(bp);
> -	bp->fp_array_size = fp_array_size;
> -	BNX2X_DEV_INFO("fp_array_size %d\n", bp->fp_array_size);
> +	BNX2X_DEV_INFO("fp_array_size %d\n", fp_array_size);
>   
> -	fp = kzalloc_objs(*fp, bp->fp_array_size);
> +	fp = kzalloc_objs(*fp, fp_array_size);
>   	if (!fp)
>   		goto alloc_err;
>   	bp->fp = fp;
> +	bp->fp_array_size = fp_array_size;

... when you have fixed the root cause of the issue

>   	for (i = 0; i < bp->fp_array_size; i++) {
>   		fp[i].tpa_info =
>   			kzalloc_objs(struct bnx2x_agg_info,


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

* Re: Re: [PATCH] bnx2x: fix NULL pointer dereference in bnx2x_free_mem_bp()
  2026-08-17 13:11 ` Vadim Fedorenko
@ 2026-08-17 15:08   ` Jiangshan Yi
  0 siblings, 0 replies; 3+ messages in thread
From: Jiangshan Yi @ 2026-08-17 15:08 UTC (permalink / raw)
  To: vadim.fedorenko
  Cc: 13667453960, andrew+netdev, ariele, davem, dmitry, edumazet,
	eilong, kuba, linux-kernel, manishc, netdev, pabeni, sashiko-bot,
	skalluru, stable, yijiangshan, yuvalmin

> > +	if (bp->fp) {
> there is no need to put this defensive code ...
> > +		for (i = 0; i < bp->fp_array_size; i++)
> > +			kfree(bp->fp[i].tpa_info);
> > +	}
> ......
> > -	bp->fp_array_size = fp_array_size;
> > -	BNX2X_DEV_INFO("fp_array_size %d\n", bp->fp_array_size);
> > +	BNX2X_DEV_INFO("fp_array_size %d\n", fp_array_size);
> >   
> > -	fp = kzalloc_objs(*fp, bp->fp_array_size);
> > +	fp = kzalloc_objs(*fp, fp_array_size);
> >   	if (!fp)
> >   		goto alloc_err;
> >   	bp->fp = fp;
> > +	bp->fp_array_size = fp_array_size;
> ... when you have fixed the root cause of the issue


Hi Vadim,

Thanks for the review. You're right - once the bp->fp_array_size assignment is moved after bp->fp is set, the loop in bnx2x_free_mem_bp() naturally won't execute
when bp->fp is NULL, since bp->fp_array_size remains zero. The if (bp->fp) guard is indeed redundant. I'll remove it and send a v2 with only the assignment-order
fix.

Best regards,
Jiangshan Yi

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

end of thread, other threads:[~2026-08-17 15:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-17  3:33 [PATCH] bnx2x: fix NULL pointer dereference in bnx2x_free_mem_bp() Jiangshan Yi
2026-08-17 13:11 ` Vadim Fedorenko
2026-08-17 15:08   ` Jiangshan Yi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.