Repository navigation
kamadak_exif: Fix out-of-bounds write, use-after-free and memory blowup - #265
Open
dhivadhiva wants to merge 4 commits into
Open
dhivadhiva wants to merge 4 commits into
dhivadhiva wants to merge 4 commits into
Conversation
In only knew IFDs 0 and 1. kamadak-exif reads up to 8 IFDs, and a field
from IFD 2 or later came back as In::UNKNOWN (-1), so In::index()
returned 65535. Writer::set_jpeg(), set_strips() and set_tiles() index
two-element arrays with it, so for a TIFF with three IFDs
writer.set_jpeg(thumb, exif->fields()[2].ifd_num());
wrote far outside the Writer object. get_field() with such an In also
looked in IFD 0 instead. In now keeps kamadak's IFD number, and Writer
keeps its per-IFD data in a map keyed by it. The setters also hand the
Rust writer the new data before freeing the old.
The defaulted Writer move assignment moved the storage members first,
freeing the target's fields and data, and then swapped the Rust writers
(Crubit moves are swaps), so the source was left with a Rust writer that
referenced freed memory. Reusing it
a = std::move(b);
b.push_field(...);
b.write(...);
copied freed memory into the output. Write the move assignment out so
that it drops the target's Rust writer first and leaves the source with
an empty one.
Also stop two Rust panics that abort the C++ caller: pushing a moved-from
Field, which Writer now ignores, and UIntValue::get() on a value that is
not an unsigned integer, which now returns nullopt.
Add exif_bridge_test with regression tests for these.
kamadak-exif parses field values lazily, but the bridge parsed all of them as soon as a file was read: the Rust reader scanned fields() for Make and MakerNote, and the Exif constructor copied fields() into its cache. Any number of entries can point at the same bytes, so a 64 KB file with 5450 entries that each cover the whole file took 1 GiB in Reader::read_raw(), even for a caller that only wanted one tag through get_field(). - Look up Make and MakerNote with get_field(), in the TIFF and Exif contexts of each IFD, which is where the scan could find them. - Build the C++ field cache on the first fields() call, and document that fields() parses every value. - The bridge's maker note parser copies its values eagerly too, so limit the bytes it copies to 4x the TIFF data. A maker note over the limit is dropped, as a truncated one already is. Peak RSS of read_raw() on 64 KB inputs with 5450 such entries, in IFD 0 or in a Canon maker note, goes from 1027 MiB to 6 MiB.
main now exports its own kamadak_exif/exif_bridge_test.cc and CTest setup (dec4a4d). Keep both: main's CMakeLists.txt, plus absl::base as a public dependency of exif_bridge for absl::call_once; and main's tests followed by this branch's, renamed to the ExifBridgeTest suite and switched to IsOk().
main replaced the bridge's Vec wrappers with Crubit's rs::Vec and slices (7d5896e). Keep that, and keep this branch's lazy field cache: Exif's constructor fills only the buffer and maker note fields, and fields() and the Rust fields() doc are moved over to rs::Vec / Vec<Field>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
kamadak_exifhas three memory problems, two of them reachable from file contents alone. This fixes them and two panics that abort the process, and adds regression tests toexif_bridge_test.1. Out-of-bounds write in
Writerfor IFD 2 and laterkamadak-exif reads up to 8 IFDs, but
Inonly knew 0 and 1. A field from IFD 2 or later came back asIn::UNKNOWN(-1), soIn::index()returned 65535, andWriter::set_jpeg(),set_strips()andset_tiles()index two-elementstd::arrays with it:With
-D_GLIBCXX_ASSERTIONSthis fails__n < this->size(); without it, it writes far outside the object.get_field(tag, that_in)also looked in IFD 0 instead (it returned 100 where IFD 2 holds 102).Innow keeps kamadak's IFD number, andWriterkeeps its per-IFD data in astd::mapkeyed by it. The setters also hand the Rust writer the new data before freeing the old.2. Use-after-free after
Writermove assignmentThe defaulted move assignment moved the storage members first, which freed the target's fields and data, and then swapped the Rust writers (Crubit moves are swaps). The source was left with a Rust writer that referenced the freed memory:
The move assignment is now written out: it drops the target's Rust writer first and leaves the source with an empty one. The defaulted move constructor was already safe, because the storage members hand their blocks over, and now says so.
3. Small files taking about 1 GiB in
read_raw()kamadak-exif parses field values lazily, but the bridge parsed all of them on read: the Rust reader scanned
fields()for Make and MakerNote, and theExifconstructor copiedfields()into its cache. Entries can all point at the same bytes, so a 64 KB file with 5450 entries that each cover the whole file peaked at 1027 MiB inread_raw(), even for callers that only useget_field(), such asExifReader::GetUIntTag(). The bridge's own maker note parser copied its values eagerly in the same way.get_field(), in the TIFF and Exif contexts of each IFD, which is where the scan could find them.fields()call, withabsl::call_once, and held on the heap so thatExifstays movable.fields()now documents that it parses every value.read_raw()peak RSS before4. Panics that abort the process
Fieldhit anunwrap()in Rust.Writernow ignores such a field.UIntValue::get()had_ => panic!(), reachable throughUIntValue::new_(), which accepts any value. It returns nullopt instead.Testing
9 new tests in
exif_bridge_test(main has since exported that target with its own 18 tests; the merge keeps both, with mine renamed to theExifBridgeTestsuite):main, 4 of them fail: the IFD numbers ({0, 1, 65535}), the out-of-bounds write (abort), the move assignment (a freed field shows up in the output) and the moved-from field (Rust panic, abort).Exif) and the new maker note limit.-D_GLIBCXX_ASSERTIONS. The first commit builds and passes its 6 tests on its own, andkamadak_exif_examplestill runs.Compatibility
Writer's move assignment is now user-declared and stillnoexcept.In::UNKNOWNstays but is never returned, and a lookup with it finds nothing instead of searching IFD 0.Exif::fields()parses on its first call instead of in the constructor.exif_bridgenow linksabsl::base(public) forabsl::call_once.