Skip to content

Commit db0360d

Browse files
authored
A way to disable the optimization for filling the stack traces (#3638)
* Verifying the capability of PrivateSecurityManager so platforms not (fully) supporting SecurityManager do not poison the stack trace. * Changelog
1 parent 7acbc48 commit db0360d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ 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
49+
// stubs. So the PrivateSecurityManager, though can be instantiated, will not produce meaningful
50+
// results
51+
return false;
52+
}
53+
// Add more checks here as needed
54+
return true;
55+
} catch (Exception ignored) {
56+
return false;
57+
}
58+
}
59+
60+
private static PrivateSecurityManager createPrivateSecurityManager() {
3261
PrivateSecurityManager psm;
3362
try {
3463
final SecurityManager sm = System.getSecurityManager();
@@ -40,7 +69,7 @@ final class PrivateSecurityManagerStackTraceUtil {
4069
psm = null;
4170
}
4271

43-
SECURITY_MANAGER = psm;
72+
return psm;
4473
}
4574

4675
private PrivateSecurityManagerStackTraceUtil() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3639" link="https://github.com/apache/logging-log4j2/issues/3639"/>
7+
<description format="asciidoc">
8+
Verify the capability of SecurityManager so that platforms not (fully) supporting it will not poison the stack trace
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)