Skip to content

Commit 2b924d4

Browse files
catenacybervictorjulien
authored andcommitted
windows: always quote path for windows functions needing it
Ticket: 8600 CreateServiceA doc states for example : > If the path contains a space, it must be quoted so that it is correctly interpreted. Also fixes strlcat usage and check return value to error out on truncated path (cherry picked from commit ac1b3cc)
1 parent 5560c7e commit 2b924d4

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

src/win32-service.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,29 @@ int SCServiceInstall(int argc, char **argv)
219219
do {
220220
memset(path, 0, sizeof(path));
221221

222-
if (GetModuleFileName(NULL, path, MAX_PATH) == 0 ){
222+
path[0] = '"';
223+
if (GetModuleFileName(NULL, path + 1, sizeof(path) - 1) == 0) {
223224
SCLogError("Can't get path to service binary: %d", (int)GetLastError());
224225
break;
225226
}
227+
if (strlcat(path, "\"", sizeof(path)) >= sizeof(path)) {
228+
SCLogError("failed to construct service path string: path truncated: %s", path);
229+
break;
230+
}
226231

227232
/* skip name of binary itself */
228233
for (i = 1; i < argc; i++) {
229234
if ((strlen(argv[i]) <= strlen("--service-install")) && (strncmp("--service-install", argv[i], strlen(argv[i])) == 0)) {
230235
continue;
231236
}
232-
strlcat(path, " ", sizeof(path) - strlen(path) - 1);
233-
strlcat(path, argv[i], sizeof(path) - strlen(path) - 1);
237+
if (strlcat(path, " ", sizeof(path)) >= sizeof(path)) {
238+
SCLogError("failed to construct service path string: path truncated: %s", path);
239+
return -1;
240+
}
241+
if (strlcat(path, argv[i], sizeof(path)) >= sizeof(path)) {
242+
SCLogError("failed to construct service path string: path truncated: %s", path);
243+
return -1;
244+
}
234245
}
235246

236247
if ((scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL) {
@@ -344,18 +355,29 @@ int SCServiceChangeParams(int argc, char **argv)
344355
do {
345356
memset(path, 0, sizeof(path));
346357

347-
if (GetModuleFileName(NULL, path, MAX_PATH) == 0 ){
358+
path[0] = '"';
359+
if (GetModuleFileName(NULL, path + 1, sizeof(path) - 1) == 0) {
348360
SCLogError("Can't get path to service binary: %d", (int)GetLastError());
349361
break;
350362
}
363+
if (strlcat(path, "\"", sizeof(path)) >= sizeof(path)) {
364+
SCLogError("failed to construct service path string: path truncated: %s", path);
365+
break;
366+
}
351367

352368
/* skip name of binary itself */
353369
for (i = 1; i < argc; i++) {
354370
if ((strlen(argv[i]) <= strlen("--service-change-params")) && (strncmp("--service-change-params", argv[i], strlen(argv[i])) == 0)) {
355371
continue;
356372
}
357-
strlcat(path, " ", sizeof(path) - strlen(path) - 1);
358-
strlcat(path, argv[i], sizeof(path) - strlen(path) - 1);
373+
if (strlcat(path, " ", sizeof(path)) >= sizeof(path)) {
374+
SCLogError("failed to construct service path string: path truncated: %s", path);
375+
return -1;
376+
}
377+
if (strlcat(path, argv[i], sizeof(path)) >= sizeof(path)) {
378+
SCLogError("failed to construct service path string: path truncated: %s", path);
379+
return -1;
380+
}
359381
}
360382

361383
if ((scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL) {

0 commit comments

Comments
 (0)