--- sys/conf/NOTES 2006/01/14 15:37:51 +++ sys/conf/NOTES 2006/01/20 21:30:26 @@ -327,6 +327,12 @@ options DEBUG_MEMGUARD # +# DEBUG_REDZONE enables buffer underflows and buffer overflows detection for +# malloc(9). +# +options DEBUG_REDZONE + +# # KTRACE enables the system-call tracing facility ktrace(2). To be more # SMP-friendly, KTRACE uses a worker thread to process most trace events # asynchronously to the thread generating the event. This requires a --- sys/conf/files 2006/01/27 23:15:39 +++ sys/conf/files 2006/01/27 23:34:16 @@ -1866,6 +1866,7 @@ vm/default_pager.c standard vm/device_pager.c standard vm/phys_pager.c standard +vm/redzone.c optional DEBUG_REDZONE vm/swap_pager.c standard vm/uma_core.c standard vm/uma_dbg.c standard --- sys/conf/options 2006/01/27 23:15:39 +++ sys/conf/options 2006/01/27 23:34:16 @@ -528,6 +528,9 @@ # The MemGuard replacement allocator used for tamper-after-free detection DEBUG_MEMGUARD opt_vm.h +# The RedZone malloc(9) protection +DEBUG_REDZONE opt_vm.h + # Standard SMP options SMP opt_global.h --- sys/kern/kern_malloc.c 2005/12/30 11:48:11 +++ sys/kern/kern_malloc.c 2006/01/27 23:33:14 @@ -65,6 +65,9 @@ #ifdef DEBUG_MEMGUARD #include #endif +#ifdef DEBUG_REDZONE +#include +#endif #if defined(INVARIANTS) && defined(__i386__) #include @@ -259,10 +262,14 @@ caddr_t va; uma_zone_t zone; uma_keg_t keg; -#ifdef DIAGNOSTIC +#if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE) unsigned long osize = size; #endif +#ifdef DEBUG_REDZONE + size = redzone_size_ntor(size); +#endif + #ifdef INVARIANTS /* * Check that exactly one of M_WAITOK or M_NOWAIT is specified. @@ -331,6 +338,10 @@ memset(va, 0x70, osize); } #endif +#ifdef DEBUG_REDZONE + if (va != NULL) + va = redzone_setup(va, osize); +#endif return ((void *) va); } @@ -351,6 +362,11 @@ if (addr == NULL) return; +#ifdef DEBUG_REDZONE + redzone_check(addr); + addr = redzone_addr_ntor(addr); +#endif + #ifdef DEBUG_MEMGUARD if (memguard_cmp(mtp)) { memguard_free(addr); @@ -409,6 +425,11 @@ if (addr == NULL) return (malloc(size, mtp, flags)); +#ifdef DEBUG_REDZONE + slab = NULL; + alloc = redzone_get_size(addr); +#else + /* * XXX: Should report free of old memory and alloc of new memory to * per-CPU stats. @@ -435,13 +456,20 @@ /* Reuse the original block if appropriate */ if (size <= alloc - && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) + && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) { +#ifdef DEBUG_REDZONE + redzone_check(oaddr); + addr = redzone_setup(addr, osize); +#endif return (addr); + } #ifdef DEBUG_MEMGUARD } #endif +#endif /* DEBUG_REDZONE */ + /* Allocate a new, bigger (or smaller) block */ if ((newaddr = malloc(size, mtp, flags)) == NULL) return (NULL); --- /dev/null Sat Jan 28 00:43:44 2006 +++ sys/vm/redzone.c Sat Jan 28 00:32:40 2006 @@ -0,0 +1,176 @@ +/*- + * Copyright (c) 2006 Pawel Jakub Dawidek + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +SYSCTL_NODE(_vm, OID_AUTO, redzone, CTLFLAG_RW, NULL, "RedZone data"); +static u_long redzone_extra_mem = 0; +SYSCTL_ULONG(_vm_redzone, OID_AUTO, extra_mem, CTLFLAG_RD, &redzone_extra_mem, + 0, "Extra memory allocated by redzone"); + +#define REDZONE_CHSIZE (16) +#define REDZONE_CFSIZE (16) +#define REDZONE_HSIZE (sizeof(struct stack) + sizeof(u_long) + REDZONE_CHSIZE) +#define REDZONE_FSIZE (REDZONE_CFSIZE) + +static u_long +redzone_roundup(u_long n) +{ + + if (n <= 128) + return (128); + else if (n <= 256) + return (256); + else if (n <= 512) + return (512); + else if (n <= 1024) + return (1024); + else if (n <= 2048) + return (2048); + return (PAGE_SIZE); +} + +u_long +redzone_get_size(caddr_t naddr) +{ + u_long nsize; + + bcopy(naddr - REDZONE_CHSIZE - sizeof(u_long), &nsize, sizeof(nsize)); + return (nsize); +} + +u_long +redzone_size_ntor(u_long nsize) +{ + + return (nsize + redzone_roundup(nsize) + REDZONE_FSIZE); +} + +void * +redzone_addr_ntor(caddr_t naddr) +{ + + return (naddr - redzone_roundup(redzone_get_size(naddr))); +} + +/* + * Sets redzones and remembers allocation backtrace. + */ +void * +redzone_setup(caddr_t raddr, u_long nsize) +{ + struct stack st; + caddr_t haddr, faddr; + + atomic_add_long(&redzone_extra_mem, redzone_size_ntor(nsize) - nsize); + + haddr = raddr + redzone_roundup(nsize) - REDZONE_HSIZE; + faddr = haddr + REDZONE_HSIZE + nsize; + + /* Redzone header. */ + stack_save(&st); + bcopy(&st, haddr, sizeof(st)); + haddr += sizeof(st); + bcopy(&nsize, haddr, sizeof(nsize)); + haddr += sizeof(nsize); + memset(haddr, 0x42, REDZONE_CHSIZE); + haddr += REDZONE_CHSIZE; + + /* Redzone footer. */ + memset(faddr, 0x42, REDZONE_CFSIZE); + + return (haddr); +} + +/* + * Verify redzones. + * This function is called on free() and realloc(). + */ +void +redzone_check(caddr_t naddr) +{ + struct stack st; + caddr_t haddr, faddr; + u_int ncorruptions; + u_long nsize; + int i; + + haddr = naddr - REDZONE_HSIZE; + bcopy(haddr, &st, sizeof(st)); + haddr += sizeof(st); + bcopy(haddr, &nsize, sizeof(nsize)); + haddr += sizeof(nsize); + + atomic_subtract_long(&redzone_extra_mem, + redzone_size_ntor(nsize) - nsize); + + /* Look for buffer underflow. */ + ncorruptions = 0; + for (i = 0; i < REDZONE_CHSIZE; i++, haddr++) { + if (*(u_char *)haddr != 0x42) + ncorruptions++; + } + if (ncorruptions > 0) { + printf("REDZONE: Buffer underflow detected. %u byte%s " + "corrupted before %p (%lu bytes allocated).\n", + ncorruptions, ncorruptions == 1 ? "" : "s", naddr, nsize); + printf("Allocation backtrace:\n"); + stack_print(&st); + printf("Free backtrace:\n"); + stack_save(&st); + stack_print(&st); + } + faddr = naddr + nsize; + /* Look for buffer overflow. */ + ncorruptions = 0; + for (i = 0; i < REDZONE_CFSIZE; i++, faddr++) { + if (*(u_char *)faddr != 0x42) + ncorruptions++; + } + if (ncorruptions > 0) { + printf("REDZONE: Buffer overflow detected. %u byte%s corrupted " + "after %p (%lu bytes allocated).\n", ncorruptions, + ncorruptions == 1 ? "" : "s", naddr + nsize, nsize); + printf("Allocation backtrace:\n"); + stack_print(&st); + printf("Free backtrace:\n"); + stack_save(&st); + stack_print(&st); + } +} --- /dev/null Sat Jan 28 00:43:51 2006 +++ sys/vm/redzone.h Sat Jan 28 00:32:40 2006 @@ -0,0 +1,38 @@ +/*- + * Copyright (c) 2006 Pawel Jakub Dawidek + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _VM_REDZONE_H_ +#define _VM_REDZONE_H_ + +u_long redzone_get_size(caddr_t naddr); +u_long redzone_size_ntor(u_long nsize); +void *redzone_addr_ntor(caddr_t naddr); +void *redzone_setup(caddr_t raddr, u_long nsize); +void redzone_check(caddr_t naddr); + +#endif /* _VM_REDZONE_H_ */