From patchwork Thu Sep 13 23:51:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 10321 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp86393jap; Thu, 13 Sep 2018 16:52:58 -0700 (PDT) X-Google-Smtp-Source: ANB0VdY4e/UjfeRdFrCczYIC7AgH8jQABs23CmP+IAnrdh9poz5Zr98JDdIolDlHD4PAOa28KvUN X-Received: by 2002:a1c:8893:: with SMTP id k141-v6mr262319wmd.36.1536882778795; Thu, 13 Sep 2018 16:52:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536882778; cv=none; d=google.com; s=arc-20160816; b=Pskpi9eWkPwVdnn/vf9H9anGavK5hu7sCTxM58qknaZ4c59YXZuRd4loJNXxCcwW6o Vh0iaoXts73Nf+if1R4ibqLaJ3FUl9CkoaowXH4NfvJQCAlbtyhvr4Vs2dP4Uc5V4kbf YKbryfAKjzTzHl9fI7jzlZfEOqq7OIbcJGvije1tp8F8Lcf5RR2IJ/YkMFrVbzpSD6Bm S/RQE4zwyjrUym83x+MJ9WpyqMaOkoQqRX5X7GImaRY+EvJVqD2ERHgzwb3ErDH8irfA ZtvgdPBiNGprz6nMZFTS0UaBGAuODZvG9BcjbfcA3qzMR4Ie27xpboVXBtGq3vN8Iubh vBag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:delivered-to; bh=3AZuwnX+rdcnMKzshm8l5CpxGfZTto9IdgEAeS37Kjo=; b=yyydZ95HN/vdn97U20rXP/8r1gH30XLPn+yFTEzB3a0u/jezhQLh7KFIDlmb4VXcsu MVyC5zPYug2APY4X4qfQ1Cd7qmkvxAMa8SQhKbwRJRmUD+pi9tmWkMSui3kmmakWXzo5 +pqZ8Z6pn+rWI0KK6pV59g1yZeayLdvkPnbkDm72fViSOQflU0sIXGpTYNjzHp04UsYk NvHGlxRuj4iLcr6lfWNt1u2IHiKqgbnWcneda8Ry7obiHdrVwU/VXXPZRMKpgd6kcPbh tgGRKESSXxS8BEv1eHqCabaN463muOInTZMLct9CbaGR7aBFj44s0rcUdqxdSVFbuvwn LzcQ== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 68-v6si5133140wra.134.2018.09.13.16.52.58; Thu, 13 Sep 2018 16:52:58 -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 A95BC68A5A5; Fri, 14 Sep 2018 02:52:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-qmta-pe02-1.mx.upcmail.net (vie01a-qmta-pe02-1.mx.upcmail.net [62.179.121.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D27A368A425 for ; Fri, 14 Sep 2018 02:52:32 +0300 (EEST) Received: from [172.31.218.40] (helo=vie01a-dmta-pe04-1.mx.upcmail.net) by vie01a-pqmta-pe02.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1g0bPf-00019G-Lj for ffmpeg-devel@ffmpeg.org; Fri, 14 Sep 2018 01:52:43 +0200 Received: from [172.31.216.43] (helo=vie01a-pemc-psmtp-pe01) by vie01a-dmta-pe04.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1g0bPa-0001Gc-1Y for ffmpeg-devel@ffmpeg.org; Fri, 14 Sep 2018 01:52:38 +0200 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe01 with SMTP @ mailcloud.upcmail.net id bBsY1y00U0S5wYM01BsZJF; Fri, 14 Sep 2018 01:52:34 +0200 X-SourceIP: 213.47.41.20 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Fri, 14 Sep 2018 01:51:55 +0200 Message-Id: <20180913235156.9551-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.18.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/diracdec: Propagate errors from codeblock() 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" Consider a component to be damaged if more than 50% of its subbands are damaged Fixes: Timeout (part 1 of 2) Fixes: 9774/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_DIRAC_fuzzer-5748957085958144 Signed-off-by: Michael Niedermayer --- libavcodec/diracdec.c | 47 +++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c index 9a417caec5..cbd7fd1532 100644 --- a/libavcodec/diracdec.c +++ b/libavcodec/diracdec.c @@ -488,7 +488,7 @@ UNPACK_ARITH(10, int32_t) * Decode the coeffs in the rectangle defined by left, right, top, bottom * [DIRAC_STD] 13.4.3.2 Codeblock unpacking loop. codeblock() */ -static inline void codeblock(DiracContext *s, SubBand *b, +static inline int codeblock(DiracContext *s, SubBand *b, GetBitContext *gb, DiracArith *c, int left, int right, int top, int bottom, int blockcnt_one, int is_arith) @@ -505,7 +505,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, zero_block = get_bits1(gb); if (zero_block) - return; + return 0; } if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) { @@ -516,7 +516,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, quant = dirac_get_se_golomb(gb); if (quant > INT_MAX - b->quant || b->quant + quant < 0) { av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n"); - return; + return AVERROR_INVALIDDATA; } b->quant += quant; } @@ -524,7 +524,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, if (b->quant > (DIRAC_MAX_QUANT_INDEX - 1)) { av_log(s->avctx, AV_LOG_ERROR, "Unsupported quant %d\n", b->quant); b->quant = 0; - return; + return AVERROR_INVALIDDATA; } qfactor = ff_dirac_qscale_tab[b->quant]; @@ -559,6 +559,7 @@ static inline void codeblock(DiracContext *s, SubBand *b, buf += b->stride; } } + return 0; } /** @@ -593,7 +594,7 @@ INTRA_DC_PRED(10, uint32_t) * Dirac Specification -> * 13.4.2 Non-skipped subbands. subband_coeffs() */ -static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith) +static av_always_inline int decode_subband_internal(DiracContext *s, SubBand *b, int is_arith) { int cb_x, cb_y, left, right, top, bottom; DiracArith c; @@ -601,9 +602,10 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width; int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height; int blockcnt_one = (cb_width + cb_height) == 2; + int ret; if (!b->length) - return; + return 0; init_get_bits8(&gb, b->coeff_data, b->length); @@ -616,7 +618,9 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b left = 0; for (cb_x = 0; cb_x < cb_width; cb_x++) { right = (b->width * (cb_x+1LL)) / cb_width; - codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith); + ret = codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith); + if (ret < 0) + return ret; left = right; } top = bottom; @@ -629,33 +633,35 @@ static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b intra_dc_prediction_8(b); } } + return 0; } static int decode_subband_arith(AVCodecContext *avctx, void *b) { DiracContext *s = avctx->priv_data; - decode_subband_internal(s, b, 1); - return 0; + return decode_subband_internal(s, b, 1); } static int decode_subband_golomb(AVCodecContext *avctx, void *arg) { DiracContext *s = avctx->priv_data; SubBand **b = arg; - decode_subband_internal(s, *b, 0); - return 0; + return decode_subband_internal(s, *b, 0); } /** * Dirac Specification -> * [DIRAC_STD] 13.4.1 core_transform_data() */ -static void decode_component(DiracContext *s, int comp) +static int decode_component(DiracContext *s, int comp) { AVCodecContext *avctx = s->avctx; SubBand *bands[3*MAX_DWT_LEVELS+1]; enum dirac_subband orientation; int level, num_bands = 0; + int ret[3*MAX_DWT_LEVELS+1]; + int i; + int damaged_count = 0; /* Unpack all subbands at all levels. */ for (level = 0; level < s->wavelet_depth; level++) { @@ -677,11 +683,20 @@ static void decode_component(DiracContext *s, int comp) /* arithmetic coding has inter-level dependencies, so we can only execute one level at a time */ if (s->is_arith) avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level], - NULL, 4-!!level, sizeof(SubBand)); + ret + 3*level + !!level, 4-!!level, sizeof(SubBand)); } /* golomb coding has no inter-level dependencies, so we can execute all subbands in parallel */ if (!s->is_arith) - avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*)); + avctx->execute(avctx, decode_subband_golomb, bands, ret, num_bands, sizeof(SubBand*)); + + for (i = 0; i < s->wavelet_depth * 3 + 1; i++) { + if (ret[i] < 0) + damaged_count++; + } + if (damaged_count > (s->wavelet_depth * 3 + 1) /2) + return AVERROR_INVALIDDATA; + + return 0; } #define PARSE_VALUES(type, x, gb, ebits, buf1, buf2) \ @@ -1867,7 +1882,9 @@ static int dirac_decode_frame_internal(DiracContext *s) if (!s->zero_res && !s->low_delay) { memset(p->idwt.buf, 0, p->idwt.stride * p->idwt.height); - decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */ + ret = decode_component(s, comp); /* [DIRAC_STD] 13.4.1 core_transform_data() */ + if (ret < 0) + return ret; } ret = ff_spatial_idwt_init(&d, &p->idwt, s->wavelet_idx+2, s->wavelet_depth, s->bit_depth);