sys/dev/misc/tbridge/safe_mem.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | /* * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>. * 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 COPYRIGHT HOLDERS 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 * COPYRIGHT HOLDERS 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 <sys/types.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/libkern.h> #include <sys/tbridge.h> MALLOC_DEFINE(M_SAFEMEM, "safemem", "safemem kernel port"); struct safe_mem_hdr { struct safe_mem_hdr *prev; struct safe_mem_hdr *next; struct safe_mem_tail *tail; const char *file; int line; size_t alloc_sz; char sig[8]; /* SAFEMEM */ }; struct safe_mem_tail { char sig[8]; /* SAFEMEM */ }; static struct safe_mem_hdr *safe_mem_hdr_first = NULL; static int safemem_error_count = 0; void safemem_reset_error_count(void) { safemem_error_count = 0; } int safemem_get_error_count(void) { return safemem_error_count; } void * _alloc_safe_mem(size_t req_sz, const char *file, int line) { struct safe_mem_hdr *hdr, *hdrp; struct safe_mem_tail *tail; size_t alloc_sz; char *mem, *user_mem; /* only shift, to make sure things (TM) keep aligned */ alloc_sz = req_sz; mem = kmalloc(alloc_sz << 2, M_SAFEMEM, M_WAITOK | M_ZERO); user_mem = mem + alloc_sz; hdr = (struct safe_mem_hdr *)(user_mem - sizeof(*hdr)); tail = (struct safe_mem_tail *)(user_mem + alloc_sz); strcpy(hdr->sig, "SAFEMEM"); strcpy(tail->sig, "SAFEMEM"); hdr->tail = tail; hdr->alloc_sz = alloc_sz; hdr->file = file; hdr->line = line; hdr->next = NULL; if (safe_mem_hdr_first == NULL) { safe_mem_hdr_first = hdr; } else { hdrp = safe_mem_hdr_first; while (hdrp->next != NULL) hdrp = hdrp->next; hdr->prev = hdrp; hdrp->next = hdr; } return user_mem; } void _free_safe_mem(void *mem_ptr, const char *file, int line) { struct safe_mem_hdr *hdr; struct safe_mem_tail *tail; size_t alloc_sz; char *mem = mem_ptr; char *user_mem = mem_ptr; hdr = (struct safe_mem_hdr *)(user_mem - sizeof(*hdr)); tail = (struct safe_mem_tail *)(user_mem + hdr->alloc_sz); mem -= hdr->alloc_sz; #ifdef DEBUG kprintf("freeing safe_mem (hdr): %#lx (%s:%d)\n", (unsigned long)(void *)hdr, hdr->file, hdr->line); #endif if (hdr->alloc_sz == 0) { tbridge_printf("SAFEMEM BUG: double-free at %s:%d !!!\n", file, line); return; } /* Integrity checks */ if ((memcmp(hdr->sig, "SAFEMEM\0", 8) != 0) || (memcmp(tail->sig, "SAFEMEM\0", 8) != 0)) { tbridge_printf("SAFEMEM BUG: safe_mem buffer under- or overflow " "at %s:%d !!!\n", file, line); return; } if (safe_mem_hdr_first == NULL) { tbridge_printf("SAFEMEM BUG: safe_mem list should not be empty " "at %s:%d !!!\n", file, line); return; } if (hdr->prev != NULL) hdr->prev->next = hdr->next; if (hdr->next != NULL) hdr->next->prev = hdr->prev; if (safe_mem_hdr_first == hdr) safe_mem_hdr_first = hdr->next; alloc_sz = hdr->alloc_sz; bzero(mem, alloc_sz << 2); kfree(mem, M_SAFEMEM); } void check_and_purge_safe_mem(void) { struct safe_mem_hdr *hdr; char *mem; #ifdef DEBUG int ok; #endif if (safe_mem_hdr_first == NULL) return; hdr = safe_mem_hdr_first; while ((hdr = safe_mem_hdr_first) != NULL) { #ifdef DEBUG if ((hdr->alloc_sz > 0) && (memcmp(hdr->sig, "SAFEMEM\0", 8) == 0) && (memcmp(hdr->tail->sig, "SAFEMEM\0", 8) == 0)) ok = 1; else ok = 0; kprintf("SAFEMEM: un-freed safe_mem: %#lx (%s:%d) [integrity=%s]\n", (unsigned long)(void *)hdr, hdr->file, hdr->line, ok? "ok" : "failed"); #endif mem = (void *)hdr; mem += sizeof(*hdr); _free_safe_mem(mem, "check_and_purge_safe_mem", 0); } } |