< prev index next >

src/os/posix/vm/os_posix.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  *


1088 #else
1089    VMError::report_and_die("unimplemented ucontext_get_pc");
1090 #endif
1091 }
1092 
1093 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
1094   size_t stack_size = 0;
1095   size_t guard_size = 0;
1096   int detachstate = 0;
1097   pthread_attr_getstacksize(attr, &stack_size);
1098   pthread_attr_getguardsize(attr, &guard_size);
1099   // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
1100   LINUX_ONLY(stack_size -= guard_size);
1101   pthread_attr_getdetachstate(attr, &detachstate);
1102   jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
1103     stack_size / 1024, guard_size / 1024,
1104     (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
1105   return buf;
1106 }
1107 










































1108 // Check minimum allowable stack sizes for thread creation and to initialize
1109 // the java system classes, including StackOverflowError - depends on page
1110 // size.
1111 // The space needed for frames during startup is platform dependent. It
1112 // depends on word size, platform calling conventions, C frame layout and
1113 // interpreter/C1/C2 design decisions. Therefore this is given in a
1114 // platform (os/cpu) dependent constant.
1115 // To this, space for guard mechanisms is added, which depends on the
1116 // page size which again depends on the concrete system the VM is running
1117 // on. Space for libc guard pages is not included in this size.
1118 jint os::Posix::set_minimum_stack_sizes() {
1119   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1120                                    JavaThread::stack_guard_zone_size() +
1121                                    JavaThread::stack_shadow_zone_size();
1122 
1123   _java_thread_min_stack_allowed = align_size_up(_java_thread_min_stack_allowed, vm_page_size());
1124 
1125   size_t stack_size_in_bytes = ThreadStackSize * K;
1126   if (stack_size_in_bytes != 0 &&
1127       stack_size_in_bytes < _java_thread_min_stack_allowed) {


   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  *


1088 #else
1089    VMError::report_and_die("unimplemented ucontext_get_pc");
1090 #endif
1091 }
1092 
1093 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
1094   size_t stack_size = 0;
1095   size_t guard_size = 0;
1096   int detachstate = 0;
1097   pthread_attr_getstacksize(attr, &stack_size);
1098   pthread_attr_getguardsize(attr, &guard_size);
1099   // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
1100   LINUX_ONLY(stack_size -= guard_size);
1101   pthread_attr_getdetachstate(attr, &detachstate);
1102   jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
1103     stack_size / 1024, guard_size / 1024,
1104     (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
1105   return buf;
1106 }
1107 
1108 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
1109 
1110   if (filename == NULL || outbuf == NULL || outbuflen < 1) {
1111     assert(false, "os::Posix::realpath: invalid arguments.");
1112     errno = EINVAL;
1113     return NULL;
1114   }
1115 
1116   char* result = NULL;
1117 
1118   // This assumes platform realpath() is implemented according to POSIX.1-2008.
1119   // POSIX.1-2008 allows to specify NULL for the output buffer, in which case
1120   // output buffer is dynamically allocated and must be ::free()'d by the caller.
1121   char* p = ::realpath(filename, NULL);
1122   if (p != NULL) {
1123     if (strlen(p) < outbuflen) {
1124       strcpy(outbuf, p);
1125       result = outbuf;
1126     } else {
1127       errno = ENAMETOOLONG;
1128     }
1129     ::free(p); // *not* os::free
1130   } else {
1131     // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath
1132     // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and
1133     // that it complains about the NULL we handed down as user buffer.
1134     // In this case, use the user provided buffer but at least check whether realpath caused
1135     // a memory overwrite.
1136     if (errno == EINVAL) {
1137       outbuf[outbuflen - 1] = '\0';
1138       p = ::realpath(filename, outbuf);
1139       if (p != NULL) {
1140         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwriter detected.");
1141         result = p;
1142       }
1143     }
1144   }
1145   return result;
1146 
1147 }
1148 
1149 
1150 // Check minimum allowable stack sizes for thread creation and to initialize
1151 // the java system classes, including StackOverflowError - depends on page
1152 // size.
1153 // The space needed for frames during startup is platform dependent. It
1154 // depends on word size, platform calling conventions, C frame layout and
1155 // interpreter/C1/C2 design decisions. Therefore this is given in a
1156 // platform (os/cpu) dependent constant.
1157 // To this, space for guard mechanisms is added, which depends on the
1158 // page size which again depends on the concrete system the VM is running
1159 // on. Space for libc guard pages is not included in this size.
1160 jint os::Posix::set_minimum_stack_sizes() {
1161   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1162                                    JavaThread::stack_guard_zone_size() +
1163                                    JavaThread::stack_shadow_zone_size();
1164 
1165   _java_thread_min_stack_allowed = align_size_up(_java_thread_min_stack_allowed, vm_page_size());
1166 
1167   size_t stack_size_in_bytes = ThreadStackSize * K;
1168   if (stack_size_in_bytes != 0 &&
1169       stack_size_in_bytes < _java_thread_min_stack_allowed) {


< prev index next >