From 6a7a6af9e25d0daa49bb142c85a298c5c81e76a4 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 16 Sep 2026 12:06:46 +0200 Subject: fix: harden instantiation, stream parsing, and host reentrancy Signed-off-by: Henry --- crates/parser/src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'crates/parser/src/lib.rs') diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 2752d8b..019e027 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -190,10 +190,24 @@ impl Parser { #[cfg(feature = "std")] fn read_more(stream: &mut impl std::io::Read, buffer: &mut alloc::vec::Vec, hint: usize) -> Result { let len = buffer.len(); - buffer.resize(len + hint, 0); - let read_bytes = stream - .read(&mut buffer[len..]) - .map_err(|e| ParseError::Other(alloc::format!("Error reading from stream: {e}")))?; + // Size hints can come from untrusted section lengths. + let increment = hint.clamp(1, 64 * 1024); + let new_len = + len.checked_add(increment).ok_or_else(|| ParseError::Other("stream buffer is too large".into()))?; + buffer + .try_reserve(increment) + .map_err(|e| ParseError::Other(alloc::format!("Error reserving stream buffer: {e}")))?; + buffer.resize(new_len, 0); + let read_bytes = loop { + match stream.read(&mut buffer[len..]) { + Ok(read_bytes) => break read_bytes, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(e) => { + buffer.truncate(len); + return Err(ParseError::Other(alloc::format!("Error reading from stream: {e}"))); + } + } + }; buffer.truncate(len + read_bytes); Ok(read_bytes) } -- cgit v1.3.1