Craft Anything: understand how any software works, and craft your own.
Rust
Keep Unity bundle blocks within the data region (#1021)
Tomáš Doubek785539196a18
diff --git a/crates/formats/src/unity_bundle.rs b/crates/formats/src/unity_bundle.rsindex 1932df66..f2be52b7 100644--- a/crates/formats/src/unity_bundle.rs+++ b/crates/formats/src/unity_bundle.rs@@ -144,7 +144,8 @@ pub fn decompress(kind: u32, input: &[u8], out_len: u32) -> Result<Vec<u8>, FormOk(output)}-/// Read the block table (`table` as stored in the file).+/// Read the block table (`table` as stored in the file), checking that its compressed blocks+/// fit the header's data region before returning them to readers.pub fn table(table: &[u8], h: &Header) -> Result<(Vec<Block>, Vec<Node>), FormatError> {let t = decompress(h.flags & 0x3f, table, h.table_uncompressed_size)?;let mut pos = 16; // the data's hash@@ -155,11 +156,15 @@ pub fn table(table: &[u8], h: &Header) -> Result<(Vec<Block>, Vec<Node>), Format}Ok(n)};+ let data_end = if h.flags & 0x80 != 0 { h.table_offset } else { h.size };+ let bad_blocks = || FormatError::Malformed("the compressed blocks don't fit the bundle's data region".into());+ let mut remaining = data_end.checked_sub(h.data_offset).ok_or_else(bad_blocks)?;let mut blocks = Vec::new();for _ in 0..count(&mut pos, "blocks", 10)? {let uncompressed_size = u32::from_be_bytes(be(&t, &mut pos, "block")?);let compressed_size = u32::from_be_bytes(be(&t, &mut pos, "block")?);let flags = u16::from_be_bytes(be(&t, &mut pos, "block")?);+ remaining = remaining.checked_sub(u64::from(compressed_size)).ok_or_else(bad_blocks)?;blocks.push(Block { uncompressed_size, compressed_size, flags });}let mut nodes = Vec::new();diff --git a/crates/formats/tests/unity_bundle_data_region.rs b/crates/formats/tests/unity_bundle_data_region.rsnew file mode 100644index 00000000..2c21d750--- /dev/null+++ b/crates/formats/tests/unity_bundle_data_region.rs@@ -0,0 +1,96 @@+use craft_anything_formats::{FormatError, unity_bundle};++/// A stored table describing blocks without requiring their contents to decompress.+fn bundle(version: u32, flags: u32, sizes: &[u32], data_len: usize) -> (Vec<u8>, Vec<u8>) {+ let mut table = vec![0; 16];+ table.extend_from_slice(&(sizes.len() as i32).to_be_bytes());+ for (i, size) in sizes.iter().enumerate() {+ table.extend_from_slice(&1u32.to_be_bytes());+ table.extend_from_slice(&size.to_be_bytes());+ table.extend_from_slice(&((i % 4) as u16).to_be_bytes());+ }+ table.extend_from_slice(&0i32.to_be_bytes());+ let mut bytes = b"UnityFS\0".to_vec();+ bytes.extend_from_slice(&version.to_be_bytes());+ bytes.extend_from_slice(b"fixture\0fixture\0");+ let size_at = bytes.len();+ bytes.extend_from_slice(&0u64.to_be_bytes());+ bytes.extend_from_slice(&(table.len() as u32).to_be_bytes());+ bytes.extend_from_slice(&(table.len() as u32).to_be_bytes());+ bytes.extend_from_slice(&flags.to_be_bytes());+ if version >= 7 {+ bytes.resize(bytes.len().next_multiple_of(16), 0);+ }+ if flags & 0x80 == 0 {+ bytes.extend_from_slice(&table);+ }+ if flags & 0x200 != 0 {+ bytes.resize(bytes.len().next_multiple_of(16), 0);+ }+ bytes.resize(bytes.len() + data_len, 0);+ if flags & 0x80 != 0 {+ bytes.extend_from_slice(&table);+ }+ let size = bytes.len() as u64;+ bytes[size_at..size_at + 8].copy_from_slice(&size.to_be_bytes());+ (bytes, table)+}++/// Rejects individual and cumulative overruns even when table or suffix bytes could fill them.+#[test]+fn blocks_cannot_borrow_trailing_table_or_appended_bytes() {+ for version in [6, 7, 8] {+ for flags in [0, 0x80, 0x200, 0x280] {+ for sizes in [&[6][..], &[2, 0, 3, 1][..]] {+ let (mut bytes, table) = bundle(version, flags, sizes, 5);+ bytes.extend_from_slice(&[0; 64]);+ let h = unity_bundle::header(&bytes, bytes.len() as u64).unwrap();+ assert!(matches!(unity_bundle::table(&table, &h), Err(FormatError::Malformed(_))), "version {version}, flags {flags:#x}, sizes {sizes:?}");+ }+ }+ }+}++/// Preserves exact fits, unused data bytes and zero-length blocks across supported layouts.+#[test]+fn exact_boundaries_spare_data_and_empty_blocks_remain_valid() {+ for version in [6, 7, 8] {+ for flags in [0, 0x80, 0x200, 0x280] {+ for sizes in [&[][..], &[0][..], &[5][..], &[2, 0, 3, 0][..]] {+ for data_len in [5, 7] {+ let (bytes, table) = bundle(version, flags, sizes, data_len);+ let h = unity_bundle::header(&bytes, bytes.len() as u64).unwrap();+ let (blocks, nodes) = unity_bundle::table(&table, &h).unwrap();+ assert_eq!(blocks.iter().map(|b| b.compressed_size).collect::<Vec<_>>(), sizes);+ assert!(nodes.is_empty());+ }+ }+ let (bytes, table) = bundle(version, flags, &[0, 0], 0);+ let h = unity_bundle::header(&bytes, bytes.len() as u64).unwrap();+ assert_eq!(unity_bundle::table(&table, &h).unwrap().0.len(), 2);+ }+ }+}++/// Excludes the alignment gap from the space available for compressed block contents.+#[test]+fn alignment_padding_is_not_available_to_blocks() {+ for flags in [0x200, 0x280] {+ let (bytes, table) = bundle(6, flags, &[1], 0);+ let h = unity_bundle::header(&bytes, bytes.len() as u64).unwrap();+ assert_eq!(h.data_offset % 16, 0);+ assert!(unity_bundle::table(&table, &h).is_err());+ }+}++/// Checks totals above 32 bits using a large declared extent without allocating its contents.+#[test]+fn cumulative_block_sizes_do_not_wrap_at_32_bits() {+ let (bytes, table) = bundle(8, 0, &[u32::MAX, 2], 1);+ let mut h = unity_bundle::header(&bytes, bytes.len() as u64).unwrap();+ // A header-only reader can describe a multi-gigabyte file without allocating it.+ h.size = h.data_offset + u64::from(u32::MAX) + 1;+ assert!(unity_bundle::table(&table, &h).is_err());+ h.size += 1;+ assert_eq!(unity_bundle::table(&table, &h).unwrap().0.len(), 2);+}diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex d4f62def..e04e5f7b 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -1053,8 +1053,9 @@ Owner: open · Status: in progress— claimed: Afloat16, branch bugfix/r3.12-unity-bundle-decompressed-length-20261105- [x] Locate Unity bundle metadata within the declared bundle size.— claimed: Afloat16, branch bugfix/r3.12-unity-bundle-declared-size-20261105-- [ ] Check that Unity bundle compressed blocks end before the trailing block table, or the declared+- [x] Check that Unity bundle compressed blocks end before the trailing block table, or the declaredbundle end for front-positioned tables, so bytes outside the data region cannot supply block contents.+ — claimed: Codex, branch bugfix/r3.12-unity-bundle-data-region; PR #1021- [x] Reject Unity serialized objects outside their declared file.— claimed: Afloat16, branch bugfix/r3.12-unity-serialized-object-ranges-20261105- [x] Preserve compression method slots in Unreal pak indexes.diff --git a/docs/STATUS.md b/docs/STATUS.mdindex 9eecc8a0..5490c163 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -25,6 +25,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi| Refuse split ZIP archives without their other volumes | ZIP archives requiring other volumes are reported as unsupported. Explicit classic directory sizes and offsets must agree with ZIP64 unless set to their own sentinel. | Synthetic byte fixtures in `zip_split_volumes` integration regressions. Cover classic disk markers and counts, ZIP64 locator and record fields, conflicting classic/ZIP64 entry counts, entry disk sentinels both alone and after the sizes and local offset, and valid single-volume controls with mixed count sentinels. Additional regressions cover explicit directory-bound conflicts and each independently saturated end field. || Check Unity bundle blocks against their declared output lengths | Unity bundle pieces must match their declared decoded lengths; LZMA must finish within the supplied compressed piece. | Synthetic byte fixtures in `unity_bundle_decompressed_length` integration regressions. Cover stored pieces that are short or long, LZ4 and LZ4HC size mismatches, LZMA with or without end markers, empty and short zero streams, bounded dictionary/output growth, oversized dictionary properties, and truncated or trailing compressed bytes. || Locate Unity bundle metadata within the declared bundle size | Unity bundle metadata placement respects the declared bundle extent. | Synthetic byte fixtures in `unity_bundle_declared_size` integration regressions. Cover versions 6 through 8, front and trailing tables, padding, appended bytes, invalid extents, and header-only prefixes of larger bundles. |+| Keep Unity bundle blocks inside the data region | The shared table parser refuses compressed block totals crossing the trailing table or the declared bundle end before listing or extraction can read block contents. | Synthetic `unity_bundle_data_region` regressions cover versions 6–8, both table positions, data alignment, appended bytes, cumulative sizes above 32 bits, exact fits, spare bytes and empty blocks. No real game bundle was run for this correction. || Reject Unity serialized objects outside their declared file | Unity serialized object ranges must fit the declared file. | Synthetic byte fixtures in `unity_serialized_object_ranges` integration regressions. Cover offset and end overflow, ranges crossing the file boundary, both byte orders, valid existing objects, and empty objects at EOF. || Preserve compression method slots in Unreal pak indexes | PAK compression names retain their original method numbers. | Synthetic byte fixtures in `unreal_pak_compression_slots` integration regressions. Cover interior and leading empty slots in plain and packed indexes, plus unchanged contiguous method tables. || Require complete terminators in Unreal FString values | PAK and IoStore strings require complete terminators. | Synthetic byte fixtures in `unreal_fstring_terminators` integration regressions. Cover missing ANSI and UTF-16 terminators, zero-length strings, terminated empty strings, Unicode names, and the shared IoStore path. |