The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=a69b66a1b3f67d27c8f06287df18b15431e8dd7a
commit a69b66a1b3f67d27c8f06287df18b15431e8dd7a Author: Mark Johnston <[email protected]> AuthorDate: 2026-11-30 22:46:04 +0000 Commit: Mark Johnston <[email protected]> CommitDate: 2026-12-02 19:25:00 +0000 linux: Release futexes and clear tids earlier during execve During execve, other threads in the process are terminated. As a part of this, they invoke the sv_thread_detach handler, which modifies user memory. This handler is invoked only after the new process image has been loaded, in which case they can modify memory in a setuid or setgid image process. Fix this by implementing sv_onexec_old, which explicitly runs before the new image is loaded. This is how native executables release robust mutexes. Approved by: so Security: FreeBSD-SA-26:99.linux Security: CVE-2026-127757 Reported by: mmacy, kbowling Reviewed by: kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D62207 --- sys/amd64/linux/linux_sysvec.c | 1 + sys/amd64/linux32/linux32_sysvec.c | 1 + sys/arm64/linux/linux_sysvec.c | 1 + sys/compat/linux/linux_emul.h | 1 + sys/compat/linux/linux_fork.c | 62 ++++++++++++++++++++++++++++++-------- sys/compat/linux/linux_futex.c | 2 +- sys/powerpc/linux/linux_sysvec.c | 2 ++ 7 files changed, 57 insertions(+), 13 deletions(-) diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c index 501cc9c1ebaa..4f8c549c2b6e 100644 --- a/sys/amd64/linux/linux_sysvec.c +++ b/sys/amd64/linux/linux_sysvec.c @@ -717,6 +717,7 @@ struct sysentvec elf_linux_sysvec = { .sv_hwcap2 = NULL, .sv_hwcap3 = NULL, .sv_hwcap4 = NULL, + .sv_onexec_old = linux_on_exec_old, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, diff --git a/sys/amd64/linux32/linux32_sysvec.c b/sys/amd64/linux32/linux32_sysvec.c index 6b327a582cdf..34f97c252141 100644 --- a/sys/amd64/linux32/linux32_sysvec.c +++ b/sys/amd64/linux32/linux32_sysvec.c @@ -817,6 +817,7 @@ struct sysentvec elf_linux_sysvec = { .sv_hwcap2 = NULL, .sv_hwcap3 = NULL, .sv_hwcap4 = NULL, + .sv_onexec_old = linux_on_exec_old, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, diff --git a/sys/arm64/linux/linux_sysvec.c b/sys/arm64/linux/linux_sysvec.c index b61eb6534d75..9cc6540faef0 100644 --- a/sys/arm64/linux/linux_sysvec.c +++ b/sys/arm64/linux/linux_sysvec.c @@ -461,6 +461,7 @@ struct sysentvec elf_linux_sysvec = { .sv_hwcap2 = &linux_elf_hwcap2, .sv_hwcap3 = &linux_elf_hwcap3, .sv_hwcap4 = &linux_elf_hwcap4, + .sv_onexec_old = linux_on_exec_old, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, diff --git a/sys/compat/linux/linux_emul.h b/sys/compat/linux/linux_emul.h index 96084aee219e..598cea5848b2 100644 --- a/sys/compat/linux/linux_emul.h +++ b/sys/compat/linux/linux_emul.h @@ -54,6 +54,7 @@ struct linux_emuldata *em_find(struct thread *); void linux_proc_init(struct thread *, struct thread *, bool); void linux_on_exit(struct proc *); void linux_schedtail(struct thread *); +void linux_on_exec_old(struct thread *); int linux_on_exec(struct proc *, struct image_params *); void linux_thread_dtor(struct thread *); int linux_common_execve(struct thread *, struct image_args *); diff --git a/sys/compat/linux/linux_fork.c b/sys/compat/linux/linux_fork.c index 353bac7635a6..e21aba8ae14c 100644 --- a/sys/compat/linux/linux_fork.c +++ b/sys/compat/linux/linux_fork.c @@ -531,23 +531,15 @@ linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args return (0); } -void -linux_thread_detach(struct thread *td) +static void +linux_thread_clear_tid(struct thread *td, struct linux_emuldata *em) { - struct linux_emuldata *em; int *child_clear_tid; - int error; - - em = em_find(td); - KASSERT(em != NULL, ("thread_detach: emuldata not found.\n")); - - LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid); - - release_futexes(td, em); child_clear_tid = em->child_clear_tid; - if (child_clear_tid != NULL) { + int error; + LINUX_CTR2(thread_detach, "thread(%d) %p", em->em_tid, child_clear_tid); @@ -563,6 +555,21 @@ linux_thread_detach(struct thread *td) if (error != 0) linux_msg(td, "futex stuff in thread_detach failed."); } +} + +void +linux_thread_detach(struct thread *td) +{ + struct linux_emuldata *em; + + em = em_find(td); + KASSERT(em != NULL, ("thread_detach: emuldata not found.\n")); + + LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid); + + release_futexes(td, em); + + linux_thread_clear_tid(td, em); /* * Do not rely on the robust list which is maintained by userspace, @@ -570,3 +577,34 @@ linux_thread_detach(struct thread *td) */ umtx_thread_exit(td); } + +void +linux_on_exec_old(struct thread *td) +{ + struct proc *p; + struct thread *td2; + + p = td->td_proc; + + /* + * There is no need to lock the list as only this thread can be + * running. + */ + FOREACH_THREAD_IN_PROC(p, td2) { + struct linux_emuldata *em; + + em = em_find(td2); + + release_futexes(td2, em); + if (td2 != td) + linux_thread_clear_tid(td2, em); + + /* + * Make sure linux_thread_detach() doesn't modify the new + * vmspace. + */ + em->robust_futexes = NULL; + em->child_clear_tid = NULL; + } + umtx_exec(p); +} diff --git a/sys/compat/linux/linux_futex.c b/sys/compat/linux/linux_futex.c index 4a6c3d966e33..0dd1c2562f59 100644 --- a/sys/compat/linux/linux_futex.c +++ b/sys/compat/linux/linux_futex.c @@ -972,7 +972,7 @@ retry: if (error == -1) return (EFAULT); if (error == 1) { - error = thread_check_susp(td, false); + error = thread_check_susp(curthread, false); if (error != 0) return (error); goto retry; diff --git a/sys/powerpc/linux/linux_sysvec.c b/sys/powerpc/linux/linux_sysvec.c index d263ebe124ff..6f5543ca7740 100644 --- a/sys/powerpc/linux/linux_sysvec.c +++ b/sys/powerpc/linux/linux_sysvec.c @@ -569,6 +569,7 @@ struct sysentvec elf_linux_sysvec = { .sv_trap = NULL, .sv_hwcap = &cpu_features, .sv_hwcap2 = &cpu_features2, + .sv_onexec_old = linux_on_exec_old, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor, @@ -611,6 +612,7 @@ struct sysentvec elf64_linux_sysvec_v2 = { .sv_trap = NULL, .sv_hwcap = &cpu_features, .sv_hwcap2 = &cpu_features2, + .sv_onexec_old = linux_on_exec_old, .sv_onexec = linux_on_exec_vmspace, .sv_onexit = linux_on_exit, .sv_ontdexit = linux_thread_dtor,
