From nobody Mon Nov 01 14:33:19 2021 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 BA479182E231; Mon, 1 Nov 2021 14:33: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 4Hjb9z501lz3sgD; Mon, 1 Nov 2021 14:33:27 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 9CD13134A6; Mon, 1 Nov 2021 14:33:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1A1EXJZv021821; Mon, 1 Nov 2021 14:33:19 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1A1EXJ0R021820; Mon, 1 Nov 2021 14:33:19 GMT (envelope-from git) Date: Mon, 1 Nov 2021 14:33:19 GMT Message-Id: <202111011433.1A1EXJ0R021820@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 3388bf06d767 - stable/13 - Generalize sanitizer interceptors for memory and string routines 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/stable/13 X-Git-Reftype: branch X-Git-Commit: 3388bf06d767faf3390a77a96c3365926f05c201 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=3388bf06d767faf3390a77a96c3365926f05c201 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 - int 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 */ #else #define strcpy(d, s) __builtin_strcpy((d), (s)) #define strcmp(s1, s2) __builtin_strcmp((s1), (s2)) #define strlen(s) __builtin_strlen((s)) -#endif +#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_ +#endif + +#ifdef SAN_PREFIX +#define SAN_INTERCEPTOR(func) __CONCAT(SAN_PREFIX, 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); +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 */ #else #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 +#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)) -#endif +#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);