Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8271858: Handle to jimage file inherited into child processes (posix) #4991

Conversation

tstuefe
Copy link
Member

@tstuefe tstuefe commented Aug 4, 2021

We should not leak the handle to the jimage file (lib/modules) to childs.

JDK-8194734 did solve this for windows. We should use FD_CLOEXEC on Posix as well.

Note that this only poses a problem when a child process is spawned off not via Runtime.exec but via another route since the spawnhelper closes all file descriptors.


test:

manually verified that lib/modules now has the FD_CLOEXEC flag set.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8271858: Handle to jimage file inherited into child processes (posix)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4991/head:pull/4991
$ git checkout pull/4991

Update a local copy of the PR:
$ git checkout pull/4991
$ git pull https://git.openjdk.java.net/jdk pull/4991/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4991

View PR using the GUI difftool:
$ git pr show -t 4991

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4991.diff

Sorry, something went wrong.

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 4, 2021

👋 Welcome back stuefe! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 4, 2021
@openjdk
Copy link

openjdk bot commented Aug 4, 2021

@tstuefe To determine the appropriate audience for reviewing this pull request, one or more labels corresponding to different subsystems will normally be applied automatically. However, no automatic labelling rule matches the changes in this pull request. In order to have an "RFR" email sent to the correct mailing list, you will need to add one or more applicable labels manually using the /label pull request command.

Applicable Labels
  • 2d
  • awt
  • beans
  • build
  • compiler
  • core-libs
  • hotspot
  • hotspot-compiler
  • hotspot-gc
  • hotspot-jfr
  • hotspot-runtime
  • i18n
  • ide-support
  • javadoc
  • jdk
  • jmx
  • kulla
  • net
  • nio
  • security
  • serviceability
  • shenandoah
  • sound
  • swing

@@ -38,7 +38,7 @@
* Return the file descriptor.
*/
jint osSupport::openReadOnly(const char *path) {
return ::open(path, 0);
return ::open(path, O_CLOEXEC);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay but I think it would be useful to know why this one place needs O_CLOEXEC and not others.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably others too, if we care about inheriting read handles to child.

The background is that I am playing with a new test which checks that the VM has no open inheritable file descriptors. It is supposed to replace some specialized tests which are maintenance-intensive. I am still tuning the test, since at the moment it turns out too many false positives.

Anyway, this is the very first descriptor the VM opens, therefore it triggered my test. I thought since there is a sibling fix for windows, a similar fix for posix would be symmetric.

If you think this is not worth fixing, or we should fix all occurrences in one swoop, that is fine too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Unix systems, the JDK has always relied on the Runtime.exec implementation to close the file descriptors. On Windows the inheritance has to be disabled in the parent. So if the gtest launcher is forking directly then we may have a bit of whack-a-mole to change all the places that open file descriptors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was born since we have jtreg tests that assert that certain VM internal fds, namely of log files, are not handed down to child processes. The motivation originally was Windows, since child processes would block that file from being moved. The test is done for both Unix and Windows, however. It is fragile and difficult to analyze when it fails. I wanted to throw away the Unix portion of that test and replace it with a simple gtest, either checking CLOEXEC for just that particular fd, or (my current approach) for all handles.

But if what you are saying is how we do things - we don't bother with CLOEXEC, just rely on Runtime.exec() to close all fds, and accept the fact that "foreign" forks will cause us problems - then I could just throw the Unix portion of that test away without replacing it with anything.

ATM the libs/module fd seems to be the only file descriptor tripping up my test though. Maybe it's not so bad. I am mainly afraid of situations where the gtestlauncher runs in some instrumented form, maybe with a virus scanner in its belly, with foreign code opening fds without our knowledge. I am still unsure about which direction to go.

Copy link
Contributor

@AlanBateman AlanBateman Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/modules is opened/mapped early in the startup so I assume this is why this one shows up quickly. Adding O_CLOEXEC everywhere is okay, up you if you want to do that or switch it to Runtime.exec.

@AlanBateman
Copy link
Contributor

/label add core-libs

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Aug 5, 2021
@openjdk
Copy link

openjdk bot commented Aug 5, 2021

@AlanBateman
The core-libs label was successfully added.

@mlbridge
Copy link

mlbridge bot commented Aug 5, 2021

Webrevs

@tstuefe
Copy link
Member Author

tstuefe commented Aug 11, 2021

I withdraw the PR and close the issue as won't fix since the issue is very unlikely to cause real problems (only in the event of someone raw-forking, bypassing Runtime.exec()). I'll find a narrower solution to simplify the test.

@tstuefe tstuefe closed this Aug 11, 2021
@tstuefe tstuefe deleted the JDK-8271858-Handle-to-jimage-file-inherited-into-child-processes-posix branch August 23, 2021 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

None yet

2 participants