From nobody Mon Apr 22 15:54:27 2024 X-Original-To: dev-commits-src-main@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 4VNVDh261wz5HMqD; Mon, 22 Apr 2024 15:54:28 +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 4VNVDh1bW2z440n; Mon, 22 Apr 2024 15:54:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713801268; 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=mrmXBrAJuGkNE0fpjzjWA9eQZ9MktbCNgIOT1rXdIXU=; b=tI7ukZUW1Ge1hawqxzsMR5hsdDQILR+nxHXK8YmBvHixPbYEYG2wYdjq95AkaQ4p4CEDEc aqdV/sCFuQatxTvqVxS1h7N02aqYarcGiRzof5H35KQHDEi2yzsLKtsSCj8nMIZ05JBwjD k6XofN2Qqloo/z4VYk+hUJVaWw+/9U77qrqgTuExpuBZUYwrEKV07enathCLukynJr3Zg+ cwF5iX8Vr9U/VRfBAdlIv0CpCj80OWnj1+ySDUPwvyEUPDg0wB7XIDnZBFryWi2xCbvTb/ QFgCXfruYatsijfSTaSGMgQlLyBwlV9h6tyZd4zxANnwJssN/t8UITYdkpVCzA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713801268; a=rsa-sha256; cv=none; b=NJERIPc9D/dgNp4M0ERv4bgi3QgbjP9ud4nMRj22wQb1erQVu6Y2nepR0Jq8yyYYEbrUCH P5nnxkKdFEEKkAGdde3mwVxZvYLxDg46Mt9RluZUO2zx7W+zNJzfVhFUwPG9UZb+zsvlMt 9Xjxa3uajUCBGbmhZ7JW9PjxGaqbRkQSG9ezLNiHxXAYVd+KcEaUugzJa2ClDAs+dqfiBQ Ow0QyysZn1/RCKVb2VkUdpkeMruomkODoS6QH9QrMeT9POiNIkSwv6BFbshItpxXMN84ld ripaKihnsBMWMLTbf8IWVb/XzJuPZ5G0EgR+MwvhewQumrV9l0pI4+QxQvQX9g== 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=1713801268; 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=mrmXBrAJuGkNE0fpjzjWA9eQZ9MktbCNgIOT1rXdIXU=; b=yvIfb+tkLP6QTjDmQvORRmOtkVNFfK2d3vXa2u289AZwtA6OHulU0H0eT2j82gNGhNxdJn 0as0HVrr1HYBu4ce0CbbLARcpl/v4TibusgjfUg5Sxeh4ZODIBYpMZfY70LFptyPOu54ba WkXfMHaZBFpedwhMJ/LwmEaf0Oc5dqc4drTHChdAkdTJ5dCfHNLMM+uJeCO3cTO0BRkb4M ISyCn7xr4KTvmEsLYPc9c5lW+v5O2I6H4kv0N2iMqkl1fOQwy95++jNGViLJCz+h04pwpB VsFwu8J6JaQpc2gPheUQzYZHqx48b/CxkbEIv4Df6g4DC5zrLv/6Cpjop5G9WA== 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 4VNVDh0wDZztNh; Mon, 22 Apr 2024 15:54:28 +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 43MFsRPC041221; Mon, 22 Apr 2024 15:54:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43MFsR3n041218; Mon, 22 Apr 2024 15:54:27 GMT (envelope-from git) Date: Mon, 22 Apr 2024 15:54:27 GMT Message-Id: <202404221554.43MFsR3n041218@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: 800da341bc4a - main - thread: Simplify sanitizer integration with thread creation List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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: 800da341bc4a35f4b4d82d104b130825d9a42ffa Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=800da341bc4a35f4b4d82d104b130825d9a42ffa commit 800da341bc4a35f4b4d82d104b130825d9a42ffa Author: Mark Johnston AuthorDate: 2024-04-22 15:43:17 +0000 Commit: Mark Johnston CommitDate: 2024-04-22 15:46:59 +0000 thread: Simplify sanitizer integration with thread creation fork() may allocate a new thread in one of two ways: from UMA, or cached in a freed proc that was just allocated from UMA. In either case, KASAN and KMSAN need to initialize some state; in particular they need to initialize the shadow mapping of the new thread's stack. This is done differently between KASAN and KMSAN, which is confusing. This patch improves things a bit: - Add a new thread_recycle() function, which moves all kernel stack handling out of kern_fork.c, since it doesn't really belong there. - Then, thread_alloc_stack() has only one local caller, so just inline it. - Avoid redundant shadow stack initialization: thread_alloc() initializes the KMSAN shadow stack (via kmsan_thread_alloc()) even through vm_thread_new() already did that. - Add kasan_thread_alloc(), for consistency with kmsan_thread_alloc(). No functional change intended. Reviewed by: khng MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D44891 --- sys/kern/kern_fork.c | 17 +++-------------- sys/kern/kern_thread.c | 20 ++++++++++++-------- sys/kern/subr_asan.c | 10 ++++++++++ sys/sys/asan.h | 4 ++++ sys/sys/proc.h | 2 +- sys/vm/vm_glue.c | 9 ++++----- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index d0044dfc19a0..41f6a76c4fa1 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -40,7 +40,6 @@ #include #include -#include #include #include #include @@ -1019,19 +1018,9 @@ fork1(struct thread *td, struct fork_req *fr) } proc_linkup(newproc, td2); } else { - kmsan_thread_alloc(td2); - if (td2->td_kstack == 0 || td2->td_kstack_pages != pages) { - if (td2->td_kstack != 0) - vm_thread_dispose(td2); - if (!thread_alloc_stack(td2, pages)) { - error = ENOMEM; - goto fail2; - } - } else { - kasan_mark((void *)td2->td_kstack, - ptoa(td2->td_kstack_pages), - ptoa(td2->td_kstack_pages), 0); - } + error = thread_recycle(td2, pages); + if (error != 0) + goto fail2; } if ((flags & RFMEM) == 0) { diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c index 0bcd5c2f2add..725791769ac3 100644 --- a/sys/kern/kern_thread.c +++ b/sys/kern/kern_thread.c @@ -798,6 +798,7 @@ thread_alloc(int pages) } td->td_tid = tid; bzero(&td->td_sa.args, sizeof(td->td_sa.args)); + kasan_thread_alloc(td); kmsan_thread_alloc(td); cpu_thread_alloc(td); EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); @@ -805,15 +806,18 @@ thread_alloc(int pages) } int -thread_alloc_stack(struct thread *td, int pages) +thread_recycle(struct thread *td, int pages) { - - KASSERT(td->td_kstack == 0, - ("thread_alloc_stack called on a thread with kstack")); - if (!vm_thread_new(td, pages)) - return (0); - cpu_thread_alloc(td); - return (1); + if (td->td_kstack == 0 || td->td_kstack_pages != pages) { + if (td->td_kstack != 0) + vm_thread_dispose(td); + if (!vm_thread_new(td, pages)) + return (ENOMEM); + cpu_thread_alloc(td); + } + kasan_thread_alloc(td); + kmsan_thread_alloc(td); + return (0); } /* diff --git a/sys/kern/subr_asan.c b/sys/kern/subr_asan.c index c3664d9cf6c2..75d9e75c531a 100644 --- a/sys/kern/subr_asan.c +++ b/sys/kern/subr_asan.c @@ -39,6 +39,7 @@ __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $"); #include #include #include +#include #include #include @@ -294,6 +295,15 @@ kasan_mark(const void *addr, size_t size, size_t redzsize, uint8_t code) } } +void +kasan_thread_alloc(struct thread *td) +{ + if (td->td_kstack != 0) { + kasan_mark((void *)td->td_kstack, ptoa(td->td_kstack_pages), + ptoa(td->td_kstack_pages), 0); + } +} + /* -------------------------------------------------------------------------- */ #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) \ diff --git a/sys/sys/asan.h b/sys/sys/asan.h index a3becdef5f57..6a01d0531725 100644 --- a/sys/sys/asan.h +++ b/sys/sys/asan.h @@ -53,14 +53,18 @@ #define KASAN_KSTACK_FREED 0xFE #define KASAN_EXEC_ARGS_FREED 0xFF +struct thread; + void kasan_init(void); void kasan_init_early(vm_offset_t, size_t); void kasan_shadow_map(vm_offset_t, size_t); void kasan_mark(const void *, size_t, size_t, uint8_t); +void kasan_thread_alloc(struct thread *); #else /* KASAN */ #define kasan_init() #define kasan_shadow_map(a, s) #define kasan_mark(p, s, l, c) +#define kasan_thread_alloc(t) #endif /* !KASAN */ #endif /* !_SYS_ASAN_H_ */ diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 33b836f4150e..1b542d1374b4 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1262,7 +1262,6 @@ void cpu_thread_free(struct thread *); void cpu_thread_swapin(struct thread *); void cpu_thread_swapout(struct thread *); struct thread *thread_alloc(int pages); -int thread_alloc_stack(struct thread *, int pages); int thread_check_susp(struct thread *td, bool sleep); void thread_cow_get_proc(struct thread *newtd, struct proc *p); void thread_cow_get(struct thread *newtd, struct thread *td); @@ -1275,6 +1274,7 @@ void thread_exit(void) __dead2; void thread_free(struct thread *td); void thread_link(struct thread *td, struct proc *p); void thread_reap_barrier(void); +int thread_recycle(struct thread *, int pages); int thread_single(struct proc *p, int how); void thread_single_end(struct proc *p, int how); void thread_stash(struct thread *td); diff --git a/sys/vm/vm_glue.c b/sys/vm/vm_glue.c index 4292a7533503..f9235fe03fab 100644 --- a/sys/vm/vm_glue.c +++ b/sys/vm/vm_glue.c @@ -543,8 +543,6 @@ vm_thread_new(struct thread *td, int pages) td->td_kstack = ks; td->td_kstack_pages = pages; td->td_kstack_domain = ks_domain; - kasan_mark((void *)ks, ptoa(pages), ptoa(pages), 0); - kmsan_mark((void *)ks, ptoa(pages), KMSAN_STATE_UNINIT); return (1); } @@ -562,11 +560,12 @@ vm_thread_dispose(struct thread *td) td->td_kstack = 0; td->td_kstack_pages = 0; td->td_kstack_domain = MAXMEMDOM; - kasan_mark((void *)ks, 0, ptoa(pages), KASAN_KSTACK_FREED); - if (pages == kstack_pages) + if (pages == kstack_pages) { + kasan_mark((void *)ks, 0, ptoa(pages), KASAN_KSTACK_FREED); uma_zfree(kstack_cache, (void *)ks); - else + } else { vm_thread_stack_dispose(ks, pages); + } } /*