Skip to contentSkip to conversation

Xen Security Advisory 140 - QEMU leak of uninitialized heap memory in rtl8139 device model

DISCUSSION Ian Jackson 2 participants 2 messages Aug 13, 2015 – Aug 14, 2015 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

Yuriy Kohut writes ("Re: Xen Security Advisory 140 - QEMU leak of uninitialized heap memory in rtl8139 device model"):

Quoted 10 lines

Please find attached patches for the 'Qemu-dm 3.4 stable branch’ (git://xenbits.xen.org/qemu-xen-3.4-testing.git):

# sha256sum xsa140-qemut-3.4-?.patch
a6f614aea18f5ebf37b7d462c9190d7b9426a7b2ca304f314d05b8a328c9f831  xsa140-qemut-3.4-1.patch
dd3f90a407f83fdaf7efa42a5aabcc479ad88a0bc8b98d31f1809dfe81543186  xsa140-qemut-3.4-2.patch
b091a84fe888362a1501faf8aa546d2b8816e0ce6e197d8da9cd0bafc0e26dbb  xsa140-qemut-3.4-3.patch
454e6d0d6fe464c7a696c168ca5218fbd5d496eab1f5565bc02e391997b02a3d  xsa140-qemut-3.4-4.patch
def8a6a33bddd77518b9ba2f8f16b2ac4ff962c34f24a94173e41b5a82adf68a  xsa140-qemut-3.4-5.patch
c599838dfea5aa50eed8bc2ca373734a6ef4529738aa1d056637625d04d35875  xsa140-qemut-3.4-6.patch
6d2efbd7b492355160f38a61e0a83c5fb5be86e2a4c953cc2f4e05a2dda7001e  xsa140-qemut-3.4-7.patch

Hi. Thanks a lot for this.

We (Xen maintainers) intend to handle these by applying this (as a bugfix) to xen.git#staging, which is the 4.6 release prep branch. They apply with some minor line offsets. We'll then feed that into our maintained stable branches in the usual way, and update the advisory.

Yuriy, can I have your Signed-off-by for the backport work, in accordance with
http://wiki.xenproject.org/wiki/Submitting_Xen_Project_Patches#Signing_off_a_patch
?

If so I will repost this as a formal patch series.

Thanks,
Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

Hi Ian,

Please find attached "new" patches for the 'Qemu-dm 3.4 stable branch’ (git://xenbits.xen.org/qemu-xen-3.4-testing.git) with Signed-off-by included:

# sha256sum xsa140-qemut-3.4-?.patch
444b0487b6ae702b13626780b94cfe9d5b7e39c0b6ae26fc162fe93c84c83407  xsa140-qemut-3.4-1.patch
b08ee945330020a522a549ce9aa118abe93624e66b925cbb5f22e0c771642afa  xsa140-qemut-3.4-2.patch
21be371510876261830d3895b68d6288e57ca651fc67befb0323d3bc3bdb5b1c  xsa140-qemut-3.4-3.patch
3fbd3d4a236b249bf4b2cae53d2e8242d2c5f53efd8848c879de80ae64de05c8  xsa140-qemut-3.4-4.patch
71c32327b813c8a2c9dc0e4dd3fc08bfcf1d107febaa2eae085a67781890fe2b  xsa140-qemut-3.4-5.patch
5542dd9cca45586e45d5f6eb4276e61a485994ea31f2aad4ac65801832890bf1  xsa140-qemut-3.4-6.patch
908e492694dd5102280799cac6151f551aff35a08900eee33ca14fa026c8dc51  xsa140-qemut-3.4-7.patch

From f0ef09010d85c93eb1db22da8ebdb7cee71d7490 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Thu, 13 Aug 2015 19:13:23 +0300
Subject: [PATCH] [PATCH 1/7] rtl8139: avoid nested ifs in IP header parsing

Transmit offload needs to parse packet headers. If header fields have unexpected values the offload processing is skipped.

The code currently uses nested ifs because there is relatively little input validation. The next patches will add missing input validation and a goto label is more appropriate to avoid deep if statement nesting.

---
 hw/rtl8139.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
hw/rtl8139.c +19 -14
index 20e486a..646de28 100644
@@ -2113,26 +2113,30 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             size_t   eth_payload_len  = 0;
 
             int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12));
-            if (proto == ETH_P_IP)
+            if (proto != ETH_P_IP)
             {
-                DEBUG_PRINT(("RTL8139: +++ C+ mode has IP packet\n"));
+                goto skip_offload;
+            }
 
-                /* not aligned */
-                eth_payload_data = saved_buffer + ETH_HLEN;
-                eth_payload_len  = saved_size   - ETH_HLEN;
+            DEBUG_PRINT(("+++ C+ mode has IP packet\n"));
 
-                ip = (ip_header*)eth_payload_data;
+            /* not aligned */
+            eth_payload_data = saved_buffer + ETH_HLEN;
+            eth_payload_len  = saved_size   - ETH_HLEN;
 
-                if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
-                    DEBUG_PRINT(("RTL8139: +++ C+ mode packet has bad IP version %d expected %d\n", IP_HEADER_VERSION(ip), IP_HEADER_VERSION_4));
-                    ip = NULL;
-                } else {
-                    hlen = IP_HEADER_LENGTH(ip);
-                    ip_protocol = ip->ip_p;
-                    ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
-                }
+            ip = (ip_header*)eth_payload_data;
+
+            if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
+                DEBUG_PRINT(("+++ C+ mode packet has bad IP version %d "
+                    "expected %d\n", IP_HEADER_VERSION(ip),
+                    IP_HEADER_VERSION_4));
+                goto skip_offload;
             }
 
+            hlen = IP_HEADER_LENGTH(ip);
+            ip_protocol = ip->ip_p;
+            ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
+
             if (ip)
             {
                 if (txdw0 & CP_TX_IPCS)
@@ -2315,6 +2319,7 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             }
         }
 
+skip_offload:
         /* update tally counter */
         ++s->tally_counters.TxOk;
 
Signature · 1 line
1.7.12.4

From 24554d9623854af845426edc347dae56d519914d Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:25:55 +0300
Subject: [PATCH] [PATCH 2/7] rtl8139: drop tautologous if (ip) {...}
statement

The previous patch stopped using the ip pointer as an indicator that the IP header is present. When we reach the if (ip) {...} statement we know ip is always non-NULL.

Remove the if statement to reduce nesting.

---
 hw/rtl8139.c | 270 ++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 139 insertions(+), 131 deletions(-)
hw/rtl8139.c +139 -131

324 lines not on this page · raw message

Signature · 1 line
1.7.12.4

From b0c1d0562a132bc1af5f02de651364d74cba6338 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:27:46 +0300
Subject: [PATCH] [PATCH 3/7] rtl8139: skip offload on short Ethernet/IP
header

Transmit offload features access Ethernet and IP headers the packet. If the packet is too short we must not attempt to access header fields:

  int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12));
  ...
  eth_payload_data = saved_buffer + ETH_HLEN;
  ...
  ip = (ip_header*)eth_payload_data;
  if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
---
 hw/rtl8139.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/rtl8139.c +5 -0
index 84118a6..2b4f11a 100644
@@ -2103,6 +2103,11 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             #define ETH_HLEN    14
             #define ETH_MTU     1500
 
+            /* Large enough for Ethernet and IP headers? */
+            if (saved_size < ETH_HLEN + sizeof(ip_header)) {
+                goto skip_offload;
+            }
+
             /* ip packet header */
             ip_header *ip = 0;
             int hlen = 0;
Signature · 1 line
1.7.12.4

From 2aac4be1942668ca084b98db103cde9676309e73 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:29:15 +0300
Subject: [PATCH] [PATCH 4/7] rtl8139: check IP Header Length field

The IP Header Length field was only checked in the IP checksum case, but is used in other cases too.

---
 hw/rtl8139.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
hw/rtl8139.c +8 -12
index 2b4f11a..e414a6f 100644
@@ -2139,24 +2139,20 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             }
 
             hlen = IP_HEADER_LENGTH(ip);
+            if (hlen < sizeof(ip_header) || hlen > eth_payload_len) {
+                goto skip_offload;
+            }
+
             ip_protocol = ip->ip_p;
             ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
 
             if (txdw0 & CP_TX_IPCS)
             {
                 DEBUG_PRINT(("+++ C+ mode need IP checksum\n"));
-                if (hlen<sizeof(ip_header) || hlen>eth_payload_len) {/* min header length */
-                    /* bad packet header len */
-                    /* or packet too short */
-                }
-
-                else
-                {
-                    ip->ip_sum = 0;
-                    ip->ip_sum = ip_checksum(ip, hlen);
-                    DEBUG_PRINT(("+++ C+ mode IP header len=%d checksum=%04x\n",
-                        hlen, ip->ip_sum));
-                }
+                ip->ip_sum = 0;
+                ip->ip_sum = ip_checksum(ip, hlen);
+                DEBUG_PRINT(("+++ C+ mode IP header len=%d checksum=%04x\n",
+                    hlen, ip->ip_sum));
             }
             if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
             {
Signature · 1 line
1.7.12.4

From 9dce83feb24f2a3b089fe57fd18f5d7f858459a4 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:30:21 +0300
Subject: [PATCH] [PATCH 5/7] rtl8139: check IP Total Length field

The IP Total Length field includes the IP header and data. Make sure it is valid and does not exceed the Ethernet payload size.

---
 hw/rtl8139.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/rtl8139.c +6 -1
index e414a6f..97542d1 100644
@@ -2144,7 +2144,12 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             }
 
             ip_protocol = ip->ip_p;
-            ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
+
+            ip_data_len = be16_to_cpu(ip->ip_len);
+            if (ip_data_len < hlen || ip_data_len > eth_payload_len) {
+                goto skip_offload;
+            }
+            ip_data_len -= hlen;
 
             if (txdw0 & CP_TX_IPCS)
             {
Signature · 1 line
1.7.12.4

From 0bd223c61ad95a97caa6fb717240f4c55a176847 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:31:30 +0300
Subject: [PATCH] [PATCH 6/7] rtl8139: skip offload on short TCP header

TCP Large Segment Offload accesses the TCP header in the packet. If the packet is too short we must not attempt to access header fields:

  tcp_header *p_tcp_hdr = (tcp_header*)(eth_payload_data + hlen);
  int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
---
 hw/rtl8139.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/rtl8139.c +5 -0
index 97542d1..1ff420f 100644
@@ -2161,6 +2161,11 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             }
             if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
             {
+                /* Large enough for the TCP header? */
+                if (ip_data_len < sizeof(tcp_header)) {
+                    goto skip_offload;
+                }
+
                 int large_send_mss = (txdw0 >> 16) & CP_TC_LGSEN_MSS_MASK;
 
                 DEBUG_PRINT(("+++ C+ mode offloaded task TSO MTU=%d IP data %d "
Signature · 1 line
1.7.12.4

From 92191dc85f1815fa61dbf2983f3f0c85a6fb1e76 Mon Sep 17 00:00:00 2001
From: Yuriy Kohut <[email protected]>
Date: Fri, 14 Aug 2015 10:32:32 +0300
Subject: [PATCH] [PATCH 7/7] rtl8139: check TCP Data Offset field

The TCP Data Offset field contains the length of the header. Make sure it is valid and does not exceed the IP data length.

---
 hw/rtl8139.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/rtl8139.c +5 -0
index 1ff420f..bc41a1c 100644
@@ -2190,6 +2190,11 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
 
                 int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
 
+                /* Invalid TCP data offset? */
+                if (tcp_hlen < sizeof(tcp_header) || tcp_hlen > ip_data_len) {
+                    goto skip_offload;
+                }
+
                 /* ETH_MTU = ip header len + tcp header len + payload */
                 int tcp_data_len = ip_data_len - tcp_hlen;
                 int tcp_chunk_size = ETH_MTU - hlen - tcp_hlen;
Signature · 1 line
1.7.12.4

---
Yura

Quoted 27 lines · Ian Jackson

Yuriy Kohut writes ("Re: Xen Security Advisory 140 - QEMU leak of uninitialized heap memory in rtl8139 device model"):

Quoted 10 lines

Please find attached patches for the 'Qemu-dm 3.4 stable branch’ (git://xenbits.xen.org/qemu-xen-3.4-testing.git):

# sha256sum xsa140-qemut-3.4-?.patch
a6f614aea18f5ebf37b7d462c9190d7b9426a7b2ca304f314d05b8a328c9f831  xsa140-qemut-3.4-1.patch
dd3f90a407f83fdaf7efa42a5aabcc479ad88a0bc8b98d31f1809dfe81543186  xsa140-qemut-3.4-2.patch
b091a84fe888362a1501faf8aa546d2b8816e0ce6e197d8da9cd0bafc0e26dbb  xsa140-qemut-3.4-3.patch
454e6d0d6fe464c7a696c168ca5218fbd5d496eab1f5565bc02e391997b02a3d  xsa140-qemut-3.4-4.patch
def8a6a33bddd77518b9ba2f8f16b2ac4ff962c34f24a94173e41b5a82adf68a  xsa140-qemut-3.4-5.patch
c599838dfea5aa50eed8bc2ca373734a6ef4529738aa1d056637625d04d35875  xsa140-qemut-3.4-6.patch
6d2efbd7b492355160f38a61e0a83c5fb5be86e2a4c953cc2f4e05a2dda7001e  xsa140-qemut-3.4-7.patch

Hi. Thanks a lot for this.

We (Xen maintainers) intend to handle these by applying this (as a bugfix) to xen.git#staging, which is the 4.6 release prep branch. They apply with some minor line offsets. We'll then feed that into our maintained stable branches in the usual way, and update the advisory.

Yuriy, can I have your Signed-off-by for the backport work, in accordance with
http://wiki.xenproject.org/wiki/Submitting_Xen_Project_Patches#Signing_off_a_patch
?

Quoted 4 lines

If so I will repost this as a formal patch series.

Thanks,
Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel