address review: keep the TIFF header and Exif/GPS sub-IFDs out of the blanked thumbnail range

This commit is contained in:
Dylan M. Taylor committed 2026-11-10 23:24:20 +00:00
1 parent ab5b0bcee9
commit e345f8cfb1
2 files changed
+38 -5

No files matched your search

+21 -5
View File
@@ -526,11 +526,12 @@ fn exif_without_thumbnail(exif: &[u8]) -> Option<Vec<u8>> {
}
// Classic TIFF: also blank the thumbnail JPEG itself (IFD1's JPEGInterchangeFormat/Length),
// which stays recoverable in the block once nothing points at it.
// Only a range clear of IFD0's table and values: one that covers them is not a thumbnail.
// Only a range clear of the TIFF header, IFD0's table and values, and its Exif/GPS sub-IFDs: one
// that covers them is not a thumbnail.
let thumb = (!big_tiff)
.then(|| thumbnail_range(order, body, next - prefix))
.flatten()
.filter(|&(off, len)| !overlaps_ifd0(order, body, first - 2, count, off, len));
.filter(|&(off, len)| off >= 8 && !overlaps_ifd0(order, body, first - 2, count, off, len));
let mut out = exif.to_vec();
out.get_mut(next..next + width)?.fill(0);
if let Some((off, len)) = thumb
@@ -542,15 +543,30 @@ fn exif_without_thumbnail(exif: &[u8]) -> Option<Vec<u8>> {
}
/// Whether `off..off + len` of `body` covers part of classic IFD0 (`ifd0` is its entry count's
/// offset): its table with the next-IFD pointer, or an out-of-line value of one of its entries.
/// offset) or of its Exif / GPS sub-IFDs.
fn overlaps_ifd0(order: Order, body: &[u8], ifd0: usize, count: usize, off: usize, len: usize) -> bool {
overlaps_ifd(order, body, ifd0, count, off, len)
|| (0..count).any(|i| {
let e = ifd0 + 2 + i * 12;
// A LONG pointer (tag 0x8769 ExifIFD, 0x8825 GPSIFD) to a sub-IFD, whose own count says its size.
matches!(order.u16(body, e), Some(0x8769 | 0x8825))
&& order
.u32(body, e + 8)
.and_then(|v| usize::try_from(v).ok())
.is_some_and(|sub| order.u16(body, sub).is_some_and(|n| overlaps_ifd(order, body, sub, usize::from(n), off, len)))
})
}
/// Whether `off..off + len` of `body` covers the table of the classic IFD at `ifd` (its entry
/// count's offset) with the next-IFD pointer, or an out-of-line value of one of its entries.
fn overlaps_ifd(order: Order, body: &[u8], ifd: usize, count: usize, off: usize, len: usize) -> bool {
let end = off.saturating_add(len);
let hits = |a: usize, size: usize| a < end && off < a.saturating_add(size);
if hits(ifd0, 2 + count * 12 + 4) {
if hits(ifd, 2 + count * 12 + 4) {
return true;
}
(0..count).any(|i| {
let e = ifd0 + 2 + i * 12;
let e = ifd + 2 + i * 12;
let unit = match order.u16(body, e + 2) {
Some(1 | 2 | 6 | 7) => 1,
Some(3 | 8) => 2,
+17
View File
@@ -550,6 +550,23 @@ fn thumbnail_blanking_keeps_ifd0_and_ignores_malformed_tags() {
let out = export_exif(&over, None);
assert_eq!(next_ifd(e, &out), 0, "IFD1 is still unlinked");
assert_eq!(out[table.start..table.end], over[table.start..table.end], "IFD0's table is untouched");
// Offset 0, length 8: the range covers the TIFF header, which must stay parseable.
let mut header = exif.clone();
header[ifd1 + 2 + 8..ifd1 + 2 + 12].copy_from_slice(&e.u32(0));
header[ifd1 + 2 + 12 + 8..ifd1 + 2 + 12 + 12].copy_from_slice(&e.u32(8));
let out = export_exif(&header, None);
assert_eq!(out[..8], header[..8], "the TIFF header is untouched");
// IFD0's last entry (Unit) turned into an ExifIFD pointer at IFD1's table: that range is not a thumbnail.
let mut sub = exif.clone();
let entry = 8 + 2 + 3 * 12;
sub[entry..entry + 2].copy_from_slice(&e.u16(0x8769));
sub[entry + 2..entry + 4].copy_from_slice(&e.u16(LONG));
sub[entry + 4..entry + 8].copy_from_slice(&e.u32(1));
sub[entry + 8..entry + 12].copy_from_slice(&e.u32(ifd1 as u32));
sub[ifd1 + 2 + 8..ifd1 + 2 + 12].copy_from_slice(&e.u32(ifd1 as u32 + 2));
sub[ifd1 + 2 + 12 + 8..ifd1 + 2 + 12 + 12].copy_from_slice(&e.u32(4));
let out = export_exif(&sub, None);
assert_eq!(out[ifd1 + 2..ifd1 + 6], sub[ifd1 + 2..ifd1 + 6], "the sub-IFD's table is untouched");
// A count of 2 on the offset tag: the thumbnail bytes are not blanked.
let mut counted = exif.clone();
counted[ifd1 + 2 + 4..ifd1 + 2 + 8].copy_from_slice(&e.u32(2));