1f8cd5f23SChristian Brueffer.\" Copyright (c) 2005 Christian Brueffer 2f8cd5f23SChristian Brueffer.\" All rights reserved. 3f8cd5f23SChristian Brueffer.\" 4f8cd5f23SChristian Brueffer.\" Redistribution and use in source and binary forms, with or without 5f8cd5f23SChristian Brueffer.\" modification, are permitted provided that the following conditions 6f8cd5f23SChristian Brueffer.\" are met: 7f8cd5f23SChristian Brueffer.\" 1. Redistributions of source code must retain the above copyright 8f8cd5f23SChristian Brueffer.\" notice, this list of conditions and the following disclaimer. 9f8cd5f23SChristian Brueffer.\" 2. Redistributions in binary form must reproduce the above copyright 10f8cd5f23SChristian Brueffer.\" notice, this list of conditions and the following disclaimer in the 11f8cd5f23SChristian Brueffer.\" documentation and/or other materials provided with the distribution. 12f8cd5f23SChristian Brueffer.\" 13f8cd5f23SChristian Brueffer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14f8cd5f23SChristian Brueffer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15f8cd5f23SChristian Brueffer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16f8cd5f23SChristian Brueffer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17f8cd5f23SChristian Brueffer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18f8cd5f23SChristian Brueffer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19f8cd5f23SChristian Brueffer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20f8cd5f23SChristian Brueffer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21f8cd5f23SChristian Brueffer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22f8cd5f23SChristian Brueffer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23f8cd5f23SChristian Brueffer.\" SUCH DAMAGE. 24f8cd5f23SChristian Brueffer.\" 25f8cd5f23SChristian Brueffer.\" $FreeBSD$ 26f8cd5f23SChristian Brueffer.\" 27*0e775b6dSGleb Smirnoff.Dd October 12, 2011 28f8cd5f23SChristian Brueffer.Dt MEMGUARD 9 29f8cd5f23SChristian Brueffer.Os 30f8cd5f23SChristian Brueffer.Sh NAME 31f8cd5f23SChristian Brueffer.Nm MemGuard 32f8cd5f23SChristian Brueffer.Nd "memory allocator for debugging purposes" 33f8cd5f23SChristian Brueffer.Sh SYNOPSIS 34f8cd5f23SChristian Brueffer.Cd "options DEBUG_MEMGUARD" 35f8cd5f23SChristian Brueffer.Sh DESCRIPTION 36f8cd5f23SChristian Brueffer.Nm 37f8cd5f23SChristian Bruefferis a simple and small replacement memory allocator designed 38f8cd5f23SChristian Bruefferto help detect tamper-after-free scenarios. 39f8cd5f23SChristian BruefferThese problems are more and more common and likely with 40f8cd5f23SChristian Brueffermultithreaded kernels where race conditions are more prevalent. 41f8cd5f23SChristian Brueffer.Pp 42f8cd5f23SChristian Brueffer.Nm 43e3813573SMatthew D Flemingcan take over 44f8cd5f23SChristian Brueffer.Fn malloc , 45f8cd5f23SChristian Brueffer.Fn realloc 46f8cd5f23SChristian Bruefferand 47f8cd5f23SChristian Brueffer.Fn free 48e3813573SMatthew D Flemingfor a single malloc type. 49*0e775b6dSGleb SmirnoffAlternatively 50e3813573SMatthew D Fleming.Nm 51*0e775b6dSGleb Smirnoffcan take over 52*0e775b6dSGleb Smirnoff.Fn uma_zalloc , 53*0e775b6dSGleb Smirnoff.Fn uma_zalloc_arg 54*0e775b6dSGleb Smirnoffand 55*0e775b6dSGleb Smirnoff.Fn uma_free 56*0e775b6dSGleb Smirnofffor a single 57*0e775b6dSGleb Smirnoff.Xr uma 58*0e775b6dSGleb Smirnoffzone. 59*0e775b6dSGleb SmirnoffAlso 60*0e775b6dSGleb Smirnoff.Nm 61*0e775b6dSGleb Smirnoffcan guard all allocations larger than 62e3813573SMatthew D Fleming.Dv PAGE_SIZE , 63e3813573SMatthew D Flemingand can guard a random fraction of all allocations. 64e3813573SMatthew D FlemingThere is also a knob to prevent allocations smaller than a specified 65e3813573SMatthew D Flemingsize from being guarded, to limit memory waste. 66f8cd5f23SChristian Brueffer.Sh EXAMPLES 6781ae4b8dSRuslan ErmilovTo use 6881ae4b8dSRuslan Ermilov.Nm 69e3813573SMatthew D Flemingfor a memory type, either add an entry to 70104a3dc0SPawel Jakub Dawidek.Pa /boot/loader.conf : 71104a3dc0SPawel Jakub Dawidek.Bd -literal -offset indent 72104a3dc0SPawel Jakub Dawidekvm.memguard.desc=<memory_type> 73104a3dc0SPawel Jakub Dawidek.Ed 74104a3dc0SPawel Jakub Dawidek.Pp 75e3813573SMatthew D FlemingOr set the 76104a3dc0SPawel Jakub Dawidek.Va vm.memguard.desc 77104a3dc0SPawel Jakub Dawidek.Xr sysctl 8 78e3813573SMatthew D Flemingvariable at run-time: 79104a3dc0SPawel Jakub Dawidek.Bd -literal -offset indent 80104a3dc0SPawel Jakub Dawideksysctl vm.memguard.desc=<memory_type> 81104a3dc0SPawel Jakub Dawidek.Ed 82104a3dc0SPawel Jakub Dawidek.Pp 83e3813573SMatthew D FlemingWhere 84e3813573SMatthew D Fleming.Ar memory_type 85*0e775b6dSGleb Smirnoffcan be either a short description of the memory type to monitor, 86*0e775b6dSGleb Smirnoffeither name of 87*0e775b6dSGleb Smirnoff.Xr uma 9 88*0e775b6dSGleb Smirnoffzone. 89e3813573SMatthew D FlemingOnly allocations from that 90e3813573SMatthew D Fleming.Ar memory_type 91e3813573SMatthew D Flemingmade after 92e3813573SMatthew D Fleming.Va vm.memguard.desc 93e3813573SMatthew D Flemingis set will potentially be guarded. 94e3813573SMatthew D FlemingIf 95e3813573SMatthew D Fleming.Va vm.memguard.desc 96e3813573SMatthew D Flemingis modified at run-time then only allocations of the new 97e3813573SMatthew D Fleming.Ar memory_type 98e3813573SMatthew D Flemingwill potentially be guarded once the 99e3813573SMatthew D Fleming.Xr sysctl 8 100e3813573SMatthew D Flemingis set. 101e3813573SMatthew D FlemingExisting guarded allocations will still be properly released by 102*0e775b6dSGleb Smirnoffeither 103*0e775b6dSGleb Smirnoff.Xr free 9 104*0e775b6dSGleb Smirnoffor 105*0e775b6dSGleb Smirnoff.Xr uma_zfree 9 , 106*0e775b6dSGleb Smirnoffdepending on what kind of allocation was taken over. 107e3813573SMatthew D Fleming.Pp 108*0e775b6dSGleb SmirnoffTo determine short description of a 109e3813573SMatthew D Fleming.Xr malloc 9 110*0e775b6dSGleb Smirnofftype one can either take it from the first column of 111*0e775b6dSGleb Smirnoff.Xr vmstat 8 Fl m 112*0e775b6dSGleb Smirnoffoutput, or to find it in the kernel source. 113*0e775b6dSGleb SmirnoffIt is the second argument to 114*0e775b6dSGleb Smirnoff.Xr MALLOC_DEFINE 9 115*0e775b6dSGleb Smirnoffmacro. 116*0e775b6dSGleb SmirnoffTo determine name of 117*0e775b6dSGleb Smirnoff.Xr uma 9 118*0e775b6dSGleb Smirnoffzone one can either take it from the first column of 119*0e775b6dSGleb Smirnoff.Xr vmstat 8 Fl z 120*0e775b6dSGleb Smirnoffoutput, or to find it in the kernel source. 121*0e775b6dSGleb SmirnoffIt is the first argument to the 122*0e775b6dSGleb Smirnoff.Xr uma_zcreate 9 123*0e775b6dSGleb Smirnofffunction. 124e3813573SMatthew D Fleming.Pp 125104a3dc0SPawel Jakub DawidekThe 126104a3dc0SPawel Jakub Dawidek.Va vm.memguard.divisor 127e3813573SMatthew D Flemingboot-time tunable is used to scale how much of the system's physical 128e3813573SMatthew D Flemingmemory 129e3813573SMatthew D Fleming.Nm 130e3813573SMatthew D Flemingis allowed to consume. 131e3813573SMatthew D FlemingThe default is 10, so up to 132e3813573SMatthew D Fleming.Va cnt.v_page_count Ns /10 133e3813573SMatthew D Flemingpages can be used. 134e3813573SMatthew D Fleming.Nm 135e3813573SMatthew D Flemingwill reserve 136e3813573SMatthew D Fleming.Va vm_kmem_max 137e3813573SMatthew D Fleming/ 138e3813573SMatthew D Fleming.Va vm.memguard.divisor 139e3813573SMatthew D Flemingbytes of virtual address space, limited by twice the physical memory 140e3813573SMatthew D Flemingsize. 141e3813573SMatthew D FlemingThe physical limit is reported as 142e3813573SMatthew D Fleming.Va vm.memguard.phys_limit 143e3813573SMatthew D Flemingand the virtual space reserved for 144e3813573SMatthew D Fleming.Nm 145e3813573SMatthew D Flemingis reported as 146e3813573SMatthew D Fleming.Va vm.memguard.mapsize . 147e3813573SMatthew D Fleming.Pp 148e3813573SMatthew D Fleming.Nm 149e3813573SMatthew D Flemingwill not do page promotions for any allocation smaller than 150e3813573SMatthew D Fleming.Va vm.memguard.minsize 151e3813573SMatthew D Flemingbytes. 152e3813573SMatthew D FlemingThe default is 0, meaning all allocations can potentially be guarded. 153e3813573SMatthew D Fleming.Nm 154e3813573SMatthew D Flemingcan guard sufficiently large allocations randomly, with average 155e3813573SMatthew D Flemingfrequency of every one in 100000 / 156e3813573SMatthew D Fleming.Va vm.memguard.frequency 157e3813573SMatthew D Flemingallocations. 158e3813573SMatthew D FlemingThe default is 0, meaning no allocations are randomly guarded. 159e3813573SMatthew D Fleming.Pp 160e3813573SMatthew D Fleming.Nm 161e3813573SMatthew D Flemingcan optionally add unmapped guard pages around each allocation to 162e3813573SMatthew D Flemingdetect overflow and underflow, if 163e3813573SMatthew D Fleming.Va vm.memguard.options 164e3813573SMatthew D Fleminghas the 1 bit set. 165e3813573SMatthew D FlemingThis option is enabled by default. 166e3813573SMatthew D Fleming.Nm 167e3813573SMatthew D Flemingwill optionally guard all allocations of 168e3813573SMatthew D Fleming.Dv PAGE_SIZE 169e3813573SMatthew D Flemingor larger if 170e3813573SMatthew D Fleming.Va vm.memguard.options 171e3813573SMatthew D Fleminghas the 2 bit set. 172e3813573SMatthew D FlemingThis option is off by default. 173*0e775b6dSGleb SmirnoffBy default 174*0e775b6dSGleb Smirnoff.Nm 175*0e775b6dSGleb Smirnoffdoesn't guard those 176*0e775b6dSGleb Smirnoff.Xr uma 9 177*0e775b6dSGleb Smirnoffzones that have been initialized with the 178*0e775b6dSGleb Smirnoff.Dv UMA_ZONE_NOFREE 179*0e775b6dSGleb Smirnoffflag set, since it can produce false positives on them. 180*0e775b6dSGleb SmirnoffHowever, this safety measure can be turned off by setting bit 3 181*0e775b6dSGleb Smirnoffof the 182*0e775b6dSGleb Smirnoff.Va vm.memguard.options 183*0e775b6dSGleb Smirnofftunable. 184f8cd5f23SChristian Brueffer.Sh SEE ALSO 185f8cd5f23SChristian Brueffer.Xr sysctl 8 , 186f8cd5f23SChristian Brueffer.Xr vmstat 8 , 187f8cd5f23SChristian Brueffer.Xr contigmalloc 9 , 188b88c6e87SChristian Brueffer.Xr malloc 9 , 189*0e775b6dSGleb Smirnoff.Xr redzone 9 , 190*0e775b6dSGleb Smirnoff.Xr uma 9 191f8cd5f23SChristian Brueffer.Sh HISTORY 192f8cd5f23SChristian Brueffer.Nm 193f8cd5f23SChristian Bruefferfirst appeared in 194f8cd5f23SChristian Brueffer.Fx 6.0 . 195f8cd5f23SChristian Brueffer.Sh AUTHORS 196f8cd5f23SChristian Brueffer.An -nosplit 197f8cd5f23SChristian Brueffer.Nm 198e3813573SMatthew D Flemingwas originally written by 199f8cd5f23SChristian Brueffer.An Bosko Milekic Aq bmilekic@FreeBSD.org . 200e3813573SMatthew D FlemingThis manual page was originally written by 201f8cd5f23SChristian Brueffer.An Christian Brueffer Aq brueffer@FreeBSD.org . 202e3813573SMatthew D FlemingAdditions have been made by 203e3813573SMatthew D Fleming.An Matthew Fleming Aq mdf@FreeBSD.org 204*0e775b6dSGleb Smirnoffand 205*0e775b6dSGleb Smirnoff.An Gleb Smirnoff Aq glebius@FreeBSD.org 206e3813573SMatthew D Flemingto both the implementation and the documentation. 207