Source-Changes-HG archive

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

[src/trunk]: src/sys/lib/libkern Add kasan interceptors for strcpy/strcmp/str...



details:   https://anonhg.NetBSD.org/src/rev/1e1231f1d8f0
branches:  trunk
changeset: 992841:1e1231f1d8f0
user:      maxv <maxv%NetBSD.org@localhost>
date:      Mon Aug 27 08:53:19 2018 +0000

description:
Add kasan interceptors for strcpy/strcmp/strlen.

diffstat:

 sys/arch/amd64/amd64/asan.c |  53 +++++++++++++++++++++++++++++++++++++++++++-
 sys/lib/libkern/libkern.h   |  13 +++++++++-
 2 files changed, 62 insertions(+), 4 deletions(-)

diffs (111 lines):

diff -r 69032ef08291 -r 1e1231f1d8f0 sys/arch/amd64/amd64/asan.c
--- a/sys/arch/amd64/amd64/asan.c       Mon Aug 27 07:57:56 2018 +0000
+++ b/sys/arch/amd64/amd64/asan.c       Mon Aug 27 08:53:19 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: asan.c,v 1.6 2018/08/23 11:56:10 maxv Exp $    */
+/*     $NetBSD: asan.c,v 1.7 2018/08/27 08:53:19 maxv Exp $    */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: asan.c,v 1.6 2018/08/23 11:56:10 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: asan.c,v 1.7 2018/08/27 08:53:19 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -543,6 +543,55 @@
        return __builtin_memset(b, c, len);
 }
 
+char *
+kasan_strcpy(char *dst, const char *src)
+{
+       char *save = dst;
+
+       while (1) {
+               kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR);
+               kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR);
+               *dst = *src;
+               if (*src == '\0')
+                       break;
+               src++, dst++;
+       }
+
+       return save;
+}
+
+int
+kasan_strcmp(const char *s1, const char *s2)
+{
+       while (1) {
+               kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR);
+               kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR);
+               if (*s1 != *s2)
+                       break;
+               if (*s1 == '\0')
+                       return 0;
+               s1++, s2++;
+       }
+
+       return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
+}
+
+size_t
+kasan_strlen(const char *str)
+{
+       const char *s;
+
+       s = str;
+       while (1) {
+               kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR);
+               if (*s == '\0')
+                       break;
+               s++;
+       }
+
+       return (s - str);
+}
+
 /* -------------------------------------------------------------------------- */
 
 #if defined(__clang__) && (__clang_major__ - 0 >= 6)
diff -r 69032ef08291 -r 1e1231f1d8f0 sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Mon Aug 27 07:57:56 2018 +0000
+++ b/sys/lib/libkern/libkern.h Mon Aug 27 08:53:19 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.128 2018/08/20 15:04:52 maxv Exp $       */
+/*     $NetBSD: libkern.h,v 1.129 2018/08/27 08:53:19 maxv Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -371,7 +371,7 @@
 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
 #if defined(_KERNEL) && defined(KASAN)
 void   *kasan_memset(void *, int, size_t);
-int    kasan_memcmp(const void *, const void *, size_t);
+int     kasan_memcmp(const void *, const void *, size_t);
 void   *kasan_memcpy(void *, const void *, size_t);
 #define        memcpy(d, s, l)         kasan_memcpy(d, s, l)
 #define        memcmp(a, b, l)         kasan_memcmp(a, b, l)
@@ -389,9 +389,18 @@
 size_t  strnlen(const char *, size_t);
 char   *strsep(char **, const char *);
 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
+#if defined(_KERNEL) && defined(KASAN)
+char   *kasan_strcpy(char *, const char *);
+int     kasan_strcmp(const char *, const char *);
+size_t  kasan_strlen(const char *);
+#define        strcpy(d, s)            kasan_strcpy(d, s)
+#define        strcmp(a, b)            kasan_strcmp(a, b)
+#define        strlen(a)               kasan_strlen(a)
+#else
 #define        strcpy(d, s)            __builtin_strcpy(d, s)
 #define        strcmp(a, b)            __builtin_strcmp(a, b)
 #define        strlen(a)               __builtin_strlen(a)
+#endif /* _KERNEL && KASAN */
 #endif
 
 /* Functions for which we always use built-ins. */



Home | Main Index | Thread Index | Old Index