Skip to content

Keep the pairing builders, notes and PSK minting off the aborting heap - #239

Merged
kahrendt merged 2 commits into
mainfrom
pairing-bounded
Nov 4, 2026
Merged

kahrendt merged 2 commits into
mainfrom
pairing-bounded

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Group C of the allocation follow-ups (Sendspin/backlog#92): the pairing paths still built heap temporaries for every base64 field and every pairing note, and minting a new long-term PSK aborted the process if noise-c could not allocate the hash state. On ESPHome operator new is internal-RAM-only and aborts, so these run on the protocol task during every pairing exchange.

Change

  • Builders: a file-local write_b64url() in protocol.cpp encodes each base64 field into a stack buffer (at most 89 bytes), assigns it to the document, which copies it, and wipes the buffer. All eight pairing fields use it, replacing b64url_encode() temporaries and their hand-written wipes.
  • Pairing notes: PairingNote::text is an IdText (a static_assert checks every emitted code fits); note_display_pairing_code() takes a std::string_view like its siblings; the main-loop drain builds the listener's std::string, per the conventions.md rule.
  • PSK minting: RecordStore::resolve_pairing_outcome() returns std::optional. It mints the PSK straight into the self-wiping PairingOutcome and derives its psk_id with the fallible psk_id_for(ptr, len); both pairing flows answer a failure with pair/abort (METHOD_NOT_SUPPORTED, like the other internal failures). The aborting std::array overload now runs only while start() loads the record store.
  • Docs: internals.md "Memory" updates the exception sentence and the pairing row.

platform_random_bytes() still aborts when noise-c cannot allocate its random state; that is left for a separate change.

Tests

  • AbortAfterConfirmDoesNotReclearAlreadyWithdrawnCode now asserts the failure names the attempt's server.
  • Mutants killed: a field encoded one byte short, and an empty text for each of the started, succeeded, failed and code notes.
  • Gap: the hash-failure path needs a noise-c allocation fault to reach, so it is untested.

- The pairing builders encode each base64 field through write_b64url(),
  a stack buffer the document copies from and that is wiped afterwards,
  in place of heap std::string temporaries.
- PairingNote::text is an IdText; note_display_pairing_code() takes a
  string_view like its siblings, and the main-loop drain builds the
  listener's std::string.
- resolve_pairing_outcome() mints the PSK into the self-wiping outcome
  and derives its psk_id with the fallible psk_id_for() overload; a
  pairing that cannot hash its PSK fails with pair/abort instead of
  aborting the process.
- internals.md "Memory" names the remaining aborting paths.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The base64 comment still references the removed pairing-PSK encoding path and needs to state the general requirement to wipe returned strings when encoding secrets.

1 open issue
What changed in this PR

Reduces abort-prone heap use in Sendspin’s protocol-task pairing paths, particularly on ESPHome, while preserving existing listener interfaces.

Changes:

  • Encode pairing fields through wiped stack buffers and hold queued note text inline.
  • Make pairing outcomes fallible and abort pairing cleanly when PSK identifier hashing fails.
  • Adapt tests and update documentation for the allocation behavior.
File Description
tests/​test_record_store.cpp Adapt successful pairing tests to optional outcomes.
tests/​test_pairing_state_machine.cpp Verify pairing failures identify the attempt’s server.
src/​record_store.h Declare optional pairing outcomes and document failure.
src/​record_store.cpp Mint secrets into self-wiping storage and handle hash failure.
src/​protocol.cpp Replace base64 string temporaries with wiped stack buffers.
src/​crypto/​keys.h Document hash failure in fallible PSK identifier derivation.
src/​connection_manager_pairing.cpp Handle minting failure in both pairing flows.
src/​client.cpp Store note text inline and build listener strings during draining.
include/​sendspin/​client.h Change the private code-note parameter to std::string_view.
docs/​internals.md Update pairing allocations and remaining abort behavior.

Review effort: Max

Comment thread src/protocol.cpp Resolved
@kahrendt
kahrendt merged commit ea5dcaa into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the pairing-bounded branch November 4, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants