From patchwork Sun Nov 14 19:30:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 31420 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp4833843iob; Sun, 14 Nov 2021 11:31:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwqRqP0twAI72IDmYIRPYrSmNvO6xEeavRDaLqyGrcU1uTQLim1K2SZUqPOi6ZgJTBpTOGC X-Received: by 2002:a05:6402:354c:: with SMTP id f12mr46161551edd.108.1636918312115; Sun, 14 Nov 2021 11:31:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636918312; cv=none; d=google.com; s=arc-20160816; b=wZ+t6b4BnhREwTufxi/daUjAs2SLFxOfwR2FUlQ5T3qFlLr52fwUlYrRPryQPl/n7f n1cOC3OEhVRVo8kKJULKh/FhW7OSrQN2ibZ6sJFC5eOVa6AKN1BtHSxeqLC7rpaG+hsu eIuiUZZbctC5L8W44x1ysD5GRz0fLJp/ofS8Zw3SpirnrqhvlK6YYXCrzSOL6z+OJXtV +tth65Fw0re/cr+/E5yzjFW1Mt42ATJJsLK8zTn00OnQIfye0RetB3Yfs8aeesHLOjXq lhX9jpOUBdovEE1ROGr6pT/aIb0iS65DSBSvtoZXHGBoxr/wBE+8kocBLOIsiTYZ4iya qcPQ== 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=vjkXwgb63x8jF7Q453M2uK+PEnQdCyeXEZ3iu79TZ3c=; b=u1WUfnH5c+yqIJ7mZlyz7YaKOQ7a/mjtvL++o2JvD71/RHCsum53YXGb0mGzPCGzVi 8JchVRY4rQpJa5BHWrRV57wnktT0+o/5CammIr8mttk+uvOzugnxQ1kNcPZMSkWmWgTG +LSuTn/+3BgRr2Xp9ZtX0b7ivOYQ+bTzgekgNfuDplyq86aSyJnQ21RuvCZ5KZ2XXz7y v224UnHsKxRCfWzuyFjERKxTelWrvvuwgn5ICzKf/FtABzUrIn2nZveZHPba2QWjHb5C vlgvUrFSVnSuPHmfCs+ageg4GzAr5Wi4UtrhT2NivS3zMVcnpLH3+5fVMzaNZe5Tx/VF uuHg== 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 j18si3883109edw.37.2021.11.14.11.31.51; Sun, 14 Nov 2021 11:31:52 -0800 (PST) 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 59C0168A921; Sun, 14 Nov 2021 21:31:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe02-1.mx.upcmail.net (vie01a-dmta-pe02-1.mx.upcmail.net [62.179.121.157]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 56030689B43 for ; Sun, 14 Nov 2021 21:31:31 +0200 (EET) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe02.mx.upcmail.net with esmtp (Exim 4.92) (envelope-from ) id 1mmLDu-008dbc-0H for ffmpeg-devel@ffmpeg.org; Sun, 14 Nov 2021 20:31:30 +0100 Received: from localhost ([213.47.68.29]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id mLCvmVwcFSgGFmLCvmnk8v; Sun, 14 Nov 2021 20:30:30 +0100 X-Env-Mailfrom: michael@niedermayer.cc X-Env-Rcptto: ffmpeg-devel@ffmpeg.org X-SourceIP: 213.47.68.29 X-CNFS-Analysis: v=2.3 cv=f8Q2+96M c=1 sm=1 tr=0 a=2hcxjKEKjp0CzLx6oWAm4g==:117 a=2hcxjKEKjp0CzLx6oWAm4g==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=GEAsPZ9sns4A:10 a=ZZnuYtJkoWoA:10 a=1jJl8SqfTGeWKV-06KAA:9 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Sun, 14 Nov 2021 20:30:28 +0100 Message-Id: <20211114193029.17983-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 X-CMAE-Envelope: MS4wfBybeT+dFpmNSykCT9c6sEc0lOQICfBD/b5alSRaBoEbXAAJlWqoKZoIfzSvlHW9pjCNk6KtFgJD4V1Y6eZZU8ICNOZpTsAYZhjkrsbSaOvlwDguy/RZ nvnvLrqaYl0+xpHwNtMO/3ExsD1lv129qcmKiP+ttoXFmcrPYyv7OjLl Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/txd: Move input size checks before allocation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: 9yHO4XR3rPng Fixes: Timeout Fixes: 40882/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_TXD_fuzzer-4893364584054784 Signed-off-by: Michael Niedermayer --- libavcodec/txd.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/libavcodec/txd.c b/libavcodec/txd.c index 86d60f33777..93e4fa96050 100644 --- a/libavcodec/txd.c +++ b/libavcodec/txd.c @@ -65,8 +65,26 @@ static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, if (depth == 8) { avctx->pix_fmt = AV_PIX_FMT_PAL8; - } else if (depth == 16 || depth == 32) { + if (bytestream2_get_bytes_left(&gb) < w * h + 4 * 256) + return AVERROR_INVALIDDATA; + } else if (depth == 16) { avctx->pix_fmt = AV_PIX_FMT_RGBA; + switch (d3d_format) { + case 0: + if (!(flags & 1)) + goto unsupported; + case TXD_DXT1: + if (bytestream2_get_bytes_left(&gb) < AV_CEIL_RSHIFT(w, 2) * AV_CEIL_RSHIFT(h, 2) * 8 + 4) + return AVERROR_INVALIDDATA; + break; + case TXD_DXT3: + if (bytestream2_get_bytes_left(&gb) < AV_CEIL_RSHIFT(w, 2) * AV_CEIL_RSHIFT(h, 2) * 16 + 4) + return AVERROR_INVALIDDATA; + } + } else if (depth == 32) { + avctx->pix_fmt = AV_PIX_FMT_RGBA; + if (bytestream2_get_bytes_left(&gb) < h * w * 4) + return AVERROR_INVALIDDATA; } else { avpriv_report_missing_feature(avctx, "Color depth of %u", depth); return AVERROR_PATCHWELCOME; @@ -92,8 +110,6 @@ static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, v = bytestream2_get_be32(&gb); pal[y] = (v >> 8) + (v << 24); } - if (bytestream2_get_bytes_left(&gb) < w * h) - return AVERROR_INVALIDDATA; bytestream2_skip(&gb, 4); for (y=0; yheight; j += 4) { for (i = 0; i < avctx->width; i += 4) { uint8_t *p = ptr + i * 4 + j * stride; @@ -117,8 +129,6 @@ static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, } break; case TXD_DXT3: - if (bytestream2_get_bytes_left(&gb) < AV_CEIL_RSHIFT(w, 2) * AV_CEIL_RSHIFT(h, 2) * 16) - return AVERROR_INVALIDDATA; for (j = 0; j < avctx->height; j += 4) { for (i = 0; i < avctx->width; i += 4) { uint8_t *p = ptr + i * 4 + j * stride; @@ -134,8 +144,6 @@ static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, switch (d3d_format) { case 0x15: case 0x16: - if (bytestream2_get_bytes_left(&gb) < h * w * 4) - return AVERROR_INVALIDDATA; for (y=0; y