Source-Changes-HG archive

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

[src/trunk]: src/sys/kern Add kASan redzones on pools and pool_caches. Also e...



details:   https://anonhg.NetBSD.org/src/rev/a2a27a27ccd5
branches:  trunk
changeset: 365601:a2a27a27ccd5
user:      maxv <maxv%NetBSD.org@localhost>
date:      Thu Aug 23 12:18:02 2018 +0000

description:
Add kASan redzones on pools and pool_caches. Also enable POOL_REDZONE
on DIAGNOSTIC.

diffstat:

 sys/kern/subr_pool.c |  35 ++++++++++++++++++++++++++---------
 1 files changed, 26 insertions(+), 9 deletions(-)

diffs (109 lines):

diff -r 66935e8a3150 -r a2a27a27ccd5 sys/kern/subr_pool.c
--- a/sys/kern/subr_pool.c      Thu Aug 23 11:56:10 2018 +0000
+++ b/sys/kern/subr_pool.c      Thu Aug 23 12:18:02 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_pool.c,v 1.223 2018/07/04 02:19:02 kamil Exp $    */
+/*     $NetBSD: subr_pool.c,v 1.224 2018/08/23 12:18:02 maxv Exp $     */
 
 /*-
  * Copyright (c) 1997, 1999, 2000, 2002, 2007, 2008, 2010, 2014, 2015
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.223 2018/07/04 02:19:02 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.224 2018/08/23 12:18:02 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -55,6 +55,7 @@
 #include <sys/xcall.h>
 #include <sys/cpu.h>
 #include <sys/atomic.h>
+#include <sys/asan.h>
 
 #include <uvm/uvm_extern.h>
 
@@ -85,8 +86,16 @@
 static struct pool psppool;
 #endif
 
+#if defined(DIAGNOSTIC) || defined(KASAN)
+#define POOL_REDZONE
+#endif
+
 #ifdef POOL_REDZONE
-# define POOL_REDZONE_SIZE 2
+# ifdef KASAN
+#  define POOL_REDZONE_SIZE 8
+# else
+#  define POOL_REDZONE_SIZE 2
+# endif
 static void pool_redzone_init(struct pool *, size_t);
 static void pool_redzone_fill(struct pool *, void *);
 static void pool_redzone_check(struct pool *, void *);
@@ -2728,12 +2737,14 @@
 #define STATIC_BYTE    0xFE
 CTASSERT(POOL_REDZONE_SIZE > 1);
 
+#ifndef KASAN
 static inline uint8_t
 pool_pattern_generate(const void *p)
 {
        return (uint8_t)(((uintptr_t)p) * PRIME
           >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
 }
+#endif
 
 static void
 pool_redzone_init(struct pool *pp, size_t requested_size)
@@ -2777,12 +2788,14 @@
 static void
 pool_redzone_fill(struct pool *pp, void *p)
 {
+       if (!pp->pr_redzone)
+               return;
+#ifdef KASAN
+       kasan_alloc(p, pp->pr_reqsize, pp->pr_reqsize + POOL_REDZONE_SIZE);
+#else
        uint8_t *cp, pat;
        const uint8_t *ep;
 
-       if (!pp->pr_redzone)
-               return;
-
        cp = (uint8_t *)p + pp->pr_reqsize;
        ep = cp + POOL_REDZONE_SIZE;
 
@@ -2798,17 +2811,20 @@
                *cp = pool_pattern_generate(cp);
                cp++;
        }
+#endif
 }
 
 static void
 pool_redzone_check(struct pool *pp, void *p)
 {
+       if (!pp->pr_redzone)
+               return;
+#ifdef KASAN
+       kasan_free(p, pp->pr_reqsize + POOL_REDZONE_SIZE);
+#else
        uint8_t *cp, pat, expected;
        const uint8_t *ep;
 
-       if (!pp->pr_redzone)
-               return;
-
        cp = (uint8_t *)p + pp->pr_reqsize;
        ep = cp + POOL_REDZONE_SIZE;
 
@@ -2828,6 +2844,7 @@
                }
                cp++;
        }
+#endif
 }
 
 #endif /* POOL_REDZONE */



Home | Main Index | Thread Index | Old Index