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 more fuzz found panics #499

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion backhand-cli/src/bin/unsquashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ fn main() -> ExitCode {
return ExitCode::SUCCESS;
}

let squashfs = Squashfs::from_reader_with_offset_and_kind(file, args.offset, kind).unwrap();
let squashfs = match Squashfs::from_reader_with_offset_and_kind(file, args.offset, kind) {
Ok(s) => s,
Err(_e) => {
let line = format!("{:>14}", red_bold.apply_to(format!("Could not read image: {_e}")));
pb.finish_with_message(line);
return ExitCode::FAILURE;
}
};
let root_process = unsafe { geteuid() == 0 };
if root_process {
umask(Mode::from_bits(0).unwrap());
Expand Down
24 changes: 17 additions & 7 deletions backhand/src/squashfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,16 @@ impl<'b> Squashfs<'b> {
return Ok(None);
}

// ignore blocks before our block_index, grab all the rest of the bytes
// TODO: perf
let offset = self.dir_blocks.0.get(&block_index).unwrap();
let block = &self.dir_blocks.1[*offset as usize..];
let Some(offset) = self.dir_blocks.0.get(&block_index) else {
return Err(BackhandError::CorruptedOrInvalidSquashfs);
};
let Some(block) = &self.dir_blocks.1.get(*offset as usize..) else {
return Err(BackhandError::CorruptedOrInvalidSquashfs);
};

if (block.len() as u32) < (block_offset as u32 + file_size - 3) {
return Err(BackhandError::CorruptedOrInvalidSquashfs);
}

let bytes = &block[block_offset..][..file_size as usize - 3];
let mut dirs = vec![];
Expand Down Expand Up @@ -512,9 +518,13 @@ impl<'b> Squashfs<'b> {
for d in &dirs {
trace!("extracting entry: {:#?}", d.dir_entries);
for entry in &d.dir_entries {
let inode_key =
(d.inode_num as i32 + entry.inode_offset as i32).try_into().unwrap();
let found_inode = &self.inodes[&inode_key];
let Ok(inode_key) = (d.inode_num as i32 + entry.inode_offset as i32).try_into()
else {
return Err(BackhandError::CorruptedOrInvalidSquashfs);
};
let Some(found_inode) = &self.inodes.get(&inode_key) else {
return Err(BackhandError::CorruptedOrInvalidSquashfs);
};
let header = found_inode.header;
fullpath.push(entry.name()?);

Expand Down
Loading