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