PR #26079 opened by deruina
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26079
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26079.patch

# Summary of changes

`mov_read_iref_dimg()` reads `reference_count` item IDs without checking that 
they fit in the `dimg` sub-box. If a file declares more entries than the 
sub-box holds, the demuxer reads IDs from whatever comes after it, before 
`mov_read_iref()` seeks back to the end of the sub-box.

The `cdsc` path already has this bound (dcc18b6077), but `dimg` was left 
without it. This passes the sub-box size down and adds the same check.

Tested with a small HEIF where the `dimg` sub-box has room for one ID: count=1 
parses the same as before, count=3 is now rejected with "iref dimg entry count 
3 exceeds the sub-box size". The HEIF/AVIF FATE tests still pass.

I used AI (Claude) to help find and write this; I reviewed and tested it myself.


>From 07862b9ec8387e40dc08a55941403124d17e9bf9 Mon Sep 17 00:00:00 2001
From: Dean Ruina <[email protected]>
Date: Sat, 14 Nov 2026 20:02:21 +0000
Subject: [PATCH] avformat/mov: bound the iref dimg entry count by the sub-box
 size

mov_read_iref_dimg() reads reference_count to_item_IDs without checking
that they fit in the dimg sub-box. A box that declares more entries than
it holds makes the demuxer read item IDs from whatever follows the
sub-box (the next iref sub-box or the next box in meta) before
mov_read_iref() seeks back to the end of the sub-box.

The thmb/cdsc path got the same bound in dcc18b6077 (avformat/mov: bound
the iref thmb/cdsc entry count by the sub-box size); dimg was left
without it. Pass the sub-box size down and apply the same check.

This is hardening; no memory safety issue is known to depend on it.

Signed-off-by: Dean Ruina <[email protected]>
---
 libavformat/mov.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index ca33cb2285b..b41cdd7b3ef 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -9834,10 +9834,12 @@ fail:
     return ret;
 }
 
-static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version)
+static int mov_read_iref_dimg(MOVContext *c, AVIOContext *pb, int version,
+                              uint32_t size)
 {
     HEIFItem *item = NULL;
     int entries;
+    int item_id_size = version ? 4 : 2;
     int from_item_id = version ? avio_rb32(pb) : avio_rb16(pb);
     int ret;
 
@@ -9876,6 +9878,11 @@ static int mov_read_iref_dimg(MOVContext *c, AVIOContext 
*pb, int version)
                "Derived image item references no input images\n");
         return AVERROR_INVALIDDATA;
     }
+    if ((int64_t)entries * item_id_size > (int64_t)size - item_id_size - 2) {
+        av_log(c->fc, AV_LOG_ERROR, "iref dimg entry count %d exceeds the 
sub-box size\n",
+               entries);
+        return AVERROR_INVALIDDATA;
+    }
 
     av_log(c->fc, AV_LOG_TRACE, "dimg: from_item_id %d, entries %d\n",
            from_item_id, entries);
@@ -10013,7 +10020,7 @@ static int mov_read_iref(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
         type = avio_rl32(pb);
         switch (type) {
         case MKTAG('d','i','m','g'):
-            ret = mov_read_iref_dimg(c, pb, version);
+            ret = mov_read_iref_dimg(c, pb, version, size - 8);
             if (ret < 0)
                 return ret;
             break;
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to