From c920d20c101e1ca7475e2f758b9823eb21693b5a Mon Sep 17 00:00:00 2001
From: Stefan Hajnoczi <stefanha@redhat.com>
Date: Thu, 17 Sep 2015 14:08:31 -0400
Subject: [PATCH] net: add checks to validate ring buffer
 pointers(CVE-2015-5279)

RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: <1442498911-32447-2-git-send-email-stefanha@redhat.com>
Patchwork-id: 67810
O-Subject: [RHEL-6.7.z qemu-kvm PATCH 1/1] net: add checks to validate ring buffer pointers(CVE-2015-5279)
Bugzilla: 1263274
RH-Acked-by: Fam Zheng <famz@redhat.com>
RH-Acked-by: John Snow <jsnow@redhat.com>
RH-Acked-by: Thomas Huth <thuth@redhat.com>

From: P J P <pjp@fedoraproject.org>

Ne2000 NIC uses ring buffer of NE2000_MEM_SIZE(49152)
bytes to process network packets. While receiving packets
via ne2000_receive() routine, a local 'index' variable
could exceed the ring buffer size, which could lead to a
memory buffer overflow. Added other checks at initialisation.

Reported-by: Qinghao Tang <luodalongde@gmail.com>
Signed-off-by: P J P <pjp@fedoraproject.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
(cherry picked from commit 9bbdbc66e5765068dce76e9269dce4547afd8ad4)
Signed-off-by: Jeff E. Nelson <jen@redhat.com>

Conflicts:

The downstream filename is hw/ne2000.c while the upstream filename is
hw/net/ne2000.c.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 hw/ne2000.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/hw/ne2000.c b/hw/ne2000.c
index 9946c63..cad7905 100644
--- a/hw/ne2000.c
+++ b/hw/ne2000.c
@@ -254,6 +254,9 @@ ssize_t ne2000_receive(VLANClientState *nc, const uint8_t *buf, size_t size_)
     }
 
     index = s->curpag << 8;
+    if (index >= NE2000_PMEM_END) {
+        index = s->start;
+    }
     /* 4 bytes for header */
     total_len = size + 4;
     /* address for next packet (4 bytes for CRC) */
@@ -338,13 +341,19 @@ void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
         offset = addr | (page << 4);
         switch(offset) {
         case EN0_STARTPG:
-            s->start = val << 8;
+            if (val << 8 <= NE2000_PMEM_END) {
+                s->start = val << 8;
+            }
             break;
         case EN0_STOPPG:
-            s->stop = val << 8;
+            if (val << 8 <= NE2000_PMEM_END) {
+                s->stop = val << 8;
+            }
             break;
         case EN0_BOUNDARY:
-            s->boundary = val;
+            if (val << 8 < NE2000_PMEM_END) {
+                s->boundary = val;
+            }
             break;
         case EN0_IMR:
             s->imr = val;
@@ -385,7 +394,9 @@ void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
             s->phys[offset - EN1_PHYS] = val;
             break;
         case EN1_CURPAG:
-            s->curpag = val;
+            if (val << 8 < NE2000_PMEM_END) {
+                s->curpag = val;
+            }
             break;
         case EN1_MULT ... EN1_MULT + 7:
             s->mult[offset - EN1_MULT] = val;
-- 
2.1.0

