xref: /freebsd/share/man/man9/memguard.9 (revision 02e7c2cbabe0c58c6c2f3e3572e8c9dcbc5543a7)
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*02e7c2cbSEnji Cooper.Dd March 22, 2017
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.
490e775b6dSGleb SmirnoffAlternatively
50e3813573SMatthew D Fleming.Nm
510e775b6dSGleb Smirnoffcan take over
520e775b6dSGleb Smirnoff.Fn uma_zalloc ,
530e775b6dSGleb Smirnoff.Fn uma_zalloc_arg
540e775b6dSGleb Smirnoffand
550e775b6dSGleb Smirnoff.Fn uma_free
560e775b6dSGleb Smirnofffor a single
57*02e7c2cbSEnji Cooper.Xr uma 9
580e775b6dSGleb Smirnoffzone.
590e775b6dSGleb SmirnoffAlso
600e775b6dSGleb Smirnoff.Nm
610e775b6dSGleb 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
850e775b6dSGleb Smirnoffcan be either a short description of the memory type to monitor,
860e775b6dSGleb Smirnoffeither name of
870e775b6dSGleb Smirnoff.Xr uma 9
880e775b6dSGleb 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
1020e775b6dSGleb Smirnoffeither
1030e775b6dSGleb Smirnoff.Xr free 9
1040e775b6dSGleb Smirnoffor
1050e775b6dSGleb Smirnoff.Xr uma_zfree 9 ,
1060e775b6dSGleb Smirnoffdepending on what kind of allocation was taken over.
107e3813573SMatthew D Fleming.Pp
1080e775b6dSGleb SmirnoffTo determine short description of a
109e3813573SMatthew D Fleming.Xr malloc 9
1100e775b6dSGleb Smirnofftype one can either take it from the first column of
1110e775b6dSGleb Smirnoff.Xr vmstat 8 Fl m
1120e775b6dSGleb Smirnoffoutput, or to find it in the kernel source.
1130e775b6dSGleb SmirnoffIt is the second argument to
1140e775b6dSGleb Smirnoff.Xr MALLOC_DEFINE 9
1150e775b6dSGleb Smirnoffmacro.
1160e775b6dSGleb SmirnoffTo determine name of
1170e775b6dSGleb Smirnoff.Xr uma 9
1180e775b6dSGleb Smirnoffzone one can either take it from the first column of
1190e775b6dSGleb Smirnoff.Xr vmstat 8 Fl z
1200e775b6dSGleb Smirnoffoutput, or to find it in the kernel source.
1210e775b6dSGleb SmirnoffIt is the first argument to the
1220e775b6dSGleb Smirnoff.Xr uma_zcreate 9
1230e775b6dSGleb 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
13244f1c916SBryan Drewery.Va vm_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.
1730e775b6dSGleb SmirnoffBy default
1740e775b6dSGleb Smirnoff.Nm
175*02e7c2cbSEnji Cooperdoes not guard
1760e775b6dSGleb Smirnoff.Xr uma 9
1770e775b6dSGleb Smirnoffzones that have been initialized with the
1780e775b6dSGleb Smirnoff.Dv UMA_ZONE_NOFREE
1790e775b6dSGleb Smirnoffflag set, since it can produce false positives on them.
1800e775b6dSGleb SmirnoffHowever, this safety measure can be turned off by setting bit 3
1810e775b6dSGleb Smirnoffof the
1820e775b6dSGleb Smirnoff.Va vm.memguard.options
1830e775b6dSGleb 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 ,
1890e775b6dSGleb Smirnoff.Xr redzone 9 ,
1900e775b6dSGleb 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
1998a7314fcSBaptiste Daroussin.An Bosko Milekic Aq Mt bmilekic@FreeBSD.org .
200e3813573SMatthew D FlemingThis manual page was originally written by
2018a7314fcSBaptiste Daroussin.An Christian Brueffer Aq Mt brueffer@FreeBSD.org .
202e3813573SMatthew D FlemingAdditions have been made by
2038a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org
2040e775b6dSGleb Smirnoffand
2058a7314fcSBaptiste Daroussin.An Gleb Smirnoff Aq Mt glebius@FreeBSD.org
206e3813573SMatthew D Flemingto both the implementation and the documentation.
207