335584f
From: Paolo Bonzini <pbonzini@redhat.com>
335584f
Date: Thu, 1 Jun 2017 17:18:23 +0200
335584f
Subject: [PATCH] megasas: do not read DCMD opcode more than once from frame
335584f
335584f
Avoid TOC-TOU bugs by storing the DCMD opcode in the MegasasCmd
335584f
335584f
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
335584f
(cherry picked from commit 5104fac8539eaf155fc6de93e164be43e1e62242)
335584f
---
335584f
 hw/scsi/megasas.c | 25 +++++++++++--------------
335584f
 1 file changed, 11 insertions(+), 14 deletions(-)
335584f
335584f
diff --git a/hw/scsi/megasas.c b/hw/scsi/megasas.c
335584f
index c353118882..a3f75c1650 100644
335584f
--- a/hw/scsi/megasas.c
335584f
+++ b/hw/scsi/megasas.c
335584f
@@ -63,6 +63,7 @@ typedef struct MegasasCmd {
335584f
 
335584f
     hwaddr pa;
335584f
     hwaddr pa_size;
335584f
+    uint32_t dcmd_opcode;
335584f
     union mfi_frame *frame;
335584f
     SCSIRequest *req;
335584f
     QEMUSGList qsg;
335584f
@@ -513,6 +514,7 @@ static MegasasCmd *megasas_enqueue_frame(MegasasState *s,
335584f
         cmd->context &= (uint64_t)0xFFFFFFFF;
335584f
     }
335584f
     cmd->count = count;
335584f
+    cmd->dcmd_opcode = -1;
335584f
     s->busy++;
335584f
 
335584f
     if (s->consumer_pa) {
335584f
@@ -1562,22 +1564,21 @@ static const struct dcmd_cmd_tbl_t {
335584f
 
335584f
 static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
335584f
 {
335584f
-    int opcode;
335584f
     int retval = 0;
335584f
     size_t len;
335584f
     const struct dcmd_cmd_tbl_t *cmdptr = dcmd_cmd_tbl;
335584f
 
335584f
-    opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
335584f
-    trace_megasas_handle_dcmd(cmd->index, opcode);
335584f
+    cmd->dcmd_opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
335584f
+    trace_megasas_handle_dcmd(cmd->index, cmd->dcmd_opcode);
335584f
     if (megasas_map_dcmd(s, cmd) < 0) {
335584f
         return MFI_STAT_MEMORY_NOT_AVAILABLE;
335584f
     }
335584f
-    while (cmdptr->opcode != -1 && cmdptr->opcode != opcode) {
335584f
+    while (cmdptr->opcode != -1 && cmdptr->opcode != cmd->dcmd_opcode) {
335584f
         cmdptr++;
335584f
     }
335584f
     len = cmd->iov_size;
335584f
     if (cmdptr->opcode == -1) {
335584f
-        trace_megasas_dcmd_unhandled(cmd->index, opcode, len);
335584f
+        trace_megasas_dcmd_unhandled(cmd->index, cmd->dcmd_opcode, len);
335584f
         retval = megasas_dcmd_dummy(s, cmd);
335584f
     } else {
335584f
         trace_megasas_dcmd_enter(cmd->index, cmdptr->desc, len);
335584f
@@ -1592,13 +1593,11 @@ static int megasas_handle_dcmd(MegasasState *s, MegasasCmd *cmd)
335584f
 static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
335584f
                                         SCSIRequest *req)
335584f
 {
335584f
-    int opcode;
335584f
     int retval = MFI_STAT_OK;
335584f
     int lun = req->lun;
335584f
 
335584f
-    opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
335584f
-    trace_megasas_dcmd_internal_finish(cmd->index, opcode, lun);
335584f
-    switch (opcode) {
335584f
+    trace_megasas_dcmd_internal_finish(cmd->index, cmd->dcmd_opcode, lun);
335584f
+    switch (cmd->dcmd_opcode) {
335584f
     case MFI_DCMD_PD_GET_INFO:
335584f
         retval = megasas_pd_get_info_submit(req->dev, lun, cmd);
335584f
         break;
335584f
@@ -1606,7 +1605,7 @@ static int megasas_finish_internal_dcmd(MegasasCmd *cmd,
335584f
         retval = megasas_ld_get_info_submit(req->dev, lun, cmd);
335584f
         break;
335584f
     default:
335584f
-        trace_megasas_dcmd_internal_invalid(cmd->index, opcode);
335584f
+        trace_megasas_dcmd_internal_invalid(cmd->index, cmd->dcmd_opcode);
335584f
         retval = MFI_STAT_INVALID_DCMD;
335584f
         break;
335584f
     }
335584f
@@ -1827,7 +1826,6 @@ static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
335584f
 {
335584f
     MegasasCmd *cmd = req->hba_private;
335584f
     uint8_t *buf;
335584f
-    uint32_t opcode;
335584f
 
335584f
     trace_megasas_io_complete(cmd->index, len);
335584f
 
335584f
@@ -1837,8 +1835,7 @@ static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
335584f
     }
335584f
 
335584f
     buf = scsi_req_get_buf(req);
335584f
-    opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
335584f
-    if (opcode == MFI_DCMD_PD_GET_INFO && cmd->iov_buf) {
335584f
+    if (cmd->dcmd_opcode == MFI_DCMD_PD_GET_INFO && cmd->iov_buf) {
335584f
         struct mfi_pd_info *info = cmd->iov_buf;
335584f
 
335584f
         if (info->inquiry_data[0] == 0x7f) {
335584f
@@ -1849,7 +1846,7 @@ static void megasas_xfer_complete(SCSIRequest *req, uint32_t len)
335584f
             memcpy(info->vpd_page83, buf, len);
335584f
         }
335584f
         scsi_req_continue(req);
335584f
-    } else if (opcode == MFI_DCMD_LD_GET_INFO) {
335584f
+    } else if (cmd->dcmd_opcode == MFI_DCMD_LD_GET_INFO) {
335584f
         struct mfi_ld_info *info = cmd->iov_buf;
335584f
 
335584f
         if (cmd->iov_buf) {