From da84f235ef985883c79066204ee838717aacd41a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sun, 16 Jun 2019 09:01:54 +0000 Subject: child_spawn*: make sure posix_spawn() doesn't clobber pipes with dup2+close --- src/libstddjb/child_spawn.c | 4 ++-- src/libstddjb/child_spawn1_internal.c | 11 +++++++---- src/libstddjb/child_spawn2.c | 22 ++++++++++++++-------- src/libstddjb/child_spawn3.c | 22 ++++++++++++++-------- 4 files changed, 37 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/libstddjb/child_spawn.c b/src/libstddjb/child_spawn.c index 964e4e2..58bbfc8 100644 --- a/src/libstddjb/child_spawn.c +++ b/src/libstddjb/child_spawn.c @@ -77,14 +77,14 @@ pid_t child_spawn (char const *prog, char const *const *argv, char const *const } e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - if (n >= 2) + if (n >= 2 && p[1][0]) { e = posix_spawn_file_actions_adddup2(&actions, p[1][0], 0) ; if (e) goto erractions ; e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; if (e) goto erractions ; } - if (n) + if (n && p[0][1] != 1) { e = posix_spawn_file_actions_adddup2(&actions, p[0][1], 1) ; if (e) goto erractions ; diff --git a/src/libstddjb/child_spawn1_internal.c b/src/libstddjb/child_spawn1_internal.c index 0f9cb16..116a65e 100644 --- a/src/libstddjb/child_spawn1_internal.c +++ b/src/libstddjb/child_spawn1_internal.c @@ -32,10 +32,13 @@ pid_t child_spawn1_internal (char const *prog, char const *const *argv, char con } e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - e = posix_spawn_file_actions_adddup2(&actions, p[to & 1], to & 1) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ; - if (e) goto erractions ; + if (p[to & 1] != to & 1) + { + e = posix_spawn_file_actions_adddup2(&actions, p[to & 1], to & 1) ; + if (e) goto erractions ; + e = posix_spawn_file_actions_addclose(&actions, p[to & 1]) ; + if (e) goto erractions ; + } if (to & 2) { e = posix_spawn_file_actions_adddup2(&actions, to & 1, !(to & 1)) ; diff --git a/src/libstddjb/child_spawn2.c b/src/libstddjb/child_spawn2.c index d69764d..4e7d0a5 100644 --- a/src/libstddjb/child_spawn2.c +++ b/src/libstddjb/child_spawn2.c @@ -56,14 +56,20 @@ pid_t child_spawn2 (char const *prog, char const *const *argv, char const *const } e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; + if (p[1][0] != fds[0]) + { + e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; + if (e) goto erractions ; + e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; + if (e) goto erractions ; + } + if (p[0][1] != fds[1]) + { + e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; + if (e) goto erractions ; + e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; + if (e) goto erractions ; + } { int haspath = !!getenv("PATH") ; if (!haspath && (setenv("PATH", SKALIBS_DEFAULTPATH, 0) < 0)) diff --git a/src/libstddjb/child_spawn3.c b/src/libstddjb/child_spawn3.c index 82220ea..9dbed41 100644 --- a/src/libstddjb/child_spawn3.c +++ b/src/libstddjb/child_spawn3.c @@ -62,14 +62,20 @@ pid_t child_spawn3 (char const *prog, char const *const *argv, char const *const } e = posix_spawn_file_actions_init(&actions) ; if (e) goto errattr ; - e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; - if (e) goto erractions ; - e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; - if (e) goto erractions ; + if (p[1][0] != fds[0]) + { + e = posix_spawn_file_actions_adddup2(&actions, p[1][0], fds[0]) ; + if (e) goto erractions ; + e = posix_spawn_file_actions_addclose(&actions, p[1][0]) ; + if (e) goto erractions ; + } + if (p[0][1] != fds[1]) + { + e = posix_spawn_file_actions_adddup2(&actions, p[0][1], fds[1]) ; + if (e) goto erractions ; + e = posix_spawn_file_actions_addclose(&actions, p[0][1]) ; + if (e) goto erractions ; + } { int haspath = !!getenv("PATH") ; size_t envlen = env_len(envp) ; -- cgit v1.2.3