Skip to content

Commit 425a37a

Browse files
committed
Verifying the capability of PrivateSecurityManager so platforms not (fully) supporting SecurityManager do not poison the stack trace.
1 parent 7acbc48 commit 425a37a

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/util/PrivateSecurityManagerStackTraceUtil.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ final class PrivateSecurityManagerStackTraceUtil {
2929
private static final PrivateSecurityManager SECURITY_MANAGER;
3030

3131
static {
32+
PrivateSecurityManager candidate = createPrivateSecurityManager();
33+
if (isCapable(candidate)) {
34+
SECURITY_MANAGER = candidate;
35+
} else {
36+
SECURITY_MANAGER = null;
37+
}
38+
}
39+
40+
private static boolean isCapable(PrivateSecurityManager candidate) {
41+
if (candidate == null) {
42+
return false;
43+
}
44+
45+
try {
46+
final Class<?>[] result = candidate.getClassContext();
47+
if (result == null || result.length == 0) {
48+
// This happens e.g. on Android which has real implementation of SecurityManager replaced with merely stubs.
49+
// So the PrivateSecurityManager, though can be instantiated, will not produce meaningful results
50+
return false;
51+
}
52+
// Add more checks here as needed
53+
return true;
54+
} catch (Exception ignored) {
55+
return false;
56+
}
57+
}
58+
59+
private static PrivateSecurityManager createPrivateSecurityManager() {
3260
PrivateSecurityManager psm;
3361
try {
3462
final SecurityManager sm = System.getSecurityManager();
@@ -40,7 +68,7 @@ final class PrivateSecurityManagerStackTraceUtil {
4068
psm = null;
4169
}
4270

43-
SECURITY_MANAGER = psm;
71+
return psm;
4472
}
4573

4674
private PrivateSecurityManagerStackTraceUtil() {

0 commit comments

Comments
 (0)