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.\" 25*02e7c2cbSEnji Cooper.Dd March 22, 2017 26f8cd5f23SChristian Brueffer.Dt MEMGUARD 9 27f8cd5f23SChristian Brueffer.Os 28f8cd5f23SChristian Brueffer.Sh NAME 29f8cd5f23SChristian Brueffer.Nm MemGuard 30f8cd5f23SChristian Brueffer.Nd "memory allocator for debugging purposes" 31f8cd5f23SChristian Brueffer.Sh SYNOPSIS 32f8cd5f23SChristian Brueffer.Cd "options DEBUG_MEMGUARD" 33f8cd5f23SChristian Brueffer.Sh DESCRIPTION 34f8cd5f23SChristian Brueffer.Nm 35f8cd5f23SChristian Bruefferis a simple and small replacement memory allocator designed 36f8cd5f23SChristian Bruefferto help detect tamper-after-free scenarios. 37f8cd5f23SChristian BruefferThese problems are more and more common and likely with 38f8cd5f23SChristian Brueffermultithreaded kernels where race conditions are more prevalent. 39f8cd5f23SChristian Brueffer.Pp 40f8cd5f23SChristian Brueffer.Nm 41e3813573SMatthew D Flemingcan take over 42f8cd5f23SChristian Brueffer.Fn malloc , 43f8cd5f23SChristian Brueffer.Fn realloc 44f8cd5f23SChristian Bruefferand 45f8cd5f23SChristian Brueffer.Fn free 46e3813573SMatthew D Flemingfor a single malloc type. 470e775b6dSGleb SmirnoffAlternatively 48e3813573SMatthew D Fleming.Nm 490e775b6dSGleb Smirnoffcan take over 500e775b6dSGleb Smirnoff.Fn uma_zalloc , 510e775b6dSGleb Smirnoff.Fn uma_zalloc_arg 520e775b6dSGleb Smirnoffand 530e775b6dSGleb Smirnoff.Fn uma_free 540e775b6dSGleb Smirnofffor a single 55*02e7c2cbSEnji Cooper.Xr uma 9 560e775b6dSGleb Smirnoffzone. 570e775b6dSGleb SmirnoffAlso 580e775b6dSGleb Smirnoff.Nm 590e775b6dSGleb Smirnoffcan guard all allocations larger than 60e3813573SMatthew D Fleming.Dv PAGE_SIZE , 61e3813573SMatthew D Flemingand can guard a random fraction of all allocations. 62e3813573SMatthew D FlemingThere is also a knob to prevent allocations smaller than a specified 63e3813573SMatthew D Flemingsize from being guarded, to limit memory waste. 64f8cd5f23SChristian Brueffer.Sh EXAMPLES 6581ae4b8dSRuslan ErmilovTo use 6681ae4b8dSRuslan Ermilov.Nm 67e3813573SMatthew D Flemingfor a memory type, either add an entry to 68104a3dc0SPawel Jakub Dawidek.Pa /boot/loader.conf : 69104a3dc0SPawel Jakub Dawidek.Bd -literal -offset indent 70104a3dc0SPawel Jakub Dawidekvm.memguard.desc=<memory_type> 71104a3dc0SPawel Jakub Dawidek.Ed 72104a3dc0SPawel Jakub Dawidek.Pp 73e3813573SMatthew D FlemingOr set the 74104a3dc0SPawel Jakub Dawidek.Va vm.memguard.desc 75104a3dc0SPawel Jakub Dawidek.Xr sysctl 8 76e3813573SMatthew D Flemingvariable at run-time: 77104a3dc0SPawel Jakub Dawidek.Bd -literal -offset indent 78104a3dc0SPawel Jakub Dawideksysctl vm.memguard.desc=<memory_type> 79104a3dc0SPawel Jakub Dawidek.Ed 80104a3dc0SPawel Jakub Dawidek.Pp 81e3813573SMatthew D FlemingWhere 82e3813573SMatthew D Fleming.Ar memory_type 830e775b6dSGleb Smirnoffcan be either a short description of the memory type to monitor, 840e775b6dSGleb Smirnoffeither name of 850e775b6dSGleb Smirnoff.Xr uma 9 860e775b6dSGleb Smirnoffzone. 87e3813573SMatthew D FlemingOnly allocations from that 88e3813573SMatthew D Fleming.Ar memory_type 89e3813573SMatthew D Flemingmade after 90e3813573SMatthew D Fleming.Va vm.memguard.desc 91e3813573SMatthew D Flemingis set will potentially be guarded. 92e3813573SMatthew D FlemingIf 93e3813573SMatthew D Fleming.Va vm.memguard.desc 94e3813573SMatthew D Flemingis modified at run-time then only allocations of the new 95e3813573SMatthew D Fleming.Ar memory_type 96e3813573SMatthew D Flemingwill potentially be guarded once the 97e3813573SMatthew D Fleming.Xr sysctl 8 98e3813573SMatthew D Flemingis set. 99e3813573SMatthew D FlemingExisting guarded allocations will still be properly released by 1000e775b6dSGleb Smirnoffeither 1010e775b6dSGleb Smirnoff.Xr free 9 1020e775b6dSGleb Smirnoffor 1030e775b6dSGleb Smirnoff.Xr uma_zfree 9 , 1040e775b6dSGleb Smirnoffdepending on what kind of allocation was taken over. 105e3813573SMatthew D Fleming.Pp 1060e775b6dSGleb SmirnoffTo determine short description of a 107e3813573SMatthew D Fleming.Xr malloc 9 1080e775b6dSGleb Smirnofftype one can either take it from the first column of 1090e775b6dSGleb Smirnoff.Xr vmstat 8 Fl m 1100e775b6dSGleb Smirnoffoutput, or to find it in the kernel source. 1110e775b6dSGleb SmirnoffIt is the second argument to 1120e775b6dSGleb Smirnoff.Xr MALLOC_DEFINE 9 1130e775b6dSGleb Smirnoffmacro. 1140e775b6dSGleb SmirnoffTo determine name of 1150e775b6dSGleb Smirnoff.Xr uma 9 1160e775b6dSGleb Smirnoffzone one can either take it from the first column of 1170e775b6dSGleb Smirnoff.Xr vmstat 8 Fl z 1180e775b6dSGleb Smirnoffoutput, or to find it in the kernel source. 1190e775b6dSGleb SmirnoffIt is the first argument to the 1200e775b6dSGleb Smirnoff.Xr uma_zcreate 9 1210e775b6dSGleb Smirnofffunction. 122e3813573SMatthew D Fleming.Pp 123104a3dc0SPawel Jakub DawidekThe 124104a3dc0SPawel Jakub Dawidek.Va vm.memguard.divisor 125e3813573SMatthew D Flemingboot-time tunable is used to scale how much of the system's physical 126e3813573SMatthew D Flemingmemory 127e3813573SMatthew D Fleming.Nm 128e3813573SMatthew D Flemingis allowed to consume. 129e3813573SMatthew D FlemingThe default is 10, so up to 13044f1c916SBryan Drewery.Va vm_cnt.v_page_count Ns /10 131e3813573SMatthew D Flemingpages can be used. 132e3813573SMatthew D Fleming.Nm 133e3813573SMatthew D Flemingwill reserve 134e3813573SMatthew D Fleming.Va vm_kmem_max 135e3813573SMatthew D Fleming/ 136e3813573SMatthew D Fleming.Va vm.memguard.divisor 137e3813573SMatthew D Flemingbytes of virtual address space, limited by twice the physical memory 138e3813573SMatthew D Flemingsize. 139e3813573SMatthew D FlemingThe physical limit is reported as 140e3813573SMatthew D Fleming.Va vm.memguard.phys_limit 141e3813573SMatthew D Flemingand the virtual space reserved for 142e3813573SMatthew D Fleming.Nm 143e3813573SMatthew D Flemingis reported as 144e3813573SMatthew D Fleming.Va vm.memguard.mapsize . 145e3813573SMatthew D Fleming.Pp 146e3813573SMatthew D Fleming.Nm 147e3813573SMatthew D Flemingwill not do page promotions for any allocation smaller than 148e3813573SMatthew D Fleming.Va vm.memguard.minsize 149e3813573SMatthew D Flemingbytes. 150e3813573SMatthew D FlemingThe default is 0, meaning all allocations can potentially be guarded. 151e3813573SMatthew D Fleming.Nm 152e3813573SMatthew D Flemingcan guard sufficiently large allocations randomly, with average 153e3813573SMatthew D Flemingfrequency of every one in 100000 / 154e3813573SMatthew D Fleming.Va vm.memguard.frequency 155e3813573SMatthew D Flemingallocations. 156e3813573SMatthew D FlemingThe default is 0, meaning no allocations are randomly guarded. 157e3813573SMatthew D Fleming.Pp 158e3813573SMatthew D Fleming.Nm 159e3813573SMatthew D Flemingcan optionally add unmapped guard pages around each allocation to 160e3813573SMatthew D Flemingdetect overflow and underflow, if 161e3813573SMatthew D Fleming.Va vm.memguard.options 162e3813573SMatthew D Fleminghas the 1 bit set. 163e3813573SMatthew D FlemingThis option is enabled by default. 164e3813573SMatthew D Fleming.Nm 165e3813573SMatthew D Flemingwill optionally guard all allocations of 166e3813573SMatthew D Fleming.Dv PAGE_SIZE 167e3813573SMatthew D Flemingor larger if 168e3813573SMatthew D Fleming.Va vm.memguard.options 169e3813573SMatthew D Fleminghas the 2 bit set. 170e3813573SMatthew D FlemingThis option is off by default. 1710e775b6dSGleb SmirnoffBy default 1720e775b6dSGleb Smirnoff.Nm 173*02e7c2cbSEnji Cooperdoes not guard 1740e775b6dSGleb Smirnoff.Xr uma 9 1750e775b6dSGleb Smirnoffzones that have been initialized with the 1760e775b6dSGleb Smirnoff.Dv UMA_ZONE_NOFREE 1770e775b6dSGleb Smirnoffflag set, since it can produce false positives on them. 1780e775b6dSGleb SmirnoffHowever, this safety measure can be turned off by setting bit 3 1790e775b6dSGleb Smirnoffof the 1800e775b6dSGleb Smirnoff.Va vm.memguard.options 1810e775b6dSGleb Smirnofftunable. 182f8cd5f23SChristian Brueffer.Sh SEE ALSO 183f8cd5f23SChristian Brueffer.Xr sysctl 8 , 184f8cd5f23SChristian Brueffer.Xr vmstat 8 , 185f8cd5f23SChristian Brueffer.Xr contigmalloc 9 , 186b88c6e87SChristian Brueffer.Xr malloc 9 , 1870e775b6dSGleb Smirnoff.Xr redzone 9 , 1880e775b6dSGleb Smirnoff.Xr uma 9 189f8cd5f23SChristian Brueffer.Sh HISTORY 190f8cd5f23SChristian Brueffer.Nm 191f8cd5f23SChristian Bruefferfirst appeared in 192f8cd5f23SChristian Brueffer.Fx 6.0 . 193f8cd5f23SChristian Brueffer.Sh AUTHORS 194f8cd5f23SChristian Brueffer.An -nosplit 195f8cd5f23SChristian Brueffer.Nm 196e3813573SMatthew D Flemingwas originally written by 1978a7314fcSBaptiste Daroussin.An Bosko Milekic Aq Mt bmilekic@FreeBSD.org . 198e3813573SMatthew D FlemingThis manual page was originally written by 1998a7314fcSBaptiste Daroussin.An Christian Brueffer Aq Mt brueffer@FreeBSD.org . 200e3813573SMatthew D FlemingAdditions have been made by 2018a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org 2020e775b6dSGleb Smirnoffand 2038a7314fcSBaptiste Daroussin.An Gleb Smirnoff Aq Mt glebius@FreeBSD.org 204e3813573SMatthew D Flemingto both the implementation and the documentation. 205