From nobody Mon Dec 18 02:20:21 2023 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 4Stk7T4SDDz54f0x; Mon, 18 Dec 2023 02:20:21 +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 4Stk7T41kRz3Y4v; Mon, 18 Dec 2023 02:20:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702866021; 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=XqFQMsczplXI+UJaets/dXcwN5rfBNW6Coh/wNUUYAQ=; b=YvnooAVNxVvS0qI74zPEc1LyXTvcmkHkwPN1Tpz5Ah3FBf0ivhw5tER6xozCia+X1MrIFr 5qzMBCYE4BQeXJjiP6LJ5ypdf/slD/zND2AzcK5Lic4pwdSbTD/zWjv1fbboXu13zI81tG HdCefB4/NrUKnAUp2/yrZnw2JyehCMR9Ph9yKejSi7fQOEpjCkPRnTARMUOxHp9esyNKmF F1SSok7dYRTgOZHQ8V3D5CZUkBbWpxNKOUvILeTU9t7aFZK0TqFcas42xdfU/xUpwcUm0e yjI1We7ms63otB61F5GCSqMhd52Zf2aO4C2JfVkdytDgtJPz7mkg+CaGknUrbw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702866021; a=rsa-sha256; cv=none; b=lomD0ZlUq6tGIp7spZ53bjZPVcDjWQ+1mFg6Pi0NMh+Q5Rh0W4JTdSFSWIyJdhVVcJMweB Zx4MwmSSBEt8llqtiBV3gQVJadss9dcaN4TAmzlm2YB7/t5UYGo4BfBRpaaYo8EjgW2c8G GjVeO3UzNkXQQ+fEox+VzPwj26qr6SgyuBfPKvyvIUmYx0oDmqSoCMpYKKWbNQIihRobP8 fSEL1AOq98MUQMvRlujLgw3K18z58Choiptby0BDn/26kwrW/9cg3TViC2szro0V/iEmxx aQ/SKl9z6z2ceT6uM+CWv7FZfJABUkhHU5BqGUCGY69RVZ4zcE2I0JGWnnznyA== 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=1702866021; 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=XqFQMsczplXI+UJaets/dXcwN5rfBNW6Coh/wNUUYAQ=; b=c9ugLii2vgjEGfpy5fWoUJOPY56HyIPsuXj/0lkul5lelHXLq6IQRAJvYvjrDN/0KEK8Sa mCQkFWLjGP6lZwON1phkyNQaiTvJBRJrJZwz7tOURUBXwFhN+NfB8+yJ4oqf7v6+ehgHZ7 fGK9VygG9sK3erGy/lmaB0EA/xZdBSKYAi+FoHSlzfLZ15Vz7Zhcg4iRiM6mibCSGszJy1 OnNH+fnwo/rr3ovglzTkWHOmffdFHwZh43VuFdgjYG4eafB5J8i7U/ymdw9m8ldGHD6eIA jRTVHUnTRJjE4c0Hn0R0spNtrQl3ZkZKNRRdmX2BMDm9wyLHIyGk72AO4k0Qog== 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 4Stk7T346Gz12w4; Mon, 18 Dec 2023 02:20:21 +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 3BI2KLRH089325; Mon, 18 Dec 2023 02:20:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BI2KLAh089322; Mon, 18 Dec 2023 02:20:21 GMT (envelope-from git) Date: Mon, 18 Dec 2023 02:20:21 GMT Message-Id: <202312180220.3BI2KLAh089322@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: a9184e99afe8 - stable/14 - kthread: Set *newtdp earlier in kthread_add1() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/14 X-Git-Reftype: branch X-Git-Commit: a9184e99afe8e8ca7945fc1618d9b0c65bbdc6de Auto-Submitted: auto-generated The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=a9184e99afe8e8ca7945fc1618d9b0c65bbdc6de commit a9184e99afe8e8ca7945fc1618d9b0c65bbdc6de Author: Mark Johnston AuthorDate: 2023-12-09 15:22:06 +0000 Commit: Mark Johnston CommitDate: 2023-12-18 02:20:13 +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); }