git: 37cef00192ef - main - livedump: Silence KASAN and KMSAN when livedumping

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 25 Nov 2024 21:13:00 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=37cef00192ef1347d27ac64be419267a4ba604ac

commit 37cef00192ef1347d27ac64be419267a4ba604ac
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-11-25 21:11:37 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-11-25 21:12:51 +0000

    livedump: Silence KASAN and KMSAN when livedumping
    
    The livedumper triggers reports from both of these sanitizers since it
    necessarily accesses uninitialized or freed memory.  Add a flag to
    silence reports from both sanitizers.
    
    Reviewed by:    mhorne, khng
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D47714
---
 sys/kern/kern_vnodedumper.c | 2 ++
 sys/kern/subr_asan.c        | 3 +++
 sys/kern/subr_msan.c        | 6 ++++++
 sys/sys/proc.h              | 1 +
 4 files changed, 12 insertions(+)

diff --git a/sys/kern/kern_vnodedumper.c b/sys/kern/kern_vnodedumper.c
index 5a2c255390cc..c89e209cc1a5 100644
--- a/sys/kern/kern_vnodedumper.c
+++ b/sys/kern/kern_vnodedumper.c
@@ -133,8 +133,10 @@ livedump_start_vnode(struct vnode *vp, int flags, uint8_t compression)
 	if (error != 0)
 		goto out;
 
+	curthread->td_pflags2 |= TDP2_SAN_QUIET;
 	dump_savectx();
 	error = minidumpsys(livedi, true);
+	curthread->td_pflags2 &= ~TDP2_SAN_QUIET;
 
 	EVENTHANDLER_INVOKE(livedumper_finish);
 out:
diff --git a/sys/kern/subr_asan.c b/sys/kern/subr_asan.c
index 61466ffbf2ab..9f8a7bfccd40 100644
--- a/sys/kern/subr_asan.c
+++ b/sys/kern/subr_asan.c
@@ -405,6 +405,9 @@ kasan_shadow_check(unsigned long addr, size_t size, bool write,
 
 	if (__predict_false(!kasan_enabled))
 		return;
+	if (__predict_false(curthread != NULL &&
+	    (curthread->td_pflags2 & TDP2_SAN_QUIET) != 0))
+		return;
 	if (__predict_false(size == 0))
 		return;
 	if (__predict_false(kasan_md_unsupported(addr)))
diff --git a/sys/kern/subr_msan.c b/sys/kern/subr_msan.c
index 52ec5ce13bd1..1c18a4a4f8e5 100644
--- a/sys/kern/subr_msan.c
+++ b/sys/kern/subr_msan.c
@@ -179,6 +179,9 @@ kmsan_report_hook(const void *addr, msan_orig_t *orig, size_t size, size_t off,
 
 	if (__predict_false(KERNEL_PANICKED() || kdb_active || kmsan_reporting))
 		return;
+	if (__predict_false(curthread != NULL &&
+	    (curthread->td_pflags2 & TDP2_SAN_QUIET) != 0))
+		return;
 
 	kmsan_reporting = true;
 	__compiler_membar();
@@ -232,6 +235,9 @@ kmsan_report_inline(msan_orig_t orig, unsigned long pc)
 
 	if (__predict_false(KERNEL_PANICKED() || kdb_active || kmsan_reporting))
 		return;
+	if (__predict_false(curthread != NULL &&
+	    (curthread->td_pflags2 & TDP2_SAN_QUIET) != 0))
+		return;
 
 	kmsan_reporting = true;
 	__compiler_membar();
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 69b6c7a61da0..c5ef4f849eab 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -567,6 +567,7 @@ enum {
 #define	TDP2_SBPAGES	0x00000001 /* Owns sbusy on some pages */
 #define	TDP2_COMPAT32RB	0x00000002 /* compat32 ABI for robust lists */
 #define	TDP2_ACCT	0x00000004 /* Doing accounting */
+#define	TDP2_SAN_QUIET	0x00000008 /* Disable warnings from K(A|M)SAN */
 
 /*
  * Reasons that the current thread can not be run yet.