Skip to content

fix inflate::uncompress bug #116

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 2 commits into from
Jun 17, 2024
Merged
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
38 changes: 27 additions & 11 deletions zlib-rs/src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,13 @@ pub fn uncompress<'a>(

let mut stream = z_stream {
next_in: input.as_ptr() as *mut u8,
avail_in: input.len() as _,
total_in: 0,
next_out: dest,
avail_out: output.len() as _,
total_out: 0,
msg: core::ptr::null_mut(),
state: core::ptr::null_mut(),
avail_in: 0,

zalloc: None,
zfree: None,
opaque: core::ptr::null_mut(),
data_type: 0,
adler: 0,
reserved: 0,

..z_stream::default()
};

let err = init(&mut stream, config);
Expand All @@ -196,7 +190,7 @@ pub fn uncompress<'a>(
left -= stream.avail_out as u64;
}

if stream.avail_out == 0 {
if stream.avail_in == 0 {
stream.avail_in = Ord::min(len, u32::MAX as u64) as u32;
len -= stream.avail_in as u64;
}
Expand Down Expand Up @@ -2266,3 +2260,25 @@ pub fn get_header<'a>(
});
ReturnCode::Ok
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn uncompress_buffer_overflow() {
let mut output = [0; 1 << 13];
let input = [
72, 137, 58, 0, 3, 39, 255, 255, 255, 255, 255, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 184, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14,
14, 14, 14, 14, 14, 63, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14, 255, 14, 103, 14,
14, 14, 14, 14, 14, 61, 14, 255, 255, 63, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14,
255, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 6, 14, 14, 14, 14, 14, 14, 14, 14, 71,
4, 137, 106,
];

let config = InflateConfig { window_bits: 15 };

let (_decompressed, err) = uncompress_slice(&mut output, &input, config);
assert_eq!(err, ReturnCode::DataError);
}
}