diff mbox

[v4] scsi: esp: check length before dma read

Message ID 1466012179-31839-1-git-send-email-ppandit@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Prasad Pandit June 15, 2016, 5:36 p.m. UTC
From: Prasad J Pandit <pjp@fedoraproject.org>

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.

Reported-by: Li Qiang <liqiang6-s@360.cn>
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
---
 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

Comments

Amit Shah June 17, 2016, 4:19 a.m. UTC | #1
Hi,

On (Wed) 15 Jun 2016 [23:06:19], P J P wrote:
> From: Prasad J Pandit <pjp@fedoraproject.org>
> 
> 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.
> 
> Reported-by: Li Qiang <liqiang6-s@360.cn>
> Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>

[...]

> diff --git a/include/hw/scsi/esp.h b/include/hw/scsi/esp.h
> index 6c79527..d2c4886 100644
> --- a/include/hw/scsi/esp.h
> +++ b/include/hw/scsi/esp.h
> @@ -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];

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
Prasad Pandit June 17, 2016, 8:16 a.m. UTC | #2
+-- 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
Paolo Bonzini June 17, 2016, 8:21 a.m. UTC | #3
On 17/06/2016 06:19, Amit Shah wrote:
> Hi,
> 
> On (Wed) 15 Jun 2016 [23:06:19], P J P wrote:
>> From: Prasad J Pandit <pjp@fedoraproject.org>
>>
>> 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.
>>
>> Reported-by: Li Qiang <liqiang6-s@360.cn>
>> Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
> 
> [...]
> 
>> diff --git a/include/hw/scsi/esp.h b/include/hw/scsi/esp.h
>> index 6c79527..d2c4886 100644
>> --- a/include/hw/scsi/esp.h
>> +++ b/include/hw/scsi/esp.h
>> @@ -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];
> 
> 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

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
Paolo Bonzini June 17, 2016, 8:26 a.m. UTC | #4
On 17/06/2016 10:16, P J P wrote:
> +-- 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
diff mbox

Patch

diff --git a/hw/scsi/esp.c b/hw/scsi/esp.c
index 4b94bbc..cc74ba8 100644
--- a/hw/scsi/esp.c
+++ b/hw/scsi/esp.c
@@ -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();
diff --git a/include/hw/scsi/esp.h b/include/hw/scsi/esp.h
index 6c79527..d2c4886 100644
--- a/include/hw/scsi/esp.h
+++ b/include/hw/scsi/esp.h
@@ -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;