Repository navigation
Fix restricted process handle inheritance on Android - #137485
Conversation
Enumerate open descriptors without allocation in the Android fork child to avoid seccomp-forbidden close_range calls. Add mobile coverage for explicit and excluded handle inheritance. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
There was a problem hiding this comment.
🔵 Needs a closer look
Android sandbox and ABI dependencies require platform-owner validation, as native-harness coverage does not validate CoreCLR/NativeAOT integration and the managed tests remain uncompiled and unrun.
No open issues 🎉
What changed in this PR
This PR addresses Android child-process failures caused by close_range under app seccomp restrictions, while preserving explicit handle inheritance in System.Diagnostics.Process.
Changes:
- Enumerates
/proc/self/fdusinggetdents64on Android and marks descriptors close-on-exec before applying the existing allow-list. - Leaves other platforms and unrestricted launches on their existing paths.
- Adds four Android test cases covering parent-handle inheritability and explicit inclusion or exclusion.
The PR reports successful native harness validation on x64 and x86; managed tests have not yet been successfully compiled or run.
| File | Description |
|---|---|
src/native/libs/System.Native/pal_process.c |
Adds Android-specific descriptor enumeration to avoid close_range. |
src/libraries/System.Diagnostics.Process/tests/ProcessTests.Mobile.cs |
Adds Android coverage for restricted handle inheritance. |
Review effort: Balanced
|
This is overlaping in scope with #136194 therefore do not merge until that one is merged. Once OpenHarmony PR is merged I'll see if there is any work left to address the original issue. |
adamsitnik
left a comment
There was a problem hiding this comment.
@krwq the fix LGTM, big thanks for taking care of making the CI green!
I am not sure if sh is always available on Android, please verify that before we merge it.
|
@adamsitnik the |
|
/azp run runtime-android |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp list |
This comment was marked as resolved.
This comment was marked as resolved.
|
/azp run runtime-androidemulator |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Copilot generated Android CI failures analysisDevice build 1685138 failed. Emulator build 1685139 passed.
Our tests passed: all four new inheritance cases and both original standard-handle cases passed on CoreCLR ARM/ARM64 devices and CoreCLR/NativeAOT x64 emulators—24 passing cases across four configurations. No local fixes, pushes, or CI reruns were made. The GitHub Build Analysis snapshot predates these Android runs; the classifications above use their logs and existing issue evidence. |
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM, thank you for fixing the issue @krwq !
|
/ba-g failures are unrelated |
@krwq You are still expected to ensure that the failures are tracked. They won't get fixed otherwise. https://github.com/dotnet/runtime/blob/main/docs/workflow/ci/failure-analysis.md#what-to-do-if-you-determine-the-failure-is-unrelated It looks suspicious that the new unknown failure is System.Diagnostics.Process tests and this PR is changing System.Diagnostics.Process. |
Resolve the pal_process.c conflict with dotnet#137485: OpenHarmony has the same seccomp restriction on close_range() as Android, so reuse the getdents64-based handle-inheritance enumeration and extend it to TARGET_OPENHARMONY instead of keeping a duplicate copy.
Note
Alternative for discussion: return
ENOTSUPon Android instead of implementing restricted handle inheritance. That would make direct process launches with non-nullProcessStartInfo.InheritedHandles(including[]) fail at startup rather than run without the requested restriction. LeavingInheritedHandlesnull would retain ordinary inheritance behavior. This PR shows an implementation that preserves the feature; feedback on the preferred approach is welcome.Summary
Android app seccomp policy kills the child with SIGSYS when
RestrictHandleInheritancecallsclose_range, rather than returning an error. The runtime reports that as exit code 159. This affects the restricted case ofMobileProcessTests.Process_Start_WithStandardHandles_CanRedirectIOon CoreCLR and NativeAOT.As of November 13, 2026, the issue's Build Analysis summary reports 1 hit in the past 24 hours, 15 in the past 7 days, and 26 in the past month. These are reported matching occurrences, not necessarily distinct builds.
/proc/self/fdwithgetdents64and mark open descriptors close-on-exec, then retain the existing explicit allow-list handling.forkandexec, without scanning every possible descriptor number.Validation
Native product code was cross-built on Windows using NDK r27c and exercised in a real API29 emulator app sandbox (
seccomp=2), through a temporary JNI harness linked to the actualSystemNative_ForkAndExecProcess, not a copy of the proposed helper.cmake --build "$root\product-build" --target System.Native-Static -j 2— passed for x64, both unchanged baseline and fix.cmake --build "$root\product-build-x86" --target System.Native-Static -j 2— passed.cmake --build "$root\product-build-arm64" --target System.Native-Static -j 2— passed; not executed.cmake --build "$root\product-build-arm" --target System.Native-Static -j 2— passed; not executed.& "$root\build-probe.ps1" -ProductLibrary "$root\baseline-System.Native.a", followed by APK installation and launch withadb -s emulator-5580 shell am start -n org.dotnet.handleprobe/.MainActivity— baseline 2/14 passed, all 12 restricted cases failed with SIGSYS.& "$root\build-probe.ps1", followed by the same APK installation/launch — fixed x64 14/14 passed.& "$root\build-probe.ps1" -Architecture x86, followed by the same APK installation/launch — fixed x86 14/14 passed.The native cases cover redirected standard handles, explicit/excluded descriptors, CLOEXEC descriptors, fd 4096 above a lowered soft limit, multiple directory-read batches, and failed-exec error reporting. The temporary harness and SDK files are not included in this PR.
Supplementary short native timing samples with 256 extra open descriptors and 50 launches per configuration: fixed x64 unrestricted/restricted means were 9.504/10.236 ms; fixed x86 6.895/7.133 ms. All launches succeeded. These are not BenchmarkDotNet results or a historical performance comparison; no performance improvement is claimed.
Managed validation is incomplete:
.\dotnet.cmd build src\libraries\System.Diagnostics.Process\tests\System.Diagnostics.Process.Tests.csproj -p:Configuration=Release— failed because the local shared-framework targeting pack is missing..\dotnet.cmd build src\libraries\System.Diagnostics.Process\tests\System.Diagnostics.Process.Tests.csproj -p:Configuration=Release -p:TargetOS=android -p:TargetArchitecture=x64 -p:RuntimeFlavor=CoreCLR— failed at the same targeting-pack gate..\build.cmd clr+libs -rc Release— failed in the existing NativeAOT datadescriptor build: MSVC C1083,Cannot open compiler generated file: '': Invalid argument. No unrelated build fix was attempted.git diff --check— passed.Fixes #136906
Note
This pull request description and code changes were generated with GitHub Copilot.