From f3de805ace484db4a3bf9191a150ef4843ae92f3 Mon Sep 17 00:00:00 2001 From: Olivier Certner Date: Fri, 18 Aug 2023 01:54:44 +0200 Subject: [PATCH] groupmember(): Extract the supplementary group search in a separate function This is in preparation for the introduction of the new realgroupmember() function, which does the same search into supplementary groups as groupmember(). Reviewed by: mhorne MFC after: 2 weeks Sponsored by: Kumacom SAS Differential Revision: https://reviews.freebsd.org/D40640 (cherry picked from commit b725f232f3b09b4bcbc426854fe1545234c66965) --- sys/kern/kern_prot.c | 51 +++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 648c067dc52..21f5e5d3bc1 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1273,36 +1273,43 @@ sys___setugid(struct thread *td, struct __setugid_args *uap) } /* - * Check if gid is a member of the group set. + * Returns whether gid designates a supplementary group in cred. + */ +static int +supplementary_group_member(gid_t gid, struct ucred *cred) +{ + int l, h, m; + + /* + * Perform a binary search of the supplemental groups. This is possible + * because we sort the groups in crsetgroups(). + */ + l = 1; + h = cred->cr_ngroups; + + while (l < h) { + m = l + (h - l) / 2; + if (cred->cr_groups[m] < gid) + l = m + 1; + else + h = m; + } + + return (l < cred->cr_ngroups && cred->cr_groups[l] == gid); +} + +/* + * Check if gid is a member of the (effective) group set (i.e., effective and + * supplementary groups). */ int groupmember(gid_t gid, struct ucred *cred) { - int l; - int h; - int m; if (cred->cr_groups[0] == gid) - return(1); - - /* - * If gid was not our primary group, perform a binary search - * of the supplemental groups. This is possible because we - * sort the groups in crsetgroups(). - */ - l = 1; - h = cred->cr_ngroups; - while (l < h) { - m = l + ((h - l) / 2); - if (cred->cr_groups[m] < gid) - l = m + 1; - else - h = m; - } - if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid)) return (1); - return (0); + return (supplementary_group_member(gid, cred)); } /*