From 834a138b396ddbc87f8bb1b4b9c67ac8630f5e6b Mon Sep 17 00:00:00 2001 From: Vincent Ulitzsch Date: Tue, 8 Jan 2019 20:17:29 +0100 Subject: [PATCH] [giflib] Add giflib (#2062) * Add initial version of giflib * Fix a memory leak in dgif_target This commit fixes a memory that was caused by calling the wrong close function. * giflib: Minor bug fixes that also addressees most comments from Doris --- projects/giflib/Dockerfile | 21 +++++++++ projects/giflib/build.sh | 18 ++++++++ projects/giflib/dgif_target.c | 70 +++++++++++++++++++++++++++++ projects/giflib/dgif_target.options | 2 + projects/giflib/gif.dict | 19 ++++++++ projects/giflib/project.yaml | 5 +++ 6 files changed, 135 insertions(+) create mode 100644 projects/giflib/Dockerfile create mode 100755 projects/giflib/build.sh create mode 100644 projects/giflib/dgif_target.c create mode 100644 projects/giflib/dgif_target.options create mode 100644 projects/giflib/gif.dict create mode 100644 projects/giflib/project.yaml diff --git a/projects/giflib/Dockerfile b/projects/giflib/Dockerfile new file mode 100644 index 000000000..b5e30ca9c --- /dev/null +++ b/projects/giflib/Dockerfile @@ -0,0 +1,21 @@ +# Copyright 2018 Google Inc. +# +# 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 +MAINTAINER esr@thyrsus.com +RUN apt-get update && apt-get install -y make autoconf automake libtool wget +RUN git clone --depth=1 https://git.code.sf.net/p/giflib/code $SRC/giflib-code +COPY *.c *.options build.sh $SRC/ diff --git a/projects/giflib/build.sh b/projects/giflib/build.sh new file mode 100755 index 000000000..fdd20056d --- /dev/null +++ b/projects/giflib/build.sh @@ -0,0 +1,18 @@ +cd $SRC/giflib-code +./autogen.sh +make +cd .. +for file in $SRC/*.c; +do + name=$(basename $file .c) + $CC $CFLAGS -c -I giflib-code/lib ${file} -o ${name}.o + $CXX $CXXFLAGS -std=c++11 -I giflib-code/lib ${name}.o \ + -o $OUT/${name} -lFuzzingEngine giflib-code/lib/.libs/libgif.a +done +# Place dict and config in OUT +wget -O $OUT/gif.dict \ + https://raw.githubusercontent.com/mirrorer/afl/master/dictionaries/gif.dict \ + &> /dev/null +cp $SRC/*.options $OUT/ +find $SRC/giflib-code -iname "*.gif" -exec \ + zip -ujq $OUT/dgif_target_seed_corpus.zip "{}" \; diff --git a/projects/giflib/dgif_target.c b/projects/giflib/dgif_target.c new file mode 100644 index 000000000..5fb5c0d6c --- /dev/null +++ b/projects/giflib/dgif_target.c @@ -0,0 +1,70 @@ +/***************************************************************************** + +gif2rgb - convert GIF to 24-bit RGB pixel triples or vice-versa + +*****************************************************************************/ + +/*************************************************************************** + +Toshio Kuratomi had written this in a comment about the rgb2gif code: + + Besides fixing bugs, what's really needed is for someone to work out how to + calculate a colormap for writing GIFs from rgb sources. Right now, an rgb + source that has only two colors (b/w) is being converted into an 8 bit GIF.... + Which is horrendously wasteful without compression. + +I (ESR) took this off the main to-do list in 2012 because I don't think +the GIFLIB project actually needs to be in the converters-and-tools business. +Plenty of hackers do that; our job is to supply stable library capability +with our utilities mainly interesting as test tools. + +***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif /* _WIN32 */ + +#include "gif_lib.h" + +#define PROGRAM_NAME "gif2rgb" + +/* =========================================================================== + * Display error message and exit + */ +void fuzz_error(const char *msg) +{ + fprintf(stderr, "%s: %s\n", "gif2rgb_fuzzer", msg); + exit(1); +} +/* end */ + +int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) +{ + char *inFileName = "/tmp/gif.gif"; + FILE *in = fopen(inFileName, "w"); + if(in==NULL){ + fuzz_error("failed fopen"); + } + int Error = 0; + if (fwrite(Data, 1, (unsigned)Size, in) != Size) + fuzz_error("failed fwrite"); + if (fclose(in)) + fuzz_error("failed fclose"); + GifFileType *GifFile; + GifFile = DGifOpenFileName(inFileName, &Error); + if (GifFile == NULL){ + return 0; + } + DGifSlurp(GifFile); + + DGifCloseFile(GifFile, &Error); + return 0; +} diff --git a/projects/giflib/dgif_target.options b/projects/giflib/dgif_target.options new file mode 100644 index 000000000..7adf373d0 --- /dev/null +++ b/projects/giflib/dgif_target.options @@ -0,0 +1,2 @@ +[libfuzzer] +dict = gif.dict diff --git a/projects/giflib/gif.dict b/projects/giflib/gif.dict new file mode 100644 index 000000000..e69aa75dc --- /dev/null +++ b/projects/giflib/gif.dict @@ -0,0 +1,19 @@ +# +# AFL dictionary for GIF images +# ----------------------------- +# +# Created by Michal Zalewski +# + +header_87a="87a" +header_89a="89a" +header_gif="GIF" + +marker_2c="," +marker_3b=";" + +section_2101="!\x01\x12" +section_21f9="!\xf9\x04" +section_21fe="!\xfe" +section_21ff="!\xff\x11" + diff --git a/projects/giflib/project.yaml b/projects/giflib/project.yaml new file mode 100644 index 000000000..613be7fdc --- /dev/null +++ b/projects/giflib/project.yaml @@ -0,0 +1,5 @@ +homepage: "http://giflib.sourceforge.net/" +primary_contact: "esr@thyrsus.com" +auto_ccs: + - "vincent.ulitzsch@live.de" + - "bshas3@gmail.com"