Vulnerability: FFmpeg before 2017-02-04 has an out-of-bounds write caused by a heap-based buffer overflow related to the decode_frame_common function in libavcodec/pngdec.c.
Vulnerability ID: CVE-2017-7863
CWE: CWE-787
Severity score: 6.4
Project: https://github.com/FFmpeg/FFmpeg
Published: 2017-04-14
File: libavcodec/pngdec.c
Commit message: avcodec/pngdec: Check trns more completely

Fixes out of array access
Fixes: 546/clusterfuzz-testcase-4809433909559296

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/targets/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
Commit author: Michael Niedermayer

Vulnerable version (before fix):
 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, {     int v, i;      if (s->color_type == PNG_COLOR_TYPE_PALETTE) {         if (length > 256 || !(s->state & PNG_PLTE))             return AVERROR_INVALIDDATA; static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,         }     } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {         if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||             (s->color_type == PNG_COLOR_TYPE_RGB && length != 6))             return AVERROR_INVALIDDATA;          for (i = 0; i < length / 2; i++) { static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,         size_t raw_bpp = s->bpp - byte_depth;         unsigned x, y;          for (y = 0; y < s->height; ++y) {             uint8_t *row = &s->image_buf[s->image_linesize * y]; 

Fixed version (after fix):
 static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s, {     int v, i;      if (!(s->state & PNG_IHDR)) {         av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");         return AVERROR_INVALIDDATA;     }      if (s->state & PNG_IDAT) {         av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");         return AVERROR_INVALIDDATA;     }      if (s->color_type == PNG_COLOR_TYPE_PALETTE) {         if (length > 256 || !(s->state & PNG_PLTE))             return AVERROR_INVALIDDATA; static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,         }     } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {         if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||             (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||             s->bit_depth == 1)             return AVERROR_INVALIDDATA;          for (i = 0; i < length / 2; i++) { static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,         size_t raw_bpp = s->bpp - byte_depth;         unsigned x, y;          av_assert0(s->bit_depth > 1);          for (y = 0; y < s->height; ++y) {             uint8_t *row = &s->image_buf[s->image_linesize * y]; 

Patch:
patch: @@ -772,6 +772,16 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
 {
     int v, i;
 
+    if (!(s->state & PNG_IHDR)) {
+        av_log(avctx, AV_LOG_ERROR, "trns before IHDR\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (s->state & PNG_IDAT) {
+        av_log(avctx, AV_LOG_ERROR, "trns after IDAT\n");
+        return AVERROR_INVALIDDATA;
+    }
+
     if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
         if (length > 256 || !(s->state & PNG_PLTE))
             return AVERROR_INVALIDDATA;
@@ -782,7 +792,8 @@ static int decode_trns_chunk(AVCodecContext *avctx, PNGDecContext *s,
         }
     } else if (s->color_type == PNG_COLOR_TYPE_GRAY || s->color_type == PNG_COLOR_TYPE_RGB) {
         if ((s->color_type == PNG_COLOR_TYPE_GRAY && length != 2) ||
-            (s->color_type == PNG_COLOR_TYPE_RGB && length != 6))
+            (s->color_type == PNG_COLOR_TYPE_RGB && length != 6) ||
+            s->bit_depth == 1)
             return AVERROR_INVALIDDATA;
 
         for (i = 0; i < length / 2; i++) {
@@ -1242,6 +1253,8 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
         size_t raw_bpp = s->bpp - byte_depth;
         unsigned x, y;
 
+        av_assert0(s->bit_depth > 1);
+
         for (y = 0; y < s->height; ++y) {
             uint8_t *row = &s->image_buf[s->image_linesize * y];
path: libavcodec/pngdec.c