From nobody Wed Nov 04 13:25:02 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jKR4t62Jhz6ym8v for ; Wed, 04 Nov 2026 13:25:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jKR4t5YsPz3LKm for ; Wed, 04 Nov 2026 13:25:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793798702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/h+RfYoL7Dm5oLyxV9eDdodesrsotUJcGz+vIRAhuOY=; b=JnIrGTW2GMPCB47nm4tcCPJ7JvA+qJ0ur2wgCaEIyHN6FMVS9oYUG1DsMSawuToiMriF1I Kam01ZWRGFAX2q+ZjCqkJlwa10yZkYbYflmnjcZNJmfr5Ea0J0Cxg8iMUYMdQoCeoRRNUs BB4qU6KPhanoIYj2Jo4HB0S63dN4/KpmJol0fE41YTZM3pKLFv13dQYhUEwZxdjIrfd5nf 4phV4R5f1cPlPEXnC3olD5PdRXFRu01xRruvos/1Fn+rtA69HwnVap0dEth7rzN5mIR5yv KvL1oDkL2HN5/7VYPSFQavjJBgY/6jXBO+MOF0oNBBpeJ87E3c20BibJ6UviWg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1793798702; b=StgrQYpFqeN85nucKUhhx2nZbM+jkRJlpmpPqk8RHihjSOG0xP7c1F8t/tFJlJxKoVwio7 rnN9fVHEsw3tuOdYW8nDZCqGiwUTrnXOkIY+qFHZpZ4LMfWphza1fkrg9pEesKx2eBPjpM 5MS57DtU66HCi5DrMwMWaQoZXD4n45QZ58Uli/SDILF5vLnv4XfE6lvQElN2DOxxucKWYM r1VpP3Orgh/dLK6XJdHaha//fllyGyKVEYeTpZ8Kmsj5agOCBL5+3o3UBQ3DEeIGtVTAth hJjHjs6qQv3dmeZWjZsmHRwPLVILXQ+UNE2nwc5JbfqKQYlf4yPhfmJD7XfLNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793798702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/h+RfYoL7Dm5oLyxV9eDdodesrsotUJcGz+vIRAhuOY=; b=QL2ImuUde2mmeDSA/eTMvQLd4/23mLmjJ9wPCwkdxH2kDeLe8aEJYhajIT+FiBiA+zmPR9 Nis2fz4vfVx2/X6n0qU4FwsEOASNdC8WQRw5v8A1/aGeSpC3IvbIZc6ciqUAROz5o39fZO 8xxOt3wQdKZD3TmZWHrV/Jqhs/3YY/3SMPPsCE5yFmOpQO0T844eJRRTvF4Iubh4V1Cgkw E/0giYUrnHLCazyTSB/uf6j87aGUi83TKjHy6hUOrwn5VVuB5gqYRrBssi296vzTu3Lj2v FxZW29MoKHT4HwF4K95ZazYAEtlWKDmlDLo6Ve0VszEaQl9dHRaM3Au5323lNA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jKR4t4wZJz13th for ; Wed, 04 Nov 2026 13:25:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3c393 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 04 Nov 2026 13:25:02 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: 6f2f1f34784e - stable/15 - cred: Fix a race in the FreeBSD-14-compatible setgroups(2) List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 6f2f1f34784e05b4491f246c7348d1a1e1aa4cf8 Auto-Submitted: auto-generated Date: Wed, 04 Nov 2026 13:25:02 +0000 Message-Id: <6aeb322e.3c393.5d37184d@gitrepo.freebsd.org> The branch stable/15 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=6f2f1f34784e05b4491f246c7348d1a1e1aa4cf8 commit 6f2f1f34784e05b4491f246c7348d1a1e1aa4cf8 Author: Olivier Certner AuthorDate: 2026-09-25 17:27:25 +0000 Commit: Olivier Certner CommitDate: 2026-11-04 13:21:19 +0000 cred: Fix a race in the FreeBSD-14-compatible setgroups(2) The freebsd14_setgroups() function would try to modify the effective GID on the current process' credentials without holding the process lock, allowing races with other threads concurrently modifying the process credentials. In the worst case, freebsd14_setgroups() could be manipulating a 'struct ucred' already freed by another thread (in the very small window after reading 'p_ucred' without lock but before modifying the effective GID). Concurrent uses of freebsd14_setgroups() or setcred() could also lead to non-atomic credentials modifications. Fix this by making kern_setgroups() take a new boolean indicating whether the passed array includes the effective GID in its first slot. When this boolean is true, it internally keeps the effective GID in a separate variable, pretends that the groups[] array that was passed actually starts at 'groups + 1', do the usual steps to set the supplementary groups and new extra ones to set the effective GID along, without releasing the process lock in between. Reported by: markj Reviewed by: markj MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D60028 (cherry picked from commit e6fbef451dd45159071a1b234cd13c66fbb654fa) --- sys/kern/kern_prot.c | 58 ++++++++++++++++++++++++++++++++++++++------------- sys/sys/syscallsubr.h | 3 ++- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 5370028f4490..bfb95508b64e 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1219,9 +1219,7 @@ freebsd14_setgroups(struct thread *td, struct freebsd14_setgroups_args *uap) /* * Before FreeBSD 15.0, we allow one more group to be supplied to - * account for the egid appearing before the supplementary groups. This - * may technically allow one more supplementary group for systems that - * did use the default NGROUPS_MAX if we round it back up to 1024. + * account for the egid appearing before the supplementary groups. */ gidsetsize = uap->gidsetsize; if (gidsetsize > ngroups_max + 1 || gidsetsize < 0) @@ -1234,11 +1232,9 @@ freebsd14_setgroups(struct thread *td, struct freebsd14_setgroups_args *uap) error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t)); if (error == 0) { - int ngroups = gidsetsize > 0 ? gidsetsize - 1 /* egid */ : 0; + int ngroups = gidsetsize; - error = kern_setgroups(td, &ngroups, groups + 1); - if (error == 0 && gidsetsize > 0) - td->td_proc->p_ucred->cr_gid = groups[0]; + error = kern_setgroups(td, &ngroups, groups, true); } if (groups != smallgroups) @@ -1281,7 +1277,7 @@ sys_setgroups(struct thread *td, struct setgroups_args *uap) error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t)); if (error == 0) - error = kern_setgroups(td, &gidsetsize, groups); + error = kern_setgroups(td, &gidsetsize, groups, false); if (groups != smallgroups) free(groups, M_TEMP); @@ -1289,25 +1285,43 @@ sys_setgroups(struct thread *td, struct setgroups_args *uap) } /* - * CAUTION: This function normalizes 'groups', possibly also changing the value - * of '*ngrpp' as a consequence. + * 'includes_egid' indicates that the first element of groups[] (if any) is the + * desired effective GID and that only the other elements will be used to set + * the supplementary groups. If true, and groups[] is empty, the effective GID + * is left unchanged and all supplementary groups deleted (see setgroups(2)). + * + * CAUTION: This function normalizes 'groups' (only the supplementary groups on + * 'includes_egid') and may need to update the value of '*ngrpp' as + * a consequence. */ int -kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups) +kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups, bool includes_egid) { struct proc *p = td->td_proc; struct ucred *newcred, *oldcred; + gid_t egid; int ngrp, error; ngrp = *ngrpp; /* Sanity check size. */ - if (ngrp < 0 || ngrp > ngroups_max) + if (ngrp < 0 || ngrp > (includes_egid ? ngroups_max + 1 : ngroups_max)) return (EINVAL); + if (includes_egid) { + if (ngrp > 0) { + egid = groups[0]; + groups++; + ngrp--; + } else + includes_egid = false; + } + AUDIT_ARG_GROUPSET(groups, ngrp); + if (includes_egid) + AUDIT_ARG_EGID(egid); groups_normalize(&ngrp, groups); - *ngrpp = ngrp; + *ngrpp = includes_egid ? ngrp + 1 : ngrp; newcred = crget(); crextend(newcred, ngrp); @@ -1324,15 +1338,29 @@ kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups) */ error = mac_cred_check_setgroups(oldcred, ngrp, ngrp == 0 ? NULL : groups); - if (error) + if (error != 0) goto fail; + + if (includes_egid) { + error = mac_cred_check_setegid(oldcred, egid); + if (error != 0) + goto fail; + } #endif error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS); - if (error) + if (error != 0) goto fail; + if (includes_egid) { + error = priv_check_cred(oldcred, PRIV_CRED_SETEGID); + if (error != 0) + goto fail; + } + crsetgroups_internal(newcred, ngrp, groups); + if (includes_egid) + change_egid(newcred, egid); setsugid(p); proc_set_cred(p, newcred); PROC_UNLOCK(p); diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index 164b3d874cf6..8061ea65e978 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -331,7 +331,8 @@ int kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags, struct mbuf *control, enum uio_seg segflg); int kern_setcred(struct thread *const td, const u_int flags, struct setcred *const wcred); -int kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups); +int kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups, + bool includes_egid); int kern_setitimer(struct thread *, u_int, struct itimerval *, struct itimerval *); int kern_setpriority(struct thread *td, int which, int who, int prio);