Skip to content

System.Native: avoid close_range on OpenHarmony - #136187

Closed
springmin wants to merge 1 commit into
dotnet:mainfrom
springmin:pr/ohos-pal-process
Closed

springmin wants to merge 1 commit into
dotnet:mainfrom
springmin:pr/ohos-pal-process

Conversation

@springmin

Copy link
Copy Markdown
Contributor

Summary

OpenHarmony's seccomp policy traps close_range() with SIGSYS, so
Process.Start with handle-inheritance restrictions dies before a fallback can
run. Skip the libc function and the raw-syscall paths on TARGET_OPENHARMONY
and set FD_CLOEXEC on the open descriptors by listing /proc/self/fd.

Changes

  • src/native/libs/System.Native/pal_process.c: add !defined(TARGET_OPENHARMONY)
    to both close_range branches of RestrictHandleInheritance; on OpenHarmony
    the function calls SetCloexecForAllFdsFallback() instead.
  • The fallback reads /proc/self/fd with getdents64 into a stack buffer (it
    runs in the forked child, where malloc is not safe) and sets FD_CLOEXEC on
    the descriptors it finds. The already-existing bounded RLIMIT_NOFILE scan
    remains the fallback for when the listing cannot be read completely.
  • One file, 171 insertions and 8 deletions.

Impact

  • OpenHarmony only. Every other target keeps the upstream close_range paths
    and upstream's ENOTSUP result when neither close_range nor fdwalk is
    available.
  • Shared helpers are factored so the fdwalk callback and the /proc/self/fd
    walk use the same per-descriptor routine; behavior on Illumos/Solaris is
    unchanged.

Validation

  • pal_process.c compiles for openharmony-arm64 with the OHOS NDK clang
    (--target=aarch64-linux-ohos, -Weverything), no warnings.

Test

  1. Build System.Native for openharmony-arm64 (this defines
    TARGET_OPENHARMONY).
  2. On the device, start a child process with ProcessStartInfo.InheritedHandles
    configured; the child should exec, the inherited handles should survive, and
    the other descriptors should be close-on-exec.

Note

This PR was authored with AI assistance (agent tooling) under the author's direction.

The OpenHarmony seccomp policy traps close_range() with SIGSYS, which kills
the process before a fallback can run. Skip the libc function and the
raw-syscall paths on TARGET_OPENHARMONY, and set FD_CLOEXEC on the open
descriptors by reading /proc/self/fd; the bounded rlimit scan remains as a
fallback when the listing cannot be read.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 20, 2026
springmin added a commit to springmin/runtime-ohos that referenced this pull request Oct 20, 2026
…6187)

A: NativeAOT and pack integration (7 files, 57+/7-)
B: libraries TFM groups and console support (7 files, 31+/6-)
C: System.Native avoid close_range (1 file, 171+/8-)
All three based on cf3fbf9; in flight: N3/N4/N8 + these three.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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.

@springmin

Copy link
Copy Markdown
Contributor Author

Superseded by #136194, which consolidates the remaining native-libs fixes and includes device validation (hello world, child process start, network interfaces, RSA/TLS through the OpenSSL shim).

Note

This comment was drafted with AI assistance (agent tooling) under the author's direction.

@springmin springmin closed this Oct 21, 2026
springmin added a commit to springmin/runtime-ohos that referenced this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant