Skip to content

[Unsafe] Guard JNI string lengths - #13043

Open
simonrozsival wants to merge 2 commits into
mainfrom
simonrozsival-jni-string-length-guard
Open

simonrozsival wants to merge 2 commits into
mainfrom
simonrozsival-jni-string-length-guard

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Pull Request
title and
description
should follow the
commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

JNIEnv.NewString(char[]?, int) previously forwarded negative or oversized lengths to JNI without checking the source array. It now rejects invalid bounds before pinning/native access, retains the existing null-first behavior, and still accepts legal prefixes. One regression test covers negative and oversized lengths, null with an invalid length, and valid prefix marshaling.

Context: #11467 (related background only; remains open).

Validation

  • make prepare && make all — failed during bootstrap restore because the machine-wide SDK requested unavailable Microsoft.NETCore.App.Ref 10.0.13.
  • PATH="$PWD/bin/Debug/dotnet:$PATH" make prepare && PATH="$PWD/bin/Debug/dotnet:$PATH" make all — prepare and solution compilation passed; final workload configuration failed because the API 37.1 reference assembly was missing.
  • PATH="$PWD/bin/Debug/dotnet:$PATH" make leeroy — passed, including the extra API-level build and local workload configuration.
  • ./dotnet-local.sh build -t:Install -c Debug -p:IncludeCategories=JniStringLength -p:Device=emulator-5570 -p:AdbTarget="-s emulator-5570" tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj — passed with 0 warnings and 0 errors; emulator only.
  • (cd tests/Mono.Android-Tests/Mono.Android-Tests && ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build --device emulator-5570 -c Debug -p:IncludeCategories=JniStringLength -p:AdbTarget="-s emulator-5570" --report-trx --results-directory ../../../bin/TestDebug/TestResults) — passed, 1/1 regression test on emulator-5570.

The full on-device runtime suite was not run. No physical device was used.

`Android.Runtime.JNIEnv.NewString()` forwards its length to JNI without a
bounds check.  Reject negative and oversized lengths before pinning while
preserving the existing null-first return behavior.

Context: #11467

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 8, 2026 22:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Empty arrays with zero length still pass a null pointer to JNI NewString.

1 open finding
What changed in this PR

Adds bounds validation to safely marshal character-array prefixes into JNI strings.

Changes:

  • Rejects negative and oversized lengths.
  • Adds on-device regression coverage for bounds and prefix marshaling.
File Description
src/​Mono.Android/​Android.Runtime/​JNIEnv.cs Validates JNI string lengths.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JnienvTest.cs Tests invalid lengths and valid prefixes.

🧠 Review effort: Balanced

Comment thread src/Mono.Android/Android.Runtime/JNIEnv.cs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival changed the title [Mono.Android] Guard JNI string lengths [Unsafe] Guard JNI string lengths Oct 9, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants