335584f
From: Paolo Bonzini <pbonzini@redhat.com>
335584f
Date: Thu, 1 Jun 2017 17:23:13 +0200
335584f
Subject: [PATCH] megasas: do not read command more than once from frame
335584f
335584f
Avoid TOC-TOU bugs by passing the frame_cmd down, and checking
335584f
cmd->dcmd_opcode instead of cmd->frame->header.frame_cmd.
335584f
335584f
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
335584f
(cherry picked from commit 36c327a69d723571f02a7691631667cdb1865ee1)
335584f
---
335584f
 hw/scsi/megasas.c | 60 +++++++++++++++++++++++--------------------------------
335584f
 1 file changed, 25 insertions(+), 35 deletions(-)
335584f
335584f
diff --git a/hw/scsi/megasas.c b/hw/scsi/megasas.c
335584f
index a3f75c1650..38e0a2f5ef 100644
335584f
--- a/hw/scsi/megasas.c
335584f
+++ b/hw/scsi/megasas.c
335584f
@@ -1591,12 +1591,13 @@ static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
335584f
 }
335584f
 
335584f
 static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
335584f
-                                        SCSIRequest *req)
335584f
+                                        SCSIRequest *req, size_t resid)
335584f
 {
335584f
     int retval = MFI_STAT_OK;
335584f
     int lun = req->lun;
335584f
 
335584f
     trace_megasas_dcmd_internal_finish(cmd->index, cmd->dcmd_opcode, lun);
335584f
+    cmd->iov_size -= resid;
335584f
     switch (cmd->dcmd_opcode) {
335584f
     case MFI_DCMD_PD_GET_INFO:
335584f
         retval = megasas_pd_get_info_submit(req->dev, lun, cmd);
335584f
@@ -1649,11 +1650,12 @@ static int megasas_enqueue_req(MegasasCmd *cmd, bool is_write)
335584f
 }
335584f
 
335584f
 static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
335584f
-                               bool is_logical)
335584f
+                               int frame_cmd)
335584f
 {
335584f
     uint8_t *cdb;
335584f
     bool is_write;
335584f
     struct SCSIDevice *sdev = NULL;
335584f
+    bool is_logical = (frame_cmd == MFI_CMD_LD_SCSI_IO);
335584f
 
335584f
     cdb = cmd->frame->pass.cdb;
335584f
 
335584f
@@ -1661,7 +1663,7 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
335584f
         if (cmd->frame->header.target_id >= MFI_MAX_LD ||
335584f
             cmd->frame->header.lun_id != 0) {
335584f
             trace_megasas_scsi_target_not_present(
335584f
-                mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
335584f
+                mfi_frame_desc[frame_cmd], is_logical,
335584f
                 cmd->frame->header.target_id, cmd->frame->header.lun_id);
335584f
             return MFI_STAT_DEVICE_NOT_FOUND;
335584f
         }
335584f
@@ -1671,19 +1673,20 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
335584f
 
335584f
     cmd->iov_size = le32_to_cpu(cmd->frame->header.data_len);
335584f
     trace_megasas_handle_scsi(mfi_frame_desc[cmd->frame->header.frame_cmd],
335584f
-                              is_logical, cmd->frame->header.target_id,
335584f
+    trace_megasas_handle_scsi(mfi_frame_desc[frame_cmd], is_logical,
335584f
+                              cmd->frame->header.target_id,
335584f
                               cmd->frame->header.lun_id, sdev, cmd->iov_size);
335584f
 
335584f
     if (!sdev || (megasas_is_jbod(s) && is_logical)) {
335584f
         trace_megasas_scsi_target_not_present(
335584f
-            mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
335584f
+            mfi_frame_desc[frame_cmd], is_logical,
335584f
             cmd->frame->header.target_id, cmd->frame->header.lun_id);
335584f
         return MFI_STAT_DEVICE_NOT_FOUND;
335584f
     }
335584f
 
335584f
     if (cmd->frame->header.cdb_len > 16) {
335584f
         trace_megasas_scsi_invalid_cdb_len(
335584f
-                mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical,
335584f
+                mfi_frame_desc[frame_cmd], is_logical,
335584f
                 cmd->frame->header.target_id, cmd->frame->header.lun_id,
335584f
                 cmd->frame->header.cdb_len);
335584f
         megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
335584f
@@ -1703,7 +1706,7 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
335584f
                             cmd->frame->header.lun_id, cdb, cmd);
335584f
     if (!cmd->req) {
335584f
         trace_megasas_scsi_req_alloc_failed(
335584f
-                mfi_frame_desc[cmd->frame->header.frame_cmd],
335584f
+                mfi_frame_desc[frame_cmd],
335584f
                 cmd->frame->header.target_id, cmd->frame->header.lun_id);
335584f
         megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
335584f
         cmd->frame->header.scsi_status = BUSY;
335584f
@@ -1725,11 +1728,11 @@ static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd,
335584f
     return MFI_STAT_INVALID_STATUS;
335584f
 }
335584f
 
335584f
-static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd)
335584f
+static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_cmd)
335584f
 {
335584f
     uint32_t lba_count, lba_start_hi, lba_start_lo;
335584f
     uint64_t lba_start;
335584f
-    bool is_write = (cmd->frame->header.frame_cmd == MFI_CMD_LD_WRITE);
335584f
+    bool is_write = (frame_cmd == MFI_CMD_LD_WRITE);
335584f
     uint8_t cdb[16];
335584f
     int len;
335584f
     struct SCSIDevice *sdev = NULL;
335584f
@@ -1746,20 +1749,20 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd)
335584f
     }
335584f
 
335584f
     trace_megasas_handle_io(cmd->index,
335584f
-                            mfi_frame_desc[cmd->frame->header.frame_cmd],
335584f
+                            mfi_frame_desc[frame_cmd],
335584f
                             cmd->frame->header.target_id,
335584f
                             cmd->frame->header.lun_id,
335584f
                             (unsigned long)lba_start, (unsigned long)lba_count);
335584f
     if (!sdev) {
335584f
         trace_megasas_io_target_not_present(cmd->index,
335584f
-            mfi_frame_desc[cmd->frame->header.frame_cmd],
335584f
+            mfi_frame_desc[frame_cmd],
335584f
             cmd->frame->header.target_id, cmd->frame->header.lun_id);
335584f
         return MFI_STAT_DEVICE_NOT_FOUND;
335584f
     }
335584f
 
335584f
     if (cmd->frame->header.cdb_len > 16) {
335584f
         trace_megasas_scsi_invalid_cdb_len(
335584f
-            mfi_frame_desc[cmd->frame->header.frame_cmd], 1,
335584f
+            mfi_frame_desc[frame_cmd], 1,
335584f
             cmd->frame->header.target_id, cmd->frame->header.lun_id,
335584f
             cmd->frame->header.cdb_len);
335584f
         megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE));
335584f
@@ -1781,7 +1784,7 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd)
335584f
                             cmd->frame->header.lun_id, cdb, cmd);
335584f
     if (!cmd->req) {
335584f
         trace_megasas_scsi_req_alloc_failed(
335584f
-            mfi_frame_desc[cmd->frame->header.frame_cmd],
335584f
+            mfi_frame_desc[frame_cmd],
335584f
             cmd->frame->header.target_id, cmd->frame->header.lun_id);
335584f
         megasas_write_sense(cmd, SENSE_CODE(NO_SENSE));
335584f
         cmd->frame->header.scsi_status = BUSY;
335584f
@@ -1799,23 +1802,11 @@ static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd)
335584f
     return MFI_STAT_INVALID_STATUS;
335584f
 }
335584f
 
335584f
-static int megasas_finish_internal_command(MegasasCmd *cmd,
335584f
-                                           SCSIRequest *req, size_t resid)
335584f
-{
335584f
-    int retval = MFI_STAT_INVALID_CMD;
335584f
-
335584f
-    if (cmd->frame->header.frame_cmd == MFI_CMD_DCMD) {
335584f
-        cmd->iov_size -= resid;
335584f
-        retval = megasas_finish_internal_dcmd(cmd, req);
335584f
-    }
335584f
-    return retval;
335584f
-}
335584f
-
335584f
 static QEMUSGList *megasas_get_sg_list(SCSIRequest *req)
335584f
 {
335584f
     MegasasCmd *cmd = req->hba_private;
335584f
 
335584f
-    if (cmd->frame->header.frame_cmd == MFI_CMD_DCMD) {
335584f
+    if (cmd->dcmd_opcode != -1) {
335584f
         return NULL;
335584f
     } else {
335584f
         return &cmd->qsg;
335584f
@@ -1829,7 +1820,7 @@ static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
335584f
 
335584f
     trace_megasas_io_complete(cmd->index, len);
335584f
 
335584f
-    if (cmd->frame->header.frame_cmd != MFI_CMD_DCMD) {
335584f
+    if (cmd->dcmd_opcode != -1) {
335584f
         scsi_req_continue(req);
335584f
         return;
335584f
     }
335584f
@@ -1872,7 +1863,7 @@ static void megasas_command_complete(SCSIRequest *req, uint32_t status,
335584f
         /*
335584f
          * Internal command complete
335584f
          */
335584f
-        cmd_status = megasas_finish_internal_command(cmd, req, resid);
335584f
+        cmd_status = megasas_finish_internal_dcmd(cmd, req, resid);
335584f
         if (cmd_status == MFI_STAT_INVALID_STATUS) {
335584f
             return;
335584f
         }
335584f
@@ -1943,6 +1934,7 @@ static void megasas_handle_frame(MegasasState *s, uint64_t frame_addr,
335584f
 {
335584f
     uint8_t frame_status = MFI_STAT_INVALID_CMD;
335584f
     uint64_t frame_context;
335584f
+    int frame_cmd;
335584f
     MegasasCmd *cmd;
335584f
 
335584f
     /*
335584f
@@ -1961,7 +1953,8 @@ static void megasas_handle_frame(MegasasState *s, uint64_t frame_addr,
335584f
         s->event_count++;
335584f
         return;
335584f
     }
335584f
-    switch (cmd->frame->header.frame_cmd) {
335584f
+    frame_cmd = cmd->frame->header.frame_cmd;
335584f
+    switch (frame_cmd) {
335584f
     case MFI_CMD_INIT:
335584f
         frame_status = megasas_init_firmware(s, cmd);
335584f
         break;
335584f
@@ -1972,18 +1965,15 @@ static void megasas_handle_frame(MegasasState *s, uint64_t frame_addr,
335584f
         frame_status = megasas_handle_abort(s, cmd);
335584f
         break;
335584f
     case MFI_CMD_PD_SCSI_IO:
335584f
-        frame_status = megasas_handle_scsi(s, cmd, 0);
335584f
-        break;
335584f
     case MFI_CMD_LD_SCSI_IO:
335584f
-        frame_status = megasas_handle_scsi(s, cmd, 1);
335584f
+        frame_status = megasas_handle_scsi(s, cmd, frame_cmd);
335584f
         break;
335584f
     case MFI_CMD_LD_READ:
335584f
     case MFI_CMD_LD_WRITE:
335584f
-        frame_status = megasas_handle_io(s, cmd);
335584f
+        frame_status = megasas_handle_io(s, cmd, frame_cmd);
335584f
         break;
335584f
     default:
335584f
-        trace_megasas_unhandled_frame_cmd(cmd->index,
335584f
-                                          cmd->frame->header.frame_cmd);
335584f
+        trace_megasas_unhandled_frame_cmd(cmd->index, frame_cmd);
335584f
         s->event_count++;
335584f
         break;
335584f
     }