From f08fc3bb2a4f2582674ac3234a10b91a28e4e1a2 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 19 Jul 2017 18:31:37 +0100 Subject: [PATCH] Add property to injectors to specify max allowed injections, closes #195 --- .../asm/launch/platform/MainAttributes.java | 10 +++---- .../asm/mixin/injection/Inject.java | 26 +++++++++++++++++++ .../asm/mixin/injection/ModifyArg.java | 26 +++++++++++++++++++ .../asm/mixin/injection/ModifyArgs.java | 26 +++++++++++++++++++ .../asm/mixin/injection/ModifyConstant.java | 26 +++++++++++++++++++ .../asm/mixin/injection/ModifyVariable.java | 26 +++++++++++++++++++ .../asm/mixin/injection/Redirect.java | 26 +++++++++++++++++++ .../mixin/injection/struct/InjectionInfo.java | 14 ++++++++++ .../asm/mixin/refmap/ReferenceMapper.java | 10 ++----- .../spongepowered/asm/util/Annotations.java | 6 ++--- .../org/spongepowered/asm/util/Bytecode.java | 4 +-- 11 files changed, 179 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/spongepowered/asm/launch/platform/MainAttributes.java b/src/main/java/org/spongepowered/asm/launch/platform/MainAttributes.java index 3e58cab6..b1b86c66 100644 --- a/src/main/java/org/spongepowered/asm/launch/platform/MainAttributes.java +++ b/src/main/java/org/spongepowered/asm/launch/platform/MainAttributes.java @@ -33,6 +33,8 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; +import org.apache.commons.io.IOUtils; + /** * "Main" attribute cache for a URI container, mainly to avoid constantly * opening jar files just to read odd values out of the manifest. @@ -76,13 +78,7 @@ final class MainAttributes { } catch (IOException ex) { // be quiet checkstyle } finally { - if (jarFile != null) { - try { - jarFile.close(); - } catch (IOException ex) { - // this could be an issue later on :( - } - } + IOUtils.closeQuietly(jarFile); } return new Attributes(); } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java index 196651ac..16c8f056 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Inject.java @@ -183,6 +183,32 @@ public @interface Inject { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java index 4851332a..4e47d099 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArg.java @@ -164,6 +164,32 @@ public @interface ModifyArg { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java index f968cc1d..316e3a05 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyArgs.java @@ -155,6 +155,32 @@ public @interface ModifyArgs { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java index d4e3936e..06b87f8d 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyConstant.java @@ -136,6 +136,32 @@ public @interface ModifyConstant { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java index 9f912900..65388982 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/ModifyVariable.java @@ -195,6 +195,32 @@ public @interface ModifyVariable { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java index 20a640a8..5cb286eb 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/Redirect.java @@ -304,6 +304,32 @@ public @interface Redirect { */ public int expect() default 1; + /** + * Injection points are in general expected to match every candidate + * instruction in the target method or slice, except in cases where options + * such as {@link At#ordinal} are specified which naturally limit the number + * of results. + * + *

This option allows for sanity-checking to be performed on the results + * of an injection point by specifying a maximum allowed number of matches, + * similar to that afforded by {@link Group#max}. For example if your + * injection is expected to match 4 invocations of a target method, but + * instead matches 5, this can become a detectable tamper condition by + * setting this value to 4. + * + *

Setting any value 1 or greater is allowed. Values less than 1 or less + * than {@link #require} are ignored. {@link #require} supercedes this + * argument such that if allow is less than require the + * value of require is always used.

+ * + *

Note that this option is not a limit on the query behaviour of + * this injection point. It is only a sanity check used to ensure that the + * number of matches is not too high + * + * @return Maximum allowed number of injections for this + */ + public int allow() default -1; + /** * Returns constraints which must be validated for this injector to * succeed. See {@link Constraint} for details of constraint formats. diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 9feb7251..cd55b591 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -128,6 +128,11 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC */ private int requiredCallbackCount = 0; + /** + * Maximum number of callbacks allowed to be injected + */ + private int maxCallbackCount = Integer.MAX_VALUE; + /** * Actual number of injected callbacks */ @@ -217,6 +222,11 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC } else if (this.group.isDefault()) { this.requiredCallbackCount = this.mixin.getDefaultRequiredInjections(); } + + Integer allow = Annotations.getValue(this.annotation, "allow"); + if (allow != null) { + this.maxCallbackCount = Math.max(Math.max(this.requiredCallbackCount, 1), allow); + } } // stub @@ -271,6 +281,10 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC throw new InjectionError( String.format("Critical injection failure: %s %s%s in %s failed injection check, (%d/%d) succeeded", this.getDescription(), this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.requiredCallbackCount)); + } else if (this.injectedCallbackCount > this.maxCallbackCount) { + throw new InjectionError( + String.format("Critical injection failure: %s %s%s in %s failed injection check, %d succeeded of %d allowed", + this.getDescription(), this.method.name, this.method.desc, this.mixin, this.injectedCallbackCount, this.maxCallbackCount)); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java b/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java index 2a368e72..02f556b5 100644 --- a/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java +++ b/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java @@ -24,7 +24,6 @@ */ package org.spongepowered.asm.mixin.refmap; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -32,6 +31,7 @@ import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -232,13 +232,7 @@ public final class ReferenceMapper implements Serializable { } catch (Exception ex) { logger.error("Failed reading REFMAP JSON from " + resourcePath + ": " + ex.getClass().getName() + " " + ex.getMessage()); } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ex) { - // don't really care - } - } + IOUtils.closeQuietly(reader); } return ReferenceMapper.DEFAULT_MAPPER; diff --git a/src/main/java/org/spongepowered/asm/util/Annotations.java b/src/main/java/org/spongepowered/asm/util/Annotations.java index 5dce79da..75ca8bbd 100644 --- a/src/main/java/org/spongepowered/asm/util/Annotations.java +++ b/src/main/java/org/spongepowered/asm/util/Annotations.java @@ -24,8 +24,6 @@ */ package org.spongepowered.asm.util; -import static com.google.common.base.Preconditions.*; - import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; @@ -38,6 +36,8 @@ import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.lib.tree.FieldNode; import org.spongepowered.asm.lib.tree.MethodNode; +import com.google.common.base.Preconditions; + /** * Utility class for working with ASM annotations */ @@ -351,7 +351,7 @@ public final class Annotations { */ @SuppressWarnings("unchecked") public static T getValue(AnnotationNode annotation, String key, Class annotationClass) { - checkNotNull(annotationClass, "annotationClass cannot be null"); + Preconditions.checkNotNull(annotationClass, "annotationClass cannot be null"); T value = Annotations.getValue(annotation, key); if (value == null) { try { diff --git a/src/main/java/org/spongepowered/asm/util/Bytecode.java b/src/main/java/org/spongepowered/asm/util/Bytecode.java index 596ad3d4..1f6ad814 100644 --- a/src/main/java/org/spongepowered/asm/util/Bytecode.java +++ b/src/main/java/org/spongepowered/asm/util/Bytecode.java @@ -24,8 +24,6 @@ */ package org.spongepowered.asm.util; -import static org.spongepowered.asm.lib.ClassWriter.*; - import java.io.OutputStream; import java.io.PrintWriter; import java.lang.annotation.Annotation; @@ -319,7 +317,7 @@ public final class Bytecode { * @param classNode the classNode to verify */ public static void dumpClass(ClassNode classNode) { - ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES); + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); classNode.accept(cw); Bytecode.dumpClass(cw.toByteArray()); }