[PULL,1/4] slirp: Don't pass possibly -1 fd to send()

Message ID 20181110140925.22457-2-samuel.thibault@ens-lyon.org
State New
Headers show
Series
  • [PULL,1/4] slirp: Don't pass possibly -1 fd to send()
Related show

Commit Message

Samuel Thibault Nov. 10, 2018, 2:09 p.m.
From: Peter Maydell <peter.maydell@linaro.org>


Coverity complains (CID 1005726) that we might pass -1 as the fd
argument to send() in slirp_send(), because we previously checked for
"so->s == -1 && so->extra".  The case of "so->s == -1 but so->extra
NULL" should not in theory happen, but it is hard to guarantee
because various places in the code do so->s = qemu_socket(...) and so
will end up with so->s == -1 on failure, and not all the paths which
call that always throw away the socket in that case (eg
tcp_fconnect()).  So just check specifically for the condition and
fail slirp_send().

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

---
 slirp/slirp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

-- 
2.19.1

Patch

diff --git a/slirp/slirp.c b/slirp/slirp.c
index 51de41fc02..3c3c03b22f 100644
--- a/slirp/slirp.c
+++ b/slirp/slirp.c
@@ -1091,6 +1091,17 @@  ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
         return len;
     }
 
+    if (so->s == -1) {
+        /*
+         * This should in theory not happen but it is hard to be
+         * sure because some code paths will end up with so->s == -1
+         * on a failure but don't dispose of the struct socket.
+         * Check specifically, so we don't pass -1 to send().
+         */
+        errno = EBADF;
+        return -1;
+    }
+
     return send(so->s, buf, len, flags);
 }