From 7fb37ac08f81f77a11e0fbf3749b8303415552f9 Mon Sep 17 00:00:00 2001 From: mayhem-bot Date: Thu, 2 Jun 2022 20:37:01 -0400 Subject: [PATCH] Mayhem support Mayhem support --- .github/workflows/ClassFuzzer-mayhem.yml | 57 ++++++++++++++++++++++ .github/workflows/jazzer_driver-mayhem.yml | 55 +++++++++++++++++++++ mayhem/ClassFuzzer.java | 55 +++++++++++++++++++++ mayhem/ClassFuzzer.mayhemfile | 8 +++ mayhem/Dockerfile | 32 ++++++++++++ mayhem/Dockerfile.dockerignore | 0 mayhem/build.sh | 51 +++++++++++++++++++ mayhem/jazzer_driver.mayhemfile | 4 ++ 8 files changed, 262 insertions(+) create mode 100644 .github/workflows/ClassFuzzer-mayhem.yml create mode 100644 .github/workflows/jazzer_driver-mayhem.yml create mode 100644 mayhem/ClassFuzzer.java create mode 100644 mayhem/ClassFuzzer.mayhemfile create mode 100644 mayhem/Dockerfile create mode 100644 mayhem/Dockerfile.dockerignore create mode 100644 mayhem/build.sh create mode 100644 mayhem/jazzer_driver.mayhemfile diff --git a/.github/workflows/ClassFuzzer-mayhem.yml b/.github/workflows/ClassFuzzer-mayhem.yml new file mode 100644 index 00000000..5df60ef7 --- /dev/null +++ b/.github/workflows/ClassFuzzer-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/ClassFuzzer.mayhemfile + --cmd '/out/jazzer_driver --agent_path=/out/jazzer_agent_deploy.jar --cp=/out/javassist.jar::/out + --target_class=ClassFuzzer --jvm_args="-Xmx2048m"' --env LD_LIBRARY_PATH="/usr/lib/jvm/java-15-openjdk-amd64/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/.github/workflows/jazzer_driver-mayhem.yml b/.github/workflows/jazzer_driver-mayhem.yml new file mode 100644 index 00000000..402aa27c --- /dev/null +++ b/.github/workflows/jazzer_driver-mayhem.yml @@ -0,0 +1,55 @@ +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 }} --cmd /out/jazzer_driver --target + jazzer_driver --file mayhem/jazzer_driver.mayhemfile + sarif-output: sarif + - name: Upload SARIF file(s) + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: sarif diff --git a/mayhem/ClassFuzzer.java b/mayhem/ClassFuzzer.java new file mode 100644 index 00000000..65772c7a --- /dev/null +++ b/mayhem/ClassFuzzer.java @@ -0,0 +1,55 @@ +// 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. +// +//////////////////////////////////////////////////////////////////////////////// + +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.lang.RuntimeException; + +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CannotCompileException; +import javassist.NotFoundException; +import java.lang.NullPointerException; + + +public class ClassFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + ClassPool pool = ClassPool.getDefault(); + CtClass cc = null; + + try { + cc = pool.makeClass(new ByteArrayInputStream(data.consumeRemainingAsBytes())); + } catch (IOException | RuntimeException e) { + } + + try { + cc.getSuperclass(); + cc.getNestedClasses(); + cc.getClassFile(); + cc.getInterfaces(); + cc.getDeclaringClass(); + cc.getComponentType(); + } catch (NotFoundException | NullPointerException e) { + } + + try { + cc.toBytecode(); + } catch (IOException | NullPointerException | CannotCompileException e) { + } + } +} diff --git a/mayhem/ClassFuzzer.mayhemfile b/mayhem/ClassFuzzer.mayhemfile new file mode 100644 index 00000000..281041b8 --- /dev/null +++ b/mayhem/ClassFuzzer.mayhemfile @@ -0,0 +1,8 @@ +project: PROJECT +target: ClassFuzzer +cmds: +- cmd: /out/jazzer_driver --agent_path=/out/jazzer_agent_deploy.jar --cp=/out/javassist.jar::/out --target_class=ClassFuzzer --jvm_args="-Xmx2048m" + env: + LD_LIBRARY_PATH: '"/usr/lib/jvm/java-15-openjdk-amd64/lib/server":/out' + timeout: 50 + libfuzzer: true diff --git a/mayhem/Dockerfile b/mayhem/Dockerfile new file mode 100644 index 00000000..41765bb0 --- /dev/null +++ b/mayhem/Dockerfile @@ -0,0 +1,32 @@ +# 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 curl -L https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip -o maven.zip && unzip maven.zip -d $SRC/maven && rm -rf maven.zip + +ENV MVN $SRC/maven/apache-maven-3.6.3/bin/mvn + +COPY . javassist +RUN rm -rf javassist/mayhem + +COPY mayhem/build.sh $SRC/ +COPY mayhem/ClassFuzzer.java $SRC/ +WORKDIR $SRC/javassist + +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 00000000..e69de29b diff --git a/mayhem/build.sh b/mayhem/build.sh new file mode 100644 index 00000000..72db6179 --- /dev/null +++ b/mayhem/build.sh @@ -0,0 +1,51 @@ +#!/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. +# +################################################################################ + + +MAVEN_ARGS="-Djavac.src.version=15 -Djavac.target.version=15 -DskipTests" +$MVN package org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade $MAVEN_ARGS +CURRENT_VERSION=$($MVN org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \ + -Dexpression=project.version -q -DforceStdout) +cp "target/javassist-$CURRENT_VERSION.jar" $OUT/javassist.jar + +ALL_JARS="javassist.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 + 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\") +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 00000000..2ad581f3 --- /dev/null +++ b/mayhem/jazzer_driver.mayhemfile @@ -0,0 +1,4 @@ +project: PROJECT +target: jazzer_driver +cmds: +- cmd: /out/jazzer_driver