Skip to content

harden(l1): equalise answer timing, expire captured hellos, canary the gate - #1

Merged
croc100 merged 1 commit into
mainfrom
harden/answer-timing-replay-freshness-gate-canaries
Oct 3, 2026
Merged

croc100 merged 1 commit into
mainfrom
harden/answer-timing-replay-freshness-gate-canaries

Conversation

@croc100

@croc100 croc100 commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Closes three L1 relay distinguishers and fixes three defects found while closing them. Full narrative in docs/CHANGELOG.md; backlog of what remains in docs/ROADMAP.md (H1–H25).

Weaknesses closed

  • Answer timing. A relay answered tag-holders from local state but spliced everyone else to the borrowed site, splitting one address into two latency modes — a clock-only distinguisher that survived every byte-shape measurement. ProfileSite now measures the site's answer time and the relay waits it out (from hello arrival, so the key exchange comes out of the budget), capped so a slow/hostile site can't stall every handshake. cmd/probe captures a third unauthenticated session and reports the residual (the splice's TCP connect) side by side rather than assuming it away.
  • Replay freshness. A cache of seen randoms was a memory budget that expired, so a captured hello just had to outwait the TTL. The tag now also covers a coarse clock window (an HMAC input, nothing new on the wire), so a captured hello expires on its own. Out-of-window clients fail with ErrRelayRejected naming the clock, instead of the splice's raw AEAD failure. The cache also no longer collapses at its ceiling — rotating generations evict only the oldest, which the freshness window makes safe.
  • Canaries. Probe tests all asserted "finds nothing", indistinguishable from a harness that silently broke. Harness and probe binary are now pointed at deliberately-wrong relays and required to fail, one canary per measurement. The first run caught a 512-byte tolerance inherited from the cert-flight comparison that had made the post-handshake check inert.

Defects found on the way

  • TCP teardown mirroring never worked on Windows (WSAECONNRESET vs the synthetic syscall.ECONNRESET), so a Windows relay FIN'd against a site sending RST — invisible because CI is Linux-only. Detection is per-platform now, with a test.
  • The splice could truncate the site's answer under load by racing the grace timer; teardown now waits on the site's direction by name, bounded.
  • A gofmt check that cannot run on a CRLF checkout — recorded, not fixed.

Not yet fixed (noted in ROADMAP)

Two distinguishers found while reading this code: the relay hardcodes its ServerHello cipher suite instead of echoing the site's (H4), and the splice ignores the ClientHello SNI while the docs claim it honours it (H5).

Verification

go build, go vet, go test ./..., gofmt, and hack/smoke.sh (including its new gate-failure canary) all pass. Not verified locally: -race (needs cgo/a C toolchain this machine lacks); CI covers it.

🤖 Generated with Claude Code

…e gate

Three weaknesses closed and three defects found while closing them. Full
narrative in docs/CHANGELOG.md; the reasoning that does not fit a diff is
below.

Answer timing. A relay has two answer paths costing structurally different
amounts of time: a client holding a valid tag is answered out of local state,
while everyone else is spliced, which costs a TCP connection to the borrowed
site plus that site's own answer time. Connections to one relay address
therefore split into two latency modes, and no real web server does that. It
needs no decryption, no key and no active probing — a clock is enough — and it
survived every shape measurement because shape was only ever counted in bytes.
ProfileSite now measures how long the site takes to answer and the relay waits
that out, from the hello's arrival so the key exchange comes out of the budget
rather than being added to it, and capped so a slow or hostile site cannot turn
every handshake into a stall. Measured: 510us against a site taking 150ms.

This narrows the gap rather than closing it, and the gate says so. The splice
still pays the TCP connect that the profile's measurement does not include, so
cmd/probe captures a third, unauthenticated session against the relay and
reports the two paths side by side. The residual is measured, not assumed.

Replay freshness. The tag is a function of the hello, so a verbatim replay
verifies by construction and a cache of seen randoms was what refused it. But a
cache is a memory budget and every memory budget expires: a censor holding one
captured hello only had to wait out the TTL, and waiting is free, so the TTL
was the real lifetime of the defence. The tag now also covers a coarse clock and
a relay accepts only adjacent windows, so a captured hello expires on its own.
Nothing changes on the wire — the window is an input to the HMAC, not a field.
The cost is stated rather than hidden: a client whose clock is wrong past the
slack is silently unreachable, because an out-of-window tag has to be answered
exactly like a forged one. Dial now fails with ErrRelayRejected naming the clock
among the causes, instead of the raw AEAD failure being spliced used to produce.

The cache also stopped collapsing. At its ceiling it used to empty itself, so a
relay busy enough to fill it inside one TTL periodically forgot everything and
ran unprotected until it refilled — a cliff that appears on a relay doing well,
not one under attack. Entries now live in rotating generations and the ceiling
costs the oldest only. The freshness window is what makes that safe.

Canaries. Every test in internal/network/probe asserted the harness finds
nothing, which is also what a harness returns once it has quietly stopped
working, and the gate downstream keeps certifying relays either way. The harness
and the probe binary are now pointed at deliberately-wrong relays and required
to fail, one canary per measurement so none can be satisfied by an unrelated
failure. The first run earned its keep: the post-handshake comparison had
inherited a 512-byte tolerance from the certificate-flight comparison, and that
window is only a few hundred bytes in total, so a relay sending no session
tickets at all was inside tolerance. That measurement had been inert.

Defects found on the way:

- TCP teardown mirroring never worked on Windows. A peer reset arrives as
  WSAECONNRESET while syscall.ECONNRESET there is a synthetic value no socket
  returns, so the check matched nothing and a Windows relay closed every spliced
  connection with a FIN against a site sending RST. This is a distinguisher
  cmd/probe found and fixed once already, on a platform most of the residential
  pool this design bets on would run, invisible because CI is Linux-only.
  Detection is per-platform now and has its own test.
- The splice truncated the site's answer under load. Teardown waited for
  whichever direction finished first, then allowed a fixed 200ms; when the site
  closed straight after answering, the client-to-site copy could win that race
  and start a grace the response had to beat. It now waits on the site's
  direction by name, bounded, because a site always finishes what it started.
- A gofmt check that cannot run on a CRLF checkout, i.e. any Windows clone.
  Recorded rather than fixed.

What is owed but not done is in docs/ROADMAP.md's new hardening backlog, H1-H25,
including two distinguishers found while reading this code and not yet fixed:
the relay hardcodes its ServerHello cipher suite instead of echoing the one the
borrowed site negotiated (H4), and the splice ignores the ClientHello's SNI
while the docs claim it honours it (H5).

Verified: go build, go vet, go test ./..., gofmt, and hack/smoke.sh including
its new gate-failure canary. Not verified locally: -race, which needs cgo and a
C toolchain this machine lacks; CI covers it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@croc100
croc100 merged commit ffdc685 into main Oct 3, 2026
2 checks passed
@croc100
croc100 deleted the harden/answer-timing-replay-freshness-gate-canaries branch October 3, 2026 01:25
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.

1 participant