Fix flexible array buffer overflow
authorFrediano Ziglio <fziglio@redhat.com>
Fri, 18 May 2018 10:41:57 +0000 (11:41 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Fri, 26 Oct 2018 15:52:24 +0000 (16:52 +0100)
commitb1f84cd9841f6045bc2769db37e46c1c259291c5
treed5614d627978bcf0207761c0a0b95587b2c040e6
parent46e1bf19570ddf7d9e76f5d718a7c922a6a2b9c6
Fix flexible array buffer overflow

This is kind of a DoS, possibly flexible array in the protocol
causes the network size check to be ignored due to integer overflows.

The size of flexible array is computed as (message_end - position),
then this size is added to the number of bytes before the array and
this number is used to check if we overflow initial message.

An example is:

    message {
        uint32 dummy[2];
        uint8 data[] @end;
    } LenMessage;

which generated this (simplified remove useless code) code:

    { /* data */
        data__nelements = message_end - (start + 8);

        data__nw_size = data__nelements;
    }

    nw_size = 8 + data__nw_size;

    /* Check if message fits in reported side */
    if (nw_size > (uintptr_t) (message_end - start)) {
        return NULL;
    }

Following code:
- data__nelements == message_end - (start + 8)
- data__nw_size == data__nelements == message_end - (start + 8)
- nw_size == 8 + data__nw_size == 8 + message_end - (start + 8) ==
  8 + message_end - start - 8 == message_end -start
- the check for overflow is (nw_size > (message_end - start)) but
  nw_size == message_end - start so the check is doing
  ((message_end - start) > (message_end - start)) which is always false.

If message_end - start < 8 then data__nelements (number of element
on the array above) computation generate an integer underflow that
later create a buffer overflow.

Add a check to make sure that the array starts before the message ends
to avoid the overflow.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
[Salvatore Bonaccorso: Drop generated diff from commit messages causing
 problem when applying with quilt. Remove addition to testsuite]

Gbp-Pq: Name Fix-flexible-array-buffer-overflow.patch
spice-common/python_modules/demarshal.py