Commit b9dcde1f88 for ffmpeg
commit b9dcde1f88bc636b8a4b372862a01a13b3003473
Author: Dean Ruina <ruinadd@gmail.com>
Date: Sat Nov 14 20:02:21 2026 +0000
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 <ruinadd@gmail.com>
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 30be8d5b67..f35334b064 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -9835,10 +9835,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;
@@ -9877,6 +9879,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);
@@ -10014,7 +10021,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;