Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on:
'inout-v*',
'sponge-cursor-v*',
'zeroize-v*',
'zeroize_allocator-v*',
'zeroize_derive-v*',
'zlint-v*'
]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.92.0 # Pinned to prevent breakages
toolchain: 1.100.0 # Pinned to prevent breakages
components: clippy
- run: cargo clippy --workspace --all-features --lib --bins --tests -- -Dwarnings

Expand Down
163 changes: 163 additions & 0 deletions .github/workflows/zeroize_allocator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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

# 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-09-26
- run: rustup component add miri
- run: cargo miri 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 }}
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"sponge-cursor",
"wycheproof2blb",
"zeroize",
"zeroize_allocator",
"zeroize_derive",
"zlint"
]
Expand Down
20 changes: 20 additions & 0 deletions zeroize_allocator/Cargo.toml
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
78 changes: 78 additions & 0 deletions zeroize_allocator/src/lib.rs
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 + ?Sized> {
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 + ?Sized> 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();
Comment thread
tarcieri marked this conversation as resolved.
Resolved
}
// Now do the actual deallocation
// SAFETY: we assume inner's deallocate is sound
unsafe { self.inner.deallocate(ptr, layout) }
}
}
Loading
Loading