Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 826c388

Browse files
authored
Error for short out in MdCtxRef::digest_final() (#2608)
If a caller passes an output buffer that is shorter than the digest size, digest_final() would write past its end, usually corrupting the stack. This is reachable from safe rust. Check the size and return an error if it is too small.
1 parent 1d10902 commit 826c388

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

openssl/src/md_ctx.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ impl MdCtxRef {
242242
pub fn digest_final(&mut self, out: &mut [u8]) -> Result<usize, ErrorStack> {
243243
let mut len = u32::try_from(out.len()).unwrap_or(u32::MAX);
244244

245+
if self.size() > len as usize {
246+
return Err(ErrorStack::get());
247+
}
248+
245249
unsafe {
246250
cvt(ffi::EVP_DigestFinal(
247251
self.as_ptr(),
@@ -549,4 +553,13 @@ mod test {
549553
// Validate result of digest of "World"
550554
assert_eq!(reset_result, world_expected);
551555
}
556+
557+
#[test]
558+
fn digest_final_checks_length() {
559+
let mut ctx = MdCtx::new().unwrap();
560+
ctx.digest_init(Md::sha256()).unwrap();
561+
ctx.digest_update(b"Some Crypto Text").unwrap();
562+
let mut digest = [0; 16];
563+
assert!(ctx.digest_final(&mut digest).is_err());
564+
}
552565
}

0 commit comments

Comments
 (0)