From patchwork Fri Apr 19 22:03:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 12824 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id F1FEC44936D for ; Sat, 20 Apr 2019 01:14:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D040968062B; Sat, 20 Apr 2019 01:14:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0F4EF68A947 for ; Sat, 20 Apr 2019 01:14:05 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id q16so7489894wmj.3 for ; Fri, 19 Apr 2019 15:14:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6cKHQnWO+LP/aqbI69Zt1NG2z/8YFnOR5xnClt3ciFQ=; b=jHgElAqzth0juPsfOQYd2wMoEs3Uqp05gqOAruxxpeQu8voEkdgEqPGN760l+oz+z+ CsxbIaqbue72T1LTyc9e3gh4QIH4jqoQGpIKnCVT+XQkhYbfDxaO2fyESlsLij6/ewUd eQwinpDQotxr6vjClJcBwofbs/Ao10Lv+I/LjX20fY46+cYCAE6d0n2Dvf0nlCHBCjyj uTHkmpSlBsNtj2F16NlYlRDbyHqZ3Q7f6bnSs1+1gO/EsB/CA92Y/pNNdq3XwQm/FX+1 MbGb8JnYCLoJ9E50vLk7Bvzf6flYUL5qo0z9lATWQmcqqDLblbNmPZ0fGdoATJBnFu+q 9+Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6cKHQnWO+LP/aqbI69Zt1NG2z/8YFnOR5xnClt3ciFQ=; b=DOaSHR3v2BzXLK7fli0hJu4H8IIyiOXpa0EdQRgO4w7/dCwuftCsQOy3E8eyY6H550 cOg1ZcWK7ef2XshWe/5XRiVZz3IPmF1RmkTWhWF709Azdemzr8C4kkGGsIFxB/9Xuqow yzZvBPx2QOAt8TZMOfNsYNmOXkiVzYo/LIGkb+yNixcQidc5sOwlAxikg0Lz06iZSbvu fJLiXn/BB7DUGlkm4be3C4CNl5LjjRWXomZYYuGt0smTd2ssWYc96XRlhSXxY0It6Sau zA8KGaRPkI4n9Ku9xVh8jihZNjCbmWAYhzZsjX9tLnTYW5sz9fi0A+LQLwR+fMMd0Ooz WbbA== X-Gm-Message-State: APjAAAUTvl3Lw0T2nQukRQ8RzdOO4Wrq75hDvFIFB1Hs3MmeZoyiyzCt bnc1WDuWuchLDpi+ExF6qM7EVnPVD+g= X-Google-Smtp-Source: APXvYqyuH0U/2xml+MQ4Pred6nI2QMRRJ9wt3cemHUkSUbdRP4l83vfXuBJzqyQX6Dh0ogukaupf6A== X-Received: by 2002:a05:600c:20d:: with SMTP id 13mr3981007wmi.60.1555711593144; Fri, 19 Apr 2019 15:06:33 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.gmail.com with ESMTPSA id z11sm5650642wmf.12.2019.04.19.15.06.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Apr 2019 15:06:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Apr 2019 00:03:15 +0200 Message-Id: <20190419220316.47392-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190419220316.47392-1-andreas.rheinhardt@gmail.com> References: <20190413133052.2668-1-andreas.rheinhardt@googlemail.com> <20190419220316.47392-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] lavf/webm_chunk: Fix NULL dereference 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The earlier version of the webm_chunk muxer had several bugs: 1. If the first packet of an audio stream didn't have a PTS of zero, then no chunk will be started before a packet is delivered to the underlying Matroska/WebM muxer, i.e. the AVFormatContext used to write these packets had a NULL as AVIOContext for output. This is behind the crash in ticket #5752. 2. If an error happens during writing a packet, the underlyimg Matroska/WebM muxer context is freed. This leads to a use-after-free coupled with a double-free in webm_chunk_write_trailer (which supposes that the underlying AVFormatContext is still valid). 3. Even when no error occurs at all, webm_chunk_write_trailer is still buggy: After the underlying Matroska/WebM muxer has written its trailer, ending the chunk implicitly flushes it again which is illegal at this point. These bugs have been fixed. Fixes #5752. Signed-off-by: Andreas Rheinhardt --- libavformat/webm_chunk.c | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c index 2c99753b5b..391fee721a 100644 --- a/libavformat/webm_chunk.c +++ b/libavformat/webm_chunk.c @@ -168,7 +168,7 @@ static int chunk_start(AVFormatContext *s) return 0; } -static int chunk_end(AVFormatContext *s) +static int chunk_end(AVFormatContext *s, int flush) { WebMChunkContext *wc = s->priv_data; AVFormatContext *oc = wc->avf; @@ -179,11 +179,14 @@ static int chunk_end(AVFormatContext *s) char filename[MAX_FILENAME_SIZE]; AVDictionary *options = NULL; - if (wc->chunk_start_index == wc->chunk_index) + if (!oc->pb) return 0; - // Flush the cluster in WebM muxer. - oc->oformat->write_packet(oc, NULL); + + if (flush) + // Flush the cluster in WebM muxer. + oc->oformat->write_packet(oc, NULL); buffer_size = avio_close_dyn_buf(oc->pb, &buffer); + oc->pb = NULL; ret = get_chunk_filename(s, 0, filename); if (ret < 0) goto fail; @@ -194,7 +197,6 @@ static int chunk_end(AVFormatContext *s) goto fail; avio_write(pb, buffer, buffer_size); ff_format_io_close(s, &pb); - oc->pb = NULL; fail: av_dict_free(&options); av_free(buffer); @@ -216,27 +218,19 @@ static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt) } // For video, a new chunk is started only on key frames. For audio, a new - // chunk is started based on chunk_duration. - if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && + // chunk is started based on chunk_duration. Also, a new chunk is started + // unconditionally if there is no currently open chunk. + if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY)) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && - (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) { + wc->duration_written >= wc->chunk_duration)) { wc->duration_written = 0; - if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) { - goto fail; + if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) { + return ret; } } ret = oc->oformat->write_packet(oc, pkt); - if (ret < 0) - goto fail; - -fail: - if (ret < 0) { - oc->streams = NULL; - oc->nb_streams = 0; - avformat_free_context(oc); - } return ret; } @@ -245,12 +239,20 @@ static int webm_chunk_write_trailer(AVFormatContext *s) { WebMChunkContext *wc = s->priv_data; AVFormatContext *oc = wc->avf; + int ret; + + if (!oc->pb) { + ret = chunk_start(s); + if (ret < 0) + goto fail; + } oc->oformat->write_trailer(oc); - chunk_end(s); + ret = chunk_end(s, 0); +fail: oc->streams = NULL; oc->nb_streams = 0; avformat_free_context(oc); - return 0; + return ret; } #define OFFSET(x) offsetof(WebMChunkContext, x)