Skip to content

argon2: detect allocation failures in hash_password_into #568

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

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions argon2/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,43 @@ impl Zeroize for Block {
self.0.zeroize();
}
}

/// Custom implementation of `Box<[Block]>` until `Box::try_new_zeroed_slice` is stabilized.
#[cfg(feature = "alloc")]
pub(crate) struct Blocks {
p: core::ptr::NonNull<Block>,
len: usize,
}

#[cfg(feature = "alloc")]
impl Blocks {
pub fn new(len: usize) -> Option<Self> {
use alloc::alloc::{Layout, alloc_zeroed};
use core::ptr::NonNull;

let layout = Layout::array::<Block>(len).ok()?;
// SAFETY: e use `alloc_zeroed` correctly
let p = unsafe { alloc_zeroed(layout) };

let p = NonNull::new(p.cast())?;
Some(Self { p, len })
}

pub fn as_slice(&mut self) -> &mut [Block] {
// SAFETY: `self.p` is a valid non-zero pointer that points to memory of the necessary size
unsafe { slice::from_raw_parts_mut(self.p.as_ptr(), self.len) }
}
}

#[cfg(feature = "alloc")]
impl Drop for Blocks {
fn drop(&mut self) {
use alloc::alloc::{Layout, dealloc};
// SAFETY: layout was checked during construction
let layout = unsafe { Layout::array::<Block>(self.len).unwrap_unchecked() };
// SAFETY: we use `dealloc` correctly with the previously allocated pointer
unsafe {
dealloc(self.p.as_ptr().cast(), layout);
}
}
}
7 changes: 6 additions & 1 deletion argon2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ pub enum Error {
/// Time cost is too small.
TimeTooSmall,

/// Invalid version
/// Invalid version.
VersionInvalid,

/// Failed to allocate memory.
AllocFail,
}

impl fmt::Display for Error {
Expand All @@ -79,6 +82,7 @@ impl fmt::Display for Error {
Error::ThreadsTooMany => "too many threads",
Error::TimeTooSmall => "time cost is too small",
Error::VersionInvalid => "invalid version",
Error::AllocFail => "allocation failure",
})
}
}
Expand Down Expand Up @@ -116,6 +120,7 @@ impl From<Error> for password_hash::Error {
Error::ThreadsTooMany => InvalidValue::TooLong.param_error(),
Error::TimeTooSmall => InvalidValue::TooShort.param_error(),
Error::VersionInvalid => password_hash::Error::Version,
Error::AllocFail => InvalidValue::TooLong.param_error(),
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
compile_error!("this crate builds on 32-bit and 64-bit platforms only");

#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -286,8 +285,8 @@ impl<'key> Argon2<'key> {
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn hash_password_into(&self, pwd: &[u8], salt: &[u8], out: &mut [u8]) -> Result<()> {
let mut blocks = vec![Block::default(); self.params.block_count()];
self.hash_password_into_with_memory(pwd, salt, out, &mut blocks)
let mut blocks = block::Blocks::new(self.params.block_count()).ok_or(Error::AllocFail)?;
self.hash_password_into_with_memory(pwd, salt, out, blocks.as_slice())
}

/// Hash a password and associated parameters into the provided output buffer.
Expand Down