From patchwork Thu Oct 12 15:34:40 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: 77623 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 B7AA7385035F for ; Thu, 12 Oct 2023 15:35:38 +0000 (GMT) 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 B6D493858039 for ; Thu, 12 Oct 2023 15:35:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6D493858039 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697124923; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kmEM8iTW3Jdm4jrMbUy7co4H3kvcyDs00Nqz7o85LNI=; b=jKtko27geNfgdPi4E0clyyt3g8aqXuoo2S6i44TzuDrVUpsCVYVD2XXyzLv+Uc+SQUDXTv SNZXxU7cJhASFo4srepBc7wdiQ3KgFZCPMZTILKX4ezXXmCYhnF0CU/tjX3iVO3cEBx3k4 8SQnGuXcDt6WxU52YfgnetJGNMrbDro= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-275-9vIGPEYpPGSZWTFR0RjTDw-1; Thu, 12 Oct 2023 11:35:17 -0400 X-MC-Unique: 9vIGPEYpPGSZWTFR0RjTDw-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-774105e8c7fso128303685a.0 for ; Thu, 12 Oct 2023 08:35:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697124916; x=1697729716; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kmEM8iTW3Jdm4jrMbUy7co4H3kvcyDs00Nqz7o85LNI=; b=a48E4kgAY6rRjD/CDI+JyKZG+N5Pjdz5fLATvSrfbZjgl+z+cqv2w8r7uxBFIWOax/ NDoV9kgjJMk/fpPCFc8p2te2/6mxW42ZJaKuqvgZ4giOattUB0yv+L5JMzTBYgwH4cl6 ++u8hg+PPT2OLZhRXsuyORQsaK3tUKdpTHFNrEjbTbCNzS3Csl8QXZG/46+79HlRvoUv Ypuv++avIhhYosw9hAwEWJH7ESJWll+IbQpMF2UBVbh+6YKXWqiBS59SiI+wUPdy+rh5 SNfrEOlY61pd5iit273zdqVr4Frn5nRmTJO0CN7N4xg1i9Ttu6JHskdqvxsU4jm95vFw Gdtg== X-Gm-Message-State: AOJu0Yw5SWpkaa21eDYhozz4gb2E3bPfoZgtr941+JqxYsm2d7XIZtH1 N52w53RjnO53DF8nFrCIiLyay5686rGinOWswwIo1jomyhA3S9BjuQtl/DWkpLJk2IyD/i2ZIUG 2wiquwT38fNNT+iozDHolEpsr2ae+0g4jCjOvvLla9B1i2banfn+GpI1AYR6n0tmChh/ipE/cMY C78hYp X-Received: by 2002:a05:620a:4413:b0:76f:1272:2aa8 with SMTP id v19-20020a05620a441300b0076f12722aa8mr32248394qkp.6.1697124916167; Thu, 12 Oct 2023 08:35:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHtoytD4gbnc2QG/Wt+4O9R49eGuwSEpEBT5SziVqWP/11bUeuI4SbdwWPkpX8DvDBOHfM2Gw== X-Received: by 2002:a05:620a:4413:b0:76f:1272:2aa8 with SMTP id v19-20020a05620a441300b0076f12722aa8mr32248364qkp.6.1697124915823; Thu, 12 Oct 2023 08:35:15 -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 c2-20020a05620a134200b00767c961eb47sm6109816qkl.43.2023.10.12.08.35.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 08:35:15 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH 1/2] posix/group_member: Add group_member2 with error return. Date: Thu, 12 Oct 2023 11:34:40 -0400 Message-ID: <20231012153508.2057768-2-josimmon@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231012153508.2057768-1-josimmon@redhat.com> References: <20231012153508.2057768-1-josimmon@redhat.com> 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: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Because group_member doesn't return an error status and we are trying to get rid of alloca usage. Add a new group_member2 which returns -1 and sets errno on error. The old group_member will be deprecated in a later change. --- include/unistd.h | 1 + posix/Makefile | 1 + posix/group_member.c | 32 +++++++++++++++++++++++++++++ posix/tst-group_member2.c | 42 +++++++++++++++++++++++++++++++++++++++ posix/unistd.h | 3 +++ 5 files changed, 79 insertions(+) create mode 100644 posix/tst-group_member2.c diff --git a/include/unistd.h b/include/unistd.h index e241603b81..39d5bda372 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -131,6 +131,7 @@ extern __gid_t __getegid (void) attribute_hidden; extern int __getgroups (int __size, __gid_t __list[]) attribute_hidden; libc_hidden_proto (__getpgid) extern int __group_member (__gid_t __gid) attribute_hidden; +extern int __group_member2 (__gid_t __gid) attribute_hidden; extern int __setuid (__uid_t __uid); extern int __setreuid (__uid_t __ruid, __uid_t __euid); extern int __setgid (__gid_t __gid); diff --git a/posix/Makefile b/posix/Makefile index c36b9d981e..b46ff3259c 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -294,6 +294,7 @@ tests := \ tst-glob_symlinks \ tst-gnuglob \ tst-gnuglob64 \ + tst-group_member2 \ tst-mmap \ tst-mmap-offset \ tst-nanosleep \ diff --git a/posix/group_member.c b/posix/group_member.c index 22422b1f9f..6064eb7264 100644 --- a/posix/group_member.c +++ b/posix/group_member.c @@ -16,6 +16,8 @@ License along with the GNU C Library; if not, see . */ +#include +#include #include #include #include @@ -47,3 +49,33 @@ __group_member (gid_t gid) return 0; } weak_alias (__group_member, group_member) + +int +__group_member2 (gid_t gid) +{ + int n; + gid_t *groups; + struct scratch_buffer buf; + scratch_buffer_init (&buf); + + n = __getgroups (0, NULL); + if (!scratch_buffer_set_array_size (&buf, n, sizeof (*groups))) + { + errno = ENOMEM; + return -1; + } + groups = buf.data; + + n = __getgroups (n, groups); + + while (n-- > 0) + if (groups[n] == gid) + { + scratch_buffer_free (&buf); + return 1; + } + + scratch_buffer_free (&buf); + return 0; +} +weak_alias (__group_member2, group_member2) diff --git a/posix/tst-group_member2.c b/posix/tst-group_member2.c new file mode 100644 index 0000000000..4a360c71da --- /dev/null +++ b/posix/tst-group_member2.c @@ -0,0 +1,42 @@ +/* Basic tests for group_memmber2. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +#include + +static int do_test (void) +{ + int n; + gid_t *groups; + + n = getgroups (0, NULL); + groups = alloca (n * sizeof (*groups)); + n = getgroups (n, groups); + + while (n-- > 0) + TEST_COMPARE (1, group_member2 (groups[n])); + + return EXIT_SUCCESS; +} + +#include + diff --git a/posix/unistd.h b/posix/unistd.h index 0477527a60..96816b186d 100644 --- a/posix/unistd.h +++ b/posix/unistd.h @@ -713,6 +713,9 @@ extern int getgroups (int __size, __gid_t __list[]) __THROW __wur #ifdef __USE_GNU /* Return nonzero iff the calling process is in group GID. */ extern int group_member (__gid_t __gid) __THROW; +/* Return nonzero iff the calling process is in group GID. Return + -1 on error and set errno. */ +extern int group_member2 (__gid_t __gid) __THROW; #endif /* Set the user ID of the calling process to UID.