From patchwork Thu Jun 24 20:57:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 28612 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2042:0:0:0:0 with SMTP id z2csp823313iod; Thu, 24 Jun 2021 13:58:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSOB69+tLra87djY0y+ad10nUY6vlBU3qG4/VaJe5mTJEPJp5of2xqtdnEsq3CYyF1ykN8 X-Received: by 2002:a05:6402:2687:: with SMTP id w7mr9869641edd.98.1624568327395; Thu, 24 Jun 2021 13:58:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624568327; cv=none; d=google.com; s=arc-20160816; b=lmQYICTjZRY9SrG5b5ajCSiQ4QjvsSi2MITZKPZjcayfPiLWLEX7Wjk2eBReLfabJO Amggsx7dMdBZeeB9R9ndR4gJktGAvZYUzsjcxACpYcrAKXNVBUhuoAPgX5TqQrdywgV2 wbKm09nhe6G1sQvRlV5+lj7O4m95bnDNGjC7KZ9/ngTMlu4I70A/w2pU1TGKMoOM8KjN jxo7xG8lwHBJeiwRK/ZeI6msrkIqYzjXjMAygdV4QAhcOzoIrB6snIqIYQq5UDzI30ki Bt3Isjwp7SQQFMy98F65WRAY7aLcMIewsOaYrawNvGxr68N7qDrB9Nrg8qdzttME5C2G UPug== 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=bZ8jYGAbO/E8YUQv5says2+3AqbNQJB2Oh/O64A4cxg=; b=W+2KSfAFz8xVYYYdm3v8q7By7ruSwXbmMRmGQ8/mwb9hSYNRdtT0wJWM6ZIdNsNnzd dMDaThHwV0dctTvWZgz4SF1IfkMXoVSddRN24yMuSBKxr3i5CT4l38lZJ+h8Ov+qROWl O8mZDZGSmvDNwGZUS2viayFE283KRl5nPHUk+pfMWl3gHmDuk3LyqmMgfTLpSiDwVsa6 8jk33SWOhzzLnVNxnl5C3oz/uKwopVSfd8KRvpC9/qEDRSTTJEz9vlDRLTVe5YztQmmH r2KPdpx32L1pjEPJy6OaO4r4KJyyjOw7KEVM7RWfFz7E4XYIwYksyH+mS/dQEdUc9TZh hLiw== 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 dq21si3624521ejc.595.2021.06.24.13.58.47; Thu, 24 Jun 2021 13:58:47 -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 12A47689969; Thu, 24 Jun 2021 23:58:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe03-3.mx.upcmail.net (vie01a-dmta-pe03-3.mx.upcmail.net [62.179.121.162]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EBE886809A7 for ; Thu, 24 Jun 2021 23:58:25 +0300 (EEST) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe03.mx.upcmail.net with esmtp (Exim 4.92) (envelope-from ) id 1lwWQb-004sG5-0M for ffmpeg-devel@ffmpeg.org; Thu, 24 Jun 2021 22:58:25 +0200 Received: from localhost ([213.47.68.29]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id wWPcljwGaljeHwWPdlKH5K; Thu, 24 Jun 2021 22:57:25 +0200 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=BoHjPrf5 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=nZOtpAppAAAA:20 a=gbekUa7by_NmWyUsaaoA:9 a=1fhp2MxaeJtTNGEnv6mo:22 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 24 Jun 2021 22:57:22 +0200 Message-Id: <20210624205724.18711-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 X-CMAE-Envelope: MS4wfHBDzQJnTWjw2CF5s+ovATNUXP79eOS/HJLbVxX4yZ/czCCqE5AlSytXfQCZzH2F1Ss5Q+HZ7HY4lIE2gfuQMND72cpJcHDpvtuAMKf7MNQ1RmV024cm 2sHggDNM9YAFOv7V240TT+JdFNfX3Dwx+FBtHiEOfCSyFq1vhTsM7qOo Subject: [FFmpeg-devel] [PATCH 1/3] avformat/matroskadec: Reset state also on failure in matroska_reset_status() 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: PGJxbqycwpi9 The calling code does not handle failures and will fail with assertion failures later. Seeking can always fail even when the position was previously read. Fixes: Assertion failure Fixes: 35253/clusterfuzz-testcase-minimized-ffmpeg_dem_MATROSKA_fuzzer-4693059982983168 Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg Signed-off-by: Michael Niedermayer --- libavformat/matroskadec.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 356a02339c..a0e6e0cf8b 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -804,20 +804,22 @@ static int matroska_read_close(AVFormatContext *s); static int matroska_reset_status(MatroskaDemuxContext *matroska, uint32_t id, int64_t position) { + int64_t err = 0; if (position >= 0) { - int64_t err = avio_seek(matroska->ctx->pb, position, SEEK_SET); - if (err < 0) - return err; - } + err = avio_seek(matroska->ctx->pb, position, SEEK_SET); + if (err > 0) + err = 0; + } else + position = avio_tell(matroska->ctx->pb); matroska->current_id = id; matroska->num_levels = 1; matroska->unknown_count = 0; - matroska->resync_pos = avio_tell(matroska->ctx->pb); + matroska->resync_pos = position; if (id) matroska->resync_pos -= (av_log2(id) + 7) / 8; - return 0; + return err; } static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos) @@ -1873,6 +1875,7 @@ static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, uint32_t saved_id = matroska->current_id; int64_t before_pos = avio_tell(matroska->ctx->pb); int ret = 0; + int ret2; /* seek */ if (avio_seek(matroska->ctx->pb, pos, SEEK_SET) == pos) { @@ -1897,7 +1900,9 @@ static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, } /* Seek back - notice that in all instances where this is used * it is safe to set the level to 1. */ - matroska_reset_status(matroska, saved_id, before_pos); + ret2 = matroska_reset_status(matroska, saved_id, before_pos); + if (ret >= 0) + ret = ret2; return ret; }