Skip to contentSkip to conversation

scsi: esp: check length before dma read

PATCH In review P J P 3 participants 5 messages Jun 15, 2016 – Jun 17, 2016

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH v4 1/1 scsi: esp: check length before dma read

From: Prasad J Pandit <[email protected]>

While doing DMA read into ESP command buffer 's->cmdbuf', it could write past the 's->cmdbuf' area, if it was partially filled; ie. 's->cmdlen' wasn't set at the start of the buffer. Check 'len' to avoid OOB access. Also increase the command buffer size to 32, which is maximum when 's->do_cmd' is set.

---
 hw/scsi/esp.c         | 6 ++++--
 include/hw/scsi/esp.h | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)

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

hw/scsi/esp.c +4 -2
index 4b94bbc..cc74ba8 100644
@@ -249,6 +249,8 @@ static void esp_do_dma(ESPState *s)
     len = s->dma_left;
     if (s->do_cmd) {
         trace_esp_do_dma(s->cmdlen, len);
+        assert (s->cmdlen <= sizeof(s->cmdbuf) &&
+                len <= sizeof(s->cmdbuf) - s->cmdlen);
         s->dma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
         s->ti_size = 0;
         s->cmdlen = 0;
@@ -348,7 +350,7 @@ static void handle_ti(ESPState *s)
     s->dma_counter = dmalen;
 
     if (s->do_cmd)
-        minlen = (dmalen < 32) ? dmalen : 32;
+        minlen = (dmalen < ESP_CMDBUF_SZ) ? dmalen : ESP_CMDBUF_SZ;
     else if (s->ti_size < 0)
         minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
     else
@@ -452,7 +454,7 @@ void esp_reg_write(ESPState *s, uint32_t saddr, uint64_t val)
         break;
     case ESP_FIFO:
         if (s->do_cmd) {
-            if (s->cmdlen < TI_BUFSZ) {
+            if (s->cmdlen < ESP_CMDBUF_SZ) {
                 s->cmdbuf[s->cmdlen++] = val & 0xff;
             } else {
                 trace_esp_error_fifo_overrun();
include/hw/scsi/esp.h +2 -1
index 6c79527..d2c4886 100644
@@ -14,6 +14,7 @@ void esp_init(hwaddr espaddr, int it_shift,
 
 #define ESP_REGS 16
 #define TI_BUFSZ 16
+#define ESP_CMDBUF_SZ 32
 
 typedef struct ESPState ESPState;
 
@@ -31,7 +32,7 @@ struct ESPState {
     SCSIBus bus;
     SCSIDevice *current_dev;
     SCSIRequest *current_req;
-    uint8_t cmdbuf[TI_BUFSZ];
+    uint8_t cmdbuf[ESP_CMDBUF_SZ];
     uint32_t cmdlen;
     uint32_t do_cmd;
 
Signature · 1 line
2.5.5
#1 lore raw

Hi,

Quoted 10 lines · P J P

From: Prasad J Pandit <[email protected]>

While doing DMA read into ESP command buffer 's->cmdbuf', it could write past the 's->cmdbuf' area, if it was partially filled; ie. 's->cmdlen' wasn't set at the start of the buffer. Check 'len' to avoid OOB access. Also increase the command buffer size to 32, which is maximum when 's->do_cmd' is set.

[...]

Quoted 13 lines
include/hw/scsi/esp.h +1 -0
index 6c79527..d2c4886 100644
@@ -14,6 +14,7 @@ void esp_init(hwaddr espaddr, int it_shift,
 
 #define ESP_REGS 16
 #define TI_BUFSZ 16
+#define ESP_CMDBUF_SZ 32
 
 typedef struct ESPState ESPState;
 

include/hw/scsi/esp.h struct ESPState { · line 35

Quoted 5 lines
include/hw/scsi/esp.h +1 -1
@@ -31,7 +32,7 @@ struct ESPState {
     SCSIBus bus;
     SCSIDevice *current_dev;
     SCSIRequest *current_req;
-    uint8_t cmdbuf[TI_BUFSZ];
+    uint8_t cmdbuf[ESP_CMDBUF_SZ];

This was flagged as an incompatibility in the vmstate by a nightly run of the vmstate checker:

Section "esp" Description "esp" Field "cmdbuf" size mismatch: 16 , 32 Section "dc390" Description "esp" Field "cmdbuf" size mismatch: 16 , 32 Section "am53c974" Description "esp" Field "cmdbuf" size mismatch: 16 , 32

Amit

author
#2 lore raw
+-- On Fri, 17 Jun 2016, Amit Shah wrote --+
| This was flagged as an incompatibility in the vmstate by a nightly run
| of the vmstate checker:
| 
| Section "esp" Description "esp" Field "cmdbuf" size mismatch: 16 , 32
| Section "dc390" Description "esp" Field "cmdbuf" size mismatch: 16 , 32
| Section "am53c974" Description "esp" Field "cmdbuf" size mismatch: 16 , 32

Probably vmstate file needs to be regenerated?
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#3 lore raw
Quoted 9 lines · P J P
+-- On Fri, 17 Jun 2016, Amit Shah wrote --+
| This was flagged as an incompatibility in the vmstate by a nightly run
| of the vmstate checker:
| 
| Section "esp" Description "esp" Field "cmdbuf" size mismatch: 16 , 32
| Section "dc390" Description "esp" Field "cmdbuf" size mismatch: 16 , 32
| Section "am53c974" Description "esp" Field "cmdbuf" size mismatch: 16 , 32

Probably vmstate file needs to be regenerated?

No, that's not what that means. It means that your patch broke migration compatibility. I've posted a patch to just remove the code (which also has the effect of reverting your patch and mine).

Paolo

#4 lore raw
In reply to Amit Shah
Quoted 38 lines · Amit Shah

Hi,

Quoted 10 lines · P J P

From: Prasad J Pandit <[email protected]>

While doing DMA read into ESP command buffer 's->cmdbuf', it could write past the 's->cmdbuf' area, if it was partially filled; ie. 's->cmdlen' wasn't set at the start of the buffer. Check 'len' to avoid OOB access. Also increase the command buffer size to 32, which is maximum when 's->do_cmd' is set.

[...]

Quoted 13 lines
include/hw/scsi/esp.h +1 -0
index 6c79527..d2c4886 100644
@@ -14,6 +14,7 @@ void esp_init(hwaddr espaddr, int it_shift,
 
 #define ESP_REGS 16
 #define TI_BUFSZ 16
+#define ESP_CMDBUF_SZ 32
 
 typedef struct ESPState ESPState;
 

include/hw/scsi/esp.h struct ESPState { · line 35

Quoted 5 lines
include/hw/scsi/esp.h +1 -1
@@ -31,7 +32,7 @@ struct ESPState {
     SCSIBus bus;
     SCSIDevice *current_dev;
     SCSIRequest *current_req;
-    uint8_t cmdbuf[TI_BUFSZ];
+    uint8_t cmdbuf[ESP_CMDBUF_SZ];

This was flagged as an incompatibility in the vmstate by a nightly run of the vmstate checker:

Quoted 3 lines

Section "esp" Description "esp" Field "cmdbuf" size mismatch: 16 , 32 Section "dc390" Description "esp" Field "cmdbuf" size mismatch: 16 , 32 Section "am53c974" Description "esp" Field "cmdbuf" size mismatch: 16 , 32

Oh, good catch. But from reading the spec the implementation was completely busted. Let's just drop handle_satn_stop, cmdbuf, cmdlen and do_cmd.

Paolo