Skip to content

Commit afa52e4

Browse files
committed
8210549: Runtime.exec: in closeDescriptors(), use FD_CLOEXEC instead of close()
Reviewed-by: rriggs
1 parent 164cae4 commit afa52e4

File tree

5 files changed

+197
-23
lines changed

5 files changed

+197
-23
lines changed

make/test/JtregNativeJdk.gmk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ BUILD_JDK_JTREG_LIBRARIES_JDK_LIBS_libGetXSpace := java.base:libjava
6262
ifeq ($(call isTargetOs, windows), true)
6363
BUILD_JDK_JTREG_EXCLUDE += libDirectIO.c libInheritedChannel.c \
6464
libExplicitAttach.c libImplicitAttach.c \
65-
exelauncher.c
65+
exelauncher.c libFDLeaker.c exeFDLeakTester.c
6666

6767
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeNullCallerTest := $(LIBCXX)
6868
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exerevokeall := advapi32.lib

src/java.base/unix/native/libjava/childproc.c

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ closeSafely(int fd)
5252
return (fd == -1) ? 0 : close(fd);
5353
}
5454

55+
int
56+
markCloseOnExec(int fd)
57+
{
58+
const int flags = fcntl(fd, F_GETFD);
59+
if (flags < 0) {
60+
return -1;
61+
}
62+
if ((flags & FD_CLOEXEC) == 0) {
63+
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
64+
return -1;
65+
}
66+
}
67+
return 0;
68+
}
69+
5570
static int
5671
isAsciiDigit(char c)
5772
{
@@ -68,21 +83,15 @@ isAsciiDigit(char c)
6883
#endif
6984

7085
static int
71-
closeDescriptors(void)
86+
markDescriptorsCloseOnExec(void)
7287
{
7388
DIR *dp;
7489
struct dirent *dirp;
75-
int from_fd = FAIL_FILENO + 1;
76-
77-
/* We're trying to close all file descriptors, but opendir() might
78-
* itself be implemented using a file descriptor, and we certainly
79-
* don't want to close that while it's in use. We assume that if
80-
* opendir() is implemented using a file descriptor, then it uses
81-
* the lowest numbered file descriptor, just like open(). So we
82-
* close a couple explicitly. */
83-
84-
close(from_fd); /* for possible use by opendir() */
85-
close(from_fd + 1); /* another one for good luck */
90+
/* This function marks all file descriptors beyond stderr as CLOEXEC.
91+
* That includes the file descriptor used for the fail pipe: we want that
92+
* one to stay open up until the execve, but it should be closed with the
93+
* execve. */
94+
const int fd_from = STDERR_FILENO + 1;
8695

8796
#if defined(_AIX)
8897
/* AIX does not understand '/proc/self' - it requires the real process ID */
@@ -91,18 +100,22 @@ closeDescriptors(void)
91100
#endif
92101

93102
if ((dp = opendir(FD_DIR)) == NULL)
94-
return 0;
103+
return -1;
95104

96105
while ((dirp = readdir(dp)) != NULL) {
97106
int fd;
98107
if (isAsciiDigit(dirp->d_name[0]) &&
99-
(fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
100-
close(fd);
108+
(fd = strtol(dirp->d_name, NULL, 10)) >= fd_from) {
109+
if (markCloseOnExec(fd) == -1) {
110+
closedir(dp);
111+
return -1;
112+
}
113+
}
101114
}
102115

103116
closedir(dp);
104117

105-
return 1;
118+
return 0;
106119
}
107120

108121
static int
@@ -394,11 +407,11 @@ childProcess(void *arg)
394407
fail_pipe_fd = FAIL_FILENO;
395408

396409
/* close everything */
397-
if (closeDescriptors() == 0) { /* failed, close the old way */
410+
if (markDescriptorsCloseOnExec() == -1) { /* failed, close the old way */
398411
int max_fd = (int)sysconf(_SC_OPEN_MAX);
399412
int fd;
400-
for (fd = FAIL_FILENO + 1; fd < max_fd; fd++)
401-
if (close(fd) == -1 && errno != EBADF)
413+
for (fd = STDERR_FILENO + 1; fd < max_fd; fd++)
414+
if (markCloseOnExec(fd) == -1 && errno != EBADF)
402415
goto WhyCantJohnnyExec;
403416
}
404417

@@ -413,9 +426,6 @@ childProcess(void *arg)
413426
sigprocmask(SIG_SETMASK, &unblock_signals, NULL);
414427
}
415428

416-
if (fcntl(FAIL_FILENO, F_SETFD, FD_CLOEXEC) == -1)
417-
goto WhyCantJohnnyExec;
418-
419429
JDK_execvpe(p->mode, p->argv[0], p->argv, p->envv);
420430

421431
WhyCantJohnnyExec:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
25+
/**
26+
* @test id=posix_spawn
27+
* @summary Check that we don't leak FDs
28+
* @requires os.family != "windows"
29+
* @library /test/lib
30+
* @run main/othervm/native -Djdk.lang.Process.launchMechanism=posix_spawn -agentlib:FDLeaker FDLeakTest
31+
*/
32+
33+
/**
34+
* @test id=fork
35+
* @summary Check that we don't leak FDs
36+
* @requires os.family != "windows"
37+
* @library /test/lib
38+
* @run main/othervm/native -Djdk.lang.Process.launchMechanism=fork -agentlib:FDLeaker FDLeakTest
39+
*/
40+
41+
/**
42+
* @test id=vfork
43+
* @summary Check that we don't leak FDs
44+
* @requires os.family == "linux"
45+
* @library /test/lib
46+
* @run main/othervm/native -Djdk.lang.Process.launchMechanism=vfork -agentlib:FDLeaker FDLeakTest
47+
*/
48+
49+
import jdk.test.lib.process.ProcessTools;
50+
public class FDLeakTest {
51+
// This test has two native parts:
52+
// - a library invoked with -agentlib that ensures that, in the parent JVM, we open
53+
// a native fd without setting FD_CLOEXEC (libFDLeaker.c). This is necessary because
54+
// there is no way to do this from Java: if Java functions correctly, all files the
55+
// user could open via its APIs should be marked with FD_CLOEXEC.
56+
// - a small native executable that tests - without using /proc - whether any file
57+
// descriptors other than stdin/out/err are open.
58+
//
59+
// What should happen: In the child process, between the initial fork and the exec of
60+
// the target binary, we should close all filedescriptors that are not stdin/out/err.
61+
// If that works, the child process should not see any other file descriptors save
62+
// those three.
63+
public static void main(String[] args) throws Exception {
64+
ProcessBuilder pb = ProcessTools.createNativeTestProcessBuilder("FDLeakTester");
65+
pb.inheritIO();
66+
Process p = pb.start();
67+
p.waitFor();
68+
if (p.exitValue() != 0) {
69+
throw new RuntimeException("Failed");
70+
}
71+
}
72+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include <errno.h>
25+
#include <fcntl.h>
26+
#include <stdio.h>
27+
#include <string.h>
28+
#include <unistd.h>
29+
30+
/* Check if any fd past stderr is valid; if true, print warning on stderr and return -1
31+
*
32+
* Note: check without accessing /proc since:
33+
* - non-portable
34+
* - may cause creation of temporary file descriptors
35+
*/
36+
int main(int argc, char** argv) {
37+
int errors = 0;
38+
int rc = 0;
39+
char buf[128];
40+
int max_fd = (int)sysconf(_SC_OPEN_MAX);
41+
if (max_fd == -1) {
42+
snprintf(buf, sizeof(buf), "*** sysconf(_SC_OPEN_MAX) failed? (%d) ***\n", errno);
43+
rc = write(2, buf, strlen(buf));
44+
max_fd = 10000;
45+
}
46+
// We start after stderr fd
47+
for (int fd = 3; fd < max_fd; fd++) {
48+
if (fcntl(fd, F_GETFD, 0) >= 0) {
49+
// Error: found valid file descriptor
50+
errors++;
51+
snprintf(buf, sizeof(buf), "*** Parent leaked file descriptor %d ***\n", fd);
52+
rc = write(2, buf, strlen(buf));
53+
}
54+
}
55+
return errors > 0 ? -1 : 0;
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include <stdio.h>
25+
#include "jvmti.h"
26+
27+
JNIEXPORT jint JNICALL
28+
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
29+
const char* filename = "./testfile_FDLeaker.txt";
30+
FILE* f = fopen(filename, "w");
31+
if (f == NULL) {
32+
return JNI_ERR;
33+
}
34+
printf("Opened and leaked %s (%d)", filename, fileno(f));
35+
return JNI_OK;
36+
}

0 commit comments

Comments
 (0)