diff --git a/crates/metadata/src/camera_fields.rs b/crates/metadata/src/camera_fields.rs index 445f51498..d18bcdec9 100644 --- a/crates/metadata/src/camera_fields.rs +++ b/crates/metadata/src/camera_fields.rs @@ -17,6 +17,8 @@ pub struct CameraInfo { pub focal_length: Option, /// 35 mm-equivalent focal length in millimetres. pub focal_length_35mm: Option, + /// DateTimeOriginal as stored (`YYYY:MM:DD HH:MM:SS`). + pub date_taken: Option, } impl CameraInfo { @@ -35,11 +37,11 @@ struct Tiff<'a> { impl Tiff<'_> { fn u16(&self, o: usize) -> Option { - let s = self.b.get(o..o + 2)?; + let s = self.b.get(o..o.checked_add(2)?)?; Some(if self.le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) } fn u32(&self, o: usize) -> Option { - let s = self.b.get(o..o + 4)?; + let s = self.b.get(o..o.checked_add(4)?)?; let a = [s[0], s[1], s[2], s[3]]; Some(if self.le { u32::from_le_bytes(a) } else { u32::from_be_bytes(a) }) } @@ -48,20 +50,25 @@ impl Tiff<'_> { let Some(n) = self.u16(off) else { return Vec::new() }; (0..n as usize) .filter_map(|k| { - let e = off + 2 + k * 12; - Some((self.u16(e)?, self.u16(e + 2)?, self.u32(e + 4)?, e + 8)) + // Checked: offsets come from the file, and usize is 32 bits on wasm. + let e = off.checked_add(2)?.checked_add(k * 12)?; + Some((self.u16(e)?, self.u16(e.checked_add(2)?)?, self.u32(e.checked_add(4)?)?, e.checked_add(8)?)) }) .collect() } fn data_at(&self, typ: u16, count: u32, field: usize) -> Option { let size = match typ { - 1 | 2 | 6 | 7 => 1, + 1 | 2 | 6 | 7 => 1usize, 3 | 8 => 2, 4 | 9 | 11 => 4, 5 | 10 | 12 => 8, _ => return None, - } * count as usize; - if size <= 4 { Some(field) } else { self.u32(field).map(|v| v as usize) } + } + .checked_mul(usize::try_from(count).ok()?)?; + let at = if size <= 4 { field } else { usize::try_from(self.u32(field)?).ok()? }; + // The whole value must lie inside the payload, not just its first element. + self.b.get(at..at.checked_add(size)?)?; + Some(at) } fn number(&self, typ: u16, count: u32, field: usize) -> Option { let at = self.data_at(typ, count, field)?; @@ -69,11 +76,11 @@ impl Tiff<'_> { 3 => self.u16(at).map(f64::from), 4 => self.u32(at).map(f64::from), 5 => { - let (n, d) = (self.u32(at)?, self.u32(at + 4)?); + let (n, d) = (self.u32(at)?, self.u32(at.checked_add(4)?)?); (d != 0).then(|| n as f64 / d as f64) } 10 => { - let (n, d) = (self.u32(at)? as i32, self.u32(at + 4)? as i32); + let (n, d) = (self.u32(at)? as i32, self.u32(at.checked_add(4)?)? as i32); (d != 0).then(|| n as f64 / d as f64) } _ => None, @@ -84,7 +91,7 @@ impl Tiff<'_> { return None; } let at = self.data_at(typ, count, field)?; - let s = self.b.get(at..at + count as usize)?; + let s = self.b.get(at..at.checked_add(usize::try_from(count).ok()?)?)?; let s = String::from_utf8_lossy(s).trim_end_matches('\0').trim().to_string(); (!s.is_empty()).then_some(s) } @@ -118,6 +125,7 @@ pub fn read(exif: &[u8]) -> CameraInfo { 0x920A => info.focal_length = t.number(typ, count, field), 0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0), 0xA434 => info.lens = t.text(typ, count, field), + 0x9003 => info.date_taken = t.text(typ, count, field), 0x8769 => { if let Some(p) = t.u32(field) { queue.push(p as usize); @@ -161,6 +169,9 @@ pub fn build(info: &CameraInfo) -> Vec { if let Some(v) = info.iso { exif.push((0x8827, 3, 1, (v as u16).to_le_bytes().to_vec())); } + if let Some(s) = &info.date_taken { + exif.push((0x9003, 2, s.len() as u32 + 1, text(s))); + } if let Some(v) = info.focal_length { exif.push((0x920A, 5, 1, rational(v))); } @@ -224,6 +235,7 @@ mod tests { iso: Some(200.0), focal_length: Some(35.0), focal_length_35mm: Some(52.0), + date_taken: Some("2024:02:17 12:40:19".into()), }; let b = build(&info); let r = read(&b); @@ -237,4 +249,53 @@ mod tests { let _ = read(&b[..cut]); } } + + /// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random + /// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm). + #[test] + fn hostile_payloads_never_panic() { + let entry = |tag: u16, typ: u16, count: u32, value: u32| { + let mut e = tag.to_le_bytes().to_vec(); + e.extend_from_slice(&typ.to_le_bytes()); + e.extend_from_slice(&count.to_le_bytes()); + e.extend_from_slice(&value.to_le_bytes()); + e + }; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + let entries = [ + entry(0x010F, 2, u32::MAX, 8), // Make: count far past the end + entry(0x0110, 2, 16, u32::MAX - 1), // Model: offset at the top of the range + entry(0x829A, 5, u32::MAX, 20), // ExposureTime: count overflows the size + entry(0x829D, 5, 1, u32::MAX - 3), // FNumber: rational straddles u32::MAX + entry(0x9003, 2, 0x4000_0000, 8), // DateTimeOriginal: size overflow on 32 bits + entry(0x8769, 4, 1, 8), // Exif IFD pointing back at IFD0 + entry(0x8769, 4, 1, u32::MAX), // ... and past the end + ]; + b.extend_from_slice(&(entries.len() as u16 + 3).to_le_bytes()); // claims more entries than exist + for e in entries { + b.extend_from_slice(&e); + } + assert_eq!(read(&b), CameraInfo::default()); + let mut be = b"MM\0*".to_vec(); + be.extend_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(read(&be), CameraInfo::default()); + // Deterministic noise behind a valid header, and every truncation of a real payload. + let mut x = 0x2545_F491u32; + for _ in 0..200 { + let mut n = b"II*\0\x08\0\0\0".to_vec(); + for _ in 0..96 { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + n.push(x as u8); + } + let _ = read(&n); + } + let good = + build(&CameraInfo { make: Some("Make".into()), date_taken: Some("2024:02:17 12:40:19".into()), exposure_time: Some(0.004), ..Default::default() }); + for cut in 0..good.len() { + let _ = read(&good[..cut]); + } + } }