diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-10-12 18:44:42 +0000 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-10-12 18:55:24 +0000 |
commit | d8fe1419fb480ec5fc4dd0c4bc49f1d1a1a90663 (patch) | |
tree | 49461a6113fff49ecfb947a602d95ee91a373d2d /api/adapter.c | |
parent | api: rewrite based on SwDevice (diff) | |
download | wintun-d8fe1419fb480ec5fc4dd0c4bc49f1d1a1a90663.tar.xz wintun-d8fe1419fb480ec5fc4dd0c4bc49f1d1a1a90663.zip |
driver: automatically close long-lived handle
There's only one handle that's likely to be open in a long lived way:
the tun registration handle. So we can force that closed automatically
when the device is about to close, if it's been improperly left open.
Other handles will indeed hold up closing, but if those exist, they're a
sign of a larger bug elsewhere that should be addressed. On the other
hand, tun registration handles might legitimately be open during driver
upgrades. This also saves us the trouble of dereferencing a freed
FileObject as in the general case.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'api/adapter.c')
-rw-r--r-- | api/adapter.c | 43 |
1 files changed, 0 insertions, 43 deletions
diff --git a/api/adapter.c b/api/adapter.c index b71032a..6aa6c21 100644 --- a/api/adapter.c +++ b/api/adapter.c @@ -191,8 +191,6 @@ WintunCloseAdapter(WINTUN_ADAPTER *Adapter) if (!Adapter) return; Free(Adapter->InterfaceFilename); - if (Adapter->DevInfo) - AdapterForceCloseHandles(Adapter->DevInfo, &Adapter->DevInfoData); if (Adapter->SwDevice) SwDeviceClose(Adapter->SwDevice); if (Adapter->DevInfo) @@ -896,47 +894,6 @@ cleanup: return RET_ERROR(Adapter, LastError); } -#define TUN_IOCTL_FORCE_CLOSE_HANDLES CTL_CODE(51820U, 0x971U, METHOD_NEITHER, FILE_READ_DATA | FILE_WRITE_DATA) - -_Use_decl_annotations_ -BOOL -AdapterForceCloseHandles(HDEVINFO DevInfo, SP_DEVINFO_DATA *DevInfoData) -{ - DWORD LastError = ERROR_SUCCESS; - WCHAR InstanceId[MAX_INSTANCE_ID]; - DWORD RequiredChars = _countof(InstanceId); - if (!SetupDiGetDeviceInstanceIdW(DevInfo, DevInfoData, InstanceId, RequiredChars, &RequiredChars)) - { - LOG_LAST_ERROR(L"Failed to get adapter instance ID"); - return FALSE; - } - WINTUN_ADAPTER Adapter = { .InterfaceFilename = AdapterGetDeviceObjectFileName(InstanceId) }; - if (!Adapter.InterfaceFilename) - { - LOG_LAST_ERROR(L"Failed to get adapter file name"); - return FALSE; - } - HANDLE Handle = AdapterOpenDeviceObject(&Adapter); - Free(Adapter.InterfaceFilename); - if (Handle == INVALID_HANDLE_VALUE) - { - LastError = LOG(WINTUN_LOG_ERR, L"Failed to get adapter file object"); - return FALSE; - } - DWORD RequiredBytes; - if (DeviceIoControl(Handle, TUN_IOCTL_FORCE_CLOSE_HANDLES, NULL, 0, NULL, 0, &RequiredBytes, NULL)) - { - LastError = ERROR_SUCCESS; - Sleep(200); - } - else if (GetLastError() == ERROR_NOTHING_TO_TERMINATE) - LastError = ERROR_SUCCESS; - else - LastError = LOG_LAST_ERROR(L"Failed to perform force close ioctl"); - CloseHandle(Handle); - return RET_ERROR(TRUE, LastError); -} - _Use_decl_annotations_ BOOL AdapterRemoveInstance(HDEVINFO DevInfo, SP_DEVINFO_DATA *DevInfoData) |