Repository navigation
Description
Background
Stack Encrypt (packages/stack-encrypt) is the Rust encryption engine behind every CipherStash SDK. A match index stores a Bloom filter over the tokens of a text value, and a text search query sends a filter over the query's text. The typed query chain (cipher.query(op::Matches, "ali").on(&plan)) owns a copy of the query text while it derives that filter. The engine's rule is that any plaintext it owns is wiped before the memory is freed, so a freed heap block never holds a secret. tests/heap_hygiene.rs watches every free during an operation and fails on a block that still holds the plaintext.
Problem
The term operations are generated by term_operation! in packages/stack-encrypt/src/target/operations.rs. Its consume form (equality, the order terms) hands the owned value to the derivation, which wipes it. Its view form (matching) borrows the value as &str, derives the filter, and then lets the closure drop the source. In Owned mode the source is a String, and String::drop does not wipe. So the query text of a typed Matches query is freed with the plaintext in it.
Verified by running: a heap-hygiene watch over cipher.query(op::Matches, &text).on(&plan) reports "27 bytes freed unwiped" in operations::matching::{{closure}}, through Input::derive and QueryOp::prepare. The same watch over op::Eq is clean. The dynamic path is not affected: its match derivation reads a ValueFrame, and the frame wipes itself.
Nothing catches it today because the heap-hygiene suite watched only the dynamic query_term. PR #1592 adds a watch over the typed equality query; the typed match query is left out of that test with a pointer to this issue.
Proposal
- Wipe the source in the
viewform ofterm_operation!once the term is derived, asjson_termsdoes withM::release(source, |value| drop(PlainJson::protect(value))). A text source needs a wipe that works throughAsRef<str>: bound theviewform'sSonZeroizewhere every caller is owned text, or giveSourceMode::releasea wipe the mode supplies. - Extend
a_typed_query_leaves_no_plaintext_on_the_heapintests/heap_hygiene.rstoop::Matches, and keep it failing until the wipe lands. - Check the other
view-form operations for the same drop.
Activity
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation
fix(stack-encrypt): address the review of the query preparation