Skip to content

kamadak_exif: Fix out-of-bounds write, use-after-free and memory blowup - #265

Open
dhivadhiva wants to merge 4 commits into
google:mainfrom
dhivadhiva:exif-memory-safety
Open

dhivadhiva wants to merge 4 commits into
google:mainfrom
dhivadhiva:exif-memory-safety

Conversation

@dhivadhiva

@dhivadhiva dhivadhiva commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

kamadak_exif has 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 to exif_bridge_test.

1. Out-of-bounds write in Writer for IFD 2 and later

kamadak-exif reads up to 8 IFDs, but In only knew 0 and 1. A field from IFD 2 or later came back as In::UNKNOWN (-1), so In::index() returned 65535, and Writer::set_jpeg(), set_strips() and set_tiles() index two-element std::arrays with it:

absl::StatusOr<Exif> exif = Reader().read_raw(tiff_with_three_ifds);
Writer w;
w.set_jpeg(thumb, exif->fields()[2].ifd_num());  // writes at index 65535

With -D_GLIBCXX_ASSERTIONS this 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).

In now keeps kamadak's IFD number, and Writer keeps its per-IFD data in a std::map keyed by it. The setters also hand the Rust writer the new data before freeing the old.

2. Use-after-free after Writer move assignment

The 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:

a = std::move(b);
b.push_field(...);
b.write(false);  // ASan: heap-use-after-free; freed bytes end up in the output

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 the Exif constructor copied fields() 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 in read_raw(), even for callers that only use get_field(), such as ExifReader::GetUIntTag(). The bridge's own maker note parser copied its values eagerly in the same way.

  • Make and MakerNote are looked up with get_field(), in the TIFF and Exif contexts of each IFD, which is where the scan could find them.
  • The C++ field cache is built on the first fields() call, with absl::call_once, and held on the heap so that Exif stays movable. fields() now documents that it parses every value.
  • The maker note parser stops at 4x the TIFF data size and drops a maker note over that limit, as it already drops a truncated one.
64 KB input, 5450 entries over the same bytes read_raw() peak RSS before after
Entries in IFD 0 1027 MiB 6 MiB
Entries in a Canon maker note 1027 MiB 5 MiB

4. Panics that abort the process

  • Pushing a moved-from Field hit an unwrap() in Rust. Writer now ignores such a field.
  • UIntValue::get() had _ => panic!(), reachable through UIntValue::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 the ExifBridgeTest suite):

  • On 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).
  • The other 5 cover behaviour the rewrite has to keep (replacing data, move construction, Canon maker note parsing, a moved Exif) and the new maker note limit.
  • All 9 pass on this branch, also under ASan with -D_GLIBCXX_ASSERTIONS. The first commit builds and passes its 6 tests on its own, and kamadak_exif_example still runs.
  • A local run of this repo's CI workflow, which now runs all CTest tests, passes on a clean checkout of the latest merge with main (Crubit eb002c3, nightly-2026-11-26): 28/28 tests, including main's 18 and the example.

Compatibility

  • No public signatures change. Writer's move assignment is now user-declared and still noexcept.
  • In::UNKNOWN stays 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_bridge now links absl::base (public) for absl::call_once.
  • Maker notes whose values add up to more than 4x the TIFF data are dropped.

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>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant