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