aboutsummaryrefslogtreecommitdiff
path: root/sys/sys
diff options
context:
space:
mode:
authorMark Johnston <markj@FreeBSD.org>2021-07-19 20:09:42 +0000
committerMark Johnston <markj@FreeBSD.org>2021-07-30 01:13:32 +0000
commita90d053b84223a4e5cb65852a9b6193570ab1c7d (patch)
treec5df8f2fecf5adc97c33c7d93a9e168c85d0a940 /sys/sys
parent419d406e4ee068644218fb881bc80f79f8191970 (diff)
downloadsrc-a90d053b84223a4e5cb65852a9b6193570ab1c7d.tar.gz
src-a90d053b84223a4e5cb65852a9b6193570ab1c7d.zip
Simplify kernel sanitizer interceptors
KASAN and KCSAN implement interceptors for various primitive operations that are not instrumented by the compiler. KMSAN requires them as well. Rather than adding new cases for each sanitizer which requires interceptors, implement the following protocol: - When interceptor definitions are required, define SAN_NEEDS_INTERCEPTORS and SANITIZER_INTERCEPTOR_PREFIX. - In headers that declare functions which need to be intercepted by a sanitizer runtime, use SANITIZER_INTERCEPTOR_PREFIX to provide declarations. - When SAN_RUNTIME is defined, do not redefine the names of intercepted functions. This is typically the case in files which implement sanitizer runtimes but is also needed in, for example, files which define ifunc selectors for intercepted operations. MFC after: 2 weeks Sponsored by: The FreeBSD Foundation
Diffstat (limited to 'sys/sys')
-rw-r--r--sys/sys/atomic_san.h10
-rw-r--r--sys/sys/bus_san.h11
-rw-r--r--sys/sys/libkern.h16
-rw-r--r--sys/sys/systm.h34
4 files changed, 31 insertions, 40 deletions
diff --git a/sys/sys/atomic_san.h b/sys/sys/atomic_san.h
index 5d10f58f7565..30bd01c97cf2 100644
--- a/sys/sys/atomic_san.h
+++ b/sys/sys/atomic_san.h
@@ -43,10 +43,6 @@
#error do not include this header, use machine/atomic.h
#endif
-#ifndef ATOMIC_SAN_PREFIX
-#error No sanitizer prefix available
-#endif
-
#define ATOMIC_SAN_FUNC_1(sp, op, name, type) \
void sp##_atomic_##op##_##name(volatile type *, type); \
void sp##_atomic_##op##_acq_##name(volatile type *, type); \
@@ -104,7 +100,7 @@
ATOMIC_SAN_THREAD_FENCE(sp);
#define ATOMIC_SAN_FUNCS(name, type) \
- _ATOMIC_SAN_FUNCS(ATOMIC_SAN_PREFIX, name, type)
+ _ATOMIC_SAN_FUNCS(SAN_INTERCEPTOR_PREFIX, name, type)
ATOMIC_SAN_FUNCS(char, uint8_t);
ATOMIC_SAN_FUNCS(short, uint16_t);
@@ -123,8 +119,8 @@ ATOMIC_SAN_FUNCS(64, uint64_t);
* For instance, KASAN callers of atomic_add_char() will be redirected to
* kasan_atomic_add_char().
*/
-#define ATOMIC_SAN(func) \
- __CONCAT(ATOMIC_SAN_PREFIX, __CONCAT(_atomic_, func))
+#define ATOMIC_SAN(func) \
+ __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_atomic_, func))
#define atomic_add_char ATOMIC_SAN(add_char)
#define atomic_add_acq_char ATOMIC_SAN(add_acq_char)
diff --git a/sys/sys/bus_san.h b/sys/sys/bus_san.h
index 05d5ecd4b844..fd3c4c803675 100644
--- a/sys/sys/bus_san.h
+++ b/sys/sys/bus_san.h
@@ -43,10 +43,6 @@
#error do not include this header, use machine/bus.h
#endif
-#ifndef BUS_SAN_PREFIX
-#error No sanitizer prefix defined
-#endif
-
#define BUS_SAN_MULTI(sp, rw, width, type) \
void sp##_bus_space_##rw##_multi_##width(bus_space_tag_t, \
bus_space_handle_t, bus_size_t, type *, bus_size_t); \
@@ -122,7 +118,7 @@
BUS_SAN_MISC(sp);
#define BUS_SAN_FUNCS(width, type) \
- _BUS_SAN_FUNCS(BUS_SAN_PREFIX, width, type)
+ _BUS_SAN_FUNCS(SAN_INTERCEPTOR_PREFIX, width, type)
BUS_SAN_FUNCS(1, uint8_t);
BUS_SAN_FUNCS(2, uint16_t);
@@ -131,7 +127,8 @@ BUS_SAN_FUNCS(8, uint64_t);
#ifndef SAN_RUNTIME
-#define BUS_SAN(func) __CONCAT(BUS_SAN_PREFIX, __CONCAT(_bus_space_, func))
+#define BUS_SAN(func) \
+ __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_bus_space_, func))
#define bus_space_map BUS_SAN(map)
#define bus_space_unmap BUS_SAN(unmap)
@@ -224,6 +221,6 @@ BUS_SAN_FUNCS(8, uint64_t);
#define bus_space_poke_8 BUS_SAN(poke_8)
#define bus_space_peek_8 BUS_SAN(peek_8)
-#endif /* !KCSAN_RUNTIME */
+#endif /* !SAN_RUNTIME */
#endif /* !_SYS_BUS_SAN_H_ */
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index d8d3dce1b705..147eba9bd1bd 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -193,7 +193,11 @@ size_t strspn(const char *, const char *);
char *strstr(const char *, const char *);
int strvalid(const char *, size_t);
-#ifdef SAN_PREFIX
+#ifdef SAN_NEEDS_INTERCEPTORS
+#ifndef SAN_INTERCEPTOR
+#define SAN_INTERCEPTOR(func) \
+ __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
+#endif
char *SAN_INTERCEPTOR(strcpy)(char *, const char *);
int SAN_INTERCEPTOR(strcmp)(const char *, const char *);
size_t SAN_INTERCEPTOR(strlen)(const char *);
@@ -202,11 +206,11 @@ size_t SAN_INTERCEPTOR(strlen)(const char *);
#define strcmp(s1, s2) SAN_INTERCEPTOR(strcmp)((s1), (s2))
#define strlen(s) SAN_INTERCEPTOR(strlen)(s)
#endif /* !SAN_RUNTIME */
-#else
-#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 */
+#else /* !SAN_NEEDS_INTERCEPTORS */
+#define strcpy(d, s) __builtin_strcpy((d), (s))
+#define strcmp(s1, s2) __builtin_strcmp((s1), (s2))
+#define strlen(s) __builtin_strlen((s))
+#endif /* SAN_NEEDS_INTERCEPTORS */
static __inline char *
index(const char *p, int ch)
diff --git a/sys/sys/systm.h b/sys/sys/systm.h
index 8080f22266e2..671b5c2b8d38 100644
--- a/sys/sys/systm.h
+++ b/sys/sys/systm.h
@@ -351,15 +351,9 @@ 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);
-#if defined(KASAN)
-#define SAN_PREFIX kasan_
-#elif defined(KCSAN)
-#define SAN_PREFIX kcsan_
-#endif
-
-#ifdef SAN_PREFIX
-#define SAN_INTERCEPTOR(func) __CONCAT(SAN_PREFIX, func)
-
+#ifdef SAN_NEEDS_INTERCEPTORS
+#define SAN_INTERCEPTOR(func) \
+ __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
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);
@@ -373,15 +367,15 @@ int SAN_INTERCEPTOR(memcmp)(const void *, const void *, size_t);
#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 */
-#else
-#define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
-#define bzero(buf, len) __builtin_memset((buf), 0, (len))
-#define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
-#define memset(buf, c, len) __builtin_memset((buf), (c), (len))
-#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 */
+#else /* !SAN_NEEDS_INTERCEPTORS */
+#define bcopy(from, to, len) __builtin_memmove((to), (from), (len))
+#define bzero(buf, len) __builtin_memset((buf), 0, (len))
+#define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len))
+#define memset(buf, c, len) __builtin_memset((buf), (c), (len))
+#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_NEEDS_INTERCEPTORS */
void *memset_early(void * _Nonnull buf, int c, size_t len);
#define bzero_early(buf, len) memset_early((buf), 0, (len))
@@ -412,7 +406,7 @@ int copyout(const void * _Nonnull __restrict kaddr,
int copyout_nofault(const void * _Nonnull __restrict kaddr,
void * __restrict udaddr, size_t len);
-#ifdef SAN_PREFIX
+#ifdef SAN_NEEDS_INTERCEPTORS
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);
@@ -421,7 +415,7 @@ int SAN_INTERCEPTOR(copyout)(const void *, void *, size_t);
#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 */
+#endif /* SAN_NEEDS_INTERCEPTORS */
int fubyte(volatile const void *base);
long fuword(volatile const void *base);