From patchwork Mon May 15 18:50:12 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: 69385 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 90C4B3857708 for ; Mon, 15 May 2023 18:50:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 90C4B3857708 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684176639; bh=A+JwuZqHafc1HcGYqNViSsHRL5jJjvoEthf/I2ABifw=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=S06PP4Ai3xbqlke/mT33RCBKf6zqTE5WojLHTP09cDZi2TvYaYlWue29hq53WoPnz Z84FHJObeIgOZWhFfVdrDBPDPcJhPXuuDq886vZg0y25ziDkovLboLlXLDheBH7goj t+TKm975wCAUqIqcgUfTia4Issf47PmYkvii8aL8= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id DC833385773C for ; Mon, 15 May 2023 18:50:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC833385773C Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-512-UrJYzAJHOF-zOBiM9VolOQ-1; Mon, 15 May 2023 14:50:16 -0400 X-MC-Unique: UrJYzAJHOF-zOBiM9VolOQ-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-757742895b8so638146285a.2 for ; Mon, 15 May 2023 11:50:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684176615; x=1686768615; 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=A+JwuZqHafc1HcGYqNViSsHRL5jJjvoEthf/I2ABifw=; b=Kl/ANqIDNeGQnT3OknkXXq9mjN6Guo51/KGIco+A+36nNzNDft3Z7W/v26af1atcMi r7jbSQSu9UyjVoUhX06C8twQmmHRWCwZE11dtg6GZjVzYqlemmImQXmObpg1j4vFymLq 39Ax4rc1ti4R3pAjAEisr9DWhz3TYEj95SHBN5r1mrRuYjuivjNbayksKWLIbniR1prh ghw+tnfWdHcyxrzLE+ckEVH7u7XCcPxJZEayszVcVyGhsTQOTuTp9CQf2n3yXenTzYCF aOntLboURYBGRgWj7kEwGwvteDN+QBUyIOOEIdyMDLo/6NpyO2+Tn/jmHFw4norzBtOD xRYg== X-Gm-Message-State: AC+VfDzC4vohjja8CmulTgY5QfBpBViQIcCxsJ4ns+GUvo/JJfMJ9zCS xnMobBZeX3obLWBP7/peeS0Dlvipk/fh1eku7RAFFq9U8FmrFPebc2ZOhwIIiYbR+xa95vyDyc0 if71AO9UncK6N3yG0VSXZJIiTLmpKhsw/+W/Vsybf+3avgY5qslNqA2q1CIteIQbPkwA3fsRId9 xWuwtY X-Received: by 2002:a05:622a:1a1d:b0:3f5:39a7:1b9e with SMTP id f29-20020a05622a1a1d00b003f539a71b9emr3166327qtb.56.1684176615680; Mon, 15 May 2023 11:50:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ve9vX4mzVqQ40OvU77dsNLIJR1D5vVNh514HzveFWlemSIO15B/IibnAdgRrmLFdRI6NSMw== X-Received: by 2002:a05:622a:1a1d:b0:3f5:39a7:1b9e with SMTP id f29-20020a05622a1a1d00b003f539a71b9emr3166295qtb.56.1684176615391; Mon, 15 May 2023 11:50:15 -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 e1-20020a05620a208100b0074de8d07052sm6237qka.47.2023.05.15.11.50.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 11:50:14 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3] vfprintf-internal: Replace alloca with malloc. Date: Mon, 15 May 2023 14:50:12 -0400 Message-Id: <20230515185012.2768620-1-josimmon@redhat.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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_H2, 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Avoid potential stack overflow from unbounded alloca. --- stdio-common/vfprintf-internal.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index c76c06e49b..2d134ea08f 100644 --- a/stdio-common/vfprintf-internal.c +++ b/stdio-common/vfprintf-internal.c @@ -1001,6 +1001,7 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, scratch_buffer_init (&specsbuf); struct printf_spec *specs = specsbuf.data; size_t specs_limit = specsbuf.length / sizeof (specs[0]); + bool malloced_pa_user = false; /* Used as a backing store for args_value, args_size, args_type below. */ @@ -1171,7 +1172,10 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, else if (__glibc_unlikely (__printf_va_arg_table != NULL) && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL) { - args_value[cnt].pa_user = alloca (args_size[cnt]); + args_value[cnt].pa_user = malloc (args_size[cnt]); + if (args_value[cnt].pa_user == NULL) + break; + malloced_pa_user = true; (*__printf_va_arg_table[args_type[cnt] - PA_LAST]) (args_value[cnt].pa_user, ap_savep); } @@ -1335,6 +1339,9 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, - specs[nspecs_done].end_of_fmt)); } all_done: + if (malloced_pa_user) + for (cnt = 0; cnt < nargs; ++cnt) + free (args_value[cnt].pa_user); scratch_buffer_free (&argsbuf); scratch_buffer_free (&specsbuf); }