From 4b851f1e220cf1eb194135c8b0010e58a9952853 Mon Sep 17 00:00:00 2001 From: CheeseHunter117 Date: Thu, 16 Jun 2022 05:54:46 +0200 Subject: [PATCH] apache-commons-configuration: initial integration (#7847) * Added fuzz targets for apache-commons-configuration * Make recommended changes * Further requested changes made --- .../apache-commons-configuration/Dockerfile | 52 +++++++++++++++++ .../INIConfigurationReadFuzzer.java | 31 ++++++++++ .../INIConfigurationWriteFuzzer.java | 37 ++++++++++++ .../JSONConfigurationReadFuzzer.java | 33 +++++++++++ .../JSONConfigurationWriteFuzzer.java | 36 ++++++++++++ .../XMLConfigurationLoadFuzzer.java | 44 ++++++++++++++ .../YAMLConfigurationReadFuzzer.java | 22 +++++++ .../YAMLConfigurationWriteFuzzer.java | 25 ++++++++ .../apache-commons-configuration/build.sh | 57 +++++++++++++++++++ .../apache-commons-configuration/ini.dict | 32 +++++++++++ .../apache-commons-configuration/project.yaml | 13 +++++ 11 files changed, 382 insertions(+) create mode 100644 projects/apache-commons-configuration/Dockerfile create mode 100644 projects/apache-commons-configuration/INIConfigurationReadFuzzer.java create mode 100644 projects/apache-commons-configuration/INIConfigurationWriteFuzzer.java create mode 100644 projects/apache-commons-configuration/JSONConfigurationReadFuzzer.java create mode 100644 projects/apache-commons-configuration/JSONConfigurationWriteFuzzer.java create mode 100644 projects/apache-commons-configuration/XMLConfigurationLoadFuzzer.java create mode 100644 projects/apache-commons-configuration/YAMLConfigurationReadFuzzer.java create mode 100644 projects/apache-commons-configuration/YAMLConfigurationWriteFuzzer.java create mode 100755 projects/apache-commons-configuration/build.sh create mode 100644 projects/apache-commons-configuration/ini.dict create mode 100644 projects/apache-commons-configuration/project.yaml diff --git a/projects/apache-commons-configuration/Dockerfile b/projects/apache-commons-configuration/Dockerfile new file mode 100644 index 000000000..3ae8d122f --- /dev/null +++ b/projects/apache-commons-configuration/Dockerfile @@ -0,0 +1,52 @@ +# 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 + +# Install a working version of Maven +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 -f maven.zip + +ENV MVN $SRC/maven/apache-maven-3.6.3/bin/mvn + +# Clone the commons-configuration library source code +RUN git clone --depth=1 "https://gitbox.apache.org/repos/asf/commons-configuration.git" commons-configuration + +# Get the dictionaries +RUN git clone --depth=1 "https://github.com/google/fuzzing" fuzzing +RUN cp fuzzing/dictionaries/xml.dict $SRC/XMLConfigurationLoadFuzzer.dict + +RUN cp fuzzing/dictionaries/yaml.dict $SRC/YAMLConfigurationReadFuzzer.dict +RUN cp $SRC/YAMLConfigurationReadFuzzer.dict $SRC/YAMLConfigurationWriteFuzzer.dict + +RUN cp fuzzing/dictionaries/json.dict $SRC/JSONConfigurationReadFuzzer.dict +RUN cp $SRC/JSONConfigurationReadFuzzer.dict $SRC/JSONConfigurationWriteFuzzer.dict + +COPY ini.dict $SRC/INIConfigurationReadFuzzer.dict +RUN cp $SRC/INIConfigurationReadFuzzer.dict $SRC/INIConfigurationWriteFuzzer.dict + +# Get the corpora +RUN git clone --depth=1 "https://github.com/dvyukov/go-fuzz-corpus" corpora +RUN zip -q $SRC/XMLConfigurationLoadFuzzer_seed_corpus.zip corpora/xml/corpus/* + +RUN zip -q $SRC/JSONConfigurationReadFuzzer_seed_corpus.zip corpora/json/corpus/* +RUN cp $SRC/JSONConfigurationReadFuzzer_seed_corpus.zip $SRC/JSONConfigurationWriteFuzzer_seed_corpus.zip + +# Copy the build script and all the fuzztargets +COPY build.sh $SRC/ +COPY *Fuzzer.java $SRC/ +WORKDIR $SRC/commons-configuration diff --git a/projects/apache-commons-configuration/INIConfigurationReadFuzzer.java b/projects/apache-commons-configuration/INIConfigurationReadFuzzer.java new file mode 100644 index 000000000..0411d2400 --- /dev/null +++ b/projects/apache-commons-configuration/INIConfigurationReadFuzzer.java @@ -0,0 +1,31 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.io.IOException; + +import org.apache.commons.configuration2.INIConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class INIConfigurationReadFuzzer extends INIConfiguration { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + // Create helper objects from fuzzer data + final char commentCharacter = data.consumeChar(); + final char separatorCharacter = data.consumeChar(); + final byte[] byteArray = data.consumeBytes(Integer.MAX_VALUE); + + // Create needed objects + INIConfiguration iniConfig = new INIConfiguration(); + InputStream inputStream = new ByteArrayInputStream(byteArray); + InputStreamReader reader = new InputStreamReader(inputStream); + + try { + iniConfig.setSeparatorUsedInInput(Character.toString(separatorCharacter)); + iniConfig.setCommentLeadingCharsUsedInInput(Character.toString(commentCharacter)); + iniConfig.read(reader); + } catch (IOException | ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/INIConfigurationWriteFuzzer.java b/projects/apache-commons-configuration/INIConfigurationWriteFuzzer.java new file mode 100644 index 000000000..c96249401 --- /dev/null +++ b/projects/apache-commons-configuration/INIConfigurationWriteFuzzer.java @@ -0,0 +1,37 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.io.IOException; + +import org.apache.commons.configuration2.INIConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class INIConfigurationWriteFuzzer extends INIConfiguration { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + // Create helper objects from fuzzer data + final char commentCharacter = data.consumeChar(); + final char inputSeparatorCharacter = data.consumeChar(); + final char outputSeparatorCharacter = data.consumeChar(); + final byte[] byteArray = data.consumeBytes(Integer.MAX_VALUE); + + // Create the needed objects + INIConfiguration iniConfig = new INIConfiguration(); + InputStream inputStream = new ByteArrayInputStream(byteArray); + InputStreamReader reader = new InputStreamReader(inputStream); + StringWriter writer = new StringWriter(); + + try { + iniConfig.setSeparatorUsedInInput(Character.toString(inputSeparatorCharacter)); + iniConfig.setCommentLeadingCharsUsedInInput(Character.toString(commentCharacter)); + iniConfig.read(reader); + + iniConfig.setSeparatorUsedInOutput(Character.toString(outputSeparatorCharacter)); + iniConfig.write(writer); + } catch (IOException | ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/JSONConfigurationReadFuzzer.java b/projects/apache-commons-configuration/JSONConfigurationReadFuzzer.java new file mode 100644 index 000000000..049147f1a --- /dev/null +++ b/projects/apache-commons-configuration/JSONConfigurationReadFuzzer.java @@ -0,0 +1,33 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ByteArrayInputStream; + +import org.apache.commons.configuration2.JSONConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class JSONConfigurationReadFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + // Create helper objects from fuzzer data + final boolean useReader = data.consumeBoolean(); + final byte[] byteArray = data.consumeBytes(Integer.MAX_VALUE); + + // Create needed objects + final JSONConfiguration jsonConfig = new JSONConfiguration(); + final InputStream inputStream = new ByteArrayInputStream(byteArray); + final InputStreamReader reader; + + try { + if (useReader) { + reader = new InputStreamReader(inputStream); + jsonConfig.read(reader); + } else { + jsonConfig.read(inputStream); + } + + } catch (ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/JSONConfigurationWriteFuzzer.java b/projects/apache-commons-configuration/JSONConfigurationWriteFuzzer.java new file mode 100644 index 000000000..7bfa029e1 --- /dev/null +++ b/projects/apache-commons-configuration/JSONConfigurationWriteFuzzer.java @@ -0,0 +1,36 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.io.IOException; + +import org.apache.commons.configuration2.JSONConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class JSONConfigurationWriteFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + // Create helper objects from fuzzer data + final boolean useReader = data.consumeBoolean(); + final byte[] byteArray = data.consumeBytes(Integer.MAX_VALUE); + + // Create needed objects + JSONConfiguration jsonConfig = new JSONConfiguration(); + InputStream inputStream = new ByteArrayInputStream(byteArray); + InputStreamReader reader; + StringWriter writer = new StringWriter(); + + try { + if (useReader) { + reader = new InputStreamReader(inputStream); + jsonConfig.read(reader); + } else { + jsonConfig.read(inputStream); + } + jsonConfig.write(writer); + } catch (IOException | ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/XMLConfigurationLoadFuzzer.java b/projects/apache-commons-configuration/XMLConfigurationLoadFuzzer.java new file mode 100644 index 000000000..fdbb2ea3b --- /dev/null +++ b/projects/apache-commons-configuration/XMLConfigurationLoadFuzzer.java @@ -0,0 +1,44 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.File; +import java.io.FileWriter; +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.apache.commons.configuration2.XMLConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; +import org.apache.commons.configuration2.io.FileHandler; + +public class XMLConfigurationLoadFuzzer { + public static void fuzzerTestOneInput(FuzzedDataProvider data) { + // Create needed objects + final File tempFile; + try { + tempFile = File.createTempFile("XMLConfiguration", "xml"); + tempFile.deleteOnExit(); + } catch (IOException ioe) { + // Preparations failed ; exit early + return; + } + final String absoluteFilepath = tempFile.getAbsolutePath(); + + try { + final FileWriter fileWriter = new FileWriter(tempFile); + fileWriter.write(data.consumeRemainingAsString()); + fileWriter.close(); + } catch (IOException ioe) { + // Preparations failed ; exit early + return; + } + + final FileHandler fileHandler = new FileHandler(new XMLConfiguration()); + fileHandler.setPath(absoluteFilepath); + + try { + fileHandler.load(); + } catch (ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/YAMLConfigurationReadFuzzer.java b/projects/apache-commons-configuration/YAMLConfigurationReadFuzzer.java new file mode 100644 index 000000000..a56cad59d --- /dev/null +++ b/projects/apache-commons-configuration/YAMLConfigurationReadFuzzer.java @@ -0,0 +1,22 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.apache.commons.configuration2.YAMLConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class YAMLConfigurationReadFuzzer { + public static void fuzzerTestOneInput(byte[] data) { + // Create needed objects + YAMLConfiguration yamlConfig = new YAMLConfiguration(); + InputStream inputStream = new ByteArrayInputStream(data); + + try { + yamlConfig.read(inputStream); + } catch (ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/YAMLConfigurationWriteFuzzer.java b/projects/apache-commons-configuration/YAMLConfigurationWriteFuzzer.java new file mode 100644 index 000000000..e79795ce3 --- /dev/null +++ b/projects/apache-commons-configuration/YAMLConfigurationWriteFuzzer.java @@ -0,0 +1,25 @@ +import com.code_intelligence.jazzer.api.FuzzedDataProvider; + +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.StringWriter; +import java.io.IOException; + +import org.apache.commons.configuration2.YAMLConfiguration; +import org.apache.commons.configuration2.ex.ConfigurationException; + +public class YAMLConfigurationWriteFuzzer { + public static void fuzzerTestOneInput(byte[] data) { + // Create needed objects + YAMLConfiguration yamlConfig = new YAMLConfiguration(); + InputStream inputStream = new ByteArrayInputStream(data); + StringWriter writer = new StringWriter(); + + try { + yamlConfig.read(inputStream); + yamlConfig.write(writer); + } catch (IOException | ConfigurationException ignored) { + // expected Exceptions get ignored + } + } +} diff --git a/projects/apache-commons-configuration/build.sh b/projects/apache-commons-configuration/build.sh new file mode 100755 index 000000000..3f4a3ad7f --- /dev/null +++ b/projects/apache-commons-configuration/build.sh @@ -0,0 +1,57 @@ +#!/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. +# +################################################################################ + +# Moving the corpora and dictionaries to $OUT +mv $SRC/{*.zip,*.dict} $OUT + +# Building the library +MAVEN_ARGS="--no-transfer-progress -Dmaven.test.skip=true -Djavac.src.version=15 -Djavac.target.version=15 -Djdk.version=15" + +CURRENT_VERSION=$($MVN org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \ +-Dexpression=project.version -q -DforceStdout) + +## maven-shade-plugin is for packaging all the dependencies into one .jar +$MVN clean package org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade $MAVEN_ARGS +cp "target/commons-configuration2-$CURRENT_VERSION.jar" "$OUT/commons-configuration.jar" + +ALL_JARS="commons-configuration.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 -iname '*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:-Djava.awt.headless=true\" \ +\$@" > $OUT/$fuzzer_basename + + # Make the execution wrapper executable + chmod +x $OUT/$fuzzer_basename +done diff --git a/projects/apache-commons-configuration/ini.dict b/projects/apache-commons-configuration/ini.dict new file mode 100644 index 000000000..2b86996b3 --- /dev/null +++ b/projects/apache-commons-configuration/ini.dict @@ -0,0 +1,32 @@ +# sources: +# - https://en.wikipedia.org/wiki/INI_file +# - https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms717987(v=vs.85) + +# Basics +assign="=" +section_left_bracket="[" +section_right_bracket="]" +comment_semicolon=";" +comment_hash="#" +apostrophe="'" +double_quotes="\"" + +# Old section nesting symbols +backslash="\\" +comma="," +period="." + +# Escape sequences +escaped_backslash="\\'" +escaped_double_quotes="\\"" +escaped_null_byte="\\0" +escaped_alert="\\a" +escaped_backspace="\\b" +escaped_tab="\\t" +escaped_carriage_return="\\r" +escaped_linefeed="\\n" +escaped_semicolon="\\;" +escaped_hash="\\#" +escaped_equal_sign="\\=" +escaped_colon="\\:" +escaped_unicode_beginning="\\x" diff --git a/projects/apache-commons-configuration/project.yaml b/projects/apache-commons-configuration/project.yaml new file mode 100644 index 000000000..530426358 --- /dev/null +++ b/projects/apache-commons-configuration/project.yaml @@ -0,0 +1,13 @@ +homepage: "https://commons.apache.org/proper/commons-configuration/" +language: jvm +main_repo: "https://gitbox.apache.org/repos/asf/commons-configuration.git" +fuzzing_engines: + - libfuzzer +sanitizers: + - address +vendor_ccs: + - "wagner@code-intelligence.com" + - "yakdan@code-intelligence.com" + - "glendowne@code-intelligence.com" + - "patrice.salathe@code-intelligence.com" + - "yoshi.weber@gmail.com"