From patchwork Wed Dec 21 23:05:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 39742 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp42500pzb; Wed, 21 Dec 2022 15:05:34 -0800 (PST) X-Google-Smtp-Source: AMrXdXsiHIIovAPN4Gv581OFgmv4Vgu94uXApUYE9bToNiUZLF/fX7eZkcWlG5x5XkIV9i/A3Gdw X-Received: by 2002:a05:6402:6d8:b0:462:6e5e:329a with SMTP id n24-20020a05640206d800b004626e5e329amr3171106edy.8.1671663934349; Wed, 21 Dec 2022 15:05:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671663934; cv=none; d=google.com; s=arc-20160816; b=DaoD3SEgDmISQsJDYRYtUJGLdhUSnHHj2RR6d15KTkLsPY9XHZqZSqJ+gN/RNi3lAM IWGvN31F5cy4kGtW7MmGMg1etuV75k4XQBl737+dYEIo4O2dou+umMTTyes8/CBdzhzC QhyKNvqRTpZKcAsjxm/dG+8elTBZQZkxPvX1imAT/F2612MzG5wcUIPTGGKQpbKjbu2E IVEywkufnZwW6RyOPRQAN6hshjquyJCQnzpxZsUEUcZqZSOB3FEJbGAmtVqK+QnGYW8z sdVb6dAibTW54WQrQ2PQuJjb1Q1t3QkuNad0Za4inYSKSGJC7hLJTKq0ZC9mVS0JMgW4 mlPg== 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=4xDfKu38204q9h/h5b7ZWYDCP7m7E7CbreCX7RtZ9fk=; b=Rq3cz3HhCa6DbcYzR+2S9MBktOiivTS5jj1I9l0fXojXmIFPy+4ognHhN81HIKKQig PT1gpL/2TYkHtZaxR2Sm6UUHtoNRYkiwE2HM5XDx0lM2GM0HlsJU9x2X7A+/jLOUf+cK dveOBEu+bQhuGLbpyp6BnotgTF8SlcRVltWDRQPXBxeEFBGC9euuLXTTyGtwgqsh9bRY yuCYzBVlVpoIOrWN0pWnjYMFogLdQnkYUpbIgGAnnlAvwxggvNGailbbgY3JxZqljsdu N9k6LSyC2np/tSYYxEOrihdT2Mp8a1d64ydPR5vnfmclAKbK3Hw268TEp4jEEgaeDxND q1OA== 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 i15-20020a50fd0f000000b0046b13b72f40si13038297eds.609.2022.12.21.15.05.33; Wed, 21 Dec 2022 15:05:34 -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 8870A68B0DB; Thu, 22 Dec 2022 01:05:29 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 31F5568B0DB for ; Thu, 22 Dec 2022 01:05:21 +0200 (EET) Received: (Authenticated sender: michael@niedermayer.cc) by mail.gandi.net (Postfix) with ESMTPSA id CC6CCFF80C for ; Wed, 21 Dec 2022 23:05:20 +0000 (UTC) From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 22 Dec 2022 00:05:20 +0100 Message-Id: <20221221230520.11540-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avcodec/sunrast: Fix maplength check 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: BiX3x4LtRv1t Fixes: out of bounds read Found-by: Ibrahim Mohamed Reviewed-by; Ibrahim Mohamed Signed-off-by: Michael Niedermayer --- libavcodec/sunrast.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/libavcodec/sunrast.c b/libavcodec/sunrast.c index 45b29e4d72..3668d2be7f 100644 --- a/libavcodec/sunrast.c +++ b/libavcodec/sunrast.c @@ -19,6 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" #include "libavutil/common.h" #include "libavutil/intreadwrite.h" #include "avcodec.h" @@ -75,6 +76,12 @@ static int sunrast_decode_frame(AVCodecContext *avctx, AVFrame *p, return AVERROR_PATCHWELCOME; } + if (maplength > 768) { + av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n"); + return AVERROR_INVALIDDATA; + } + + // This also checks depth to be valid switch (depth) { case 1: avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE; @@ -96,15 +103,23 @@ static int sunrast_decode_frame(AVCodecContext *avctx, AVFrame *p, return AVERROR_INVALIDDATA; } + // This checks w and h to be valid in the sense that bytes of a padded bitmap are addressable with 32bit int ret = ff_set_dimensions(avctx, w, h); if (ret < 0) return ret; + // ensured by ff_set_dimensions() + av_assert0(w <= (INT32_MAX - 7) / depth); + /* scanlines are aligned on 16 bit boundaries */ len = (depth * w + 7) >> 3; alen = len + (len & 1); - if (buf_end - buf < maplength + (len * h) * 3 / 256) + // ensured by ff_set_dimensions() + av_assert0(h <= INT32_MAX / (3 * len)); + + // maplength is limited to 768 and the right term is limited to INT32_MAX / 256 so the add needs no check + if (buf_end - buf < (uint64_t)maplength + (len * h) * 3 / 256) return AVERROR_INVALIDDATA; if ((ret = ff_get_buffer(avctx, p, 0)) < 0) @@ -118,7 +133,7 @@ static int sunrast_decode_frame(AVCodecContext *avctx, AVFrame *p, } else if (maplength) { unsigned int len = maplength / 3; - if (maplength % 3 || maplength > 768) { + if (maplength % 3) { av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n"); return AVERROR_INVALIDDATA; }