From 285405387fff24927c9596d46ce3a3fbe39b62e2 Mon Sep 17 00:00:00 2001 From: happy-qop <106136863+happy-qop@users.noreply.github.com> Date: Fri, 27 May 2022 08:54:58 +0800 Subject: [PATCH] Initial integration of Junrar (#7750) initial integration of junrar --- projects/junrar/Dockerfile | 38 ++++++++++++++ projects/junrar/JunrarFuzzer.java | 84 +++++++++++++++++++++++++++++++ projects/junrar/build.sh | 53 +++++++++++++++++++ projects/junrar/project.yaml | 9 ++++ 4 files changed, 184 insertions(+) create mode 100644 projects/junrar/Dockerfile create mode 100644 projects/junrar/JunrarFuzzer.java create mode 100755 projects/junrar/build.sh create mode 100644 projects/junrar/project.yaml diff --git a/projects/junrar/Dockerfile b/projects/junrar/Dockerfile new file mode 100644 index 000000000..fc57551b3 --- /dev/null +++ b/projects/junrar/Dockerfile @@ -0,0 +1,38 @@ +# 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://services.gradle.org/distributions/gradle-7.4.2-bin.zip -o gradle.zip && \ + unzip gradle.zip -d $SRC/gradle && \ + rm -rf gradle.zip + +ENV GRADLE_HOME $SRC/gradle/gradle-7.4.2 +ENV PATH $GRADLE_HOME/bin:$PATH + +# Dict +# no existing rar.dict found on web, build rar dict manually later + +# Seeds +RUN git clone --depth 1 https://github.com/strongcourage/fuzzing-corpus.git && \ + zip -j $SRC/JunrarFuzzer_seed_corpus.zip fuzzing-corpus/rar/* && \ + rm -rf fuzzing-corpus + +RUN git clone --depth 1 https://github.com/junrar/junrar.git junrar + +COPY build.sh $SRC/ +COPY JunrarFuzzer.java $SRC/ +WORKDIR $SRC/junrar diff --git a/projects/junrar/JunrarFuzzer.java b/projects/junrar/JunrarFuzzer.java new file mode 100644 index 000000000..9f507bef2 --- /dev/null +++ b/projects/junrar/JunrarFuzzer.java @@ -0,0 +1,84 @@ +// 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.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.ByteArrayInputStream; + +import com.github.junrar.Archive; +import com.github.junrar.rarfile.FileHeader; +import com.github.junrar.exception.RarException; +import com.github.junrar.io.SeekableReadOnlyByteChannel; +import com.github.junrar.rarfile.MainHeader; +import com.github.junrar.volume.Volume; + +public class JunrarFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + + try { + InputStream inputStream = new ByteArrayInputStream(data.consumeRemainingAsBytes()); + Archive v0 = null; + FileHeader v1 = null; + SeekableReadOnlyByteChannel v2 = null; + MainHeader v3 = null; + Volume v4 = null; + + v0 = new Archive(inputStream); + v2 = v0.getChannel(); + if (v2 != null) { + v2.getPosition(); + } + + v0.getFileHeaders(); + v0.getHeaders(); + + v3 = v0.getMainHeader(); + if (v3 != null) { + v3.getEncryptVersion(); + v3.isEncrypted(); + //v3.print(); + } + + v4 = v0.getVolume(); + if (v4 != null) { + v4.getChannel(); + v4.getLength(); + } + + v0.isEncrypted(); + + while (true) { + v1 = v0.nextFileHeader(); + if (v1 == null) { + break; + } + + v1.getCTime(); + v1.hasVolumeNumber(); + v1.isSubBlock(); + + v0.extractFile(v1, OutputStream.nullOutputStream()); + } + + } catch (IOException e1) { + } catch (RarException e2) { + return; + } + } +} diff --git a/projects/junrar/build.sh b/projects/junrar/build.sh new file mode 100755 index 000000000..e93bdefda --- /dev/null +++ b/projects/junrar/build.sh @@ -0,0 +1,53 @@ +#!/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 $OUT +#mv $SRC/{*.zip,*.dict} $OUT + +./gradlew build +CURRENT_VERSION=$(./gradlew properties --no-daemon --console=plain -q | grep "^version:" | awk '{printf $2}') +cp "build/libs/junrar-$CURRENT_VERSION-sources.jar" $OUT/junrar.jar + +curl -L https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar -o $OUT/slf4j-api.jar +curl -L https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.jar -o $OUT/slf4j-simple.jar + +ALL_JARS="junrar.jar slf4j-api.jar slf4j-simple.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/projects/junrar/project.yaml b/projects/junrar/project.yaml new file mode 100644 index 000000000..9d4f67ea5 --- /dev/null +++ b/projects/junrar/project.yaml @@ -0,0 +1,9 @@ +fuzzing_engines: +- libfuzzer +homepage: https://github.com/junrar/junrar +language: jvm +main_repo: https://github.com/junrar/junrar.git +sanitizers: +- address +vendor_ccs: +- all.u.ever.know@gmail.com