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