ab660b
From f0d4faae8258385338bc1ec252250454346b7ef7 Mon Sep 17 00:00:00 2001
ab660b
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
ab660b
Date: Fri, 4 Jun 2021 19:25:28 +0400
ab660b
Subject: [PATCH 2/7] bootp: limit vendor-specific area to input packet memory
ab660b
 buffer
ab660b
MIME-Version: 1.0
ab660b
Content-Type: text/plain; charset=UTF-8
ab660b
Content-Transfer-Encoding: 8bit
ab660b
ab660b
sizeof(bootp_t) currently holds DHCP_OPT_LEN. Remove this optional field
ab660b
from the structure, to help with the following patch checking for
ab660b
minimal header size. Modify the bootp_reply() function to take the
ab660b
buffer boundaries and avoiding potential buffer overflow.
ab660b
ab660b
Related to CVE-2021-3592.
ab660b
ab660b
https://gitlab.freedesktop.org/slirp/libslirp/-/issues/44
ab660b
ab660b
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ab660b
(cherry picked from commit f13cad45b25d92760bb0ad67bec0300a4d7d5275)
ab660b
---
ab660b
 src/bootp.c | 26 +++++++++++++++-----------
ab660b
 src/bootp.h |  2 +-
ab660b
 src/mbuf.c  |  5 +++++
ab660b
 src/mbuf.h  |  1 +
ab660b
 4 files changed, 22 insertions(+), 12 deletions(-)
ab660b
ab660b
diff --git a/src/bootp.c b/src/bootp.c
ab660b
index 46e9681..e0db8d1 100644
ab660b
--- a/src/bootp.c
ab660b
+++ b/src/bootp.c
ab660b
@@ -92,21 +92,22 @@ found:
ab660b
     return bc;
ab660b
 }
ab660b
 
ab660b
-static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
ab660b
+static void dhcp_decode(const struct bootp_t *bp,
ab660b
+                        const uint8_t *bp_end,
ab660b
+                        int *pmsg_type,
ab660b
                         struct in_addr *preq_addr)
ab660b
 {
ab660b
-    const uint8_t *p, *p_end;
ab660b
+    const uint8_t *p;
ab660b
     int len, tag;
ab660b
 
ab660b
     *pmsg_type = 0;
ab660b
     preq_addr->s_addr = htonl(0L);
ab660b
 
ab660b
     p = bp->bp_vend;
ab660b
-    p_end = p + DHCP_OPT_LEN;
ab660b
     if (memcmp(p, rfc1533_cookie, 4) != 0)
ab660b
         return;
ab660b
     p += 4;
ab660b
-    while (p < p_end) {
ab660b
+    while (p < bp_end) {
ab660b
         tag = p[0];
ab660b
         if (tag == RFC1533_PAD) {
ab660b
             p++;
ab660b
@@ -114,10 +115,10 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
ab660b
             break;
ab660b
         } else {
ab660b
             p++;
ab660b
-            if (p >= p_end)
ab660b
+            if (p >= bp_end)
ab660b
                 break;
ab660b
             len = *p++;
ab660b
-            if (p + len > p_end) {
ab660b
+            if (p + len > bp_end) {
ab660b
                 break;
ab660b
             }
ab660b
             DPRINTF("dhcp: tag=%d len=%d\n", tag, len);
ab660b
@@ -144,7 +145,9 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
ab660b
     }
ab660b
 }
ab660b
 
ab660b
-static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
ab660b
+static void bootp_reply(Slirp *slirp,
ab660b
+                        const struct bootp_t *bp,
ab660b
+                        const uint8_t *bp_end)
ab660b
 {
ab660b
     BOOTPClient *bc = NULL;
ab660b
     struct mbuf *m;
ab660b
@@ -157,7 +160,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
ab660b
     uint8_t client_ethaddr[ETH_ALEN];
ab660b
 
ab660b
     /* extract exact DHCP msg type */
ab660b
-    dhcp_decode(bp, &dhcp_msg_type, &preq_addr);
ab660b
+    dhcp_decode(bp, bp_end, &dhcp_msg_type, &preq_addr);
ab660b
     DPRINTF("bootp packet op=%d msgtype=%d", bp->bp_op, dhcp_msg_type);
ab660b
     if (preq_addr.s_addr != htonl(0L))
ab660b
         DPRINTF(" req_addr=%08" PRIx32 "\n", ntohl(preq_addr.s_addr));
ab660b
@@ -179,9 +182,10 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
ab660b
         return;
ab660b
     }
ab660b
     m->m_data += IF_MAXLINKHDR;
ab660b
+    m_inc(m, sizeof(struct bootp_t) + DHCP_OPT_LEN);
ab660b
     rbp = (struct bootp_t *)m->m_data;
ab660b
     m->m_data += sizeof(struct udpiphdr);
ab660b
-    memset(rbp, 0, sizeof(struct bootp_t));
ab660b
+    memset(rbp, 0, sizeof(struct bootp_t) + DHCP_OPT_LEN);
ab660b
 
ab660b
     if (dhcp_msg_type == DHCPDISCOVER) {
ab660b
         if (preq_addr.s_addr != htonl(0L)) {
ab660b
@@ -235,7 +239,7 @@ static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
ab660b
     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
ab660b
 
ab660b
     q = rbp->bp_vend;
ab660b
-    end = (uint8_t *)&rbp[1];
ab660b
+    end = rbp->bp_vend + DHCP_OPT_LEN;
ab660b
     memcpy(q, rfc1533_cookie, 4);
ab660b
     q += 4;
ab660b
 
ab660b
@@ -364,6 +368,6 @@ void bootp_input(struct mbuf *m)
ab660b
     struct bootp_t *bp = mtod(m, struct bootp_t *);
ab660b
 
ab660b
     if (bp->bp_op == BOOTP_REQUEST) {
ab660b
-        bootp_reply(m->slirp, bp);
ab660b
+        bootp_reply(m->slirp, bp, m_end(m));
ab660b
     }
ab660b
 }
ab660b
diff --git a/src/bootp.h b/src/bootp.h
ab660b
index a57fa51..31ce5fd 100644
ab660b
--- a/src/bootp.h
ab660b
+++ b/src/bootp.h
ab660b
@@ -114,7 +114,7 @@ struct bootp_t {
ab660b
     uint8_t bp_hwaddr[16];
ab660b
     uint8_t bp_sname[64];
ab660b
     char bp_file[128];
ab660b
-    uint8_t bp_vend[DHCP_OPT_LEN];
ab660b
+    uint8_t bp_vend[];
ab660b
 };
ab660b
 
ab660b
 typedef struct {
ab660b
diff --git a/src/mbuf.c b/src/mbuf.c
ab660b
index cb2e971..0c1a530 100644
ab660b
--- a/src/mbuf.c
ab660b
+++ b/src/mbuf.c
ab660b
@@ -233,3 +233,8 @@ void *mtod_check(struct mbuf *m, size_t len)
ab660b
 
ab660b
     return NULL;
ab660b
 }
ab660b
+
ab660b
+void *m_end(struct mbuf *m)
ab660b
+{
ab660b
+    return m->m_data + m->m_len;
ab660b
+}
ab660b
diff --git a/src/mbuf.h b/src/mbuf.h
ab660b
index 2015e32..a9752a3 100644
ab660b
--- a/src/mbuf.h
ab660b
+++ b/src/mbuf.h
ab660b
@@ -119,6 +119,7 @@ void m_adj(struct mbuf *, int);
ab660b
 int m_copy(struct mbuf *, struct mbuf *, int, int);
ab660b
 struct mbuf *dtom(Slirp *, void *);
ab660b
 void *mtod_check(struct mbuf *, size_t len);
ab660b
+void *m_end(struct mbuf *);
ab660b
 
ab660b
 static inline void ifs_init(struct mbuf *ifm)
ab660b
 {
ab660b
-- 
ab660b
2.29.0
ab660b