From patchwork Thu Apr 23 03:07:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19183 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 3371844BA43 for ; Thu, 23 Apr 2020 06:08:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1571668BE10; Thu, 23 Apr 2020 06:08:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D743F68BDFD for ; Thu, 23 Apr 2020 06:08:10 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id j2so5016449wrs.9 for ; Wed, 22 Apr 2020 20:08:10 -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=qHqJGwxJeH7PC2Bysww2cFij6rAeumpAnE3PmhJKw38=; b=plxXGghEYspcUetLhY3ZwmmgFdeJnxgAi8uyAs4epF7BeYBNAEVjuMFldZLqS396Jk PmF8OMDzRb7jscUAPXnTxj9JW51p4A1zzAKkVkMXHsYf2ND2MaEu0/Z6xMPGHgpQaY4x O4fJkEp6E9Q3wcSQMYu+juGVK+PC18/0LCILGiOfbFmgA/kqNCMPLuwYD/CXT9LWvGAV e6MFrWdP6lgiB5oLz/inEjsbqg4kOJQgb8dDwZj36dWqOca7VFCehpQdpvFjgiwbjsmG UzSOFLnAsN6dYPZkTLbr9If+VBZrNJU5/dh/fWb4xZJS9lLH1eOY5t1Ty9rjkHnj3Lj0 tavQ== 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=qHqJGwxJeH7PC2Bysww2cFij6rAeumpAnE3PmhJKw38=; b=e3ZcmZ4MYP4CJ4FjanKx6jhKIo2BhKTlcQCWzpDyykItnAAKZVNa7DAoSZgG1BGcK9 dg+JHldkfUr2ybUj+v8JGgrjKi0j+TzK0vHekXQh+Ox+4S65IxQwXycxeCYwiSyUGm1Q qZW+fGrZxOjfhc0ruIO8OhSVnG0oEF45eMR5lbDXXLw2+cKQafOlv7hW2BVt9tbr+V0Z fSglXyuks3xD/4qPvuAItv6CCmxNCW3r1dmu+Ovk+MziLh+OtYezMB4SW1ojiSo4eMFo ramclT82H2KrM89G27uRJNCgH/XdhL696tbKS12Sp0uCTP1/NUA1yD0p3TILpkIYWtIv b/XA== X-Gm-Message-State: AGi0PuZgLrk2vkyBdK6AfC8OKLFZ4R02F5v035aspOY8GfpeZKqhMz68 zhC6YRuVEujSm8is8Kpt1l7quI56 X-Google-Smtp-Source: APiQypLl+x4Hr+GaYJr9+69XjBlfFv272LMu6dyK/qvMwGoM9InTfXxCh+cip3ZyVkfAJ2dj7q2j8g== X-Received: by 2002:a05:6000:12c7:: with SMTP id l7mr2343531wrx.239.1587611289993; Wed, 22 Apr 2020 20:08:09 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id m1sm1497733wro.64.2020.04.22.20.08.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2020 20:08:09 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Apr 2020 05:07:34 +0200 Message-Id: <20200423030741.12158-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200423030741.12158-1-andreas.rheinhardt@gmail.com> References: <20200423030741.12158-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/11] avformat/matroskadec: Fix buffer overflow when demuxing RealAudio 28.8 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" RealAudio 28.8 (like other RealAudio codecs) uses a special demuxing mode in which the data of the existing Matroska Blocks is not simply forwarded as-is. Instead data from several Blocks is recombined together to output several packets. The parameters governing this process are parsed from the CodecPrivate: Coded framesize (cfs), frame size (w) and sub_packet_h (h). During demuxing, h/2 pieces of data of size cfs each are read from every Matroska (Simple)Block and put at offset m * 2 * w + n * cfs of a buffer of size h * w, where m ranges from 0 to h/2 - 1 for each Block while n is initially zero and incremented after a Block has been parsed until it is h, at which poin the assembled packets are output and n reset. The highest offset is given by (h/2 - 1) * 2 * w + (h - 1) * cfs + cfs while the destination buffer's size is given by h * w. For even h, this leads to a buffer overflow (and potential segfault) if h * cfs > 2 * w; for odd h, the condition is h * cfs > 3 * w. This commit adds a check to rule this out. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskadec.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c index 844f96cd52..951695b5b5 100644 --- a/libavformat/matroskadec.c +++ b/libavformat/matroskadec.c @@ -2612,6 +2612,9 @@ static int matroska_parse_tracks(AVFormatContext *s) return AVERROR_INVALIDDATA; if (codec_id == AV_CODEC_ID_RA_288) { + if ((int64_t)track->audio.sub_packet_h * track->audio.coded_framesize + > (2 + (track->audio.sub_packet_h & 1)) * track->audio.frame_size) + return AVERROR_INVALIDDATA; st->codecpar->block_align = track->audio.coded_framesize; track->codec_priv.size = 0; } else {