git: b357c9444899 - main - cap_enter(2): stricter checks
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 04 Nov 2026 16:23:24 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=b357c94448997b1702c5d3be9cc3a9531ba92ffc
commit b357c94448997b1702c5d3be9cc3a9531ba92ffc
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-10-21 21:57:59 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-04 16:23:11 +0000
cap_enter(2): stricter checks
- single-thread the process
Make cap_enter() a serialization point, ensuring that no threads are
executing with old credentials cached after the syscall returned.
- disallow entering capability mode if the file descriptor table or pwd
are shared. They are shared after rfork(2), and allowing it lets another
process grant additional privileges to the process in capability mode.
Reviewed by: markj
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D60963
---
sys/kern/sys_capability.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/sys/kern/sys_capability.c b/sys/kern/sys_capability.c
index d43102ab7849..e7187b7904ee 100644
--- a/sys/kern/sys_capability.c
+++ b/sys/kern/sys_capability.c
@@ -102,19 +102,35 @@ sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
{
struct ucred *newcred, *oldcred;
struct proc *p;
+ int error;
if (IN_CAPABILITY_MODE(td))
return (0);
- newcred = crget();
+ error = 0;
+ oldcred = newcred = crget();
p = td->td_proc;
PROC_LOCK(p);
+ if ((p->p_flag & P_HADTHREADS) != 0 &&
+ thread_single(p, SINGLE_BOUNDARY)) {
+ error = ERESTART;
+ goto out1;
+ }
+ if (refcount_load(&p->p_pd->pd_sharecount) > 1 ||
+ refcount_load(&p->p_fd->fd_refcnt) > 1) {
+ error = EBUSY;
+ goto out;
+ }
oldcred = crcopysafe(p, newcred);
newcred->cr_flags |= CRED_FLAG_CAPMODE;
proc_set_cred(p, newcred);
+out:
+ if ((p->p_flag & P_HADTHREADS) != 0)
+ thread_single_end(p, SINGLE_BOUNDARY);
+out1:
PROC_UNLOCK(p);
crfree(oldcred);
- return (0);
+ return (error);
}
/*