/usr/src/ndiswrapper-1.57/wrapmem.c is in ndiswrapper-dkms 1.57-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /*
* Copyright (C) 2006 Giridhar Pemmasani
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#define _WRAPMEM_C_
#include "ntoskernel.h"
#include "wrapmem.h"
struct slack_alloc_info {
struct nt_list list;
size_t size;
};
#if ALLOC_DEBUG > 1
static struct nt_list allocs;
#endif
static struct nt_list slack_allocs;
static spinlock_t alloc_lock;
#if ALLOC_DEBUG
const char *alloc_type_name[ALLOC_TYPE_MAX] = {
"kmalloc_atomic",
"kmalloc_nonatomic",
"vmalloc_atomic",
"vmalloc_nonatomic",
"kmalloc_slack",
"pages"
};
struct alloc_info {
enum alloc_type type;
size_t size;
#if ALLOC_DEBUG > 1
struct nt_list list;
const char *file;
int line;
ULONG tag;
#endif
};
static atomic_t alloc_sizes[ALLOC_TYPE_MAX];
#endif
/* allocate memory and add it to list of allocated pointers; if a
* driver doesn't free this memory for any reason (buggy driver or we
* allocate space behind driver's back since we need more space than
* corresponding Windows structure provides etc.), this gets freed
* automatically when module is unloaded
*/
void *slack_kmalloc(size_t size)
{
struct slack_alloc_info *info;
ENTER4("size = %zu", size);
info = kmalloc(size + sizeof(*info), irql_gfp());
if (!info)
return NULL;
info->size = size;
spin_lock_bh(&alloc_lock);
InsertTailList(&slack_allocs, &info->list);
spin_unlock_bh(&alloc_lock);
#if ALLOC_DEBUG
atomic_add(size, &alloc_sizes[ALLOC_TYPE_SLACK]);
#endif
TRACE4("%p, %p", info, info + 1);
EXIT4(return info + 1);
}
/* free pointer and remove from list of allocated pointers */
void slack_kfree(void *ptr)
{
struct slack_alloc_info *info;
ENTER4("%p", ptr);
info = ptr - sizeof(*info);
spin_lock_bh(&alloc_lock);
RemoveEntryList(&info->list);
spin_unlock_bh(&alloc_lock);
#if ALLOC_DEBUG
atomic_sub(info->size, &alloc_sizes[ALLOC_TYPE_SLACK]);
#endif
kfree(info);
EXIT4(return);
}
void *slack_kzalloc(size_t size)
{
void *ptr = slack_kmalloc(size);
if (ptr)
memset(ptr, 0, size);
return ptr;
}
#if ALLOC_DEBUG
void *wrap_kmalloc(size_t size, gfp_t flags, const char *file, int line)
{
struct alloc_info *info;
info = kmalloc(size + sizeof(*info), flags);
if (!info)
return NULL;
if (flags & GFP_ATOMIC)
info->type = ALLOC_TYPE_KMALLOC_ATOMIC;
else
info->type = ALLOC_TYPE_KMALLOC_NON_ATOMIC;
info->size = size;
atomic_add(size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
info->file = file;
info->line = line;
info->tag = 0;
spin_lock_bh(&alloc_lock);
InsertTailList(&allocs, &info->list);
spin_unlock_bh(&alloc_lock);
#endif
TRACE4("%p", info + 1);
return info + 1;
}
void *wrap_kzalloc(size_t size, gfp_t flags, const char *file, int line)
{
void *ptr = wrap_kmalloc(size, flags, file, line);
if (ptr)
memset(ptr, 0, size);
return ptr;
}
void wrap_kfree(void *ptr)
{
struct alloc_info *info;
TRACE4("%p", ptr);
if (!ptr)
return;
info = ptr - sizeof(*info);
atomic_sub(info->size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
spin_lock_bh(&alloc_lock);
RemoveEntryList(&info->list);
spin_unlock_bh(&alloc_lock);
if (!(info->type == ALLOC_TYPE_KMALLOC_ATOMIC ||
info->type == ALLOC_TYPE_KMALLOC_NON_ATOMIC)) {
WARNING("invalid type: %d", info->type);
return;
}
#endif
kfree(info);
}
void *wrap_vmalloc(unsigned long size, const char *file, int line)
{
struct alloc_info *info;
info = vmalloc(size + sizeof(*info));
if (!info)
return NULL;
info->type = ALLOC_TYPE_VMALLOC_NON_ATOMIC;
info->size = size;
atomic_add(size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
info->file = file;
info->line = line;
info->tag = 0;
spin_lock_bh(&alloc_lock);
InsertTailList(&allocs, &info->list);
spin_unlock_bh(&alloc_lock);
#endif
return info + 1;
}
void *wrap__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
const char *file, int line)
{
struct alloc_info *info;
info = __vmalloc(size + sizeof(*info), gfp_mask, prot);
if (!info)
return NULL;
if (gfp_mask & GFP_ATOMIC)
info->type = ALLOC_TYPE_VMALLOC_ATOMIC;
else
info->type = ALLOC_TYPE_VMALLOC_NON_ATOMIC;
info->size = size;
atomic_add(size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
info->file = file;
info->line = line;
info->tag = 0;
spin_lock_bh(&alloc_lock);
InsertTailList(&allocs, &info->list);
spin_unlock_bh(&alloc_lock);
#endif
return info + 1;
}
void wrap_vfree(void *ptr)
{
struct alloc_info *info;
info = ptr - sizeof(*info);
atomic_sub(info->size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
spin_lock_bh(&alloc_lock);
RemoveEntryList(&info->list);
spin_unlock_bh(&alloc_lock);
if (!(info->type == ALLOC_TYPE_VMALLOC_ATOMIC ||
info->type == ALLOC_TYPE_VMALLOC_NON_ATOMIC)) {
WARNING("invalid type: %d", info->type);
return;
}
#endif
vfree(info);
}
void *wrap_alloc_pages(gfp_t flags, unsigned int size,
const char *file, int line)
{
struct alloc_info *info;
size += sizeof(*info);
info = (struct alloc_info *)__get_free_pages(flags, get_order(size));
if (!info)
return NULL;
info->type = ALLOC_TYPE_PAGES;
info->size = size;
atomic_add(size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
info->file = file;
info->line = line;
info->tag = 0;
spin_lock_bh(&alloc_lock);
InsertTailList(&allocs, &info->list);
spin_unlock_bh(&alloc_lock);
#endif
return info + 1;
}
void wrap_free_pages(unsigned long ptr, int order)
{
struct alloc_info *info;
info = (void *)ptr - sizeof(*info);
atomic_sub(info->size, &alloc_sizes[info->type]);
#if ALLOC_DEBUG > 1
spin_lock_bh(&alloc_lock);
RemoveEntryList(&info->list);
spin_unlock_bh(&alloc_lock);
if (info->type != ALLOC_TYPE_PAGES) {
WARNING("invalid type: %d", info->type);
return;
}
#endif
free_pages((unsigned long)info, get_order(info->size));
}
#if ALLOC_DEBUG > 1
void *wrap_ExAllocatePoolWithTag(enum pool_type pool_type, SIZE_T size,
ULONG tag, const char *file, int line)
{
void *addr;
struct alloc_info *info;
ENTER4("pool_type: %d, size: %zu, tag: %u", pool_type, size, tag);
addr = (ExAllocatePoolWithTag)(pool_type, size, tag);
if (!addr)
return NULL;
info = addr - sizeof(*info);
info->file = file;
info->line = line;
info->tag = tag;
EXIT4(return addr);
}
#endif
int alloc_size(enum alloc_type type)
{
if ((int)type >= 0 && type < ALLOC_TYPE_MAX)
return atomic_read(&alloc_sizes[type]);
else
return -EINVAL;
}
#endif // ALLOC_DEBUG
int wrapmem_init(void)
{
#if ALLOC_DEBUG > 1
InitializeListHead(&allocs);
#endif
InitializeListHead(&slack_allocs);
spin_lock_init(&alloc_lock);
return 0;
}
void wrapmem_exit(void)
{
#if ALLOC_DEBUG
enum alloc_type type;
#endif
struct nt_list *ent;
/* free all pointers on the slack list */
while (1) {
struct slack_alloc_info *info;
spin_lock_bh(&alloc_lock);
ent = RemoveHeadList(&slack_allocs);
spin_unlock_bh(&alloc_lock);
if (!ent)
break;
info = container_of(ent, struct slack_alloc_info, list);
#if ALLOC_DEBUG
atomic_sub(info->size, &alloc_sizes[ALLOC_TYPE_SLACK]);
#endif
kfree(info);
}
#if ALLOC_DEBUG
for (type = 0; type < ALLOC_TYPE_MAX; type++) {
int n = atomic_read(&alloc_sizes[type]);
if (n)
WARNING("%d bytes of memory in %s leaking", n,
alloc_type_name[type]);
}
#if ALLOC_DEBUG > 1
while (1) {
struct alloc_info *info;
spin_lock_bh(&alloc_lock);
ent = RemoveHeadList(&allocs);
spin_unlock_bh(&alloc_lock);
if (!ent)
break;
info = container_of(ent, struct alloc_info, list);
atomic_sub(info->size, &alloc_sizes[ALLOC_TYPE_SLACK]);
printk(KERN_DEBUG DRIVER_NAME
": %s:%d leaked %zd bytes at %p (%s, tag 0x%08X)\n",
info->file, info->line, info->size, info + 1,
alloc_type_name[info->type], info->tag);
if (info->type == ALLOC_TYPE_KMALLOC_ATOMIC ||
info->type == ALLOC_TYPE_KMALLOC_NON_ATOMIC)
kfree(info);
else if (info->type == ALLOC_TYPE_VMALLOC_ATOMIC ||
info->type == ALLOC_TYPE_VMALLOC_NON_ATOMIC)
vfree(info);
else if (info->type == ALLOC_TYPE_PAGES)
free_pages((unsigned long)info, get_order(info->size));
else
WARNING("invalid type: %d; not freed", info->type);
}
#endif
#endif
return;
}
|