< prev index next >

src/os/linux/vm/os_linux.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
   1 /*
   2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


2301 void os::jvm_path(char *buf, jint buflen) {
2302   // Error checking.
2303   if (buflen < MAXPATHLEN) {
2304     assert(false, "must use a large-enough buffer");
2305     buf[0] = '\0';
2306     return;
2307   }
2308   // Lazy resolve the path to current module.
2309   if (saved_jvm_path[0] != 0) {
2310     strcpy(buf, saved_jvm_path);
2311     return;
2312   }
2313 
2314   char dli_fname[MAXPATHLEN];
2315   bool ret = dll_address_to_library_name(
2316                                          CAST_FROM_FN_PTR(address, os::jvm_path),
2317                                          dli_fname, sizeof(dli_fname), NULL);
2318   assert(ret, "cannot locate libjvm");
2319   char *rp = NULL;
2320   if (ret && dli_fname[0] != '\0') {
2321     rp = realpath(dli_fname, buf);
2322   }
2323   if (rp == NULL) {
2324     return;
2325   }
2326 
2327   if (Arguments::sun_java_launcher_is_altjvm()) {
2328     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2329     // value for buf is "<JAVA_HOME>/jre/lib/<vmtype>/libjvm.so".
2330     // If "/jre/lib/" appears at the right place in the string, then
2331     // assume we are installed in a JDK and we're done. Otherwise, check
2332     // for a JAVA_HOME environment variable and fix up the path so it
2333     // looks like libjvm.so is installed there (append a fake suffix
2334     // hotspot/libjvm.so).
2335     const char *p = buf + strlen(buf) - 1;
2336     for (int count = 0; p > buf && count < 5; ++count) {
2337       for (--p; p > buf && *p != '/'; --p)
2338         /* empty */ ;
2339     }
2340 
2341     if (strncmp(p, "/jre/lib/", 9) != 0) {
2342       // Look for JAVA_HOME in the environment.
2343       char* java_home_var = ::getenv("JAVA_HOME");
2344       if (java_home_var != NULL && java_home_var[0] != 0) {
2345         char* jrelib_p;
2346         int len;
2347 
2348         // Check the current module name "libjvm.so".
2349         p = strrchr(buf, '/');
2350         if (p == NULL) {
2351           return;
2352         }
2353         assert(strstr(p, "/libjvm") == p, "invalid library name");
2354 
2355         rp = realpath(java_home_var, buf);
2356         if (rp == NULL) {
2357           return;
2358         }
2359 
2360         // determine if this is a legacy image or modules image
2361         // modules image doesn't have "jre" subdirectory
2362         len = strlen(buf);
2363         assert(len < buflen, "Ran out of buffer room");
2364         jrelib_p = buf + len;
2365         snprintf(jrelib_p, buflen-len, "/jre/lib");
2366         if (0 != access(buf, F_OK)) {
2367           snprintf(jrelib_p, buflen-len, "/lib");
2368         }
2369 
2370         if (0 == access(buf, F_OK)) {
2371           // Use current module name "libjvm.so"
2372           len = strlen(buf);
2373           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2374         } else {
2375           // Go back to path of .so
2376           rp = realpath(dli_fname, buf);
2377           if (rp == NULL) {
2378             return;
2379           }
2380         }
2381       }
2382     }
2383   }
2384 
2385   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2386   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2387 }
2388 
2389 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2390   // no prefix required, not even "_"
2391 }
2392 
2393 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2394   // no suffix required
2395 }
2396 


   1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


2301 void os::jvm_path(char *buf, jint buflen) {
2302   // Error checking.
2303   if (buflen < MAXPATHLEN) {
2304     assert(false, "must use a large-enough buffer");
2305     buf[0] = '\0';
2306     return;
2307   }
2308   // Lazy resolve the path to current module.
2309   if (saved_jvm_path[0] != 0) {
2310     strcpy(buf, saved_jvm_path);
2311     return;
2312   }
2313 
2314   char dli_fname[MAXPATHLEN];
2315   bool ret = dll_address_to_library_name(
2316                                          CAST_FROM_FN_PTR(address, os::jvm_path),
2317                                          dli_fname, sizeof(dli_fname), NULL);
2318   assert(ret, "cannot locate libjvm");
2319   char *rp = NULL;
2320   if (ret && dli_fname[0] != '\0') {
2321     rp = os::Posix::realpath(dli_fname, buf, buflen);
2322   }
2323   if (rp == NULL) {
2324     return;
2325   }
2326 
2327   if (Arguments::sun_java_launcher_is_altjvm()) {
2328     // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2329     // value for buf is "<JAVA_HOME>/jre/lib/<vmtype>/libjvm.so".
2330     // If "/jre/lib/" appears at the right place in the string, then
2331     // assume we are installed in a JDK and we're done. Otherwise, check
2332     // for a JAVA_HOME environment variable and fix up the path so it
2333     // looks like libjvm.so is installed there (append a fake suffix
2334     // hotspot/libjvm.so).
2335     const char *p = buf + strlen(buf) - 1;
2336     for (int count = 0; p > buf && count < 5; ++count) {
2337       for (--p; p > buf && *p != '/'; --p)
2338         /* empty */ ;
2339     }
2340 
2341     if (strncmp(p, "/jre/lib/", 9) != 0) {
2342       // Look for JAVA_HOME in the environment.
2343       char* java_home_var = ::getenv("JAVA_HOME");
2344       if (java_home_var != NULL && java_home_var[0] != 0) {
2345         char* jrelib_p;
2346         int len;
2347 
2348         // Check the current module name "libjvm.so".
2349         p = strrchr(buf, '/');
2350         if (p == NULL) {
2351           return;
2352         }
2353         assert(strstr(p, "/libjvm") == p, "invalid library name");
2354 
2355         rp = os::Posix::realpath(java_home_var, buf, buflen);
2356         if (rp == NULL) {
2357           return;
2358         }
2359 
2360         // determine if this is a legacy image or modules image
2361         // modules image doesn't have "jre" subdirectory
2362         len = strlen(buf);
2363         assert(len < buflen, "Ran out of buffer room");
2364         jrelib_p = buf + len;
2365         snprintf(jrelib_p, buflen-len, "/jre/lib");
2366         if (0 != access(buf, F_OK)) {
2367           snprintf(jrelib_p, buflen-len, "/lib");
2368         }
2369 
2370         if (0 == access(buf, F_OK)) {
2371           // Use current module name "libjvm.so"
2372           len = strlen(buf);
2373           snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2374         } else {
2375           // Go back to path of .so
2376           rp = os::Posix::realpath(dli_fname, buf, buflen);
2377           if (rp == NULL) {
2378             return;
2379           }
2380         }
2381       }
2382     }
2383   }
2384 
2385   strncpy(saved_jvm_path, buf, MAXPATHLEN);
2386   saved_jvm_path[MAXPATHLEN - 1] = '\0';
2387 }
2388 
2389 void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2390   // no prefix required, not even "_"
2391 }
2392 
2393 void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2394   // no suffix required
2395 }
2396 


< prev index next >