Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: be2iscsi: fix NULL pointer dereference in beiscsi_alloc_mem() error path
@ 2026-11-28 23:05 TAKANO Mitsuhiro via B4 Relay
  2026-11-28 23:11 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: TAKANO Mitsuhiro via B4 Relay @ 2026-11-28 23:05 UTC (permalink / raw)
  To: Ketan Mukadam, James E.J. Bottomley, Martin K. Petersen
  Cc: linux-scsi, linux-kernel, TAKANO Mitsuhiro

From: TAKANO Mitsuhiro <takano32@gmail.com>

In beiscsi_alloc_mem(), both jumps to free_mem occur while the current
mem_descr->mem_array is still NULL and the j DMA fragments allocated for
the current descriptor are stored in mem_arr_orig[0..j-1]. The error path
dereferences mem_descr->mem_array[j - 1] directly, causing a NULL pointer
dereference whenever j > 0, and also skips freeing init_mem[0].mem_array
when unwinding earlier descriptors.

Free the j fragments from mem_arr_orig first, then unwind and free all
previously completed descriptors from i - 1 down to 0.

Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
---
Verification: compile-tested on x86_64 with CONFIG_BE2ISCSI=m
(make W=1 drivers/scsi/be2iscsi/be_main.o; 0 warnings) and checked with
scripts/checkpatch.pl --strict. Checked against linux-next (next-20261127)
and lore.kernel.org for duplicates. Not runtime-tested on OneConnect
hardware.

The change and the changelog text were produced with an AI coding
assistant (Gemini) working from the tree; I reviewed the diff and the
reasoning, and I am responsible for the submission.
---
 drivers/scsi/be2iscsi/be_main.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/drivers/scsi/be2iscsi/be_main.c b/drivers/scsi/be2iscsi/be_main.c
index cadcaefaa..b420715ea 100644
--- a/drivers/scsi/be2iscsi/be_main.c
+++ b/drivers/scsi/be2iscsi/be_main.c
@@ -2555,22 +2555,22 @@ static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
 	kfree(mem_arr_orig);
 	return 0;
 free_mem:
-	mem_descr->num_elements = j;
-	while ((i) || (j)) {
+	for (; j > 0; j--) {
+		mem_arr = &mem_arr_orig[j - 1];
+		dma_free_coherent(&phba->pcidev->dev, mem_arr->size,
+				  mem_arr->virtual_address,
+				  (unsigned long)mem_arr->bus_address.u.a64.address);
+	}
+	while (i > 0) {
+		i--;
+		mem_descr--;
 		for (j = mem_descr->num_elements; j > 0; j--) {
-			dma_free_coherent(&phba->pcidev->dev,
-					    mem_descr->mem_array[j - 1].size,
-					    mem_descr->mem_array[j - 1].
-					    virtual_address,
-					    (unsigned long)mem_descr->
-					    mem_array[j - 1].
-					    bus_address.u.a64.address);
-		}
-		if (i) {
-			i--;
-			kfree(mem_descr->mem_array);
-			mem_descr--;
+			mem_arr = &mem_descr->mem_array[j - 1];
+			dma_free_coherent(&phba->pcidev->dev, mem_arr->size,
+					  mem_arr->virtual_address,
+					  (unsigned long)mem_arr->bus_address.u.a64.address);
 		}
+		kfree(mem_descr->mem_array);
 	}
 	kfree(mem_arr_orig);
 	kfree(phba->init_mem);

---
base-commit: f93eb7cfc4bfa5d46340f360873726e3e77f2365
change-id: 20261128-b4-scsi-be2iscsi-null-mem-array-70ceed1d59c9

Best regards,
--  
TAKANO Mitsuhiro <takano32@gmail.com>



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

* Re: [PATCH] scsi: be2iscsi: fix NULL pointer dereference in beiscsi_alloc_mem() error path
  2026-11-28 23:05 [PATCH] scsi: be2iscsi: fix NULL pointer dereference in beiscsi_alloc_mem() error path TAKANO Mitsuhiro via B4 Relay
@ 2026-11-28 23:11 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 23:11 UTC (permalink / raw)
  To: TAKANO Mitsuhiro; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] scsi: be2iscsi: missing Fixes trailer for beiscsi_alloc_mem()
  error path fix
--

commit f497efcc4ca2b2970e263b27d42a6e4480250ecf
Author: TAKANO Mitsuhiro <takano32@gmail.com>

> scsi: be2iscsi: fix NULL pointer dereference in beiscsi_alloc_mem() error path
>
> In beiscsi_alloc_mem(), both jumps to free_mem occur while the current
> mem_descr->mem_array is still NULL and the j DMA fragments allocated for
> the current descriptor are stored in mem_arr_orig[0..j-1]. The error path
> dereferences mem_descr->mem_array[j - 1] directly, causing a NULL pointer
> dereference whenever j > 0, and also skips freeing init_mem[0].mem_array
> when unwinding earlier descriptors.
>
> Free the j fragments from mem_arr_orig first, then unwind and free all
> previously completed descriptors from i - 1 down to 0.
>
> Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>

[Severity: Low]
This fixes a NULL pointer dereference and memory leak in
beiscsi_alloc_mem() that have been present since the driver was added.
Should this include:

  Fixes: 6733b39a1301 ("[SCSI] be2iscsi: add 10Gbps iSCSI - BladeEngine 2 driver")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-b4-scsi-be2iscsi-null-mem-array-v1-1-82c66b29ab81@gmail.com?part=1

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

end of thread, other threads:[~2026-11-28 23:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 23:05 [PATCH] scsi: be2iscsi: fix NULL pointer dereference in beiscsi_alloc_mem() error path TAKANO Mitsuhiro via B4 Relay
2026-11-28 23:11 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox