Blame SOURCES/jdk8197429-pr3546-rh1536622-increased_stack_guard_causes_segfaults_on_x86_32.patch

9fd28f
# HG changeset patch
9fd28f
# User aph
9fd28f
# Date 1530894306 -3600
9fd28f
#      Fri Jul 06 17:25:06 2018 +0100
9fd28f
# Node ID 1485461a0fd1ff977a6acb8f2ed1069aaaf3b07e
9fd28f
# Parent  d7bcbcfde5057ad066ad2fb55a87d19a5827ddee
9fd28f
8197429: Increased stack guard causes segfaults on x86-32
9fd28f
Reviewed-by: dholmes
9fd28f
9fd28f
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp openjdk/hotspot/src/os/linux/vm/os_linux.cpp
9fd28f
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.cpp
9fd28f
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp
9fd28f
@@ -724,6 +724,10 @@
9fd28f
   }
9fd28f
 }
9fd28f
 
9fd28f
+void os::Linux::expand_stack_to(address bottom) {
9fd28f
+  _expand_stack_to(bottom);
9fd28f
+}
9fd28f
+
9fd28f
 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
9fd28f
   assert(t!=NULL, "just checking");
9fd28f
   assert(t->osthread()->expanding_stack(), "expand should be set");
9fd28f
diff --git openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp openjdk/hotspot/src/os/linux/vm/os_linux.hpp
9fd28f
--- openjdk.orig/hotspot/src/os/linux/vm/os_linux.hpp
9fd28f
+++ openjdk/hotspot/src/os/linux/vm/os_linux.hpp
9fd28f
@@ -249,6 +249,8 @@
9fd28f
   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
9fd28f
 
9fd28f
 private:
9fd28f
+  static void expand_stack_to(address bottom);
9fd28f
+
9fd28f
   typedef int (*sched_getcpu_func_t)(void);
9fd28f
   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
9fd28f
   typedef int (*numa_max_node_func_t)(void);
9fd28f
diff --git openjdk.orig/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
9fd28f
--- openjdk.orig/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
9fd28f
+++ openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
9fd28f
@@ -892,6 +892,27 @@
9fd28f
 void os::workaround_expand_exec_shield_cs_limit() {
9fd28f
 #if defined(IA32)
9fd28f
   size_t page_size = os::vm_page_size();
9fd28f
+
9fd28f
+  /*
9fd28f
+   * JDK-8197429
9fd28f
+   *
9fd28f
+   * Expand the stack mapping to the end of the initial stack before
9fd28f
+   * attempting to install the codebuf.  This is needed because newer
9fd28f
+   * Linux kernels impose a distance of a megabyte between stack
9fd28f
+   * memory and other memory regions.  If we try to install the
9fd28f
+   * codebuf before expanding the stack the installation will appear
9fd28f
+   * to succeed but we'll get a segfault later if we expand the stack
9fd28f
+   * in Java code.
9fd28f
+   *
9fd28f
+   */
9fd28f
+  if (os::is_primordial_thread()) {
9fd28f
+    address limit = Linux::initial_thread_stack_bottom();
9fd28f
+    if (! DisablePrimordialThreadGuardPages) {
9fd28f
+      limit += (StackYellowPages + StackRedPages) * page_size;
9fd28f
+    }
9fd28f
+    os::Linux::expand_stack_to(limit);
9fd28f
+  }
9fd28f
+
9fd28f
   /*
9fd28f
    * Take the highest VA the OS will give us and exec
9fd28f
    *
9fd28f
@@ -910,6 +931,16 @@
9fd28f
   char* hint = (char*) (Linux::initial_thread_stack_bottom() -
9fd28f
                         ((StackYellowPages + StackRedPages + 1) * page_size));
9fd28f
   char* codebuf = os::attempt_reserve_memory_at(page_size, hint);
9fd28f
+
9fd28f
+  if (codebuf == NULL) {
9fd28f
+    // JDK-8197429: There may be a stack gap of one megabyte between
9fd28f
+    // the limit of the stack and the nearest memory region: this is a
9fd28f
+    // Linux kernel workaround for CVE-2017-1000364.  If we failed to
9fd28f
+    // map our codebuf, try again at an address one megabyte lower.
9fd28f
+    hint -= 1 * M;
9fd28f
+    codebuf = os::attempt_reserve_memory_at(page_size, hint);
9fd28f
+  }
9fd28f
+
9fd28f
   if ( (codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true)) ) {
9fd28f
     return; // No matter, we tried, best effort.
9fd28f
   }
9fd28f
diff --git openjdk.orig/hotspot/test/runtime/StackGap/T.java openjdk/hotspot/test/runtime/StackGap/T.java
9fd28f
new file mode 100644
9fd28f
--- /dev/null
9fd28f
+++ openjdk/hotspot/test/runtime/StackGap/T.java
9fd28f
@@ -0,0 +1,33 @@
9fd28f
+/*
9fd28f
+ * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
9fd28f
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fd28f
+ *
9fd28f
+ * This code is free software; you can redistribute it and/or modify it
9fd28f
+ * under the terms of the GNU General Public License version 2 only, as
9fd28f
+ * published by the Free Software Foundation.
9fd28f
+ *
9fd28f
+ * This code is distributed in the hope that it will be useful, but WITHOUT
9fd28f
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fd28f
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fd28f
+ * version 2 for more details (a copy is included in the LICENSE file that
9fd28f
+ * accompanied this code).
9fd28f
+ *
9fd28f
+ * You should have received a copy of the GNU General Public License version
9fd28f
+ * 2 along with this work; if not, write to the Free Software Foundation,
9fd28f
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fd28f
+ *
9fd28f
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fd28f
+ * or visit www.oracle.com if you need additional information or have any
9fd28f
+ * questions.
9fd28f
+ */
9fd28f
+
9fd28f
+public class T {
9fd28f
+
9fd28f
+  public static void test(int n) {
9fd28f
+    if (n == 0) return;
9fd28f
+    System.out.println (n);
9fd28f
+    test (n - 1);
9fd28f
+
9fd28f
+  }
9fd28f
+
9fd28f
+}
9fd28f
diff --git openjdk.orig/hotspot/test/runtime/StackGap/exestack-gap.c openjdk/hotspot/test/runtime/StackGap/exestack-gap.c
9fd28f
new file mode 100644
9fd28f
--- /dev/null
9fd28f
+++ openjdk/hotspot/test/runtime/StackGap/exestack-gap.c
9fd28f
@@ -0,0 +1,82 @@
9fd28f
+/*
9fd28f
+ * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
9fd28f
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fd28f
+ *
9fd28f
+ * This code is free software; you can redistribute it and/or modify it
9fd28f
+ * under the terms of the GNU General Public License version 2 only, as
9fd28f
+ * published by the Free Software Foundation.
9fd28f
+ *
9fd28f
+ * This code is distributed in the hope that it will be useful, but WITHOUT
9fd28f
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fd28f
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fd28f
+ * version 2 for more details (a copy is included in the LICENSE file that
9fd28f
+ * accompanied this code).
9fd28f
+ *
9fd28f
+ * You should have received a copy of the GNU General Public License version
9fd28f
+ * 2 along with this work; if not, write to the Free Software Foundation,
9fd28f
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fd28f
+ *
9fd28f
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fd28f
+ * or visit www.oracle.com if you need additional information or have any
9fd28f
+ * questions.
9fd28f
+ */
9fd28f
+
9fd28f
+#include <jni.h>
9fd28f
+#include <stdio.h>
9fd28f
+#include <stdlib.h>
9fd28f
+
9fd28f
+JNIEnv* create_vm(JavaVM **jvm, char *extra_option)
9fd28f
+{
9fd28f
+    JNIEnv* env;
9fd28f
+    JavaVMInitArgs args;
9fd28f
+    JavaVMOption options[4];
9fd28f
+    args.version = JNI_VERSION_1_8;
9fd28f
+    args.nOptions = 3 + (extra_option != NULL);
9fd28f
+    options[0].optionString = "-Xss2048k";
9fd28f
+    char classpath[4096];
9fd28f
+    snprintf(classpath, sizeof classpath,
9fd28f
+             "-Djava.class.path=%s", getenv("CLASSPATH"));
9fd28f
+    options[1].optionString = classpath;
9fd28f
+    options[2].optionString = "-XX:+UnlockExperimentalVMOptions";
9fd28f
+    if (extra_option) {
9fd28f
+      options[3].optionString = extra_option;
9fd28f
+    }
9fd28f
+    args.options = &options[0];
9fd28f
+    args.ignoreUnrecognized = 0;
9fd28f
+    int rv;
9fd28f
+    rv = JNI_CreateJavaVM(jvm, (void**)&env, &args);
9fd28f
+    if (rv < 0) return NULL;
9fd28f
+    return env;
9fd28f
+}
9fd28f
+
9fd28f
+void run(char *extra_arg) {
9fd28f
+  JavaVM *jvm;
9fd28f
+  jclass T_class;
9fd28f
+  jmethodID test_method;
9fd28f
+  JNIEnv *env = create_vm(&jvm, extra_arg);
9fd28f
+  if (env == NULL)
9fd28f
+    exit(1);
9fd28f
+  T_class = (*env)->FindClass(env, "T");
9fd28f
+  if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
9fd28f
+    (*env)->ExceptionDescribe(env);
9fd28f
+    exit(1);
9fd28f
+  }
9fd28f
+  test_method = (*env)->GetStaticMethodID(env, T_class, "test", "(I)V");
9fd28f
+  if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
9fd28f
+    (*env)->ExceptionDescribe(env);
9fd28f
+    exit(1);
9fd28f
+  }
9fd28f
+  (*env)->CallStaticVoidMethod(env, T_class, test_method, 1000);
9fd28f
+}
9fd28f
+
9fd28f
+
9fd28f
+int main(int argc, char **argv)
9fd28f
+{
9fd28f
+  if (argc > 1) {
9fd28f
+    run(argv[1]);
9fd28f
+  } else {
9fd28f
+    run(NULL);
9fd28f
+  }
9fd28f
+
9fd28f
+  return 0;
9fd28f
+}
9fd28f
diff --git openjdk.orig/hotspot/test/runtime/StackGap/testme.sh openjdk/hotspot/test/runtime/StackGap/testme.sh
9fd28f
new file mode 100644
9fd28f
--- /dev/null
9fd28f
+++ openjdk/hotspot/test/runtime/StackGap/testme.sh
9fd28f
@@ -0,0 +1,73 @@
9fd28f
+# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
9fd28f
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fd28f
+#
9fd28f
+# This code is free software; you can redistribute it and/or modify it
9fd28f
+# under the terms of the GNU General Public License version 2 only, as
9fd28f
+# published by the Free Software Foundation.
9fd28f
+#
9fd28f
+# This code is distributed in the hope that it will be useful, but WITHOUT
9fd28f
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fd28f
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fd28f
+# version 2 for more details (a copy is included in the LICENSE file that
9fd28f
+# accompanied this code).
9fd28f
+#
9fd28f
+# You should have received a copy of the GNU General Public License version
9fd28f
+# 2 along with this work; if not, write to the Free Software Foundation,
9fd28f
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fd28f
+#
9fd28f
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fd28f
+# or visit www.oracle.com if you need additional information or have any
9fd28f
+# questions.
9fd28f
+#!/bin/sh
9fd28f
+
9fd28f
+#
9fd28f
+# @test testme.sh
9fd28f
+# @bug 8197429
9fd28f
+# @summary Linux kernel stack guard should not cause segfaults on x86-32
9fd28f
+# @compile T.java
9fd28f
+# @run shell testme.sh
9fd28f
+#
9fd28f
+
9fd28f
+if [ "${TESTSRC}" = "" ]
9fd28f
+then
9fd28f
+  TESTSRC=${PWD}
9fd28f
+  echo "TESTSRC not set.  Using "${TESTSRC}" as default"
9fd28f
+fi
9fd28f
+echo "TESTSRC=${TESTSRC}"
9fd28f
+## Adding common setup Variables for running shell tests.
9fd28f
+. ${TESTSRC}/../../test_env.sh
9fd28f
+
9fd28f
+if [ "${VM_OS}" != "linux" ]
9fd28f
+then
9fd28f
+  echo "Test only valid for Linux"
9fd28f
+  exit 0
9fd28f
+fi
9fd28f
+
9fd28f
+gcc_cmd=`which gcc`
9fd28f
+if [ "x$gcc_cmd" = "x" ]; then
9fd28f
+    echo "WARNING: gcc not found. Cannot execute test." 2>&1
9fd28f
+    exit 0;
9fd28f
+fi
9fd28f
+
9fd28f
+CFLAGS="-m${VM_BITS}"
9fd28f
+
9fd28f
+LD_LIBRARY_PATH=.:${COMPILEJAVA}/jre/lib/${VM_CPU}/${VM_TYPE}:/usr/lib:$LD_LIBRARY_PATH
9fd28f
+export LD_LIBRARY_PATH
9fd28f
+
9fd28f
+cp ${TESTSRC}${FS}exestack-gap.c .
9fd28f
+
9fd28f
+# Copy the result of our @compile action:
9fd28f
+cp ${TESTCLASSES}${FS}T.class .
9fd28f
+
9fd28f
+echo "Compilation flag: ${COMP_FLAG}"
9fd28f
+# Note pthread may not be found thus invoke creation will fail to be created.
9fd28f
+# Check to ensure you have a /usr/lib/libpthread.so if you don't please look
9fd28f
+# for /usr/lib/`uname -m`-linux-gnu version ensure to add that path to below compilation.
9fd28f
+
9fd28f
+$gcc_cmd -DLINUX ${CFLAGS} -o stack-gap \
9fd28f
+    -I${COMPILEJAVA}/include -I${COMPILEJAVA}/include/linux \
9fd28f
+    -L${COMPILEJAVA}/jre/lib/${VM_CPU}/${VM_TYPE} \
9fd28f
+    -ljvm -lpthread exestack-gap.c
9fd28f
+
9fd28f
+./stack-gap || exit $?
9fd28f
+./stack-gap -XX:+DisablePrimordialThreadGuardPages || exit $?