yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From 9a7810c257711ce02627916d886fc1029f7a8190 Mon Sep 17 00:00:00 2001
ddf19c
From: jmaloy <jmaloy@redhat.com>
ddf19c
Date: Thu, 13 Feb 2020 15:50:49 +0000
ddf19c
Subject: [PATCH 3/7] tcp_emu: fix unsafe snprintf() usages
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: jmaloy <jmaloy@redhat.com>
ddf19c
Message-id: <20200213155049.3936-3-jmaloy@redhat.com>
ddf19c
Patchwork-id: 93826
ddf19c
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 2/2] tcp_emu: fix unsafe snprintf() usages
ddf19c
Bugzilla: 1798994
ddf19c
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
ddf19c
From: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
ddf19c
Various calls to snprintf() assume that snprintf() returns "only" the
ddf19c
number of bytes written (excluding terminating NUL).
ddf19c
ddf19c
https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04
ddf19c
ddf19c
"Upon successful completion, the snprintf() function shall return the
ddf19c
number of bytes that would be written to s had n been sufficiently
ddf19c
large excluding the terminating null byte."
ddf19c
ddf19c
Before patch ce131029, if there isn't enough room in "m_data" for the
ddf19c
"DCC ..." message, we overflow "m_data".
ddf19c
ddf19c
After the patch, if there isn't enough room for the same, we don't
ddf19c
overflow "m_data", but we set "m_len" out-of-bounds. The next time an
ddf19c
access is bounded by "m_len", we'll have a buffer overflow then.
ddf19c
ddf19c
Use slirp_fmt*() to fix potential OOB memory access.
ddf19c
ddf19c
Reported-by: Laszlo Ersek <lersek@redhat.com>
ddf19c
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
ddf19c
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
ddf19c
Message-Id: <20200127092414.169796-7-marcandre.lureau@redhat.com>
ddf19c
(cherry picked from libslirp commit 68ccb8021a838066f0951d4b2817eb6b6f10a843)
ddf19c
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
ddf19c
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 slirp/src/tcp_subr.c | 44 +++++++++++++++++++++-----------------------
ddf19c
 1 file changed, 21 insertions(+), 23 deletions(-)
ddf19c
ddf19c
diff --git a/slirp/src/tcp_subr.c b/slirp/src/tcp_subr.c
ddf19c
index 954d1a6..26d4ead 100644
ddf19c
--- a/slirp/src/tcp_subr.c
ddf19c
+++ b/slirp/src/tcp_subr.c
ddf19c
@@ -655,8 +655,7 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
                 NTOHS(n1);
ddf19c
                 NTOHS(n2);
ddf19c
                 m_inc(m, snprintf(NULL, 0, "%d,%d\r\n", n1, n2) + 1);
ddf19c
-                m->m_len = snprintf(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
ddf19c
-                assert(m->m_len < M_ROOM(m));
ddf19c
+                m->m_len = slirp_fmt(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
ddf19c
             } else {
ddf19c
                 *eol = '\r';
ddf19c
             }
ddf19c
@@ -696,9 +695,9 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
             n4 = (laddr & 0xff);
ddf19c
 
ddf19c
             m->m_len = bptr - m->m_data; /* Adjust length */
ddf19c
-            m->m_len += snprintf(bptr, M_FREEROOM(m),
ddf19c
-                                 "ORT %d,%d,%d,%d,%d,%d\r\n%s", n1, n2, n3, n4,
ddf19c
-                                 n5, n6, x == 7 ? buff : "");
ddf19c
+            m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
ddf19c
+                                  "ORT %d,%d,%d,%d,%d,%d\r\n%s",
ddf19c
+                                  n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
ddf19c
             return 1;
ddf19c
         } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
ddf19c
             /*
ddf19c
@@ -731,10 +730,9 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
             n4 = (laddr & 0xff);
ddf19c
 
ddf19c
             m->m_len = bptr - m->m_data; /* Adjust length */
ddf19c
-            m->m_len += snprintf(bptr, M_FREEROOM(m),
ddf19c
-                         "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
ddf19c
-                         n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
ddf19c
-
ddf19c
+            m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
ddf19c
+                                  "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
ddf19c
+                                  n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
ddf19c
             return 1;
ddf19c
         }
ddf19c
 
ddf19c
@@ -757,8 +755,8 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
         if (m->m_data[m->m_len - 1] == '\0' && lport != 0 &&
ddf19c
             (so = tcp_listen(slirp, INADDR_ANY, 0, so->so_laddr.s_addr,
ddf19c
                              htons(lport), SS_FACCEPTONCE)) != NULL)
ddf19c
-            m->m_len = snprintf(m->m_data, M_ROOM(m),
ddf19c
-                                "%d", ntohs(so->so_fport)) + 1;
ddf19c
+            m->m_len = slirp_fmt0(m->m_data, M_ROOM(m),
ddf19c
+                                  "%d", ntohs(so->so_fport));
ddf19c
         return 1;
ddf19c
 
ddf19c
     case EMU_IRC:
ddf19c
@@ -777,10 +775,10 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
                 return 1;
ddf19c
             }
ddf19c
             m->m_len = bptr - m->m_data; /* Adjust length */
ddf19c
-            m->m_len += snprintf(bptr, M_FREEROOM(m),
ddf19c
-                                 "DCC CHAT chat %lu %u%c\n",
ddf19c
-                                 (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
-                                 ntohs(so->so_fport), 1);
ddf19c
+            m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
ddf19c
+                                  "DCC CHAT chat %lu %u%c\n",
ddf19c
+                                  (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
+                                  ntohs(so->so_fport), 1);
ddf19c
         } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport,
ddf19c
                           &n1) == 4) {
ddf19c
             if ((so = tcp_listen(slirp, INADDR_ANY, 0, htonl(laddr),
ddf19c
@@ -788,10 +786,10 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
                 return 1;
ddf19c
             }
ddf19c
             m->m_len = bptr - m->m_data; /* Adjust length */
ddf19c
-            m->m_len += snprintf(bptr, M_FREEROOM(m),
ddf19c
-                         "DCC SEND %s %lu %u %u%c\n", buff,
ddf19c
-                         (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
-                         ntohs(so->so_fport), n1, 1);
ddf19c
+            m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
ddf19c
+                                  "DCC SEND %s %lu %u %u%c\n", buff,
ddf19c
+                                  (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
+                                  ntohs(so->so_fport), n1, 1);
ddf19c
         } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport,
ddf19c
                           &n1) == 4) {
ddf19c
             if ((so = tcp_listen(slirp, INADDR_ANY, 0, htonl(laddr),
ddf19c
@@ -799,10 +797,10 @@ int tcp_emu(struct socket *so, struct mbuf *m)
ddf19c
                 return 1;
ddf19c
             }
ddf19c
             m->m_len = bptr - m->m_data; /* Adjust length */
ddf19c
-            m->m_len += snprintf(bptr, M_FREEROOM(m),
ddf19c
-                         "DCC MOVE %s %lu %u %u%c\n", buff,
ddf19c
-                         (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
-                         ntohs(so->so_fport), n1, 1);
ddf19c
+            m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
ddf19c
+                                  "DCC MOVE %s %lu %u %u%c\n", buff,
ddf19c
+                                  (unsigned long)ntohl(so->so_faddr.s_addr),
ddf19c
+                                  ntohs(so->so_fport), n1, 1);
ddf19c
         }
ddf19c
         return 1;
ddf19c
 
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c