aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-07 10:15:01 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-07 10:16:56 +0100
commit86584e134eec1a81298149f8c04c77727f6dccb9 (patch)
tree6e3323d82c8dc1c2c7da58fa86573ca1cea33e71
parentfe93624fb69e3858ebaea2d9e53e0522c1025698 (diff)
downloadbusybox-w32-86584e134eec1a81298149f8c04c77727f6dccb9.tar.gz
busybox-w32-86584e134eec1a81298149f8c04c77727f6dccb9.tar.bz2
busybox-w32-86584e134eec1a81298149f8c04c77727f6dccb9.zip
ash: fix open fds leaking in redirects. Closes 9561
commit e19923f6652a638ac39c84012e97f52cf5a7568e deleted clearredir() call in shellexec(): ash: [REDIR] Remove redundant CLOEXEC calls Upstream commit: Now that we're marking file descriptors as CLOEXEC in savefd, we no longer need to close them on exec or in setinputfd. but it missed one place where we don't set CLOEXEC. Fixing this. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-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 aee3d419c..efb4615db 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -5433,11 +5433,11 @@ redirect(union node *redir, int flags)
5433 /* Careful to not accidentally "save" 5433 /* Careful to not accidentally "save"
5434 * to the same fd as right side fd in N>&M */ 5434 * to the same fd as right side fd in N>&M */
5435 int minfd = right_fd < 10 ? 10 : right_fd + 1; 5435 int minfd = right_fd < 10 ? 10 : right_fd + 1;
5436#if defined(F_DUPFD_CLOEXEC)
5437 i = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
5438#else
5436 i = fcntl(fd, F_DUPFD, minfd); 5439 i = fcntl(fd, F_DUPFD, minfd);
5437/* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds 5440#endif
5438 * are closed in popredir() in the child, preventing them from leaking
5439 * into child. (popredir() also cleans up the mess in case of failures)
5440 */
5441 if (i == -1) { 5441 if (i == -1) {
5442 i = errno; 5442 i = errno;
5443 if (i != EBADF) { 5443 if (i != EBADF) {
@@ -5452,6 +5452,9 @@ redirect(union node *redir, int flags)
5452 remember_to_close: 5452 remember_to_close:
5453 i = CLOSED; 5453 i = CLOSED;
5454 } else { /* fd is open, save its copy */ 5454 } else { /* fd is open, save its copy */
5455#if !defined(F_DUPFD_CLOEXEC)
5456 fcntl(i, F_SETFD, FD_CLOEXEC);
5457#endif
5455 /* "exec fd>&-" should not close fds 5458 /* "exec fd>&-" should not close fds
5456 * which point to script file(s). 5459 * which point to script file(s).
5457 * Force them to be restored afterwards */ 5460 * Force them to be restored afterwards */
diff --git a/shell/ash_test/ash-redir/redir_leak.right b/shell/ash_test/ash-redir/redir_leak.right
new file mode 100644
index 000000000..b1c48292b
--- /dev/null
+++ b/shell/ash_test/ash-redir/redir_leak.right
@@ -0,0 +1,6 @@
14
24
34
44
54
64
diff --git a/shell/ash_test/ash-redir/redir_leak.tests b/shell/ash_test/ash-redir/redir_leak.tests
new file mode 100755
index 000000000..c8a9c6343
--- /dev/null
+++ b/shell/ash_test/ash-redir/redir_leak.tests
@@ -0,0 +1,10 @@
1# Each of these should show only four lines:
2# fds 0,1,2 are stdio; fd 3 is open by opendir() in ls.
3# This test detects bugs where redirects leave stray open fds.
4
5ls -1 /proc/self/fd | wc -l
6ls -1 /proc/self/fd >/proc/self/fd/1 | wc -l
7ls -1 /proc/self/fd >/proc/self/fd/1 2>&1 | wc -l
8echo "`ls -1 /proc/self/fd `" | wc -l
9echo "`ls -1 /proc/self/fd >/proc/self/fd/1 `" | wc -l
10echo "`ls -1 /proc/self/fd >/proc/self/fd/1 2>&1 `" | wc -l
diff --git a/shell/hush_test/hush-redir/redir_leak.right b/shell/hush_test/hush-redir/redir_leak.right
new file mode 100644
index 000000000..b1c48292b
--- /dev/null
+++ b/shell/hush_test/hush-redir/redir_leak.right
@@ -0,0 +1,6 @@
14
24
34
44
54
64
diff --git a/shell/hush_test/hush-redir/redir_leak.tests b/shell/hush_test/hush-redir/redir_leak.tests
new file mode 100755
index 000000000..c8a9c6343
--- /dev/null
+++ b/shell/hush_test/hush-redir/redir_leak.tests
@@ -0,0 +1,10 @@
1# Each of these should show only four lines:
2# fds 0,1,2 are stdio; fd 3 is open by opendir() in ls.
3# This test detects bugs where redirects leave stray open fds.
4
5ls -1 /proc/self/fd | wc -l
6ls -1 /proc/self/fd >/proc/self/fd/1 | wc -l
7ls -1 /proc/self/fd >/proc/self/fd/1 2>&1 | wc -l
8echo "`ls -1 /proc/self/fd `" | wc -l
9echo "`ls -1 /proc/self/fd >/proc/self/fd/1 `" | wc -l
10echo "`ls -1 /proc/self/fd >/proc/self/fd/1 2>&1 `" | wc -l