Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/KernelHeap.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,11 @@ void _ExFreePoolWithTracking(PVOID pointer, const char* FileName, int LineNumber
HEAP_VERBOSE("_ExFreePoolWithTracking: State info - heap handle: %p, TrackFreedMemory: %d",
state->HeapHandle, state->TrackFreedMemory);

if (state->TrackFreedMemory) {
EnterCriticalSection(&state->TrackingLock);
/* Hold TrackingLock across double-free check AND untrack to prevent TOCTOU.
* CRITICAL_SECTION is recursive, so UntrackAllocation can re-enter safely. */
EnterCriticalSection(&state->TrackingLock);

if (state->TrackFreedMemory) {
PLIST_ENTRY current = state->MemoryAllocations.Flink;
while (current != &state->MemoryAllocations) {
PMEMORY_TRACKING_ENTRY entry = CONTAINING_RECORD(current, MEMORY_TRACKING_ENTRY, ListEntry);
Expand All @@ -375,13 +377,13 @@ void _ExFreePoolWithTracking(PVOID pointer, const char* FileName, int LineNumber
}
HEAP_TRACE("_ExFreePoolWithTracking: No double-free detected for %p", pointer);
}

LeaveCriticalSection(&state->TrackingLock);
}

HEAP_TRACE("_ExFreePoolWithTracking: Calling UntrackAllocation for %p", pointer);
found = UntrackAllocation(pointer, FileName, LineNumber);

LeaveCriticalSection(&state->TrackingLock);

HEAP_VERBOSE("_ExFreePoolWithTracking: UntrackAllocation returned %d for %p", found, pointer);

if (!found && !state->SuppressErrors) {
Expand Down
Loading