Skip to content

Commit 9710107

Browse files
committed
Fix previous commit 8bda0af
Fix #294 Previous commit was incomplete.
1 parent 8bda0af commit 9710107

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

sys/windivert.c

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* windivert.c
3-
* (C) 2021, all rights reserved,
3+
* (C) 2022, all rights reserved,
44
*
55
* This file is part of WinDivert.
66
*
@@ -122,7 +122,7 @@ typedef enum
122122
WINDIVERT_CONTEXT_STATE_OPENING = 0xA0, // Context is opening.
123123
WINDIVERT_CONTEXT_STATE_OPEN = 0xB1, // Context is open.
124124
WINDIVERT_CONTEXT_STATE_CLOSING = 0xC2, // Context is closing.
125-
WINDIVERT_CONTEXT_STATE_CLOSED = 0xD3 // Context is closed.
125+
WINDIVERT_CONTEXT_STATE_CLOSED = 0xD3, // Context is closed.
126126
} context_state_t;
127127
struct context_s
128128
{
@@ -333,7 +333,7 @@ extern VOID windivert_worker(IN WDFWORKITEM item);
333333
static void windivert_read_service(context_t context);
334334
extern VOID windivert_create(IN WDFDEVICE device, IN WDFREQUEST request,
335335
IN WDFFILEOBJECT object);
336-
static NTSTATUS windivert_install_provider();
336+
static NTSTATUS windivert_install_provider(void);
337337
static NTSTATUS windivert_install_sublayer(layer_t layer);
338338
static NTSTATUS windivert_install_callouts(context_t context, UINT8 layer,
339339
UINT64 flags);
@@ -1165,12 +1165,14 @@ extern NTSTATUS DriverEntry(IN PDRIVER_OBJECT driver_obj,
11651165
if (!NT_SUCCESS(status))
11661166
{
11671167
DEBUG_ERROR("failed to begin WFP transaction", status);
1168+
FwpmTransactionAbort0(engine_handle);
11681169
goto driver_entry_exit;
11691170
}
11701171
status = windivert_install_provider();
11711172
if (!NT_SUCCESS(status))
11721173
{
11731174
DEBUG_ERROR("failed to install provider", status);
1175+
FwpmTransactionAbort0(engine_handle);
11741176
goto driver_entry_exit;
11751177
}
11761178
status = windivert_install_sublayer(WINDIVERT_LAYER_INBOUND_NETWORK_IPV4);
@@ -1282,6 +1284,7 @@ extern NTSTATUS DriverEntry(IN PDRIVER_OBJECT driver_obj,
12821284
if (!NT_SUCCESS(status))
12831285
{
12841286
DEBUG_ERROR("failed to commit WFP transaction", status);
1287+
FwpmTransactionAbort0(engine_handle);
12851288
goto driver_entry_exit;
12861289
}
12871290

@@ -1358,6 +1361,7 @@ static void windivert_driver_unload(void)
13581361
if (!NT_SUCCESS(status))
13591362
{
13601363
DEBUG_ERROR("failed to begin WFP transaction", status);
1364+
FwpmTransactionAbort0(engine_handle);
13611365
FwpmEngineClose0(engine_handle);
13621366
return;
13631367
}
@@ -1408,6 +1412,7 @@ static void windivert_driver_unload(void)
14081412
status = FwpmTransactionCommit0(engine_handle);
14091413
if (!NT_SUCCESS(status))
14101414
{
1415+
FwpmTransactionAbort0(engine_handle);
14111416
DEBUG_ERROR("failed to commit WFP transaction", status);
14121417
}
14131418
FwpmEngineClose0(engine_handle);
@@ -1567,7 +1572,7 @@ extern VOID windivert_create(IN WDFDEVICE device, IN WDFREQUEST request,
15671572
// Clean-up on error:
15681573
if (!NT_SUCCESS(status))
15691574
{
1570-
context->state = WINDIVERT_CONTEXT_STATE_INVALID;
1575+
context->state = WINDIVERT_CONTEXT_STATE_CLOSED;
15711576
if (context->read_queue != NULL)
15721577
{
15731578
WdfObjectDelete(context->read_queue);
@@ -1576,14 +1581,7 @@ extern VOID windivert_create(IN WDFDEVICE device, IN WDFREQUEST request,
15761581
{
15771582
WdfObjectDelete(context->worker);
15781583
}
1579-
if (context->process != NULL)
1580-
{
1581-
ObDereferenceObject(context->process);
1582-
}
1583-
if (context->engine_handle != NULL)
1584-
{
1585-
FwpmEngineClose0(context->engine_handle);
1586-
}
1584+
// process/engine_handle handled by windivert_destroy()
15871585
}
15881586

15891587
WdfRequestComplete(request, status);
@@ -1602,15 +1600,15 @@ static NTSTATUS windivert_install_callouts(context_t context, UINT8 layer,
16021600
accept, close;
16031601
NTSTATUS status = STATUS_SUCCESS;
16041602

1605-
inbound = ((flags & WINDIVERT_FILTER_FLAG_INBOUND) != 0);
1606-
outbound = ((flags & WINDIVERT_FILTER_FLAG_OUTBOUND) != 0);
1607-
ipv4 = ((flags & WINDIVERT_FILTER_FLAG_IP) != 0);
1608-
ipv6 = ((flags & WINDIVERT_FILTER_FLAG_IPV6) != 0);
1609-
bind = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_BIND) != 0);
1610-
connect = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_CONNECT) != 0);
1611-
listen = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_LISTEN) != 0);
1612-
accept = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_ACCEPT) != 0);
1613-
close = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_CLOSE) != 0);
1603+
inbound = ((flags & WINDIVERT_FILTER_FLAG_INBOUND) != 0);
1604+
outbound = ((flags & WINDIVERT_FILTER_FLAG_OUTBOUND) != 0);
1605+
ipv4 = ((flags & WINDIVERT_FILTER_FLAG_IP) != 0);
1606+
ipv6 = ((flags & WINDIVERT_FILTER_FLAG_IPV6) != 0);
1607+
bind = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_BIND) != 0);
1608+
connect = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_CONNECT) != 0);
1609+
listen = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_LISTEN) != 0);
1610+
accept = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_ACCEPT) != 0);
1611+
close = ((flags & WINDIVERT_FILTER_FLAG_EVENT_SOCKET_CLOSE) != 0);
16141612

16151613
i = 0;
16161614
switch (layer)
@@ -1802,8 +1800,7 @@ static NTSTATUS windivert_install_callout(context_t context, UINT idx,
18021800
if (!NT_SUCCESS(status))
18031801
{
18041802
DEBUG_ERROR("failed to begin WFP transaction", status);
1805-
FwpsCalloutUnregisterByKey0(&callout_guid);
1806-
return status;
1803+
goto windivert_install_callout_error;
18071804
}
18081805
status = FwpmCalloutAdd0(engine, &mcallout, NULL, NULL);
18091806
if (!NT_SUCCESS(status))
@@ -1821,8 +1818,7 @@ static NTSTATUS windivert_install_callout(context_t context, UINT idx,
18211818
if (!NT_SUCCESS(status))
18221819
{
18231820
DEBUG_ERROR("failed to commit WFP transaction", status);
1824-
FwpsCalloutUnregisterByKey0(&callout_guid);
1825-
return status;
1821+
goto windivert_install_callout_error;
18261822
}
18271823

18281824
KeAcquireInStackQueuedSpinLock(&context->lock, &lock_handle);
@@ -1877,6 +1873,7 @@ static void windivert_uninstall_callouts(context_t context,
18771873
// RPC handle was closed first. So, this path is "normal" if
18781874
// the user's app crashed or never closed the WinDivert handle.
18791875
DEBUG_ERROR("failed to begin WFP transaction", status);
1876+
FwpmTransactionAbort0(engine);
18801877
goto windivert_uninstall_callouts_unregister;
18811878
}
18821879
for (i = 0; i < WINDIVERT_CONTEXT_MAXLAYERS; i++)
@@ -1921,6 +1918,7 @@ static void windivert_uninstall_callouts(context_t context,
19211918
if (!NT_SUCCESS(status))
19221919
{
19231920
DEBUG_ERROR("failed to commit WFP transaction", status);
1921+
FwpmTransactionAbort0(engine);
19241922
// continue
19251923
}
19261924

@@ -2103,9 +2101,15 @@ extern VOID windivert_destroy(IN WDFOBJECT object)
21032101
filter = context->filter;
21042102
KeReleaseInStackQueuedSpinLock(&lock_handle);
21052103
windivert_uninstall_callouts(context, WINDIVERT_CONTEXT_STATE_CLOSED);
2106-
FwpmEngineClose0(context->engine_handle);
2104+
if (context->engine_handle != NULL)
2105+
{
2106+
FwpmEngineClose0(context->engine_handle);
2107+
}
21072108
windivert_free((PVOID)filter);
2108-
ObDereferenceObject(context->process);
2109+
if (context->process != NULL)
2110+
{
2111+
ObDereferenceObject(context->process);
2112+
}
21092113
}
21102114

21112115
/*

0 commit comments

Comments
 (0)