From patchwork Tue Jun 27 18:05:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Simmons-Talbott X-Patchwork-Id: 1800776 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=IqnubHz7; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QrCMP2Z7Zz20bN for ; Wed, 28 Jun 2023 04:06:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF8733858412 for ; Tue, 27 Jun 2023 18:06:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EF8733858412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687889183; bh=xm5XXYlVMFacvrhdmyIQYQ9WP2l7wYAsbQ8SO3E3UrA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=IqnubHz7a/KiyUm4wCSRY4LRLjf8kD0kVVS8dW051noYT4HtFJ+eWXH4l882tET2V lBV33ESgcq5+kcMvqscfLiUrh3GEjfDaLiBfNSPDJiCuz3qRweRgXUUjC14VdUObb9 HZ1bgmyuJqJkwbsO8gkno5TebcYefZAZr8AyQRvM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D61AC3858D28 for ; Tue, 27 Jun 2023 18:06:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D61AC3858D28 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433-u6WS88l0PNuQh7VvAnj-fQ-1; Tue, 27 Jun 2023 14:06:06 -0400 X-MC-Unique: u6WS88l0PNuQh7VvAnj-fQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-635a4267cf5so29187466d6.2 for ; Tue, 27 Jun 2023 11:06:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687889165; x=1690481165; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xm5XXYlVMFacvrhdmyIQYQ9WP2l7wYAsbQ8SO3E3UrA=; b=hbKjgCyKMoyljyIW1yNOVasC7up8YSRyQpG6BnYZM1nMD6oGY3uV3cLQlAOO43phqA /ZFbUo/z/+bQOvSHLhwdkQBELC3BKlY299V1g5f0XPd67mmBQotJmlqyCsFui9wE0AT6 HYWEKByy2SrqlUoAF4l9ptlreqDdK03aQ5TK46dCVnEC4q9cs4TmrcpfZ0XGVteuWwzF DacDQk8T2O1OM1dK2DnMQYXSFnlME59kVcZig3tjQIWndVCR4ddEF0DC7b7Tfw3rm0d5 r8RaaWZ43OOZtNtGoJkPzUy4HMyX/CBX4DC3HDdLV8cu38l14QjPygayLHTikoDE1aHQ JxEA== X-Gm-Message-State: AC+VfDy69U7ixdX+A9tr1EeuAnzVvGzz9gckGiHlf3R420gnYiwGH98W G/6y6uM/l8uNoi/REYbZyiIWaSjXRuZsCS3w4hTD8I68TH2B9lW6TLbIQJMh9gnazPIxGVb+/Kt rknJ2tS0O0AS7OnPN/8RtC3GCLj1y3A84Ytx9jyjuSPHkwB4+0Q2GL62Te+5ujo5L0k2uZXdWQG NugWIz X-Received: by 2002:a05:6214:f01:b0:625:87e4:1689 with SMTP id gw1-20020a0562140f0100b0062587e41689mr42595233qvb.20.1687889165216; Tue, 27 Jun 2023 11:06:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6tW0v0QwuZgRQIeLCk/4g+C5txlmMidGO8lfKahM/aG1i9UHRxw057Zlu2ZWAUieotytW+cg== X-Received: by 2002:a05:6214:f01:b0:625:87e4:1689 with SMTP id gw1-20020a0562140f0100b0062587e41689mr42595209qvb.20.1687889164865; Tue, 27 Jun 2023 11:06:04 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id es2-20020a056214192200b00621268e14efsm4800485qvb.55.2023.06.27.11.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 11:06:04 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] vfscanf-internal: Get rid of unbounded allocas Date: Tue, 27 Jun 2023 14:05:56 -0400 Message-Id: <20230627180556.728512-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Joe Simmons-Talbott via Libc-alpha From: Joe Simmons-Talbott Reply-To: Joe Simmons-Talbott Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Replace two unbounded allocas with scratch_buffers to avoid potential stack overflow. --- stdio-common/vfscanf-internal.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c index bfb9baa21a..60376ccff9 100644 --- a/stdio-common/vfscanf-internal.c +++ b/stdio-common/vfscanf-internal.c @@ -335,6 +335,12 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, struct char_buffer charbuf; scratch_buffer_init (&charbuf.scratch); + struct scratch_buffer wc_ext_sbuf; + scratch_buffer_init (&wc_ext_sbuf); + + struct scratch_buffer mb_ext_sbuf; + scratch_buffer_init (&mb_ext_sbuf); + #ifdef __va_copy __va_copy (arg, argptr); #else @@ -1488,8 +1494,14 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, wcdigits[n] = (const wchar_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_INDIGITS0_WC + n); - wchar_t *wc_extended = (wchar_t *) - alloca ((to_level + 2) * sizeof (wchar_t)); + wchar_t *wc_extended; + if (!scratch_buffer_set_array_size + (&wc_ext_sbuf, sizeof (wchar_t), to_level + 2)) + { + done = EOF; + goto errout; + } + wc_extended = wc_ext_sbuf.data; __wmemcpy (wc_extended, wcdigits[n], to_level); wc_extended[to_level] = __towctrans (L'0' + n, map); wc_extended[to_level + 1] = '\0'; @@ -1525,7 +1537,13 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, /* Allocate memory for extended multibyte digit. */ char *mb_extended; - mb_extended = (char *) alloca (mbdigits_len + mblen + 1); + if (!scratch_buffer_set_array_size + (&mb_ext_sbuf, 1, mbdigits_len + mblen + 1)) + { + done = EOF; + goto errout; + } + mb_extended = mb_ext_sbuf.data; /* And get the mbdigits + extra_digit string. */ *(char *) __mempcpy (__mempcpy (mb_extended, mbdigits[n], @@ -3063,5 +3081,7 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, free (*strptr); *strptr = NULL; } + scratch_buffer_free (&wc_ext_sbuf); + scratch_buffer_free (&mb_ext_sbuf); return done; }