diff --git a/projects/apache-commons-codec/BinaryEncodingFuzzer.java b/projects/apache-commons-codec/BinaryEncodingFuzzer.java new file mode 100644 index 000000000..0afde6dcc --- /dev/null +++ b/projects/apache-commons-codec/BinaryEncodingFuzzer.java @@ -0,0 +1,97 @@ +// 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 org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.binary.BaseNCodecInputStream; +import org.apache.commons.codec.binary.Base64InputStream; +import org.apache.commons.codec.binary.Base32InputStream; +import org.apache.commons.codec.binary.Base16InputStream; +import org.apache.commons.codec.binary.BaseNCodecOutputStream; +import org.apache.commons.codec.binary.Base64OutputStream; +import org.apache.commons.codec.binary.Base32OutputStream; +import org.apache.commons.codec.binary.Base16OutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; + +public class BinaryEncodingFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + int selection = data.consumeInt(1,3); + BaseNCodecInputStream bis; + BaseNCodecOutputStream bos; + ByteArrayOutputStream baos; + String encodingType; + + byte[] originalInput = data.consumeRemainingAsBytes(); + final int SIZE = originalInput.length * 2; + byte[] encoded = new byte[SIZE]; + + //Base32 encoding requires 5 bytes per unencoded block + if(originalInput.length < 5){ + return; + } + + switch(selection){ + case 1: bis = new Base16InputStream(new ByteArrayInputStream(originalInput), true); + baos = new ByteArrayOutputStream(SIZE); + bos = new Base16OutputStream(baos, false); + encodingType = "Base16"; + break; + + case 2: bis = new Base32InputStream(new ByteArrayInputStream(originalInput), true); + baos = new ByteArrayOutputStream(SIZE); + bos = new Base32OutputStream(baos, false); + encodingType = "Base32"; + break; + + case 3: bis = new Base64InputStream(new ByteArrayInputStream(originalInput), true); + baos = new ByteArrayOutputStream(SIZE); + bos = new Base64OutputStream(baos, false); + encodingType = "Base64"; + break; + + default: return; + } + + try { + bis.read(encoded); + } + catch (IOException e){ + return; + } + + + try{ + bos.write(encoded, 0, SIZE); + } + catch (IOException e){ + return; + } + + byte[] decoded = baos.toByteArray(); + + if(!Arrays.equals(originalInput, decoded)){ + throw new IllegalStateException("Failed to decode: " + Hex.encodeHexString(originalInput) + "\n" + + "Encoded using: " + "" + "\n" + + "Encoded data: " + Hex.encodeHexString(encoded) + "\n" + + "Decoded data: " + Hex.encodeHexString(decoded) + "\n"); + } + + } +} diff --git a/projects/apache-commons-codec/Dockerfile b/projects/apache-commons-codec/Dockerfile new file mode 100644 index 000000000..a817890e5 --- /dev/null +++ b/projects/apache-commons-codec/Dockerfile @@ -0,0 +1,29 @@ +# 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://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.zip -o maven.zip && \ + unzip maven.zip -d $SRC/maven && \ + rm -rf maven.zip + +ENV MVN $SRC/maven/apache-maven-3.8.5/bin/mvn + +RUN git clone --depth 1 https://gitbox.apache.org/repos/asf/commons-codec.git commons-codec + +COPY build.sh $SRC/ +COPY *Fuzzer.java $SRC/ +WORKDIR $SRC/commons-codec diff --git a/projects/apache-commons-codec/build.sh b/projects/apache-commons-codec/build.sh new file mode 100755 index 000000000..5902e9863 --- /dev/null +++ b/projects/apache-commons-codec/build.sh @@ -0,0 +1,49 @@ +#!/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="-Dmaven.test.skip=true -Djavac.src.version=15 -Djavac.target.version=15" +$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/commons-codec-$CURRENT_VERSION.jar" $OUT/commons-codec.jar + +ALL_JARS="commons-codec.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/apache-commons-codec/project.yaml b/projects/apache-commons-codec/project.yaml new file mode 100644 index 000000000..7234d0717 --- /dev/null +++ b/projects/apache-commons-codec/project.yaml @@ -0,0 +1,12 @@ +homepage: "https://commons.apache.org/proper/commons-codec/" +language: jvm +primary_contact: "glendowne@code-intelligence.com" +auto_ccs: + - "wagner@code-intelligence.com" + - "patrice.salathe@code-intelligence.com" + - "yakdan@code-intelligence.com" +fuzzing_engines: + - "libfuzzer" +sanitizers: + - "address" +main_repo: "https://gitbox.apache.org/repos/asf/commons-codec.git"