CVE-2023-47470: Out-of-bounds Write (CWE-787)
value: Buffer Overflow vulnerability in Ffmpeg before github commit 4565747056a11356210ed8edcecb920105e40b60 allows a remote attacker to achieve an out-of-array write, execute arbitrary code, and cause a denial of service (DoS) via the ref_pic_list_struct function in libavcodec/evc_ps.c
CWE description: The product writes data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 7.8
Published: 2023-11-16T03:15Z
Repository: https://github.com/FFmpeg/FFmpeg
Language: C
Files changed: libavcodec/evc_ps.c
Commit date: 2023-09-15 17:13:54 +0200
Commit message: avcodec/evc_ps: Check ref_pic_num and sps_max_dec_pic_buffering_minus1

Fixes: out of array write

Found-by: dongsookim@korea.ac.kr
Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Diff: diff --git a/libavcodec/evc_ps.c b/libavcodec/evc_ps.c
index 64384a392c24c..28cf5378e42f3 100644
--- a/libavcodec/evc_ps.c
+++ b/libavcodec/evc_ps.c
@@ -22,14 +22,18 @@
 #include "evc_ps.h"
 
 #define EXTENDED_SAR 255
 
 // @see ISO_IEC_23094-1 (7.3.7 Reference picture list structure syntax)
-static int ref_pic_list_struct(GetBitContext *gb, RefPicListStruct *rpl)
+static int ref_pic_list_struct(EVCParserSPS *sps, GetBitContext *gb, RefPicListStruct *rpl)
 {
     uint32_t delta_poc_st, strp_entry_sign_flag = 0;
     rpl->ref_pic_num = get_ue_golomb_long(gb);
+
+    if ((unsigned)rpl->ref_pic_num  > sps->sps_max_dec_pic_buffering_minus1)
+        return AVERROR_INVALIDDATA;
+
     if (rpl->ref_pic_num > 0) {
         delta_poc_st = get_ue_golomb_long(gb);
 
         rpl->ref_pics[0] = delta_poc_st;
         if (rpl->ref_pics[0] != 0) {
@@ -237,30 +241,38 @@ int ff_evc_parse_sps(GetBitContext *gb, EVCParamSets *ps)
 
     if (!sps->sps_rpl_flag)
         sps->max_num_tid0_ref_pics = get_ue_golomb_31(gb);
     else {
         sps->sps_max_dec_pic_buffering_minus1 = get_ue_golomb_long(gb);
+        if ((unsigned)sps->sps_max_dec_pic_buffering_minus1 > 16 - 1)
+            return AVERROR_INVALIDDATA;
         sps->long_term_ref_pic_flag = get_bits1(gb);
         sps->rpl1_same_as_rpl0_flag = get_bits1(gb);
         sps->num_ref_pic_list_in_sps[0] = get_ue_golomb(gb);
 
         if ((unsigned)sps->num_ref_pic_list_in_sps[0] >= EVC_MAX_NUM_RPLS) {
             ret = AVERROR_INVALIDDATA;
             goto fail;
         }
 
-        for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i)
-            ref_pic_list_struct(gb, &sps->rpls[0][i]);
+        for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i) {
+            ret = ref_pic_list_struct(sps, gb, &sps->rpls[0][i]);
+            if (ret < 0)
+                goto fail;
+        }
 
         if (!sps->rpl1_same_as_rpl0_flag) {
             sps->num_ref_pic_list_in_sps[1] = get_ue_golomb(gb);
             if ((unsigned)sps->num_ref_pic_list_in_sps[1] >= EVC_MAX_NUM_RPLS) {
                 ret = AVERROR_INVALIDDATA;
                 goto fail;
             }
-            for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i)
-                ref_pic_list_struct(gb, &sps->rpls[1][i]);
+            for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i) {
+                ret = ref_pic_list_struct(sps, gb, &sps->rpls[1][i]);
+                if (ret < 0)
+                    goto fail;
+            }
         }
     }
 
     sps->picture_cropping_flag = get_bits1(gb);
 

Vulnerable code: static int ref_pic_list_struct(GetBitContext *gb, RefPicListStruct *rpl)
        for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i)
            ref_pic_list_struct(gb, &sps->rpls[0][i]);
            for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i)
                ref_pic_list_struct(gb, &sps->rpls[1][i]);
Fixed code: static int ref_pic_list_struct(EVCParserSPS *sps, GetBitContext *gb, RefPicListStruct *rpl)

    if ((unsigned)rpl->ref_pic_num  > sps->sps_max_dec_pic_buffering_minus1)
        return AVERROR_INVALIDDATA;

        if ((unsigned)sps->sps_max_dec_pic_buffering_minus1 > 16 - 1)
            return AVERROR_INVALIDDATA;
        for (int i = 0; i < sps->num_ref_pic_list_in_sps[0]; ++i) {
            ret = ref_pic_list_struct(sps, gb, &sps->rpls[0][i]);
            if (ret < 0)
                goto fail;
        }
            for (int i = 0; i < sps->num_ref_pic_list_in_sps[1]; ++i) {
                ret = ref_pic_list_struct(sps, gb, &sps->rpls[1][i]);
                if (ret < 0)
                    goto fail;
            }