Skip to content

Commit 30efcc6

Browse files
committed
Correctly identify system modules not included in boot module layer
1 parent 782a36c commit 30efcc6

File tree

1 file changed

+22
-14
lines changed
  • libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy

1 file changed

+22
-14
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.lang.StackWalker.StackFrame;
3535
import java.lang.module.ModuleFinder;
3636
import java.lang.module.ModuleReference;
37+
import java.lang.module.ResolvedModule;
3738
import java.nio.file.Path;
3839
import java.util.ArrayList;
3940
import java.util.HashSet;
@@ -123,20 +124,10 @@ ModuleEntitlements policyEntitlements(String componentName, String moduleName, L
123124

124125
public static final String ALL_UNNAMED = "ALL-UNNAMED";
125126

126-
private static final Set<Module> systemModules = findSystemModules();
127+
private static final Set<ModuleReference> systemModules = findSystemModules();
127128

128-
private static Set<Module> findSystemModules() {
129-
var systemModulesDescriptors = ModuleFinder.ofSystem()
130-
.findAll()
131-
.stream()
132-
.map(ModuleReference::descriptor)
133-
.collect(Collectors.toUnmodifiableSet());
134-
return Stream.concat(
135-
// entitlements is a "system" module, we can do anything from it
136-
Stream.of(PolicyManager.class.getModule()),
137-
// anything in the boot layer is also part of the system
138-
ModuleLayer.boot().modules().stream().filter(m -> systemModulesDescriptors.contains(m.getDescriptor()))
139-
).collect(Collectors.toUnmodifiableSet());
129+
private static Set<ModuleReference> findSystemModules() {
130+
return ModuleFinder.ofSystem().findAll().stream().collect(Collectors.toUnmodifiableSet());
140131
}
141132

142133
/**
@@ -615,14 +606,31 @@ private static boolean isTriviallyAllowed(Class<?> requestingClass) {
615606
logger.debug("Entitlement trivially allowed: no caller frames outside the entitlement library");
616607
return true;
617608
}
618-
if (systemModules.contains(requestingClass.getModule())) {
609+
if (isSystemModule(requestingClass.getModule())) {
619610
logger.debug("Entitlement trivially allowed from system module [{}]", requestingClass.getModule().getName());
620611
return true;
621612
}
622613
logger.trace("Entitlement not trivially allowed");
623614
return false;
624615
}
625616

617+
/**
618+
* Determines if the given {@link Module} is a system module. That is, a module that is included as part of the Java runtime.
619+
*/
620+
private static boolean isSystemModule(Module module) {
621+
if (module.getLayer() == null) {
622+
// This is an unnamed module, so not a system module
623+
return false;
624+
}
625+
626+
ModuleReference moduleReference = module.getLayer()
627+
.configuration()
628+
.findModule(module.getName())
629+
.map(ResolvedModule::reference)
630+
.orElse(null);
631+
return module == PolicyManager.class.getModule() || (moduleReference != null && systemModules.contains(moduleReference));
632+
}
633+
626634
@Override
627635
public String toString() {
628636
return "PolicyManager{" + "serverEntitlements=" + serverEntitlements + ", pluginsEntitlements=" + pluginsEntitlements + '}';

0 commit comments

Comments
 (0)