Skip to content

Commit

Permalink
Prevent 'sun.security.util.Debug' from being reachable.
Browse files Browse the repository at this point in the history
Suppress deprecation warnings.
Fix review feedback
  • Loading branch information
lazar-mitrovic committed Nov 10, 2021
1 parent b01f54f commit cf6e8fa
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 209 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -45,9 +45,7 @@
import org.graalvm.nativeimage.c.function.CEntryPointLiteral;

public interface ProcessPropertiesSupport {
default String getExecutableName() {
return "java";
}
String getExecutableName();

long getProcessID();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, 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
Expand Down Expand Up @@ -53,13 +53,16 @@
import java.util.Set;
import java.util.StringJoiner;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import org.graalvm.compiler.core.common.NumUtil;
import org.graalvm.compiler.core.common.SuppressFBWarnings;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.ProcessProperties;
import org.graalvm.nativeimage.c.function.CFunctionPointer;
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
import org.graalvm.util.DirectAnnotationAccess;

import com.oracle.svm.core.RuntimeAssertionsSupport;
Expand Down Expand Up @@ -319,23 +322,25 @@ public void setModule(Object module) {
}

/**
* Final fields in subsituted classes are treated as implicitly RecomputeFieldValue even when
* Final fields in substituted classes are treated as implicitly RecomputeFieldValue even when
* not annotated with @RecomputeFieldValue. Their name must not match a field in the original
* class, i.e., allPermDomain.
*/
static final LazyFinalReference<java.security.ProtectionDomain> allPermDomainReference = new LazyFinalReference<>(() -> {
java.security.Permissions perms = new java.security.Permissions();
perms.add(SecurityConstants.ALL_PERMISSION);
CodeSource cs;
try {
CodeSource cs = null;

if (ImageSingletons.lookup(ProcessPropertiesSupport.class) instanceof BaseProcessPropertiesSupport) {
// Try to use executable image's name as code source for the class.
// The file location can be used by Java code to determine its location on disk, similar
// to argv[0].
cs = new CodeSource(new File(ProcessProperties.getExecutableName()).toURI().toURL(), (Certificate[]) null);
} catch (MalformedURLException ex) {
// This should not really happen; the file is cannonicalized, absolute, so it should
// always have file:// URL.
cs = null;
try {
cs = new CodeSource(new File(ProcessProperties.getExecutableName()).toURI().toURL(), (Certificate[]) null);
} catch (MalformedURLException e) {
// This should not really happen; the file is cannonicalized, absolute, so it should
// always have file:// URL.
}
}
return new java.security.ProtectionDomain(cs, perms);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2021, 2021, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
package com.oracle.svm.core.jdk;

import com.oracle.svm.core.threadlocal.FastThreadLocalFactory;
import com.oracle.svm.core.threadlocal.FastThreadLocalObject;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;

import java.security.AccessControlContext;
import java.security.PrivilegedActionException;
import java.security.ProtectionDomain;
import java.util.ArrayDeque;
import java.util.Objects;

/**
* Stack for storing AccessControlContexts. Used in conjunction with
* {@code StackAccessControlContextVisitor}.
*/
class PrivilegedStack {

public static class StackElement {
protected AccessControlContext context;
protected Class<?> caller;

StackElement(AccessControlContext context, Class<?> caller) {
this.context = context;
this.caller = caller;
}

public AccessControlContext getContext() {
return context;
}

public Class<?> getCaller() {
return caller;
}
}

/* Local AccessControlContext stack */
private static final FastThreadLocalObject<ArrayDeque<StackElement>> stack;

static {

@SuppressWarnings("unchecked")
Class<ArrayDeque<StackElement>> cls = (Class<ArrayDeque<StackElement>>) (Object) ArrayDeque.class;
stack = FastThreadLocalFactory.createObject(cls, "AccessControlContextStack");
}

@SuppressWarnings("unchecked")
private static ArrayDeque<StackElement> getStack() {
ensureInitialized();
return stack.get();
}

private static void ensureInitialized() {
if (stack.get() == null) {
ArrayDeque<StackElement> tmp = new ArrayDeque<>();
stack.set(tmp);
}
}

public static void push(AccessControlContext context, Class<?> caller) {
getStack().push(new StackElement(context, caller));
}

public static void pop() {
getStack().pop();
}

public static AccessControlContext peekContext() {
return Objects.requireNonNull(getStack().peek()).getContext();
}

public static Class<?> peekCaller() {
return Objects.requireNonNull(getStack().peek()).getCaller();
}

public static int length() {
return getStack().size();
}
}

@InternalVMMethod
@SuppressWarnings({"unused"})
public class AccessControllerUtil {

/**
* Instance that is used to mark contexts that were disallowed in
* {@code AccessControlContextReplacerFeature.replaceAccessControlContext()} If this marker is
* passed to {@code AccessController.doPrivileged()} a runtime error will be thrown.
*/
public static final AccessControlContext DISALLOWED_CONTEXT_MARKER;

static {
try {
DISALLOWED_CONTEXT_MARKER = ReflectionUtil.lookupConstructor(AccessControlContext.class, ProtectionDomain[].class, boolean.class).newInstance(new ProtectionDomain[0], true);
} catch (ReflectiveOperationException ex) {
throw VMError.shouldNotReachHere(ex);
}
}

static Throwable wrapCheckedException(Throwable ex) {
if (ex instanceof Exception && !(ex instanceof RuntimeException)) {
return new PrivilegedActionException((Exception) ex);
} else {
return ex;
}
}

static Throwable wrapCheckedExceptionForPrivilegedAction(Throwable ex) {
if (JavaVersionUtil.JAVA_SPEC <= 11) {
return wrapCheckedException(ex);
}
return ex;
}
}
Loading

0 comments on commit cf6e8fa

Please sign in to comment.