Skip to content

stack-encrypt: wipe what serde reads whole when an OpaqueBlob decrypt is refused #1754

Open

Description

@coderdan

Background

Stack Encrypt (packages/stack-encrypt) is the Rust encryption engine that every CipherStash SDK runs on. Decrypting an OpaqueBlob<T> turns the decrypted bytes back into a Rust value with serde.

A blob can be a valid ciphertext of a different shape from T, so the read fails part-way. Since #1676 (PR #1750), stack-encrypt reads the bytes "in place" into a blank T::default() that is wiped if the read fails part-way. So for an ordinary derived struct, every field read before the failure is wiped.

Problem

serde still builds some values in a temporary of its own, and stores them in T only once they are complete. If the read fails inside one of them, the pieces already built are freed without being wiped, and those pieces can hold decrypted text. This happens for:

  • an element of a Vec or other sequence, a map entry, an Option, a Box, and an enum;
  • a field read with #[serde(deserialize_with)] or #[serde(with)];
  • a struct marked #[serde(transparent)], from or try_from, one with a #[serde(flatten)] field, and one whose every field uses deserialize_with. serde reads these whole wherever they appear, so when T itself is one of them, nothing is wiped;
  • a type with a hand-written Deserialize that does not implement deserialize_in_place.

Separately, serde reserves room for at most 1 MiB of a sequence's elements up front. A longer sequence grows its buffer while reading, and each old buffer is freed unwiped, even when decryption succeeds. Integers and other inline values are left in those old buffers.

The lifecycle conformance profile records all of this as the native-partial-decode-inside-whole-values known gap. It is pinned by two failing-on-purpose heap tests in packages/stack-encrypt/tests/heap_hygiene.rs.

Proposal

  1. For each case, decide whether stack-encrypt can close it. Some options:

    • an in-place read of Option or Box when the inner type is Default;
    • an element seed that wipes a part-built element;
    • refusing the unsupported derive shapes at compile time.

    Otherwise, keep it as a documented limit that the caller handles with parts that wipe themselves on drop (Zeroizing<String>, ZeroizeOnDrop).

  2. For the 1 MiB regrowth, look at a fix in serde or in Vitamin C's storage reader, or an opt-in wiping sequence type.

  3. This is done when every case is closed, with its negative control turned into passing evidence, or is kept as a documented limit with the reason written in the lifecycle contract.

Activity

  1. added
    enhancement
    rust
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SDKenhancementrust

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions