Skip to content

stack-encrypt: a fieldless enum is not wiped from memory after it is encrypted #2015

Backlog

Description

@coderdan

Background

Stack Encrypt is the Rust engine in packages/stack-encrypt. After it encrypts a value, it wipes the plaintext from memory, so the value does not stay in RAM. It does this through the Zeroize trait, from the zeroize crate: keyset.encrypt accepts a type only if it implements Zeroize. Most users get the impl from #[derive(Zeroize)].

A fieldless enum is an enum whose variants are only names and contain no data, such as enum Status { Active, Suspended, Closed }. The engine's derive encrypts one as a single number: the position of the variant.

Problem

#[derive(Zeroize)] does nothing for a fieldless enum. The engine accepts the type as one that can be wiped, and the variant stays in memory.

#[derive(Encrypt, Zeroize)]
enum Diagnosis { None, Diabetes, Hiv }

keyset.encrypt(Diagnosis::Hiv).await?;
// The plaintext, the number 2, is still in memory.

Why it happens:

  1. For an enum, #[derive(Zeroize)] calls zeroize on each field of the current variant. A fieldless variant has no fields, so the generated code is an empty match arm for every variant.
  2. The engine's bound is only Zeroize. It cannot tell an impl that wipes from one that does nothing.

Verified with zeroize 1.9 and zeroize_derive 1.5: after .zeroize(), a Status::Suspended is still Status::Suspended. The expanded impl has an empty arm for each variant.

The variant is the secret. If it were not sensitive, the program would not encrypt it. The leftover is one small number for each value, and reading it needs access to the process's memory, such as a core dump. That is the case Zeroize exists for.

Where it applies:

Proposal

Decide whether the engine should accept a fieldless enum that cannot be wiped, and make the code match the decision.

Approaches to evaluate:

  1. The engine's derive writes the wipe. Stack Encrypt is getting its own derives (stack-encrypt: #[derive(Encrypt, Decrypt)] does not compile in a crate that depends only on stack-encrypt #2013). A derive on a fieldless enum could also generate a Zeroize impl. That impl would overwrite the value with one fixed variant. The user writes nothing more.

  2. Refuse an enum with no real wipe. The compiler cannot see inside a Zeroize impl, so this needs a marker of some kind. Check whether it can be done without a second trait for the user to implement.

  3. Document only. Keep the docs from fix(stack-encrypt): encrypt a derived newtype with keyset.encrypt #2014. They show how to implement Zeroize by hand for an enum:

    impl Zeroize for Diagnosis {
        fn zeroize(&mut self) {
            *self = Diagnosis::None;
        }
    }

Whatever is chosen, add a test that encrypts a fieldless enum and checks that the value is wiped afterwards.

Relationship to other work

Activity

  1. added
    bug
    Rust
    on Nov 28, 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

    RustSDKbug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions