-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3173587
commit 893ca28
Showing
6 changed files
with
84 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class DisallowedClassRule { | ||
protected interface ExceptionFactory { | ||
Exception create(); | ||
} | ||
|
||
protected String disallowedClassName; | ||
protected ExceptionFactory disallowedClassException; | ||
|
||
public DisallowedClassRule(String disallowedClassName, ExceptionFactory disallowedClassException) { | ||
this.disallowedClassName = disallowedClassName; | ||
this.disallowedClassException = disallowedClassException; | ||
} | ||
|
||
public String getDisallowedClassName() { | ||
return disallowedClassName; | ||
} | ||
|
||
public Exception getDisallowedClassException() { | ||
return disallowedClassException.create(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/ca/dmoj/java/DisallowedClassesClassFileTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ca.dmoj.java; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
|
||
public class DisallowedClassesClassFileTransformer implements ClassFileTransformer { | ||
protected DisallowedClassRule[] disallowedClassRules; | ||
|
||
public DisallowedClassesClassFileTransformer(DisallowedClassRule... disallowedClassRules) { | ||
super(); | ||
this.disallowedClassRules = disallowedClassRules; | ||
} | ||
|
||
@Override | ||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, byte[] classfileBuffer) | ||
throws IllegalClassFormatException { | ||
if (className == null) return classfileBuffer; | ||
|
||
Exception disallowed = null; | ||
for (DisallowedClassRule rule : disallowedClassRules) { | ||
// If the class ever loaded it's because a submission used it | ||
if (className.startsWith(rule.getDisallowedClassName())) { | ||
disallowed = rule.getDisallowedClassException(); | ||
break; | ||
} | ||
} | ||
|
||
if (disallowed != null) ExceptionHandler.dumpExceptionAndExit(disallowed); | ||
|
||
// Don't actually retransform anything | ||
return classfileBuffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ca.dmoj.java; | ||
|
||
class ExceptionHandler { | ||
public static void dumpExceptionAndExit(Throwable exception) { | ||
System.err.print("7257b50d-e37a-4664-b1a5-b1340b4206c0: "); | ||
exception.printStackTrace(); | ||
System.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package ca.dmoj.java; | ||
|
||
public class UnsafeDisallowedException extends RuntimeException { | ||
} |