Skip to content

Commit efa7ac3

Browse files
committed
Fix regex injection hook invocation for String functions
Fixes the incorrect invocation of the MethodHandle in the case where the regex injection hook is applied to a String function, which do require passing in the this object: == Java Exception: java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(String,String,String)String to (Object,Object)Object  at java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:881)  at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:866)  at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:729)  at com.code_intelligence.jazzer.sanitizers.RegexInjection.hookInternal(RegexInjection.kt:126)  at com.code_intelligence.jazzer.sanitizers.RegexInjection.patternHook(RegexInjection.kt:101) ... Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45246
1 parent 6a1beba commit efa7ac3

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

sanitizers/src/main/java/com/code_intelligence/jazzer/sanitizers/RegexInjection.kt

+18-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object RegexInjection {
2929
* Part of an OOM "exploit" for [java.util.regex.Pattern.compile] with the
3030
* [java.util.regex.Pattern.CANON_EQ] flag, formed by three consecutive combining marks, in this
3131
* case grave accents: ◌̀.
32-
* See [patternCompileWithFlagsHook] for details.
32+
* See [compileWithFlagsHook] for details.
3333
*/
3434
private const val CANON_EQ_ALMOST_EXPLOIT = "\u0300\u0300\u0300"
3535

@@ -49,7 +49,7 @@ object RegexInjection {
4949
fun compileWithFlagsHook(method: MethodHandle, alwaysNull: Any?, args: Array<Any?>, hookId: Int): Any? {
5050
val pattern = args[0] as String?
5151
val hasCanonEqFlag = ((args[1] as Int) and Pattern.CANON_EQ) != 0
52-
return hookInternal(method, pattern, hasCanonEqFlag, args, hookId)
52+
return hookInternal(method, pattern, hasCanonEqFlag, hookId, *args)
5353
}
5454

5555
@MethodHooks(
@@ -65,6 +65,13 @@ object RegexInjection {
6565
targetMethod = "matches",
6666
targetMethodDescriptor = "(Ljava/lang/String;Ljava/lang/CharSequence;)Z"
6767
),
68+
)
69+
@JvmStatic
70+
fun patternHook(method: MethodHandle, alwaysNull: Any?, args: Array<Any?>, hookId: Int): Any? {
71+
return hookInternal(method, args[0] as String?, false, hookId, *args)
72+
}
73+
74+
@MethodHooks(
6875
MethodHook(
6976
type = HookType.REPLACE,
7077
targetClassName = "java.lang.String",
@@ -97,11 +104,17 @@ object RegexInjection {
97104
),
98105
)
99106
@JvmStatic
100-
fun patternHook(method: MethodHandle, alwaysNull: Any?, args: Array<Any?>, hookId: Int): Any? {
101-
return hookInternal(method, args[0] as String?, false, args, hookId)
107+
fun stringHook(method: MethodHandle, thisObject: Any?, args: Array<Any?>, hookId: Int): Any? {
108+
return hookInternal(method, args[0] as String?, false, hookId, thisObject, *args)
102109
}
103110

104-
private fun hookInternal(method: MethodHandle, pattern: String?, hasCanonEqFlag: Boolean, args: Array<Any?>, hookId: Int): Any? {
111+
private fun hookInternal(
112+
method: MethodHandle,
113+
pattern: String?,
114+
hasCanonEqFlag: Boolean,
115+
hookId: Int,
116+
vararg args: Any?
117+
): Any? {
105118
if (hasCanonEqFlag && pattern != null) {
106119
// With CANON_EQ enabled, Pattern.compile allocates an array with a size that is
107120
// (super-)exponential in the number of consecutive Unicode combining marks. We use a mild case

0 commit comments

Comments
 (0)