Skip to content

Commit 9c9ded7

Browse files
kkent030315dapa
andauthored
archive: fix size overflow in name index parser (#455)
Co-Authored-By: dapa <dapa@github>
1 parent 8fda7a8 commit 9c9ded7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/archive/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ impl<'a> NameIndex<'a> {
375375
// This is a total hack, because strtab returns "" if idx == 0, need to change
376376
// but previous behavior might rely on this, as ELF strtab's have "" at 0th index...
377377
let hacked_size = size + 1;
378+
if hacked_size < 2 {
379+
return Err(Error::Malformed(format!(
380+
"Size ({hacked_size:#x}) too small"
381+
)));
382+
}
378383
let strtab = strtab::Strtab::parse(buffer, *offset - 1, hacked_size, b'\n')?;
379384
// precious time was lost when refactoring because strtab::parse doesn't update the mutable seek...
380385
*offset += hacked_size - 2;
@@ -639,6 +644,19 @@ mod tests {
639644
assert_eq!(Member::bsd_filename_length("#1/1 A"), None);
640645
}
641646

647+
/// https://github.com/m4b/goblin/issues/450
648+
const MALFORMED_ARCHIVE_INDEX_TOO_SMALL: [u8; 132] = [
649+
0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x55, 0x52, 0x09, 0x5C, 0x09, 0x09, 0x10,
650+
0x27, 0x2B, 0x09, 0x0A, 0x53, 0x54, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
651+
0x09, 0x09, 0x09, 0x09, 0x2A, 0x29, 0x2A, 0x09, 0xF7, 0x08, 0x09, 0x09, 0x00, 0x01, 0x01,
652+
0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x01, 0x00, 0x31, 0x20, 0x20, 0x20,
653+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x08, 0x2F, 0x2F, 0x20, 0x20, 0x20,
654+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x09,
655+
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x23, 0x42, 0x21, 0x09, 0xF7, 0x08, 0x20, 0x20, 0x00,
656+
0x3C, 0x20, 0x20, 0x20, 0x00, 0x20, 0x20, 0x20, 0x20, 0x09, 0x09, 0x01, 0x01, 0x30, 0x0D,
657+
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x00, 0x00, 0x27, 0x55,
658+
];
659+
642660
/// https://github.com/m4b/goblin/issues/450
643661
const MALFORMED_ARCHIVE: [u8; 212] = [
644662
0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x2F, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
@@ -658,6 +676,17 @@ mod tests {
658676
0x14, 0x34,
659677
];
660678

679+
#[test]
680+
fn parse_name_index_too_small() {
681+
let res = Archive::parse(&MALFORMED_ARCHIVE_INDEX_TOO_SMALL);
682+
assert_eq!(res.is_err(), true);
683+
if let Err(Error::Malformed(msg)) = res {
684+
assert_eq!(msg, "Size (0x1) too small");
685+
} else {
686+
panic!("Expected a Malformed error but got {:?}", res);
687+
}
688+
}
689+
661690
#[test]
662691
fn parse_malformed_archive() {
663692
let res = Archive::parse(&MALFORMED_ARCHIVE);

0 commit comments

Comments
 (0)