CVE-2018-1999015: Out-of-bounds Read (CWE-125)
value: FFmpeg before commit 5aba5b89d0b1d73164d3b81764828bb8b20ff32a contains an out of array read vulnerability in ASF_F format demuxer that can result in heap memory reading. This attack appear to be exploitable via specially crafted ASF file that has to provided as input. This vulnerability appears to have been fixed in 5aba5b89d0b1d73164d3b81764828bb8b20ff32a and later.
CWE description: The product reads data past the end, or before the beginning, of the intended buffer.
CVSS v3 base score: 6.5
Published: 2018-07-23T15:29Z
Repository: https://github.com/FFmpeg/FFmpeg
Language: C
Files changed: libavcodec/mpeg4videodec.c
Commit date: 2018-07-04 23:14:24 +0200
Commit message: avcodec/mpeg4videodec: Check for bitstream end in read_quant_matrix_ext()

Fixes: out of array read
Fixes: asff-crash-0e53d0dc491dfdd507530b66562812fbd4c36678

Found-by: Paul Ch <paulcher@icloud.com>
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Diff: diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index 2df525e03a78..24c280df4658 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -2865,48 +2865,57 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
         s->v_edge_pos = s->height;
     }
     return 0;
 }
 
-static void read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
+static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
 {
     int i, j, v;
 
     if (get_bits1(gb)) {
+        if (get_bits_left(gb) < 64*8)
+            return AVERROR_INVALIDDATA;
         /* intra_quantiser_matrix */
         for (i = 0; i < 64; i++) {
             v = get_bits(gb, 8);
             j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
             s->intra_matrix[j]        = v;
             s->chroma_intra_matrix[j] = v;
         }
     }
 
     if (get_bits1(gb)) {
+        if (get_bits_left(gb) < 64*8)
+            return AVERROR_INVALIDDATA;
         /* non_intra_quantiser_matrix */
         for (i = 0; i < 64; i++) {
             get_bits(gb, 8);
         }
     }
 
     if (get_bits1(gb)) {
+        if (get_bits_left(gb) < 64*8)
+            return AVERROR_INVALIDDATA;
         /* chroma_intra_quantiser_matrix */
         for (i = 0; i < 64; i++) {
             v = get_bits(gb, 8);
             j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
             s->chroma_intra_matrix[j] = v;
         }
     }
 
     if (get_bits1(gb)) {
+        if (get_bits_left(gb) < 64*8)
+            return AVERROR_INVALIDDATA;
         /* chroma_non_intra_quantiser_matrix */
         for (i = 0; i < 64; i++) {
             get_bits(gb, 8);
         }
     }
 
     next_start_code_studio(gb);
+    return 0;
 }
 
 static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
 {
     uint32_t startcode;

Vulnerable code: static void read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
Fixed code: static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
        if (get_bits_left(gb) < 64*8)
            return AVERROR_INVALIDDATA;
        if (get_bits_left(gb) < 64*8)
            return AVERROR_INVALIDDATA;
        if (get_bits_left(gb) < 64*8)
            return AVERROR_INVALIDDATA;
        if (get_bits_left(gb) < 64*8)
            return AVERROR_INVALIDDATA;
    return 0;