From 7dc5e0186a2484fea01b43bdd2823eae54f00bcd Mon Sep 17 00:00:00 2001
From: Sanil Dulal <sanildulal187@gmail.com>
Date: Fri, 18 Sep 2026 07:50:48 +0545
Subject: [PATCH] avformat/movenc: pad the track extradata copies

ff_mov_write_packet() stores extradata delivered mid stream via
AV_PKT_DATA_NEW_EXTRADATA in a new stsd entry using av_memdup(), which
allocates exactly sd->size bytes. mov_write_av1c_tag() then hands that
buffer to ff_isom_write_av1c(), which parses it with parse_obu_header()
and init_get_bits8(). FFmpeg bitreaders read in 32 bit units and therefore
require AV_INPUT_BUFFER_PADDING_SIZE trailing bytes, which av_memdup() does
not allocate, so the parse reads past the end of the buffer.

The extradata handed to the muxer is padded, demuxers allocate
AVCodecParameters.extradata as extradata_size + AV_INPUT_BUFFER_PADDING_SIZE
with the padding zeroed, and mov_write_header() already copies it the padded
way. The padding is lost only in the two av_memdup() copies.

Found by fuzzing the muxers with an ASan build, for an AV1 stream written
through the avif/image2 path:

  heap-buffer-overflow READ of size 4
    get_bits                 libavcodec/get_bits.h
    parse_obu_header         libavcodec/av1_parse.h
    ff_isom_write_av1c       libavformat/av1.c
    mov_write_av1c_tag       libavformat/movenc.c
    mov_write_ipco_tag       libavformat/movenc.c
    mov_write_meta_tag       libavformat/movenc.c
    mov_write_single_packet  libavformat/movenc.c

The values decoded from out of bounds memory are written into the av1C box.
Reverting only the stsd copy reproduces the overflow on 21 of 25 collected
test cases, reverting only the other copy does not, so that copy is the one
that is reachable. The remaining av_memdup() copies of track extradata are
converted as well so that every buffer the box writers may parse carries the
padding they assume; they are not known to be reachable today.

fate-lavf-mov, fate-lavf-mp4, fate-lavf-ismv and fate-lavf-nut pass
unchanged.

Signed-off-by: Sanil Dulal <sanildulal187@gmail.com>
---
 libavformat/movenc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ecc6544..b750fd8 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -7094,11 +7094,13 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (trk->extradata_size[0] == 0 && par->extradata_size > 0 &&
         !TAG_IS_AVCI(trk->tag) &&
         (par->codec_id != AV_CODEC_ID_DNXHD)) {
-        trk->extradata[0] = av_memdup(par->extradata, par->extradata_size);
+        trk->extradata[0] = av_malloc(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!trk->extradata[0]) {
             ret = AVERROR(ENOMEM);
             goto err;
         }
+        memcpy(trk->extradata[0], par->extradata, par->extradata_size);
+        memset(trk->extradata[0] + par->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
         trk->extradata_size[0] = par->extradata_size;
     }
 
@@ -7144,9 +7146,11 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return AVERROR(ENOMEM);
             trk->extradata_size = extradata_size;
 
-            trk->extradata[trk->stsd_count] = av_memdup(sd->data, sd->size);
+            trk->extradata[trk->stsd_count] = av_malloc(sd->size + AV_INPUT_BUFFER_PADDING_SIZE);
             if (!trk->extradata[trk->stsd_count])
                 return AVERROR(ENOMEM);
+            memcpy(trk->extradata[trk->stsd_count], sd->data, sd->size);
+            memset(trk->extradata[trk->stsd_count] + sd->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
             trk->extradata_size[trk->stsd_count] = sd->size;
             trk->last_stsd_index = trk->stsd_count;
@@ -7857,9 +7861,11 @@ static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
             return AVERROR(ENOMEM);
     }
 
-    track->extradata[0] = av_memdup(stub_header, sizeof(stub_header));
+    track->extradata[0] = av_malloc(sizeof(stub_header) + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!track->extradata[0])
         return AVERROR(ENOMEM);
+    memcpy(track->extradata[0], stub_header, sizeof(stub_header));
+    memset(track->extradata[0] + sizeof(stub_header), 0, AV_INPUT_BUFFER_PADDING_SIZE);
     track->extradata_size[0] = sizeof(stub_header);
 
     pkt->stream_index = tracknum;
-- 
2.42.0.windows.2

