Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(redact): size image clearing with checked arithmetic (#2177) (#2221)

Anik Tahabilder6e04f6eed772
diff --git a/crates/redact/src/image.rs b/crates/redact/src/image.rs
index 043a77db9..23ef2559a 100644
--- a/crates/redact/src/image.rs
+++ b/crates/redact/src/image.rs
@@ -23,9 +23,9 @@ fn components(doc: &Document, s: &Stream) -> Option<usize> {
b"DeviceRGB" | b"RGB" | b"CalRGB" | b"Lab" => 3,
b"DeviceCMYK" | b"CMYK" => 4,
b"ICCBased" => {
- // /N lives in the ICC profile stream.
+ // /N lives in the ICC profile stream, and shall be 1, 3 or 4 (ISO 32000-2 §8.6.5.5).
match &*doc.resolve(cs.as_array()?.get(1)?) {
- Object::Stream(icc) => icc.dict.int(b"N")? as usize,
+ Object::Stream(icc) => usize::try_from(icc.dict.int(b"N")?).ok().filter(|n| matches!(n, 1 | 3 | 4))?,
_ => return None,
}
}
@@ -70,12 +70,18 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]
if !matches!(bpc, 1 | 2 | 4 | 8 | 16) {
return None;
}
+ // Every size comes from the file: multiply checked, so an overflowing product refuses the image
+ // instead of sizing the writes below.
+ let bits_per_pixel = ncomp.checked_mul(bpc)?;
+ let row = w.checked_mul(bits_per_pixel)?.div_ceil(8);
+ let bytes = row.checked_mul(h)?;
+ let row_bits = row.checked_mul(8)?;
+ bytes.checked_mul(8)?;
let mut data = s.decoded().ok()?;
- let row = (w * ncomp * bpc).div_ceil(8);
- if data.len() < row * h {
+ if data.len() < bytes {
return None;
}
- data.truncate(row * h);
+ data.truncate(bytes);
// Image masks: a sample of 0 paints unless /Decode is [1 0]; cleared samples must not paint.
let clear_bit = if mask {
let inverted = s.dict.get(b"Decode").and_then(Object::as_array).and_then(|d| d.first()).and_then(Object::as_f64) == Some(1.0);
@@ -83,7 +89,6 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]
} else {
false
};
- let bits_per_pixel = ncomp * bpc;
let mut cleared = 0usize;
for j in 0..h {
// Image space: (0,0) is the bottom-left of the unit square; row 0 is the top.
@@ -101,13 +106,15 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]
continue;
}
cleared += 1;
- let start = j * row * 8 + i * bits_per_pixel;
+ // `bytes * 8` fits, and every bit index is below it, so this arithmetic can't overflow.
+ let start = j * row_bits + i * bits_per_pixel;
for bit in start..start + bits_per_pixel {
let (byte, shift) = (bit / 8, 7 - bit % 8);
+ let b = data.get_mut(byte)?;
if clear_bit {
- data[byte] |= 1 << shift;
+ *b |= 1 << shift;
} else {
- data[byte] &= !(1 << shift);
+ *b &= !(1 << shift);
}
}
}
diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs
index 9b09dabb5..b9a230fc4 100644
--- a/crates/redact/src/tests.rs
+++ b/crates/redact/src/tests.rs
@@ -1056,6 +1056,21 @@ fn redacting_every_inheriting_page_retires_the_parent_resources() {
}
}
+#[test]
+fn a_hostile_icc_component_count_is_refused_not_multiplied_into_a_crash() {
+ // /N comes from the file. 2^60 × 8 bpc × width 2 overflows the row size; a negative /N wrapped
+ // to a huge usize. Either way the half-covered image can't be cleared, so it is removed whole.
+ for n in ["1152921504606846976", "-1", "2"] {
+ let img = stream("/Type /XObject /Subtype /Image /Width 2 /Height 1 /ColorSpace [/ICCBased 8 0 R] /BitsPerComponent 8", [255, 255]);
+ let icc = stream(&format!("/N {n}"), b"");
+ let mut doc = one_page(b"q 20 0 0 10 100 100 cm /Im Do Q", "/XObject << /Im 7 0 R >>", vec![img, icc]);
+ mark(&mut doc, 0, &[[95.0, 95.0, 110.0, 115.0]], "");
+ let r = apply(&mut doc, None).unwrap();
+ assert_eq!((r.images_removed, r.images_cleared), (1, 0), "/N {n}: {r:?}");
+ assert!(!content(&doc, 0).contains("/Im Do"), "/N {n}");
+ }
+}
+
#[test]
fn image_alternatives_and_masks_cannot_keep_redacted_pixels() {
for extra in ["/Alternates [<< /Image 8 0 R >>]", "/SMask 8 0 R", "/Mask 8 0 R", "/Mask [0 0]"] {