Skip to content

Commit

Permalink
Add auto-deref workaround for ZeroizeOnDrop (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 6, 2022
1 parent 3d9cd39 commit de5f676
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
18 changes: 11 additions & 7 deletions zeroize/derive/src/lib.rs
Expand Up @@ -69,11 +69,13 @@ fn derive_zeroize(mut s: synstructure::Structure<'_>) -> TokenStream {

/// Custom derive for `ZeroizeOnDrop`
fn derive_zeroize_on_drop(mut s: synstructure::Structure<'_>) -> TokenStream {
let zeroizers = generate_fields(&mut s);
let zeroizers = generate_fields(&mut s, quote! { zeroize_or_on_drop });

let drop_impl = s.gen_impl(quote! {
gen impl Drop for @Self {
fn drop(&mut self) {
use zeroize::__internal::AssertZeroize;
use zeroize::__internal::AssertZeroizeOnDrop;
match self {
#zeroizers
}
Expand Down Expand Up @@ -251,7 +253,7 @@ impl ZeroizeAttrs {
}
}

fn generate_fields(s: &mut synstructure::Structure<'_>) -> TokenStream {
fn generate_fields(s: &mut synstructure::Structure<'_>, method: TokenStream) -> TokenStream {
s.bind_with(|_| BindStyle::RefMut);

s.filter_variants(|vi| {
Expand All @@ -265,7 +267,7 @@ fn generate_fields(s: &mut synstructure::Structure<'_>) -> TokenStream {
result
})
.filter(|bi| filter_skip(&bi.ast().attrs, true))
.each(|bi| quote! { #bi.zeroize(); })
.each(|bi| quote! { #bi.#method(); })
}

fn filter_skip(attrs: &[Attribute], start: bool) -> bool {
Expand All @@ -291,7 +293,7 @@ fn filter_skip(attrs: &[Attribute], start: bool) -> bool {

/// Custom derive for `Zeroize` (without `Drop`)
fn derive_zeroize_without_drop(mut s: synstructure::Structure<'_>) -> TokenStream {
let zeroizers = generate_fields(&mut s);
let zeroizers = generate_fields(&mut s, quote! { zeroize });

s.bound_impl(
quote!(zeroize::Zeroize),
Expand Down Expand Up @@ -507,15 +509,17 @@ mod tests {
const _DERIVE_Drop_FOR_Z: () = {
impl Drop for Z {
fn drop(&mut self) {
use zeroize::__internal::AssertZeroize;
use zeroize::__internal::AssertZeroizeOnDrop;
match self {
Z {
a: ref mut __binding_0,
b: ref mut __binding_1,
c: ref mut __binding_2,
} => {
{ __binding_0.zeroize(); }
{ __binding_1.zeroize(); }
{ __binding_2.zeroize(); }
{ __binding_0.zeroize_or_on_drop(); }
{ __binding_1.zeroize_or_on_drop(); }
{ __binding_2.zeroize_or_on_drop(); }
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions zeroize/src/lib.rs
Expand Up @@ -262,6 +262,31 @@ pub trait Zeroize {
/// Marker trait signifying that this type will [`zeroize`](Zeroize::zeroize) itself on [`Drop`].
pub trait ZeroizeOnDrop {}

#[doc(hidden)]
pub mod __internal {
use super::*;

/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
pub trait AssertZeroizeOnDrop {
fn zeroize_or_on_drop(&mut self);
}

impl<T: ZeroizeOnDrop> AssertZeroizeOnDrop for &mut T {
fn zeroize_or_on_drop(&mut self) {}
}

/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
pub trait AssertZeroize {
fn zeroize_or_on_drop(&mut self);
}

impl<T: Zeroize> AssertZeroize for T {
fn zeroize_or_on_drop(&mut self) {
self.zeroize()
}
}
}

/// Marker trait for types whose `Default` is the desired zeroization result
pub trait DefaultIsZeroes: Copy + Default + Sized {}

Expand Down

0 comments on commit de5f676

Please sign in to comment.