From patchwork Sat Nov 16 05:24:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16294 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 4538D44607E for ; Sat, 16 Nov 2019 07:25:01 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2E7F068A45F; Sat, 16 Nov 2019 07:25:01 +0200 (EET) 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 A4C2D68A3B2 for ; Sat, 16 Nov 2019 07:24:54 +0200 (EET) Received: by mail-wm1-f66.google.com with SMTP id z26so11625196wmi.4 for ; Fri, 15 Nov 2019 21:24:54 -0800 (PST) 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=i3XeZ4g2d2ER4cn0nCiO+xLjMRuS6Ib3AHYf5W09Qzs=; b=qInEpbn5qzqNYwuQJQ1fwA+wKeKv7goWKGxBOHnu+alNxTLkbmvk58AcSIRcoOww32 usuu3xIqpxAAFI3qHiuweu1ZwyU5mXiAueexo23RiFgvbom9sccrMM5i1E0eIZMG+zJ+ ZryNvg5DJp2Xljq9/wLCN/YRNmjqYLhODu4PYhvTHO/F3RUfyW1dW47vjGk44Bc4VrBV 9j+UMpm/gypP0mdxOXpbsH41M8reOwlf3cEVX3iY5nybixV9Pj1jAAqhoTG1Do3dW2XC oAKeEAoSyuF5229QW0/VZE4ByhyuJIeNjoBVvEAfl42KIckR6A64GpIXuyYvEJFy91Cy eB/A== 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=i3XeZ4g2d2ER4cn0nCiO+xLjMRuS6Ib3AHYf5W09Qzs=; b=ml79a99QUhfiDOL678ky3e2mMOQLNNRRKkjoDZHwgjUM0bCaxu3ZrASC5r+BZNtWLS VViplP2UAVdZx3jaZsCi2GgUwFVgKJw2pgt9YAkkIb0WzognZeWjF4oQCdOKDlhz2/uN laFsZKmC29Dwuvr6XgKh00w1jNZ03GtxtzE99orQky+6roSmp4ZKgh+gJwJv9OP3f0Ht I8/wEvhYycpNtV17f/+Zsa8vjltfe9bB/PCmCn27AYJ7vs5a9vW8umzJvKVmVVyHAaid GXuiRDXVvGy2/jzQqnw0xwS/XxBAsaMc5qgUaIOlYXHqxOXGAnA6wwTXj7CugOArAD5Y fW4w== X-Gm-Message-State: APjAAAXScST3cHrqL9mP0LESp9GNaffOCClUnqIa4eixs4r7+5D+/5NI hR+pzWhJWv1I3S9iF0I5yDS/GTOa X-Google-Smtp-Source: APXvYqz/eKQfecH5pogmydYNM/DEFT7llungqQ6EElQ6BU4jTwbuOB/z9ya+6pDSg6K2F13yFk2Y6A== X-Received: by 2002:a05:600c:2383:: with SMTP id m3mr17906529wma.166.1573881893845; Fri, 15 Nov 2019 21:24:53 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id u26sm12511155wmj.9.2019.11.15.21.24.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 21:24:53 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 16 Nov 2019 06:24:30 +0100 Message-Id: <20191116052431.3908-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191116052431.3908-1-andreas.rheinhardt@gmail.com> References: <20191116052431.3908-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/cbs: Fix potential overflow 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 number of bits in a PutBitsContext must fit into an int, yet the various cbs functions to write units did not check that the argument in init_put_bits is in the range 0..INT_MAX / 8. This has been changed. Furthermore, the check 8 * data_size > data_bit_start that there is data beyond the initial padding could also overflow, so divide it by 8 to get an equivalent check without this problem. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_av1.c | 4 +++- libavcodec/cbs_h2645.c | 6 ++++-- libavcodec/cbs_jpeg.c | 4 +++- libavcodec/cbs_mpeg2.c | 6 ++++-- libavcodec/cbs_vp9.c | 4 +++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c index cca112ecf8..f091c5ac41 100644 --- a/libavcodec/cbs_av1.c +++ b/libavcodec/cbs_av1.c @@ -1239,7 +1239,9 @@ static int cbs_av1_write_unit(CodedBitstreamContext *ctx, err = cbs_av1_write_obu(ctx, unit, &pbc); if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + if (priv->write_buffer_size == INT_MAX / 8) + return AVERROR(ENOMEM); + priv->write_buffer_size = FFMIN(2 * priv->write_buffer_size, INT_MAX / 8); goto reallocate_and_try_again; } if (err < 0) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index 5dd371153a..6025035e22 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -1101,7 +1101,7 @@ static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx, const uint8_t *pos = data + data_bit_start / 8; av_assert0(data_bit_start >= 0 && - 8 * data_size > data_bit_start); + data_size > data_bit_start / 8); if (data_size * 8 + 8 > put_bits_left(pbc)) return AVERROR(ENOSPC); @@ -1411,7 +1411,9 @@ static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx, if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + if (priv->write_buffer_size == INT_MAX / 8) + return AVERROR(ENOMEM); + priv->write_buffer_size = FFMIN(2 * priv->write_buffer_size, INT_MAX / 8); goto reallocate_and_try_again; } // Overflow but we didn't notice. diff --git a/libavcodec/cbs_jpeg.c b/libavcodec/cbs_jpeg.c index a20f062f1b..71679ef5df 100644 --- a/libavcodec/cbs_jpeg.c +++ b/libavcodec/cbs_jpeg.c @@ -406,7 +406,9 @@ static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx, if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + if (priv->write_buffer_size == INT_MAX / 8) + return AVERROR(ENOMEM); + priv->write_buffer_size = FFMIN(2 * priv->write_buffer_size, INT_MAX / 8); goto reallocate_and_try_again; } if (err < 0) { diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c index 52252093b3..8591ada7bf 100644 --- a/libavcodec/cbs_mpeg2.c +++ b/libavcodec/cbs_mpeg2.c @@ -337,7 +337,7 @@ static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx, uint8_t *pos = slice->data + slice->data_bit_start / 8; av_assert0(slice->data_bit_start >= 0 && - 8 * slice->data_size > slice->data_bit_start); + slice->data_size > slice->data_bit_start / 8); if (slice->data_size * 8 + 8 > put_bits_left(pbc)) return AVERROR(ENOSPC); @@ -400,7 +400,9 @@ static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx, if (err == AVERROR(ENOSPC)) { // Overflow. - priv->write_buffer_size *= 2; + if (priv->write_buffer_size == INT_MAX / 8) + return AVERROR(ENOMEM); + priv->write_buffer_size = FFMIN(2 * priv->write_buffer_size, INT_MAX / 8); goto reallocate_and_try_again; } if (err < 0) { diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c index ebb4c573ea..88191fee3d 100644 --- a/libavcodec/cbs_vp9.c +++ b/libavcodec/cbs_vp9.c @@ -548,7 +548,9 @@ static int cbs_vp9_write_unit(CodedBitstreamContext *ctx, err = cbs_vp9_write_frame(ctx, &pbc, frame); if (err == AVERROR(ENOSPC)) { - priv->write_buffer_size *= 2; + if (priv->write_buffer_size == INT_MAX / 8) + return AVERROR(ENOMEM); + priv->write_buffer_size = FFMIN(2 * priv->write_buffer_size, INT_MAX / 8); goto reallocate_and_try_again; } if (err < 0)