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

Fix: Fix possible Null Pointer Exception #406

Merged
merged 5 commits into from
Apr 20, 2023

Conversation

arthurscchan
Copy link
Collaborator

Summary

In PublicKeyFactory Line 108, SubjectPublicKeyInfo.getInstance method could return null and make PublicKeyFactory Line 148 throws a NullPointerException.

From InputStream javadoc, we see that if the end of the stream is reached, it will return -1. Which means that if an empty byte array is provided, the tag will be -1 and it will return null in ASN1InputStream. In this case, it will trigger the null pointer exception above. Thus this fix adds an additional checking to ensure the PEM content is not an empty array.

Fixes: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=57247

Release Note

Documentation

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@loosebazooka loosebazooka added the safe to test conformance testing label label Mar 30, 2023
loosebazooka
loosebazooka previously approved these changes Mar 30, 2023
Copy link
Collaborator

@vlsi vlsi 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 the verification should be performed by the downstream library, and adding 0 check here is wrong

@vlsi
Copy link
Collaborator

vlsi commented Mar 30, 2023

The PR title says NullPointerException while the fix checks for empty array. It does not sound aligned

@loosebazooka
Copy link
Member

@vlsi, I believe there is a sufficient description to describe the issue. The title is aligned with the problem.

@vlsi
Copy link
Collaborator

vlsi commented Mar 30, 2023

@loosebazooka , we should not add arbitrary checks. Is there anything in ASN1Sequence.getInstance that says “empty byte array must not be passed there”?
If empty array triggers issues, it should be documented and tested at bc-java level, and we should not add those checks at random in sigstore-java.

Then, I do not like to merge changes without corresponding tests.
The PR add no tests, so it is possible that someone would remove the “useless” check tomorrow in a different PR.

@loosebazooka
Copy link
Member

loosebazooka commented Mar 30, 2023

I mean I agree here, this is a bouncy castle issue, but it still causes crashes in our code.
I believe oss-fuzz caches issues that cause crashes and you can repeat those against the proejct (is this right @arthurscchan ?) So if these were removed, oss-fuzz would catch it. (but yeah tests could be added to the relevant test classes to track these)

@vlsi
Copy link
Collaborator

vlsi commented Mar 30, 2023

If it is a bouncy castle issue, it should be fixed and tested at bouncy castle side, so everybody wins from the improvement.

So if these were removed, oss-fuzz would catch it.

oss-fuzz is non-deterministic, so it is not guaranteed to catch it. The cases identified by oss-fuzz should be baked into reproducible tests to prevent regressions.

@arthurscchan
Copy link
Collaborator Author

@loosebazooka Indeed, this could be reproducible.

@loosebazooka
Copy link
Member

@arthurscchan can you elaborate or point to the docs on how reproducing works for ossfuzz, it might also make sense to just bake these test cases into our testsuite anyway.

@arthurscchan
Copy link
Collaborator Author

arthurscchan commented Mar 31, 2023

In general, the oss-fuzz tool provides function to reproduce testcase, see https://google.github.io/oss-fuzz/advanced-topics/reproducing/#reproducing-bugs
The testcase that cause the problem could find in the bug page.

@DavidKorczynski
Copy link
Contributor

DavidKorczynski commented Apr 4, 2023

In general, the oss-fuzz tool provides function to reproduce testcase, see https://google.github.io/oss-fuzz/advanced-topics/reproducing/#reproducing-bugs The testcase that cause the problem could find in the bug page.

In addition to this, we also added some documentation here: https://github.com/sigstore/sigstore-java/blob/main/DEVELOPMENT.md#reproduce-oss-fuzz-crash

To reproduce this crash:

git clone https://github.com/google/oss-fuzz
cd oss-fuzz
python3 infra/helper.py build_fuzzers sigstore-java

# Download reproducer testcase to a local file
#   From this page: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=57247
#   download `Reproducer Testcase: https://oss-fuzz.com/download?testcase_id=5658600826863616` to the current folder
python3 infra/helper.py reproduce sigstore-java KeysFuzzer ./clusterfuzz-testcase-minimized-KeysFuzzer-5658600826863616

@loosebazooka
Copy link
Member

I think it still makes sense to include a testcase in KeysTest.java to document this behavior. The relationship between ossfuzz and this project is not guaranteed to be transferable. Anyone forking this project or keeping a private copy should be allowed the benefit of a full testsuite.

@DavidKorczynski
Copy link
Contributor

I think it still makes sense to include a testcase in KeysTest.java to document this behavior

@arthurscchan can you do this?

@loosebazooka
Copy link
Member

I can do it too, if arthur is busy

@arthurscchan
Copy link
Collaborator Author

arthurscchan commented Apr 4, 2023

Yes, I will create a unit test case for this issue and push to this PR. I am assuming the unit test case should be add in here (https://github.com/sigstore/sigstore-java/blob/main/sigstore-java/src/test/java/dev/sigstore/encryption/KeysTest.java), right?

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Copy link
Collaborator

@vlsi vlsi left a comment

Choose a reason for hiding this comment

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

Please structure tests in such a way that they fail before you apply the fix.
The current test passes even with the current sigstore-java, so it does not look like the test is helpful

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@loosebazooka loosebazooka added safe to test conformance testing label and removed safe to test conformance testing label labels Apr 5, 2023
loosebazooka
loosebazooka previously approved these changes Apr 5, 2023
@vlsi
Copy link
Collaborator

vlsi commented Apr 5, 2023

The bug has nothing to do with sigstore-java, and the bug belongs to bc-java. If we commit a workaround, there should be a reference to bc-java issue, so we could drop the workaround once bc-java issue is resolved

@loosebazooka
Copy link
Member

Sure, we can link to bcgit/bc-java#1370 for now.

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@loosebazooka loosebazooka requested a review from vlsi April 20, 2023 17:43
Copy link
Collaborator

@vlsi vlsi left a comment

Choose a reason for hiding this comment

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

thanks

@loosebazooka loosebazooka added safe to test conformance testing label and removed safe to test conformance testing label labels Apr 20, 2023
@loosebazooka loosebazooka merged commit 5e126a4 into sigstore:main Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test conformance testing label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants