< prev index next >

src/os/solaris/vm/os_solaris.cpp

Print this page
rev 12741 : 8173848: realpath is unsafe
Summary: Fix occurrences of realpath in hotspot to use safe POSIX.1-2008 form.
Reviewed-by: dsamersoff, dholmes, clanger


2017 static char saved_jvm_path[MAXPATHLEN] = { 0 };
2018 
2019 // Find the full path to the current module, libjvm.so
2020 void os::jvm_path(char *buf, jint buflen) {
2021   // Error checking.
2022   if (buflen < MAXPATHLEN) {
2023     assert(false, "must use a large-enough buffer");
2024     buf[0] = '\0';
2025     return;
2026   }
2027   // Lazy resolve the path to current module.
2028   if (saved_jvm_path[0] != 0) {
2029     strcpy(buf, saved_jvm_path);
2030     return;
2031   }
2032 
2033   Dl_info dlinfo;
2034   int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
2035   assert(ret != 0, "cannot locate libjvm");
2036   if (ret != 0 && dlinfo.dli_fname != NULL) {
2037     realpath((char *)dlinfo.dli_fname, buf);


2038   } else {
2039     buf[0] = '\0';
2040     return;
2041   }
2042 
2043   if (Arguments::sun_java_launcher_is_altjvm()) {
2044     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2045     // value for buf is "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".
2046     // If "/jre/lib/" appears at the right place in the string, then
2047     // assume we are installed in a JDK and we're done.  Otherwise, check
2048     // for a JAVA_HOME environment variable and fix up the path so it
2049     // looks like libjvm.so is installed there (append a fake suffix
2050     // hotspot/libjvm.so).
2051     const char *p = buf + strlen(buf) - 1;
2052     for (int count = 0; p > buf && count < 5; ++count) {
2053       for (--p; p > buf && *p != '/'; --p)
2054         /* empty */ ;
2055     }
2056 
2057     if (strncmp(p, "/jre/lib/", 9) != 0) {
2058       // Look for JAVA_HOME in the environment.
2059       char* java_home_var = ::getenv("JAVA_HOME");
2060       if (java_home_var != NULL && java_home_var[0] != 0) {
2061         char* jrelib_p;
2062         int   len;
2063 
2064         // Check the current module name "libjvm.so".
2065         p = strrchr(buf, '/');
2066         assert(strstr(p, "/libjvm") == p, "invalid library name");
2067 
2068         realpath(java_home_var, buf);


2069         // determine if this is a legacy image or modules image
2070         // modules image doesn't have "jre" subdirectory
2071         len = strlen(buf);
2072         assert(len < buflen, "Ran out of buffer space");
2073         jrelib_p = buf + len;
2074         snprintf(jrelib_p, buflen-len, "/jre/lib");
2075         if (0 != access(buf, F_OK)) {
2076           snprintf(jrelib_p, buflen-len, "/lib");
2077         }
2078 
2079         if (0 == access(buf, F_OK)) {
2080           // Use current module name "libjvm.so"
2081           len = strlen(buf);
2082           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2083         } else {
2084           // Go back to path of .so
2085           realpath((char *)dlinfo.dli_fname, buf);


2086         }
2087       }
2088     }
2089   }
2090 
2091   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2092   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2093 }
2094 
2095 
2096 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2097   // no prefix required, not even "_"
2098 }
2099 
2100 
2101 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2102   // no suffix required
2103 }
2104 
2105 // This method is a copy of JDK's sysGetLastErrorString




2017 static char saved_jvm_path[MAXPATHLEN] = { 0 };
2018 
2019 // Find the full path to the current module, libjvm.so
2020 void os::jvm_path(char *buf, jint buflen) {
2021   // Error checking.
2022   if (buflen < MAXPATHLEN) {
2023     assert(false, "must use a large-enough buffer");
2024     buf[0] = '\0';
2025     return;
2026   }
2027   // Lazy resolve the path to current module.
2028   if (saved_jvm_path[0] != 0) {
2029     strcpy(buf, saved_jvm_path);
2030     return;
2031   }
2032 
2033   Dl_info dlinfo;
2034   int ret = dladdr(CAST_FROM_FN_PTR(void *, os::jvm_path), &dlinfo);
2035   assert(ret != 0, "cannot locate libjvm");
2036   if (ret != 0 && dlinfo.dli_fname != NULL) {
2037     if (os::Posix::realpath((char *)dlinfo.dli_fname, buf, buflen) == NULL) {
2038       return;
2039     }
2040   } else {
2041     buf[0] = '\0';
2042     return;
2043   }
2044 
2045   if (Arguments::sun_java_launcher_is_altjvm()) {
2046     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2047     // value for buf is "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".
2048     // If "/jre/lib/" appears at the right place in the string, then
2049     // assume we are installed in a JDK and we're done.  Otherwise, check
2050     // for a JAVA_HOME environment variable and fix up the path so it
2051     // looks like libjvm.so is installed there (append a fake suffix
2052     // hotspot/libjvm.so).
2053     const char *p = buf + strlen(buf) - 1;
2054     for (int count = 0; p > buf && count < 5; ++count) {
2055       for (--p; p > buf && *p != '/'; --p)
2056         /* empty */ ;
2057     }
2058 
2059     if (strncmp(p, "/jre/lib/", 9) != 0) {
2060       // Look for JAVA_HOME in the environment.
2061       char* java_home_var = ::getenv("JAVA_HOME");
2062       if (java_home_var != NULL && java_home_var[0] != 0) {
2063         char* jrelib_p;
2064         int   len;
2065 
2066         // Check the current module name "libjvm.so".
2067         p = strrchr(buf, '/');
2068         assert(strstr(p, "/libjvm") == p, "invalid library name");
2069 
2070         if (os::Posix::realpath(java_home_var, buf, buflen) == NULL) {
2071           return;
2072         }
2073         // determine if this is a legacy image or modules image
2074         // modules image doesn't have "jre" subdirectory
2075         len = strlen(buf);
2076         assert(len < buflen, "Ran out of buffer space");
2077         jrelib_p = buf + len;
2078         snprintf(jrelib_p, buflen-len, "/jre/lib");
2079         if (0 != access(buf, F_OK)) {
2080           snprintf(jrelib_p, buflen-len, "/lib");
2081         }
2082 
2083         if (0 == access(buf, F_OK)) {
2084           // Use current module name "libjvm.so"
2085           len = strlen(buf);
2086           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2087         } else {
2088           // Go back to path of .so
2089           if (os::Posix::realpath((char *)dlinfo.dli_fname, buf, buflen) == NULL) {
2090             return;
2091           }
2092         }
2093       }
2094     }
2095   }
2096 
2097   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2098   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2099 }
2100 
2101 
2102 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2103   // no prefix required, not even "_"
2104 }
2105 
2106 
2107 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2108   // no suffix required
2109 }
2110 
2111 // This method is a copy of JDK's sysGetLastErrorString


< prev index next >