From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Cc: patches@linaro.org,
Samuel Thibault <samuel.thibault@ens-lyon.org>,
Jan Kiszka <jan.kiszka@siemens.com>
Subject: [Qemu-devel] [PATCH for-3.1 4/4] slirp: fork_exec(): create and connect child socket before fork()
Date: Tue, 6 Nov 2018 15:13:23 +0000 [thread overview]
Message-ID: <20181106151323.16154-5-peter.maydell@linaro.org> (raw)
In-Reply-To: <20181106151323.16154-1-peter.maydell@linaro.org>
Currently fork_exec() fork()s, and then creates and connects the
child socket which it uses for communication with the parent in
the child process. This is awkward because the child has no
mechanism to report failure back to the parent, which might end
up blocked forever in accept(). The child code also has an issue
pointed out by Coverity (CID 1005727), where if the qemu_socket()
call fails it will pass -1 as a file descriptor to connect().
Fix these issues by moving the creation of the child's end of
the socket to before the fork(), where we are in a position to
handle a possible failure.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
slirp/misc.c | 55 ++++++++++++++++++++++++++++++++--------------------
1 file changed, 34 insertions(+), 21 deletions(-)
diff --git a/slirp/misc.c b/slirp/misc.c
index 260187b6b62..57bdd808e2d 100644
--- a/slirp/misc.c
+++ b/slirp/misc.c
@@ -85,9 +85,10 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
int
fork_exec(struct socket *so, const char *ex, int do_pty)
{
- int s;
- struct sockaddr_in addr;
+ int s, cs;
+ struct sockaddr_in addr, csaddr;
socklen_t addrlen = sizeof(addr);
+ socklen_t csaddrlen = sizeof(csaddr);
int opt;
const char *argv[256];
/* don't want to clobber the original */
@@ -120,10 +121,35 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
}
}
+ if (getsockname(s, (struct sockaddr *)&csaddr, &csaddrlen) < 0) {
+ closesocket(s);
+ return 0;
+ }
+ cs = qemu_socket(AF_INET, SOCK_STREAM, 0);
+ if (cs < 0) {
+ closesocket(s);
+ return 0;
+ }
+ csaddr.sin_addr = loopback_addr;
+ /*
+ * This connect won't block because we've already listen()ed on
+ * the server end (even though we won't accept() the connection
+ * until later on).
+ */
+ do {
+ ret = connect(cs, (struct sockaddr *)&csaddr, csaddrlen);
+ } while (ret < 0 && errno == EINTR);
+ if (ret < 0) {
+ closesocket(s);
+ closesocket(cs);
+ return 0;
+ }
+
pid = fork();
switch(pid) {
case -1:
error_report("Error: fork failed: %s", strerror(errno));
+ closesocket(cs);
close(s);
return 0;
@@ -131,21 +157,10 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
setsid();
/* Set the DISPLAY */
- getsockname(s, (struct sockaddr *)&addr, &addrlen);
close(s);
- /*
- * Connect to the socket
- * XXX If any of these fail, we're in trouble!
- */
- s = qemu_socket(AF_INET, SOCK_STREAM, 0);
- addr.sin_addr = loopback_addr;
- do {
- ret = connect(s, (struct sockaddr *)&addr, addrlen);
- } while (ret < 0 && errno == EINTR);
-
- dup2(s, 0);
- dup2(s, 1);
- dup2(s, 2);
+ dup2(cs, 0);
+ dup2(cs, 1);
+ dup2(cs, 2);
for (s = getdtablesize() - 1; s >= 3; s--)
close(s);
@@ -178,12 +193,10 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
default:
qemu_add_child_watch(pid);
+ closesocket(cs);
/*
- * XXX this could block us...
- * XXX Should set a timer here, and if accept() doesn't
- * return after X seconds, declare it a failure
- * The only reason this will block forever is if socket()
- * of connect() fail in the child process
+ * This should never block, because we already connect()ed
+ * on the child end before we forked.
*/
do {
so->s = accept(s, (struct sockaddr *)&addr, &addrlen);
--
2.19.1
next prev parent reply other threads:[~2018-11-06 15:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-06 15:13 [Qemu-devel] [PATCH for-3.1 0/4] slirp: fix coverity issues Peter Maydell
2018-11-06 15:13 ` [Qemu-devel] [PATCH for-3.1 1/4] slirp: Don't pass possibly -1 fd to send() Peter Maydell
2018-11-06 23:05 ` Samuel Thibault
2018-11-06 15:13 ` [Qemu-devel] [PATCH for-3.1 2/4] slirp: Use g_new() to allocate sockets in socreate() Peter Maydell
2018-11-06 23:07 ` Samuel Thibault
2018-11-06 15:13 ` [Qemu-devel] [PATCH for-3.1 3/4] slirp: Remove code that handles socreate() failure Peter Maydell
2018-11-06 23:08 ` Samuel Thibault
2018-11-06 15:13 ` Peter Maydell [this message]
2018-11-06 23:11 ` [Qemu-devel] [PATCH for-3.1 4/4] slirp: fork_exec(): create and connect child socket before fork() Samuel Thibault
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181106151323.16154-5-peter.maydell@linaro.org \
--to=peter.maydell@linaro.org \
--cc=jan.kiszka@siemens.com \
--cc=patches@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=samuel.thibault@ens-lyon.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).