[SERVICES] Use safe-string routines in some places, and do not hardcode buffer lengths.

This commit is contained in:
Hermès Bélusca-Maïto 2018-02-25 00:48:43 +01:00
parent 1aa359ec7f
commit c6d65fec69
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 43 additions and 39 deletions

View file

@ -15,6 +15,7 @@
#include "services.h" #include "services.h"
#include <userenv.h> #include <userenv.h>
#include <strsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
@ -88,7 +89,8 @@ ScmCreateNewControlPipe(PSERVICE_IMAGE pServiceImage)
} }
/* Create '\\.\pipe\net\NtControlPipeXXX' instance */ /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
swprintf(szControlPipeName, L"\\\\.\\pipe\\net\\NtControlPipe%lu", ServiceCurrent); StringCchPrintfW(szControlPipeName, ARRAYSIZE(szControlPipeName),
L"\\\\.\\pipe\\net\\NtControlPipe%lu", ServiceCurrent);
DPRINT("PipeName: %S\n", szControlPipeName); DPRINT("PipeName: %S\n", szControlPipeName);
@ -1861,7 +1863,8 @@ ScmLoadService(PSERVICE Service,
if (Service->dwErrorControl != SERVICE_ERROR_IGNORE) if (Service->dwErrorControl != SERVICE_ERROR_IGNORE)
{ {
/* Log a failed service start */ /* Log a failed service start */
swprintf(szLogBuffer, L"%lu", dwError); StringCchPrintfW(szLogBuffer, ARRAYSIZE(szLogBuffer),
L"%lu", dwError);
lpLogStrings[0] = Service->lpServiceName; lpLogStrings[0] = Service->lpServiceName;
lpLogStrings[1] = szLogBuffer; lpLogStrings[1] = szLogBuffer;
ScmLogEvent(EVENT_SERVICE_START_FAILED, ScmLogEvent(EVENT_SERVICE_START_FAILED,
@ -1990,19 +1993,21 @@ ScmAutoStartServices(VOID)
CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry); CurrentService = CONTAINING_RECORD(ServiceEntry, SERVICE, ServiceListEntry);
/* Build the safe boot path */ /* Build the safe boot path */
wcscpy(szSafeBootServicePath, StringCchCopyW(szSafeBootServicePath, ARRAYSIZE(szSafeBootServicePath),
L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot"); L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot");
switch (SafeBootEnabled) switch (SafeBootEnabled)
{ {
/* NOTE: Assumes MINIMAL (1) and DSREPAIR (3) load same items */ /* NOTE: Assumes MINIMAL (1) and DSREPAIR (3) load same items */
case 1: case 1:
case 3: case 3:
wcscat(szSafeBootServicePath, L"\\Minimal\\"); StringCchCatW(szSafeBootServicePath, ARRAYSIZE(szSafeBootServicePath),
L"\\Minimal\\");
break; break;
case 2: case 2:
wcscat(szSafeBootServicePath, L"\\Network\\"); StringCchCatW(szSafeBootServicePath, ARRAYSIZE(szSafeBootServicePath),
L"\\Network\\");
break; break;
} }
@ -2019,9 +2024,8 @@ ScmAutoStartServices(VOID)
RegCloseKey(hKey); RegCloseKey(hKey);
/* Finish Safe Boot path off */ /* Finish Safe Boot path off */
wcsncat(szSafeBootServicePath, StringCchCatW(szSafeBootServicePath, ARRAYSIZE(szSafeBootServicePath),
CurrentService->lpServiceName, CurrentService->lpServiceName);
MAX_PATH - wcslen(szSafeBootServicePath));
/* Check that the key is in the Safe Boot path */ /* Check that the key is in the Safe Boot path */
dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,

View file

@ -13,6 +13,7 @@
#include "services.h" #include "services.h"
#include <winnls.h> #include <winnls.h>
#include <strsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
@ -1241,36 +1242,36 @@ RControlService(
if (dwError == ERROR_SUCCESS) if (dwError == ERROR_SUCCESS)
{ {
if (dwControl == SERVICE_CONTROL_STOP || if (dwControl == SERVICE_CONTROL_STOP ||
dwControl == SERVICE_CONTROL_PAUSE || dwControl == SERVICE_CONTROL_PAUSE ||
dwControl == SERVICE_CONTROL_CONTINUE) dwControl == SERVICE_CONTROL_CONTINUE)
{
/* Log a successful send control */
switch (dwControl)
{ {
/* Log a successful send control */ case SERVICE_CONTROL_STOP:
uID = IDS_SERVICE_STOP;
break;
switch (dwControl) case SERVICE_CONTROL_PAUSE:
{ uID = IDS_SERVICE_PAUSE;
case SERVICE_CONTROL_STOP: break;
uID = IDS_SERVICE_STOP;
break;
case SERVICE_CONTROL_PAUSE: case SERVICE_CONTROL_CONTINUE:
uID = IDS_SERVICE_PAUSE; uID = IDS_SERVICE_RESUME;
break; break;
case SERVICE_CONTROL_CONTINUE:
uID = IDS_SERVICE_RESUME;
break;
}
LoadStringW(GetModuleHandle(NULL), uID, szLogBuffer, 80);
lpLogStrings[0] = lpService->lpDisplayName;
lpLogStrings[1] = szLogBuffer;
ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS,
EVENTLOG_INFORMATION_TYPE,
2,
lpLogStrings);
} }
LoadStringW(GetModuleHandle(NULL), uID, szLogBuffer, ARRAYSIZE(szLogBuffer));
lpLogStrings[0] = lpService->lpDisplayName;
lpLogStrings[1] = szLogBuffer;
ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS,
EVENTLOG_INFORMATION_TYPE,
2,
lpLogStrings);
}
} }
return dwError; return dwError;
@ -1757,7 +1758,8 @@ RSetServiceStatus(
(lpServiceStatus->dwWin32ExitCode != ERROR_SUCCESS)) (lpServiceStatus->dwWin32ExitCode != ERROR_SUCCESS))
{ {
/* Log a failed service stop */ /* Log a failed service stop */
swprintf(szLogBuffer, L"%lu", lpServiceStatus->dwWin32ExitCode); StringCchPrintfW(szLogBuffer, ARRAYSIZE(szLogBuffer),
L"%lu", lpServiceStatus->dwWin32ExitCode);
lpLogStrings[0] = lpService->lpDisplayName; lpLogStrings[0] = lpService->lpDisplayName;
lpLogStrings[1] = szLogBuffer; lpLogStrings[1] = szLogBuffer;
@ -1787,7 +1789,7 @@ RSetServiceStatus(
break; break;
} }
LoadStringW(GetModuleHandle(NULL), uID, szLogBuffer, 80); LoadStringW(GetModuleHandle(NULL), uID, szLogBuffer, ARRAYSIZE(szLogBuffer));
lpLogStrings[0] = lpService->lpDisplayName; lpLogStrings[0] = lpService->lpDisplayName;
lpLogStrings[1] = szLogBuffer; lpLogStrings[1] = szLogBuffer;
@ -1797,8 +1799,6 @@ RSetServiceStatus(
lpLogStrings); lpLogStrings);
} }
DPRINT("Set %S to %lu\n", lpService->lpDisplayName, lpService->Status.dwCurrentState); DPRINT("Set %S to %lu\n", lpService->lpDisplayName, lpService->Status.dwCurrentState);
DPRINT("RSetServiceStatus() done\n"); DPRINT("RSetServiceStatus() done\n");