From dff22045587b37282adeb2167486a572f51f1f16 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 15 Jun 2014 16:37:42 -0400 Subject: [PATCH] FML now sets a security manager (FINALLY!). It's primary purpose at this point is to catch rogue calls to System.exit so that they can cause a proper crash report, rather than silently abandoning the game. --- .../mods/fml/common/launcher/FMLTweaker.java | 12 +++++++ .../fml/relauncher/FMLSecurityManager.java | 36 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/cpw/mods/fml/relauncher/FMLSecurityManager.java diff --git a/src/main/java/cpw/mods/fml/common/launcher/FMLTweaker.java b/src/main/java/cpw/mods/fml/common/launcher/FMLTweaker.java index 3131c91ce5..ed0a5c180a 100644 --- a/src/main/java/cpw/mods/fml/common/launcher/FMLTweaker.java +++ b/src/main/java/cpw/mods/fml/common/launcher/FMLTweaker.java @@ -19,6 +19,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import cpw.mods.fml.relauncher.FMLLaunchHandler; +import cpw.mods.fml.relauncher.FMLSecurityManager; public class FMLTweaker implements ITweaker { private File gameDir; @@ -26,6 +27,17 @@ public class FMLTweaker implements ITweaker { private List standaloneArgs; private static URI jarLocation; + public FMLTweaker() + { + try + { + System.setSecurityManager(new FMLSecurityManager()); + } + catch (SecurityException se) + { + throw new RuntimeException("FML was unable to install the security manager. The game will not start", se); + } + } @SuppressWarnings("unchecked") @Override public void acceptOptions(List args, File gameDir, File assetsDir, String profile) diff --git a/src/main/java/cpw/mods/fml/relauncher/FMLSecurityManager.java b/src/main/java/cpw/mods/fml/relauncher/FMLSecurityManager.java new file mode 100644 index 0000000000..4c7fba0ec0 --- /dev/null +++ b/src/main/java/cpw/mods/fml/relauncher/FMLSecurityManager.java @@ -0,0 +1,36 @@ +package cpw.mods.fml.relauncher; + +import java.security.Permission; + +/** + * A custom security manager stopping certain events from happening + * unexpectedly. + * + * @author cpw + * + */ +public class FMLSecurityManager extends SecurityManager { + @Override + public void checkPermission(Permission perm) + { + String permName = perm.getName() != null ? perm.getName() : "missing"; + if (permName.startsWith("exitVM")) + { + String callingClass = getClassContext()[4].getName(); + // FML is allowed to call system exit + if (!callingClass.startsWith("cpw.mods.fml.")) + { + throw new ExitTrappedException(); + } + } + else if ("setSecurityManager".equals(permName)) + { + throw new SecurityException("Cannot replace the FML security manager"); + } + return; + } + + public static class ExitTrappedException extends SecurityException { + private static final long serialVersionUID = 1L; + } +}