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