From patchwork Sat Apr 13 13:30:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 12722 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 7DEE6447EDF for ; Sat, 13 Apr 2019 16:41:18 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 64A4868AC2C; Sat, 13 Apr 2019 16:41:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3C02168A925 for ; Sat, 13 Apr 2019 16:41:12 +0300 (EEST) Received: by mail-wr1-f66.google.com with SMTP id k11so15550714wro.5 for ; Sat, 13 Apr 2019 06:41:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LX6+42UrLeuax9ZCBb1orKDDFr8fb0Gbi7zEcxANNas=; b=E7wmGw+k94aW9LgJCff7ZXOCI34pKjSHj5y8uXeN0OudR2PE6v7HA2YupBS4LCYAae Ok6VDUsHuSkmgg3P+D0AVUt0PteoUTzQWrXG7GPgJSCEsiR/vAkSRdWvaV37GxhPzvHj RHScS8q0MfPrM8S1j2Vfs+zUL4D2eElcS+eUgOYIn4RXA2UsRax5oEwyanSCTJ0AjWI+ AAOyTmFyFtPJhp8I5mUsz+vCYo/u580C6bTXqX+J05Z3UXKCLiMEn7KfdcmTsiyQnJNL fvtUkndqZhA96aJ7os2NKZBX8vZhhUn3WbQDIULBwxJD9PqbvTUOkj2FMJKreXh7LYFK ZIlQ== 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=LX6+42UrLeuax9ZCBb1orKDDFr8fb0Gbi7zEcxANNas=; b=twEF0XruzvvfvmO0gZid/ruXr9ecUMCUAIaAZhlbK9KbYHrPAJb09ZFJ3WZuIQHmeA c3OnqeLiYcZgWM5DmryUaC9nEcCJNdI2X1VLeJs0lZsSR8fjsI3q3krBvWKPuBYD6ine r2CkwisFSdiaaynhO6bncL3yfJ5NrUmNMAI+YwokgXyBf3pZkeRyMSzY3V0676HtihuE +gcAuQpyng30VBFMFO+UcxLXDL6y4os+gJrHOzLb4vg+rAEqPpyamwF5XKD3bfDRiZ0c adBhtXszJbfZGeQNRf/bQSFo0/MAmBo/JW+ltIftgYjQeuNGGi6RDgWOJtWPndsiTi5b RmHQ== X-Gm-Message-State: APjAAAUl7ZmEQ+K38JPLNfF3tn4MreDMuLfdZqh2UcoydqRLWDdFxMg5 kY0Dd/ve3Gr4Vrxtd6anH8yE6fne X-Google-Smtp-Source: APXvYqyMt2Z3e/sFJC6IqXZWOdLIOyyT0vB6BlLQa3lQ0VWndCjq1pKAc6V3YcJCloET2BqLsrcicw== X-Received: by 2002:a5d:6947:: with SMTP id r7mr6204191wrw.167.1555162871571; Sat, 13 Apr 2019 06:41:11 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id t74sm19108603wmt.3.2019.04.13.06.41.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 13 Apr 2019 06:41:10 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 13 Apr 2019 15:30:51 +0200 Message-Id: <20190413133052.2668-2-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190413133052.2668-1-andreas.rheinhardt@googlemail.com> References: <20190413133052.2668-1-andreas.rheinhardt@googlemail.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 --- There is one problem that could not be completely solved: If there is no output AVIOContext when writing the trailer and opening one fails, then I can't write the Matroska/WebM trailer (as this might lead to NULL pointer dereference (something might need to be written and the AVIOContext for writing is not existing). And in this case a bit of memory (like the tracks that is part of MatroskaMuxContest) will leak. But this only happens in the very unlikely scenario that there isn't enough memory to open a dynamic buffer. 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)