Skip to content

Commit

Permalink
Improve security manager support (open-telemetry#11466)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored and zeitlinger committed Jun 12, 2024
1 parent f6be3f8 commit 055de11
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.api.internal;

import io.opentelemetry.api.trace.SpanKind;
import java.security.PrivilegedAction;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -88,12 +89,14 @@ private SupportabilityMetrics start() {
ScheduledExecutorService executor =
Executors.newScheduledThreadPool(
1,
runnable -> {
Thread result = new Thread(runnable, "supportability_metrics_reporter");
result.setDaemon(true);
result.setContextClassLoader(null);
return result;
});
runnable ->
doPrivileged(
() -> {
Thread result = new Thread(runnable, "supportability_metrics_reporter");
result.setDaemon(true);
result.setContextClassLoader(null);
return result;
}));
executor.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS);
// the condition below will always be false, but by referencing the executor it ensures the
// executor can't become unreachable in the middle of the scheduleAtFixedRate() method
Expand All @@ -107,6 +110,13 @@ private SupportabilityMetrics start() {
return this;
}

private static <T> T doPrivileged(PrivilegedAction<T> action) {
if (System.getSecurityManager() == null) {
return action.run();
}
return java.security.AccessController.doPrivileged(action);
}

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public Void run() throws Exception {
}

private static void execute(PrivilegedExceptionAction<Void> action) throws Exception {
if (isSecurityManagerSupportEnabled && System.getSecurityManager() != null) {
// When security manager support is enabled we use doPrivileged even if security manager is not
// present because security manager could be installed later. ByteBuddy initialization captures
// the access control context used during transformation. If we don't use doPrivileged here then
// that context will not have the privileges if security manager is installed later.
if (isSecurityManagerSupportEnabled) {
doPrivilegedExceptionAction(action);
} else {
action.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
import io.opentelemetry.instrumentation.api.util.VirtualField;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.PrivilegedAction;

final class RuntimeFieldBasedImplementationSupplier
implements RuntimeVirtualFieldSupplier.VirtualFieldSupplier {

@Override
public <U extends T, V extends F, T, F> VirtualField<U, V> find(
Class<T> type, Class<F> fieldType) {
if (System.getSecurityManager() == null) {
return findInternal(type, fieldType);
}
return java.security.AccessController.doPrivileged(
(PrivilegedAction<VirtualField<U, V>>) () -> findInternal(type, fieldType));
}

private static <U extends T, V extends F, T, F> VirtualField<U, V> findInternal(
Class<T> type, Class<F> fieldType) {
try {
String virtualFieldImplClassName =
getVirtualFieldImplementationClassName(type.getTypeName(), fieldType.getTypeName());
Expand Down

0 comments on commit 055de11

Please sign in to comment.