From patchwork Thu May 4 15:15:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 3570 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp579712vsd; Thu, 4 May 2017 08:15:41 -0700 (PDT) X-Received: by 10.28.163.129 with SMTP id m123mr2113991wme.88.1493910941178; Thu, 04 May 2017 08:15:41 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t22si2704929wra.75.2017.05.04.08.15.40; Thu, 04 May 2017 08:15:41 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E2CA8689827; Thu, 4 May 2017 18:15:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe01-2.mx.upcmail.net (vie01a-qmta-pe01-2.mx.upcmail.net [62.179.121.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 14B6568988C for ; Thu, 4 May 2017 18:15:26 +0300 (EEST) Received: from [172.31.218.34] (helo=vie01a-dmta-pe02-1.mx.upcmail.net) by vie01a-pqmta-pe01.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1d6ITa-00084X-5F for ffmpeg-devel@ffmpeg.org; Thu, 04 May 2017 17:15:30 +0200 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe02.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1d6ITU-0007nn-8H for ffmpeg-devel@ffmpeg.org; Thu, 04 May 2017 17:15:24 +0200 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id GFFJ1v02o0S5wYM01FFLb1; Thu, 04 May 2017 17:15:20 +0200 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 4 May 2017 17:15:18 +0200 Message-Id: <20170504151518.9230-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] avcodec: Avoid splitting side data repeatedly X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixes Timeout Fixes: 508/clusterfuzz-testcase-6245747678773248 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/targets/ffmpeg Signed-off-by: Michael Niedermayer --- libavcodec/avpacket.c | 24 ++++++++++++++++++++++++ libavcodec/decode.c | 9 +++++++-- libavcodec/internal.h | 4 ++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c index d97400eaef..de12a1f1ce 100644 --- a/libavcodec/avpacket.c +++ b/libavcodec/avpacket.c @@ -463,6 +463,30 @@ int av_packet_split_side_data(AVPacket *pkt){ return 0; } +int ff_packet_split_and_drop_side_data(AVPacket *pkt){ + if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){ + int i; + unsigned int size; + uint8_t *p; + + p = pkt->data + pkt->size - 8 - 5; + for (i=1; ; i++){ + size = AV_RB32(p); + if (size>INT_MAX - 5 || p - pkt->data < size) + return 0; + if (p[4]&128) + break; + if (p - pkt->data < size + 5) + return 0; + p-= size+5; + } + pkt->size = p - pkt->data - size; + av_assert0(pkt->size >= 0); + return 1; + } + return 0; +} + #endif uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 708071fd07..43ba04550a 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -392,7 +392,9 @@ static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame) tmp = *pkt; #if FF_API_MERGE_SD FF_DISABLE_DEPRECATION_WARNINGS - did_split = av_packet_split_side_data(&tmp); + did_split = avci->compat_decode_partial_size ? + ff_packet_split_and_drop_side_data(&tmp) : + av_packet_split_side_data(&tmp); if (did_split) { ret = extract_packet_props(avctx->internal, &tmp); @@ -961,6 +963,7 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, AVPacket *avpkt) { int i, ret = 0; + AVCodecInternal *avci = avctx->internal; if (!avpkt->data && avpkt->size) { av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n"); @@ -981,7 +984,9 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, AVPacket tmp = *avpkt; #if FF_API_MERGE_SD FF_DISABLE_DEPRECATION_WARNINGS - int did_split = av_packet_split_side_data(&tmp); + int did_split = avci->compat_decode_partial_size ? + ff_packet_split_and_drop_side_data(&tmp) : + av_packet_split_side_data(&tmp); //apply_param_change(avctx, &tmp); if (did_split) { diff --git a/libavcodec/internal.h b/libavcodec/internal.h index e30d4aa73d..f3d10a14f1 100644 --- a/libavcodec/internal.h +++ b/libavcodec/internal.h @@ -356,6 +356,10 @@ int ff_set_sar(AVCodecContext *avctx, AVRational sar); int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding); +#if FF_API_MERGE_SD_API +int ff_packet_split_and_drop_side_data(AVPacket *pkt); +#endif + /** * Select the (possibly hardware accelerated) pixel format. * This is a wrapper around AVCodecContext.get_format() and should be used