From patchwork Wed May 10 15:32:30 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: 69079 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 A91EF385354B for ; Wed, 10 May 2023 15:33:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A91EF385354B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683732784; bh=7JM6WkCOc8wXA9/n2t5+HJ7jJDknaInwqb9e+PoP7Ag=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=wOPzcq/WyA3Q0FAQ0kpfUExzZM5AW7gUSprqGnn5SHxxGR1VZEejvfttGrDTA7Lw1 5k/zmZlrvC3CzXj0w0AD2Lo62v9AHB/O7BRkCwX9AiOG5P8kH4SfhUC7V707Pujb5U CHUQ5I7UhYAbaPDDj8LASAjnF3haO53puWjMMqW4= 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 DA8453856947 for ; Wed, 10 May 2023 15:32:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA8453856947 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-625-kjBilIYwP_uwqGYi2Jw8TA-1; Wed, 10 May 2023 11:32:34 -0400 X-MC-Unique: kjBilIYwP_uwqGYi2Jw8TA-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7578369dff3so1293208685a.0 for ; Wed, 10 May 2023 08:32:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683732753; x=1686324753; 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=7JM6WkCOc8wXA9/n2t5+HJ7jJDknaInwqb9e+PoP7Ag=; b=Dodq+p13L0CsjlUX+x2rFxRctT8+zVo73+pW2ApiUa2XqdLzWT9W2Wl1GodpX1pqCu gh5YLm9VMrSF1CESkhcQHZiopgkdTGXv4/LQmdgS94riehN3BC3+T/L4iMEg/3ZAWoBA Fe+HKaFVCvgO1tnfZhGWuhCTNDlMknjRXZYT7qZssPkMwx5vLtMbUjstHfkHuJGM53sM 1UOEKp5nMkR78ZU3HyMytjI6U36bh0PqsbfOUc8Y7aZMXIcGD/mgiycKSCs+0hTfjoPD Cga6ro1QhkgvpPR9Oea6tBNmqiDECK+AGPwvxgV+eioP53ZnWLn4H9n3R8ierdarqspd KGYQ== X-Gm-Message-State: AC+VfDxmN3r7YqMH3YE7VhgE5PsahBweQjYuIgK3bdk89jbJhtdRZUZL iZs5y343VEZi90bZW1RVv3BoennFFwOmGX++rBVc3TlrmS2YL99gisH0lh+h7sBIhyaiEWtfGjK mGssLhj8oWZMJZfwqiJZsJCtqfweBnCf1rDRKx5cmql8XOEooPbZbCCSLuGEKyWH+NXyUDxw1Ko KUkkKT X-Received: by 2002:ad4:5f0c:0:b0:5ef:5503:d41c with SMTP id fo12-20020ad45f0c000000b005ef5503d41cmr35435468qvb.15.1683732753526; Wed, 10 May 2023 08:32:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5AV51zqLvEpmvLAsT4VH8+f23mCnrvnGQJyaqgLrysBNpEt7Yn980K7LGpmozQNNHEvmLKeA== X-Received: by 2002:ad4:5f0c:0:b0:5ef:5503:d41c with SMTP id fo12-20020ad45f0c000000b005ef5503d41cmr35435421qvb.15.1683732753168; Wed, 10 May 2023 08:32: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 c7-20020a0cd607000000b005ef493c6bebsm1604549qvj.77.2023.05.10.08.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 May 2023 08:32:32 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] vfprintf-internal: Replace alloca with malloc. Date: Wed, 10 May 2023 11:32:30 -0400 Message-Id: <20230510153230.2207571-1-josimmon@redhat.com> X-Mailer: git-send-email 2.39.2 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Avoid potential stack overflow from unbounded alloca. --- Changes to v1: * Don't check pointer before calling free() * Move deallocation code after the all_done label stdio-common/vfprintf-internal.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index c76c06e49b..58c21481ce 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); } @@ -1334,7 +1338,11 @@ printf_positional (struct Xprintf_buffer * buf, const CHAR_T *format, (specs[nspecs_done].next_fmt - 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); } -- 2.39.2