Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Add KASAN instrumentation for copyin/copyinstr/copyoutst...



details:   https://anonhg.NetBSD.org/src/rev/b284cb2fd716
branches:  trunk
changeset: 450383:b284cb2fd716
user:      maxv <maxv%NetBSD.org@localhost>
date:      Thu Apr 11 17:43:45 2019 +0000

description:
Add KASAN instrumentation for copyin/copyinstr/copyoutstr. No copyout for
now, because mm.c needs whitelisting.

diffstat:

 sys/kern/subr_asan.c |  36 ++++++++++++++++++++++++++++++++++--
 sys/sys/systm.h      |  11 ++++++++++-
 2 files changed, 44 insertions(+), 3 deletions(-)

diffs (85 lines):

diff -r bc6bef0e27c3 -r b284cb2fd716 sys/kern/subr_asan.c
--- a/sys/kern/subr_asan.c      Thu Apr 11 14:47:06 2019 +0000
+++ b/sys/kern/subr_asan.c      Thu Apr 11 17:43:45 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_asan.c,v 1.6 2019/04/07 09:20:04 maxv Exp $       */
+/*     $NetBSD: subr_asan.c,v 1.7 2019/04/11 17:43:45 maxv Exp $       */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.6 2019/04/07 09:20:04 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.7 2019/04/11 17:43:45 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -479,6 +479,38 @@
        return (s - str);
 }
 
+#undef copyinstr
+#undef copyoutstr
+#undef copyin
+
+int    kasan_copyinstr(const void *, void *, size_t, size_t *);
+int    kasan_copyoutstr(const void *, void *, size_t, size_t *);
+int    kasan_copyin(const void *, void *, size_t);
+int    copyinstr(const void *, void *, size_t, size_t *);
+int    copyoutstr(const void *, void *, size_t, size_t *);
+int    copyin(const void *, void *, size_t);
+
+int
+kasan_copyin(const void *uaddr, void *kaddr, size_t len)
+{
+       kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
+       return copyin(uaddr, kaddr, len);
+}
+
+int
+kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
+{
+       kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
+       return copyinstr(uaddr, kaddr, len, done);
+}
+
+int
+kasan_copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done)
+{
+       kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR);
+       return copyoutstr(kaddr, uaddr, len, done);
+}
+
 /* -------------------------------------------------------------------------- */
 
 void __asan_register_globals(struct __asan_global *, size_t);
diff -r bc6bef0e27c3 -r b284cb2fd716 sys/sys/systm.h
--- a/sys/sys/systm.h   Thu Apr 11 14:47:06 2019 +0000
+++ b/sys/sys/systm.h   Thu Apr 11 17:43:45 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: systm.h,v 1.282 2019/04/06 03:06:28 thorpej Exp $      */
+/*     $NetBSD: systm.h,v 1.283 2019/04/11 17:43:45 maxv Exp $ */
 
 /*-
  * Copyright (c) 1982, 1988, 1991, 1993
@@ -269,9 +269,18 @@
 #endif /* KERNEL */
 
 int    copystr(const void *, void *, size_t, size_t *);
+#if defined(_KERNEL) && defined(KASAN)
+int    kasan_copyinstr(const void *, void *, size_t, size_t *);
+int    kasan_copyoutstr(const void *, void *, size_t, size_t *);
+int    kasan_copyin(const void *, void *, size_t);
+#define copyinstr      kasan_copyinstr
+#define copyoutstr     kasan_copyoutstr
+#define copyin         kasan_copyin
+#else
 int    copyinstr(const void *, void *, size_t, size_t *);
 int    copyoutstr(const void *, void *, size_t, size_t *);
 int    copyin(const void *, void *, size_t);
+#endif
 int    copyout(const void *, void *, size_t);
 
 #ifdef KLEAK



Home | Main Index | Thread Index | Old Index