Skip to content

fix(process): re-read unreadable tokens before signaling and name the process - #8553

Merged
kaoru0822-kitauji merged 5 commits into
mainfrom
fix/8337-darwin-transient-token-read
Nov 2, 2026
Merged

kaoru0822-kitauji merged 5 commits into
mainfrom
fix/8337-darwin-transient-token-read

Conversation

@kaoru0822-kitauji

@kaoru0822-kitauji kaoru0822-kitauji commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

What broke: On macOS, one unreadable KERN_PROCARGS2 token read of a live group member during the pre-signal group check failed the whole owned-command cleanup (malformed_procargs_path_padding). The final sweep already re-reads such tokens, but this check read each token once, and the failure named only a PID.
Fix: One shared, bounded re-read (3 × 40 ms, each a full identity/token/identity read) now serves both the sweep and the pre-signal check. In the pre-signal check it is bound to the first observed generation. An unreadable failure now carries the native identity, and the summary names the process (native name, ppid, pgid, start identity).
Risk: Ownership stays fail-closed. A persistently unreadable member still fails, about 150–190 ms later (failure path only). The cleanup summary text gains a per-candidate descriptor, and native identities gain an optional display name.

Summary

Fixes #8337. npm run verify:package on Native package baseline (darwin arm64) intermittently failed final cleanup with live candidates <pid>=malformed_procargs_path_padding after a successful command. Cleanup now re-reads a transiently unreadable token before it decides a group. When a member stays unreadable, the failure says which process it is.

Problem and expected behavior

  • Kernel source (xnu-11417.140.69, xnu-12377.121.6): sysctl_procargsx copies p_argc, p_argslen and user_stack and then takes the task map. Its own comment says the result can be stale during execve. exec_copyout_strings also publishes p_argslen before it copies the new strings out. So a read during exec can return a zero-filled or stale strings area. Exec keeps the PID and the start time, so the helper's before/after identity check cannot reject that read.
  • The parser's padding rule ("executable_path=" + path + NUL, aligned to the pointer size) matches exec_save_path and exec_extract_strings. A steady-state scan of 342 local same-user processes found no malformed layouts. Option (a), changing the parser, therefore does not apply.
  • scanWithOwnershipEvidence already re-read unavailable tokens. processOwnershipFailures (cleanup plan and revalidateOwnedProcessGroup) took one read, and any unreadable live same-generation member failed cleanup.
  • Expected: a transient read settles before the group is classified. Real uncertainty still fails closed, and the failure identifies the process.

Change and scope

  • ProcessOwnershipObservation.reobserveUnavailableTokenFacts is the single re-read owner and replaces the sweep's private loop.
    • current (sweep): classifies whichever process now holds the PID. This is the existing behaviour.
    • first-observed (pre-signal check, given the caller's run token): re-reads only same-generation facts.
      • Each attempt re-reads the unreadable members together with the group's owned members. A member that moves groups or turns unreadable while another member waits is therefore seen within the same bound, and a moved member retains ownership and fails cleanup.
      • A re-read that sees another generation marks the fact as changed.
      • Changed, exited, exiting and foreign-token facts are final and never re-read, including a PID reuse that the first read already established.
      • An inconclusive re-read of an owned member fails closed.
  • environment-unreadable failures carry identityAfter, the native readable identity. The summary appends (name "…", ppid N, pgid N, identity …) per candidate. Only native facts are shown; process arguments and environment are never included.
  • The Darwin helper reports p_comm and the Linux procfs reader reports comm, both as the optional display-only name.
  • Not changed:
    • Hosts without native identities (custom or test hosts only).
    • observeOwnedProcessGroups capture settlement, which reports unverifiable rather than failing.
    • The macOS 26 synthetic layout for com.apple.private.no-read-procargs processes (exec path, one NUL, argv[0]). The parser would also reject it as path_padding for 6 of 8 path lengths. No local binary has that entitlement and the macOS 15 source lacks the path, so it is left as a follow-up.
  • Contribution scope: an ordinary fix within existing boundaries. Not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: the native identity readers (Darwin helper, Linux procfs) gain name. ProcessOwnershipObservation owns the token re-reads. Consumers are the sweep and the pre-signal group check in ProcessOwnership. The sweep's duplicated loop is removed.

  • Contribution scope fit and linked maintainer decision: not applicable (fix inside existing ownership boundary).

  • CLI and MCP/tool-catalog contract: no schema or catalog change. The cleanup failure reason text gains a per-candidate descriptor. Structured failures[] entries gain optional identityAfter, which is visible through process_cleanup details.

  • Provider, bridge, target-format, or platform compatibility: the Darwin helper response gains optional name. The helper source digest changes, so the helper cache recompiles once.

  • Evidence, artifact, provenance, or reconstruction contract: none.

  • Target selection, authority, upstream behavior, and cleanup details: no new signal authority. Every re-read is a full identity/token/identity read. In the pre-signal check a generation change stays unresolved.

  • Generated metadata, package, or installation impact: none.

  • Read existing files or captured data.

  • Launch or attach to a process.

  • Inject instrumentation or run scripts/code.

  • Change process memory, registers, files, or configuration.

  • Contact network services.

  • Recover protected code or work around protections.

  • Documentation or usability only; no target interaction.

  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:

    • New src/process/ProcessOwnership.tokenReread.test.ts covers:
      • a transient read with and without a sweep
      • a persistent read: fails closed, names the process, read count bounded by TOKEN_READ_RETRY_ATTEMPTS
      • exit during a re-read
      • PID reuse during a re-read and before it starts
      • a readable member that changes groups during another member's re-read (with the refresh removed, cleanup returns cleaned: true while the member runs on)
      • a PID reuse that the first Linux read already established, with a zombie replacement
      • an owned member whose own refresh read is transient
      • mixed member outcomes
      • cleanup retry
    • ProcessOwnership.nativeExit (Linux ESRCH): token reads go from 2 to 3. The one extra read happens before exit settles the member.
    • Linux stat and procIdentity tests: add name.
    • Swift snapshot probe: asserts name.
    • tests/support/hostCleanup.ts accepts the descriptor.
  • Base reproduction: with src/process/ProcessOwnership.ts and src/process/ProcessOwnershipObservation.ts restored from origin/main, 7 of 8 original new tests fail. Each review regression fails with only its guard removed, or with the previous helper. The transient case returns exactly the CI summary: process ownership token could not be read for 1 live process(es): malformed_procargs_path_padding=1; live candidates 101=malformed_procargs_path_padding.

  • Root-cause investigation (executed, macOS 26.7.1 arm64):

    • A bounded harness ran owned execve chains and posix_spawned #!/usr/bin/env node children under 8× CPU oversubscription: about 91k execs and 30M KERN_PROCARGS2 reads, all parsed by the production parser. It produced transient zero-filled strings (malformed_procargs_executable_path), EIO and EINVAL, each readable again within 17.5 ms. It produced no path_padding.
    • A native watcher read every group member's procargs throughout 15 real rea trace <node> fixture --provider hopper --json runs (about 670k reads). It saw no malformed reads, and no group members were alive after the leader exited.
    • path_padding itself is not reproduced locally. CI runs macOS 15.8.2, whose xnu source is not published.
    • The CI summary does not say which phase failed. The sweep already re-reads for up to about 1 s, while the pre-signal check read once. The retained retry at close also failed, but its details were not logged, so a persistent state is not ruled out. If it recurs, the new descriptor identifies the process.
  • User-visible output, from a real Darwin host where a detached /bin/sleep stays in the group: process ownership token could not be read for 1 live process(es): platform_binary_environment_withheld=1; live candidates 10967=platform_binary_environment_withheld (name "sleep", ppid 1, pgid 10966, identity 1793549013:220807)

  • Lifecycle regressions covered:

    • mixed child outcomes
    • cleanup failure followed by a retry with the same ownership
    • exit and PID reuse during the re-read
    • a group change by another member while the re-read waits

    Cancellation is not applicable, because cleanup reads run without a caller signal.

  • Remaining proof gaps: no CI macOS 15.8 kernel behaviour, no verify:package run, no real Linux procfs run (simulated only).

  • Observed, derived, and inferred claims remain distinguishable.

  • Artifact identity, source provenance, and failed attempts remain preserved.

  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run test:local -- $(find src/process -name '*.test.ts'): 67 files, 917 passed, 6 skipped (after review fixes).

  • npm run test:local -- on the 13 other source tests that use ownership (firmware, apktool, browser, ghidra, application): 218 passed, 9 skipped.

  • npx vitest run --project process-boundary

    • darwinProcessRunTokenReader, darwinHelperCache, processLineageRetention (real Swift helper compile): 53 passed.
    • ownedCommand, ownedCommandCancellation, processCleanupRetryOwnership, processCaptureLifecycle, processRunTokenEnvironment, verifierCleanup: passed. Rerun after the review fixes together with the Darwin files above: 130 passed, 2 skipped.
    • packageVerifierCommandOwnership and agentEvaluationCleanup: fail locally on the known Node 26 stripTypeScriptTypes (transform) loader crash, before reaching this code.
  • npx vitest run --project process-global (4 ownership and cleanup files): 102 passed.

  • Real-host cleanup timing after the second review round (6 runs, warm helper, runOwnedCommand with sweep):

    Case origin/main This PR
    Readable child 131 ms 133 ms
    One live withheld member 317 ms 472 ms
    Withheld member plus readable member 317 ms 481 ms

    Only the failure path costs more. Both versions fail closed; only this PR names the process.

  • npm run check:fast: pass.

  • npm run format:check: pass.

  • npm run test:local -- tests/conformance/readmeConsistency.test.ts: 9 passed.

  • node scripts/check-doc-facts.mjs and node scripts/generate-error-schema.mjs --check: exit 0.

  • Not run locally: full npm run docs:check and test:focused, because of the Node 26 crash. CI runs them on Node 24.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. The new fields are optional, and the summary text is a diagnostic.
  • Real Hopper/Ghidra, browser, or OS coverage: real macOS 26.7.1 arm64 (Darwin helper, owned commands, real CLI runs with a simulated Hopper launcher). No real Hopper/Ghidra or provider coverage, no CI macOS 15.8, no real Linux.
  • Package or release metadata impact: none. The bridge Swift source is already packaged.
  • Security, privacy, process, or containment review: the added name is the kernel accounting name (≤16 bytes), with no argv or environment. Re-reads add no signal authority. A generation change before signaling stays unresolved.

Author readiness

Human owner: @kaoru0822-kitauji

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code

… process

Darwin KERN_PROCARGS2 samples a process's argument length and stack top
before it reads the address space, and exec publishes the new length
before it copies the new strings out (xnu sysctl_procargsx,
exec_copyout_strings). A read during exec can return a zero-filled or
stale strings area, and exec keeps the PID and start time that bracket
the read. The final sweep already re-read such tokens, but the
pre-signal group check read each member's token once, so one transient
read of a live group member failed the whole cleanup.

Move the bounded re-read into one shared fact-reading helper and use it
for both the sweep and the pre-signal group check. The group check binds
re-reads to the generation it first observed, so a reused PID stays
unresolved; the sweep keeps classifying the PID's current process.

An unreadable-token failure now carries the native identity read after
the token and the summary names the candidate by its native name,
parent, group and start identity. The Darwin helper and Linux procfs
reader report the short native process name (p_comm/comm) for display.

Fixes #8337

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji added bug Something isn't working track:bugs Small-bug loop track PRs labels Nov 1, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T16:51:20.715643Z f47826b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5 Tier: apex

[Medium impact] The changes reach shared process-ownership cleanup workflows.

The last refresh can still reject a transient token read without retrying it, so the remaining prior finding should be addressed before merging.

Summary

Shares bounded token rereads between the ownership sweep and pre-signal group checks. Unreadable-token errors now include native process names and identity facts.

  • Pre-signal checks retry unreadable tokens without switching process generations.
  • Unreadable-token failures name the process from native identity facts.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read identity, token, identity] --> B{Live unreadable token?}
  B -->|Yes, attempts remain| C[Wait 40 ms]
  C --> D[Reread selected members]
  D --> E{Pre-signal check?}
  E -->|Yes| F[Keep first-generation binding]
  E -->|No| G[Use current PID facts]
  F --> B
  G --> B
  B -->|No, or limit reached| H[Check ownership and current group]
  H -->|Uncertain| I[Fail cleanup and retain ownership]
  H -->|Verified| J[Allow group signal]
Loading

Reviews (3) · Last reviewed commit: "fix(process): keep known identity change..." · Reviewed by Greptile

kaoru0822-kitauji and others added 2 commits November 1, 2026 16:24
The pre-signal group check re-read only members whose token was
unavailable, so a readable member kept the relation it had before the
retry waits. A member that moved to another process group during those
waits could be validated on its old group, and the old group signaled
while the moved member kept running.

After any retry wait, the shared re-read helper now reads every other
live pre-signal fact once more with the same generation binding. A moved
member then takes the existing changed-group path, which retains its
ownership evidence and fails cleanup. An inconclusive refresh replaces
the stale fact so it fails closed. Nothing changes when no re-read ran,
and the sweep keeps classifying current processes because every group
it finds is revalidated before signaling.

Also cover PID reuse that happens before the re-read starts, where both
re-read identities agree and only the first-read generation rejects the
replacement's token.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kaoru0822-kitauji and others added 2 commits November 1, 2026 16:42
The pre-signal re-read treated a first read without a same-generation
identity as unbound and replaced it with the re-read. On Linux the first
read can already establish PID reuse (an ESRCH token whose exit check sees
another generation); the re-read then classified the replacement, so a
zombie replacement could let cleanup succeed.

A pre-signal check now re-reads only same-generation facts. Changed,
exited, exiting and foreign-token facts are final. Each attempt also
re-reads the group's owned members instead of one separate refresh after
the loop, so a member that moves groups or turns unreadable while another
waits is seen within the same bound. The caller's run token selects the
owned members, and the sweep keeps classifying current processes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji
kaoru0822-kitauji merged commit 2ac6d3e into main Nov 2, 2026
49 of 51 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the fix/8337-darwin-transient-token-read branch November 2, 2026 08:21
coyoteXujie pushed a commit to coyoteXujie/rea that referenced this pull request Nov 2, 2026
… process (morluto#8553)

* fix(process): re-read unreadable tokens before signaling and name the process

Darwin KERN_PROCARGS2 samples a process's argument length and stack top
before it reads the address space, and exec publishes the new length
before it copies the new strings out (xnu sysctl_procargsx,
exec_copyout_strings). A read during exec can return a zero-filled or
stale strings area, and exec keeps the PID and start time that bracket
the read. The final sweep already re-read such tokens, but the
pre-signal group check read each member's token once, so one transient
read of a live group member failed the whole cleanup.

Move the bounded re-read into one shared fact-reading helper and use it
for both the sweep and the pre-signal group check. The group check binds
re-reads to the generation it first observed, so a reused PID stays
unresolved; the sweep keeps classifying the PID's current process.

An unreadable-token failure now carries the native identity read after
the token and the summary names the candidate by its native name,
parent, group and start identity. The Darwin helper and Linux procfs
reader report the short native process name (p_comm/comm) for display.

Fixes morluto#8337

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(process): refresh other pre-signal members after token re-read waits

The pre-signal group check re-read only members whose token was
unavailable, so a readable member kept the relation it had before the
retry waits. A member that moved to another process group during those
waits could be validated on its old group, and the old group signaled
while the moved member kept running.

After any retry wait, the shared re-read helper now reads every other
live pre-signal fact once more with the same generation binding. A moved
member then takes the existing changed-group path, which retains its
ownership evidence and fails cleanup. An inconclusive refresh replaces
the stale fact so it fails closed. Nothing changes when no re-read ran,
and the sweep keeps classifying current processes because every group
it finds is revalidated before signaling.

Also cover PID reuse that happens before the re-read starts, where both
re-read identities agree and only the first-read generation rejects the
replacement's token.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(process): keep known identity changes final across token re-reads

The pre-signal re-read treated a first read without a same-generation
identity as unbound and replaced it with the re-read. On Linux the first
read can already establish PID reuse (an ESRCH token whose exit check sees
another generation); the re-read then classified the replacement, so a
zombie replacement could let cleanup succeed.

A pre-signal check now re-reads only same-generation facts. Changed,
exited, exiting and foreign-token facts are final. Each attempt also
re-reads the group's owned members instead of one separate refresh after
the loop, so a member that moves groups or turns unreadable while another
waits is seen within the same bound. The caller's run token selects the
owned members, and the sweep keeps classifying current processes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@morluto morluto added the area: runtime Process capture and runtime observation label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime Process capture and runtime observation bug Something isn't working track:bugs Small-bug loop track PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] macOS package verifier intermittently fails cleanup on malformed_procargs_path_padding

2 participants