ia64/xen-unstable

changeset 12025:66fe61db9e69

[QEMU] ne2000: Stop memory access beyond buffer

As a program that runs in dom0 which serves users from guests,
the qemu drivers need to be vigilant to the input that comes
from the guests since they may be malicious.

As it is there are multiple ways to get ne2000 to read/write
memory beyond the 48K buffer that it has allocated for each
adapter.

This patch checks the addresses and prevents this from occuring.

The boundary is checked each time since it's changed for every
packet received while the other parameters are only changed
(by the guest) during setup.

Signed-off: Herbert Xu <herbert@gondor.apana.org.au>
author kaf24@localhost.localdomain
date Fri Oct 27 17:50:40 2006 +0100 (2006-10-27)
parents 1940ee13f9d6
children a1939d76c0e8
files tools/ioemu/hw/ne2000.c
line diff
     1.1 --- a/tools/ioemu/hw/ne2000.c	Fri Oct 27 15:44:27 2006 +0100
     1.2 +++ b/tools/ioemu/hw/ne2000.c	Fri Oct 27 17:50:40 2006 +0100
     1.3 @@ -137,6 +137,7 @@ typedef struct NE2000State {
     1.4      uint8_t curpag;
     1.5      uint8_t mult[8]; /* multicast mask array */
     1.6      int irq;
     1.7 +    int tainted;
     1.8      PCIDevice *pci_dev;
     1.9      VLANClientState *vc;
    1.10      uint8_t macaddr[6];
    1.11 @@ -226,6 +227,27 @@ static int ne2000_can_receive(void *opaq
    1.12  
    1.13  #define MIN_BUF_SIZE 60
    1.14  
    1.15 +static inline int ne2000_valid_ring_addr(NE2000State *s, unsigned int addr)
    1.16 +{
    1.17 +    addr <<= 8;
    1.18 +    return addr < s->stop && addr >= s->start;
    1.19 +}
    1.20 +
    1.21 +static inline int ne2000_check_state(NE2000State *s)
    1.22 +{
    1.23 +    if (!s->tainted)
    1.24 +        return 0;
    1.25 +
    1.26 +    if (s->start >= s->stop || s->stop > NE2000_MEM_SIZE)
    1.27 +        return -EINVAL;
    1.28 +
    1.29 +    if (!ne2000_valid_ring_addr(s, s->curpag))
    1.30 +        return -EINVAL;
    1.31 +
    1.32 +    s->tainted = 0;
    1.33 +    return 0;
    1.34 +}
    1.35 +
    1.36  static void ne2000_receive(void *opaque, const uint8_t *buf, int size)
    1.37  {
    1.38      NE2000State *s = opaque;
    1.39 @@ -239,6 +261,12 @@ static void ne2000_receive(void *opaque,
    1.40      printf("NE2000: received len=%d\n", size);
    1.41  #endif
    1.42  
    1.43 +    if (ne2000_check_state(s))
    1.44 +        return;
    1.45 +
    1.46 +    if (!ne2000_valid_ring_addr(s, s->boundary))
    1.47 +        return;
    1.48 +
    1.49      if (s->cmd & E8390_STOP || ne2000_buffer_full(s))
    1.50          return;
    1.51      
    1.52 @@ -359,9 +387,11 @@ static void ne2000_ioport_write(void *op
    1.53          switch(offset) {
    1.54          case EN0_STARTPG:
    1.55              s->start = val << 8;
    1.56 +            s->tainted = 1;
    1.57              break;
    1.58          case EN0_STOPPG:
    1.59              s->stop = val << 8;
    1.60 +            s->tainted = 1;
    1.61              break;
    1.62          case EN0_BOUNDARY:
    1.63              s->boundary = val;
    1.64 @@ -406,6 +436,7 @@ static void ne2000_ioport_write(void *op
    1.65              break;
    1.66          case EN1_CURPAG:
    1.67              s->curpag = val;
    1.68 +            s->tainted = 1;
    1.69              break;
    1.70          case EN1_MULT ... EN1_MULT + 7:
    1.71              s->mult[offset - EN1_MULT] = val;
    1.72 @@ -509,7 +540,7 @@ static inline void ne2000_mem_writel(NE2
    1.73  {
    1.74      addr &= ~1; /* XXX: check exact behaviour if not even */
    1.75      if (addr < 32 || 
    1.76 -        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
    1.77 +        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE - 2)) {
    1.78          cpu_to_le32wu((uint32_t *)(s->mem + addr), val);
    1.79      }
    1.80  }
    1.81 @@ -539,7 +570,7 @@ static inline uint32_t ne2000_mem_readl(
    1.82  {
    1.83      addr &= ~1; /* XXX: check exact behaviour if not even */
    1.84      if (addr < 32 || 
    1.85 -        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
    1.86 +        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE - 2)) {
    1.87          return le32_to_cpupu((uint32_t *)(s->mem + addr));
    1.88      } else {
    1.89          return 0xffffffff;