From c98c7eee9dbbe96efee611a531dbc56911c9c365 Mon Sep 17 00:00:00 2001 From: mayhem-bot Date: Fri, 3 Jun 2022 11:38:21 -0400 Subject: [PATCH] Mayhem support Mayhem support Mayhem support Delete jazzer_driver-mayhem.yml --- .../BasicJsonParserFuzzer-mayhem.yml | 57 ++++++++++++++++++ mayhem/BasicJsonParserFuzzer.java | 15 +++++ mayhem/BasicJsonParserFuzzer.mayhemfile | 9 +++ mayhem/Dockerfile | 39 ++++++++++++ mayhem/Dockerfile.dockerignore | 0 mayhem/build.sh | 60 +++++++++++++++++++ mayhem/jazzer_driver.mayhemfile | 4 ++ 7 files changed, 184 insertions(+) create mode 100644 .github/workflows/BasicJsonParserFuzzer-mayhem.yml create mode 100644 mayhem/BasicJsonParserFuzzer.java create mode 100644 mayhem/BasicJsonParserFuzzer.mayhemfile create mode 100644 mayhem/Dockerfile create mode 100644 mayhem/Dockerfile.dockerignore create mode 100755 mayhem/build.sh create mode 100644 mayhem/jazzer_driver.mayhemfile diff --git a/.github/workflows/BasicJsonParserFuzzer-mayhem.yml b/.github/workflows/BasicJsonParserFuzzer-mayhem.yml new file mode 100644 index 000000000000..57098672aa3f --- /dev/null +++ b/.github/workflows/BasicJsonParserFuzzer-mayhem.yml @@ -0,0 +1,57 @@ +name: Mayhem +on: + push: + pull_request: + workflow_dispatch: + workflow_call: +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} +jobs: + build: + name: ${{ matrix.os }} shared=${{ matrix.shared }} ${{ matrix.build_type }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest] + shared: [false] + build_type: [Release] + include: + - os: ubuntu-latest + triplet: x64-linux + steps: + - uses: actions/checkout@v2 + with: + submodules: recursive + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + file: mayhem/Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + - name: Start analysis + uses: forallsecure/mcode-action@v1 + with: + mayhem-token: ${{ secrets.MAYHEM_TOKEN }} + args: --image ${{ steps.meta.outputs.tags }} --file mayhem/BasicJsonParserFuzzer.mayhemfile + --cmd '/out/jazzer_driver --agent_path=/out/jazzer_agent_deploy.jar --cp=/out/spring-boot.jar:/out/spring-boot-loader.jar:/out/spring-core.jar:/out/spring-boot-starter-web.jar::/out + --target_class=BasicJsonParserFuzzer --jvm_args="-Xmx2048m"' --env JAVA_HOME="/out/open-jdk-17/" + --env LD_LIBRARY_PATH="/out/open-jdk-17/lib/server":/out --libfuzzer True + sarif-output: sarif + - name: Upload SARIF file(s) + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: sarif diff --git a/mayhem/BasicJsonParserFuzzer.java b/mayhem/BasicJsonParserFuzzer.java new file mode 100644 index 000000000000..6a6ac81d185a --- /dev/null +++ b/mayhem/BasicJsonParserFuzzer.java @@ -0,0 +1,15 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import org.springframework.boot.json.BasicJsonParser; +import org.springframework.boot.json.JsonParseException; + +public class BasicJsonParserFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + String content = data.consumeRemainingAsString(); + BasicJsonParser parser = new BasicJsonParser(); + try { + parser.parseList(content); + parser.parseMap(content); + } catch (JsonParseException e) { } + } +} \ No newline at end of file diff --git a/mayhem/BasicJsonParserFuzzer.mayhemfile b/mayhem/BasicJsonParserFuzzer.mayhemfile new file mode 100644 index 000000000000..662f5ec49d75 --- /dev/null +++ b/mayhem/BasicJsonParserFuzzer.mayhemfile @@ -0,0 +1,9 @@ +project: PROJECT +target: BasicJsonParserFuzzer +cmds: +- cmd: /out/jazzer_driver --agent_path=/out/jazzer_agent_deploy.jar --cp=/out/spring-boot.jar:/out/spring-boot-loader.jar:/out/spring-core.jar:/out/spring-boot-starter-web.jar::/out --target_class=BasicJsonParserFuzzer --jvm_args="-Xmx2048m" + env: + JAVA_HOME: '"/out/open-jdk-17/"' + LD_LIBRARY_PATH: '"/out/open-jdk-17/lib/server":/out' + timeout: 50 + libfuzzer: true diff --git a/mayhem/Dockerfile b/mayhem/Dockerfile new file mode 100644 index 000000000000..444ebfcf37bd --- /dev/null +++ b/mayhem/Dockerfile @@ -0,0 +1,39 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder-jvm + +RUN git clone --depth 1 https://github.com/google/fuzzing && mv fuzzing/dictionaries/json.dict $SRC/BasicJsonParserFuzzer.dict && rm -rf fuzzing + +RUN git clone --depth 1 https://github.com/dvyukov/go-fuzz-corpus && zip -j $SRC/BasicJsonParserFuzzer_seed_corpus.zip go-fuzz-corpus/json/corpus/* && rm -rf go-fuzz-corpus + +ARG java_home="/out/open-jdk-17" + +RUN apt update && apt install -y openjdk-17-jdk +ENV JAVA_HOME $java_home +ENV PATH "${java_home}:${PATH}" +ENV JVM_LD_LIBRARY_PATH "$java_home/lib/server" + +COPY . spring-boot +RUN rm -rf spring-boot/mayhem +RUN git clone --depth 1 https://github.com/spring-projects/spring-framework spring-boot/spring-framework + +COPY mayhem/build.sh $SRC/ +COPY mayhem/*Fuzzer.java $SRC/ +WORKDIR $SRC/spring-boot +ENV FUZZING_LANGUAGE=jvm SANITIZER=address FUZZING_ENGINE=libfuzzer +RUN compile +RUN chmod +x /out/jazzer_driver diff --git a/mayhem/Dockerfile.dockerignore b/mayhem/Dockerfile.dockerignore new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/mayhem/build.sh b/mayhem/build.sh new file mode 100755 index 000000000000..6e9b2d2d873a --- /dev/null +++ b/mayhem/build.sh @@ -0,0 +1,60 @@ +#!/bin/bash -eu +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +mv $SRC/{*.zip,*.dict} $OUT + +cp -r "/usr/lib/jvm/java-17-openjdk-amd64/" "$JAVA_HOME" + +CURRENT_VERSION=$(./gradlew properties --no-daemon --console=plain | sed -nr "s/^version:\ (.*)/\1/p") + +./gradlew publishToMavenLocal -x test -x intTest -i -x asciidoctor -x javadoc -x asciidoctorPdf -x :spring-boot-project:spring-boot-docs:zip -x :spring-boot-project:spring-boot-docs:publishMavenPublicationToMavenLocal +cp "/root/.m2/repository/org/springframework/boot/spring-boot/$CURRENT_VERSION/spring-boot-$CURRENT_VERSION.jar" "$OUT/spring-boot.jar" +cp "/root/.m2/repository/org/springframework/boot/spring-boot-loader/$CURRENT_VERSION/spring-boot-loader-$CURRENT_VERSION.jar" "$OUT/spring-boot-loader.jar" +cp "/root/.m2/repository/org/springframework/boot/spring-boot-starter-web/$CURRENT_VERSION/spring-boot-starter-web-$CURRENT_VERSION.jar" "$OUT/spring-boot-starter-web.jar" + +# Spring core +CURRENT_VERSION=$(./gradlew properties --no-daemon --console=plain --build-file=spring-framework/build.gradle | sed -nr "s/^version:\ (.*)/\1/p") +./gradlew build --build-file=spring-framework/spring-core/spring-core.gradle -x test -x javadoc -x :checkstyleNohttp +cp "spring-framework/spring-core/build/libs/spring-core-$CURRENT_VERSION.jar" "$OUT/spring-core.jar" + +ALL_JARS="spring-boot.jar spring-boot-loader.jar spring-core.jar spring-boot-starter-web.jar" + +# The classpath at build-time includes the project jars in $OUT as well as the +# Jazzer API. +BUILD_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "$OUT/%s:"):$JAZZER_API_PATH + +# All .jar and .class files lie in the same directory as the fuzzer at runtime. +RUNTIME_CLASSPATH=$(echo $ALL_JARS | xargs printf -- "\$this_dir/%s:"):\$this_dir + +for fuzzer in $(find $SRC -name '*Fuzzer.java'); do + fuzzer_basename=$(basename -s .java $fuzzer) + javac -cp $BUILD_CLASSPATH $fuzzer --release 17 + cp $SRC/$fuzzer_basename.class $OUT/ + + # Create an execution wrapper that executes Jazzer with the correct arguments. + echo "#!/bin/sh +# LLVMFuzzerTestOneInput for fuzzer detection. +this_dir=\$(dirname \"\$0\") +JAVA_HOME=\"\$this_dir/open-jdk-17/\" \ +LD_LIBRARY_PATH=\"$JVM_LD_LIBRARY_PATH\":\$this_dir \ +\$this_dir/jazzer_driver --agent_path=\$this_dir/jazzer_agent_deploy.jar \ +--cp=$RUNTIME_CLASSPATH \ +--target_class=$fuzzer_basename \ +--jvm_args=\"-Xmx2048m\" \ +\$@" > $OUT/$fuzzer_basename + chmod u+x $OUT/$fuzzer_basename +done diff --git a/mayhem/jazzer_driver.mayhemfile b/mayhem/jazzer_driver.mayhemfile new file mode 100644 index 000000000000..2ad581f35af8 --- /dev/null +++ b/mayhem/jazzer_driver.mayhemfile @@ -0,0 +1,4 @@ +project: PROJECT +target: jazzer_driver +cmds: +- cmd: /out/jazzer_driver