Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: Ruoyu Wang <ruoyuw560@gmail.com>
To: Ketan Mukadam <ketan.mukadam@broadcom.com>,
	"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ruoyu Wang <ruoyuw560@gmail.com>
Subject: [PATCH] scsi: be2iscsi: fix cleanup on memory allocation failure
Date: Sun,  7 Jun 2026 19:03:39 +0800	[thread overview]
Message-ID: <20260607110339.3-1-ruoyuw560@gmail.com> (raw)

beiscsi_alloc_mem() builds each memory descriptor from DMA chunks
recorded in the temporary mem_arr_orig array. After all chunks for the
current descriptor have been allocated, the code allocates
mem_descr->mem_array and copies the temporary descriptors into it.

If mem_descr->mem_array allocation fails, or if DMA allocation fails
after some chunks have been allocated for the current descriptor, the
error path records the current chunk count in mem_descr->num_elements and
then dereferences mem_descr->mem_array. That pointer has not been
allocated for the current descriptor, so the cleanup path can dereference
NULL and also fail to release the current DMA chunks.

Release the current descriptor chunks from mem_arr_orig first, then walk
backward over the descriptors that were fully initialized earlier.

Fixes: 6733b39a1301 ("[SCSI] be2iscsi: add 10Gbps iSCSI - BladeEngine 2 driver")
Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>
---
 drivers/scsi/be2iscsi/be_main.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/be2iscsi/be_main.c b/drivers/scsi/be2iscsi/be_main.c
index fd18d4d3d..31c617bb6 100644
--- a/drivers/scsi/be2iscsi/be_main.c
+++ b/drivers/scsi/be2iscsi/be_main.c
@@ -2549,8 +2549,18 @@ 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)) {
+	while (j) {
+		j--;
+		bus_add = mem_arr_orig[j].bus_address.u.a64.address;
+		dma_free_coherent(&phba->pcidev->dev,
+				  mem_arr_orig[j].size,
+				  mem_arr_orig[j].virtual_address,
+				  bus_add);
+	}
+
+	while (i) {
+		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,
@@ -2560,11 +2570,7 @@ free_mem:
 					    mem_array[j - 1].
 					    bus_address.u.a64.address);
 		}
-		if (i) {
-			i--;
-			kfree(mem_descr->mem_array);
-			mem_descr--;
-		}
+		kfree(mem_descr->mem_array);
 	}
 	kfree(mem_arr_orig);
 	kfree(phba->init_mem);
-- 
2.51.0


                 reply	other threads:[~2026-06-07 11:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260607110339.3-1-ruoyuw560@gmail.com \
    --to=ruoyuw560@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=ketan.mukadam@broadcom.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox