Add property to injectors to specify max allowed injections, closes #195

This commit is contained in:
Mumfrey committed 2017-07-19 18:31:37 +01:00
1 parent 55777fcf42
commit f08fc3bb2a
11 files changed
+179 -21

No files matched your search

@@ -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();
}
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.
*
* <p>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 <tt>4</tt>.
*
* <p>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 <tt>allow</tt> is less than <tt>require</tt> the
* value of <tt>require</tt> is always used.</p>
*
* <p>Note that this option is not a <i>limit</i> 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.
@@ -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.<Integer>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));
}
}
@@ -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;
@@ -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> 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 {
@@ -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());
}