Skip to content

Add an opt-in zeroize feature that wipes codec buffers - #201

Open
MattJackson wants to merge 2 commits into
blackbeam:masterfrom
MattJackson:upstream/codec-zeroize
Open

MattJackson wants to merge 2 commits into
blackbeam:masterfrom
MattJackson:upstream/codec-zeroize

Conversation

@MattJackson

@MattJackson MattJackson commented Nov 14, 2026 •

Copy link
Copy Markdown

Add an opt-in zeroize feature for best-effort wiping of the packet codec's buffers.

With zeroize enabled the codec overwrites with zeros: bytes consumed from the src buffer of PacketCodec::decode (before the buffer advances), the compressed codec's staging buffers on clear / regrow (old allocation wiped after copying) / drop, the compress input after compression, and partial output left in spare capacity by a failed inflate/deflate. Without the feature behaviour is unchanged. CI runs the codec tests with the feature on.

Limitations (also documented on PacketCodec, README and crate docs):

  • the flate2 Compress/Decompress state, including the zlib window (up to 32 KiB of recent plaintext), is not wiped; flate2 offers no hook for it;
  • caller-owned output buffers are not wiped: the decoded packet in decode's dst (including earlier allocations it releases when it grows, BytesMut or Vec<u8>), and the encoded bytes in encode's dst (if the codec itself grows that dst, the old allocation is wiped);
  • the packet given to encode, and anything outside the codec (sockets, kernel, caller copies), is out of scope.

The tests install a #[global_allocator] that applies to all lib tests when built with --features "test zeroize"; it only delegates to System and is noted in zeroize_tests.rs.

With the new `zeroize` feature the packet codec overwrites protocol bytes with zeros
once it is done with them:

* bytes consumed from the input buffer are wiped before the buffer advances past them
  (plain chunks and compressed envelopes);
* `compress` wipes its input before clearing it;
* buffers that need to grow move into a new allocation and the old one is wiped first,
  instead of `BytesMut::reserve` releasing or reusing it as is;
* partial output left in spare capacity by a failed inflate or deflate is wiped;
* the compressed codec wipes its staging buffer when encoding fails, and both of its
  buffers on drop.

Without the feature the code is unchanged in behaviour. CI runs the codec tests with the
feature enabled.
Replace the broad claim that the codec's buffers are wiped with an exact list of what
is overwritten and what is not: the flate2 compressor/decompressor state (zlib window)
and caller-owned dst buffers are not wiped. Mention the test-only global allocator.
Documentation only; no behaviour change.
@MattJackson

Copy link
Copy Markdown
Author

I tightened the docs; the wiping logic is unchanged. This adds a precise list of what is and is not wiped to the PacketCodec docs (flate2 internal state and caller-owned dst are explicitly not covered), rewords the feature-table row to "best-effort wiping of the packet codec's buffers", notes the test global allocator in zeroize_tests.rs, and updates the PR description to match.

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