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

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