summaryrefslogtreecommitdiff
Diffstat
-rw-r--r--shell/ash.c11
-rw-r--r--shell/ash_test/ash-redir/redir_leak.right6
-rwxr-xr-xshell/ash_test/ash-redir/redir_leak.tests10
-rw-r--r--shell/hush_test/hush-redir/redir_leak.right6
-rwxr-xr-xshell/hush_test/hush-redir/redir_leak.tests10
5 files changed, 39 insertions, 4 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 7c53946..e6d02f6 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -5426,11 +5426,11 @@ redirect(union node *redir, int flags)
/* Careful to not accidentally "save"
* to the same fd as right side fd in N>&M */
int minfd = right_fd < 10 ? 10 : right_fd + 1;
+#if defined(F_DUPFD_CLOEXEC)
+ i = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
+#else
i = fcntl(fd, F_DUPFD, minfd);
-/* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds
- * are closed in popredir() in the child, preventing them from leaking
- * into child. (popredir() also cleans up the mess in case of failures)
- */
+#endif
if (i == -1) {
i = errno;
if (i != EBADF) {
@@ -5445,6 +5445,9 @@ redirect(union node *redir, int flags)
remember_to_close:
i = CLOSED;
} else { /* fd is open, save its copy */
+#if !defined(F_DUPFD_CLOEXEC)
+ fcntl(i, F_SETFD, FD_CLOEXEC);
+#endif
/* "exec fd>&-" should not close fds
* which point to script file(s).
* Force them to be restored afterwards */