From patchwork Wed May 17 21:28:47 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: 1782961 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=BWLFPLiw; 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 4QM5pK23c5z20dX for ; Thu, 18 May 2023 07:29:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 41575385559B for ; Wed, 17 May 2023 21:29:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 41575385559B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684358951; bh=XH7cSkXlQ8r8h9UWoA4n/P9KQpbZty5RhNLMsXLDDQQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=BWLFPLiwRaXY7b41l12uuAk7eGcoQfGlJEduoT0Hv7HwQ+nxNrTCpD/WGhVgiW9Jn B393uokTQNxqd+OPfUaBQ7km+FXf9LKytbh9bQBjaU8o5AbycrrpuvshSN85qdGtsT EuuIvQVfmOBt2s8NvW6gdRTjTf9AGCXHuNCH1qXs= 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 8D1353858D1E for ; Wed, 17 May 2023 21:28:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D1353858D1E Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-YsmHtN86NJGQnziZDL4SkA-1; Wed, 17 May 2023 17:28:53 -0400 X-MC-Unique: YsmHtN86NJGQnziZDL4SkA-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6238dcbc30bso2583806d6.1 for ; Wed, 17 May 2023 14:28:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684358932; x=1686950932; 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=XH7cSkXlQ8r8h9UWoA4n/P9KQpbZty5RhNLMsXLDDQQ=; b=hX4vrfw4yOvCpW0np8iWAzJvDjzpEmmHOyi1wdBmPZ1cQThpW03vUxD2cS9Pzyu80P RtlHEl4WosPFIOP4Jjgyv0E102bbNpeHBWbBVrhrQFjbsaUBQRrlgOHB29a5uxsCACDu nPWybBF2QHhpQ62JDxVqV52FDsEsyq1Q/QjNSAXLqzsDA/jreBwOeX0pKefh96GlEQiu qKBHhlg1s9Uc3WiS5MRSM57azojaCBBesmggocubNl5dRuiGxbTO3rbCVouiftd0x+Fc orEhoQCi/DH0HqqqKquHoE+a9KM9kDmsErlSPbNCR+EaCtwBuHdGRCEcvBgVHvnshxkw JVjQ== X-Gm-Message-State: AC+VfDzu0Tfwv7S+fV0mTkWPEPVu8gRtpM+b/pw0GLBInbbwAiZ/xj5N O1+NaGxSzQyuJhTh8kTZjUwZkZVIpKuGyr20p4kyDPqdeJvE+BtnObXgX1KKoW06gKAkAzflNAB 7B10yXbmGR23mLsYivfUk0QJh9CTILiRDsMK9hsUqaGd4CIDfPx0RlQEUi7l5PeLFFygEJ6aCms fRxDn8 X-Received: by 2002:ad4:5ba3:0:b0:616:5c8b:59d with SMTP id 3-20020ad45ba3000000b006165c8b059dmr1660249qvq.20.1684358932476; Wed, 17 May 2023 14:28:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7S2PfzUyJRz4nqeZnsd4i/cHcY7TBlpdp2G0ywatW/TvHwq1/KaV0qwJJSjan9Tmyh0Pyrhg== X-Received: by 2002:ad4:5ba3:0:b0:616:5c8b:59d with SMTP id 3-20020ad45ba3000000b006165c8b059dmr1660229qvq.20.1684358932128; Wed, 17 May 2023 14:28:52 -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 j3-20020a0cf9c3000000b00623872d75basm25869qvo.124.2023.05.17.14.28.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 14:28:51 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH] vfprintf-internal: Get rid of alloca. Date: Wed, 17 May 2023 17:28:47 -0400 Message-Id: <20230517212847.1821277-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, 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" Avoid potential stack overflow from unbounded alloca. Use the existing scratch_buffer instead. --- stdio-common/vfprintf-internal.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index c76c06e49b..baaefb763a 100644 --- a/stdio-common/vfprintf-internal.c +++ b/stdio-common/vfprintf-internal.c @@ -1066,6 +1066,8 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, union printf_arg *args_value; int *args_size; int *args_type; + int *args_pa_user; + size_t args_pa_user_offset; { /* Calculate total size needed to represent a single argument across all three argument-related arrays. */ @@ -1082,6 +1084,7 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, now. */ args_size = &args_value[nargs].pa_int; args_type = &args_size[nargs]; + args_pa_user = &args_type[nargs]; memset (args_type, (mode_flags & PRINTF_FORTIFY) != 0 ? '\xff' : '\0', nargs * sizeof (*args_type)); } @@ -1171,7 +1174,24 @@ 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]); + if (args_pa_user > argsbuf + args_size) + { + args_pa_user_offset = args_pa_user - &args_type[nargs]; + if (!scratch_buffer_grow_preserve (&argsbuf)) + { + Xprintf_buffer_mark_failed (buf); + goto all_done; + } + args_value = argsbuf.data; + /* Set up the remaining two arrays to each point past the end of + the prior array, since space for all three has been allocated + now. */ + args_size = &args_value[nargs].pa_int; + args_type = &args_size[nargs]; + args_pa_user = &args_type[nargs] + args_pa_user_offset; + } + args_value[cnt].pa_user = args_pa_user; + args_pa_user += args_size[cnt]; (*__printf_va_arg_table[args_type[cnt] - PA_LAST]) (args_value[cnt].pa_user, ap_savep); }