aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-04-02 21:48:36 -0500
committerRob Landley <rob@landley.net>2019-04-02 21:48:36 -0500
commitaa349873624f2a5d4129c0ef8c33a1495d0b46fc (patch)
treed23ef41e1c638c2689d863c2969d8e313a251737
parentdaff8901cc80dda766a8094ae9737eaeae232917 (diff)
downloadtoybox-aa349873624f2a5d4129c0ef8c33a1495d0b46fc.tar.gz
Don't leak stdout pipe filehandle into children.
-rw-r--r--lib/xwrap.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/lib/xwrap.c b/lib/xwrap.c
index 31843edf..12170ada 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -235,20 +235,25 @@ pid_t xpopen_both(char **argv, int *pipes)
if (!(pid = CFG_TOYBOX_FORK ? xfork() : XVFORK())) {
// Child process: Dance of the stdin/stdout redirection.
+ // cestnepasun[1]->cestnepasun[0] and cestnepasun[2]->cestnepasun[1]
if (pipes) {
// if we had no stdin/out, pipe handles could overlap, so test for it
// and free up potentially overlapping pipe handles before reuse
+
+ // in child, close read end of output pipe, and return write end as out
if (cestnepasun[2]) {
close(cestnepasun[2]);
pipes[1] = cestnepasun[3];
}
+
+ // in child, close write end of input pipe, and return input end as out.
if (cestnepasun[1]) {
close(cestnepasun[1]);
pipes[0] = cestnepasun[0];
}
- // If swapping stdin/stdout
- if (!pipes[1]) pipes[1] = dup(pipes[1]);
+ // If swapping stdin/stdout, need to move a filehandle to temp
+ if (!pipes[1]) pipes[1] = dup(0);
// Are we redirecting stdin?
if (pipes[0]) {
@@ -259,7 +264,7 @@ pid_t xpopen_both(char **argv, int *pipes)
// Are we redirecting stdout?
if (pipes[1] != 1) {
dup2(pipes[1], 1);
- if (cestnepasun[2]) close(cestnepasun[2]);
+ close(pipes[1]);
}
}
if (argv) xexec(argv);