Skip to content

acquire: verify the attached process matches the selected identity - #56

Merged
bojieli merged 11 commits into
mainfrom
fix/issue-51-attach-identity
Nov 24, 2026
Merged

bojieli merged 11 commits into
mainfrom
fix/issue-51-attach-identity

Conversation

@bojieli

@bojieli bojieli commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Closes #51

What changed

Selected identity in the capture. WeChatProcessTarget.attachIdentity (PID, real UID, start time) is now what the capture attaches with, not a bare PID. With no selected target (a library-only path; the CLI always selects one), the unique WeChat PID is turned into an identity using only kernel metadata: user, start time, and an executable named WeChat. It is read twice and must not change in between.

Two-phase lldb session (LLDBCaptureScript):

  1. attach(processID:) sends the settings, process attach -p N, and process status, then lldb waits on stdin with the process stopped.
  2. When lldb prints Process N stopped, the capture re-reads the PID's UID and start time (proc_pidinfo). Only if both still match does it send capture(architecture:), which holds the conditional breakpoint, the memory read, and process continue.
  3. If they don't match, or can't be read for a live PID, it sends detach (process detach / quit) and throws CaptureError.attachedProcessMismatch. The error message says the breakpoint was never set, no passphrase was read, no output was written, and asks the user to rerun preflight.

Once lldb is attached it holds the task port, so the breakpoint commands that follow reach the verified process even if the PID is recycled later. Some other edge cases:

  • If the identity check finishes after the deadline, lldb detaches without arming the capture.
  • If the stopped PID's identity can't be read because the process is confirmed gone, this counts as a target exit, so the existing restart-following still works.
  • The stdin pipe now uses F_SETNOSIGPIPE, so a write to an lldb that has already exited fails instead of raising SIGPIPE.

Second note: replacement already running when the exit was recorded. WeChatProcessSelector.replacementsStartedBeforeExitRecorded lists same-app processes that started after the current target but no later than the recorded exit, and weren't excluded at selection. It returns nothing for an inconclusive listing. The capture still never attaches to those processes. As soon as it sees one, it prints a note. If nothing that started later shows up before the deadline, it throws CaptureError.replacementTimingAmbiguous, which explains the timing and asks the user to rerun preflight. This takes precedence over noAcceptableCandidate and over a plain timeout. I chose to report at the deadline rather than fail on first sight, so a sibling started during the capture can't preempt a genuine later restart.

CHANGELOG: new Unreleased → Fixed entries.

Tests

swift test passes locally: 163 acquire tests after merging main, run on a copy without the GreenBubblesHistoryApp target because this Mac has only the Command Line Tools. The new tests in Tests/GreenBubblesAcquireTests/AttachIdentityTests.swift use fake process metadata and a shell-script stand-in for lldb that logs every command it receives:

  • When the identity matches, the identity is read while lldb has received only the attach commands, and the breakpoint, memory read, and continue come afterwards in order. A passphrase from the fake dump is returned.
  • When the start time differs, the UID differs, or the identity can't be read, process detach / quit is sent, no breakpoint, memory read or process continue is ever sent, no candidate is offered, and attachedProcessMismatch is thrown.
  • A PID that is gone at the check is reported as targetExited, also with no capture commands. An exit before the stop never reads the identity.
  • Also covered: AttachIdentity.mismatch, Process N stopped parsing, the no-target identity rules, and the ambiguity helper (early restart, siblings, older processes, other users and app copies, inconclusive listings, no exit recorded). The error texts are checked too.
  • LLDBCaptureScriptTests now covers the two phases, including that the attach phase contains no breakpoint, memory, or continue.
  • To check the fake-lldb tests actually catch the bug, I temporarily disabled the identity check: the mismatch test then failed.

The real PID-reuse race can't be reproduced here. It needs a privileged attach racing a PID recycle. The tests reproduce the decision logic and the command ordering, not the kernel race.

Needs manual check (sudo)

scripts/sudo-smoke-test-acquire.sh gains step (e). As root, the real /usr/bin/lldb attaches to a small sleeper that the script compiles and runs as you; it never touches WeChat. The step checks two things:

  • With the sleeper's real identity, the capture verifies it, arms the breakpoint, times out, and leaves the sleeper running and not stopped.
  • With a start time off by 1 µs, it reports a mismatch without arming anything and leaves the sleeper running.

The sleeper is a forked child of a supervisor that doesn't reap it until cleanup, so its PID stays reserved. Only --no-sudo was run here (3 passed, 8 skipped). Please run scripts/sudo-smoke-test-acquire.sh. It is also the first check that real lldb prints Process N stopped the way the parser expects when commands come in over the stdin pipe.

A live WeChat capture (logout/login, including a restart during logout) was not tested here because WeChat isn't installed.

Review

I ran five rounds of Codex review (codex review --base origin/main), and every finding has been addressed:

  • Round 1 (P1): the no-target path didn't check the executable. Fixed.
  • Round 1 (P2s): the ambiguity error now takes precedence over earlier rejections; the message now claims only what was actually withheld; the smoke-test cleanup is fixed.
  • Round 2 (P2s): no root read of Info.plist; no arming after the deadline; no ambiguity reported for an inconclusive listing.
  • Round 3 (P2s): an unverifiable process gets its own diagnostic; processes older than the target are excluded.
  • Round 4 (P2): the smoke-test sleeper's identity is recorded at launch.
  • Round 5 (P2s): a vanished PID counts as an exit, and the sleeper is kept under a supervisor. These last fixes went in after round 5, which was the cap, so they were not re-reviewed by Codex.

Merged origin/main (#53, #54). I kept both sides of the CaptureError and runSingleCapture changes. An lldb exit during the attach phase is now reported through #54's exitError. #54's VerifiedPassphraseCaptureTests drove runSingleCapture with a stand-in lldb that printed reads immediately. I updated that stand-in to report Process 1 stopped first and to wait for the commands that follow the identity check, as real lldb does under the two-phase attach. The merge was not re-reviewed by Codex (the round cap had been reached); the full test suite passes.

🤖 Generated with Claude Code

bojieli and others added 11 commits November 24, 2026 09:35
The capture used to hand lldb only the numeric PID, so a PID reused between
selection and `process attach` could leave the privileged debugger stopped on
an unrelated process before anything noticed.

The selected PID, user and start time now travel into the capture. The lldb
session runs in two phases: lldb first only attaches and prints its status.
Once it reports the process stopped, the PID's user and start time are read
again; only when they still match is the breakpoint (with its memory read)
sent. Otherwise lldb is told to detach and quit, and capture fails closed
with an error that asks for preflight to be rerun.

When WeChat restarts before the capture records the old process's exit, the
new process already existed at that moment and is still never attached. The
capture now says so as soon as it sees it, and reports this timing ambiguity
at the timeout instead of a plain timeout.

The sudo smoke test gains step (e): the real lldb, as root, attaches to a
small program the script starts, with the matching and with a wrong start
time.

Closes #51

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Without a selected app, attach only to a listed PID that runs an
  executable named WeChat for this user and whose identity is unchanged
  when read again.
- At the timeout, report the restart timing ambiguity before an earlier
  rejected candidate.
- Say only what the check withholds: the capture breakpoint and the
  passphrase read (lldb itself inspects a process while attaching).
- Smoke test: SIGKILL the sleeper, also from the exit trap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The no-target path reads only kernel metadata (PID, user, start time,
  executable path), never the app's Info.plist as root.
- Detach instead of arming the capture when the identity check finishes
  after the deadline.
- Report restart timing ambiguity only for a conclusive process listing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Without a selected app, a WeChat process that was found but couldn't be
  verified is reported as such at the timeout, not as "not running".
- A process that started before the current target is never reported as
  its possible restart.
- Smoke test: the sleeper lives longer, and the comment explains why its
  PID can't be recycled while the script holds it unreaped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bash may reap a background child before `wait`, so an unreaped child does
not reserve its PID. The sleeper's user and start time are now recorded
right after launch and passed to both root attach attempts as the expected
identity; a recycled PID would show up as a mismatch. Cleanup kills the
sleeper only while its PID still has that identity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- If the stopped PID's identity can't be read because the process is
  confirmed gone, detach and report a target exit, so the capture follows a
  restart instead of failing as a mismatch. A live but unreadable PID still
  fails closed.
- Smoke test: the sleeper is the forked child of a small supervisor that
  doesn't reap it until cleanup, so its PID stays reserved through both
  root attaches; cleanup never signals a bare PID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keeps both sides of the CaptureError and runSingleCapture changes from #54.
An lldb exit during the attach phase is reported through #54's exitError.
VerifiedPassphraseCaptureTests' stand-in lldb now reports the process stopped
and waits for the post-check capture commands before printing reads, as
real lldb does under the two-phase attach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dentity

# Conflicts:
#	CHANGELOG.md
#	scripts/sudo-smoke-test-acquire.sh
…oke sleeper to the script

- When the stopped PID is confirmed gone at the identity check, record the
  exit time before `detach`, which can wait up to 3 s, so a replacement
  started during that wait counts as starting after the exit. Tested with
  a stand-in lldb that ignores `quit`.
- Smoke test (g): the sleeper's supervisor no longer has a 15-minute timer;
  it reaps the sleeper only when the stop file appears or the script (its
  parent) is gone, so the PID stays reserved through the last root attach.
- sleeper_running rejects a zombie (Z) as well as a stopped (T) sleeper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- At the timeout, a process that was found but couldn't be verified is
  reported before earlier rejected candidates, like the timing ambiguity.
- The exit-before-detach test measures the detach wait after the recorded
  time instead of bounding lldb's startup, so a loaded runner can't flake it.
- Smoke test (g) runs the capture verbosely and checks lldb's own
  transcript: the matching run shows `Breakpoint 1:` (the control), the
  mismatched run shows `Process <pid> detached` and no breakpoint at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bojieli

bojieli commented Nov 24, 2026

Copy link
Copy Markdown
Owner Author

Sudo smoke test at 62e9167 on the owner's Mac: passed: 17 failed: 0 skipped: 0, including (g): real lldb attached as root to a test program, set the breakpoint when the identity matched, and detached without one when the start time was off by 1 µs. The final Codex round found no P0/P1; its P2/P3s are fixed in 9e31a8e and 62e9167.

🤖 Generated with Claude Code

@bojieli
bojieli merged commit 86be945 into main Nov 24, 2026
4 checks passed
@bojieli bojieli mentioned this pull request Nov 24, 2026
bojieli added a commit that referenced this pull request Nov 24, 2026
Set the version to 0.12.0 in Cargo.toml, the root package in Cargo.lock,
and docs/distribution-dependencies.json. Date the CHANGELOG entry and move
the new preflight blocker and the --output requirement under "Changed".
Add the 0.12.0 setup skill notes and a "0.12.0 publication" checklist
section.

Live capture against WeChat was not re-tested. The sudo smoke test passed
17/17 at the #56 head 62e9167, including the real-lldb identity check (g).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

acquire: verify the attached process matches the selected identity

1 participant