Skip to contentSkip to conversation

slirp: fork_exec(): Don't close() a negative number in fork_exec()

PATCH Applied Peter Maydell 4 participants 7 messages Jul 9, 2017 – Jul 11, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 slirp: fork_exec(): Don't close() a negative number in fork_exec()

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

---
Issue 2 of 2 in CID 1005727 is trickier -- we need to move as much as possible of the client-end connect/accept out of the child process and into the parent as possible. I'm not sure if it's safe to do it all in the parent without deadlocking...

 slirp/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
slirp/misc.c +3 -1
index 88e9d94197..260187b6b6 100644
@@ -112,7 +112,9 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
 		    bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
 		    listen(s, 1) < 0) {
 			error_report("Error: inet socket: %s", strerror(errno));
-			closesocket(s);
+			if (s >= 0) {
+			    closesocket(s);
+			}
 
 			return 0;
 		}
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · * Peter Maydell ([email protected]) wrote:

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

Quoted 8 lines

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

---
Issue 2 of 2 in CID 1005727 is trickier -- we need to move as much as possible of the client-end connect/accept out of the child process and into the parent as possible. I'm not sure if it's safe to do it all in the parent without deadlocking...

or just bail earlier? The bit that worries me there
is the dup2(s, [012]); which is called unchecked, if that fails then your telnetd or whatever probably ends up connected to whatever your 0..2 were originally.

Quoted 8 lines
 slirp/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
slirp/misc.c +0 -0
index 88e9d94197..260187b6b6 100644

slirp/misc.c fork_exec(struct socket *so, const char *ex, int do_pty) · line 117

Quoted 7 lines
slirp/misc.c +3 -1
@@ -112,7 +112,9 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
 		    bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
 		    listen(s, 1) < 0) {
 			error_report("Error: inet socket: %s", strerror(errno));
-			closesocket(s);
+			if (s >= 0) {
+			    closesocket(s);
+			}

(I'm not convinced this would ever do anything bad, at least on a *nix system, the -ve value is always going to be an invalid fd so the close will just fail).

Dave

Quoted 6 lines
slirp/misc.c +0 -0
 			return 0;
 		}
Signature · 1 line
2.11.0

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#2 lore raw

[cc'd Eric as the sort of person

Quoted 7 lines · Dr. David Alan Gilbert
Quoted 6 lines · * Peter Maydell ([email protected]) wrote:

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

Quoted 8 lines
Quoted 6 lines

---
Issue 2 of 2 in CID 1005727 is trickier -- we need to move as much as possible of the client-end connect/accept out of the child process and into the parent as possible. I'm not sure if it's safe to do it all in the parent without deadlocking...

or just bail earlier?

The problem is you can only bail while you're in the parent before forking. Once you've started the child there's no mechanism for dealing with failure.

Quoted 4 lines

The bit that worries me there
is the dup2(s, [012]); which is called unchecked, if that fails then your telnetd or whatever probably ends up connected to whatever your 0..2 were originally.

dup2() in a child is actually pretty safe -- the only ways it can fail are:
* fd2 isn't actually an open file descriptor (can't happen)
* fd1 is negative or bigger than OPEN_MAX (can't happen)
* EINTR (just retry, I guess)

The awkward part is POSIX says that dup2() may fail with EIO if the close() of newfd failed, in which case I dunno what the child is supposed to do about it -- do a manual close(), ignore the error from close() and then dup2() again??
Linux specifically says it doesn't do this, and BSD/OSX don't document EIO as possible so I assume they have sane behaviour.

In any case, ignoring the possibility that dup2(s, [012]) in a child process could fail is AFAIK very very widespread standard behaviour for unix daemons. (We have another example in os_setup_post() in os-posix.c, for instance.)

Random extra: Linux dup2() manpage has a mysterious remark about EBUSY -- does anybody know what that's all about? It's not sanctioned by POSIX...

What I would like to do and think should be safe is:

    s = qemu_socket(...);
    bind(s);
    listen(s, 1);
    cs = qemu_socket(...);
    connect(cs, ...);
    switch (fork ()) {
        child:
           dup2
           close fds
           execvp(...);
        parent:
           break;
    }
    close(cs);
    ss = accept(s, ...);
    close(s);
    etc;

ie push the bind/listen/create client socket/connect up into before the fork(), to give the behaviour of "like socketpair() but for AF_INET".

(I believe this will work and not deadlock because connect() doesn't block until accept(), it only needs the tcp handshake.)

Quoted 19 lines
Quoted 8 lines
 slirp/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
slirp/misc.c +0 -0
index 88e9d94197..260187b6b6 100644

slirp/misc.c fork_exec(struct socket *so, const char *ex, int do_pty) · line 117

Quoted 7 lines
slirp/misc.c +3 -1
@@ -112,7 +112,9 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
                  bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
                  listen(s, 1) < 0) {
                      error_report("Error: inet socket: %s", strerror(errno));
-                     closesocket(s);
+                     if (s >= 0) {
+                         closesocket(s);
+                     }
Quoted 3 lines

(I'm not convinced this would ever do anything bad, at least on a *nix system, the -ve value is always going to be an invalid fd so the close will just fail).

Indeed. But it keeps Coverity happy.

thanks
-- PMM

#3 lore raw
Quoted 17 lines · * Peter Maydell ([email protected]) wrote:

[cc'd Eric as the sort of person

Quoted 14 lines · Dr. David Alan Gilbert
Quoted 12 lines · * Peter Maydell ([email protected]) wrote:

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

---
Issue 2 of 2 in CID 1005727 is trickier -- we need to move as much as possible of the client-end connect/accept out of the child process and into the parent as possible. I'm not sure if it's safe to do it all in the parent without deadlocking...

Quoted 5 lines
Quoted 1 line

or just bail earlier?

The problem is you can only bail while you're in the parent before forking. Once you've started the child there's no mechanism for dealing with failure.

Well, you can always exit the child before anything worse can happen.

Quoted 10 lines
Quoted 4 lines

The bit that worries me there
is the dup2(s, [012]); which is called unchecked, if that fails then your telnetd or whatever probably ends up connected to whatever your 0..2 were originally.

dup2() in a child is actually pretty safe -- the only ways it can fail are:
* fd2 isn't actually an open file descriptor (can't happen)
* fd1 is negative or bigger than OPEN_MAX (can't happen)
* EINTR (just retry, I guess)

True, I'd missed that fd1 was probably always a valid fd; so probably the rest of this is pretty academic.

Quoted 4 lines

The awkward part is POSIX says that dup2() may fail with EIO if the close() of newfd failed, in which case I dunno what the child is supposed to do about it -- do a manual close(), ignore the error from close() and then dup2() again??

I wouldn't like to bet on it being legal to call close() on an error, what state is the fd you wanted to close in?

Quoted 32 lines

Linux specifically says it doesn't do this, and BSD/OSX don't document EIO as possible so I assume they have sane behaviour.

In any case, ignoring the possibility that dup2(s, [012]) in a child process could fail is AFAIK very very widespread standard behaviour for unix daemons. (We have another example in os_setup_post() in os-posix.c, for instance.)

Random extra: Linux dup2() manpage has a mysterious remark about EBUSY -- does anybody know what that's all about? It's not sanctioned by POSIX...

What I would like to do and think should be safe is:

    s = qemu_socket(...);
    bind(s);
    listen(s, 1);
    cs = qemu_socket(...);
    connect(cs, ...);
    switch (fork ()) {
        child:
           dup2
           close fds
           execvp(...);
        parent:
           break;
    }
    close(cs);
    ss = accept(s, ...);
    close(s);
    etc;
Quoted 6 lines

ie push the bind/listen/create client socket/connect up into before the fork(), to give the behaviour of "like socketpair() but for AF_INET".

(I believe this will work and not deadlock because connect() doesn't block until accept(), it only needs the tcp handshake.)

OK, I don't know the details of the blocking htere.

Dave

Quoted 19 lines
Quoted 19 lines
Quoted 8 lines
 slirp/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
slirp/misc.c +0 -0
index 88e9d94197..260187b6b6 100644

slirp/misc.c fork_exec(struct socket *so, const char *ex, int do_pty) · line 117

Quoted 7 lines
slirp/misc.c +3 -1
@@ -112,7 +112,9 @@ fork_exec(struct socket *so, const char *ex, int do_pty)
                  bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
                  listen(s, 1) < 0) {
                      error_report("Error: inet socket: %s", strerror(errno));
-                     closesocket(s);
+                     if (s >= 0) {
+                         closesocket(s);
+                     }
Quoted 8 lines
Quoted 3 lines

(I'm not convinced this would ever do anything bad, at least on a *nix system, the -ve value is always going to be an invalid fd so the close will just fail).

Indeed. But it keeps Coverity happy.

thanks
-- PMM

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 17 lines · * Peter Maydell ([email protected]) wrote:

[cc'd Eric as the sort of person

Quoted 14 lines · Dr. David Alan Gilbert
Quoted 12 lines · * Peter Maydell ([email protected]) wrote:

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

---
Issue 2 of 2 in CID 1005727 is trickier -- we need to move as much as possible of the client-end connect/accept out of the child process and into the parent as possible. I'm not sure if it's safe to do it all in the parent without deadlocking...

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

or just bail earlier?

The problem is you can only bail while you're in the parent before forking. Once you've started the child there's no mechanism for dealing with failure.

Well, you can always exit the child before anything worse can happen.

You need a mechanism then for causing the parent to notice. The current code would leave the parent in a blocking accept() call forever (this is what all the XXX comments in the current code are about).

thanks
-- PMM

#5 lore raw
Quoted 11 lines · Peter Maydell
Quoted 6 lines
Quoted 4 lines

The problem is you can only bail while you're in the parent before forking. Once you've started the child there's no mechanism for dealing with failure.

Well, you can always exit the child before anything worse can happen.

You need a mechanism then for causing the parent to notice. The current code would leave the parent in a blocking accept() call forever (this is what all the XXX comments in the current code are about).

You can have the parent create a pipe before forking and does a blocking read, the child then manages fds and writes success (or failure) to its end of the pipe (or an early exit causes the parent to read EOF); when the parent finally gets word on the pipe, then it knows whether the child is dead-on-arrival due to early setup failures, or whether it is now safe to block on accept(). But it can easily get more and more complex the more you want to handle; and if the parent is multi-threaded, you have to be careful that you only call async-signal-safe functions in the child between the fork and exec.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw
In reply to Peter Maydell
Quoted 5 lines · Peter Maydell, on dim. 09 juil. 2017 18:54:22 +0100, wrote:

In a fork_exec() error path we try to closesocket(s) when s might be a negative number because the thing that failed was the qemu_socket() call. Add a guard so we don't do this.

(Spotted by Coverity: CID 1005727 issue 1 of 2.)

Applied to my tree, thanks!

Samuel