From patchwork Wed Feb 13 00:22:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 12056 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 22F8A4496BB for ; Wed, 13 Feb 2019 02:24:06 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 10EE868A148; Wed, 13 Feb 2019 02:24:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe05-2.mx.upcmail.net (vie01a-dmta-pe05-2.mx.upcmail.net [84.116.36.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3314B680C10 for ; Wed, 13 Feb 2019 02:23:59 +0200 (EET) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe05.mx.upcmail.net with esmtp (Exim 4.88) (envelope-from ) id 1gtiLH-0006sL-0J for ffmpeg-devel@ffmpeg.org; Wed, 13 Feb 2019 01:23:59 +0100 Received: from localhost ([213.47.41.20]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id tiKHgeyvE2WSstiKHg9AEU; Wed, 13 Feb 2019 01:22:58 +0100 X-Env-Mailfrom: michael@niedermayer.cc X-Env-Rcptto: ffmpeg-devel@ffmpeg.org X-SourceIP: 213.47.41.20 X-CNFS-Analysis: v=2.3 cv=E7kcWpVl c=1 sm=1 tr=0 a=I1eytVlZLDX1BM2VTtTtSw==:117 a=I1eytVlZLDX1BM2VTtTtSw==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=GEAsPZ9sns4A:10 a=ZZnuYtJkoWoA:10 a=PRZ5CmizAAAA:8 a=Oz9tvLE4ws4N_IwIjVAA:9 a=bpl3vXZhfGVbnQsf2V60:22 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Wed, 13 Feb 2019 01:22:38 +0100 Message-Id: <20190213002238.32283-2-michael@niedermayer.cc> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190213002238.32283-1-michael@niedermayer.cc> References: <20190213002238.32283-1-michael@niedermayer.cc> MIME-Version: 1.0 X-CMAE-Envelope: MS4wfCdDRFkOtFhMZpMQdnTA3iiaKdUCq6qNz9w/DSvlDROkTU1E98X3D1xG9GO6fIdiJCpTIMeJiaLV3YaKF6dnh1KgAwy7oRfXBnPLmIwzSrKiun7ntHNQ T10XDUVBpXbYYeMnahJVuQ24SYCEw9OaDsyAb9vIWbBhdyDLjHJ6TjNrl2eqfZjyTmVPGzroPZDkI1gd37KKziXJRTZKDqwzad8= Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/htmlsubtitles: Fixes denial of service due to use of sscanf in inner loop for handling braces 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: Kevin Backhouse via RT Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Kevin Backhouse via RT Fixes: [Semmle Security Reports #19439] Fixes: dos_sscanf2.mkv Signed-off-by: Michael Niedermayer --- libavcodec/htmlsubtitles.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libavcodec/htmlsubtitles.c b/libavcodec/htmlsubtitles.c index c0cfccfb16..d9221ba16b 100644 --- a/libavcodec/htmlsubtitles.c +++ b/libavcodec/htmlsubtitles.c @@ -24,6 +24,7 @@ #include "libavutil/common.h" #include "libavutil/parseutils.h" #include "htmlsubtitles.h" +#include static int html_color_parse(void *log_ctx, const char *str) { @@ -44,14 +45,32 @@ static void rstrip_spaces_buf(AVBPrint *buf) buf->str[--buf->len] = 0; } +/* + * Fast code for scanning text enclosed in braces. Functionally + * equivalent to this sscanf call: + * + * sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0 + */ +static int scanbraces(const char* in) { + if (strncmp(in, "{\\an", 4) != 0) { + return 0; + } + if (!isdigit(in[4])) { + return 0; + } + if (in[5] != '}') { + return 0; + } + return 1; +} + /* skip all {\xxx} substrings except for {\an%d} and all microdvd like styles such as {Y:xxx} */ static void handle_open_brace(AVBPrint *dst, const char **inp, int *an, int *closing_brace_missing) { - int len = 0; const char *in = *inp; - *an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0; + *an += scanbraces(in); if (!*closing_brace_missing) { if ( (*an != 1 && in[1] == '\\')