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

8306882: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link #15397

Closed
wants to merge 5 commits into from

Conversation

bplb
Copy link
Member

@bplb bplb commented Aug 23, 2023

Modify Path.toRealPath such that it does not collapse links such as "link/.." or "link/../.." when LinkOption.NOFOLLOW_LINKS is specified or, in some cases, not.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8306882: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15397/head:pull/15397
$ git checkout pull/15397

Update a local copy of the PR:
$ git checkout pull/15397
$ git pull https://git.openjdk.org/jdk.git pull/15397/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15397

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15397.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 23, 2023

👋 Welcome back bpb! 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 23, 2023
@openjdk
Copy link

openjdk bot commented Aug 23, 2023

@bplb The following label will be automatically applied to this pull request:

  • nio

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the nio nio-dev@openjdk.org label Aug 23, 2023
@bplb
Copy link
Member Author

bplb commented Aug 23, 2023

On Windows, the function GetFullPathName() does not access the file system and so is susceptible to collapsing links resulting in a path to a non-existent file. The path is examined to see whether it is one for which GetFullPathName would generate an incorrect result. If that is not the case, then GetFullPathName is used as before and there is no effective change. So this modification should affect only cases which would otherwise generate an incorrect result or fail.

@mlbridge
Copy link

mlbridge bot commented Aug 23, 2023

Webrevs

// to determine whether this would occur, and if so use the
// full path manually derived in the process.
String fullpath = null;
if (path.contains("..")) {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to extract code for this case to a separate method. sun.nio.fs.WindowsLinkSupport#getRealPath is already too big.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds like a good idea.

@AlanBateman
Copy link
Contributor

The changes to the Windows implementation of toRealPath(LinkOption.NOFOLLOW_LINKS) are going to take time to review.

Once suggestion that would help set the groundwork is to move the toRealPath tests to their own JUnit test. It's just historically that they were initially put in Misc.java. Also I think it would be useful to do the change to the Unix implementation separately, I think that needs a summary in the first place to understand why that change is needed.

@bplb
Copy link
Member Author

bplb commented Aug 28, 2023

Also I think it would be useful to do the change to the Unix implementation separately, I think that needs a summary in the first place to understand why that change is needed.

The issue was filed against the Unix implementation, so if the Unix implementation is separated from Windows then I think the Windows content should be moved to a new PR.

@bplb
Copy link
Member Author

bplb commented Aug 29, 2023

Once suggestion that would help set the groundwork is to move the toRealPath tests to their own JUnit test.

See JDK-8315241.

@bplb
Copy link
Member Author

bplb commented Aug 29, 2023

I think that [the Unix implementation] needs a summary in the first place to understand why that change is needed.

I can add a comment in the source, but here's some illustrative behavior using jshell. It uses some files as:

./dir/subdir
./aaa@ -> dir/subdir
./out.txt

and attempts to convert aaa/../../out.txt to a real path.

master

jshell> Path p = Path.of("aaa/../../out.txt")
p ==> aaa/../../out.txt

jshell> p.toRealPath()
$2 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/out.txt                                        

jshell> p.toRealPath(LinkOption.NOFOLLOW_LINKS)
|  Exception java.nio.file.NoSuchFileException: /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/aaa/out.txt
|        at UnixException.translateToIOException (UnixException.java:92)
|        at UnixException.rethrowAsIOException (UnixException.java:106)
|        at UnixException.rethrowAsIOException (UnixException.java:111)
|        at UnixPath.toRealPath (UnixPath.java:927)
|        at (#3:1)

As can be seen, when attempting to resolve the path without following links, the result is a non-existent file.

patch

jshell> Path p = Path.of("aaa/../../out.txt")
p ==> aaa/../../out.txt

jshell> p.toRealPath()
$2 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/out.txt

jshell> p.toRealPath(LinkOption.NOFOLLOW_LINKS)
$3 ==> /Users/bpb/dev/bugs/jdk/Path-toRealPath-8306882/aaa/../../out.txt

Here the correct result is obtained.

@bplb
Copy link
Member Author

bplb commented Aug 29, 2023

The test and Windows changes reverted by 75db499 will be addressed by merging and modifying the patch #15474 and by a separate issue to be filed, respectively.

Copy link
Contributor

@AlanBateman AlanBateman left a comment

Choose a reason for hiding this comment

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

I think looks okay, and I assume the work on converting the test to a standalone JUnit test will expand the test coverage to include this scenario.

@openjdk
Copy link

openjdk bot commented Aug 30, 2023

@bplb This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8306882: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link

Reviewed-by: alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 1 new commit pushed to the master branch:

  • 89d18ea: 8312018: Improve reservation of class space and CDS

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch.
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 30, 2023
@bplb
Copy link
Member Author

bplb commented Aug 30, 2023

[...] I assume the work on converting the test to a standalone JUnit test will expand the test coverage to include this scenario.

It will, and the test will initially have to be excluded on Windows until JDK-8315273 is fixed.

@bplb bplb requested a review from AlanBateman August 30, 2023 20:10
@bplb
Copy link
Member Author

bplb commented Aug 31, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Aug 31, 2023

Going to push as commit 63f561f.
Since your change was applied there have been 20 commits pushed to the master branch:

  • 2436fb0: 8312306: Add more Reference.reachabilityFence() calls to the security classes using Cleaner
  • 351c31e: 8315378: [BACKOUT] runtime/NMT/SummarySanityCheck.java failed with "Total committed (MMMMMM) did not match the summarized committed (NNNNNN)"
  • c12ca88: 8312521: Unused field LocaleProviderAdapter#defaultLocaleProviderAdapter could be removed
  • b38bcae: 8313656: assert(!JvmtiExport::can_support_virtual_threads()) with -XX:-DoJVMTIVirtualThreadTransitions
  • c8acab1: 8315413: Remove special filtering of Continuation.yield0 in StackWalker
  • ea5aa61: 8315383: jlink SystemModulesPlugin incorrectly parses the options
  • 29ff1e4: 8315445: 8314748 causes crashes in x64 builds
  • 145d8bc: 8315051: jdk/jfr/jvm/TestGetEventWriter.java fails with non-JVMCI GCs
  • 486fa08: 8313873: java/nio/channels/DatagramChannel/SendReceiveMaxSize.java fails on AIX due to small default RCVBUF size and different IPv6 Header interpretation
  • b0353ad: 8315242: G1: Fix -Wconversion warnings around GCDrainStackTargetSize
  • ... and 10 more: https://git.openjdk.org/jdk/compare/dd64a4a483a831fd66912491af10502c4cf8596b...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 31, 2023
@openjdk openjdk bot closed this Aug 31, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 31, 2023
@openjdk
Copy link

openjdk bot commented Aug 31, 2023

@bplb Pushed as commit 63f561f.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@bplb bplb deleted the Path-toRealPath-8306882 branch September 22, 2023 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated nio nio-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

3 participants