# HG changeset patch # User goetz # Date 1504247309 -7200 # Node ID aecdbd67e08aad8d3949b222e6f71cf6c5a03ad8 # Parent a8ec32aa385e44f82629b205b8fb37a8ad6fbd3a 8187045: [linux] Not all libraries in the VM are linked with -z,noexecstack. diff --git a/make/lib/CompileLibjsig.gmk b/make/lib/CompileLibjsig.gmk --- a/make/lib/CompileLibjsig.gmk +++ b/make/lib/CompileLibjsig.gmk @@ -36,7 +36,7 @@ ifeq ($(STATIC_BUILD), false) ifeq ($(OPENJDK_TARGET_OS), linux) LIBJSIG_CFLAGS := -fPIC -D_GNU_SOURCE -D_REENTRANT $(EXTRA_CFLAGS) - LIBJSIG_LDFLAGS := $(LDFLAGS_HASH_STYLE) $(EXTRA_CFLAGS) + LIBJSIG_LDFLAGS := $(LDFLAGS_HASH_STYLE) ${LDFLAGS_NO_EXEC_STACK} $(EXTRA_CFLAGS) LIBJSIG_LIBS := $(LIBDL) # NOTE: The old build compiled this library without -soname. diff --git a/src/share/vm/prims/whitebox.cpp b/src/share/vm/prims/whitebox.cpp --- a/src/share/vm/prims/whitebox.cpp +++ b/src/share/vm/prims/whitebox.cpp @@ -73,6 +73,10 @@ #include "utilities/nativeCallStack.hpp" #endif // INCLUDE_NMT +#ifdef LINUX +#include "utilities/elfFile.hpp" +#endif + #define SIZE_T_MAX_VALUE ((size_t) -1) @@ -1823,6 +1827,20 @@ DirectivesStack::pop(count); WB_END +// Checks that the library libfile has the noexecstack bit set. +WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile)) + jboolean ret = false; +#ifdef LINUX + // Can't be in VM when we call JNI. + ThreadToNativeFromVM ttnfv(thread); + const char* lf = env->GetStringUTFChars(libfile, NULL); + CHECK_JNI_EXCEPTION_(env, 0); + ret = (jboolean) ElfFile::specifies_noexecstack(lf); + env->ReleaseStringUTFChars(libfile, lf); +#endif + return ret; +WB_END + #define CC (char*) static JNINativeMethod methods[] = { @@ -2027,6 +2045,8 @@ (void*)&WB_GetConcurrentGCPhases}, {CC"requestConcurrentGCPhase0", CC"(Ljava/lang/String;)Z", (void*)&WB_RequestConcurrentGCPhase}, + {CC"checkLibSpecifiesNoexecstack", CC"(Ljava/lang/String;)Z", + (void*)&WB_CheckLibSpecifiesNoexecstack}, }; #undef CC diff --git a/test/runtime/execstack/TestCheckJDK.java b/test/runtime/execstack/TestCheckJDK.java new file mode 100644 --- /dev/null +++ b/test/runtime/execstack/TestCheckJDK.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test Testexecstack.java + * @summary Searches for all libraries in test VM and checks that they + * have the noexecstack bit set. + * @requires (os.family == "linux") + * @library /test/lib + * @modules java.base/jdk.internal.misc + * @build sun.hotspot.WhiteBox + * @run driver ClassFileInstaller sun.hotspot.WhiteBox + * sun.hotspot.WhiteBox$WhiteBoxPermission + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI + * TestCheckJDK + */ + +import jdk.test.lib.Asserts; +import sun.hotspot.WhiteBox; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class TestCheckJDK { + static boolean testPassed = true; + private static final WhiteBox WB = WhiteBox.getWhiteBox(); + + static void checkExecStack(Path file) { + String filename = file.toString(); + if (filename.endsWith(".so")) { + if (!WB.checkLibSpecifiesNoexecstack(filename)) { + System.out.println("Library does not have the noexecstack bit set: " + filename); + testPassed = false; + } + } + } + + public static void main(String[] args) throws Throwable { + String vmInstallDir = System.getProperty("java.home"); + + Files.walk(Paths.get(vmInstallDir)).filter(Files::isRegularFile).forEach(TestCheckJDK::checkExecStack); + + Asserts.assertTrue(testPassed, + "The tested VM contains libs that don't have the noexecstack " + + "bit set. They must be linked with -z,noexecstack."); + } +}