Skip to content

Commit

Permalink
purecap-kasan: Save some KASAN_MALLOC_REDZONE bits
Browse files Browse the repository at this point in the history
We still need some KASAN_MALLOC_REDZONE bits to cover inexact
bounds. This is hopefully a temporary measure, but for sanitisation,
we need it.
  • Loading branch information
RoundofThree committed Nov 19, 2024
1 parent 711b1e3 commit cfce178
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions sys/kern/kern_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,16 +702,18 @@ void *
if (va != NULL)
va = redzone_setup(va, osize);
#endif
#ifdef KASAN
#if defined(KASAN) && !defined(__CHERI_PURE_CAPABILITY__)
if (va != NULL)
kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
#endif
#ifdef __CHERI_PURE_CAPABILITY__
va = cheri_setbounds(va, osize);
KASSERT(cheri_getlen(va) <= CHERI_REPRESENTABLE_LENGTH(osize),
("Invalid bounds: expected %zx found %zx",
(size_t)CHERI_REPRESENTABLE_LENGTH(osize),
(size_t)cheri_getlen(va)));
if (va != NULL) {
va = cheri_setbounds(va, osize);
if (osize < CHERI_REPRESENTABLE_LENGTH(osize)) {
kasan_mark((void *)va, osize, CHERI_REPRESENTABLE_LENGTH(osize),
KASAN_MALLOC_REDZONE);
}
}
#endif
return ((void *) va);
}
Expand Down Expand Up @@ -780,7 +782,7 @@ malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds,
if (va != NULL)
va = redzone_setup(va, osize);
#endif
#ifdef KASAN
#if defined(KASAN) && !defined(__CHERI_PURE_CAPABILITY__)
if (va != NULL)
kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
#endif
Expand All @@ -791,11 +793,12 @@ malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds,
}
#endif
#ifdef __CHERI_PURE_CAPABILITY__
va = cheri_setbounds(va, osize);
KASSERT(cheri_getlen(va) <= CHERI_REPRESENTABLE_LENGTH(osize),
("Invalid bounds: expected %zx found %zx",
(size_t)CHERI_REPRESENTABLE_LENGTH(osize),
(size_t)cheri_getlen(va)));
if (va != NULL) {
va = cheri_setbounds(va, osize);
if (osize < CHERI_REPRESENTABLE_LENGTH(osize)) {
kasan_mark((void *)va, osize, CHERI_REPRESENTABLE_LENGTH(osize), KASAN_MALLOC_REDZONE);
}
}
#endif
return (va);
}
Expand Down Expand Up @@ -1158,7 +1161,14 @@ realloc(void *addr, size_t size, struct malloc_type *mtp, int flags)
/* Reuse the original block if appropriate */
if (size <= alloc &&
(size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) {
kasan_mark((void *)addr, size, alloc, KASAN_MALLOC_REDZONE);
if (!malloc_large_slab(slab)) {
if (size < CHERI_REPRESENTABLE_LENGTH(size)) {
kasan_mark((void *)addr, size, CHERI_REPRESENTABLE_LENGTH(size),
KASAN_MALLOC_REDZONE);
}
} else {
kasan_mark((void *)addr, size, alloc, KASAN_MALLOC_REDZONE);
}
return (addr);
}
#endif /* !DEBUG_REDZONE */
Expand Down

0 comments on commit cfce178

Please sign in to comment.