Blame SOURCES/kvm-tcp_emu-fix-unsafe-snprintf-usages.patch

e3b251
From bac27b0e9302c5b869d962879d43df1ebdd09c83 Mon Sep 17 00:00:00 2001
e3b251
From: jmaloy <jmaloy@redhat.com>
e3b251
Date: Wed, 19 Feb 2020 16:29:23 +0100
e3b251
Subject: [PATCH 2/2] tcp_emu: fix unsafe snprintf() usages
e3b251
MIME-Version: 1.0
e3b251
Content-Type: text/plain; charset=UTF-8
e3b251
Content-Transfer-Encoding: 8bit
e3b251
e3b251
Message-id: <20200219162923.18327-3-jmaloy@redhat.com>
e3b251
Patchwork-id: 93977
e3b251
O-Subject: [RHEL-7.8 qemu-kvm-rhev PATCH v2 2/2] tcp_emu: fix unsafe snprintf() usages
e3b251
Bugzilla: 1798974
e3b251
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
e3b251
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
e3b251
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
e3b251
e3b251
From: Marc-André Lureau <marcandre.lureau@redhat.com>
e3b251
e3b251
Various calls to snprintf() assume that snprintf() returns "only" the
e3b251
number of bytes written (excluding terminating NUL).
e3b251
e3b251
https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04
e3b251
e3b251
"Upon successful completion, the snprintf() function shall return the
e3b251
number of bytes that would be written to s had n been sufficiently
e3b251
large excluding the terminating null byte."
e3b251
e3b251
Before patch ce131029, if there isn't enough room in "m_data" for the
e3b251
"DCC ..." message, we overflow "m_data".
e3b251
e3b251
After the patch, if there isn't enough room for the same, we don't
e3b251
overflow "m_data", but we set "m_len" out-of-bounds. The next time an
e3b251
access is bounded by "m_len", we'll have a buffer overflow then.
e3b251
e3b251
Use slirp_fmt*() to fix potential OOB memory access.
e3b251
e3b251
Reported-by: Laszlo Ersek <lersek@redhat.com>
e3b251
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
e3b251
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
e3b251
Message-Id: <20200127092414.169796-7-marcandre.lureau@redhat.com>
e3b251
(cherry picked from libslirp commit 68ccb8021a838066f0951d4b2817eb6b6f10a843)
e3b251
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
e3b251
e3b251
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
e3b251
---
e3b251
 slirp/tcp_subr.c | 44 +++++++++++++++++++++-----------------------
e3b251
 1 file changed, 21 insertions(+), 23 deletions(-)
e3b251
e3b251
diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c
e3b251
index c5196a4..944e1d7 100644
e3b251
--- a/slirp/tcp_subr.c
e3b251
+++ b/slirp/tcp_subr.c
e3b251
@@ -665,8 +665,7 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 				NTOHS(n1);
e3b251
 				NTOHS(n2);
e3b251
 				m_inc(m, snprintf(NULL, 0, "%d,%d\r\n", n1, n2) + 1);
e3b251
-				m->m_len = snprintf(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
e3b251
-				assert(m->m_len < M_ROOM(m));
e3b251
+                                m->m_len = slirp_fmt(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
e3b251
 			} else {
e3b251
 				*eol = '\r';
e3b251
 			}
e3b251
@@ -706,9 +705,9 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 			n4 =  (laddr & 0xff);
e3b251
 
e3b251
 			m->m_len = bptr - m->m_data; /* Adjust length */
e3b251
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
e3b251
-                                             "ORT %d,%d,%d,%d,%d,%d\r\n%s",
e3b251
-                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
e3b251
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
e3b251
+                                              "ORT %d,%d,%d,%d,%d,%d\r\n%s",
e3b251
+                                              n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
e3b251
 			return 1;
e3b251
 		} else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
e3b251
 			/*
e3b251
@@ -739,10 +738,9 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 			n4 =  (laddr & 0xff);
e3b251
 
e3b251
 			m->m_len = bptr - m->m_data; /* Adjust length */
e3b251
-			m->m_len += snprintf(bptr, M_FREEROOM(m),
e3b251
-                                             "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
e3b251
-                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
e3b251
-
e3b251
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
e3b251
+                                              "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
e3b251
+                                              n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
e3b251
 			return 1;
e3b251
 		}
e3b251
 
e3b251
@@ -765,8 +763,8 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 		if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
e3b251
 		    (so = tcp_listen(slirp, INADDR_ANY, 0, so->so_laddr.s_addr,
e3b251
 		                     htons(lport), SS_FACCEPTONCE)) != NULL)
e3b251
-                    m->m_len = snprintf(m->m_data, M_ROOM(m),
e3b251
-                                        "%d", ntohs(so->so_fport)) + 1;
e3b251
+                    m->m_len = slirp_fmt0(m->m_data, M_ROOM(m),
e3b251
+                                          "%d", ntohs(so->so_fport));
e3b251
 		return 1;
e3b251
 
e3b251
 	 case EMU_IRC:
e3b251
@@ -786,10 +784,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 				return 1;
e3b251
 			}
e3b251
 			m->m_len = bptr - m->m_data; /* Adjust length */
e3b251
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
e3b251
-                                             "DCC CHAT chat %lu %u%c\n",
e3b251
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
-                                             ntohs(so->so_fport), 1);
e3b251
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
e3b251
+                                              "DCC CHAT chat %lu %u%c\n",
e3b251
+                                              (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
+                                              ntohs(so->so_fport), 1);
e3b251
 		} else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
e3b251
 			if ((so = tcp_listen(slirp, INADDR_ANY, 0,
e3b251
 			                     htonl(laddr), htons(lport),
e3b251
@@ -797,10 +795,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 				return 1;
e3b251
 			}
e3b251
 			m->m_len = bptr - m->m_data; /* Adjust length */
e3b251
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
e3b251
-                                             "DCC SEND %s %lu %u %u%c\n", buff,
e3b251
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
-                                             ntohs(so->so_fport), n1, 1);
e3b251
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
e3b251
+                                              "DCC SEND %s %lu %u %u%c\n", buff,
e3b251
+                                              (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
+                                              ntohs(so->so_fport), n1, 1);
e3b251
 		} else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
e3b251
 			if ((so = tcp_listen(slirp, INADDR_ANY, 0,
e3b251
 			                     htonl(laddr), htons(lport),
e3b251
@@ -808,10 +806,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
e3b251
 				return 1;
e3b251
 			}
e3b251
 			m->m_len = bptr - m->m_data; /* Adjust length */
e3b251
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
e3b251
-                                             "DCC MOVE %s %lu %u %u%c\n", buff,
e3b251
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
-                                             ntohs(so->so_fport), n1, 1);
e3b251
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
e3b251
+                                              "DCC MOVE %s %lu %u %u%c\n", buff,
e3b251
+                                              (unsigned long)ntohl(so->so_faddr.s_addr),
e3b251
+                                              ntohs(so->so_fport), n1, 1);
e3b251
 		}
e3b251
 		return 1;
e3b251
 
e3b251
-- 
e3b251
1.8.3.1
e3b251