Memory fixes for slirp tcp_emu()
cover Memory fixes for slirp tcp_emu()
Hi,
See the following 2 patches fixing some memory issues in tcp_emu(). Those are not regressions, consider it for 4.1.
Marc-André Lureau (2):
slirp: ensure there is enough space in mbuf to null-terminate
slirp: don't manipulate so_rcv in tcp_emu()
slirp/src/tcp_subr.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-)
Signature · 1 line
2.21.0.313.ge35b8cb8e2
PATCH 1/2 slirp: ensure there is enough space in mbuf to null-terminate
Prevents from buffer overflows.
Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1664205
--- slirp/src/tcp_subr.c | 3 +++ 1 file changed, 3 insertions(+)
slirp/src/tcp_subr.c +3 -0
@@ -644,6 +644,7 @@ tcp_emu(struct socket *so, struct mbuf *m)memcpy (so_rcv->sb_wptr, m->m_data, m->m_len); so_rcv->sb_wptr += m->m_len; so_rcv->sb_rptr += m->m_len; +m_inc (m, m->m_len +1 ); m->m_data[m->m_len] =0 ;/* NULL terminate */ if (strchr (m->m_data,'\r' ) ||strchr (m->m_data,'\n' )) {if (sscanf (so_rcv->sb_data,"%u%*[ ,]%u" , &n1, &n2) ==2 ) {
@@ -677,6 +678,7 @@ tcp_emu(struct socket *so, struct mbuf *m) }case EMU_FTP :/* ftp */ +m_inc (m, m->m_len +1 ); *(m->m_data+m->m_len) =0 ;/* NUL terminate for strstr */ if ((bptr = (char *)strstr (m->m_data,"ORT" )) !=NULL ) {/*
@@ -774,6 +776,7 @@ tcp_emu(struct socket *so, struct mbuf *m)/* * Need to emulate DCC CHAT, DCC SEND and DCC MOVE */ +m_inc (m, m->m_len +1 ); *(m->m_data+m->m_len) =0 ;/* NULL terminate the string for strstr */ if ((bptr = (char *)strstr (m->m_data,"DCC" )) ==NULL )return 1 ;
Signature · 1 line
2.21.0.313.ge35b8cb8e2
PATCH 2/2 slirp: don't manipulate so_rcv in tcp_emu()
In reply to Marc-André LureauFor some reason, EMU_IDENT is not like other "emulated" protocols and tries to reconstitute the original buffer, if it came in multiple packets. Unfortunately, it does so wrongly, as it doesn't respect the sbuf circular buffer appending rules, nor does it maintain some of the invariants (rptr is incremented without bounds, etc): this leads to further memory corruption revealed by ASAN or various malloc errors. Furthermore, the so_rcv buffer is regularly flushed, so there is no guarantee that buffer reconstruction will do what is expected.
Instead, do what the function comment says: "XXX Assumes the whole command came in one packet", and don't touch so_rcv.
Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1664205
--- slirp/src/tcp_subr.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-)
slirp/src/tcp_subr.c +5 -21
@@ -634,20 +634,9 @@ tcp_emu(struct socket *so, struct mbuf *m)struct socket *tmpso;struct sockaddr_in addr;socklen_t addrlen =sizeof (struct sockaddr_in ); -struct sbuf *so_rcv = &so->so_rcv; -if (m->m_len > so_rcv->sb_datalen - - (so_rcv->sb_wptr - so_rcv->sb_data)) { -return 1 ; - } - -memcpy (so_rcv->sb_wptr, m->m_data, m->m_len); - so_rcv->sb_wptr += m->m_len; - so_rcv->sb_rptr += m->m_len; -m_inc (m, m->m_len +1 ); - m->m_data[m->m_len] =0 ;/* NULL terminate */ -if (strchr (m->m_data,'\r' ) ||strchr (m->m_data,'\n' )) { -if (sscanf (so_rcv->sb_data,"%u%*[ ,]%u" , &n1, &n2) ==2 ) { +if (g_strstr_len (m->m_data, m->m_len,"\r\n" ) + &&sscanf (m->m_data,"%u%*[ ,]%u\r\n" , &n1, &n2) ==2 ) {HTONS (n1);HTONS (n2);/* n2 is the one on our host */
@@ -666,15 +655,10 @@ tcp_emu(struct socket *so, struct mbuf *m) }NTOHS (n1);NTOHS (n2); - so_rcv->sb_cc =snprintf (so_rcv->sb_data, - so_rcv->sb_datalen, -"%d,%d\r\n" , n1, n2); - so_rcv->sb_rptr = so_rcv->sb_data; - so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc; + m->m_len =snprintf (m->m_data, m->m_size, +"%d,%d\r\n" , n1, n2); } - } -m_free (m); -return 0 ; +return 1 ; }case EMU_FTP :/* ftp */
Signature · 1 line
2.21.0.313.ge35b8cb8e2
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Subject: [Qemu-devel] [PATCH 0/2] Memory fixes for slirp tcp_emu()
Type: series
Message-id: [email protected]
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
7c6d6d0 slirp: don't manipulate so_rcv in tcp_emu() 84d8e3e slirp: ensure there is enough space in mbuf to null-terminate
=== OUTPUT BEGIN ===
1/2 Checking commit 84d8e3ea687d (slirp: ensure there is enough space in mbuf to null-terminate)
ERROR: code indent should never use tabs
#22: FILE: slirp/src/tcp_subr.c:647:
+^I^I^Im_inc(m, m->m_len + 1);$
ERROR: code indent should never use tabs
#38: FILE: slirp/src/tcp_subr.c:779:
+^I^Im_inc(m, m->m_len + 1);$
total: 2 errors, 0 warnings, 21 lines checked
Patch 1/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
2/2 Checking commit 7c6d6d0e8c68 (slirp: don't manipulate so_rcv in tcp_emu())
ERROR: code indent should never use tabs
#47: FILE: slirp/src/tcp_subr.c:638:
+^I^I^Iif (g_strstr_len(m->m_data, m->m_len, "\r\n")$
WARNING: line over 80 characters
#48: FILE: slirp/src/tcp_subr.c:639:
+ && sscanf(m->m_data, "%u%*[ ,]%u\r\n", &n1, &n2) == 2) {
ERROR: code indent should never use tabs
#48: FILE: slirp/src/tcp_subr.c:639:
+^I^I^I^I&& sscanf(m->m_data, "%u%*[ ,]%u\r\n", &n1, &n2) == 2) {$
WARNING: line over 80 characters
#61: FILE: slirp/src/tcp_subr.c:658:
+ m->m_len = snprintf(m->m_data, m->m_size,
ERROR: code indent should never use tabs
#61: FILE: slirp/src/tcp_subr.c:658:
+^I^I^I^I^Im->m_len = snprintf(m->m_data, m->m_size,$
ERROR: line over 90 characters
#62: FILE: slirp/src/tcp_subr.c:659:
+ "%d,%d\r\n", n1, n2);
ERROR: code indent should never use tabs
#62: FILE: slirp/src/tcp_subr.c:659:
+^I^I^I^I^I^I^I^I^I^I"%d,%d\r\n", n1, n2);$
ERROR: code indent should never use tabs
#67: FILE: slirp/src/tcp_subr.c:661:
+^I^I^Ireturn 1;$
total: 6 errors, 2 warnings, 40 lines checked
Patch 2/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]