Skip to content

Fix restricted process handle inheritance on Android - #137485

Merged
krwq merged 1 commit into
mainfrom
krwq-android-restricted-handle-fix
Nov 17, 2026
Merged

krwq merged 1 commit into
mainfrom
krwq-android-restricted-handle-fix

Conversation

@krwq

@krwq krwq commented Nov 13, 2026

Copy link
Copy Markdown
Member

Note

Alternative for discussion: return ENOTSUP on Android instead of implementing restricted handle inheritance. That would make direct process launches with non-null ProcessStartInfo.InheritedHandles (including []) fail at startup rather than run without the requested restriction. Leaving InheritedHandles null 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 RestrictHandleInheritance calls close_range, rather than returning an error. The runtime reports that as exit code 159. This affects the restricted case of MobileProcessTests.Process_Start_WithStandardHandles_CanRedirectIO on 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.

  • On Android, enumerate /proc/self/fd with getdents64 and mark open descriptors close-on-exec, then retain the existing explicit allow-list handling.
  • Avoid allocations and directory-library locks between fork and exec, without scanning every possible descriptor number.
  • Add four Android theory cases covering inheritable/non-inheritable parent handles and explicit/excluded child inheritance. Other platforms and unrestricted launches keep their existing paths.
  • No CI, Helix, queue, or container changes.

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 actual SystemNative_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 with adb -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.
  • The added managed tests have not been successfully compiled or executed. Full managed CoreCLR/NativeAOT Android integration is not verified by the native harness.
  • git diff --check — passed.

Fixes #136906

Note

This pull request description and code changes were generated with GitHub Copilot.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

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.

🔵 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/fd using getdents64 on 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

@krwq

krwq commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

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.

@jkotas

jkotas commented Nov 13, 2026

Copy link
Copy Markdown
Member

This is overlaping in scope with #136194 therefore do not merge until that one is merged.

This is fixing blocking-clean-ci issue. You should not block on #136194 . #136194 contains changes from multiple areas - PRs like that take forever to get all signoffs and merge.

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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.

@krwq

krwq commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

@adamsitnik the sh usage is limited to tests and per Copilot research it should be available both on emulator and physical Android. Please let me know if there is anything here you think is blocking

@adamsitnik

Copy link
Copy Markdown
Member

/azp run runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@adamsitnik

Copy link
Copy Markdown
Member

/azp list

@azure-pipelines

This comment was marked as resolved.

@dotnet dotnet deleted a comment from azure-pipelines Bot Nov 17, 2026
@adamsitnik

Copy link
Copy Markdown
Member

/azp run runtime-androidemulator

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@adamsitnik adamsitnik added this to the 12.0.0 milestone Nov 17, 2026
@krwq

krwq commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Copilot generated Android CI failures analysis

Device build 1685138 failed. Emulator build 1685139 passed.

Configuration Failure Explanation Relation to this PR
CoreCLR ARM and ARM64 devices BuildChainForExpiredSelfSignedCertificate An expired certificate reports NoError instead of the expected NotTimeValid. Exact signature already tracked in #133314, including on main. Pre-existing certificate failure, not handle inheritance.
CoreCLR ARM device System.Security.Cryptography.Pkcs.Tests crashes SIGSEGV in HmacCreate while verifying a PKCS#12 MAC. Matches #131856, also observed on main. Pre-existing crypto crash; no related fix needed here.
NativeAOT ARM64 device 184 work items fail before producing test results Process and Common app logs show dlopen failed: cannot locate symbol "tls_CurrentThread". Matches the platform/runtime regression in #137692; the implicated EH-helper change predates this PR. Unrelated startup blocker. These runs cannot validate our tests. Two representative app logs inspected, not all 184.

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 adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you for fixing the issue @krwq !

@krwq

krwq commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

/ba-g failures are unrelated

@krwq
krwq merged commit cff586c into main Nov 17, 2026
113 of 117 checks passed
@krwq
krwq deleted the krwq-android-restricted-handle-fix branch November 17, 2026 12:47
@jkotas

jkotas commented Nov 17, 2026

Copy link
Copy Markdown
Member

/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.

springmin added a commit to springmin/runtime-ohos that referenced this pull request Nov 18, 2026
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.
@krwq

krwq commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

I focused the Copilot report on the Android failures, so it didn't cover this one. The Alpine failure was already tracked in #137524 and fixed by #137530.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ci-scan] Test failure: System.Diagnostics.Tests.MobileProcessTests.Process_Start_WithStandardHandles_CanRedirectIO

4 participants