Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Aug 14, 2023
1 parent be4fc46 commit 175aab3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pm_kext/src/pm_callouts.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ FWP_ACTION_TYPE classifySingle(
PortmasterPacketInfo* copiedPacketInfo = portmasterMalloc(sizeof(PortmasterPacketInfo), false);
if (!copiedPacketInfo) {
ERR("Insufficient Resources for allocating copiedPacketInfo");
// TODO: free other allocated memory.
portmasterFree(dentry);
// TODO: free other allocated memory?
return FWP_ACTION_NONE;
}
RtlCopyMemory(copiedPacketInfo, packetInfo, sizeof(PortmasterPacketInfo));
Expand All @@ -527,20 +528,22 @@ FWP_ACTION_TYPE classifySingle(
if (rc != 0) {
ERR("failed to add verdict: %d", rc);
portmasterFree(copiedPacketInfo);
// TODO: free other allocated memory.
portmasterFree(dentry);
// TODO: free other allocated memory?
return FWP_ACTION_NONE;
}

}
else {
} else {
// If not fast-tracked, copy the packet and register it.

//Inbound traffic requires special treatment - this bit shifting is a special source of error ;-)
if (packetInfo->direction == DIRECTION_INBOUND) {
status = NdisRetreatNetBufferDataStart(nb, ipHeaderSize, 0, NULL);
if (!NT_SUCCESS(status)) {
ERR("failed to retreat net buffer data start");
// TODO: free other allocated memory.
portmasterFree(copiedPacketInfo);
portmasterFree(dentry);
// TODO: free other allocated memory?
return FWP_ACTION_NONE;
}
}
Expand All @@ -549,7 +552,9 @@ FWP_ACTION_TYPE classifySingle(
status = copyPacketDataFromNB(nb, 0, &data, &dataLength);
if (!NT_SUCCESS(status)) {
ERR("copyPacketDataFromNB 2: %d", status);
// TODO: free other allocated memory.
portmasterFree(copiedPacketInfo);
portmasterFree(dentry);
// TODO: free other allocated memory?
return FWP_ACTION_NONE;
}
copiedPacketInfo->packetSize = (UINT32)dataLength;
Expand Down Expand Up @@ -1082,6 +1087,7 @@ void classifyALEOutboundIPv4(
packetInfo->flags |= PM_STATUS_SOCKET_AUTH;

if(wasPacketInjected(packetInfo, layerData)) {
portmasterFree(packetInfo);
return;
}

Expand Down Expand Up @@ -1152,6 +1158,7 @@ void classifyALEInboundIPv4(
packetInfo->flags |= PM_STATUS_SOCKET_AUTH;

if(wasPacketInjected(packetInfo, layerData)) {
portmasterFree(packetInfo);
return;
}

Expand Down Expand Up @@ -1232,6 +1239,7 @@ void classifyALEOutboundIPv6(
packetInfo->flags |= PM_STATUS_SOCKET_AUTH;

if(wasPacketInjected(packetInfo, layerData)) {
portmasterFree(packetInfo);
return;
}

Expand Down Expand Up @@ -1313,6 +1321,7 @@ void classifyALEInboundIPv6(
packetInfo->flags |= PM_STATUS_SOCKET_AUTH;

if(wasPacketInjected(packetInfo, layerData)) {
portmasterFree(packetInfo);
return;
}

Expand Down
5 changes: 5 additions & 0 deletions pm_kext/src/pm_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ NTSTATUS driverDeviceControl(__in PDEVICE_OBJECT pDeviceObject, __inout PIRP Irp
Irp->IoStatus.Information = size;
IoCompleteRequest(Irp, IO_NO_INCREMENT);

if(dentry->packet->processID != 0) {
// Packet comes from the ALE layer and it's not saved in cache. It's not needed anymore.
portmasterFree(dentry->packet);
}

// Now that the contents of the list-entry is copied, free memory
portmasterFree(dentry);
return STATUS_SUCCESS;
Expand Down

0 comments on commit 175aab3

Please sign in to comment.