fix possible TunDispatchSecurityDescriptor leak.
PATCH 1/3 fix possible TunDispatchSecurityDescriptor leak.
TunDispatchSecurityDescriptor will leak if second RtlAbsoluteToSelfRelativeSD fails. Practically this can't happen, but from wintun code it's unclear.
--- wintun.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
wintun.c +12 -3
@@ -820,6 +820,14 @@ static NTSTATUS TunInitializeDispatchSecurityDescriptor(VOID)return STATUS_SUCCESS ; } +static VOID TunFreeDispatchSecurityDescriptor (VOID ) +{ +if (!TunDispatchSecurityDescriptor) +return ; +ExFreePoolWithTag (TunDispatchSecurityDescriptor,TUN_MEMORY_TAG ); + TunDispatchSecurityDescriptor =NULL ; +} +_IRQL_requires_max_ (PASSIVE_LEVEL) static VOIDTunProcessNotification (HANDLE ParentId, HANDLE ProcessId, BOOLEAN Create)
@@ -1387,7 +1395,7 @@ TunUnload(PDRIVER_OBJECT DriverObject)NdisMDeregisterMiniportDriver (NdisMiniportDriverHandle);ExDeleteResourceLite (&TunDispatchCtxGuard);ExDeleteResourceLite (&TunDispatchDeviceListLock); -ExFreePoolWithTag (TunDispatchSecurityDescriptor,TUN_MEMORY_TAG ); +TunFreeDispatchSecurityDescriptor (); }DRIVER_INITIALIZE DriverEntry;
@@ -1398,7 +1406,7 @@ DriverEntry(DRIVER_OBJECT *DriverObject, UNICODE_STRING *RegistryPath)NTSTATUS Status;if (!NT_SUCCESS (Status =TunInitializeDispatchSecurityDescriptor ())) -return Status; + goto cleanupSD; NdisVersion =NdisGetVersion ();if (NdisVersion <NDIS_MINIPORT_VERSION_MIN )
@@ -1461,6 +1469,7 @@ cleanupNotifier: cleanupResources:ExDeleteResourceLite (&TunDispatchCtxGuard);ExDeleteResourceLite (&TunDispatchDeviceListLock); -ExFreePoolWithTag (TunDispatchSecurityDescriptor,TUN_MEMORY_TAG ); +cleanupSD: +TunFreeDispatchSecurityDescriptor ();return Status; }
Signature · 1 line
2.26.2.windows.1
PATCH 2/3 use ExEnterCriticalRegionAndAcquireResourceExclusive and ExReleaseResourceAndLeaveCriticalRegion
--- wintun.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
wintun.c +4 -8
@@ -884,15 +884,13 @@ TunDispatchDeviceControl(DEVICE_OBJECT *DeviceObject, IRP *Irp)switch (Stack->Parameters.DeviceIoControl.IoControlCode) {case TUN_IOCTL_REGISTER_RINGS : { -KeEnterCriticalRegion (); -ExAcquireResourceSharedLite (&TunDispatchCtxGuard, TRUE); +ExEnterCriticalRegionAndAcquireResourceExclusive (&TunDispatchCtxGuard);#pragma warning(suppress : 28175)TUN_CTX *Ctx = DeviceObject->Reserved; Status =NDIS_STATUS_ADAPTER_NOT_READY ;if (Ctx) Status =TunRegisterBuffers (Ctx, Irp); -ExReleaseResourceLite (&TunDispatchCtxGuard); -KeLeaveCriticalRegion (); +ExReleaseResourceAndLeaveCriticalRegion (&TunDispatchCtxGuard);break ; }case TUN_IOCTL_FORCE_CLOSE_HANDLES :
@@ -913,14 +911,12 @@ _Use_decl_annotations_static NTSTATUS TunDispatchClose (DEVICE_OBJECT *DeviceObject,IRP *Irp) { -KeEnterCriticalRegion (); -ExAcquireResourceSharedLite (&TunDispatchCtxGuard, TRUE); +ExEnterCriticalRegionAndAcquireResourceExclusive (&TunDispatchCtxGuard);#pragma warning(suppress : 28175)TUN_CTX *Ctx = DeviceObject->Reserved;if (Ctx)TunUnregisterBuffers (Ctx,IoGetCurrentIrpStackLocation (Irp)->FileObject); -ExReleaseResourceLite (&TunDispatchCtxGuard); -KeLeaveCriticalRegion (); +ExReleaseResourceAndLeaveCriticalRegion (&TunDispatchCtxGuard);return NdisDispatchClose (DeviceObject, Irp); }
Signature · 1 line
2.26.2.windows.1
Re: [PATCH 2/3] use ExEnterCriticalRegionAndAcquireResourceExclusive and ExReleaseResourceAndLeaveCriticalRegion
Looking back over this, the enter+acquire of the existing code is shared here, so replacing with exclusive will change behavior. For now, please ignore this patch.
Quoted 48 lines · Shawn Hoffman
Signed-off-by Shawn Hoffman <[email protected]>--- wintun.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)wintun.c +4
-9@@ -884,15 +884,13 @@ TunDispatchDeviceControl(DEVICE_OBJECT *DeviceObject, IRP *Irp)switch (Stack->Parameters.DeviceIoControl.IoControlCode) {case TUN_IOCTL_REGISTER_RINGS : { -KeEnterCriticalRegion (); -ExAcquireResourceSharedLite (&TunDispatchCtxGuard, TRUE); +ExEnterCriticalRegionAndAcquireResourceExclusive (&TunDispatchCtxGuard);#pragma warning(suppress : 28175)TUN_CTX *Ctx = DeviceObject->Reserved; Status =NDIS_STATUS_ADAPTER_NOT_READY ;if (Ctx) Status =TunRegisterBuffers (Ctx, Irp); -ExReleaseResourceLite (&TunDispatchCtxGuard); -KeLeaveCriticalRegion (); +ExReleaseResourceAndLeaveCriticalRegion (&TunDispatchCtxGuard);break ; }case TUN_IOCTL_FORCE_CLOSE_HANDLES :@@ -913,14 +911,12 @@ _Use_decl_annotations_static NTSTATUS TunDispatchClose (DEVICE_OBJECT *DeviceObject,IRP *Irp) { -KeEnterCriticalRegion (); -ExAcquireResourceSharedLite (&TunDispatchCtxGuard, TRUE); +ExEnterCriticalRegionAndAcquireResourceExclusive (&TunDispatchCtxGuard);#pragma warning(suppress : 28175)TUN_CTX *Ctx = DeviceObject->Reserved;if (Ctx)TunUnregisterBuffers (Ctx,IoGetCurrentIrpStackLocation (Irp)->FileObject); -ExReleaseResourceLite (&TunDispatchCtxGuard); -KeLeaveCriticalRegion (); +ExReleaseResourceAndLeaveCriticalRegion (&TunDispatchCtxGuard);return NdisDispatchClose (DeviceObject, Irp); } --2.26.2.windows.1
Re: [PATCH 2/3] use ExEnterCriticalRegionAndAcquireResourceExclusive and ExReleaseResourceAndLeaveCriticalRegion
Do you have a replacement for this or was it a botched idea?
PATCH 3/3 use RtlSubAuthoritySid instead of directly poking SID
In reply to Shawn Hoffman--- wintun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
wintun.c +1 -1
@@ -788,7 +788,7 @@ static NTSTATUS TunInitializeDispatchSecurityDescriptor(VOID)SID LocalSystem = {0 };if (!NT_SUCCESS (Status =RtlInitializeSid (&LocalSystem, &NtAuthority,1 )))return Status; - LocalSystem.SubAuthority[0 ] =18 ; + *RtlSubAuthoritySid (&LocalSystem,0 ) =SECURITY_LOCAL_SYSTEM_RID ;struct {ACL Dacl;
Signature · 1 line
2.26.2.windows.1
Jason A. Donenfeld
AppliedRe: [PATCH 3/3] use RtlSubAuthoritySid instead of directly poking SID
Applied, thanks.
Thanks for the report. Fixed slightly differently here
https://git.zx2c4.com/wintun/commit/?id=b19c7abd417e62dbb08a8d3aa4db96ce9cfd0830