Craft Anything: understand how any software works, and craft your own.
Rust
A .utoc naming billions of empty compression methods no longer runs out of memory (#61)
Agapenorfef3f8231e55
diff --git a/crates/formats/src/unreal_iostore.rs b/crates/formats/src/unreal_iostore.rsindex 2797e08a..8d23d4f2 100644--- a/crates/formats/src/unreal_iostore.rs+++ b/crates/formats/src/unreal_iostore.rs@@ -172,9 +172,13 @@ pub fn toc(data: &[u8]) -> Result<Toc, FormatError> {method: b[11],}).collect();- let methods = (0..header.method_count)+ // A block names its method in one byte, so only the first 255 names can be used; the rest are+ // skipped, never collected (a zero name length would otherwise read billions of empty names).+ let named = header.method_count.min(u32::from(u8::MAX));+ let methods = (0..named).map(|_| c.take(header.method_length as usize, "compression method").map(|b| String::from_utf8_lossy(b).trim_end_matches('\0').to_string())).collect::<Result<Vec<_>, _>>()?;+ c.take(((header.method_count - named) as usize).saturating_mul(header.method_length as usize), "compression methods")?;if header.flags.signed {let hash = c.count("signature size", 2)?;c.take(hash * 2, "signatures")?;@@ -308,4 +312,26 @@ mod tests {assert_eq!(files(&t, 10).unwrap().2, 3, "sibling {sibling}");}}++ #[test]+ fn a_huge_count_of_empty_method_names_is_not_collected() {+ let mut d = sample(0x9);+ d[36..40].copy_from_slice(&0x8000_0000u32.to_le_bytes());+ d[40..44].copy_from_slice(&0u32.to_le_bytes());+ let t = toc(&d).unwrap();+ assert_eq!(t.methods.len(), 255);+ // Names past the 255 a block can point at are skipped, so the directory index still reads.+ // The sample's one 32-byte name (after the header, chunks, seed and blocks) is taken out.+ let mut d = sample(0x9);+ assert_eq!(&d[262..267], b"Oodle");+ d.drain(262..294);+ d[36..40].copy_from_slice(&300u32.to_le_bytes());+ d[40..44].copy_from_slice(&0u32.to_le_bytes());+ assert_eq!(files(&toc(&d).unwrap(), 10).unwrap().2, 3);+ // More names than the file holds is refused, not read past the end.+ let mut d = sample(0x9);+ d[36..40].copy_from_slice(&300u32.to_le_bytes());+ d[40..44].copy_from_slice(&32u32.to_le_bytes());+ assert!(toc(&d).is_err());+ }}diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 583c751b..c72829ae 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -144,6 +144,9 @@ Owner: open · Status: done (branch chore/r0.5-cleanups)- [x] Checked arithmetic on file offsets in `elf.rs`: `va_to_off` adds a segment's file offset to anaddress delta unchecked, so a `PT_LOAD` at offset `u64::MAX` overflows, found by a lint scanof `crates/formats` (#43)+- [x] Capped allocation in `unreal_iostore.rs`: a `.utoc` header naming 2^31 compression methods of+ length 0 collected empty names until memory ran out (6 GiB from a 559-byte file); only the+ 255 a block can point at are read, the rest skipped. Found by mutation fuzzing of `crates/formats`- [x] `cargo xtask layers` also scans `apps/`- [x] Engines in `KNOWN` without a `ToolPaths` override (tshark, objdump, rizin, java)- [x] Missing `database` icon for SQLite in the UI (`kind_icon`)