fix(android): authenticate and bound persistent snapshot helper sessions

Merged
TH
7d ago
+3264-260

Summary

Persistent snapshot-helper sessions are activated by a versioned mode, not by a port. The host
launches -e mode authenticated-session-v1 -e sessionSocket <name> and never sessionPort. A
helper build that does not implement that mode rejects the launch in onStart, before it binds
anything, so an old build on the device keeps the one-shot transport and never opens a loopback TCP
listener. Nothing is read from the artifact to decide this: provider-supplied artifacts serve
sessions exactly like the bundled one.

The host owns the whole abstract socket name. The helper binds the launched name unchanged and
refuses any name outside the agentdevice-snapshot-helper- namespace. Readiness repeats the name in
sessionSocket, and the host refuses a record naming another socket, so the launch arguments, the
forward, and the ready record must agree. A ready record that is not well formed is an ordinary
failed start, handled by the existing finite start backoff.

Before reading a command, the helper admits only the kernel-reported shell or root UID used by adb,
or closes the peer when that identity cannot be read at all. The UID check alone does not bind a
connection to the host: adbd relays every connection to the host forward, including one an emulator
guest opens through 10.0.2.2:<port>, and adbd re-resolves the abstract name per connection, so a
process that binds the name after the helper exits would receive the next command. The per-session
secret binds both directions. At startup the helper draws 32 bytes with SecureRandom and reports
them only as sessionSecret in its ready record, on the stdout of the am instrument run the host
started; the secret is never in argv, logcat, a file or a diagnostic, and it never crosses the
socket. Every connection runs an HMAC-SHA256 challenge-response before any command byte: the host
sends a nonce, the helper answers with its nonce and a server proof over both, the host checks it in
constant time and only then sends its client proof and the command, and the helper checks the client
proof with MessageDigest.isEqual before it reads the command. A listener that cannot prove the
secret gets the nonce and nothing else; the host drops that session with a typed diagnostic and
answers the call one-shot. The UID check stays as defense in depth.

Each command ends with a newline. Reads are nonblocking with poll, so the deadline holds on Android
7.0 and 7.1.0 too, where the socket timeout covers sends only. A connection gets a read deadline of
2 s from accept plus 1 s for each 64 KiB of command bytes received; bytes after the newline, and
bytes after the deadline, earn nothing. A silent peer closes at 2 s, and the largest frame can arrive
over a link of about 0.5 Mbit/s. A read that expires, or that reaches EOF before the newline, closes
without a reply. Each command frame is limited to 513 KiB on the host and in the helper, covering a
64 KiB stdin fill of JSON-escaped control characters and the largest gesture plan. The cap is decided
from the raw byte count as soon as it is crossed, so a capped prefix is never decoded, and a complete
frame is decoded strictly: malformed UTF-8 closes the connection instead of reaching the parser. An
empty or over-cap frame is answered with one error frame; any other read failure closes silently.

Every session wait takes the absolute deadline and measures its own remaining time when it starts,
through one helper, so a pause before a wait cannot extend it. Each response is one frame, built
inside the command's failure boundary, so an encoding failure returns a minimal error frame without
the success payload. The frame goes out in nonblocking sends under one deadline, fixed when the write
starts: 8 s plus the frame size at 64 KiB/s. A peer that stops reading is closed at that deadline.
The helper uses only API 23 platform classes.

Refs #4519.

Validation

Base 6e18ea3349. Head 249620200b.

  • At 249620200b, pnpm check:affected --run: passed (exactCommit=true): format, lint, typecheck, layering, fallow, native-dupes, build, vitest-related, unit. The JVM suite runs in pnpm build:android and passed. Coverage and Android smoke run in CI.
  • Secret handshake, red on 1a79bce: with the 1a79bce host, an unproven listener received snapshot snapshot-… and the touch path sent the input; with the handshake removed from the helper, all six SessionHandshakeTest cases fail.
  • Android 17 emulator (a temporary clone of the Pixel_9_Pro_XL_API_37 image on emulator-5562, because that AVD was running on emulator-5556), head APK with installed digest equal to the artifact: session snapshot then a reused snapshot on android-helper. Through the host forward, a command without the handshake got 0 bytes and a close; a valid nonce got the server hello, and a wrong client proof plus command got a close with nothing more. From inside the guest through 10.0.2.2, a raw command got 0 bytes and a nonce got only the hello. The helper PID did not change and the next snapshot reused the session.
  • 68adbe9b79 (host only): a touch command that meets an unverified listener retires the session through the timed start backoff, so later commands go one-shot without restarting the session; the new test fails on 23ff71bc9f.
  • e15bc9db48 (host only): a refused connection or a socket that closes before the peer's first byte is an ordinary transport failure with its original error; only a peer that answered without a valid proof is unverified. The new tests fail on 68adbe9b79.
  • 8eaec9b1e7 (host only, plus a Java comment): the hello is accepted only as one 64-hex nonce, one space and one 64-hex proof with the newline at byte 129, validated before any HMAC; a request that times out before a proven hello is disclosed as not dispatched. The new tests fail on e15bc9db48.
  • 249620200b (host only): the host keeps at most the 130-byte hello line from an unverified peer and refuses a missing newline at byte 129 or any byte after the line before its client proof; the new tests fail on 8eaec9b1e7.
  • Earlier rounds, still valid for the unchanged transport: Android 7.0 and API 23 runs, and real LocalSocket backpressure on a rootable Android 16 emulator.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.