thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-bootp-limit-vendor-specific-area-to-input-packet-mem.patch

6e7d01
From 8198ae7c21a4d37f7e365058f973867c41d44d21 Mon Sep 17 00:00:00 2001
6e7d01
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
6e7d01
Date: Thu, 29 Jul 2021 04:56:25 -0400
6e7d01
Subject: [PATCH 06/14] bootp: limit vendor-specific area to input packet
6e7d01
 memory buffer
6e7d01
MIME-Version: 1.0
6e7d01
Content-Type: text/plain; charset=UTF-8
6e7d01
Content-Transfer-Encoding: 8bit
6e7d01
6e7d01
RH-Author: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Message-id: <20210708082537.1550263-3-marcandre.lureau@redhat.com>
6e7d01
Patchwork-id: 101821
6e7d01
O-Subject: [RHEL-8.5.0 qemu-kvm PATCH 2/8] bootp: limit vendor-specific area to input packet memory buffer
6e7d01
Bugzilla: 1970819 1970835 1970843 1970853
6e7d01
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
6e7d01
RH-Acked-by: Eric Blake <eblake@redhat.com>
6e7d01
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
6e7d01
6e7d01
From: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
6e7d01
sizeof(bootp_t) currently holds DHCP_OPT_LEN. Remove this optional field
6e7d01
from the structure, to help with the following patch checking for
6e7d01
minimal header size. Modify the bootp_reply() function to take the
6e7d01
buffer boundaries and avoiding potential buffer overflow.
6e7d01
6e7d01
Related to CVE-2021-3592.
6e7d01
6e7d01
https://gitlab.freedesktop.org/slirp/libslirp/-/issues/44
6e7d01
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
6e7d01
(cherry picked from commit f13cad45b25d92760bb0ad67bec0300a4d7d5275)
6e7d01
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
6e7d01
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
6e7d01
---
6e7d01
 slirp/src/bootp.c | 26 +++++++++++++++-----------
6e7d01
 slirp/src/bootp.h |  2 +-
6e7d01
 slirp/src/mbuf.c  |  5 +++++
6e7d01
 slirp/src/mbuf.h  |  1 +
6e7d01
 4 files changed, 22 insertions(+), 12 deletions(-)
6e7d01
6e7d01
diff --git a/slirp/src/bootp.c b/slirp/src/bootp.c
6e7d01
index 3f9ce2553e..5754327138 100644
6e7d01
--- a/slirp/src/bootp.c
6e7d01
+++ b/slirp/src/bootp.c
6e7d01
@@ -92,21 +92,22 @@ found:
6e7d01
     return bc;
6e7d01
 }
6e7d01
 
6e7d01
-static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
6e7d01
+static void dhcp_decode(const struct bootp_t *bp,
6e7d01
+                        const uint8_t *bp_end,
6e7d01
+                        int *pmsg_type,
6e7d01
                         struct in_addr *preq_addr)
6e7d01
 {
6e7d01
-    const uint8_t *p, *p_end;
6e7d01
+    const uint8_t *p;
6e7d01
     int len, tag;
6e7d01
 
6e7d01
     *pmsg_type = 0;
6e7d01
     preq_addr->s_addr = htonl(0L);
6e7d01
 
6e7d01
     p = bp->bp_vend;
6e7d01
-    p_end = p + DHCP_OPT_LEN;
6e7d01
     if (memcmp(p, rfc1533_cookie, 4) != 0)
6e7d01
         return;
6e7d01
     p += 4;
6e7d01
-    while (p < p_end) {
6e7d01
+    while (p < bp_end) {
6e7d01
         tag = p[0];
6e7d01
         if (tag == RFC1533_PAD) {
6e7d01
             p++;
6e7d01
@@ -114,10 +115,10 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
6e7d01
             break;
6e7d01
         } else {
6e7d01
             p++;
6e7d01
-            if (p >= p_end)
6e7d01
+            if (p >= bp_end)
6e7d01
                 break;
6e7d01
             len = *p++;
6e7d01
-            if (p + len > p_end) {
6e7d01
+            if (p + len > bp_end) {
6e7d01
                 break;
6e7d01
             }
6e7d01
             DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
6e7d01
@@ -144,7 +145,9 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
6e7d01
     }
6e7d01
 }
6e7d01
 
6e7d01
-static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
6e7d01
+static void bootp_reply(Slirp *slirp,
6e7d01
+                        const struct bootp_t *bp,
6e7d01
+                        const uint8_t *bp_end)
6e7d01
 {
6e7d01
     BOOTPClient *bc = NULL;
6e7d01
     struct mbuf *m;
6e7d01
@@ -157,7 +160,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
6e7d01
     uint8_t client_ethaddr[ETH_ALEN];
6e7d01
 
6e7d01
     /* extract exact DHCP msg type */
6e7d01
-    dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
6e7d01
+    dhcp_decode(bp, bp_end, &dhcp_msg_type, &preq_addr);
6e7d01
     DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
6e7d01
     if (preq_addr.s_addr != htonl(0L))
6e7d01
         DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
6e7d01
@@ -179,9 +182,10 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
6e7d01
         return;
6e7d01
     }
6e7d01
     m->m_data += IF_MAXLINKHDR;
6e7d01
+    m_inc(m, sizeof(struct bootp_t) + DHCP_OPT_LEN);
6e7d01
     rbp = (struct bootp_t *)m->m_data;
6e7d01
     m->m_data += sizeof(struct udpiphdr);
6e7d01
-    memset(rbp, 0, sizeof(struct bootp_t));
6e7d01
+    memset(rbp, 0, sizeof(struct bootp_t) + DHCP_OPT_LEN);
6e7d01
 
6e7d01
     if (dhcp_msg_type == DHCPDISCOVER) {
6e7d01
         if (preq_addr.s_addr != htonl(0L)) {
6e7d01
@@ -235,7 +239,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
6e7d01
     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
6e7d01
 
6e7d01
     q = rbp->bp_vend;
6e7d01
-    end = (uint8_t *)&rbp[1];
6e7d01
+    end = rbp->bp_vend + DHCP_OPT_LEN;
6e7d01
     memcpy(q, rfc1533_cookie, 4);
6e7d01
     q += 4;
6e7d01
 
6e7d01
@@ -365,6 +369,6 @@ void bootp_input(struct mbuf *m)
6e7d01
     struct bootp_t *bp = mtod(m, struct bootp_t *);
6e7d01
 
6e7d01
     if (bp->bp_op == BOOTP_REQUEST) {
6e7d01
-        bootp_reply(m->slirp, bp);
6e7d01
+        bootp_reply(m->slirp, bp, m_end(m));
6e7d01
     }
6e7d01
 }
6e7d01
diff --git a/slirp/src/bootp.h b/slirp/src/bootp.h
6e7d01
index 03ece9bf28..0d20a944a8 100644
6e7d01
--- a/slirp/src/bootp.h
6e7d01
+++ b/slirp/src/bootp.h
6e7d01
@@ -114,7 +114,7 @@ struct bootp_t {
6e7d01
     uint8_t bp_hwaddr[16];
6e7d01
     uint8_t bp_sname[64];
6e7d01
     uint8_t bp_file[128];
6e7d01
-    uint8_t bp_vend[DHCP_OPT_LEN];
6e7d01
+    uint8_t bp_vend[];
6e7d01
 };
6e7d01
 
6e7d01
 typedef struct {
6e7d01
diff --git a/slirp/src/mbuf.c b/slirp/src/mbuf.c
6e7d01
index 6d0653ed3d..7db07c088e 100644
6e7d01
--- a/slirp/src/mbuf.c
6e7d01
+++ b/slirp/src/mbuf.c
6e7d01
@@ -233,3 +233,8 @@ void *mtod_check(struct mbuf *m, size_t len)
6e7d01
 
6e7d01
     return NULL;
6e7d01
 }
6e7d01
+
6e7d01
+void *m_end(struct mbuf *m)
6e7d01
+{
6e7d01
+    return m->m_data + m->m_len;
6e7d01
+}
6e7d01
diff --git a/slirp/src/mbuf.h b/slirp/src/mbuf.h
6e7d01
index 2015e3232f..a9752a36e0 100644
6e7d01
--- a/slirp/src/mbuf.h
6e7d01
+++ b/slirp/src/mbuf.h
6e7d01
@@ -119,6 +119,7 @@ void m_adj(struct mbuf *, int);
6e7d01
 int m_copy(struct mbuf *, struct mbuf *, int, int);
6e7d01
 struct mbuf *dtom(Slirp *, void *);
6e7d01
 void *mtod_check(struct mbuf *, size_t len);
6e7d01
+void *m_end(struct mbuf *);
6e7d01
 
6e7d01
 static inline void ifs_init(struct mbuf *ifm)
6e7d01
 {
6e7d01
-- 
6e7d01
2.27.0
6e7d01