diff --git a/Cargo.toml b/Cargo.toml index 4a5ec46..f2bc720 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,8 @@ authors = [ "HeroicKatora", "Calum", "CensoredUsername ", - "Robzz"] + "Robzz", + "birktj"] license = "MIT" description = "TIFF decoding and encoding library in pure Rust" repository = "https://github.com/PistonDevelopers/image-tiff" diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 515006e..15db66b 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -154,7 +154,7 @@ impl Entry { ) } - pub fn val(&self, decoder: &mut super::Decoder) + pub fn val(&self, limits: &super::Limits, decoder: &mut super::Decoder) -> TiffResult { let bo = decoder.byte_order(); match (self.type_, self.count) { @@ -170,6 +170,9 @@ impl Entry { ])) }, (Type::SHORT, n) => { + if n as usize > limits.decoding_buffer_size / std::mem::size_of::() { + return Err(TiffError::LimitsExceeded) + } let mut v = Vec::with_capacity(n as usize); try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); for _ in 0 .. n { @@ -179,6 +182,9 @@ impl Entry { }, (Type::LONG, 1) => Ok(Unsigned(try!(self.r(bo).read_u32()))), (Type::LONG, n) => { + if n as usize > limits.decoding_buffer_size / std::mem::size_of::() { + return Err(TiffError::LimitsExceeded) + } let mut v = Vec::with_capacity(n as usize); try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); for _ in 0 .. n { @@ -193,6 +199,9 @@ impl Entry { Ok(Rational(numerator, denominator)) }, (Type::RATIONAL, n) => { + if n as usize > limits.decoding_buffer_size / std::mem::size_of::() { + return Err(TiffError::LimitsExceeded) + } let mut v = Vec::with_capacity(n as usize); try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); for _ in 0 .. n { @@ -203,6 +212,9 @@ impl Entry { Ok(List(v)) }, (Type::ASCII, n) => { + if n as usize > limits.decoding_buffer_size { + return Err(TiffError::LimitsExceeded) + } try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); let string = try!(decoder.read_string(n as usize)); Ok(Ascii(string)) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 1cd8542..5e070f1 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,7 +1,6 @@ use std::io::{self, Read, Seek}; use num_traits::{FromPrimitive, Num}; use std::collections::HashMap; -use std::string::FromUtf8Error; use ::{ColorType, TiffError, TiffFormatError, TiffUnsupportedError, TiffResult}; @@ -28,7 +27,25 @@ pub enum DecodingResult { } impl DecodingResult { - pub fn to_buffer(&mut self, start: usize) -> DecodingBuffer { + fn new_u8(size: usize, limits: &Limits) -> TiffResult { + if size > limits.decoding_buffer_size { + Err(TiffError::LimitsExceeded) + } + else { + Ok(DecodingResult::U8(vec![0; size])) + } + } + + fn new_u16(size: usize, limits: &Limits) -> TiffResult { + if size > limits.decoding_buffer_size / 2 { + Err(TiffError::LimitsExceeded) + } + else { + Ok(DecodingResult::U16(vec![0; size])) + } + } + + pub fn as_buffer(&mut self, start: usize) -> DecodingBuffer { match self { DecodingResult::U8(ref mut buf) => DecodingBuffer::U8(&mut buf[start..]), DecodingResult::U16(ref mut buf) => DecodingBuffer::U16(&mut buf[start..]), @@ -45,6 +62,20 @@ pub enum DecodingBuffer<'a> { } impl<'a> DecodingBuffer<'a> { + fn len(&self) -> usize { + match self { + DecodingBuffer::U8(ref buf) => buf.len(), + DecodingBuffer::U16(ref buf) => buf.len(), + } + } + + fn byte_len(&self) -> usize { + match self { + DecodingBuffer::U8(_) => 1, + DecodingBuffer::U16(_) => 2, + } + } + fn copy<'b>(&'b mut self) -> DecodingBuffer<'b> where 'a: 'b { match self { DecodingBuffer::U8(ref mut buf) => DecodingBuffer::U8(buf), @@ -95,6 +126,28 @@ struct StripDecodeState { strip_bytes: Vec, } +/// Decoding limits +#[derive(Clone, Debug)] +pub struct Limits { + /// The maximum size of any `DecodingResult` in bytes, the default is + /// 256MiB. If the entire image is decoded at once, then this will + /// be the maximum size of the image. If it is decoded one strip at a + /// time, this will be the maximum size of a strip. + decoding_buffer_size: usize, + /// The maximum size of any ifd value in bytes, the default is + /// 1MiB. + ifd_value_size: usize, +} + +impl Default for Limits { + fn default() -> Limits { + Limits { + decoding_buffer_size: 256*1024*1024, + ifd_value_size: 1024*1024, + } + } +} + /// The representation of a TIFF decoder /// /// Currently does not support decoding of interlaced images @@ -102,6 +155,7 @@ struct StripDecodeState { pub struct Decoder where R: Read + Seek { reader: SmartReader, byte_order: ByteOrder, + limits: Limits, next_ifd: Option, ifd: Option, width: u32, @@ -151,14 +205,15 @@ fn rev_hpredict(image: DecodingBuffer, size: (u32, u32), color_type: ColorType) ColorType::RGBA(8) | ColorType::RGBA(16) | ColorType::CMYK(8) => 4, _ => return Err(TiffError::UnsupportedError(TiffUnsupportedError::HorizontalPredictor(color_type))) }; - Ok(match image { + match image { DecodingBuffer::U8(buf) => { - rev_hpredict_nsamp(buf, size, samples) + rev_hpredict_nsamp(buf, size, samples); }, DecodingBuffer::U16(buf) => { - rev_hpredict_nsamp(buf, size, samples) + rev_hpredict_nsamp(buf, size, samples); } - }) + } + Ok(()) } impl Decoder { @@ -167,6 +222,7 @@ impl Decoder { Decoder { reader: SmartReader::wrap(r, ByteOrder::LittleEndian), byte_order: ByteOrder::LittleEndian, + limits: Default::default(), next_ifd: None, ifd: None, width: 0, @@ -179,6 +235,11 @@ impl Decoder { }.init() } + pub fn with_limits(mut self, limits: Limits) -> Decoder { + self.limits = limits; + self + } + pub fn dimensions(&mut self) -> TiffResult<(u32, u32)> { Ok((self.width, self.height)) } @@ -271,10 +332,7 @@ impl Decoder { /// Returns `true` if there is at least one more image available. pub fn more_images(&self) -> bool { - match self.next_ifd { - Some(_) => true, - None => false - } + self.next_ifd.is_some() } /// Returns the byte_order @@ -296,12 +354,12 @@ impl Decoder { /// Reads a string #[inline] - pub fn read_string(&mut self, length: usize) -> Result { + pub fn read_string(&mut self, length: usize) -> TiffResult { let mut out = String::with_capacity(length); - self.reader.read_to_string(&mut out); + self.reader.read_to_string(&mut out)?; // Strings may be null-terminated, so we trim anything downstream of the null byte let trimmed = out.bytes().take_while(|&n| n != 0).collect::>(); - String::from_utf8(trimmed) + Ok(String::from_utf8(trimmed)?) } /// Reads a TIFF IFA offset/value field @@ -373,7 +431,9 @@ impl Decoder { Some(entry) => entry.clone(), }; - Ok(Some(try!(entry.val(self)))) + let limits = self.limits.clone(); + + Ok(Some(try!(entry.val(&limits, self)))) } /// Tries to retrieve a tag and convert it to the desired type. @@ -432,6 +492,11 @@ impl Decoder { }, method => return Err(TiffError::UnsupportedError(TiffUnsupportedError::UnsupportedCompressionMethod(method))) }; + + if bytes / buffer.byte_len() > max_uncompressed_length { + return Err(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered)); + } + Ok(match (color_type, buffer) { (ColorType:: RGB(8), DecodingBuffer::U8(ref mut buffer)) | (ColorType::RGBA(8), DecodingBuffer::U8(ref mut buffer)) | @@ -492,15 +557,21 @@ impl Decoder { self.initialize_strip_decoder()?; let index = self.strip_decoder.as_ref().unwrap().strip_index; - let offset = self.strip_decoder.as_ref().unwrap().strip_offsets[index]; - let byte_count = self.strip_decoder.as_ref().unwrap().strip_bytes[index]; + let offset = *self.strip_decoder.as_ref().unwrap().strip_offsets.get(index) + .ok_or(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered))?; + let byte_count = *self.strip_decoder.as_ref().unwrap().strip_bytes.get(index) + .ok_or(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered))?; let rows_per_strip = self.get_tag_u32(ifd::Tag::RowsPerStrip) .unwrap_or(self.height) as usize; let strip_height = std::cmp::min(rows_per_strip, self.height as usize - index * rows_per_strip); - let buffer_size = self.width as usize * strip_height * self.bits_per_sample.iter().count(); + let buffer_size = self.width as usize * strip_height * self.bits_per_sample.len(); + + if buffer.len() < buffer_size || byte_count as usize / buffer.byte_len() > buffer_size { + return Err(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered)); + } let units_read = self.expand_strip(buffer.copy(), offset, byte_count, buffer_size)?; @@ -542,12 +613,12 @@ impl Decoder { let buffer_size = self.width as usize * strip_height * self.bits_per_sample.iter().count(); let mut result = match self.bits_per_sample.iter().cloned().max().unwrap_or(8) { - n if n <= 8 => DecodingResult::U8(vec![0; buffer_size]), - n if n <= 16 => DecodingResult::U16(vec![0; buffer_size]), + n if n <= 8 => DecodingResult::new_u8(buffer_size, &self.limits)?, + n if n <= 16 => DecodingResult::new_u16(buffer_size, &self.limits)?, n => return Err(TiffError::UnsupportedError(TiffUnsupportedError::UnsupportedBitsPerChannel(n))), }; - self.read_strip_to_buffer(result.to_buffer(0))?; + self.read_strip_to_buffer(result.as_buffer(0))?; Ok(result) } @@ -563,13 +634,13 @@ impl Decoder { let buffer_size = self.width as usize * self.height as usize * self.bits_per_sample.iter().count(); let mut result = match self.bits_per_sample.iter().cloned().max().unwrap_or(8) { - n if n <= 8 => DecodingResult::U8(vec![0; buffer_size]), - n if n <= 16 => DecodingResult::U16(vec![0; buffer_size]), + n if n <= 8 => DecodingResult::new_u8(buffer_size, &self.limits)?, + n if n <= 16 => DecodingResult::new_u16(buffer_size, &self.limits)?, n => return Err(TiffError::UnsupportedError(TiffUnsupportedError::UnsupportedBitsPerChannel(n))), }; for i in 0..self.strip_count()? as usize { - self.read_strip_to_buffer(result.to_buffer(samples_per_strip * i))?; + self.read_strip_to_buffer(result.as_buffer(samples_per_strip * i))?; } Ok(result) diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index c80b84e..5e8f5e0 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -101,7 +101,11 @@ impl PackBitsReader { let mut buffer = Vec::new(); let mut read: usize = 0; while read < length { - read += try!(read_packbits_run(&mut reader, &mut buffer)); + let lread = read_packbits_run(&mut reader, &mut buffer)?; + if lread == 0 { + return Err(io::ErrorKind::UnexpectedEof.into()) + } + read += lread; } Ok((buffer.len(), PackBitsReader { buffer: io::Cursor::new(buffer), diff --git a/src/error.rs b/src/error.rs index 07776cd..d26d91b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,9 @@ pub enum TiffError { /// An I/O Error occurred while decoding the image IoError(io::Error), + + /// The Limits of the Decoder is exceeded, + LimitsExceeded, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -89,6 +92,7 @@ impl fmt::Display for TiffError { TiffError::UnsupportedError(ref f) => write!(fmt, "The Decoder does not support the \ image format `{}`", f), TiffError::IoError(ref e) => e.fmt(fmt), + TiffError::LimitsExceeded => write!(fmt, "The Decoder limits are exceeded"), } } } @@ -99,6 +103,7 @@ impl Error for TiffError { TiffError::FormatError(..) => "Format error", TiffError::UnsupportedError(..) => "Unsupported error", TiffError::IoError(..) => "IO error", + TiffError::LimitsExceeded => "Decoder limits exceeded", } } diff --git a/tests/encode_images.rs b/tests/encode_images.rs index f726f57..720df4b 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -139,8 +139,6 @@ fn test_rgb_u16_roundtrip() let img_file = File::open("./tests/images/rgb-3c-16b.tiff").expect("Cannot find test image!"); let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(16)); - let img_res = decoder.read_image(); - assert!(img_res.is_ok()); let image_data = match decoder.read_image().unwrap() { DecodingResult::U16(res) => res, diff --git a/tests/fuzz_images/inf_loop/id:000028,src:000508,op:havoc,rep:4 b/tests/fuzz_images/inf_loop/id:000028,src:000508,op:havoc,rep:4 new file mode 100644 index 0000000..5d152e6 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000028,src:000508,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/inf_loop/id:000029,src:000556+000125,op:splice,rep:64 b/tests/fuzz_images/inf_loop/id:000029,src:000556+000125,op:splice,rep:64 new file mode 100644 index 0000000..800ab3a Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000029,src:000556+000125,op:splice,rep:64 differ diff --git a/tests/fuzz_images/inf_loop/id:000030,src:000556+000125,op:splice,rep:128 b/tests/fuzz_images/inf_loop/id:000030,src:000556+000125,op:splice,rep:128 new file mode 100644 index 0000000..3645b47 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000030,src:000556+000125,op:splice,rep:128 differ diff --git a/tests/fuzz_images/inf_loop/id:000031,src:000527+000541,op:splice,rep:8 b/tests/fuzz_images/inf_loop/id:000031,src:000527+000541,op:splice,rep:8 new file mode 100644 index 0000000..5c17c01 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000031,src:000527+000541,op:splice,rep:8 differ diff --git a/tests/fuzz_images/inf_loop/id:000032,src:000558+000248,op:splice,rep:8 b/tests/fuzz_images/inf_loop/id:000032,src:000558+000248,op:splice,rep:8 new file mode 100644 index 0000000..cc9ad14 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000032,src:000558+000248,op:splice,rep:8 differ diff --git a/tests/fuzz_images/inf_loop/id:000033,src:000525+000106,op:splice,rep:4 b/tests/fuzz_images/inf_loop/id:000033,src:000525+000106,op:splice,rep:4 new file mode 100644 index 0000000..d30c8e7 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000033,src:000525+000106,op:splice,rep:4 differ diff --git a/tests/fuzz_images/inf_loop/id:000034,src:000560+000475,op:splice,rep:128 b/tests/fuzz_images/inf_loop/id:000034,src:000560+000475,op:splice,rep:128 new file mode 100644 index 0000000..467c73f Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000034,src:000560+000475,op:splice,rep:128 differ diff --git a/tests/fuzz_images/inf_loop/id:000039,src:000640,op:havoc,rep:4 b/tests/fuzz_images/inf_loop/id:000039,src:000640,op:havoc,rep:4 new file mode 100644 index 0000000..7a20240 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000039,src:000640,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/inf_loop/id:000040,src:000679+000165,op:splice,rep:4 b/tests/fuzz_images/inf_loop/id:000040,src:000679+000165,op:splice,rep:4 new file mode 100644 index 0000000..f77fa1f Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000040,src:000679+000165,op:splice,rep:4 differ diff --git a/tests/fuzz_images/inf_loop/id:000041,src:000698+000155,op:splice,rep:8 b/tests/fuzz_images/inf_loop/id:000041,src:000698+000155,op:splice,rep:8 new file mode 100644 index 0000000..a924847 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000041,src:000698+000155,op:splice,rep:8 differ diff --git a/tests/fuzz_images/inf_loop/id:000042,src:000698+000155,op:splice,rep:64 b/tests/fuzz_images/inf_loop/id:000042,src:000698+000155,op:splice,rep:64 new file mode 100644 index 0000000..e62f370 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000042,src:000698+000155,op:splice,rep:64 differ diff --git a/tests/fuzz_images/inf_loop/id:000043,src:000698+000155,op:splice,rep:128 b/tests/fuzz_images/inf_loop/id:000043,src:000698+000155,op:splice,rep:128 new file mode 100644 index 0000000..2220f92 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000043,src:000698+000155,op:splice,rep:128 differ diff --git a/tests/fuzz_images/inf_loop/id:000044,src:000698+000155,op:splice,rep:2 b/tests/fuzz_images/inf_loop/id:000044,src:000698+000155,op:splice,rep:2 new file mode 100644 index 0000000..3dd6c0b Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000044,src:000698+000155,op:splice,rep:2 differ diff --git a/tests/fuzz_images/inf_loop/id:000045,src:000700,op:havoc,rep:8 b/tests/fuzz_images/inf_loop/id:000045,src:000700,op:havoc,rep:8 new file mode 100644 index 0000000..61c9af1 Binary files /dev/null and b/tests/fuzz_images/inf_loop/id:000045,src:000700,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000000,sig:06,src:000000,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000000,sig:06,src:000000,op:havoc,rep:2 new file mode 100644 index 0000000..4137bb6 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000000,sig:06,src:000000,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000001,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000001,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..ff2b186 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000001,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000002,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000002,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..2fbf8f8 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000002,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000003,sig:06,src:000000,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000003,sig:06,src:000000,op:havoc,rep:4 new file mode 100644 index 0000000..bad49fd Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000003,sig:06,src:000000,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000004,sig:06,src:000000,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000004,sig:06,src:000000,op:havoc,rep:2 new file mode 100644 index 0000000..cc1418c Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000004,sig:06,src:000000,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000005,sig:06,src:000000,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000005,sig:06,src:000000,op:havoc,rep:2 new file mode 100644 index 0000000..b2f2ef1 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000005,sig:06,src:000000,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000006,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000006,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..f7ffa6e Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000006,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000007,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000007,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..20fa247 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000007,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000008,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000008,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..bb42789 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000008,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000009,sig:06,src:000000,op:havoc,rep:16 b/tests/fuzz_images/oom_crash/id:000009,sig:06,src:000000,op:havoc,rep:16 new file mode 100644 index 0000000..6d085bb Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000009,sig:06,src:000000,op:havoc,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000010,sig:06,src:000000,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000010,sig:06,src:000000,op:havoc,rep:4 new file mode 100644 index 0000000..75912c0 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000010,sig:06,src:000000,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000011,sig:06,src:000000,op:havoc,rep:16 b/tests/fuzz_images/oom_crash/id:000011,sig:06,src:000000,op:havoc,rep:16 new file mode 100644 index 0000000..685baa8 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000011,sig:06,src:000000,op:havoc,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000012,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000012,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..332852f Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000012,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000013,sig:06,src:000000,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000013,sig:06,src:000000,op:havoc,rep:4 new file mode 100644 index 0000000..672a0a5 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000013,sig:06,src:000000,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000014,sig:06,src:000000,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000014,sig:06,src:000000,op:havoc,rep:8 new file mode 100644 index 0000000..6c5e06e Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000014,sig:06,src:000000,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000015,sig:06,src:000000,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000015,sig:06,src:000000,op:havoc,rep:4 new file mode 100644 index 0000000..ea05220 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000015,sig:06,src:000000,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000016,sig:06,src:000000,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000016,sig:06,src:000000,op:havoc,rep:4 new file mode 100644 index 0000000..eb5ff12 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000016,sig:06,src:000000,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000021,sig:06,src:000004,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000021,sig:06,src:000004,op:havoc,rep:4 new file mode 100644 index 0000000..ab0904d Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000021,sig:06,src:000004,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000022,sig:06,src:000004,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000022,sig:06,src:000004,op:havoc,rep:4 new file mode 100644 index 0000000..68760e3 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000022,sig:06,src:000004,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000025,sig:06,src:000007,op:havoc,rep:16 b/tests/fuzz_images/oom_crash/id:000025,sig:06,src:000007,op:havoc,rep:16 new file mode 100644 index 0000000..de7ea9b Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000025,sig:06,src:000007,op:havoc,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000027,sig:06,src:000026+000062,op:splice,rep:8 b/tests/fuzz_images/oom_crash/id:000027,sig:06,src:000026+000062,op:splice,rep:8 new file mode 100644 index 0000000..e417ec9 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000027,sig:06,src:000026+000062,op:splice,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000028,sig:06,src:000027,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000028,sig:06,src:000027,op:havoc,rep:2 new file mode 100644 index 0000000..eee2505 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000028,sig:06,src:000027,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000029,sig:06,src:000027,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000029,sig:06,src:000027,op:havoc,rep:8 new file mode 100644 index 0000000..4613404 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000029,sig:06,src:000027,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000030,sig:06,src:000027,op:havoc,rep:16 b/tests/fuzz_images/oom_crash/id:000030,sig:06,src:000027,op:havoc,rep:16 new file mode 100644 index 0000000..52d06d8 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000030,sig:06,src:000027,op:havoc,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000031,sig:06,src:000027,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000031,sig:06,src:000027,op:havoc,rep:8 new file mode 100644 index 0000000..9158850 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000031,sig:06,src:000027,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000032,sig:06,src:000027,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000032,sig:06,src:000027,op:havoc,rep:8 new file mode 100644 index 0000000..500dfd8 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000032,sig:06,src:000027,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000033,sig:06,src:000027+000178,op:splice,rep:64 b/tests/fuzz_images/oom_crash/id:000033,sig:06,src:000027+000178,op:splice,rep:64 new file mode 100644 index 0000000..79884d4 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000033,sig:06,src:000027+000178,op:splice,rep:64 differ diff --git a/tests/fuzz_images/oom_crash/id:000034,sig:06,src:000040,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000034,sig:06,src:000040,op:havoc,rep:2 new file mode 100644 index 0000000..8c49ac1 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000034,sig:06,src:000040,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000035,sig:06,src:000056,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000035,sig:06,src:000056,op:havoc,rep:4 new file mode 100644 index 0000000..d704416 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000035,sig:06,src:000056,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000036,sig:06,src:000059+000071,op:splice,rep:8 b/tests/fuzz_images/oom_crash/id:000036,sig:06,src:000059+000071,op:splice,rep:8 new file mode 100644 index 0000000..b26198a Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000036,sig:06,src:000059+000071,op:splice,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000037,sig:06,src:000062,op:havoc,rep:32 b/tests/fuzz_images/oom_crash/id:000037,sig:06,src:000062,op:havoc,rep:32 new file mode 100644 index 0000000..7d68bf2 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000037,sig:06,src:000062,op:havoc,rep:32 differ diff --git a/tests/fuzz_images/oom_crash/id:000038,sig:06,src:000066,op:havoc,rep:32 b/tests/fuzz_images/oom_crash/id:000038,sig:06,src:000066,op:havoc,rep:32 new file mode 100644 index 0000000..1b1f2bd Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000038,sig:06,src:000066,op:havoc,rep:32 differ diff --git a/tests/fuzz_images/oom_crash/id:000039,sig:06,src:000066+000044,op:splice,rep:4 b/tests/fuzz_images/oom_crash/id:000039,sig:06,src:000066+000044,op:splice,rep:4 new file mode 100644 index 0000000..0df8bee Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000039,sig:06,src:000066+000044,op:splice,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000040,sig:06,src:000068+000059,op:splice,rep:16 b/tests/fuzz_images/oom_crash/id:000040,sig:06,src:000068+000059,op:splice,rep:16 new file mode 100644 index 0000000..dc70c47 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000040,sig:06,src:000068+000059,op:splice,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000041,sig:06,src:000073,op:havoc,rep:16 b/tests/fuzz_images/oom_crash/id:000041,sig:06,src:000073,op:havoc,rep:16 new file mode 100644 index 0000000..5998328 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000041,sig:06,src:000073,op:havoc,rep:16 differ diff --git a/tests/fuzz_images/oom_crash/id:000042,sig:06,src:000087,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000042,sig:06,src:000087,op:havoc,rep:8 new file mode 100644 index 0000000..0a0ec0f Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000042,sig:06,src:000087,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000043,sig:06,src:000181,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000043,sig:06,src:000181,op:havoc,rep:2 new file mode 100644 index 0000000..8db7a41 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000043,sig:06,src:000181,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000045,sig:06,src:000266,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000045,sig:06,src:000266,op:havoc,rep:2 new file mode 100644 index 0000000..3fa1a80 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000045,sig:06,src:000266,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000046,sig:06,src:000276,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000046,sig:06,src:000276,op:havoc,rep:4 new file mode 100644 index 0000000..5f199e1 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000046,sig:06,src:000276,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000048,sig:06,src:000313+000233,op:splice,rep:64 b/tests/fuzz_images/oom_crash/id:000048,sig:06,src:000313+000233,op:splice,rep:64 new file mode 100644 index 0000000..0bd4da7 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000048,sig:06,src:000313+000233,op:splice,rep:64 differ diff --git a/tests/fuzz_images/oom_crash/id:000051,sig:06,src:000329,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000051,sig:06,src:000329,op:havoc,rep:2 new file mode 100644 index 0000000..1ee4ca5 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000051,sig:06,src:000329,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000052,sig:06,src:000329,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000052,sig:06,src:000329,op:havoc,rep:2 new file mode 100644 index 0000000..d049006 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000052,sig:06,src:000329,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000053,sig:06,src:000329+000130,op:splice,rep:128 b/tests/fuzz_images/oom_crash/id:000053,sig:06,src:000329+000130,op:splice,rep:128 new file mode 100644 index 0000000..ad998d3 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000053,sig:06,src:000329+000130,op:splice,rep:128 differ diff --git a/tests/fuzz_images/oom_crash/id:000054,sig:06,src:000331,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000054,sig:06,src:000331,op:havoc,rep:8 new file mode 100644 index 0000000..10ed309 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000054,sig:06,src:000331,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oom_crash/id:000056,sig:06,src:000351+000306,op:splice,rep:64 b/tests/fuzz_images/oom_crash/id:000056,sig:06,src:000351+000306,op:splice,rep:64 new file mode 100644 index 0000000..69e8c52 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000056,sig:06,src:000351+000306,op:splice,rep:64 differ diff --git a/tests/fuzz_images/oom_crash/id:000058,sig:06,src:000361,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000058,sig:06,src:000361,op:havoc,rep:4 new file mode 100644 index 0000000..5c1e742 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000058,sig:06,src:000361,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000059,sig:06,src:000363,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000059,sig:06,src:000363,op:havoc,rep:4 new file mode 100644 index 0000000..3059080 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000059,sig:06,src:000363,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000060,sig:06,src:000363,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000060,sig:06,src:000363,op:havoc,rep:2 new file mode 100644 index 0000000..bc0e66e Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000060,sig:06,src:000363,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000061,sig:06,src:000364,op:havoc,rep:2 b/tests/fuzz_images/oom_crash/id:000061,sig:06,src:000364,op:havoc,rep:2 new file mode 100644 index 0000000..f27a1b0 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000061,sig:06,src:000364,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oom_crash/id:000063,sig:06,src:000060+000137,op:splice,rep:128 b/tests/fuzz_images/oom_crash/id:000063,sig:06,src:000060+000137,op:splice,rep:128 new file mode 100644 index 0000000..f4dc6ee Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000063,sig:06,src:000060+000137,op:splice,rep:128 differ diff --git a/tests/fuzz_images/oom_crash/id:000064,sig:06,src:000394,op:havoc,rep:4 b/tests/fuzz_images/oom_crash/id:000064,sig:06,src:000394,op:havoc,rep:4 new file mode 100644 index 0000000..2fa8b63 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000064,sig:06,src:000394,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oom_crash/id:000068,sig:06,src:000127,op:havoc,rep:8 b/tests/fuzz_images/oom_crash/id:000068,sig:06,src:000127,op:havoc,rep:8 new file mode 100644 index 0000000..cd94c13 Binary files /dev/null and b/tests/fuzz_images/oom_crash/id:000068,sig:06,src:000127,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oor_panic/id:000017,sig:06,src:000001,op:havoc,rep:64 b/tests/fuzz_images/oor_panic/id:000017,sig:06,src:000001,op:havoc,rep:64 new file mode 100644 index 0000000..f4aa223 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000017,sig:06,src:000001,op:havoc,rep:64 differ diff --git a/tests/fuzz_images/oor_panic/id:000018,sig:06,src:000001,op:havoc,rep:2 b/tests/fuzz_images/oor_panic/id:000018,sig:06,src:000001,op:havoc,rep:2 new file mode 100644 index 0000000..92f8191 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000018,sig:06,src:000001,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oor_panic/id:000019,sig:06,src:000001,op:havoc,rep:8 b/tests/fuzz_images/oor_panic/id:000019,sig:06,src:000001,op:havoc,rep:8 new file mode 100644 index 0000000..30f561c Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000019,sig:06,src:000001,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oor_panic/id:000023,sig:06,src:000006,op:havoc,rep:8 b/tests/fuzz_images/oor_panic/id:000023,sig:06,src:000006,op:havoc,rep:8 new file mode 100644 index 0000000..00812ec Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000023,sig:06,src:000006,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oor_panic/id:000024,sig:06,src:000006,op:havoc,rep:2 b/tests/fuzz_images/oor_panic/id:000024,sig:06,src:000006,op:havoc,rep:2 new file mode 100644 index 0000000..8dd081d Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000024,sig:06,src:000006,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oor_panic/id:000026,sig:06,src:000012,op:havoc,rep:2 b/tests/fuzz_images/oor_panic/id:000026,sig:06,src:000012,op:havoc,rep:2 new file mode 100644 index 0000000..9add392 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000026,sig:06,src:000012,op:havoc,rep:2 differ diff --git a/tests/fuzz_images/oor_panic/id:000044,sig:06,src:000224,op:havoc,rep:4 b/tests/fuzz_images/oor_panic/id:000044,sig:06,src:000224,op:havoc,rep:4 new file mode 100644 index 0000000..ad76e84 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000044,sig:06,src:000224,op:havoc,rep:4 differ diff --git a/tests/fuzz_images/oor_panic/id:000049,sig:06,src:000318,op:havoc,rep:8 b/tests/fuzz_images/oor_panic/id:000049,sig:06,src:000318,op:havoc,rep:8 new file mode 100644 index 0000000..53eb594 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000049,sig:06,src:000318,op:havoc,rep:8 differ diff --git a/tests/fuzz_images/oor_panic/id:000050,sig:06,src:000322,op:havoc,rep:32 b/tests/fuzz_images/oor_panic/id:000050,sig:06,src:000322,op:havoc,rep:32 new file mode 100644 index 0000000..4261783 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000050,sig:06,src:000322,op:havoc,rep:32 differ diff --git a/tests/fuzz_images/oor_panic/id:000055,sig:06,src:000343,op:havoc,rep:2 b/tests/fuzz_images/oor_panic/id:000055,sig:06,src:000343,op:havoc,rep:2 new file mode 100644 index 0000000..6952eb5 Binary files /dev/null and b/tests/fuzz_images/oor_panic/id:000055,sig:06,src:000343,op:havoc,rep:2 differ diff --git a/tests/fuzz_tests.rs b/tests/fuzz_tests.rs new file mode 100644 index 0000000..88cb64d --- /dev/null +++ b/tests/fuzz_tests.rs @@ -0,0 +1,42 @@ +extern crate tiff; + +use tiff::TiffResult; +use tiff::decoder::Decoder; + +use std::fs::File; + +fn test_directory bool>(path: &str, f: F) { + for entry in std::fs::read_dir(path).unwrap() { + let file = File::open(entry.unwrap().path()).unwrap(); + assert!(f(file)); + } +} + +fn decode_tiff(file: File) -> TiffResult<()> { + let mut decoder = Decoder::new(file)?; + decoder.read_image()?; + Ok(()) +} + +#[test] +fn oor_panic() { + test_directory("./tests/fuzz_images/oor_panic", |file| { + decode_tiff(file).ok(); + true + }); +} + +#[test] +fn oom_crash() { + test_directory("./tests/fuzz_images/oom_crash", |file| { + decode_tiff(file).is_err() + }); +} + +#[test] +fn inf_loop() { + test_directory("./tests/fuzz_images/inf_loop", |file| { + decode_tiff(file).ok(); + true + }); +}