From e51adf72cf5a0baf8cf8ec9539a45de50924c502 Mon Sep 17 00:00:00 2001 From: Jan Kevin Dick Date: Wed, 12 Feb 2020 13:01:45 +0100 Subject: [PATCH 1/2] Fixed setFailureActions. Function could crash if the given Vector has less then 3 Elements. Also the Function crashes if no SVC_REBOOT or SVC_RUN_COMMAND are specified. If this is the case ac.lpCommand and ac.lpRebootMessage are not set. --- Util/src/WinService.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Util/src/WinService.cpp b/Util/src/WinService.cpp index 3c992b217..3921befc3 100644 --- a/Util/src/WinService.cpp +++ b/Util/src/WinService.cpp @@ -236,10 +236,17 @@ WinService::Startup WinService::getStartup() const void WinService::setFailureActions(FailureActionVector failureActions, const std::string& command, const std::string& rebootMessage) { + + if (failureActions.size() > 3) { + throw InvalidArgumentException{ "Only 0-3 Failure Actions are supported" }; + } + open(); - auto actions = new SC_ACTION[3]; + auto actions = new SC_ACTION[failureActions.size()]; SERVICE_FAILURE_ACTIONSW ac; - + ac.lpCommand = NULL; + ac.lpRebootMsg = NULL; + std::wstring urebootMessage; Poco::UnicodeConverter::toUTF16(rebootMessage, urebootMessage); std::vector rebootMessageVector{ urebootMessage.begin(), urebootMessage.end() }; @@ -250,7 +257,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std std::vector commandVector{ uComamnd.begin(), uComamnd.end() }; commandVector.push_back('\0'); - for (auto i = 0; i < 3; i++) + for (auto i = 0; i < failureActions.size(); i++) { switch (failureActions[i].type) { @@ -276,7 +283,7 @@ void WinService::setFailureActions(FailureActionVector failureActions, const std } ac.dwResetPeriod = 0; - ac.cActions = 3; + ac.cActions = failureActions.size(); ac.lpsaActions = actions; if (!ChangeServiceConfig2W(_svcHandle, SERVICE_CONFIG_FAILURE_ACTIONS, &ac)) From 753cfcecd78763795aa1edb279709b34548b395b Mon Sep 17 00:00:00 2001 From: Jan Kevin Dick Date: Wed, 12 Feb 2020 13:10:09 +0100 Subject: [PATCH 2/2] Close Service Handle after DeleteService Function call --- Util/src/WinService.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Util/src/WinService.cpp b/Util/src/WinService.cpp index 3c992b217..1a89a0586 100644 --- a/Util/src/WinService.cpp +++ b/Util/src/WinService.cpp @@ -125,6 +125,7 @@ void WinService::unregisterService() open(); if (!DeleteService(_svcHandle)) throw SystemException("cannot unregister service", _name); + close(); }