tech-kern archive

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

Re: kasan: monitor pools



In article <2a4c7cde-ab5e-68b7-de0f-0a307e91a67d%m00nbsd.net@localhost>,
Maxime Villard  <max%m00nbsd.net@localhost> wrote:
>Here is a patch [1] that allows kasan to monitor pools and pool_caches. We
>recycle the existing POOL_REDZONE implementation - which I wrote three years
>ago, and which has never been enabled (not even on DEBUG). With this we can
>detect read/write buffer overflows on all our pools, and in particular, on
>mbufs.
>
>I guess people are fine? Otherwise if we prefer to keep a KASAN-independent
>version of POOL_REDZONE, then we need to enable it under DIAGNOSTIC at least
>for it to be meaningful.
>
>[1] http://m00nbsd.net/garbage/kasan/pool.diff

Sure, how about also following the empty function style the redzone code
follows to reduce ifdefs?

christos

Index: sys/asan.h
===================================================================
RCS file: /cvsroot/src/sys/sys/asan.h,v
retrieving revision 1.1
diff -u -p -u -r1.1 asan.h
--- sys/asan.h	20 Aug 2018 15:04:52 -0000	1.1
+++ sys/asan.h	21 Aug 2018 12:04:50 -0000
@@ -34,8 +34,14 @@
 
 #include <sys/types.h>
 
+#ifdef KASAN
 void kasan_add_redzone(size_t *);
 void kasan_alloc(const void *, size_t, size_t);
 void kasan_free(const void *, size_t);
+#else
+#define kasan_add_redzone(s)	__nothing
+#define kasan_alloc(p, s, l)	__nothing
+#define kasan_free(p, s)	__nothing
+#endif
 
 #endif /* !_SYS_ASAN_H_ */
Index: kern/kern_malloc.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_malloc.c,v
retrieving revision 1.150
diff -u -p -u -r1.150 kern_malloc.c
--- kern/kern_malloc.c	21 Aug 2018 07:56:53 -0000	1.150
+++ kern/kern_malloc.c	21 Aug 2018 12:04:51 -0000
@@ -80,9 +80,7 @@ __KERNEL_RCSID(0, "$NetBSD: kern_malloc.
 #include <sys/malloc.h>
 #include <sys/kmem.h>
 
-#ifdef KASAN
 #include <sys/asan.h>
-#endif
 
 /*
  * Built-in malloc types.  Note: ought to be removed.
@@ -111,16 +109,12 @@ void *
 kern_malloc(unsigned long size, int flags)
 {
 	const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
-#ifdef KASAN
 	size_t origsize = size;
-#endif
 	size_t allocsize, hdroffset;
 	struct malloc_header *mh;
 	void *p;
 
-#ifdef KASAN
 	kasan_add_redzone(&size);
-#endif
 
 	if (size >= PAGE_SIZE) {
 		if (size > (ULONG_MAX-PAGE_SIZE))
@@ -147,9 +141,7 @@ kern_malloc(unsigned long size, int flag
 #endif
 	mh++;
 
-#ifdef KASAN
 	kasan_alloc(mh, origsize, size);
-#endif
 
 	return mh;
 }
@@ -162,9 +154,7 @@ kern_free(void *addr)
 	mh = addr;
 	mh--;
 
-#ifdef KASAN
 	kasan_free(addr, mh->mh_size);
-#endif
 
 	if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header))
 		kmem_intr_free((char *)addr - PAGE_SIZE,
Index: kern/subr_kmem.c
===================================================================
RCS file: /cvsroot/src/sys/kern/subr_kmem.c,v
retrieving revision 1.69
diff -u -p -u -r1.69 subr_kmem.c
--- kern/subr_kmem.c	20 Aug 2018 15:04:52 -0000	1.69
+++ kern/subr_kmem.c	21 Aug 2018 12:04:51 -0000
@@ -107,9 +107,7 @@ __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,
 #include <sys/lockdebug.h>
 #include <sys/cpu.h>
 
-#ifdef KASAN
 #include <sys/asan.h>
-#endif
 
 #include <uvm/uvm_extern.h>
 #include <uvm/uvm_map.h>
@@ -247,9 +245,7 @@ kmem_intr_alloc(size_t requested_size, k
 	}
 #endif
 
-#ifdef KASAN
 	kasan_add_redzone(&requested_size);
-#endif
 
 	size = kmem_roundup_size(requested_size);
 	allocsz = size + SIZE_SIZE;
@@ -278,9 +274,7 @@ kmem_intr_alloc(size_t requested_size, k
 		FREECHECK_OUT(&kmem_freecheck, p);
 		kmem_size_set(p, requested_size);
 		p += SIZE_SIZE;
-#ifdef KASAN
 		kasan_alloc(p, origsize, size);
-#endif
 		return p;
 	}
 	return p;
@@ -323,16 +317,12 @@ kmem_intr_free(void *p, size_t requested
 	}
 #endif
 
-#ifdef KASAN
 	kasan_add_redzone(&requested_size);
-#endif
 
 	size = kmem_roundup_size(requested_size);
 	allocsz = size + SIZE_SIZE;
 
-#ifdef KASAN
 	kasan_free(p, size);
-#endif
 
 	if ((index = ((allocsz -1) >> KMEM_SHIFT))
 	    < kmem_cache_maxidx) {



Home | Main Index | Thread Index | Old Index