git: 3388bf06d767 - stable/13 - Generalize sanitizer interceptors for memory and string routines

From: Mark Johnston <>
Date: Mon, 01 Nov 2021 14:33:19 UTC
The branch stable/13 has been updated by markj:


commit 3388bf06d767faf3390a77a96c3365926f05c201
Author:     Mark Johnston <>
AuthorDate: 2021-03-24 23:43:05 +0000
Commit:     Mark Johnston <>
CommitDate: 2021-11-01 14:20:50 +0000

    Generalize sanitizer interceptors for memory and string routines
    Similar to commit 3ead60236f ("Generalize bus_space(9) and atomic(9)
    sanitizer interceptors"), use a more generic scheme for interposing
    sanitizer implementations of routines like memcpy().
    No functional change intended.
    Sponsored by:   The FreeBSD Foundation
    (cherry picked from commit ec8f1ea8d536e91ad37e03e45a688c4e255b9cb0)
 sys/kern/subr_csan.c |  6 ------
 sys/sys/libkern.h    | 18 ++++++++++--------
 sys/sys/systm.h      | 54 ++++++++++++++++++++++++++++++++--------------------
 3 files changed, 43 insertions(+), 35 deletions(-)

diff --git a/sys/kern/subr_csan.c b/sys/kern/subr_csan.c
index 06b0b6ebb020..56d2e59ff12c 100644
--- a/sys/kern/subr_csan.c
+++ b/sys/kern/subr_csan.c
@@ -350,12 +350,6 @@ kcsan_strlen(const char *str)
 	return (s - str);
-#undef copyin
-#undef copyin_nofault
-#undef copyinstr
-#undef copyout
-#undef copyout_nofault
 kcsan_copyin(const void *uaddr, void *kaddr, size_t len)
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index 8517c0dab1f6..d8d3dce1b705 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -193,18 +193,20 @@ size_t	 strspn(const char *, const char *);
 char	*strstr(const char *, const char *);
 int	 strvalid(const char *, size_t);
-#ifdef KCSAN
-char	*kcsan_strcpy(char *, const char *);
-int	kcsan_strcmp(const char *, const char *);
-size_t	kcsan_strlen(const char *);
-#define	strcpy(d, s) kcsan_strcpy((d), (s))
-#define	strcmp(s1, s2) kcsan_strcmp((s1), (s2))
-#define	strlen(s) kcsan_strlen((s))
+#ifdef SAN_PREFIX
+char	*SAN_INTERCEPTOR(strcpy)(char *, const char *);
+int	SAN_INTERCEPTOR(strcmp)(const char *, const char *);
+size_t	SAN_INTERCEPTOR(strlen)(const char *);
+#ifndef SAN_RUNTIME
+#define	strcpy(d, s)	SAN_INTERCEPTOR(strcpy)((d), (s))
+#define	strcmp(s1, s2)	SAN_INTERCEPTOR(strcmp)((s1), (s2))
+#define	strlen(s)	SAN_INTERCEPTOR(strlen)(s)
+#endif /* !SAN_RUNTIME */
 #define strcpy(d, s) __builtin_strcpy((d), (s))
 #define strcmp(s1, s2) __builtin_strcmp((s1), (s2))
 #define strlen(s) __builtin_strlen((s))
+#endif /* SAN_PREFIX */
 static __inline char *
 index(const char *p, int ch)
diff --git a/sys/sys/systm.h b/sys/sys/systm.h
index 36d48fbf080d..8080f22266e2 100644
--- a/sys/sys/systm.h
+++ b/sys/sys/systm.h
@@ -351,18 +351,28 @@ void	*memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len);
 void	*memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n);
 int	memcmp(const void *b1, const void *b2, size_t len);
-#ifdef KCSAN
-void	*kcsan_memset(void *, int, size_t);
-void	*kcsan_memcpy(void *, const void *, size_t);
-void	*kcsan_memmove(void *, const void *, size_t);
-int	kcsan_memcmp(const void *, const void *, size_t);
-#define bcopy(from, to, len) kcsan_memmove((to), (from), (len))
-#define bzero(buf, len) kcsan_memset((buf), 0, (len))
-#define bcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
-#define memset(buf, c, len) kcsan_memset((buf), (c), (len))
-#define memcpy(to, from, len) kcsan_memcpy((to), (from), (len))
-#define memmove(dest, src, n) kcsan_memmove((dest), (src), (n))
-#define memcmp(b1, b2, len) kcsan_memcmp((b1), (b2), (len))
+#if defined(KASAN)
+#define	SAN_PREFIX	kasan_
+#elif defined(KCSAN)
+#define	SAN_PREFIX	kcsan_
+#ifdef SAN_PREFIX
+void	*SAN_INTERCEPTOR(memset)(void *, int, size_t);
+void	*SAN_INTERCEPTOR(memcpy)(void *, const void *, size_t);
+void	*SAN_INTERCEPTOR(memmove)(void *, const void *, size_t);
+int	SAN_INTERCEPTOR(memcmp)(const void *, const void *, size_t);
+#ifndef SAN_RUNTIME
+#define bcopy(from, to, len)	SAN_INTERCEPTOR(memmove)((to), (from), (len))
+#define bzero(buf, len)		SAN_INTERCEPTOR(memset)((buf), 0, (len))
+#define bcmp(b1, b2, len)	SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
+#define memset(buf, c, len)	SAN_INTERCEPTOR(memset)((buf), (c), (len))
+#define memcpy(to, from, len)	SAN_INTERCEPTOR(memcpy)((to), (from), (len))
+#define memmove(dest, src, n)	SAN_INTERCEPTOR(memmove)((dest), (src), (n))
+#define memcmp(b1, b2, len)	SAN_INTERCEPTOR(memcmp)((b1), (b2), (len))
+#endif /* !SAN_RUNTIME */
 #define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
 #define bzero(buf, len) __builtin_memset((buf), 0, (len))
@@ -371,7 +381,7 @@ int	kcsan_memcmp(const void *, const void *, size_t);
 #define memcpy(to, from, len) __builtin_memcpy((to), (from), (len))
 #define memmove(dest, src, n) __builtin_memmove((dest), (src), (n))
 #define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
+#endif /* !SAN_PREFIX */
 void	*memset_early(void * _Nonnull buf, int c, size_t len);
 #define bzero_early(buf, len) memset_early((buf), 0, (len))
@@ -402,14 +412,16 @@ int	copyout(const void * _Nonnull __restrict kaddr,
 int	copyout_nofault(const void * _Nonnull __restrict kaddr,
 	    void * __restrict udaddr, size_t len);
-#ifdef KCSAN
-int	kcsan_copyin(const void *, void *, size_t);
-int	kcsan_copyinstr(const void *, void *, size_t, size_t *);
-int	kcsan_copyout(const void *, void *, size_t);
-#define	copyin(u, k, l) kcsan_copyin((u), (k), (l))
-#define	copyinstr(u, k, l, lc) kcsan_copyinstr((u), (k), (l), (lc))
-#define	copyout(k, u, l) kcsan_copyout((k), (u), (l))
+#ifdef SAN_PREFIX
+int	SAN_INTERCEPTOR(copyin)(const void *, void *, size_t);
+int	SAN_INTERCEPTOR(copyinstr)(const void *, void *, size_t, size_t *);
+int	SAN_INTERCEPTOR(copyout)(const void *, void *, size_t);
+#ifndef SAN_RUNTIME
+#define	copyin(u, k, l)		SAN_INTERCEPTOR(copyin)((u), (k), (l))
+#define	copyinstr(u, k, l, lc)	SAN_INTERCEPTOR(copyinstr)((u), (k), (l), (lc))
+#define	copyout(k, u, l)	SAN_INTERCEPTOR(copyout)((k), (u), (l))
+#endif /* !SAN_RUNTIME */
+#endif /* SAN_PREFIX */
 int	fubyte(volatile const void *base);
 long	fuword(volatile const void *base);