Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Zero-fill ext4 unwritten extents during file reads (#391)

phatloi0010d908b73d2cef
diff --git a/crates/formats/src/fsread/extfs.rs b/crates/formats/src/fsread/extfs.rs
index 258db2e6..429b88e9 100644
--- a/crates/formats/src/fsread/extfs.rs
+++ b/crates/formats/src/fsread/extfs.rs
@@ -164,16 +164,17 @@ fn extents(data: &[u8], sb: &Sb, node_off: usize, map: &mut Vec<Option<u64>>, de
let e = node_off + 12 + i * 12;
if depth == 0 {
let logical = r.u32_le(e).unwrap_or(0);
- let len = r.u16_le(e + 4).unwrap_or(0);
+ let raw_len = r.u16_le(e + 4).unwrap_or(0);
let start_hi = r.u16_le(e + 6).unwrap_or(0);
let start_lo = r.u32_le(e + 8).unwrap_or(0);
let phys = u64::from(start_lo) | (u64::from(start_hi) << 32);
- let len = if len > 32768 { len - 32768 } else { len }; // uninitialised extents
+ let unwritten = raw_len > 32768; // uninitialised extents
+ let len = if unwritten { raw_len - 32768 } else { raw_len };
for k in 0..u64::from(len) {
if let Ok(idx) = usize::try_from(u64::from(logical).saturating_add(k))
&& idx < map.len()
{
- map[idx] = Some(phys.saturating_add(k));
+ map[idx] = if unwritten { None } else { Some(phys.saturating_add(k)) };
}
}
} else {
@@ -331,6 +332,19 @@ mod tests {
check(true);
}
+ #[test]
+ fn unwritten_ext4_extents_read_as_zeros() {
+ let mut base = craft_anything_testkit::firmware::ext_image(true);
+ // Inode 11 ("hello.txt", 22 bytes) starts at 5120 + (11 - 1) * 128 = 6400; its first extent's ee_len is at +56.
+ base[6456..6458].copy_from_slice(&32769u16.to_le_bytes());
+ let img = read(&base, &Limits::default());
+ assert!(img.complete, "{:?}", img.warnings);
+ let hello = img.entries.iter().find(|e| e.path == "hello.txt").expect("hello.txt entry");
+ assert_eq!(hello.size, 22);
+ assert_eq!(hello.data.as_deref(), Some([0u8; 22].as_slice()));
+ assert_eq!(img.entries.iter().find(|e| e.path == "sub/note").unwrap().data.as_deref(), Some(b"nested note".as_slice()));
+ }
+
/// Switch to 64-bit group descriptors and send `group`'s inode table to the last
/// 1 KiB block of the address space, so an inode there starts within 128 bytes of `usize::MAX`.
fn point_table_at_the_end(img: &mut [u8], group: usize) {