Clear the PEM parser's internal base64 buffers when the section is a private key

Open
#120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
security

Research direction

Start by locating SliceIter and pem::from_buf in the PEM parser, then trace how their buffers grow and are dropped for private key sections. Clear sensitive buffer contents before those operations, using the crate's existing zeroize support where it fits. Done when parser-owned buffers no longer retain private key text after clearing, regrowth, or drop; the reporter offers a probe for validation.

Written by the indexing model from the issue text.

Description

The PEM parser's internal buffers are dropped without being cleared. This holds for SliceIter's b64_buf and for the line and b64buf buffers in pem::from_buf. When the section is a private key, those buffers held the key's base64 text. Each reallocation as a buffer grows, and the final drop, return a block that still holds part of the encoded key to the allocator.

We measured this at 1.15.1 with a counting allocator that forces every growth to move. For a private key of about RSA-4096 size, from_pem_slice left 2–3 freed blocks containing a base64 marker, and the from_buf loop up to 5. In both runs our own input buffer and the decoded DER were wiped, so these blocks are the parser's. A forced move over-counts compared with an allocator that can grow in place, so these figures are upper bounds.

This is separate from #114 (clearing PrivateKeyDer on drop), which notes that adding Drop to a public type is a breaking change. These buffers are internal, so clearing them before they are dropped or regrown changes no public API. The decoding is already constant-time for private keys (decode_secret), so this would extend the same care to the buffers around it.

A possible shape, for whatever fits the crate best: when the section kind is a private key, clear the buffer (for example with zeroize, which the alloc feature already pulls in) before it is cleared, regrown or dropped. A pre-sized buffer would avoid regrowth copies.

We are happy to test a change against our probe.

Thank you,
Tim
Founder, Synergy Geeks

Dominant language
Rust
Stars
42
Forks
55
Avg merge
3d 7h
Merged PRs (30d)
4

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rustls/pki-types

All issues in rustls/pki-types

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.