@@ -668,18 +668,13 @@ WintunFreeAdapter(_In_ WINTUN_ADAPTER *Adapter)
668668_Return_type_success_ (return != NULL ) WINTUN_ADAPTER * WINAPI
669669 WintunOpenAdapter (_In_z_ const WCHAR * Pool , _In_z_ const WCHAR * Name )
670670{
671- if (!ElevateToSystem ())
672- {
673- LOG (WINTUN_LOG_ERR , L"Failed to impersonate SYSTEM user" );
674- return NULL ;
675- }
676671 DWORD LastError ;
677672 WINTUN_ADAPTER * Adapter = NULL ;
678673 HANDLE Mutex = NamespaceTakePoolMutex (Pool );
679674 if (!Mutex )
680675 {
681676 LastError = LOG (WINTUN_LOG_ERR , L"Failed to take %s pool mutex" , Pool );
682- goto cleanupToken ;
677+ goto cleanup ;
683678 }
684679
685680 HDEVINFO DevInfo = SetupDiGetClassDevsExW (& GUID_DEVCLASS_NET , NULL , NULL , DIGCF_PRESENT , NULL , NULL , NULL );
@@ -759,8 +754,7 @@ _Return_type_success_(return != NULL) WINTUN_ADAPTER *WINAPI
759754 SetupDiDestroyDeviceInfoList (DevInfo );
760755cleanupMutex :
761756 NamespaceReleaseMutex (Mutex );
762- cleanupToken :
763- RevertToSelf ();
757+ cleanup :
764758 SetLastError (LastError );
765759 return Adapter ;
766760}
@@ -1840,11 +1834,6 @@ _Return_type_success_(return != NULL) WINTUN_ADAPTER *WINAPI WintunCreateAdapter
18401834 _In_opt_ const GUID * RequestedGUID ,
18411835 _Out_opt_ BOOL * RebootRequired )
18421836{
1843- if (!ElevateToSystem ())
1844- {
1845- LOG (WINTUN_LOG_ERR , L"Failed to impersonate SYSTEM user" );
1846- return NULL ;
1847- }
18481837 BOOL DummyRebootRequired ;
18491838 if (!RebootRequired )
18501839 RebootRequired = & DummyRebootRequired ;
@@ -1855,12 +1844,11 @@ _Return_type_success_(return != NULL) WINTUN_ADAPTER *WINAPI WintunCreateAdapter
18551844 {
18561845 Adapter = CreateAdapterViaRundll32 (Pool , Name , RequestedGUID , RebootRequired );
18571846 LastError = Adapter ? ERROR_SUCCESS : GetLastError ();
1858- goto cleanupToken ;
1847+ goto cleanup ;
18591848 }
18601849 Adapter = CreateAdapter (Pool , Name , RequestedGUID , RebootRequired );
18611850 LastError = Adapter ? ERROR_SUCCESS : GetLastError ();
1862- cleanupToken :
1863- RevertToSelf ();
1851+ cleanup :
18641852 return RET_ERROR (Adapter , LastError );
18651853}
18661854
@@ -1869,11 +1857,6 @@ _Return_type_success_(return != FALSE) BOOL WINAPI WintunDeleteAdapter(
18691857 _In_ BOOL ForceCloseSessions ,
18701858 _Out_opt_ BOOL * RebootRequired )
18711859{
1872- if (!ElevateToSystem ())
1873- {
1874- LOG (WINTUN_LOG_ERR , L"Failed to impersonate SYSTEM user" );
1875- return FALSE;
1876- }
18771860 BOOL DummyRebootRequired ;
18781861 if (!RebootRequired )
18791862 RebootRequired = & DummyRebootRequired ;
@@ -1883,14 +1866,14 @@ _Return_type_success_(return != FALSE) BOOL WINAPI WintunDeleteAdapter(
18831866 {
18841867 LastError =
18851868 DeleteAdapterViaRundll32 (Adapter , ForceCloseSessions , RebootRequired ) ? ERROR_SUCCESS : GetLastError ();
1886- goto cleanupToken ;
1869+ goto cleanup ;
18871870 }
18881871
18891872 HANDLE Mutex = NamespaceTakePoolMutex (Adapter -> Pool );
18901873 if (!Mutex )
18911874 {
18921875 LastError = LOG (WINTUN_LOG_ERR , L"Failed to take %s pool mutex" , Adapter -> Pool );
1893- goto cleanupToken ;
1876+ goto cleanup ;
18941877 }
18951878
18961879 HDEVINFO DevInfo ;
@@ -1930,8 +1913,7 @@ _Return_type_success_(return != FALSE) BOOL WINAPI WintunDeleteAdapter(
19301913 SetupDiDestroyDeviceInfoList (DevInfo );
19311914cleanupMutex :
19321915 NamespaceReleaseMutex (Mutex );
1933- cleanupToken :
1934- RevertToSelf ();
1916+ cleanup :
19351917 return RET_ERROR (TRUE, LastError );
19361918}
19371919
@@ -1990,12 +1972,6 @@ static _Return_type_success_(return != FALSE) BOOL
19901972_Return_type_success_ (return != FALSE) BOOL WINAPI
19911973 WintunDeletePoolDriver (_In_z_ const WCHAR * Pool , _Out_opt_ BOOL * RebootRequired )
19921974{
1993- if (!ElevateToSystem ())
1994- {
1995- LOG (WINTUN_LOG_ERR , L"Failed to impersonate SYSTEM user" );
1996- return FALSE;
1997- }
1998-
19991975 BOOL DummyRebootRequired ;
20001976 if (!RebootRequired )
20011977 RebootRequired = & DummyRebootRequired ;
@@ -2005,20 +1981,20 @@ _Return_type_success_(return != FALSE) BOOL WINAPI
20051981 if (MAYBE_WOW64 && NativeMachine != IMAGE_FILE_PROCESS )
20061982 {
20071983 LastError = DeletePoolDriverViaRundll32 (Pool , RebootRequired ) ? ERROR_SUCCESS : GetLastError ();
2008- goto cleanupToken ;
1984+ goto cleanup ;
20091985 }
20101986
20111987 if (!DeleteAllOurAdapters (Pool , RebootRequired ))
20121988 {
20131989 LastError = GetLastError ();
2014- goto cleanupToken ;
1990+ goto cleanup ;
20151991 }
20161992
20171993 HANDLE DriverInstallationLock = NamespaceTakeDriverInstallationMutex ();
20181994 if (!DriverInstallationLock )
20191995 {
20201996 LastError = LOG (WINTUN_LOG_ERR , L"Failed to take driver installation mutex" );
2021- goto cleanupToken ;
1997+ goto cleanup ;
20221998 }
20231999 HDEVINFO DeviceInfoSet = SetupDiGetClassDevsW (& GUID_DEVCLASS_NET , NULL , NULL , 0 );
20242000 if (!DeviceInfoSet )
@@ -2060,25 +2036,19 @@ _Return_type_success_(return != FALSE) BOOL WINAPI
20602036 SetupDiDestroyDeviceInfoList (DeviceInfoSet );
20612037cleanupDriverInstallationLock :
20622038 NamespaceReleaseMutex (DriverInstallationLock );
2063- cleanupToken :
2064- RevertToSelf ();
2039+ cleanup :
20652040 return RET_ERROR (TRUE, LastError );
20662041}
20672042
20682043_Return_type_success_ (return != FALSE) BOOL WINAPI
20692044 WintunEnumAdapters (_In_z_ const WCHAR * Pool , _In_ WINTUN_ENUM_CALLBACK Func , _In_ LPARAM Param )
20702045{
2071- if (!ElevateToSystem ())
2072- {
2073- LOG (WINTUN_LOG_ERR , L"Failed to impersonate SYSTEM user" );
2074- return FALSE;
2075- }
20762046 DWORD LastError = ERROR_SUCCESS ;
20772047 HANDLE Mutex = NamespaceTakePoolMutex (Pool );
20782048 if (!Mutex )
20792049 {
20802050 LastError = LOG (WINTUN_LOG_ERR , L"Failed to take %s pool mutex" , Pool );
2081- goto cleanupToken ;
2051+ goto cleanup ;
20822052 }
20832053 HDEVINFO DevInfo = SetupDiGetClassDevsExW (& GUID_DEVCLASS_NET , NULL , NULL , DIGCF_PRESENT , NULL , NULL , NULL );
20842054 if (DevInfo == INVALID_HANDLE_VALUE )
@@ -2112,7 +2082,6 @@ _Return_type_success_(return != FALSE) BOOL WINAPI
21122082 SetupDiDestroyDeviceInfoList (DevInfo );
21132083cleanupMutex :
21142084 NamespaceReleaseMutex (Mutex );
2115- cleanupToken :
2116- RevertToSelf ();
2085+ cleanup :
21172086 return RET_ERROR (TRUE, LastError );
21182087}
0 commit comments