git: 2a2bfa6ad92e - main - New realgroupmember()

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 28 Sep 2023 15:10:50 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=2a2bfa6ad92e9c82dcc55733ad2fd58fd2ea7559

commit 2a2bfa6ad92e9c82dcc55733ad2fd58fd2ea7559
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-08-17 23:54:45 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2023-09-28 15:05:46 +0000

    New realgroupmember()
    
    Like groupmember(), but taking into account the real group instead of
    the effective group.  Leverages the new supplementary_group_member()
    function.
    
    Reviewed by:            mhorne
    MFC after:              2 weeks
    Sponsored by:           Kumacom SAS
    Differential Revision:  https://reviews.freebsd.org/D40641
---
 sys/kern/kern_prot.c | 13 +++++++++++++
 sys/sys/ucred.h      |  1 +
 2 files changed, 14 insertions(+)

diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
index cbaeb1a50814..b62a5e9ee20b 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -1316,6 +1316,19 @@ groupmember(gid_t gid, struct ucred *cred)
 	return (supplementary_group_member(gid, cred));
 }
 
+/*
+ * Check if gid is a member of the real group set (i.e., real and supplementary
+ * groups).
+ */
+int
+realgroupmember(gid_t gid, struct ucred *cred)
+{
+	if (gid == cred->cr_rgid)
+		return (1);
+
+	return (supplementary_group_member(gid, cred));
+}
+
 /*
  * Test the active securelevel against a given level.  securelevel_gt()
  * implements (securelevel > level).  securelevel_ge() implements
diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h
index eb92776c158a..633bf436fcd4 100644
--- a/sys/sys/ucred.h
+++ b/sys/sys/ucred.h
@@ -159,6 +159,7 @@ void	cru2x(struct ucred *cr, struct xucred *xcr);
 void	cru2xt(struct thread *td, struct xucred *xcr);
 void	crsetgroups(struct ucred *cr, int n, gid_t *groups);
 int	groupmember(gid_t gid, struct ucred *cred);
+int	realgroupmember(gid_t gid, struct ucred *cred);
 #endif /* _KERNEL */
 
 #endif /* !_SYS_UCRED_H_ */