Skip to contentSkip to conversation

scsi: esp: check TI buffer index before read/write

PATCH Superseded P J P 3 participants 4 messages May 31, 2016 – Jun 6, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH v2 1/1 scsi: esp: check TI buffer index before read/write

From: Prasad J Pandit <[email protected]>

The 53C9X Fast SCSI Controller(FSC) comes with internal 16-byte FIFO buffers. One is used to handle commands and other is for information transfer. While reading/writing to these buffers an index into 's->ti_buf[TI_BUFSZ=16]' could exceed its size. Add check to avoid OOB r/w access.

---
 hw/scsi/esp.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

Update as per
-> https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg05506.html

hw/scsi/esp.c +8 -10
index 591c817..60c1b28 100644
@@ -400,16 +400,14 @@ uint64_t esp_reg_read(ESPState *s, uint32_t saddr)
     trace_esp_mem_readb(saddr, s->rregs[saddr]);
     switch (saddr) {
     case ESP_FIFO:
-        if (s->ti_size > 0) {
+        if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
+            /* Data out.  */
+            qemu_log_mask(LOG_UNIMP, "esp: PIO data read not implemented\n");
+            s->rregs[ESP_FIFO] = 0;
+            esp_raise_irq(s);
+        } else if (s->ti_rptr < s->ti_wptr) {
             s->ti_size--;
-            if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
-                /* Data out.  */
-                qemu_log_mask(LOG_UNIMP,
-                              "esp: PIO data read not implemented\n");
-                s->rregs[ESP_FIFO] = 0;
-            } else {
-                s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
-            }
+            s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
             esp_raise_irq(s);
         }
         if (s->ti_size == 0) {
@@ -456,7 +454,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
             } else {
                 trace_esp_error_fifo_overrun();
             }
-        } else if (s->ti_size == TI_BUFSZ - 1) {
+        } else if (s->ti_wptr == TI_BUFSZ - 1) {
             trace_esp_error_fifo_overrun();
         } else {
             s->ti_size++;
Signature · 1 line
2.5.5
author
#1 lore raw
+-- On Tue, 31 May 2016, P J P wrote --+
|      switch (saddr) {
|      case ESP_FIFO:
| -        if (s->ti_size > 0) {
| +        if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
| +            /* Data out.  */
| +            qemu_log_mask(LOG_UNIMP, "esp: PIO data read not implemented\n");
| +            s->rregs[ESP_FIFO] = 0;
| +            esp_raise_irq(s);
| +        } else if (s->ti_rptr < s->ti_wptr) {
|              s->ti_size--;
| -            if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
| -                /* Data out.  */
| -                qemu_log_mask(LOG_UNIMP,
| -                              "esp: PIO data read not implemented\n");
| -                s->rregs[ESP_FIFO] = 0;
| -            } else {
| -                s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
| -            }
| +            s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
|              esp_raise_irq(s);
|          }
|          if (s->ti_size == 0) {
| @@ -456,7 +454,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
|              } else {
|                  trace_esp_error_fifo_overrun();
|              }
| -        } else if (s->ti_size == TI_BUFSZ - 1) {
| +        } else if (s->ti_wptr == TI_BUFSZ - 1) {
|              trace_esp_error_fifo_overrun();
|          } else {
|              s->ti_size++;

Ping..!
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#2 lore raw
In reply to P J P
Quoted 9 lines · P J P
hw/scsi/esp.c +6 -2
+        if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
+            /* Data out.  */
+            qemu_log_mask(LOG_UNIMP, "esp: PIO data read not implemented\n");
+            s->rregs[ESP_FIFO] = 0;
+            esp_raise_irq(s);
+        } else if (s->ti_rptr < s->ti_wptr) {
             s->ti_size--;
-            if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
-                /* Data out.  */

hw/scsi/esp.c uint64_t esp_reg_read(ESPState *s, uint32_t saddr) · line 413

Quoted 10 lines
hw/scsi/esp.c +1 -6
-                qemu_log_mask(LOG_UNIMP,
-                              "esp: PIO data read not implemented\n");
-                s->rregs[ESP_FIFO] = 0;
-            } else {
-                s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
-            }
+            s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
             esp_raise_irq(s);
         }
         if (s->ti_size == 0) {

Shouldn't this become a "s->ti_rptr == s->ti_wptr" too?

Thanks,

Paolo

#3 lore raw
Quoted 23 lines · Paolo Bonzini
Quoted 9 lines · P J P
hw/scsi/esp.c +6 -2
+        if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
+            /* Data out.  */
+            qemu_log_mask(LOG_UNIMP, "esp: PIO data read not implemented\n");
+            s->rregs[ESP_FIFO] = 0;
+            esp_raise_irq(s);
+        } else if (s->ti_rptr < s->ti_wptr) {
             s->ti_size--;
-            if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
-                /* Data out.  */

hw/scsi/esp.c uint64_t esp_reg_read(ESPState *s, uint32_t saddr) · line 413

Quoted 10 lines
hw/scsi/esp.c +1 -6
-                qemu_log_mask(LOG_UNIMP,
-                              "esp: PIO data read not implemented\n");
-                s->rregs[ESP_FIFO] = 0;
-            } else {
-                s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
-            }
+            s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
             esp_raise_irq(s);
         }
         if (s->ti_size == 0) {
Quoted 1 line

Shouldn't this become a "s->ti_rptr == s->ti_wptr" too?

It would probably be helpful to document what the intended invariant relating ti_rptr, ti_wptr and ti_size is as well.

thanks
-- PMM