thebeanogamer / rpms / qemu-kvm

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