From nobody Thu Dec 28 03:11:15 2023 X-Original-To: dev-commits-src-branches@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 4T0tnb3ND5z552hR; Thu, 28 Dec 2023 03:11:15 +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 4T0tnb2SNyz3bJ9; Thu, 28 Dec 2023 03:11:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703733075; 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=tvRUOHBRxvLcyE7xmf9BR3lpUlFqR0NABSNS2t41jzg=; b=ZhDTN+DDUS/wzhFpYKR/cA+pZoCgaaMJ5rO898EtNc6dm9Ty1F7hCI6/KpIkU8mEsf9lpi lN5SSF9dOZCQXAbxe8SFzWDP4s10EFPe+JLjv6sc87eSFkA1NM86W8lxaQqCbIPJ9sAMqI 7JKzQGpwzvuKsy/L0OF9G5+cUC2bhUDsjSrxvYvA0MBA+pwrTgQIUV2OuA9DeFbyUSeAhT 1mVChpON/hsDNpoqZHOi69pui5mHI/8WxGzI2n7MJ3n3NsKp8VQIoBO4m+KiYSCHG3mhRj HqgfryVNXUZ3XFx2xeO683CWbEaG+xsLw+2vZTaiQsKM2G0MuB11WPqfuD7noQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703733075; a=rsa-sha256; cv=none; b=D9H+BUPDv8IKm6JqkpGKyOGeXmVEGC0xlo8vqLFysrTXMpUtdJskSeJSWiC3/5Ta/wQVdf Ujs2w79Q5KeSl9K0VAJCS2QlcLOk9tcOjM63Eb3jloHN5/thZPp9iNCzFgPs2p2SW3zeDN eBnGwnEWP8V/060nVPsjMIPL0WaZVbmYCzFt7M6a2BfwyODgf28tJ6T6qey8hA8kJaftmF FBefHB/p0R1yc/4UstCwfV9k1yNOhnLkI6CaQtUG+wltji7O86h9nEqUVKlwEjRgMCaERB gPfDVxKUh2AHy0Ui1OS3R3Zx3+Q8uXElSdUiRT4XmWaux2X1CdSNE8k1GhGIkw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703733075; 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=tvRUOHBRxvLcyE7xmf9BR3lpUlFqR0NABSNS2t41jzg=; b=E6dkv8og4v4isXXSJ1NIx+E9k/0MeaiB/wRnMsfohCnXTu42KxGS/rHsYeFb7fcL/iN+7W MpFoAoW4l/PejBTb2uopnm3Wc2HaT61tJNIKFcQcp4AXBEcF6zSONwE8YltA9lWk8x9xt/ lb26q6pEb8SnKqCves0FaUnCBZHFCsHLXjkGxg0zHf2rI2V28zkWxGCM//TLRrOCwOdIQQ oKjbdkXhTqFYu2ybp30wpCitNv4DXmzC1pxYmm4G0lgpVZfyPbw4yt8y3+XADyT1DR2YI2 qGbdPgEJo00QxLSuJ1r08RyEjrxqtaM7wy9Mbe12gOLeocjzMElbaETiKo4tjQ== 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 4T0tnb1Y9Zz14sJ; Thu, 28 Dec 2023 03:11:15 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BS3BF0M053100; Thu, 28 Dec 2023 03:11:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BS3BFhu053097; Thu, 28 Dec 2023 03:11:15 GMT (envelope-from git) Date: Thu, 28 Dec 2023 03:11:15 GMT Message-Id: <202312280311.3BS3BFhu053097@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: d2c1d1b2bc47 - stable/13 - kthread: Set *newtdp earlier in kthread_add1() List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org 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: d2c1d1b2bc47e451a4cc77ca1a2b0d1648343110 Auto-Submitted: auto-generated The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=d2c1d1b2bc47e451a4cc77ca1a2b0d1648343110 commit d2c1d1b2bc47e451a4cc77ca1a2b0d1648343110 Author: Mark Johnston AuthorDate: 2023-12-09 15:22:06 +0000 Commit: Mark Johnston CommitDate: 2023-12-28 03:01:26 +0000 kthread: Set *newtdp earlier in kthread_add1() syzbot reported a single boot-time crash in g_event_procbody(), a page fault when dereferencing g_event_td. g_event_td is initialized by the kproc_kthread_add() call which creates the GEOM event thread: kproc_kthread_add(g_event_procbody, NULL, &g_proc, &g_event_td, RFHIGHPID, 0, "geom", "g_event"); I believe that the caller of kproc_kthread_add() was preempted after adding the new thread to the scheduler, and before setting *newtdp, which is equal to g_event_td. Thus, since the first action of the GEOM event thread is to lock itself, it ended up dereferencing a NULL pointer. Fix the problem simply by initializing *newtdp earlier. I see no harm in that, and it matches kproc_create1(). The scheduler provides sufficient synchronization to ensure that the store is visible to the new thread, wherever it happens to run. Reported by: syzbot+5397f4d39219b85a9409@syzkaller.appspotmail.com Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D42986 (cherry picked from commit ae77041e0714627f9ec8045ca9ee2b6ea563138e) --- sys/kern/kern_kthread.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_kthread.c b/sys/kern/kern_kthread.c index 6f7fd8b3d555..9cbc74658432 100644 --- a/sys/kern/kern_kthread.c +++ b/sys/kern/kern_kthread.c @@ -287,6 +287,13 @@ kthread_add1(void (*func)(void *), void *arg, struct proc *p, } oldtd = FIRST_THREAD_IN_PROC(p); + /* + * Set the new thread pointer before the thread starts running: *newtdp + * could be a pointer that is referenced by "func". + */ + if (newtdp != NULL) + *newtdp = newtd; + bzero(&newtd->td_startzero, __rangeof(struct thread, td_startzero, td_endzero)); bcopy(&oldtd->td_startcopy, &newtd->td_startcopy, @@ -331,8 +338,6 @@ kthread_add1(void (*func)(void *), void *arg, struct proc *p, thread_lock(newtd); sched_add(newtd, SRQ_BORING); } - if (newtdp) - *newtdp = newtd; return (0); }