From: Suyuan Wang <[email protected]>

ibmvfc_bsg_request() calls bsg_job_done() before unmapping the request
and reply scatterlists.

bsg_job_done() starts block request completion, which can run
bsg_teardown_job() and free both scatterlists before the following
dma_unmap_sg() calls. The unmaps can then read freed memory.

Complete the job after the common DMA cleanup instead. The completion
path has already saved the operation result in bsg_reply and set rc to
zero; paths that return an error keep their existing cleanup and leave
completion to the FC transport. passthru_mutex stays held until after
the completion call.

Found with a static analysis tool and checked by hand.

Fixes: d31429e1517c ("[SCSI] ibmvfc: Add FC Passthru support")
Cc: [email protected]
Co-developed-by: Kaixuan Li <[email protected]>
Signed-off-by: Kaixuan Li <[email protected]>
Signed-off-by: Suyuan Wang <[email protected]>
---
Compile tested on powerpc with clang (W=1, no new warnings). I don't
have IBM Power virtual FC hardware.
---
 drivers/scsi/ibmvscsi/ibmvfc-core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/ibmvscsi/ibmvfc-core.c 
b/drivers/scsi/ibmvscsi/ibmvfc-core.c
index aa7ae81df41b..6dce72cb80b5 100644
--- a/drivers/scsi/ibmvscsi/ibmvfc-core.c
+++ b/drivers/scsi/ibmvscsi/ibmvfc-core.c
@@ -2392,14 +2392,15 @@ static int ibmvfc_bsg_request(struct bsg_job *job)
        ibmvfc_free_event(evt);
        spin_unlock_irqrestore(&vhost->host->host_lock, flags);
        bsg_reply->result = rc;
-       bsg_job_done(job, bsg_reply->result,
-                      bsg_reply->reply_payload_rcv_len);
        rc = 0;
 out:
        dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
                     job->request_payload.sg_cnt, DMA_TO_DEVICE);
        dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
                     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
+       if (!rc)
+               bsg_job_done(job, bsg_reply->result,
+                            bsg_reply->reply_payload_rcv_len);
        mutex_unlock(&vhost->passthru_mutex);
        LEAVE;
        return rc;





Reply via email to