3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * MemGuard is a simple replacement allocator for debugging only
32 * which provides ElectricFence-style memory barrier protection on
33 * objects being allocated, and is used to detect tampering-after-free
36 * See the memguard(9) man page for more information on using MemGuard.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/types.h>
43 #include <sys/queue.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_extern.h>
54 #include <vm/memguard.h>
57 * The maximum number of pages allowed per allocation. If you're using
58 * MemGuard to override very large items (> MAX_PAGES_PER_ITEM in size),
59 * you need to increase MAX_PAGES_PER_ITEM.
61 #define MAX_PAGES_PER_ITEM 64
63 SYSCTL_NODE(_vm, OID_AUTO, memguard, CTLFLAG_RW, NULL, "MemGuard data");
65 * The vm_memguard_divisor variable controls how much of kmem_map should be
66 * reserved for MemGuard.
68 u_int vm_memguard_divisor;
69 SYSCTL_UINT(_vm_memguard, OID_AUTO, divisor, CTLFLAG_RD, &vm_memguard_divisor,
70 0, "(kmem_size/memguard_divisor) == memguard submap size");
73 * Short description (ks_shortdesc) of memory type to monitor.
75 static char vm_memguard_desc[128] = "";
76 static struct malloc_type *vm_memguard_mtype = NULL;
77 TUNABLE_STR("vm.memguard.desc", vm_memguard_desc, sizeof(vm_memguard_desc));
79 memguard_sysctl_desc(SYSCTL_HANDLER_ARGS)
81 struct malloc_type_internal *mtip;
82 struct malloc_type_stats *mtsp;
83 struct malloc_type *mtp;
88 strlcpy(desc, vm_memguard_desc, sizeof(desc));
89 error = sysctl_handle_string(oidp, desc, sizeof(desc), req);
90 if (error != 0 || req->newptr == NULL)
94 * We can change memory type when no memory has been allocated for it
95 * or when there is no such memory type yet (ie. it will be loaded with
99 mtx_lock(&malloc_mtx);
100 mtp = malloc_desc2type(desc);
102 mtip = mtp->ks_handle;
103 for (i = 0; i < MAXCPU; i++) {
104 mtsp = &mtip->mti_stats[i];
105 bytes += mtsp->mts_memalloced;
106 bytes -= mtsp->mts_memfreed;
113 * If mtp is NULL, it will be initialized in memguard_cmp().
115 vm_memguard_mtype = mtp;
116 strlcpy(vm_memguard_desc, desc, sizeof(vm_memguard_desc));
118 mtx_unlock(&malloc_mtx);
121 SYSCTL_PROC(_vm_memguard, OID_AUTO, desc, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
122 memguard_sysctl_desc, "A", "Short description of memory type to monitor");
125 * Global MemGuard data.
127 static vm_map_t memguard_map;
128 static unsigned long memguard_mapsize;
129 static unsigned long memguard_mapused;
130 struct memguard_entry {
131 STAILQ_ENTRY(memguard_entry) entries;
134 static struct memguard_fifo {
135 struct memguard_entry *stqh_first;
136 struct memguard_entry **stqh_last;
138 } memguard_fifo_pool[MAX_PAGES_PER_ITEM];
143 static void memguard_guard(void *addr, int numpgs);
144 static void memguard_unguard(void *addr, int numpgs);
145 static struct memguard_fifo *vtomgfifo(vm_offset_t va);
146 static void vsetmgfifo(vm_offset_t va, struct memguard_fifo *mgfifo);
147 static void vclrmgfifo(vm_offset_t va);
150 * Local macros. MemGuard data is global, so replace these with whatever
151 * your system uses to protect global data (if it is kernel-level
152 * parallelized). This is for porting among BSDs.
154 #define MEMGUARD_CRIT_SECTION_DECLARE static struct mtx memguard_mtx
155 #define MEMGUARD_CRIT_SECTION_INIT \
156 mtx_init(&memguard_mtx, "MemGuard mtx", NULL, MTX_DEF)
157 #define MEMGUARD_CRIT_SECTION_ENTER mtx_lock(&memguard_mtx)
158 #define MEMGUARD_CRIT_SECTION_EXIT mtx_unlock(&memguard_mtx)
159 MEMGUARD_CRIT_SECTION_DECLARE;
162 * Initialize the MemGuard mock allocator. All objects from MemGuard come
163 * out of a single VM map (contiguous chunk of address space).
166 memguard_init(vm_map_t parent_map, unsigned long size)
171 /* size must be multiple of PAGE_SIZE */
176 memguard_map = kmem_suballoc(parent_map, (vm_offset_t *)&base,
177 (vm_offset_t *)&limit, (vm_size_t)size, FALSE);
178 memguard_map->system_map = 1;
179 memguard_mapsize = size;
180 memguard_mapused = 0;
182 MEMGUARD_CRIT_SECTION_INIT;
183 MEMGUARD_CRIT_SECTION_ENTER;
184 for (i = 0; i < MAX_PAGES_PER_ITEM; i++) {
185 STAILQ_INIT(&memguard_fifo_pool[i]);
186 memguard_fifo_pool[i].index = i;
188 MEMGUARD_CRIT_SECTION_EXIT;
190 printf("MEMGUARD DEBUGGING ALLOCATOR INITIALIZED:\n");
191 printf("\tMEMGUARD map base: %p\n", base);
192 printf("\tMEMGUARD map limit: %p\n", limit);
193 printf("\tMEMGUARD map size: %ld (Bytes)\n", size);
197 * Allocate a single object of specified size with specified flags (either
198 * M_WAITOK or M_NOWAIT).
201 memguard_alloc(unsigned long size, int flags)
204 struct memguard_entry *e = NULL;
207 numpgs = size / PAGE_SIZE;
208 if ((size % PAGE_SIZE) != 0)
210 if (numpgs > MAX_PAGES_PER_ITEM)
211 panic("MEMGUARD: You must increase MAX_PAGES_PER_ITEM " \
212 "in memguard.c (requested: %d pages)", numpgs);
217 * If we haven't exhausted the memguard_map yet, allocate from
218 * it and grab a new page, even if we have recycled pages in our
219 * FIFO. This is because we wish to allow recycled pages to live
220 * guarded in the FIFO for as long as possible in order to catch
221 * even very late tamper-after-frees, even though it means that
222 * we end up wasting more memory, this is only a DEBUGGING allocator
225 MEMGUARD_CRIT_SECTION_ENTER;
226 if (memguard_mapused >= memguard_mapsize) {
227 e = STAILQ_FIRST(&memguard_fifo_pool[numpgs - 1]);
229 STAILQ_REMOVE(&memguard_fifo_pool[numpgs - 1], e,
230 memguard_entry, entries);
231 MEMGUARD_CRIT_SECTION_EXIT;
234 memguard_unguard(obj, numpgs);
236 bzero(obj, PAGE_SIZE * numpgs);
239 MEMGUARD_CRIT_SECTION_EXIT;
240 if (flags & M_WAITOK)
241 panic("MEMGUARD: Failed with M_WAITOK: " \
242 "memguard_map too small");
245 memguard_mapused += (PAGE_SIZE * numpgs);
246 MEMGUARD_CRIT_SECTION_EXIT;
248 obj = (void *)kmem_malloc(memguard_map, PAGE_SIZE * numpgs, flags);
250 vsetmgfifo((vm_offset_t)obj, &memguard_fifo_pool[numpgs - 1]);
252 bzero(obj, PAGE_SIZE * numpgs);
254 MEMGUARD_CRIT_SECTION_ENTER;
255 memguard_mapused -= (PAGE_SIZE * numpgs);
256 MEMGUARD_CRIT_SECTION_EXIT;
262 * Free specified single object.
265 memguard_free(void *addr)
267 struct memguard_entry *e;
268 struct memguard_fifo *mgfifo;
272 addr = (void *)trunc_page((unsigned long)addr);
275 * Page should not be guarded by now, so force a write.
276 * The purpose of this is to increase the likelihood of catching a
277 * double-free, but not necessarily a tamper-after-free (the second
278 * thread freeing might not write before freeing, so this forces it
279 * to and, subsequently, trigger a fault).
281 temp = (int *)((unsigned long)addr + (PAGE_SIZE/2)); /* in page */
284 mgfifo = vtomgfifo((vm_offset_t)addr);
286 memguard_guard(addr, idx + 1);
287 e = malloc(sizeof(struct memguard_entry), M_TEMP, M_NOWAIT);
289 MEMGUARD_CRIT_SECTION_ENTER;
290 memguard_mapused -= (PAGE_SIZE * (idx + 1));
291 MEMGUARD_CRIT_SECTION_EXIT;
292 memguard_unguard(addr, idx + 1); /* just in case */
293 vclrmgfifo((vm_offset_t)addr);
294 kmem_free(memguard_map, (vm_offset_t)addr,
295 PAGE_SIZE * (idx + 1));
299 MEMGUARD_CRIT_SECTION_ENTER;
300 STAILQ_INSERT_TAIL(mgfifo, e, entries);
301 MEMGUARD_CRIT_SECTION_EXIT;
305 memguard_cmp(struct malloc_type *mtp)
310 * The safest way of comparsion is to always compare short description
311 * string of memory type, but it is also the slowest way.
313 return (strcmp(mtp->ks_shortdesc, vm_memguard_desc) == 0);
316 * If we compare pointers, there are two possible problems:
317 * 1. Memory type was unloaded and new memory type was allocated at the
319 * 2. Memory type was unloaded and loaded again, but allocated at a
322 if (vm_memguard_mtype != NULL)
323 return (mtp == vm_memguard_mtype);
324 if (strcmp(mtp->ks_shortdesc, vm_memguard_desc) == 0) {
325 vm_memguard_mtype = mtp;
333 * Guard a page containing specified object (make it read-only so that
334 * future writes to it fail).
337 memguard_guard(void *addr, int numpgs)
339 void *a = (void *)trunc_page((unsigned long)addr);
340 if (vm_map_protect(memguard_map, (vm_offset_t)a,
341 (vm_offset_t)((unsigned long)a + (PAGE_SIZE * numpgs)),
342 VM_PROT_READ, FALSE) != KERN_SUCCESS)
343 panic("MEMGUARD: Unable to guard page!");
347 * Unguard a page containing specified object (make it read-and-write to
348 * allow full data access).
351 memguard_unguard(void *addr, int numpgs)
353 void *a = (void *)trunc_page((unsigned long)addr);
354 if (vm_map_protect(memguard_map, (vm_offset_t)a,
355 (vm_offset_t)((unsigned long)a + (PAGE_SIZE * numpgs)),
356 VM_PROT_DEFAULT, FALSE) != KERN_SUCCESS)
357 panic("MEMGUARD: Unable to unguard page!");
361 * vtomgfifo() converts a virtual address of the first page allocated for
362 * an item to a memguard_fifo_pool reference for the corresponding item's
365 * vsetmgfifo() sets a reference in an underlying page for the specified
366 * virtual address to an appropriate memguard_fifo_pool.
368 * These routines are very similar to those defined by UMA in uma_int.h.
369 * The difference is that these routines store the mgfifo in one of the
370 * page's fields that is unused when the page is wired rather than the
371 * object field, which is used.
373 static struct memguard_fifo *
374 vtomgfifo(vm_offset_t va)
377 struct memguard_fifo *mgfifo;
379 p = PHYS_TO_VM_PAGE(pmap_kextract(va));
380 KASSERT(p->wire_count != 0 && p->queue == PQ_NONE,
381 ("MEMGUARD: Expected wired page in vtomgfifo!"));
382 mgfifo = (struct memguard_fifo *)p->pageq.tqe_next;
387 vsetmgfifo(vm_offset_t va, struct memguard_fifo *mgfifo)
391 p = PHYS_TO_VM_PAGE(pmap_kextract(va));
392 KASSERT(p->wire_count != 0 && p->queue == PQ_NONE,
393 ("MEMGUARD: Expected wired page in vsetmgfifo!"));
394 p->pageq.tqe_next = (vm_page_t)mgfifo;
397 static void vclrmgfifo(vm_offset_t va)
401 p = PHYS_TO_VM_PAGE(pmap_kextract(va));
402 KASSERT(p->wire_count != 0 && p->queue == PQ_NONE,
403 ("MEMGUARD: Expected wired page in vclrmgfifo!"));
404 p->pageq.tqe_next = NULL;