Browse Source

Fix path lengths.

Some buffers were being limited to MAX_PATH characters when higher
limits were more appropriate.
For example the maximum length of a command line is documented as 32768
characters with the application name part limited to MAX_PATH.
Long applications paths and arguments could be truncated when written to
the registry.

Thanks Joel Reingold.
Iain Patterson 12 years ago
parent
commit
f1abcb8f0a
4 changed files with 25 additions and 12 deletions
  1. 1 1
      gui.cpp
  2. 13 0
      nssm.h
  3. 5 5
      registry.cpp
  4. 6 6
      service.cpp

+ 1 - 1
gui.cpp

@@ -67,7 +67,7 @@ int install(HWND window) {
 
   /* Check parameters in the window */
   char name[STRING_SIZE];
-  char exe[MAX_PATH];
+  char exe[EXE_LENGTH];
   char flags[STRING_SIZE];
 
   /* Get service name */

+ 13 - 0
nssm.h

@@ -18,4 +18,17 @@ int str_equiv(const char *, const char *);
 #define NSSM_DATE "2010-04-04"
 #define NSSM_RUN "run"
 
+/*
+  MSDN says the commandline in CreateProcess() is limited to 32768 characters
+  and the application name to MAX_PATH.
+  A registry key is limited to 255 characters.
+  A registry value is limited to 16383 characters.
+  Therefore we limit the service name to accommodate the path under HKLM.
+*/
+#define EXE_LENGTH MAX_PATH
+#define CMD_LENGTH 32768
+#define KEY_LENGTH 255
+#define VALUE_LENGTH 16383
+#define SERVICE_NAME_LENGTH KEY_LENGTH - 55
+
 #endif

+ 5 - 5
registry.cpp

@@ -3,7 +3,7 @@
 int create_messages() {
   HKEY key;
 
-  char registry[MAX_PATH];
+  char registry[KEY_LENGTH];
   if (_snprintf(registry, sizeof(registry), "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "eventlog registry", "create_messages()", 0);
     return 1;
@@ -28,7 +28,7 @@ int create_messages() {
 
 int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
   /* Get registry */
-  char registry[MAX_PATH];
+  char registry[KEY_LENGTH];
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "create_parameters()", 0);
     return 1;
@@ -69,7 +69,7 @@ int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
 
 int create_exit_action(char *service_name, const char *action_string) {
   /* Get registry */
-  char registry[MAX_PATH];
+  char registry[KEY_LENGTH];
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "create_exit_action()", 0);
     return 1;
@@ -104,7 +104,7 @@ int create_exit_action(char *service_name, const char *action_string) {
 
 int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {
   /* Get registry */
-  char registry[MAX_PATH];
+  char registry[KEY_LENGTH];
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "get_parameters()", 0);
     return 1;
@@ -148,7 +148,7 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
 
 int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action) {
   /* Get registry */
-  char registry[MAX_PATH];
+  char registry[KEY_LENGTH];
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "get_exit_action()", 0);
     return 1;

+ 6 - 6
service.cpp

@@ -4,9 +4,9 @@ SERVICE_STATUS service_status;
 SERVICE_STATUS_HANDLE service_handle;
 HANDLE wait_handle;
 HANDLE pid;
-static char service_name[MAX_PATH];
-char exe[MAX_PATH];
-char flags[MAX_PATH];
+static char service_name[SERVICE_NAME_LENGTH];
+char exe[EXE_LENGTH];
+char flags[CMD_LENGTH];
 char dir[MAX_PATH];
 
 static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;
@@ -59,10 +59,10 @@ int install_service(char *name, char *exe, char *flags) {
   GetModuleFileName(0, path, MAX_PATH);
 
   /* Construct command */
-  char command[MAX_PATH];
+  char command[CMD_LENGTH];
   size_t runlen = strlen(NSSM_RUN);
   size_t pathlen = strlen(path);
-  if (pathlen + runlen + 2 >= MAX_PATH) {
+  if (pathlen + runlen + 2 >= VALUE_LENGTH) {
     fprintf(stderr, "The full path to " NSSM " is too long!\n");
     return 3;
   }
@@ -222,7 +222,7 @@ int start_service() {
   ZeroMemory(&pi, sizeof(pi));
 
   /* Launch executable with arguments */
-  char cmd[MAX_PATH];
+  char cmd[CMD_LENGTH];
   if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);
     return stop_service(2);