Blame SOURCES/8197429-pr3546-rh1536622.patch

43bc7e
diff -r eecfc14e66ee src/os/linux/vm/os_linux.cpp
43bc7e
--- openjdk/hotspot/src/os/linux/vm/os_linux.cpp	Mon Jan 22 16:25:24 2018 +0000
43bc7e
+++ openjdk/hotspot/src/os/linux/vm/os_linux.cpp	Wed Feb 21 13:52:31 2018 +0000
43bc7e
@@ -1,5 +1,5 @@
43bc7e
 /*
43bc7e
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
43bc7e
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
43bc7e
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43bc7e
  *
43bc7e
  * This code is free software; you can redistribute it and/or modify it
43bc7e
@@ -674,6 +674,10 @@
43bc7e
   }
43bc7e
 }
43bc7e
 
43bc7e
+void os::Linux::expand_stack_to(address bottom) {
43bc7e
+  _expand_stack_to(bottom);
43bc7e
+}
43bc7e
+
43bc7e
 bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
43bc7e
   assert(t!=NULL, "just checking");
43bc7e
   assert(t->osthread()->expanding_stack(), "expand should be set");
43bc7e
diff -r eecfc14e66ee src/os/linux/vm/os_linux.hpp
43bc7e
--- openjdk/hotspot/src/os/linux/vm/os_linux.hpp	Mon Jan 22 16:25:24 2018 +0000
43bc7e
+++ openjdk/hotspot/src/os/linux/vm/os_linux.hpp	Wed Feb 21 13:52:31 2018 +0000
43bc7e
@@ -1,5 +1,5 @@
43bc7e
 /*
43bc7e
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
43bc7e
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
43bc7e
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43bc7e
  *
43bc7e
  * This code is free software; you can redistribute it and/or modify it
43bc7e
@@ -245,6 +245,8 @@
43bc7e
   static int safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime);
43bc7e
 
43bc7e
 private:
43bc7e
+  static void expand_stack_to(address bottom);
43bc7e
+
43bc7e
   typedef int (*sched_getcpu_func_t)(void);
43bc7e
   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
43bc7e
   typedef int (*numa_max_node_func_t)(void);
43bc7e
diff -r eecfc14e66ee src/os_cpu/linux_x86/vm/os_linux_x86.cpp
43bc7e
--- openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp	Mon Jan 22 16:25:24 2018 +0000
43bc7e
+++ openjdk/hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp	Wed Feb 21 13:52:31 2018 +0000
43bc7e
@@ -1,5 +1,5 @@
43bc7e
 /*
43bc7e
- * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
43bc7e
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
43bc7e
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43bc7e
  *
43bc7e
  * This code is free software; you can redistribute it and/or modify it
43bc7e
@@ -892,6 +892,25 @@
43bc7e
 void os::workaround_expand_exec_shield_cs_limit() {
43bc7e
 #if defined(IA32)
43bc7e
   size_t page_size = os::vm_page_size();
43bc7e
+
43bc7e
+  /*
43bc7e
+   * JDK-8197429
43bc7e
+   *
43bc7e
+   * Expand the stack mapping to the end of the initial stack before
43bc7e
+   * attempting to install the codebuf.  This is needed because newer
43bc7e
+   * Linux kernels impose a distance of a megabyte between stack
43bc7e
+   * memory and other memory regions.  If we try to install the
43bc7e
+   * codebuf before expanding the stack the installation will appear
43bc7e
+   * to succeed but we'll get a segfault later if we expand the stack
43bc7e
+   * in Java code.
43bc7e
+   *
43bc7e
+   */
43bc7e
+  if (os::Linux::is_initial_thread()) {
43bc7e
+    address limit = Linux::initial_thread_stack_bottom();
43bc7e
+    limit += (StackYellowPages + StackRedPages) * page_size;
43bc7e
+    os::Linux::expand_stack_to(limit);
43bc7e
+  }
43bc7e
+
43bc7e
   /*
43bc7e
    * Take the highest VA the OS will give us and exec
43bc7e
    *
43bc7e
@@ -910,6 +929,16 @@
43bc7e
   char* hint = (char*) (Linux::initial_thread_stack_bottom() -
43bc7e
                         ((StackYellowPages + StackRedPages + 1) * page_size));
43bc7e
   char* codebuf = os::attempt_reserve_memory_at(page_size, hint);
43bc7e
+
43bc7e
+  if (codebuf == NULL) {
43bc7e
+    // JDK-8197429: There may be a stack gap of one megabyte between
43bc7e
+    // the limit of the stack and the nearest memory region: this is a
43bc7e
+    // Linux kernel workaround for CVE-2017-1000364.  If we failed to
43bc7e
+    // map our codebuf, try again at an address one megabyte lower.
43bc7e
+    hint -= 1 * M;
43bc7e
+    codebuf = os::attempt_reserve_memory_at(page_size, hint);
43bc7e
+  }
43bc7e
+
43bc7e
   if ( (codebuf == NULL) || (!os::commit_memory(codebuf, page_size, true)) ) {
43bc7e
     return; // No matter, we tried, best effort.
43bc7e
   }