From patchwork Wed Jun 7 18:21:39 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: 70754 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 3C6823858296 for ; Wed, 7 Jun 2023 18:22:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C6823858296 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686162136; bh=J1yku1nXS62LkjqVR1Om/mgCi00GghmsSEqAu2xFtM0=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=SORCowGuVXMYP5cLCkKOGTM6qj4zIdBh2HBR1IfHD58vU8G4mu+O51dFXkJw4UDi2 pWAeTUbvUqnIW2UL5FNOetMFOZ9oKRIE8VWDVSpE6pdXOFQYRfqMt1ZRYtQPMRMNiM 8Wfp+z4wNI4i/PNUR6HOgaHrx6sV7aDENWxPGEio= 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 670643858C54 for ; Wed, 7 Jun 2023 18:21:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 670643858C54 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-3-wytJxuaWMkuFQwWKHTh_-g-1; Wed, 07 Jun 2023 14:21:51 -0400 X-MC-Unique: wytJxuaWMkuFQwWKHTh_-g-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6260e66ec46so79654836d6.3 for ; Wed, 07 Jun 2023 11:21:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686162111; x=1688754111; 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=J1yku1nXS62LkjqVR1Om/mgCi00GghmsSEqAu2xFtM0=; b=f0EtfwzcN7/oT7S3jVFLX8Ep9FSvIHnHzvOplyNzxJhEojRdAn6OaTwxM9PQ3Q7I9u mWp4moBRlOP2WcFOuQhh4RL5ApdXOKMFNJWJ81qUdW308P2P01w59hiTlYNdhuZS+aIR IokC0S9a/F9dZPMAlE4h5akdZIUcRL6d4Vl2U6RqCEBprBKYgTOXmRxheNwVErhNILZw umIiB0QLPDXBqtoIggdXTEJJFPAYx9wGUtNMocfLUZK6zWTWPCfVY83zsRdvXMa21Iqi c0Iln+2L0IiwjSyTWdWYMxrDgkWZiSnq6mqt13ybjsir7ZSjCAP7QLacr/zAjOBu8N6H 8hFg== X-Gm-Message-State: AC+VfDwHuIG7oCLg2EJpOOwZZgDWa6yDXq23mrD6aZydXOIUmWb9p89D uoNEEcuEfFjVrxkKMSZycurtrGPJkacsn0khXG8aSiqkd6ceCeadGVyry4mwMwi6xMc1nHWjYiq 40wWDoRz+1dFBJaqf0gEW X-Received: by 2002:a05:6214:248f:b0:62b:5fc9:7491 with SMTP id gi15-20020a056214248f00b0062b5fc97491mr4258762qvb.30.1686162111380; Wed, 07 Jun 2023 11:21:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ77RSr55JD/Eo2tOurrl7l3Q1RahqtDujHAJEfBklxe5aAxOkVdS373oWVI/djx6s1eIaPH7A== X-Received: by 2002:a05:6214:248f:b0:62b:5fc9:7491 with SMTP id gi15-20020a056214248f00b0062b5fc97491mr4258750qvb.30.1686162111130; Wed, 07 Jun 2023 11:21:51 -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 q18-20020a0ce212000000b0062623d8ab7esm83908qvl.111.2023.06.07.11.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 11:21:50 -0700 (PDT) To: Samuel Thibault , Sergey Bugaev , libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v2] grantpt: Get rid of alloca Date: Wed, 7 Jun 2023 14:21:39 -0400 Message-Id: <20230607182139.1995884-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.4 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" Replace alloca with a scratch_buffer to avoid potential stack overflows. Checked with build-many-glibcs.py on i686-gnu. --- Changes to v1: * Move scratch_buffer_free call into same scope as the scratch_buffer and use the address of the scratch_buffer for scratch_buffer_free. sysdeps/unix/grantpt.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sysdeps/unix/grantpt.c b/sysdeps/unix/grantpt.c index 38fce52576..77c8d814cc 100644 --- a/sysdeps/unix/grantpt.c +++ b/sysdeps/unix/grantpt.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -147,10 +148,19 @@ grantpt (int fd) /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX. Try a moderate value. */ grbuflen = 1024; - grtmpbuf = (char *) __alloca (grbuflen); + struct scratch_buffer sbuf; + scratch_buffer_init (&sbuf); + if (!scratch_buffer_set_array_size (&sbuf, 1, grbuflen)) + { + retval -1; + goto cleanup; + } + grtmpbuf = sbuf.data; __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p); if (p != NULL) tty_gid = p->gr_gid; + + scratch_buffer_free(&sbuf); } gid_t gid = tty_gid == -1 ? __getgid () : tty_gid;