Skip to content

Commit 8255ffa

Browse files
committed
Fix use of AccessController in LoaderUtil
This fixes issue #2129 where `AccessController::doPrivileged` is needlessly invoked when no `SecurityManager` is installed.
1 parent b747810 commit 8255ffa

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,10 @@ public final class LoaderUtil {
6565
} catch (final SecurityException ignored) {
6666
try {
6767
// let's see if we can obtain that permission
68-
AccessController.doPrivileged(
69-
(PrivilegedAction<Void>) () -> {
70-
AccessController.checkPermission(GET_CLASS_LOADER);
71-
return null;
72-
},
73-
null,
74-
GET_CLASS_LOADER);
68+
runPrivilegedActionWithGetClassLoaderPermission((PrivilegedAction<Void>) () -> {
69+
AccessController.checkPermission(GET_CLASS_LOADER);
70+
return null;
71+
});
7572
getClassLoaderDisabled = false;
7673
} catch (final SecurityException ignore) {
7774
// no chance
@@ -108,7 +105,7 @@ public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> c
108105
}
109106
return isChild(loader1, loader2) ? loader1 : loader2;
110107
};
111-
return AccessController.doPrivileged(action, null, GET_CLASS_LOADER);
108+
return runActionInvolvingGetClassLoaderPermission(action);
112109
}
113110

114111
/**
@@ -143,22 +140,29 @@ private static boolean isChild(final ClassLoader loader1, final ClassLoader load
143140
* @return the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined
144141
*/
145142
public static ClassLoader getThreadContextClassLoader() {
146-
if (GET_CLASS_LOADER_DISABLED) {
147-
// we can at least get this class's ClassLoader regardless of security context
148-
// however, if this is null, there's really no option left at this point
149-
try {
150-
return getThisClassLoader();
151-
} catch (final SecurityException ignored) {
152-
return null;
153-
}
143+
try {
144+
return GET_CLASS_LOADER_DISABLED
145+
? getThisClassLoader()
146+
: runActionInvolvingGetClassLoaderPermission(TCCL_GETTER);
147+
} catch (final SecurityException ignored) {
148+
return null;
154149
}
155-
return AccessController.doPrivileged(TCCL_GETTER, null, GET_CLASS_LOADER);
156150
}
157151

158152
private static ClassLoader getThisClassLoader() {
159153
return LoaderUtil.class.getClassLoader();
160154
}
161155

156+
private static <T> T runActionInvolvingGetClassLoaderPermission(final PrivilegedAction<T> action) {
157+
return System.getSecurityManager() != null
158+
? runPrivilegedActionWithGetClassLoaderPermission(action)
159+
: action.run();
160+
}
161+
162+
private static <T> T runPrivilegedActionWithGetClassLoaderPermission(final PrivilegedAction<T> action) {
163+
return AccessController.doPrivileged(action, null, GET_CLASS_LOADER);
164+
}
165+
162166
private static class ThreadContextClassLoaderGetter implements PrivilegedAction<ClassLoader> {
163167
@Override
164168
public ClassLoader run() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
21+
type="fixed">
22+
<issue id="2129" link="https://github.com/apache/logging-log4j2/issues/2129"/>
23+
<description format="asciidoc">
24+
Fixed use of `SecurityManager` in `LoaderUtil` where `AccessController::doPrivileged` should only be invoked when
25+
a `SecurityManager` is installed. Some runtimes do not seem to have this method available.
26+
</description>
27+
</entry>

0 commit comments

Comments
 (0)