Skip to content

Prevent file descriptor leaks across exec#48

Open
guillerodriguez wants to merge 2 commits into
masterfrom
fix/process-exec-close-unused-fds-v2
Open

Prevent file descriptor leaks across exec#48
guillerodriguez wants to merge 2 commits into
masterfrom
fix/process-exec-close-unused-fds-v2

Conversation

@guillerodriguez

Copy link
Copy Markdown
Contributor

When spawning native processes, cpproc_forkAndExec() only closes the file descriptors created for the current spawn, allowing other JVM descriptors not marked FD_CLOEXEC to leak into the executed process. This can keep unrelated files, sockets and process pipes open unexpectedly.

Fix by marking all non-standard descriptors close-on-exec in the forked child just before exec(). Several strategies are implemented:

  • Prefer close_range(CLOSE_RANGE_CLOEXEC) if available
  • Enumerate open fds by walking the platform-specific process fd directory
  • Fall back to a bounded fcntl scan. The scan limit is computed in the parent to avoid calling sysconf() in the child, and is capped at 65536 if the limit is unknown or too high.

The exec-failure pipe is covered by the sweep, so its explicit FD_CLOEXEC fcntl becomes redundant and is dropped.

Fixes #46 (BZ#126300)
Fixes #47 (BZ#31384)

When spawning native processes, cpproc_forkAndExec() only closes
the file descriptors created for the current spawn, allowing other
JVM descriptors not marked FD_CLOEXEC to leak into the executed
process. This can keep unrelated files, sockets and process pipes
open unexpectedly.

Fix by marking all non-standard descriptors close-on-exec in the
forked child just before exec(). Several strategies are implemented:

- Prefer close_range(CLOSE_RANGE_CLOEXEC) if available
- Enumerate open fds by walking the platform-specific process fd
  directory
- Fall back to a bounded fcntl scan. The scan limit is computed in
  the parent to avoid calling sysconf() in the child, and is capped
  at 65536 if the limit is unknown or too high.

The exec-failure pipe is covered by the sweep, so its explicit
FD_CLOEXEC fcntl becomes redundant and is dropped.

Fixes #46 (BZ#126300)
Fixes #47 (BZ#31384)

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
@guillerodriguez
guillerodriguez requested a review from phvega July 17, 2026 10:47
Clarify why setting FD_CLOEXEC on the directory stream fd is safe.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
@guillerodriguez
guillerodriguez force-pushed the fix/process-exec-close-unused-fds-v2 branch from ab60354 to 5449de4 Compare July 20, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition between setting close-on-exec and Runtime.exec() File descriptors leak into child processes

1 participant