From aa349873624f2a5d4129c0ef8c33a1495d0b46fc Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 2 Apr 2019 21:48:36 -0500 Subject: Don't leak stdout pipe filehandle into children. --- lib/xwrap.c | 11 ++++++++--- 1 file 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); -- cgit v1.2.3