264ecb5
------------------------------------------------------------------------
264ecb5
*From*: 	P J P
264ecb5
*Subject*: 	[Qemu-devel] [PATCH v3] scsi: esp: check TI buffer index
264ecb5
before read/write
264ecb5
*Date*: 	Mon, 6 Jun 2016 22:04:43 +0530
264ecb5
264ecb5
------------------------------------------------------------------------
264ecb5
264ecb5
From: Prasad J Pandit <address@hidden>
264ecb5
264ecb5
The 53C9X Fast SCSI Controller(FSC) comes with internal 16-byte
264ecb5
FIFO buffers. One is used to handle commands and other is for
264ecb5
information transfer. Three control variables 'ti_rptr',
264ecb5
'ti_wptr' and 'ti_size' are used to control r/w access to the
264ecb5
information transfer buffer ti_buf[TI_BUFSZ=16]. In that,
264ecb5
264ecb5
'ti_rptr' is used as read index, where read occurs.
264ecb5
'ti_wptr' is a write index, where write would occur.
264ecb5
'ti_size' indicates total bytes to be read from the buffer.
264ecb5
264ecb5
While reading/writing to this buffer, index could exceed its
264ecb5
size. Add check to avoid OOB r/w access.
264ecb5
264ecb5
Reported-by: Huawei PSIRT <address@hidden>
264ecb5
Reported-by: Li Qiang <address@hidden>
264ecb5
Signed-off-by: Prasad J Pandit <address@hidden>
264ecb5
---
264ecb5
 hw/scsi/esp.c | 20 +++++++++-----------
264ecb5
 1 file changed, 9 insertions(+), 11 deletions(-)
264ecb5
264ecb5
Update as per:
264ecb5
  -> https://lists.gnu.org/archive/html/qemu-devel/2016-06/msg01326.html
264ecb5
264ecb5
diff --git a/tools/qemu-xen-traditional/hw/esp.c b/tools/qemu-xen-traditional/hw/esp.c
264ecb5
index c2f6f8f..4b94bbc 100644
264ecb5
--- a/tools/qemu-xen-traditional/hw/esp.c
264ecb5
+++ b/tools/qemu-xen-traditional/hw/esp.c
264ecb5
@@ -403,18 +403,17 @@ uint64_t esp_reg_read(ESPState *s, uint32_t saddr)
264ecb5
     DPRINTF("read reg[%d]: 0x%2.2x\n", saddr, s->rregs[saddr]);
264ecb5
     switch (saddr) {
264ecb5
     case ESP_FIFO:
264ecb5
-        if (s->ti_size > 0) {
264ecb5
+        if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
264ecb5
+            /* Data out.  */
264ecb5
+            ESP_ERROR("PIO data read not implemented\n");
264ecb5
+            s->rregs[ESP_FIFO] = 0;
264ecb5
+            esp_raise_irq(s);
264ecb5
+        } else if (s->ti_rptr < s->ti_wptr) {
264ecb5
             s->ti_size--;
264ecb5
-            if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
264ecb5
-                /* Data out.  */
264ecb5
-                ESP_ERROR("PIO data read not implemented\n");
264ecb5
-                s->rregs[ESP_FIFO] = 0;
264ecb5
-            } else {
264ecb5
-                s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
264ecb5
-            }
264ecb5
+            s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
264ecb5
             esp_raise_irq(s);
264ecb5
         }
264ecb5
-        if (s->ti_size == 0) {
264ecb5
+        if (s->ti_rptr == s->ti_wptr) {
264ecb5
             s->ti_rptr = 0;
264ecb5
             s->ti_wptr = 0;
264ecb5
         }
264ecb5
@@ -459,7 +457,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
264ecb5
             } else {
264ecb5
                 ESP_ERROR("fifo overrun\n");
264ecb5
             }
264ecb5
-        } else if (s->ti_size == TI_BUFSZ - 1) {
264ecb5
+        } else if (s->ti_wptr == TI_BUFSZ - 1) {
264ecb5
             ESP_ERROR("fifo overrun\n");
264ecb5
         } else {
264ecb5
             s->ti_size++;
264ecb5
-- 
264ecb5
2.5.5
264ecb5