Repository navigation
Add a zeroizing allocator to the zeroize crate #1600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rlee287
wants to merge
3
commits into
RustCrypto:master
Choose a base branch
from
rlee287:zeroizing_allocator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+496
−1
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| name: zeroize_allocator | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - ".github/workflows/zeroize_allocator.yml" | ||
| - "zeroize_allocator/**" | ||
| - "Cargo.*" | ||
| push: | ||
| branches: master | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| defaults: | ||
| run: | ||
| working-directory: zeroize_allocator | ||
|
|
||
| env: | ||
| CARGO_INCREMENTAL: 0 | ||
| RUSTFLAGS: "-Dwarnings" | ||
|
|
||
| # Cancels CI jobs when new commits are pushed to a PR branch | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| test: | ||
| strategy: | ||
| matrix: | ||
| include: | ||
| # 32-bit Linux | ||
| - target: i686-unknown-linux-gnu | ||
| platform: ubuntu-latest | ||
| rust: 1.100.0 # MSRV | ||
| deps: sudo apt update && sudo apt install gcc-multilib | ||
| - target: i686-unknown-linux-gnu | ||
| platform: ubuntu-latest | ||
| rust: stable | ||
| deps: sudo apt update && sudo apt install gcc-multilib | ||
|
|
||
| # 64-bit Linux | ||
| - target: x86_64-unknown-linux-gnu | ||
| platform: ubuntu-latest | ||
| rust: 1.100.0 # MSRV | ||
| - target: x86_64-unknown-linux-gnu | ||
| platform: ubuntu-latest | ||
| rust: stable | ||
|
|
||
| # 64-bit Windows | ||
| - target: x86_64-pc-windows-msvc | ||
| platform: windows-latest | ||
| rust: 1.100.0 # MSRV | ||
| - target: x86_64-pc-windows-msvc | ||
| platform: windows-latest | ||
| rust: stable | ||
| runs-on: ${{ matrix.platform }} | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: RustCrypto/actions/cargo-cache@master | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.rust }} | ||
| targets: ${{ matrix.target }} | ||
| - run: ${{ matrix.deps }} | ||
| - run: cargo test --target ${{ matrix.target }} | ||
|
|
||
| # Test using `cargo careful` | ||
| test-careful: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: dtolnay/rust-toolchain@nightly | ||
| - run: cargo install cargo-careful | ||
| - run: cargo careful test --all-features | ||
|
|
||
| # Cross-compiled tests | ||
| test-cross: | ||
| strategy: | ||
| matrix: | ||
| include: | ||
| # AArch64 | ||
| - target: aarch64-unknown-linux-gnu | ||
| rust: 1.100.0 | ||
| - target: aarch64-unknown-linux-gnu | ||
| rust: stable | ||
|
|
||
| # PPC32 | ||
| - target: powerpc-unknown-linux-gnu | ||
| rust: 1.100.0 # MSRV | ||
| - target: powerpc-unknown-linux-gnu | ||
| rust: stable | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: RustCrypto/actions/cargo-cache@master | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.rust }} | ||
| targets: ${{ matrix.target }} | ||
| - uses: RustCrypto/actions/cross-install@master | ||
| - run: cross test --target ${{ matrix.target }} | ||
| - run: cross test --target ${{ matrix.target }} --all-features | ||
|
|
||
| # Custom derive tests | ||
| test-derive: | ||
| runs-on: ubuntu-latest | ||
| defaults: | ||
| run: | ||
| working-directory: zeroize_derive | ||
| strategy: | ||
| matrix: | ||
| rust: | ||
| - 1.100.0 # MSRV | ||
| - stable | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: RustCrypto/actions/cargo-cache@master | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.rust }} | ||
| - run: cargo test | ||
| - run: cargo test --release | ||
|
|
||
| # Test using `cargo miri` | ||
| test-miri: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| MIRIFLAGS: "-Zmiri-symbolic-alignment-check -Zmiri-strict-provenance" | ||
| strategy: | ||
| matrix: | ||
| target: | ||
| - x86_64-unknown-linux-gnu | ||
| - s390x-unknown-linux-gnu | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: dtolnay/rust-toolchain@stable | ||
| with: | ||
| toolchain: nightly-2026-08-23 | ||
| - run: rustup component add miri | ||
| - run: cargo miri test --target ${{ matrix.target }} --all-features | ||
|
|
||
| # Tests for x86-64 `simd` support | ||
| test-x86-simd: | ||
| strategy: | ||
| matrix: | ||
| include: | ||
| - target: x86_64-unknown-linux-gnu | ||
| rust: 1.100.0 | ||
| - target: x86_64-unknown-linux-gnu | ||
| rust: stable | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: RustCrypto/actions/cargo-cache@master | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.rust }} | ||
| targets: ${{ matrix.target }} | ||
| - uses: RustCrypto/actions/cross-install@master | ||
| - run: cargo test --target ${{ matrix.target }} --features simd | ||
| - run: cargo test --target ${{ matrix.target }} --all-features | ||
|
|
||
| # Test WASM using `wasmtime` | ||
| test-wasm: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| CARGO_TARGET_WASM32_WASIP1_RUNNER: "wasmtime" | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: bytecodealliance/actions/wasmtime/setup@v1 | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: stable | ||
| targets: wasm32-wasip1 | ||
| - run: cargo test --target wasm32-wasip1 --all-features | ||
| - run: cargo test --target wasm32-wasip1 --all-features --release | ||
|
|
||
| minimal-versions: | ||
| uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master | ||
| with: | ||
| working-directory: ${{ github.workflow }} | ||
|
|
||
| no_std: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| rust: | ||
| - 1.100.0 # MSRV | ||
| - stable | ||
| target: | ||
| - armv7a-none-eabi | ||
| - thumbv7em-none-eabi | ||
| - wasm32-unknown-unknown | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: RustCrypto/actions/cargo-cache@master | ||
| - uses: dtolnay/rust-toolchain@master | ||
| with: | ||
| toolchain: ${{ matrix.rust }} | ||
| targets: ${{ matrix.target }} | ||
| - run: cargo build --no-default-features --target ${{ matrix.target }} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "zeroize_allocator" | ||
| version = "0.1.0" | ||
| authors = ["The RustCrypto Project Developers"] | ||
| edition = "2024" | ||
| rust-version = "1.100" | ||
| documentation = "https://docs.rs/zeroize_allocator" | ||
| repository = "https://github.com/RustCrypto/utils" | ||
| license = "Apache-2.0 OR MIT" | ||
| keywords = ["memory", "memset", "secure", "volatile", "zero"] | ||
| categories = ["cryptography", "memory-management", "no-std", "os"] | ||
| description = """ | ||
| A wrapping allocator that uses zeroization functions to zeroize memory upon deallocation. | ||
| """ | ||
|
|
||
| [dependencies] | ||
| zeroize = "1.9" | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #![no_std] | ||
| //! This crate provides a `ZeroizingAllocator<A>` that zeroizes memory on deallocation. | ||
| //! | ||
| //! ``` | ||
| //! use zeroize_allocator::ZeroizingAllocator; | ||
| //! | ||
| //! use std::alloc::System; | ||
| //! | ||
| //! let alloc_helper = ZeroizingAllocator::new(System); | ||
| //! let mut vec_example = Vec::new_in(&alloc_helper); | ||
| //! vec_example.extend([2, 3, 5, 7, 11, 13, 17, 19]); | ||
| //! ``` | ||
|
|
||
| use zeroize::Zeroize; | ||
|
|
||
| use core::{ | ||
| alloc::{AllocError, Allocator, Layout}, | ||
| mem::MaybeUninit, | ||
| ptr::NonNull, | ||
| slice, | ||
| }; | ||
|
|
||
| /// An allocator that zeroizes memory before freeing it. | ||
| /// The soundness of this allocator depends on adjustments of the Allocator trait | ||
| /// safety contract: see <https://github.com/rust-lang/rust/pull/165921> for details. | ||
| #[repr(transparent)] | ||
| #[derive(Debug)] | ||
| pub struct ZeroizingAllocator<A: Allocator> { | ||
| inner: A, | ||
| } | ||
| impl<A: Allocator> ZeroizingAllocator<A> { | ||
| /// Constructs a [`ZeroizingAllocator`] around a pre-existing allocator. | ||
| pub fn new(allocator: A) -> Self { | ||
| Self { inner: allocator } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * SAFETY: Most of the safety invariants are expected to be upheld by the underlying allocator. | ||
| * For the rest, see the safety comments inside this impl. | ||
| */ | ||
| unsafe impl<A: Allocator> Allocator for ZeroizingAllocator<A> { | ||
| fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
| self.inner.allocate(layout) | ||
| } | ||
|
|
||
| unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { | ||
| // Use scoping to ensure that the slice is dead when we call the underlying allocator. | ||
| { | ||
| /* | ||
| * SAFETY: | ||
| * - The memory is being deallocated -> we are not bound to avoid touching it | ||
| * - The memory is being deallocated -> no references to it still exist | ||
| * - ptr is valid and not null. | ||
| * - MaybeUninit<u8> has the same size, alignment, and ABI as u8. | ||
| * - The layout gives us the size of the memory allocation, so the zeroize only | ||
| * writes to memory belonging to this allocation. | ||
| * Thus, we can create a &mut [MaybeUninit<u8>] and zeroize it without causing UB. | ||
| * | ||
| * We need to use MaybeUninit<u8> instead of u8 to handle uninitialized padding bytes. | ||
| */ | ||
| let allocated_slice: &mut [MaybeUninit<u8>] = | ||
| unsafe { slice::from_raw_parts_mut(ptr.as_ptr().cast(), layout.size()) }; | ||
| // This is a necessary safety comment and needs to be updated as such. | ||
| #[allow(clippy::unnecessary_safety_comment)] | ||
| /* | ||
| * SAFETY: the only panic inside [Z]::zeroize() is an assert that the slice length is | ||
| * smaller than isize::MAX, which is a precondition of constructing a Layout value. | ||
| * (The checked_mul(size_of::<u8>) is a multiplication by 1, so it cannot fail.) | ||
| * Therefore, this zeroize call cannot panic. | ||
| */ | ||
| allocated_slice.zeroize(); | ||
| } | ||
| // Now do the actual deallocation | ||
| // SAFETY: we assume inner's deallocate is sound | ||
| unsafe { self.inner.deallocate(ptr, layout) } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.