From patchwork Mon May 22 16:55:29 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: 69809 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 9506D3858423 for ; Mon, 22 May 2023 16:56:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9506D3858423 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684774569; bh=tgEUw24jYAw23r0q7fYkFoFTlFeYfhuifhgUGijrcsw=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Tqbk+MYMvYfksNvBtSGZLMW15ubf3OOQJ/0ZfJPYxFySjnLuyHoqIFXza6M+N+As8 dd0289KDhKbtJimOOZXzkpY6bhKheze+jpIxWJ23J9H+TOEOvT9UQrnmBL6faDwcWs nholKYi7SOUsPCHNSr6uF3tYE6PC1kKuDtF2TWsI= 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 134C23858D37 for ; Mon, 22 May 2023 16:55:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 134C23858D37 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-311-hHbxHJj7Oli3eNgbh-8oEg-1; Mon, 22 May 2023 12:55:34 -0400 X-MC-Unique: hHbxHJj7Oli3eNgbh-8oEg-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3f687fcbf8bso31771441cf.0 for ; Mon, 22 May 2023 09:55:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684774534; x=1687366534; 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=tgEUw24jYAw23r0q7fYkFoFTlFeYfhuifhgUGijrcsw=; b=F4dFat6NlBfEkXyVdrfkO5BCbhVPxEZrfoln5zn+XBQGZEaCpSEW1MaCusbFhUfxxN XD7qh3fQ4UVrh/6LGpuN1g+cx2caP+DujTSjuLyddeJLq3Ftlxx9ZIsqyRuauX4AyKzm L0zc4oFB8LXy14WB1dkGTxzE54ofof6a4GyUYLC4IPXTUxkBMONbYdnukTGI+andelwr B/Q4w0HU93vwvwzYIMJgcPzIzPu4gkuQQvfIAJZiFIClwODIxDW1X+BZr8XRK44nHKn/ 1JGkvtDJP11VI2szox6uwEVDelpgRc8mIu3JXhwdleDevWkx1dtSLZYl/dH3hyy04LbL nwEw== X-Gm-Message-State: AC+VfDyYDzz0SPwzsQtjt7NOkdVhIFjNBCRlHwc5YJtRo/SN0EtbTa1B sWLzlyVHji1tuCGeNGSF/3u/MI858B5hlcPVdMfcOb2UTnyzh9gUlswC6/nXnt+lZECwLjPIUZz Daxfi+Uv7vJfIRXk16BVjr+vZhGop4ujLf3YT0+XnryzWyCojoSOgJWBwjcvqRJQ+yZJj/90B/L YX4R4A X-Received: by 2002:a05:622a:607:b0:3f5:4699:105f with SMTP id z7-20020a05622a060700b003f54699105fmr17004439qta.32.1684774533841; Mon, 22 May 2023 09:55:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4K1LPAZn/UlDjnnPlR9Bvk0UMYSOJQmZ9Pcb2Nc3D1WJ75QwF8YRnqgGwy3gPJClKL+2jE/g== X-Received: by 2002:a05:622a:607:b0:3f5:4699:105f with SMTP id z7-20020a05622a060700b003f54699105fmr17004424qta.32.1684774533557; Mon, 22 May 2023 09:55:33 -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 l16-20020a056214029000b0062168714c8fsm2058694qvv.120.2023.05.22.09.55.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 09:55:32 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] vfprintf-internal: Get rid of alloca. Date: Mon, 22 May 2023 12:55:29 -0400 Message-Id: <20230522165529.238596-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 v1: * fix 32bit builds. * ensure we have enough space in the scratch buffer before using it. 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..c05fc149d1 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]); + if (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); }