From e6459c655e110cd9acb5d0658d89c415eaca847b Mon Sep 17 00:00:00 2001 From: Andreas Cadhalpun Date: Wed, 11 Nov 2015 01:14:57 +0100 Subject: [PATCH 1/3] dds: validate source buffer size before copying If it is too small av_image_copy_plane segfaults. Signed-off-by: Andreas Cadhalpun --- libavcodec/dds.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libavcodec/dds.c b/libavcodec/dds.c index 04b1dd2de8..0f045d6a07 100644 --- a/libavcodec/dds.c +++ b/libavcodec/dds.c @@ -668,6 +668,12 @@ static int dds_decode(AVCodecContext *avctx, void *data, frame->palette_has_changed = 1; } + if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) { + av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n", + bytestream2_get_bytes_left(gbc), frame->height * linesize); + return AVERROR_INVALIDDATA; + } + av_image_copy_plane(frame->data[0], frame->linesize[0], gbc->buffer, linesize, linesize, frame->height); From 29b17528612c8b0f66e6982ab48cc0816e41f36f Mon Sep 17 00:00:00 2001 From: Andreas Cadhalpun Date: Wed, 11 Nov 2015 01:15:55 +0100 Subject: [PATCH 2/3] dds: validate compressed source buffer size A too small buffer will cause segfaults somewhere below decompress_texture_thread. Signed-off-by: Andreas Cadhalpun --- libavcodec/dds.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libavcodec/dds.c b/libavcodec/dds.c index 0f045d6a07..219612480f 100644 --- a/libavcodec/dds.c +++ b/libavcodec/dds.c @@ -642,9 +642,18 @@ static int dds_decode(AVCodecContext *avctx, void *data, return ret; if (ctx->compressed) { + int size = (avctx->coded_height / TEXTURE_BLOCK_H) * + (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio; ctx->slice_count = av_clip(avctx->thread_count, 1, avctx->coded_height / TEXTURE_BLOCK_H); + if (bytestream2_get_bytes_left(gbc) < size) { + av_log(avctx, AV_LOG_ERROR, + "Compressed Buffer is too small (%d < %d).\n", + bytestream2_get_bytes_left(gbc), size); + return AVERROR_INVALIDDATA; + } + /* Use the decompress function on the texture, one block per thread. */ ctx->tex_data = gbc->buffer; avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count); From 7b2211bfc4b0c4568180a8db2478023c42d9ff51 Mon Sep 17 00:00:00 2001 From: Andreas Cadhalpun Date: Wed, 11 Nov 2015 01:16:09 +0100 Subject: [PATCH 3/3] dds: add missing newline to log messages Signed-off-by: Andreas Cadhalpun --- libavcodec/dds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libavcodec/dds.c b/libavcodec/dds.c index 219612480f..ea58da0877 100644 --- a/libavcodec/dds.c +++ b/libavcodec/dds.c @@ -599,14 +599,14 @@ static int dds_decode(AVCodecContext *avctx, void *data, bytestream2_init(gbc, avpkt->data, avpkt->size); if (bytestream2_get_bytes_left(gbc) < 128) { - av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).", + av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", bytestream2_get_bytes_left(gbc)); return AVERROR_INVALIDDATA; } if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') || bytestream2_get_le32(gbc) != 124) { // header size - av_log(avctx, AV_LOG_ERROR, "Invalid DDS header."); + av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n"); return AVERROR_INVALIDDATA; }