From owner-dev-commits-src-branches@freebsd.org Thu May 20 14:10:43 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id F03DC63F3FD; Thu, 20 May 2021 14:10:42 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FmBTt0Zgnz3LrX; Thu, 20 May 2021 14:10:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5EE7326F74; Thu, 20 May 2021 14:10:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 14KEAf1M039030; Thu, 20 May 2021 14:10:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 14KEAfYF039029; Thu, 20 May 2021 14:10:41 GMT (envelope-from git) Date: Thu, 20 May 2021 14:10:41 GMT Message-Id: <202105201410.14KEAfYF039029@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: cf60931c32d7 - stable/13 - fork: Suspend other threads if both RFPROC and RFMEM are not set MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cf60931c32d7c5401c69ff0081e049a100673444 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 May 2021 14:10:43 -0000 The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=cf60931c32d7c5401c69ff0081e049a100673444 commit cf60931c32d7c5401c69ff0081e049a100673444 Author: Mark Johnston AuthorDate: 2021-05-13 12:33:23 +0000 Commit: Mark Johnston CommitDate: 2021-05-20 13:16:47 +0000 fork: Suspend other threads if both RFPROC and RFMEM are not set Otherwise, a multithreaded parent process may trigger races in vm_forkproc() if one thread calls rfork() with RFMEM set and another calls rfork() without RFMEM. Also simplify vm_forkproc() a bit, vmspace_unshare() already checks to see if the address space is shared. Reported by: syzbot+0aa7c2bec74c4066c36f@syzkaller.appspotmail.com Reported by: syzbot+ea84cb06937afeae609d@syzkaller.appspotmail.com Reviewed by: kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D30220 (cherry picked from commit 9246b3090cbc82c54350391601b9acef2aa9a625) --- sys/kern/kern_fork.c | 13 +++++++++---- sys/vm/vm_glue.c | 8 +++----- sys/vm/vm_map.c | 4 ++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 2a092b192878..0d0659b432fe 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -313,8 +313,13 @@ fork_norfproc(struct thread *td, int flags) ("fork_norfproc called with RFPROC set")); p1 = td->td_proc; - if (((p1->p_flag & (P_HADTHREADS|P_SYSTEM)) == P_HADTHREADS) && - (flags & (RFCFDG | RFFDG))) { + /* + * Quiesce other threads if necessary. If RFMEM is not specified we + * must ensure that other threads do not concurrently create a second + * process sharing the vmspace, see vmspace_unshare(). + */ + if ((p1->p_flag & (P_HADTHREADS | P_SYSTEM)) == P_HADTHREADS && + ((flags & (RFCFDG | RFFDG)) != 0 || (flags & RFMEM) == 0)) { PROC_LOCK(p1); if (thread_single(p1, SINGLE_BOUNDARY)) { PROC_UNLOCK(p1); @@ -350,8 +355,8 @@ fork_norfproc(struct thread *td, int flags) } fail: - if (((p1->p_flag & (P_HADTHREADS|P_SYSTEM)) == P_HADTHREADS) && - (flags & (RFCFDG | RFFDG))) { + if ((p1->p_flag & (P_HADTHREADS | P_SYSTEM)) == P_HADTHREADS && + ((flags & (RFCFDG | RFFDG)) != 0 || (flags & RFMEM) == 0)) { PROC_LOCK(p1); thread_single_end(p1, SINGLE_BOUNDARY); PROC_UNLOCK(p1); diff --git a/sys/vm/vm_glue.c b/sys/vm/vm_glue.c index a2500828eae4..be741fd40199 100644 --- a/sys/vm/vm_glue.c +++ b/sys/vm/vm_glue.c @@ -550,11 +550,9 @@ vm_forkproc(struct thread *td, struct proc *p2, struct thread *td2, * COW locally. */ if ((flags & RFMEM) == 0) { - if (refcount_load(&p1->p_vmspace->vm_refcnt) > 1) { - error = vmspace_unshare(p1); - if (error) - return (error); - } + error = vmspace_unshare(p1); + if (error) + return (error); } cpu_fork(td, p2, td2, flags); return (0); diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c index b3288fce5114..f17342b2a545 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -4896,6 +4896,10 @@ vmspace_unshare(struct proc *p) struct vmspace *newvmspace; vm_ooffset_t fork_charge; + /* + * The caller is responsible for ensuring that the reference count + * cannot concurrently transition 1 -> 2. + */ if (refcount_load(&oldvmspace->vm_refcnt) == 1) return (0); fork_charge = 0;