Browse Source

HPCC-17301 Unchecked string copy in javaembed.cpp

Harmless in practice - could never be longer than the specified length.

Signed-off-by: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 8 years ago
parent
commit
196f5d0283
1 changed files with 8 additions and 25 deletions
  1. 8 25
      plugins/javaembed/javaembed.cpp

+ 8 - 25
plugins/javaembed/javaembed.cpp

@@ -180,7 +180,7 @@ public:
     JavaVM *javaVM;       /* denotes a Java VM */
 } *globalState;
 
-static char helperLibraryName[_MAX_PATH];
+static StringBuffer helperLibraryName;
 
 #ifdef _WIN32
     EXTERN_C IMAGE_DOS_HEADER __ImageBase;
@@ -192,36 +192,19 @@ MODULE_INIT(INIT_PRIORITY_STANDARD)
     // Make sure we are never unloaded (as JVM does not support it)
     // we do this by doing a dynamic load of the javaembed library
 #ifdef _WIN32
-    ::GetModuleFileName((HINSTANCE)&__ImageBase, helperLibraryName, _MAX_PATH);
+    char ln[_MAX_PATH];
+    ::GetModuleFileName((HINSTANCE)&__ImageBase, ln, _MAX_PATH);
     if (strstr(path, "javaembed"))
     {
-        HINSTANCE h = LoadSharedObject(helperLibraryName, false, false);
+        HINSTANCE h = LoadSharedObject(ln, false, false);
+        helperLibraryName.set(ln);
         DBGLOG("LoadSharedObject returned %p", h);
     }
 #else
-    FILE *diskfp = fopen("/proc/self/maps", "r");
-    if (diskfp)
+    if (findLoadedModule(helperLibraryName, "javaembed"))
     {
-        char ln[_MAX_PATH];
-        while (fgets(ln, sizeof(ln), diskfp))
-        {
-            if (strstr(ln, "libjavaembed"))
-            {
-                const char *fullName = strchr(ln, '/');
-                if (fullName)
-                {
-                    char *tail = (char *) strstr(fullName, SharedObjectExtension);
-                    if (tail)
-                    {
-                        tail[strlen(SharedObjectExtension)] = 0;
-                        strcpy(helperLibraryName, fullName);
-                        HINSTANCE h = LoadSharedObject(fullName, false, false);
-                        break;
-                    }
-                }
-            }
-        }
-        fclose(diskfp);
+        HINSTANCE h = LoadSharedObject(helperLibraryName, false, false);
+        // Deliberately leak this handle
     }
 #endif
     return true;