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

8286444: javac errors after JDK-8251329 are not helpful enough to find root cause #8616

Closed
wants to merge 3 commits into from

Conversation

RealCLanger
Copy link
Contributor

@RealCLanger RealCLanger commented May 9, 2022

After https://bugs.openjdk.java.net/browse/JDK-8251329, javac throws errors when the classpath
contains jar files with . or .. in its name. The error message, however, does not help to find
the culprit. This could be improved.


Progress

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

Issue

  • JDK-8286444: javac errors after JDK-8251329 are not helpful enough to find root cause

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 8616

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

Using diff file

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

Improve error message in javac compilation when jar files with . or .. are in classpath
@bridgekeeper
Copy link

bridgekeeper bot commented May 9, 2022

👋 Welcome back clanger! 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 May 9, 2022
@openjdk
Copy link

openjdk bot commented May 9, 2022

@RealCLanger The following labels will be automatically applied to this pull request:

  • compiler
  • core-libs
  • nio

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

@openjdk openjdk bot added nio nio-dev@openjdk.org core-libs core-libs-dev@openjdk.org compiler compiler-dev@openjdk.org labels May 9, 2022
@mlbridge
Copy link

mlbridge bot commented May 9, 2022

Webrevs

@RealCLanger
Copy link
Contributor Author

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

Copy link
Contributor

@TheRealMDoerr TheRealMDoerr left a comment

Choose a reason for hiding this comment

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

LGTM. I think this is an important improvement. Developers should not be left alone figuring out which .jar file is responsible for the problem. There is currently no hint at all without this change.
(Note: Pre-submit test issues on 32 bit are unrelated.)

@openjdk
Copy link

openjdk bot commented May 10, 2022

@RealCLanger 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:

8286444: javac errors after JDK-8251329 are not helpful enough to find root cause

Reviewed-by: mdoerr

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 25 new commits pushed to the master branch:

  • cc7560e: 8286287: Reading file as UTF-16 causes Error which "shouldn't happen"
  • 82aa045: 8286015: JFR: Remove jfr.save.generated.asm
  • 1904e9d: 8286423: Destroy password protection in the example code in KeyStore
  • e4439ca: 8284283: javac crashes when several transitive supertypes are missing
  • 752ad1c: 8286422: Add OIDs for RC2 and Blowfish
  • 36bdd25: 8286573: Remove the unnecessary method Attr#attribTopLevel and its usage
  • dea6e88: 8284680: sun.font.FontConfigManager.getFontConfig() leaks charset
  • 40f43c6: 8286601: Mac Aarch: Excessive warnings to be ignored for build jdk
  • be97b4b: 8278348: [macos12] javax/swing/JTree/4908142/bug4908142.java fails in macos12
  • ff17f49: 8284888: [macos] javax/swing/JInternalFrame/8146321/JInternalFrameIconTest.java failed with "NimbusLookAndFeel] : ERROR: icon and imageIcon not same."
  • ... and 15 more: https://git.openjdk.java.net/jdk/compare/89de756ffbefac452c7df559e2a4eb50bf71368b...master

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 May 10, 2022
@AlanBateman
Copy link
Contributor

AlanBateman commented May 10, 2022

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

@LanceAndersen
Copy link
Contributor

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.

It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

@RealCLanger
Copy link
Contributor Author

RealCLanger commented May 10, 2022

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.

It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

@AlanBateman
Copy link
Contributor

I think this would be OK, but would get to get someone from our security team to bless it.

It's print the entry name, I don't think it is leaking the file path to the zip file.

@LanceAndersen
Copy link
Contributor

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.
It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

Already did ;-) so hopefully they will share their thoughts soon.

@LanceAndersen
Copy link
Contributor

I think this would be OK, but would get to get someone from our security team to bless it.

It's print the entry name, I don't think it is leaking the file path to the zip file.

I think you are probably right I am probably being overly cautious

@LanceAndersen
Copy link
Contributor

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.
It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

Already did ;-) so hopefully they will share their thoughts soon.

I think it would probably be good for a separate PR for the ZipFS change as it keeps it a bit clearer

@seanjmullan
Copy link
Member

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.
It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

Already did ;-) so hopefully they will share their thoughts soon.

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

@seanjmullan
Copy link
Member

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

The bug report also says "The error message does not give a clue which jar file is causing the problem" but the error message includes the name "invalid.jar" so I am also confused about that.

@AlanBateman
Copy link
Contributor

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

The bug report also says "The error message does not give a clue which jar file is causing the problem" but the error message includes the name "invalid.jar" so I am also confused about that.

There are two parts to it. In the case of initCEN method, the proposed change is to include the name of the rejected entry in the exception message. This is not the same thing as leaking a file path in the exception message so I don't think we have a concern here.

@seanjmullan
Copy link
Member

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

The bug report also says "The error message does not give a clue which jar file is causing the problem" but the error message includes the name "invalid.jar" so I am also confused about that.

There are two parts to it. In the case of initCEN method, the proposed change is to include the name of the rejected entry in the exception message. This is not the same thing as leaking a file path in the exception message so I don't think we have a concern here.

Ok, but @RealCLanger can you address the prior comments I had on the bug report? The error messages (before and after the fix) are the same.

@RealCLanger
Copy link
Contributor Author

/label remove nio

@RealCLanger
Copy link
Contributor Author

/label remove core-libs

@openjdk openjdk bot removed the nio nio-dev@openjdk.org label May 11, 2022
@openjdk
Copy link

openjdk bot commented May 11, 2022

@RealCLanger
The nio label was successfully removed.

@RealCLanger
Copy link
Contributor Author

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.
It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

Already did ;-) so hopefully they will share their thoughts soon.

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

Good catch, I pasted two times the error message after the proposed patch. Fixed.

@openjdk openjdk bot removed the core-libs core-libs-dev@openjdk.org label May 11, 2022
@openjdk
Copy link

openjdk bot commented May 11, 2022

@RealCLanger
The core-libs label was successfully removed.

@RealCLanger
Copy link
Contributor Author

@LanceAndersen @AlanBateman do you think adding the entry name in the exception in ZipFileSystem is ok? If so, should it maybe go into a different patch?

It should be okay as this is the name of an entry in the zip file. It might be a bit cleaner to add a method to IndexNode to return the name as String. Alternatively maybe its toString could be changed to drop the index (I would need to dig into the history to find out if there is really any use for the index in the String representation).

I think this would be OK, but would get to get someone from our security team to bless it.
It might not be a bad idea to add a method to return the name as a String. There are a couple of places where we do a new String(name) so would economize any future changes

Sounds fair. @LanceAndersen, can you please ask the security team about their ok then and let me know? In case their answer is a yes, I'll work on implementing the suggestion to return the name as String. Shall I maybe do the zipfs change in a different PR then? The more important change in the context of javac is printing out the jar name in javac itself.

Already did ;-) so hopefully they will share their thoughts soon.

I think it would probably be good for a separate PR for the ZipFS change as it keeps it a bit clearer

I've factored out the zipfs change into #8655. So this change only affects javac now.

@seanjmullan
Copy link
Member

It's probably ok, but the bug report is either incomplete or I am missing something. It says "This can be improved to something like: ..." but the same text as is emitted now is used. Can you fix this so I have a better example of what will be included in the message?

Good catch, I pasted two times the error message after the proposed patch. Fixed.

Thanks. This change and the other change in 8286594 to the exception message seems fine to me from a security perspective. However, I won't be adding my name as Reviewer as I have not reviewed the actual code.

Copy link
Contributor

@TheRealMDoerr TheRealMDoerr left a comment

Choose a reason for hiding this comment

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

Still good. This is the essential part. I'm fine with moving the rest into the separate enhancement.

@RealCLanger
Copy link
Contributor Author

Thanks for the reviews.
/integrate

@openjdk
Copy link

openjdk bot commented May 14, 2022

Going to push as commit 29c4b8e.
Since your change was applied there have been 56 commits pushed to the master branch:

  • 9eb15c9: 8286681: ShenandoahControlThread::request_gc misses the case of GCCause::_codecache_GC_threshold
  • e4378ab: 8286559: Re-examine synchronization of mark and reset methods on InflaterInputStream
  • f56396f: 6829250: Reg test: java/awt/Toolkit/ScreenInsetsTest/ScreenInsetsTest.java fails in Windows
  • 273c732: 8277493: [REDO] Quarantined jpackage apps are labeled as "damaged"
  • 583a61a: 8286671: (fc) Modify sun.nio.ch.FileChannelImpl.map0() to accept a FileDescriptor parameter
  • 1e843c3: 8286604: Update InputStream and OutputStream to use @implSpec
  • 78ffefb: 8286668: JFR: Cleanup
  • 76caeed: 8285366: Fix typos in serviceability
  • f631c98: 8285380: Fix typos in security
  • 237f280: 8286393: Address possibly lossy conversions in java.rmi
  • ... and 46 more: https://git.openjdk.java.net/jdk/compare/89de756ffbefac452c7df559e2a4eb50bf71368b...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 14, 2022
@openjdk openjdk bot closed this May 14, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 14, 2022
@RealCLanger RealCLanger deleted the jdk-8286444 branch May 14, 2022 10:56
@openjdk
Copy link

openjdk bot commented May 14, 2022

@RealCLanger Pushed as commit 29c4b8e.

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

try {
this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
} catch (ZipException ze) {
throw new IOException("ZipException opening \"" + archivePath + "\": " + ze.getMessage(), ze);
Copy link
Member

Choose a reason for hiding this comment

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

Hello Christoph, I'm sorry I'm a bit late to this discussion. One of the concerns raised in this PR is that we might end up leaking the file path of the jar being reported for error. The original example that you had in the JBS is something like this:

java -cp invalid.jar Hello.java

which after this change prints:

ZipException opening "invalid.jar": ZIP file can't be opened as a file system because an entry has a '.' or '..' element in its name

This is fine, IMO.

However, consider a slightly modified case. Let's consider a case where you have a different foo.jar in that current directory and that foo.jar has a Class-Path entry which looks like:

Manifest-Version: 1.0
Class-Path: invalid.jar
Created-By: 19-internal (N/A)

So foo.jar is pointing to invalid.jar. Now let's run the following command by passing foo.jar as -cp value (which then brings in invalid.jar through the Class-Path entry in the Manifest file):

java -cp foo.jar Hello.java

With this PR, this now returns:

ZipException opening "/private/tmp/8286444/invalid.jar": ZIP file can't be opened as a file system because an entry has a '.' or '..' element in its name

Notice that it has ended up leaking the file path of the jar that was pulled in through the MANIFEST.MF file. I think this is something that isn't allowed from a security point of view.

Perhaps an additional change needs to be done on top of this PR to prevent this, something like:

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
index 1336fb74916..7c40afb62af 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JavacFileManager.java
@@ -564,7 +564,7 @@ public class JavacFileManager extends BaseFileManager implements StandardJavaFil
                 try {
                     this.fileSystem = jarFSProvider.newFileSystem(archivePath, env);
                 } catch (ZipException ze) {
-                    throw new IOException("ZipException opening \"" + archivePath + "\": " + ze.getMessage(), ze);
+                    throw new IOException("ZipException opening \"" + archivePath.getFileName() + "\": " + ze.getMessage(), ze);
                 }
             } else {
                 this.fileSystem = FileSystems.newFileSystem(archivePath, (ClassLoader)null);

This then just prints the name of the jar in both cases:

java -cp invalid.jar Hello.java

ZipException opening "invalid.jar": ZIP file can't be opened as a file system because an entry has a '.' or '..' element in its name
java -cp foo.jar Hello.java

ZipException opening "invalid.jar": ZIP file can't be opened as a file system because an entry has a '.' or '..' element in its name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Jai,

this is a good point. Regarding the potential leaking of the jar path in the manifest case pointed out by you, I would think that it is not problematic as the exception occurs in a (developer-) tool and there it might even be useful. However, for consistency purposes, it might be clearer to just print the jar name. I guess that's already helpful enough.

I filed https://bugs.openjdk.java.net/browse/JDK-8286855 to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org integrated Pull request has been integrated
6 participants