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


>From 7b936c7773e3bf337f36cf2d0c63ab7d9fe9f83f Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 18:36:54 +0100
Subject: [PATCH 1/4] avcodec/exr: count the DWA channel rules in the
 compressed size check

Fixes: out of array read
Fixes: EqK8nzVwkNsr
Fixes: exr-dwa-skip-rle.exr
Fixes: exr-dwa-skip-ac.exr
Out of array read Replicated through UnModified FFmpeg with ASAN

Found-by: Martin Mirchev, with an LLM-enhanced static analysis engine; verified 
manually by Martin Mirchev
---
 libavcodec/exr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index d53bd9ba405..a61ba3b593a 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -1021,7 +1021,7 @@ static int dwa_uncompress(const EXRContext *s, const 
uint8_t *src, int compresse
     dc_count = AV_RL64(src + 72);
     ac_compression = AV_RL64(src + 80);
 
-    if (   compressed_size < (uint64_t)(lo_size | ac_size | dc_size | 
rle_csize) || compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize
+    if (   compressed_size < (uint64_t)(lo_size | ac_size | dc_size | 
rle_csize)
         || ac_count > (uint64_t)INT_MAX/2
     )
         return AVERROR_INVALIDDATA;
@@ -1042,7 +1042,7 @@ static int dwa_uncompress(const EXRContext *s, const 
uint8_t *src, int compresse
 
     bytestream2_init(&gb, src + 88, compressed_size - 88);
     skip = bytestream2_get_le16(&gb);
-    if (skip < 2)
+    if (skip < 2 || compressed_size < 88LL + skip + lo_size + ac_size + 
dc_size + rle_csize)
         return AVERROR_INVALIDDATA;
 
     bytestream2_skip(&gb, skip - 2);
-- 
2.54.0


>From 08a9b1d06ef6333f5fcd8342c5cce1b1c3e72305 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 18:40:21 +0100
Subject: [PATCH 2/4] avformat/mpegts: free a PESContext with its filter unless
 the stream owns it

Fixes: double free
Fixes: fHugfEQx8Ec0
Fixes: ffmpeg-n902-pmt-move-retype-20261027.ts
Double free Replicated through UnModified FFmpeg with ASAN

Found-by: Min Kang
---
 libavformat/mpegts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index ae4d177083c..270e6e58671 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -600,7 +600,7 @@ static void mpegts_close_filter(MpegTSContext *ts, 
MpegTSFilter *filter)
         av_buffer_unref(&pes->buffer);
         /* referenced private data will be freed later in
          * avformat_close_input (pes->st->priv_data == pes) */
-        if (!pes->st || pes->merged_st || !pes->st->priv_data) {
+        if (!pes->st || pes->st->priv_data != pes) {
             av_freep(&filter->u.pes_filter.opaque);
         }
     }
-- 
2.54.0


>From c08d4de41677a1e13b51d4edf5d82795841bd154 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 18:46:15 +0100
Subject: [PATCH 3/4] avformat/mpegts: remove unused PESContext.merged_st

mpegts_close_filter() no longer reads it.
---
 libavformat/mpegts.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 270e6e58671..88598d75414 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -288,7 +288,6 @@ typedef struct PESContext {
     uint8_t header[MAX_PES_HEADER_SIZE];
     AVBufferRef *buffer;
     SLConfigDescr sl;
-    int merged_st;
 } PESContext;
 
 EXTERN const FFInputFormat ff_mpegts_demuxer;
@@ -2863,7 +2862,6 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t 
*section, int section_len
                 if (st) {
                     pes->st = st;
                     pes->stream_type = stream_type;
-                    pes->merged_st = 1;
                 }
             }
             if (!pes->st) {
@@ -2882,7 +2880,6 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t 
*section, int section_len
                 if (st) {
                     pes->st = st;
                     pes->stream_type = stream_type;
-                    pes->merged_st = 1;
                 }
             }
             if (pes && !pes->st) {
-- 
2.54.0


>From 400f871a0a69753b7dc3411cc53c833a236e8036 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 11 Nov 2026 18:54:19 +0100
Subject: [PATCH 4/4] avcodec/cbs_h2645: compute the size of the assembled
 fragment in 64 bits

With a 32 bit size_t the sum of the unit sizes with the worst case
emulation prevention overhead could wrap.

This is not a security issue: the units are written from one packet of
at most INT_MAX bytes, 3/2 of that fits in 32 bits, and the more than
2^28 units needed to wrap the sum do not fit in a 32 bit address space.

Found-by: Anthony Hurtado
Ticket: frJt2MGMT7O1
---
 libavcodec/cbs_h2645.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 9329554cab9..653e4117a21 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -324,7 +324,8 @@ int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext 
*ctx,
                                        CodedBitstreamFragment *frag)
 {
     uint8_t *data;
-    size_t max_size, dp, sp;
+    uint64_t max_size;
+    size_t dp, sp;
     int err, i, zero_run;
 
     for (i = 0; i < frag->nb_units; i++) {
@@ -335,8 +336,10 @@ int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext 
*ctx,
     max_size = 0;
     for (i = 0; i < frag->nb_units; i++) {
         // Start code + content with worst-case emulation prevention.
-        max_size += 4 + frag->units[i].data_size * 3 / 2;
+        max_size += 4 + frag->units[i].data_size * UINT64_C(3) / 2;
     }
+    if (max_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(ENOMEM);
 
     data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!data)
-- 
2.54.0

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

Reply via email to