From nobody Sat Dec 9 19:13:00 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 4Snd250tjWz53Dqk; Sat, 9 Dec 2023 19:13:01 +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 4Snd250Hxyz4Tm8; Sat, 9 Dec 2023 19:13:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702149181; 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=THIOIjq24yQxHo2uJAVmRQjmdpPyVtKu9f2Ho0uAS+c=; b=fn+CW4QioObkOmz2cOuV3HEdTRNUQ9zfOyNQuBl0oB4j+kxvyni7/E0gMQKIdBUlQUhxKR IQTW4/1BcvM3Z/DWBfxt/U9E3ozgmvonOeTN6tG9QBAaZjYDc81d6ozb1+qU5TM49RU/Uw DDHLfg3ee/WuCA0ECGg4jIhnnlqOtgCgPWm/AC9FQiOG5s9ngFNFqJJY5TwjKsvKmx8z7R kPDsNkFS66R6jS2AmVHbN9s+LLKk+4PB5DokxDn/PlD0DcJdU4dkMhGyZGtuPzt8bYKTMJ HbbdNgrQwlWSKYRFhX+aa3PlwgR8Hy3lKOLb6ck91LMLcRfc+gBHQeDinHBJBA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702149181; a=rsa-sha256; cv=none; b=ueKG5Ofu8v0CSnK1Eao2K6OQ/6qtOKofPKJseXfTMAVkZVFQAMm/Os+4Vg8E5j/TPu7Fn5 QNltjHy0QseIFiYo/ooeqEwe0/QgscLRKUb7vIb5a6AOT+rg9wRJBz0t28P5WX64gKdYFC Iu7jqLjQV6HAKl97vkIZxuQTmsO72Hoei7f73wEOzXahTTC4HuVtwAVQze1kmO604+Xz6i rKVv2B5THVxz27RltcM1I2sLl8SZ6kZEQ0iNrm+nMwjEj7VjqMCaJ2+8NNg0m2nTkxBxyI 5tH/bbWb4U1Y+JGUKhUlPcI5cDT9kd7NWVZvEsPIzj7GY3VoiVZ8ryZfW/l8zg== 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=1702149181; 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=THIOIjq24yQxHo2uJAVmRQjmdpPyVtKu9f2Ho0uAS+c=; b=G4poROFIuR620ikxRCp1kTT9ErNdexqIEUPVPzIb6jgFkoGj+DblKVDh17bX0eOJIUk9Ss wRlA8bcRGzTKrR4VFniylH25YBFaF/zyGbwyVELiQ3C9RBRnqKTNF0px5H95vnSSXiC9Pw lfwqwVOQPernR3ehw5EWqj0nMdznqpLjzFWrUU2jdcjrLz/z/sxhZLVV0tAy6kow5iHM94 LuthQ34HnLLSwAYBg7VV5s+omUrmEEXxkJHDPRqyYFy8ZRJX2I5hly5ux1qthd4M5uGNof 4K8iU1PoBydFc4OHJC0hMot9RJF3qURleOPguXfOhY8dacxmv4b+cUNGVT9VbA== 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 4Snd246Sw6z2bR; Sat, 9 Dec 2023 19:13:00 +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 3B9JD0iG050812; Sat, 9 Dec 2023 19:13:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3B9JD0fT050809; Sat, 9 Dec 2023 19:13:00 GMT (envelope-from git) Date: Sat, 9 Dec 2023 19:13:00 GMT Message-Id: <202312091913.3B9JD0fT050809@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: ae77041e0714 - main - 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/main X-Git-Reftype: branch X-Git-Commit: ae77041e0714627f9ec8045ca9ee2b6ea563138e Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=ae77041e0714627f9ec8045ca9ee2b6ea563138e commit ae77041e0714627f9ec8045ca9ee2b6ea563138e Author: Mark Johnston AuthorDate: 2023-12-09 15:22:06 +0000 Commit: Mark Johnston CommitDate: 2023-12-09 19:11:33 +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 --- 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 3205ced9b9fd..8a84fd70918d 100644 --- a/sys/kern/kern_kthread.c +++ b/sys/kern/kern_kthread.c @@ -286,6 +286,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, @@ -330,8 +337,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); }