Repository navigation
Handle null context and algorithm names without crashing the JVM - #37
Merged
Merged
Conversation
Passing null as the context to Signature.sign(message, ctx) or Signature.verify(message, signature, ctx, public_key) crashed the JVM, because the JNI code passed the null array to GetByteArrayElements. A null algorithm name crashed the same way in GetStringUTFChars. Pass a null context to liboqs as NULL, and treat a null algorithm name as not enabled. Also initialize the signature length, so that a failed signing operation throws "Cannot sign message" instead of an exception built from an uninitialized length. Signed-off-by: Douglas Stebila <dstebila@uwaterloo.ca> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Passing
nullto several public APIs crashed the JVM with a SIGSEGV. Java exception handlers can't catch the crash, so it ends the whole process.Signature.sign(message, ctx)andSignature.verify(message, signature, ctx, public_key)already treat a nullctxas length 0, but the JNI code passed the null array toGetByteArrayElements. This affects every release since context support was added in 0.2.0.new Signature(null),new KeyEncapsulation(null),Sigs.is_sig_enabled(null),KEMs.is_KEM_enabled(null),Rand.randombytes_switch_algorithm(null),Signature.create_sig_new(null)andKeyEncapsulation.create_KEM_new(null)passed the null string toGetStringUTFChars. This affects all releases.This PR also fixes a related problem when signing fails. The signature length was uninitialized, so a failure such as an ML-DSA context longer than 255 bytes surfaced as an
ArrayIndexOutOfBoundsExceptionwith a garbage length, instead ofRuntimeException("Cannot sign message").Changes
NULLwith length 0, which means no context. A signature made with a null context verifies with the plainverify. The Javadoc now says that null means no context. Empty arrays behave as before.MechanismNotSupportedError,Rand.randombytes_switch_algorithmthrows its usualRuntimeException, andcreate_sig_new(null)andcreate_KEM_new(null)leave the existing mechanism in place.Testing
There are 7 new tests in
SigTest,KEMTestand a newRandTest. Before the fix, 6 of them crashed the JVM and the seventh failed on the exception message.Tested locally with
mvn packageon macOS (arm64, JDK 27) against liboqsmain(8c41c85), where all the new tests pass.Until #36 is merged, three existing
testAllSigscases fail on both this branch andmaster. They are ML-DSA-44-extmu, ML-DSA-65-extmu and ML-DSA-87-extmu, which liboqs signs only when the message is the 64-byte μ, while the test signs a 27-byte message. Onmasterthat failure shows up as theArrayIndexOutOfBoundsExceptionwith a garbage length described above. With this PR it becomesRuntimeException: Cannot sign message. Merged with #36,mvn packagepasses with no errors.Reported by Zhang Jianhui (Jack), @mingmingzhizhongcz.
🤖 Generated with Claude Code