thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
255e5e
From f66a28121435b4aaffb21cec51c3856ab627b751 Mon Sep 17 00:00:00 2001
255e5e
From: jmaloy <jmaloy@redhat.com>
255e5e
Date: Thu, 14 May 2020 21:13:14 +0100
255e5e
Subject: [PATCH 6/6] tcp_emu: fix unsafe snprintf() usages
255e5e
MIME-Version: 1.0
255e5e
Content-Type: text/plain; charset=UTF-8
255e5e
Content-Transfer-Encoding: 8bit
255e5e
255e5e
RH-Author: jmaloy <jmaloy@redhat.com>
255e5e
Message-id: <20200514211314.1534001-7-jmaloy@redhat.com>
255e5e
Patchwork-id: 96590
255e5e
O-Subject: [RHEL-8.2.0 qemu-kvm PATCH v2 6/6] tcp_emu: fix unsafe snprintf() usages
255e5e
Bugzilla: 1834477
255e5e
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
255e5e
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
255e5e
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
255e5e
255e5e
From: Marc-André Lureau <marcandre.lureau@redhat.com>
255e5e
255e5e
Various calls to snprintf() assume that snprintf() returns "only" the
255e5e
number of bytes written (excluding terminating NUL).
255e5e
255e5e
https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04
255e5e
255e5e
"Upon successful completion, the snprintf() function shall return the
255e5e
number of bytes that would be written to s had n been sufficiently
255e5e
large excluding the terminating null byte."
255e5e
255e5e
Before patch ce131029, if there isn't enough room in "m_data" for the
255e5e
"DCC ..." message, we overflow "m_data".
255e5e
255e5e
After the patch, if there isn't enough room for the same, we don't
255e5e
overflow "m_data", but we set "m_len" out-of-bounds. The next time an
255e5e
access is bounded by "m_len", we'll have a buffer overflow then.
255e5e
255e5e
Use slirp_fmt*() to fix potential OOB memory access.
255e5e
255e5e
Reported-by: Laszlo Ersek <lersek@redhat.com>
255e5e
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
255e5e
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
255e5e
Message-Id: <20200127092414.169796-7-marcandre.lureau@redhat.com>
255e5e
255e5e
(cherry picked from libslirp commit 68ccb8021a838066f0951d4b2817eb6b6f10a843)
255e5e
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
255e5e
255e5e
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
255e5e
---
255e5e
 slirp/tcp_subr.c | 45 +++++++++++++++++++++------------------------
255e5e
 1 file changed, 21 insertions(+), 24 deletions(-)
255e5e
255e5e
diff --git a/slirp/tcp_subr.c b/slirp/tcp_subr.c
255e5e
index 53aa58d..32736bb 100644
255e5e
--- a/slirp/tcp_subr.c
255e5e
+++ b/slirp/tcp_subr.c
255e5e
@@ -662,8 +662,7 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 				NTOHS(n1);
255e5e
 				NTOHS(n2);
255e5e
 				m_inc(m, snprintf(NULL, 0, "%d,%d\r\n", n1, n2) + 1);
255e5e
-				m->m_len = snprintf(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
255e5e
-				assert(m->m_len < M_ROOM(m));
255e5e
+                                m->m_len = slirp_fmt(m->m_data, M_ROOM(m), "%d,%d\r\n", n1, n2);
255e5e
 			} else {
255e5e
 				*eol = '\r';
255e5e
 			}
255e5e
@@ -701,11 +700,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 			n2 = ((laddr >> 16) & 0xff);
255e5e
 			n3 = ((laddr >> 8)  & 0xff);
255e5e
 			n4 =  (laddr & 0xff);
255e5e
-
255e5e
 			m->m_len = bptr - m->m_data; /* Adjust length */
255e5e
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
255e5e
-                                             "ORT %d,%d,%d,%d,%d,%d\r\n%s",
255e5e
-                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
255e5e
+			m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
255e5e
+			                      "ORT %d,%d,%d,%d,%d,%d\r\n%s",
255e5e
+			                      n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
255e5e
 			return 1;
255e5e
 		} else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
255e5e
 			/*
255e5e
@@ -736,10 +734,9 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 			n4 =  (laddr & 0xff);
255e5e
 
255e5e
 			m->m_len = bptr - m->m_data; /* Adjust length */
255e5e
-			m->m_len += snprintf(bptr, M_FREEROOM(m),
255e5e
-                                             "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
255e5e
-                                             n1, n2, n3, n4, n5, n6, x==7?buff:"");
255e5e
-
255e5e
+			m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
255e5e
+			                      "27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
255e5e
+			                      n1, n2, n3, n4, n5, n6, x == 7 ? buff : "");
255e5e
 			return 1;
255e5e
 		}
255e5e
 
255e5e
@@ -762,8 +759,8 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 		if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
255e5e
 		    (so = tcp_listen(slirp, INADDR_ANY, 0, so->so_laddr.s_addr,
255e5e
 		                     htons(lport), SS_FACCEPTONCE)) != NULL)
255e5e
-                    m->m_len = snprintf(m->m_data, M_ROOM(m),
255e5e
-                                        "%d", ntohs(so->so_fport)) + 1;
255e5e
+                    m->m_len = slirp_fmt0(m->m_data, M_ROOM(m),
255e5e
+                                          "%d", ntohs(so->so_fport));
255e5e
 		return 1;
255e5e
 
255e5e
 	 case EMU_IRC:
255e5e
@@ -783,10 +780,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 				return 1;
255e5e
 			}
255e5e
 			m->m_len = bptr - m->m_data; /* Adjust length */
255e5e
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
255e5e
-                                             "DCC CHAT chat %lu %u%c\n",
255e5e
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
-                                             ntohs(so->so_fport), 1);
255e5e
+                        m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
255e5e
+                                              "DCC CHAT chat %lu %u%c\n",
255e5e
+                                              (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
+                                              ntohs(so->so_fport), 1);
255e5e
 		} else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
255e5e
 			if ((so = tcp_listen(slirp, INADDR_ANY, 0,
255e5e
 			                     htonl(laddr), htons(lport),
255e5e
@@ -794,10 +791,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 				return 1;
255e5e
 			}
255e5e
 			m->m_len = bptr - m->m_data; /* Adjust length */
255e5e
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
255e5e
-                                             "DCC SEND %s %lu %u %u%c\n", buff,
255e5e
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
-                                             ntohs(so->so_fport), n1, 1);
255e5e
+			m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
255e5e
+			                      "DCC SEND %s %lu %u %u%c\n", buff,
255e5e
+			                      (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
+			                      ntohs(so->so_fport), n1, 1);
255e5e
 		} else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
255e5e
 			if ((so = tcp_listen(slirp, INADDR_ANY, 0,
255e5e
 			                     htonl(laddr), htons(lport),
255e5e
@@ -805,10 +802,10 @@ tcp_emu(struct socket *so, struct mbuf *m)
255e5e
 				return 1;
255e5e
 			}
255e5e
 			m->m_len = bptr - m->m_data; /* Adjust length */
255e5e
-                        m->m_len += snprintf(bptr, M_FREEROOM(m),
255e5e
-                                             "DCC MOVE %s %lu %u %u%c\n", buff,
255e5e
-                                             (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
-                                             ntohs(so->so_fport), n1, 1);
255e5e
+			m->m_len += slirp_fmt(bptr, M_FREEROOM(m),
255e5e
+			                      "DCC MOVE %s %lu %u %u%c\n", buff,
255e5e
+			                      (unsigned long)ntohl(so->so_faddr.s_addr),
255e5e
+			                      ntohs(so->so_fport), n1, 1);
255e5e
 		}
255e5e
 		return 1;
255e5e
 
255e5e
-- 
255e5e
1.8.3.1
255e5e