Skip to content

Commit e7d0278

Browse files
authored
Improve security manager support (#11466)
1 parent 300ad5e commit e7d0278

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/SupportabilityMetrics.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.instrumentation.api.internal;
77

88
import io.opentelemetry.api.trace.SpanKind;
9+
import java.security.PrivilegedAction;
910
import java.util.concurrent.ConcurrentHashMap;
1011
import java.util.concurrent.ConcurrentMap;
1112
import java.util.concurrent.Executors;
@@ -88,12 +89,14 @@ private SupportabilityMetrics start() {
8889
ScheduledExecutorService executor =
8990
Executors.newScheduledThreadPool(
9091
1,
91-
runnable -> {
92-
Thread result = new Thread(runnable, "supportability_metrics_reporter");
93-
result.setDaemon(true);
94-
result.setContextClassLoader(null);
95-
return result;
96-
});
92+
runnable ->
93+
doPrivileged(
94+
() -> {
95+
Thread result = new Thread(runnable, "supportability_metrics_reporter");
96+
result.setDaemon(true);
97+
result.setContextClassLoader(null);
98+
return result;
99+
}));
97100
executor.scheduleAtFixedRate(this::report, 5, 5, TimeUnit.SECONDS);
98101
// the condition below will always be false, but by referencing the executor it ensures the
99102
// executor can't become unreachable in the middle of the scheduleAtFixedRate() method
@@ -107,6 +110,13 @@ private SupportabilityMetrics start() {
107110
return this;
108111
}
109112

113+
private static <T> T doPrivileged(PrivilegedAction<T> action) {
114+
if (System.getSecurityManager() == null) {
115+
return action.run();
116+
}
117+
return java.security.AccessController.doPrivileged(action);
118+
}
119+
110120
/**
111121
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
112122
* any time.

javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/AgentInitializer.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public Void run() throws Exception {
5858
}
5959

6060
private static void execute(PrivilegedExceptionAction<Void> action) throws Exception {
61-
if (isSecurityManagerSupportEnabled && System.getSecurityManager() != null) {
61+
// When security manager support is enabled we use doPrivileged even if security manager is not
62+
// present because security manager could be installed later. ByteBuddy initialization captures
63+
// the access control context used during transformation. If we don't use doPrivileged here then
64+
// that context will not have the privileges if security manager is installed later.
65+
if (isSecurityManagerSupportEnabled) {
6266
doPrivilegedExceptionAction(action);
6367
} else {
6468
action.run();

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/field/RuntimeFieldBasedImplementationSupplier.java

+10
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@
1111
import io.opentelemetry.instrumentation.api.util.VirtualField;
1212
import java.lang.reflect.InvocationTargetException;
1313
import java.lang.reflect.Method;
14+
import java.security.PrivilegedAction;
1415

1516
final class RuntimeFieldBasedImplementationSupplier
1617
implements RuntimeVirtualFieldSupplier.VirtualFieldSupplier {
1718

1819
@Override
1920
public <U extends T, V extends F, T, F> VirtualField<U, V> find(
2021
Class<T> type, Class<F> fieldType) {
22+
if (System.getSecurityManager() == null) {
23+
return findInternal(type, fieldType);
24+
}
25+
return java.security.AccessController.doPrivileged(
26+
(PrivilegedAction<VirtualField<U, V>>) () -> findInternal(type, fieldType));
27+
}
28+
29+
private static <U extends T, V extends F, T, F> VirtualField<U, V> findInternal(
30+
Class<T> type, Class<F> fieldType) {
2131
try {
2232
String virtualFieldImplClassName =
2333
getVirtualFieldImplementationClassName(type.getTypeName(), fieldType.getTypeName());

0 commit comments

Comments
 (0)