cf91b1d
From: Prasad J Pandit <pjp@fedoraproject.org>
cf91b1d
Date: Thu, 16 Jun 2016 00:22:35 +0200
cf91b1d
Subject: [PATCH] scsi: esp: make cmdbuf big enough for maximum CDB size
cf91b1d
cf91b1d
While doing DMA read into ESP command buffer 's->cmdbuf', it could
cf91b1d
write past the 's->cmdbuf' area, if it was transferring more than 16
cf91b1d
bytes.  Increase the command buffer size to 32, which is maximum when
cf91b1d
's->do_cmd' is set, and add a check on 'len' to avoid OOB access.
cf91b1d
cf91b1d
Reported-by: Li Qiang <liqiang6-s@360.cn>
cf91b1d
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
cf91b1d
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
cf91b1d
(cherry picked from commit 926cde5f3e4d2504ed161ed0cb771ac7cad6fd11)
cf91b1d
---
cf91b1d
 hw/scsi/esp.c         | 6 ++++--
cf91b1d
 include/hw/scsi/esp.h | 3 ++-
cf91b1d
 2 files changed, 6 insertions(+), 3 deletions(-)
cf91b1d
cf91b1d
diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
cf91b1d
index 68d3e4d..b4601ad 100644
cf91b1d
--- a/hw/scsi/esp.c
cf91b1d
+++ b/hw/scsi/esp.c
cf91b1d
@@ -248,6 +248,8 @@ static void esp_do_dma(ESPState *s)
cf91b1d
     len = s->dma_left;
cf91b1d
     if (s->do_cmd) {
cf91b1d
         trace_esp_do_dma(s->cmdlen, len);
cf91b1d
+        assert (s->cmdlen <= sizeof(s->cmdbuf) &&
cf91b1d
+                len <= sizeof(s->cmdbuf) - s->cmdlen);
cf91b1d
         s->dma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
cf91b1d
         return;
cf91b1d
     }
cf91b1d
@@ -345,7 +347,7 @@ static void handle_ti(ESPState *s)
cf91b1d
     s->dma_counter = dmalen;
cf91b1d
 
cf91b1d
     if (s->do_cmd)
cf91b1d
-        minlen = (dmalen < 32) ? dmalen : 32;
cf91b1d
+        minlen = (dmalen < ESP_CMDBUF_SZ) ? dmalen : ESP_CMDBUF_SZ;
cf91b1d
     else if (s->ti_size < 0)
cf91b1d
         minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
cf91b1d
     else
cf91b1d
@@ -451,7 +453,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
cf91b1d
         break;
cf91b1d
     case ESP_FIFO:
cf91b1d
         if (s->do_cmd) {
cf91b1d
-            if (s->cmdlen < TI_BUFSZ) {
cf91b1d
+            if (s->cmdlen < ESP_CMDBUF_SZ) {
cf91b1d
                 s->cmdbuf[s->cmdlen++] = val & 0xff;
cf91b1d
             } else {
cf91b1d
                 trace_esp_error_fifo_overrun();
cf91b1d
diff --git a/include/hw/scsi/esp.h b/include/hw/scsi/esp.h
cf91b1d
index 6c79527..d2c4886 100644
cf91b1d
--- a/include/hw/scsi/esp.h
cf91b1d
+++ b/include/hw/scsi/esp.h
cf91b1d
@@ -14,6 +14,7 @@ void esp_init(hwaddr espaddr, int it_shift,
cf91b1d
 
cf91b1d
 #define ESP_REGS 16
cf91b1d
 #define TI_BUFSZ 16
cf91b1d
+#define ESP_CMDBUF_SZ 32
cf91b1d
 
cf91b1d
 typedef struct ESPState ESPState;
cf91b1d
 
cf91b1d
@@ -31,7 +32,7 @@ struct ESPState {
cf91b1d
     SCSIBus bus;
cf91b1d
     SCSIDevice *current_dev;
cf91b1d
     SCSIRequest *current_req;
cf91b1d
-    uint8_t cmdbuf[TI_BUFSZ];
cf91b1d
+    uint8_t cmdbuf[ESP_CMDBUF_SZ];
cf91b1d
     uint32_t cmdlen;
cf91b1d
     uint32_t do_cmd;
cf91b1d