Skip to contentSkip to conversation

scsi: be2iscsi: Fix NULL pointer dereference in memory cleanup

PATCH In review [email protected] 2 participants 4 messages Nov 7 – Nov 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: be2iscsi: Fix NULL pointer dereference in memory cleanup

From: Mohammad Shahid <[email protected]>

During memory allocation ,DMA memory may be allocated successfully before the descriptor array allocation fails.

In this case ,mem_descr->mem_array is NULL,but cleanup path tries to access it.

Use mem_arr_orig to free the DMA allocation of the current descriptor and handle previously allocated descriptors separately.

---
 drivers/scsi/be2iscsi/be_main.c | 34 ++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/scsi/be2iscsi/be_main.c +21 -13
index fd18d4d3d219..95706b5db7be 100644
@@ -2549,22 +2549,30 @@ 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)) {
+	/* Free DMA allocations of the current descriptor */
+	for (; j > 0; j--) {
+		dma_free_coherent(&phba->pcidev->dev,
+			mem_arr_orig[j - 1].size,
+			mem_arr_orig[j - 1].virtual_address,
+			(unsigned long)
+			mem_arr_orig[j - 1].bus_address.u.a64.address);
+	}
+
+	/* Free previously completed descriptors */
+	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_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);
 		}
+
+		kfree(mem_descr->mem_array);
+
 	}
 	kfree(mem_arr_orig);
 	kfree(phba->init_mem);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] scsi: be2iscsi: missing Fixes trailer in commit message for beiscsi_alloc_mem() --

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

commit 3a59c9fd50bd58745d465b3451671b988442c55a
Author: Mohammad Shahid <[email protected]>

Quoted 5 lines

scsi: be2iscsi: Fix NULL pointer dereference in memory cleanup

During memory allocation ,DMA memory may be allocated successfully before the descriptor array allocation fails.

Quoted 7 lines

In this case ,mem_descr->mem_array is NULL,but cleanup path tries to access it.

Use mem_arr_orig to free the DMA allocation of the current descriptor and handle previously allocated descriptors separately.

[Severity: Low]
Since this fixes the error cleanup loop in beiscsi_alloc_mem() from the initial driver commit, should the commit message include:

Fixes 6733b39a1301 ("[SCSI] be2iscsi: add 10Gbps iSCSI - BladeEngine 2 driver")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Hi ,

Could you please review and give your feedback ,when time permits.

Thanks
Shahid.

Quoted 32 lines · On 2026-11-07 09:43 +0000, [email protected] wrote:

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

commit 3a59c9fd50bd58745d465b3451671b988442c55a
Author: Mohammad Shahid <[email protected]>

Quoted 5 lines

scsi: be2iscsi: Fix NULL pointer dereference in memory cleanup

During memory allocation ,DMA memory may be allocated successfully before the descriptor array allocation fails.

Quoted 7 lines

In this case ,mem_descr->mem_array is NULL,but cleanup path tries to access it.

Use mem_arr_orig to free the DMA allocation of the current descriptor and handle previously allocated descriptors separately.

[Severity: Low]
Since this fixes the error cleanup loop in beiscsi_alloc_mem() from the initial driver commit, should the commit message include:

Fixes 6733b39a1301 ("[SCSI] be2iscsi: add 10Gbps iSCSI - BladeEngine 2 driver")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to [email protected]

Hi ,

could you please review and give your feedback, when time permits.

Thanks
Shahid.

Quoted 68 lines · On 2026-11-07 15:06 +0530, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

During memory allocation ,DMA memory may be allocated successfully before the descriptor array allocation fails.

In this case ,mem_descr->mem_array is NULL,but cleanup path tries to access it.

Use mem_arr_orig to free the DMA allocation of the current descriptor and handle previously allocated descriptors separately.

---
 drivers/scsi/be2iscsi/be_main.c | 34 ++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/scsi/be2iscsi/be_main.c +21 -13
index fd18d4d3d219..95706b5db7be 100644
@@ -2549,22 +2549,30 @@ 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)) {
+	/* Free DMA allocations of the current descriptor */
+	for (; j > 0; j--) {
+		dma_free_coherent(&phba->pcidev->dev,
+			mem_arr_orig[j - 1].size,
+			mem_arr_orig[j - 1].virtual_address,
+			(unsigned long)
+			mem_arr_orig[j - 1].bus_address.u.a64.address);
+	}
+
+	/* Free previously completed descriptors */
+	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_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);
 		}
+
+		kfree(mem_descr->mem_array);
+
 	}
 	kfree(mem_arr_orig);
 	kfree(phba->init_mem);
Signature · 1 line
2.43.0