Implementing the correct exit functions (Runtime) (#118657)

This commit is contained in:
Lorenzo Dematté 2024-12-13 16:21:42 +01:00 committed by GitHub
parent 44a231acf2
commit 1bad1cf6b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 9 deletions

View file

@ -13,7 +13,11 @@ import java.net.URL;
import java.net.URLStreamHandlerFactory;
public interface EntitlementChecker {
void check$java_lang_System$exit(Class<?> callerClass, int status);
// Exit the JVM process
void check$$exit(Class<?> callerClass, Runtime runtime, int status);
void check$$halt(Class<?> callerClass, Runtime runtime, int status);
// URLClassLoader ctor
void check$java_net_URLClassLoader$(Class<?> callerClass, URL[] urls);

View file

@ -47,14 +47,21 @@ public class RestEntitlementsCheckAction extends BaseRestHandler {
}
private static final Map<String, CheckAction> checkActions = Map.ofEntries(
entry("system_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::systemExit)),
entry("runtime_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeExit)),
entry("runtime_halt", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeHalt)),
entry("create_classloader", CheckAction.serverAndPlugin(RestEntitlementsCheckAction::createClassLoader))
);
@SuppressForbidden(reason = "Specifically testing System.exit")
private static void systemExit() {
logger.info("Calling System.exit(123);");
System.exit(123);
@SuppressForbidden(reason = "Specifically testing Runtime.exit")
private static void runtimeExit() {
logger.info("Calling Runtime.exit;");
Runtime.getRuntime().exit(123);
}
@SuppressForbidden(reason = "Specifically testing Runtime.halt")
private static void runtimeHalt() {
logger.info("Calling Runtime.halt;");
Runtime.getRuntime().halt(123);
}
private static void createClassLoader() {

View file

@ -28,7 +28,12 @@ public class ElasticsearchEntitlementChecker implements EntitlementChecker {
}
@Override
public void check$java_lang_System$exit(Class<?> callerClass, int status) {
public void check$$exit(Class<?> callerClass, Runtime runtime, int status) {
policyManager.checkExitVM(callerClass);
}
@Override
public void check$$halt(Class<?> callerClass, Runtime runtime, int status) {
policyManager.checkExitVM(callerClass);
}

View file

@ -19,8 +19,8 @@ public class Java23ElasticsearchEntitlementChecker extends ElasticsearchEntitlem
}
@Override
public void check$java_lang_System$exit(Class<?> callerClass, int status) {
public void check$$exit(Class<?> callerClass, Runtime runtime, int status) {
// TODO: this is just an example, we shouldn't really override a method implemented in the superclass
super.check$java_lang_System$exit(callerClass, status);
super.check$$exit(callerClass, runtime, status);
}
}