Skip to contentSkip to conversation

scsi: storvsc: Fix storvsc_queuecommand() memory leak

PATCH Applied Juan Vazquez 3 participants 4 messages Jan 9, 2022 – Jan 10, 2022

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

PATCH 1/1 scsi: storvsc: Fix storvsc_queuecommand() memory leak

Fix possible memory leak in error path of storvsc_queuecommand() when DMA mapping fails.

Fixes 743b237c3a7b ("scsi: storvsc: Add Isolation VM support for storvsc driver")
---
 drivers/scsi/storvsc_drv.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/scsi/storvsc_drv.c +12 -5
index 2273b843d9d2..9a0bba5a51a7 100644
@@ -1850,8 +1850,10 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
 		payload->range.offset = offset_in_hvpg;
 
 		sg_count = scsi_dma_map(scmnd);
-		if (sg_count < 0)
-			return SCSI_MLQUEUE_DEVICE_BUSY;
+		if (sg_count < 0) {
+			ret = SCSI_MLQUEUE_DEVICE_BUSY;
+			goto err_free_payload;
+		}
 
 		for_each_sg(sgl, sg, sg_count, j) {
 			/*
@@ -1886,13 +1888,18 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
 	put_cpu();
 
 	if (ret == -EAGAIN) {
-		if (payload_sz > sizeof(cmd_request->mpb))
-			kfree(payload);
 		/* no more space */
-		return SCSI_MLQUEUE_DEVICE_BUSY;
+		ret = SCSI_MLQUEUE_DEVICE_BUSY;
+		goto err_free_payload;
 	}
 
 	return 0;
+
+err_free_payload:
+	if (payload_sz > sizeof(cmd_request->mpb))
+		kfree(payload);
+
+	return ret;
 }
 
 static struct scsi_host_template scsi_driver = {
Signature · 1 line
2.32.0
maintainer
#1 lore raw
Quoted 5 lines · Juan Vazquez

Fix possible memory leak in error path of storvsc_queuecommand() when DMA mapping fails.

Fixes 743b237c3a7b ("scsi: storvsc: Add Isolation VM support for storvsc driver")

Martin, I can pick this up since the offending commit is not yet in Linus' tree.

Tianyu, Long and Michael, the change makes sense to me but can you give an ack or review here?

Quoted 46 lines
---
 drivers/scsi/storvsc_drv.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/scsi/storvsc_drv.c +12 -5
index 2273b843d9d2..9a0bba5a51a7 100644
@@ -1850,8 +1850,10 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
 		payload->range.offset = offset_in_hvpg;
 
 		sg_count = scsi_dma_map(scmnd);
-		if (sg_count < 0)
-			return SCSI_MLQUEUE_DEVICE_BUSY;
+		if (sg_count < 0) {
+			ret = SCSI_MLQUEUE_DEVICE_BUSY;
+			goto err_free_payload;
+		}
 
 		for_each_sg(sgl, sg, sg_count, j) {
 			/*
@@ -1886,13 +1888,18 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
 	put_cpu();
 
 	if (ret == -EAGAIN) {
-		if (payload_sz > sizeof(cmd_request->mpb))
-			kfree(payload);
 		/* no more space */
-		return SCSI_MLQUEUE_DEVICE_BUSY;
+		ret = SCSI_MLQUEUE_DEVICE_BUSY;
+		goto err_free_payload;
 	}
 
 	return 0;
+
+err_free_payload:
+	if (payload_sz > sizeof(cmd_request->mpb))
+		kfree(payload);
+
+	return ret;
 }
 
 static struct scsi_host_template scsi_driver = {
Signature · 1 line
2.32.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Juan Vazquez
Quoted 5 lines · Juan Vazquez

Fix possible memory leak in error path of storvsc_queuecommand() when DMA mapping fails.

Fixes 743b237c3a7b ("scsi: storvsc: Add Isolation VM support for storvsc driver")

Looks good. Thanks for the fix patch.

Quoted 43 lines
---
  drivers/scsi/storvsc_drv.c | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)
drivers/scsi/storvsc_drv.c +12 -5
index 2273b843d9d2..9a0bba5a51a7 100644
@@ -1850,8 +1850,10 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  		payload->range.offset = offset_in_hvpg;
 
  		sg_count = scsi_dma_map(scmnd);
-		if (sg_count < 0)
-			return SCSI_MLQUEUE_DEVICE_BUSY;
+		if (sg_count < 0) {
+			ret = SCSI_MLQUEUE_DEVICE_BUSY;
+			goto err_free_payload;
+		}
 
  		for_each_sg(sgl, sg, sg_count, j) {
  			/*
@@ -1886,13 +1888,18 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
  	put_cpu();
 
  	if (ret == -EAGAIN) {
-		if (payload_sz > sizeof(cmd_request->mpb))
-			kfree(payload);
  		/* no more space */
-		return SCSI_MLQUEUE_DEVICE_BUSY;
+		ret = SCSI_MLQUEUE_DEVICE_BUSY;
+		goto err_free_payload;
  	}
 
  	return 0;
+
+err_free_payload:
+	if (payload_sz > sizeof(cmd_request->mpb))
+		kfree(payload);
+
+	return ret;
  }
 
  static struct scsi_host_template scsi_driver = {
maintainer Applied
#3 lore raw
Quoted 10 lines · Tianyu Lan
Quoted 5 lines · Juan Vazquez

Fix possible memory leak in error path of storvsc_queuecommand() when DMA mapping fails.

Fixes 743b237c3a7b ("scsi: storvsc: Add Isolation VM support for storvsc driver")

Looks good. Thanks for the fix patch.

Applied. Thanks.