Source: exiv2
Version: 0.28.5+dfsg-1
Severity: important
Tags: patch fixed-upstream trixie

Dear Maintainer,

In trixie, reading the metadata of a QuickTime/MP4 video with libexiv2
allocates (and zero-fills) a buffer as large as the video itself. A phone video of 8.46 GB makes any program that reads its metadata take ~8 GB of RAM
for a few seconds, which on a 16 GB desktop means heavy swapping and a
frozen application.

How I hit it: Gwenview, with a photo folder sorted by date. To sort,
Gwenview reads the date of every file through libexiv2, videos included,
on its GUI thread. Opening any photo in a camera folder that contains one
large video made Gwenview grow to 8.2-8.5 GB within seconds, freeze, and
end up killed via KWin's "not responding" dialog.


Cause
-----

src/quicktimevideo.cpp, QuickTimeVideo::decodeBlock(): after reading a box
header, the 5-byte tag buffer is resized to the full box size

    const auto newsize = static_cast<size_t>(size - hdrsize);
    if (newsize > buf.size()) {
      buf.resize(newsize);
    }
    tagDecoder(buf, newsize, recursion_depth + 1);

before tagDecoder() looks at the tag. For "mdat" (the media data, i.e.
almost the whole file) tagDecoder() only calls discard(size), a seek. The
buffer is never used beyond its first 4 bytes, but resize() has already
allocated and zero-filled it.

Upstream removed the resize in 0.28.9 (and main):
https://github.com/Exiv2/exiv2/commit/40be751eda (0.28.x, "Remove unnecessary resize")
  https://github.com/Exiv2/exiv2/commit/1a1f0ab960  (main)

forky and sid have 0.28.9+dfsg-1 and are fixed. Only trixie is affected.

The memory is bounded by the file size (decodeBlock() enforces that a box fits in the remaining file), so this is not a crafted-file issue; it hits
ordinary large videos.


Reproduce (plain trixie)
------------------------

    exiv2 -q -pa some-large-video.mp4

and look at the maximum RSS (e.g. /usr/bin/time -v, or getrusage).

Measured here, with the exiv2 0.28.5+dfsg-1 command line tool:

- 8.46 GB .mp4, stock libexiv2-28 0.28.5+dfsg-1: max RSS 6.6 GB, 4.1 s
  (the rest was pushed to swap)
- same file, libexiv2-28 rebuilt with the attached patch: max RSS 11 MB, 0.1 s
- 0.68 GB .mp4, stock: 657 MB; patched: 11 MB

In Gwenview (4:25.04.3-1), same folder, the A/B gave 7.3 GB peak + 676 MB
swap with the stock library, 120 MB with the patched one.


Patch
-----

The attached patch is the upstream commit, unchanged apart from the DEP-3
header; it applies to 0.28.5+dfsg-1 with a 1-line offset. In 0.28.5,
tagDecoder() only ever reads 4 bytes into this buffer, so removing the
resize is safe on its own. I rebuilt the Debian package with it
(0.28.5+dfsg-1 + this patch only): the package test suite passes (100%,
6/6 ctest groups).


Request
-------

Please consider the attached patch for a trixie stable update.


-- System Information:
Debian Release: 13 (trixie)
Kernel: Linux 6.12.116+deb13-amd64
Versions: libexiv2-28 0.28.5+dfsg-1, gwenview 4:25.04.3-1



--
Emmanuel Revah
Description: QuickTimeVideo: don't allocate a buffer the size of every box
 decodeBlock() resized its tag buffer to the full box size before tagDecoder()
 checked the ignore list, so the 'mdat' box (≈ the whole video) was allocated
 and zero-filled, then discarded. An 8.4 GB .mp4 → 8.4 GB RSS just to read
 metadata. tagDecoder() only ever reads 4 bytes into this buffer.
Origin: upstream, https://github.com/Exiv2/exiv2/commit/40be751eda (0.28.x), 1a1f0ab960 (main); released in 0.28.9
Applied-Upstream: 0.28.9
--- a/src/quicktimevideo.cpp
+++ b/src/quicktimevideo.cpp
@@ -616,9 +616,6 @@ void QuickTimeVideo::decodeBlock(size_t recursion_depth, std::string const& ente
 
   // std::cerr<<"Tag=>"<<buf.data()<<"     size=>"<<size-hdrsize << std::endl;
   const auto newsize = static_cast<size_t>(size - hdrsize);
-  if (newsize > buf.size()) {
-    buf.resize(newsize);
-  }
   tagDecoder(buf, newsize, recursion_depth + 1);
 }  // QuickTimeVideo::decodeBlock

Reply via email to