bazelbuild / bazelbuild/bazel

Missing bounds and null checks in BLAKE3 JNI bindings allow native crash

Open
#31,026 0 comments 0 reactions 0 assignees View on GitHub
team-Core type: bug untriaged
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### Description of the bug:

The JNI layer that implements BLAKE3 in Bazel (blake3_jni.cc) does not validate the return value of GetPrimitiveArrayCritical or the offset/length parameters before using them. Calling Blake3MessageDigest.engineUpdate with a negative length produces a reliable native SIGSEGV inside libunix_jni.so.

Affected code:

src/main/native/blake3_jni.cc
com.google.devtools.build.lib.vfs.bazel.Blake3MessageDigest.engineUpdate

GetPrimitiveArrayCritical results are not checked for NULL, and the jint offset / length values are used for pointer arithmetic without verifying they stay within the array bounds. A negative length triggers undefined behavior while the array is pinned.

Proof of Concept:

Confirmed on Bazel 9.2.0 + OpenJDK 21. A short Java program that obtains a Blake3MessageDigest instance via reflection and calls engineUpdate(data, 0, -1) produces a native SIGSEGV.

Code:
```
import java.lang.reflect.Method;
import java.util.Arrays;

public class Blake3CrashTest {
public static void main(String[] args) throws Exception {
Class clazz = Class.forName(
"com.google.devtools.build.lib.vfs.bazel.Blake3MessageDigest");
Object md = clazz.getDeclaredConstructor().newInstance();

byte[] data = new byte[64];
Arrays.fill(data, (byte) 0x41);

Method engineUpdate = clazz.getDeclaredMethod(
"engineUpdate", byte[].class, int.class, int.class);
engineUpdate.setAccessible(true);

// Negative length triggers native SIGSEGV
engineUpdate.invoke(md, data, 0, -1);
}
}
```
Bash:
```
javac -cp /path/to/A-server.jar Blake3CrashTest.java
java -cp ".:/path/to/A-server.jar" Blake3CrashTest
```
Patch PR: https://github.com/bazelbuild/bazel/pull/31027#issue-5363161956

Contributor guide

Open the contributing guide

Research direction

Start with src/main/native/blake3_jni.cc and the com.google.devtools.build.lib.vfs.bazel.Blake3MessageDigest.engineUpdate entry point. Run the provided Blake3CrashTest against Bazel 9.2.0 to reproduce the failure, then verify that invalid offsets or lengths and NULL results from GetPrimitiveArrayCritical no longer cause unsafe native access.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.