From patchwork Thu Jul 9 10:35:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 20906 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 2D6CF44B499 for ; Thu, 9 Jul 2020 13:37:14 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0843C68B5AD; Thu, 9 Jul 2020 13:37:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 50C9268B4BB for ; Thu, 9 Jul 2020 13:37:08 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id z2so1811973wrp.2 for ; Thu, 09 Jul 2020 03:37:08 -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=IHK5FVxC6IzkghrqyLkn2VMnWZWyUxvcUjm3j60bp5Q=; b=DF9NFHEVV4cMbjxzrbbhGNi/I8JkxgOxF9fIKK7+6PVeciOEoLIkcARvePdBb1rmsV Hc8LbL6ycyfOxGAFK8LfaDvl1ekBN5lAPJ7OnaK0+G2/GktWoUzBSlM3DOBg1qyB5AMm uHTB8j2w8F7/r/7njsMhll8roT2srfCSCB+Z8L8JWcnyKCaYXlZSYb6Voe3JAMf+LEoM j6RZ6tCVQ/SaxQp03WgWh2H9jc7DzIDL8dISgqJrdkynZUT2EOgdG44XO7dJe+laqWeP bb5pxF67lqlVhQJgkRQxG2QvkkXELdROUvqg6Q+jXeePSZxJMDVZ9Dlg4UX9DWvegH3J LEtw== 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=IHK5FVxC6IzkghrqyLkn2VMnWZWyUxvcUjm3j60bp5Q=; b=nCHMcOiICm6/h8sgBkZWR2kjtJIgIdspSZ9BhYVeTwLqNcOqQHl4GHE2wvnHVfiYTW XAh8RGhBvl9ObyByhChUizh5/iapePHpVwNgQ7O4R70CuB8dKKzVPL8xln2nuteweXeC SZ/9Sn43DD9mfPKGHSzE/DEC3i90zt+CWfDf2TP24jV0BCg4eL0zV1At9Af5Z+2n7Koa n7d+2d11vVkLuPt57Ro1Sm+ZA4LG8BQTLVhUkoYXUcva9YiS3yt2LevkpP5H3Mz/VRZw 6IyYNP8I6VPh+DnxJrTwogcGVeP5KqELmf1ImiemGDA6braPwZUIGnU5T5xl9JSLTL7R UJPA== X-Gm-Message-State: AOAM532ZQymANCnMXk9AZpAgSfoLAxndVXFuPeT1U40++XkQmcSCewt+ a3BedllWNV72MR6Gl9JXVl3eOTfI X-Google-Smtp-Source: ABdhPJy7pKHw+ezJi2adhMswskRZoM5ApoFKtnTq4hSQUUiVCMDTcdffbQldqqFUD0+CFtd1amQbZg== X-Received: by 2002:adf:9286:: with SMTP id 6mr63051787wrn.361.1594291027509; Thu, 09 Jul 2020 03:37:07 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc10296.dynamic.kabel-deutschland.de. [188.193.2.150]) by smtp.gmail.com with ESMTPSA id c15sm4155623wme.23.2020.07.09.03.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2020 03:37:06 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Jul 2020 12:35:37 +0200 Message-Id: <20200709103542.19909-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200709103542.19909-1-andreas.rheinhardt@gmail.com> References: <20200709103542.19909-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/7] avformat/avc: Fix undefined pointer arithmetic for small buffers 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" avc_find_startcode_internal() would subtract 6 from a pointer (representing the end of a buffer) without checking whether the buffer was actually large enough; but pointer arithmetic is undefined except when one stays in the buffer. Signed-off-by: Andreas Rheinhardt --- libavformat/avc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libavformat/avc.c b/libavformat/avc.c index 55494eb08a..cc92fb1038 100644 --- a/libavformat/avc.c +++ b/libavformat/avc.c @@ -44,14 +44,15 @@ static inline unsigned get_ue_golomb(GetBitContext *gb) static const uint8_t *avc_find_startcode_internal(const uint8_t *p, const uint8_t *end) { - const uint8_t *a = p + 4 - ((intptr_t)p & 3); + if (end - p <= 5) + goto rest; - for (end -= 3; p < a && p < end; p++) { + for (; (uintptr_t)p & 3; p++) { if (p[0] == 0 && p[1] == 0 && p[2] == 1) return p; } - for (end -= 3; p < end; p += 4) { + for (end -= 6; p < end; p += 4) { uint32_t x = *(const uint32_t*)p; // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian @@ -70,13 +71,15 @@ static const uint8_t *avc_find_startcode_internal(const uint8_t *p, const uint8_ } } } + end += 6; - for (end += 3; p < end; p++) { +rest: + for (; end - p > 3; p++) { if (p[0] == 0 && p[1] == 0 && p[2] == 1) return p; } - return end + 3; + return end; } const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){