Skip to content

OwnedKeyMaterial tracks uses outside steps (spec 0004, entry 4) - #33

Merged
matthewjones372 merged 3 commits into
mainfrom
key-material-uses-outside-steps
Dec 1, 2026
Merged

matthewjones372 merged 3 commits into
mainfrom
key-material-uses-outside-steps

Conversation

@matthewjones372

@matthewjones372 matthewjones372 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #32. Entry 4 of spec 0004, key-material-uses-outside-steps.

What changes

OwnedKeyMaterial.use no longer registers the use in Shared. It uses two JDK structures, each commented with spec 0004:

  • inUse (AtomicInteger): a use adds itself, copies the source, puts the copy in copies, then reads the state. Revoked, zeroed or a closed view means wipe the copy, take itself out, and answer Denied.
  • copies (ConcurrentHashMap): revoke commits its state as before, then wipes every copy in the map.

Each step (forget, close a view, revoke) commits, then reads inUse. A use takes itself out, then reads the state. When forgotten material is left with no views, whichever side goes second, the step or the last use out, commits zeroed in one guarded step and wipes the source. That is the only time a use commits a step.

One ordinary use of a held key view now commits 0 steps (it was 2 after #32). KeyViewUseCostSuite's ceiling is 0; it failed at 2 before the change.

Other changes:

  • refused reads the clock before the state. A revoke made at that clock read is then still seen and answers Denied, which outranks expiry as before.
  • The forgotten-material path now takes one extra step: close or forget, then the zeroing step, then the acknowledgement.

Bench

./gradlew :surefold-tenancy-crypto-bench:run, paired runs alternating this branch and #32 on the same machine, load average 177 to 245 on 8 cores.

Run 1 caller p50 / p99 8 callers p50 / p99
this, 1 0.46 / 5.42 µs 0.46 / 2.58 µs
#32, 1 3.58 / 138.00 µs 1.79 / 213.29 µs
this, 2 0.54 / 4.92 µs 0.46 / 3.92 µs
#32, 2 2.83 / 85.25 µs 2.38 / 1071.42 µs

Unlike #32, p99 is now steady across runs even at this load. Max is scheduler noise.

Stories

The two the spec names:

  • revocation during use admission wipes its copy and refuses before the callback: the revoke is made from the view's own clock, at the read after the copy. That is the point where the test holds control (spec question 2). It answers Denied, the callback never runs, and the source is wiped. revocation during an admitted callback refuses its normal answer (unchanged) still shows the copy in use is wiped. Stress: revocation racing uses either wipes and refuses or answers from an intact copy, 100 rounds of 4 uses against a revoke.
  • the last use out of forgotten material wipes the source and notifies once: forget and close inside the callback. The source survives until the use leaves, the answer stands, then the source is wiped and notified once. Stress: forgetting during concurrent uses wipes the source only after the last use, before its notice, 100 rounds of 4×20 uses against forget and close.

That stress story first asserted one notice, and it failed 4 times in 100 rounds with two. The close step and the last use out can both reach the notice. Notices are at least once by design (a parallel acknowledgement survives another notice attempt failing requires parallel attempts, and the ledger's onTerminal is idempotent). The zeroing is a single guarded step. The story now asserts that every notice found the source already wiped, and that no use answered from a wiped copy.

Removed:

  • a committed use with throwing instrumentation releases its unpublished use: a use commits no step, so there is no instrumentation to fault.

Rewritten:

  • expiry during use admission refuses before the callback: it cut off on the use's commit. It now returns an expired time from the view's clock at the read after the copy.
  • revocation during use admission refuses before the callback → the first named story above.
  • a committed terminal acknowledgement survives instrumentation failure: faults commit 2, not 1, because close, zeroing and acknowledgement are now three steps.

Found in review

An independent review found a gap. releaseTerminal read inUse before the zeroing step, and the step did not read it again. In that gap, a view borrowed on forgotten material could admit a use and then be closed. The stale zeroing then committed under that use, and the use answered Denied after a benign close. No bytes were exposed, because the use's copy is private. The zeroing step now reads the count inside its own transaction. It commits only if nothing committed since its snapshot, and that snapshot had no views, so any admitted use had already counted itself. No story pins this interleaving: it needs a borrow between a close's commit and its zeroing, and nothing there is a point where the test holds control.

The review also confirmed KeyCallers admission (no call admitted after close returns, and the count never leaks), torn-copy refusal, and revoke reaching every copy in flight.

Verified

  • Locally, after the review fix: ./gradlew spotlessApply && ./gradlew :surefold-tenancy-crypto:build is green, all 257 tests including the Postgres suites, with detekt and Kover.

🤖 Generated with Claude Code

matthewjones372 and others added 3 commits December 1, 2026 10:41
A use counts itself in an atomic, copies the source into a concurrent
map, then reads the state. Revoke commits as before and then wipes every
mapped copy. The step that leaves forgotten material with no views, or
the last use out after it, zeroes the source. An ordinary use of a held
key view now commits no step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A view borrowed and closed between releaseTerminal's read of inUse and
the zeroing step could admit a use that the stale read missed; the
source was then zeroed under it and its answer turned to Denied. The
step now reads the count itself, so it commits only with no use in
flight. Found in review of #33.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matthewjones372
matthewjones372 changed the base branch from key-callers-atomic-count to main December 1, 2026 11:40
@matthewjones372
matthewjones372 merged commit 5c7c8f5 into main Dec 1, 2026
1 check passed
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