Close open service handles after use

Fixes handle leak pointed out in OSTIF/Quarkslab audit (finding 5.7)

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/162/head
Selva Nair 8 years ago
parent c3e4247e30
commit 225df6a756

@ -40,8 +40,8 @@ extern options_t o;
int MyStartService() int MyStartService()
{ {
SC_HANDLE schSCManager; SC_HANDLE schSCManager = NULL;
SC_HANDLE schService; SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus; SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint; DWORD dwOldCheckPoint;
DWORD dwStartTickCount; DWORD dwStartTickCount;
@ -145,9 +145,6 @@ int MyStartService()
} }
} }
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
if (ssStatus.dwCurrentState != SERVICE_RUNNING) if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{ {
/* service hasn't started */ /* service hasn't started */
@ -167,10 +164,15 @@ int MyStartService()
/* Show "OpenVPN Service Started" Tray Balloon msg */ /* Show "OpenVPN Service Started" Tray Balloon msg */
ShowTrayBalloon(LoadLocalizedString(IDS_NFO_SERVICE_STARTED), _T("")); ShowTrayBalloon(LoadLocalizedString(IDS_NFO_SERVICE_STARTED), _T(""));
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return(true); return(true);
failed: failed:
if (schService)
CloseServiceHandle(schService);
if (schSCManager)
CloseServiceHandle(schSCManager);
/* Set Service Status = Disconnecting */ /* Set Service Status = Disconnecting */
o.service_state = service_disconnected; o.service_state = service_disconnected;
SetServiceMenuStatus(); SetServiceMenuStatus();
@ -181,10 +183,11 @@ failed:
int MyStopService() int MyStopService()
{ {
SC_HANDLE schSCManager; SC_HANDLE schSCManager = NULL;
SC_HANDLE schService; SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus; SERVICE_STATUS ssStatus;
int i; int i;
BOOL ret = false;
// Open a handle to the SC Manager database. // Open a handle to the SC Manager database.
schSCManager = OpenSCManager( schSCManager = OpenSCManager(
@ -206,7 +209,7 @@ int MyStopService()
if (schService == NULL) { if (schService == NULL) {
/* can't open vpn service */ /* can't open vpn service */
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
return(false); goto out;
} }
/* Run DisConnect script */ /* Run DisConnect script */
@ -220,13 +223,20 @@ int MyStopService()
{ {
/* stop failed */ /* stop failed */
ShowLocalizedMsg(IDS_ERR_STOP_SERVICE); ShowLocalizedMsg(IDS_ERR_STOP_SERVICE);
return(false); goto out;
} }
o.service_state = service_disconnected; o.service_state = service_disconnected;
SetServiceMenuStatus(); SetServiceMenuStatus();
CheckAndSetTrayIcon(); CheckAndSetTrayIcon();
return(true); ret = true;
out:
if (schService)
CloseServiceHandle(schService);
if (schSCManager)
CloseServiceHandle(schSCManager);
return ret;
} }
int MyReStartService() int MyReStartService()
@ -244,9 +254,10 @@ int MyReStartService()
bool bool
CheckIServiceStatus(BOOL warn) CheckIServiceStatus(BOOL warn)
{ {
SC_HANDLE schSCManager; SC_HANDLE schSCManager = NULL;
SC_HANDLE schService; SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus; SERVICE_STATUS ssStatus;
BOOL ret = false;
// Open a handle to the SC Manager database. // Open a handle to the SC Manager database.
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
@ -262,28 +273,36 @@ CheckIServiceStatus(BOOL warn)
/* warn that iservice is not installed */ /* warn that iservice is not installed */
if (warn) if (warn)
ShowLocalizedMsg(IDS_ERR_INSTALL_ISERVICE); ShowLocalizedMsg(IDS_ERR_INSTALL_ISERVICE);
return(false); goto out;
} }
if (!QueryServiceStatus(schService, &ssStatus)) if (!QueryServiceStatus(schService, &ssStatus))
return(false); goto out;
if (ssStatus.dwCurrentState != SERVICE_RUNNING) if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{ {
/* warn that iservice is not started */ /* warn that iservice is not started */
if (warn) if (warn)
ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE); ShowLocalizedMsg(IDS_ERR_NOTSTARTED_ISERVICE);
return(false); goto out;
} }
return true; ret = true;
out:
if (schService)
CloseServiceHandle(schService);
if (schSCManager)
CloseServiceHandle(schSCManager);
return ret;
} }
int CheckServiceStatus() int CheckServiceStatus()
{ {
SC_HANDLE schSCManager; SC_HANDLE schSCManager = NULL;
SC_HANDLE schService; SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus; SERVICE_STATUS ssStatus;
BOOL ret = false;
// Open a handle to the SC Manager database. // Open a handle to the SC Manager database.
schSCManager = OpenSCManager( schSCManager = OpenSCManager(
@ -294,7 +313,7 @@ int CheckServiceStatus()
if (NULL == schSCManager) { if (NULL == schSCManager) {
o.service_state = service_noaccess; o.service_state = service_noaccess;
SetServiceMenuStatus(); SetServiceMenuStatus();
return(false); goto out;
} }
schService = OpenService( schService = OpenService(
@ -307,7 +326,7 @@ int CheckServiceStatus()
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE); ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
o.service_state = service_noaccess; o.service_state = service_noaccess;
SetServiceMenuStatus(); SetServiceMenuStatus();
return(false); goto out;
} }
if (!QueryServiceStatus( if (!QueryServiceStatus(
@ -316,7 +335,7 @@ int CheckServiceStatus()
{ {
/* query failed */ /* query failed */
ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE); ShowLocalizedMsg(IDS_ERR_QUERY_SERVICE);
return(false); goto out;
} }
if (ssStatus.dwCurrentState == SERVICE_RUNNING) if (ssStatus.dwCurrentState == SERVICE_RUNNING)
@ -324,13 +343,21 @@ int CheckServiceStatus()
o.service_state = service_connected; o.service_state = service_connected;
SetServiceMenuStatus(); SetServiceMenuStatus();
SetTrayIcon(connected); SetTrayIcon(connected);
return(true); ret = true;
goto out;
} }
else else
{ {
o.service_state = service_disconnected; o.service_state = service_disconnected;
SetServiceMenuStatus(); SetServiceMenuStatus();
SetTrayIcon(disconnected); SetTrayIcon(disconnected);
return(false); goto out;
} }
out:
if (schService)
CloseServiceHandle(schService);
if (schSCManager)
CloseServiceHandle(schSCManager);
return ret;
} }

Loading…
Cancel
Save