Skip to content

Commit 8cb6d76

Browse files
committed
fix: guard against malformed corpus entries in protobuf mutator
When decoding protobuf messages no size limit was enforced which could lead to OOM failures for corpus entries that were produced by a different mutator / arbitrary binary data.
1 parent e8b58db commit 8cb6d76

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

.github/workflows/fuzzing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
5757
- name: Build & Fuzz
5858
run: |
59-
bazelisk run ${{env.BUILD_BUDDY_CONFIG}} --java_runtime_version=remotejdk_${{ matrix.jdk }} ${{ matrix.bazel_args }} ${{ matrix.extra_bazel_args }} //selffuzz/src/test/java/com/code_intelligence/selffuzz/mutation:ArgumentsMutatorFuzzTest --jvmopt=-Xmx10000m -- -runs=1000000
59+
bazelisk run ${{env.BUILD_BUDDY_CONFIG}} --java_runtime_version=remotejdk_${{ matrix.jdk }} ${{ matrix.bazel_args }} ${{ matrix.extra_bazel_args }} //selffuzz/src/test/java/com/code_intelligence/selffuzz/mutation:ArgumentsMutatorFuzzTest -- -runs=1000000
6060
6161
# Notification job that runs after all matrix jobs complete
6262
notification:

selffuzz/src/test/java/com/code_intelligence/selffuzz/mutation/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ java_fuzz_target_test(
99
"ImmutableBean.java",
1010
],
1111
data = ["//selffuzz/src/test/resources:ArgumentsMutatorFuzzTest-corpus"],
12+
env = {
13+
"_JAVA_OPTIONS": "-Xmx1024m",
14+
},
1215
fuzzer_args = [
1316
# Make sure that the fuzzer can run. Longer fuzzing runs will be done in a separate GH action.
1417
"-runs=10000",

src/main/java/com/code_intelligence/jazzer/mutation/mutator/proto/BuilderMutatorFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.code_intelligence.jazzer.mutation.mutator.lang.LangMutators;
5858
import com.code_intelligence.jazzer.mutation.support.Preconditions;
5959
import com.google.protobuf.Any;
60+
import com.google.protobuf.CodedInputStream;
6061
import com.google.protobuf.Descriptors.Descriptor;
6162
import com.google.protobuf.Descriptors.EnumDescriptor;
6263
import com.google.protobuf.Descriptors.EnumValueDescriptor;
@@ -86,6 +87,11 @@
8687
import java.util.stream.Stream;
8788

8889
public final class BuilderMutatorFactory implements MutatorFactory {
90+
91+
// Generous size limit for decoded protobuf messages. This is necessary to guard against OOM
92+
// errors when the corpus format changes e.g. due to a change in the fuzz test signature.
93+
private static final int MAX_MESSAGE_SIZE = 32 * 1024 * 1024; // 32 MiB
94+
8995
private <T extends Builder, U> InPlaceMutator<T> mutatorForField(
9096
AnnotatedType initialType,
9197
FieldDescriptor field,
@@ -273,9 +279,11 @@ public B readExclusive(InputStream in) throws IOException {
273279
}
274280

275281
private Builder parseLeniently(InputStream in) throws IOException {
282+
CodedInputStream cis = CodedInputStream.newInstance(in);
283+
cis.setSizeLimit(MAX_MESSAGE_SIZE);
276284
Builder builder = defaultInstance.toBuilder();
277285
try {
278-
builder.mergeFrom(in);
286+
builder.mergeFrom(cis);
279287
} catch (InvalidProtocolBufferException ignored) {
280288
// builder has been partially modified with what could be decoded before the parser error.
281289
}

0 commit comments

Comments
 (0)