git: 28c338b34263 - stable/13 - realloc: Fix KASAN(9) shadow map updates

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 01 Nov 2021 14:33:05 UTC
The branch stable/13 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=28c338b3426330b5c9668651e4e91b2dfafde6cf

commit 28c338b3426330b5c9668651e4e91b2dfafde6cf
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2021-05-05 21:05:46 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2021-11-01 14:05:22 +0000

    realloc: Fix KASAN(9) shadow map updates
    
    When copying from the old buffer to the new buffer, we don't know the
    requested size of the old allocation, but only the size of the
    allocation provided by UMA.  This value is "alloc".  Because the copy
    may access bytes in the old allocation's red zone, we must mark the full
    allocation valid in the shadow map.  Do so using the correct size.
    
    Reported by:    kp
    Tested by:      kp
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 9a7c2de36460cdb916734a6969aac666707a639b)
---
 sys/kern/kern_malloc.c | 2 +-
 sys/vm/uma_core.c      | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index 6adb16c95528..3061cb91568f 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -1033,7 +1033,7 @@ realloc(void *addr, size_t size, struct malloc_type *mtp, int flags)
 	 * Copy over original contents.  For KASAN, the redzone must be marked
 	 * valid before performing the copy.
 	 */
-	kasan_mark(addr, size, size, 0);
+	kasan_mark(addr, alloc, alloc, 0);
 	bcopy(addr, newaddr, min(size, alloc));
 	free(addr, mtp);
 	return (newaddr);
diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c
index 1398796ee2e7..e3c7e2cc81e9 100644
--- a/sys/vm/uma_core.c
+++ b/sys/vm/uma_core.c
@@ -540,6 +540,9 @@ bucket_zone_drain(int domain)
 }
 
 #ifdef KASAN
+_Static_assert(UMA_SMALLEST_UNIT % KASAN_SHADOW_SCALE == 0,
+    "Base UMA allocation size not a multiple of the KASAN scale factor");
+
 static void
 kasan_mark_item_valid(uma_zone_t zone, void *item)
 {