git: 560c1bb21fe2 - stable/14 - cr_canseeothergids(): Make the logic easier to grasp

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Fri, 10 Oct 2025 17:16:18 UTC
The branch stable/14 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=560c1bb21fe2da4fe5c9a1186262c035b2ece9c5

commit 560c1bb21fe2da4fe5c9a1186262c035b2ece9c5
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-08-27 16:53:14 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-10-10 17:15:56 +0000

    cr_canseeothergids(): Make the logic easier to grasp
    
    Invert the initial test on whether the policy is in force so that, if
    there are no restrictions, the function bails out early, allowing to
    de-indent the rest of the code and have it finish with a non-zero (deny)
    'return'.
    
    No functional change (intended).
    
    MFC after:      5 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52272
    
    (cherry picked from commit f75d0dc533923345c653dcdcd5ebd1e53377a7c5)
---
 sys/kern/kern_prot.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
index d0f4c8cd6992..8ce75c6297e3 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -1843,19 +1843,22 @@ SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
 static int
 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
 {
-	if (!see_other_gids) {
-		if (realgroupmember(u1->cr_rgid, u2))
-			return (0);
+	if (see_other_gids)
+		return (0);
 
-		for (int i = 1; i < u1->cr_ngroups; i++)
-			if (realgroupmember(u1->cr_groups[i], u2))
-				return (0);
+	/* Restriction in force. */
 
-		if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
-			return (ESRCH);
-	}
+	if (realgroupmember(u1->cr_rgid, u2))
+		return (0);
 
-	return (0);
+	for (int i = 1; i < u1->cr_ngroups; i++)
+		if (realgroupmember(u1->cr_groups[i], u2))
+			return (0);
+
+	if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) == 0)
+		return (0);
+
+	return (ESRCH);
 }
 
 /*