From patchwork Mon Aug 28 19:48:59 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: 74848 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 335413858C74 for ; Mon, 28 Aug 2023 19:49:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 335413858C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693252180; bh=l86FHbamtwFOICcjX6fJTj3CkcHxBdh6jUoMViB0mnw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YVz5d9q59IE7cjL+3F1tSQXm97kL0drcy2YYCyw3cFyxN7CTVuuE59CqD6D7TB+iv OnLTwlyKJMotsBrUp0ORKmE+nczD/2FakJqMAp0QTMDrFBXLV/7lT5NdoetKK7JMM8 9Dd8RRUFKIDi1vRhMueclHcdplBf/8z0t896a7WY= 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 DDF243858D28 for ; Mon, 28 Aug 2023 19:49:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDF243858D28 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-417-TgpMTmxINwSkea7SWFi1vQ-1; Mon, 28 Aug 2023 15:49:16 -0400 X-MC-Unique: TgpMTmxINwSkea7SWFi1vQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-63faa1e03a8so38188046d6.2 for ; Mon, 28 Aug 2023 12:49:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693252155; x=1693856955; 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=l86FHbamtwFOICcjX6fJTj3CkcHxBdh6jUoMViB0mnw=; b=hkb3uPjaowgvxHtWjgSuB/7aXs5mqKCDwm9ncZqKIX82lxSD01bBHAtuyDhi5oKRRT rlc60jdGGQrxCCKVw/oyymzb3KGKHC7GQsBpIO3DH4rZMd9ml48asN5l+Pi5nH45lGI/ LJLGgy2SDMuh0NaB6V50p49RWdunTDRZyG223IF77mIiXXLfhaGr0+uB7wEovPjCnTo8 mLd0rk5pG4O+9noA/r0vFy+GCHRmaM5zWVJfIn4NKka8RDJzIMvcW3Af1s8IskHmyikb jImr5BhcukM+UTPmcnZbFLtZHZdN30oTBZC1O5NM33wj1B+iwRZUIJ1wWZw63EWOG/uZ WDrA== X-Gm-Message-State: AOJu0Yzz77Yj7vV0LdnDxXxIJYhPbKAwmbhLVbok/J/InDRsrNmxFZpc ZrgUJCby0ZBC3ECH7AeL4vPE4r0BLLpQHC4PUBAkk2wVVHLis8fP4Og4bPZ5xh1OpFFK2m+iHPO TTP1PJDWmvilvfpORKX5JUDENIvSxZXyL5Bln8eNxfQk5JggAq1x/3vwwsj+w0XXuHMLV5Bt1uy DIR4XX X-Received: by 2002:a0c:e114:0:b0:63c:efa5:2860 with SMTP id w20-20020a0ce114000000b0063cefa52860mr25619005qvk.25.1693252155266; Mon, 28 Aug 2023 12:49:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGA+a5FmH3sGxOVHH4+7gIYxGBx2PijZHIO+X46w0Gw+eXvcy3D2JoVsEKneyIH5KuQm7N5xA== X-Received: by 2002:a0c:e114:0:b0:63c:efa5:2860 with SMTP id w20-20020a0ce114000000b0063cefa52860mr25618993qvk.25.1693252154932; Mon, 28 Aug 2023 12:49:14 -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 h13-20020a37de0d000000b0076ee82f7726sm2556910qkj.132.2023.08.28.12.49.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Aug 2023 12:49:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2] getpw: Get rid of alloca Date: Mon, 28 Aug 2023 15:48:59 -0400 Message-ID: <20230828194911.877648-1-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 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" Since _SC_GETPW_R_SIZE_MAX will be always NSS_BUFLEN_PASSWD, use a fixed sized array rather than alloca to avoid potential stack overflow. --- Changes to v1: * Get rid of the scratch_buffer and use a fixed sized array instead. pwd/getpw.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pwd/getpw.c b/pwd/getpw.c index cf747374b8..86ccbc8d6e 100644 --- a/pwd/getpw.c +++ b/pwd/getpw.c @@ -15,7 +15,6 @@ License along with the GNU C Library; if not, see . */ -#include #include #include #include @@ -31,31 +30,39 @@ int __getpw (__uid_t uid, char *buf); int __getpw (__uid_t uid, char *buf) { - size_t buflen; - char *tmpbuf; + char tmpbuf[NSS_BUFLEN_PASSWD]; struct passwd resbuf, *p; + int retval = 0; if (buf == NULL) { __set_errno (EINVAL); - return -1; + retval = -1; + goto error_out; } - buflen = __sysconf (_SC_GETPW_R_SIZE_MAX); - tmpbuf = alloca (buflen); - - if (__getpwuid_r (uid, &resbuf, tmpbuf, buflen, &p) != 0) - return -1; + if (__getpwuid_r (uid, &resbuf, tmpbuf, sizeof (tmpbuf), &p) != 0) + { + retval = -1; + goto error_out; + } if (p == NULL) - return -1; + { + retval = -1; + goto error_out; + } if (sprintf (buf, "%s:%s:%lu:%lu:%s:%s:%s", p->pw_name, p->pw_passwd, (unsigned long int) p->pw_uid, (unsigned long int) p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell) < 0) - return -1; + { + retval = -1; + goto error_out; + } - return 0; +error_out: + return retval; } weak_alias (__getpw, getpw)