Skip to content
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

fix: Prevent buffer overflow in read_bytes function #13

Merged
merged 1 commit into from
Jul 2, 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
20 changes: 16 additions & 4 deletions src/extractor/smda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,24 @@ pub fn read_bytes<'a>(
) -> Result<&'a [u8]> {
let rva = offset - report.base_addr;
let buffer_end = report.buffer.len();
let end_of_string = rva + num_bytes as u64;
let mut end_of_string = rva + num_bytes as u64;

// If end_of_string exceeds buffer_end, adjust it to buffer_end
if end_of_string > buffer_end as u64 {
Ok(&report.buffer[rva as usize..])
} else {
Ok(&report.buffer[rva as usize..end_of_string as usize])
// println!(
// "Buffer overflow error end_of_string: {} buffer_end: {}, rva: {}, num_bytes: {}. Force end.",
// end_of_string, buffer_end, rva, num_bytes
// );
end_of_string = buffer_end as u64;
}

// Ensure that rva does not exceed the size of the buffer
if rva > buffer_end as u64 {
// println!("Offset out of buffer range rva: {} buffer_end: {}", rva, buffer_end);
return Err(Error::BufferOverflowError);
}

Ok(&report.buffer[rva as usize..end_of_string as usize])
}

pub fn read_string(report: &DisassemblyReport, offset: &u64) -> Result<String> {
Expand Down
Loading