From patchwork Mon May 22 17:06:27 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: 69810 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 29BD23858D38 for ; Mon, 22 May 2023 17:07:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29BD23858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684775222; bh=8St6KS9sH9jm1auk1g7vSmUSvqSbXQwhCU0klue/w2c=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ye7CbdNIRBbOPe9Tc+6mlDaC00MfFhQHvG4l+nAi8phNLR+/NRCTLjVzYDhlGoEOf vK42ufJ5YLQcyIVM3LtHiwpjfAiX69b5vEP3dbI5+zMA8iQeF4I1OvuShtZSXwrBuq jV0cu4N3ODs79l/qomb2JPYyQAud325Hx4tSu8EI= 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 B80EA3858D38 for ; Mon, 22 May 2023 17:06:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B80EA3858D38 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-218-h0_xRqsWNKSPjKTLujy8Pg-1; Mon, 22 May 2023 13:06:34 -0400 X-MC-Unique: h0_xRqsWNKSPjKTLujy8Pg-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-75788e0b1d3so499852985a.1 for ; Mon, 22 May 2023 10:06:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684775192; x=1687367192; 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=8St6KS9sH9jm1auk1g7vSmUSvqSbXQwhCU0klue/w2c=; b=jtTe6NtAseN5v2cOKxW2a+1nMG75PVpsZ/XSRcE/pQE6yT+DXf7xP7Q4izqqA5Xjnr iOT4ViBpAZcIuwnwhMM54AF2HNXiboFRBykXD3gBLsLcK9L5F+NV4MbsJwjKUJA5oTu8 HgwE50QTxtICzoIg78lYjalyelFkYwETBRr/uTpcytmE7Xhkbe2azPg/p4D9GmWCKfgA jYs3CUqbK/QtEjlFScjY3TZWRYjkA9rixpjAs5udNThR5JY702sn8He7xHmiSkVqMbAs O1wjSI0QhlCCnPd0Iw2F9q9P0fBRTWWmirZVPXDJxKHS9uoBdsSzRnT79fYPHG7KroLV iijw== X-Gm-Message-State: AC+VfDy0UibXMDxGkcb7pDuHa1Cxae9Arap5GsdOWTfk6Ak0rfv4scHj UG08DLPNrirROLPHiZigju6NYaccDU4WPbMRu6mOnMsbUq4Z0ff107HCvaM5IWHhY/EhUP46zc5 WhDIyjUIQIdTk1sapzkXMR6qjbYtIZoPfXlFdn2q1Rd+z9Y8zujsy9tH4CvkS6COYFuOEAzsWwA aSbQeT X-Received: by 2002:a05:620a:914:b0:75b:23a0:e7ca with SMTP id v20-20020a05620a091400b0075b23a0e7camr1492884qkv.43.1684775192426; Mon, 22 May 2023 10:06:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5nlE37oN2tw5CaVzJtT6nZjr5vpAMSiqNBtHctshYYiIrK/jwNpO755XpfaL2B3LftAHwa2A== X-Received: by 2002:a05:620a:914:b0:75b:23a0:e7ca with SMTP id v20-20020a05620a091400b0075b23a0e7camr1492860qkv.43.1684775192059; Mon, 22 May 2023 10:06:32 -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 w13-20020a05620a128d00b0074e077c9317sm1825716qki.99.2023.05.22.10.06.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 10:06:31 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3] vfprintf-internal: Get rid of alloca. Date: Mon, 22 May 2023 13:06:27 -0400 Message-Id: <20230522170627.239536-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. Use the existing scratch_buffer instead. --- Changes to v2: * Don't assume the first scratch buffer resize adds enough space. stdio-common/vfprintf-internal.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index c76c06e49b..20fd3c3043 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,25 @@ 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]); + while (args_pa_user + args_size[cnt] > + (int *) &argsbuf + argsbuf.length) + { + 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); }