dcavalca / rpms / qemu

Forked from rpms/qemu a year ago
Clone

Blame 0009-megasas-do-not-read-command-more-than-once-from-fram.patch

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