git: 018b8acde021 - stable/15 - hwpmc: On attach, ensure owner is a target effective GID's member

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 18 Sep 2025 08:51:56 UTC
The branch stable/15 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=018b8acde0218956dca99e49d384b072c7e43ca4

commit 018b8acde0218956dca99e49d384b072c7e43ca4
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-08-26 09:43:38 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-09-18 08:50:28 +0000

    hwpmc: On attach, ensure owner is a target effective GID's member
    
    This restores a check that existed prior to commit be1f7435ef218b1d
    ("kern: start tracking cr_gid outside of cr_groups[]").
    
    While here, improve pmc_can_attach()'s style by changing the type of
    'decline_attach' to 'bool', fixing tests on it, adding missing
    parentheses to 'return' statements, and by changing its return value
    type to 'bool'.
    
    Fixes:          be1f7435ef218b1d ("kern: start tracking cr_gid outside of cr_groups[]")
    MFC after:      9 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52252
    
    (cherry picked from commit 1c3c698ba4c40485ebbbd157cb49172cfa7de9b2)
---
 sys/dev/hwpmc/hwpmc_mod.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
index 9b85c989dc96..15c782b91b69 100644
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -210,7 +210,7 @@ static int	pmc_attach_one_process(struct proc *p, struct pmc *pm);
 static bool	pmc_can_allocate_row(int ri, enum pmc_mode mode);
 static bool	pmc_can_allocate_rowindex(struct proc *p, unsigned int ri,
     int cpu);
-static int	pmc_can_attach(struct pmc *pm, struct proc *p);
+static bool	pmc_can_attach(struct pmc *pm, struct proc *p);
 static void	pmc_capture_user_callchain(int cpu, int soft,
     struct trapframe *tf);
 static void	pmc_cleanup(void);
@@ -1029,19 +1029,19 @@ pmc_unlink_target_process(struct pmc *pm, struct pmc_process *pp)
  * Check if PMC 'pm' may be attached to target process 't'.
  */
 
-static int
+static bool
 pmc_can_attach(struct pmc *pm, struct proc *t)
 {
 	struct proc *o;		/* pmc owner */
 	struct ucred *oc, *tc;	/* owner, target credentials */
-	int decline_attach, i;
+	bool decline_attach;
 
 	/*
 	 * A PMC's owner can always attach that PMC to itself.
 	 */
 
 	if ((o = pm->pm_owner->po_owner) == t)
-		return 0;
+		return (false);
 
 	PROC_LOCK(o);
 	oc = o->p_ucred;
@@ -1066,18 +1066,17 @@ pmc_can_attach(struct pmc *pm, struct proc *t)
 	 * Every one of the target's group ids, must be in the owner's
 	 * group list.
 	 */
-	for (i = 0; !decline_attach && i < tc->cr_ngroups; i++)
+	for (int i = 0; !decline_attach && i < tc->cr_ngroups; i++)
 		decline_attach = !groupmember(tc->cr_groups[i], oc);
-
-	/* check the read and saved gids too */
-	if (decline_attach == 0)
-		decline_attach = !groupmember(tc->cr_rgid, oc) ||
+	if (!decline_attach)
+		decline_attach = !groupmember(tc->cr_gid, oc) ||
+		    !groupmember(tc->cr_rgid, oc) ||
 		    !groupmember(tc->cr_svgid, oc);
 
 	crfree(tc);
 	crfree(oc);
 
-	return !decline_attach;
+	return (!decline_attach);
 }
 
 /*
@@ -1412,7 +1411,7 @@ pmc_process_exec(struct thread *td, struct pmckern_procexec *pk)
 	 */
 	for (ri = 0; ri < md->pmd_npmc; ri++) {
 		if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) {
-			if (pmc_can_attach(pm, td->td_proc) != 0) {
+			if (pmc_can_attach(pm, td->td_proc)) {
 				pmc_detach_one_process(td->td_proc, pm,
 				    PMC_FLAG_NONE);
 			}