yeahuh / rpms / qemu-kvm

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