Support fuzzing grub's image file parsers
PATCH 1/6 configure.ac: detect if compiler supports -fsanitize=fuzzer
In reply to Daniel AxtensIf detected, COND_ENABLE_FUZZERS is set, which can be used in Makefile templates.
This allows us to build libfuzzer fuzzers for parts of grub.
--- configure.ac | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
configure.ac +32 -0
@@ -1922,6 +1922,31 @@ fi AC_SUBST([LIBZFS]) AC_SUBST([LIBNVPAIR]) +AC_ARG_ENABLE([fuzzers], + [AS_HELP_STRING([--enable-fuzzers], + [enable fuzzers (default=guessed)])]) +if test x"$enable_fuzzers" = xno ; then + fuzzers_excuse="explicitly disabled" +fi + +if test x"$fuzzers_excuse" = x ; then + SAVED_CFLAGS="$CFLAGS" + SAVED_CC="$CC" + CC="$HOST_CC" + CFLAGS="$HOST_CFLAGS -fsanitize=fuzzer" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include <stddef.h> +#include <stdint.h> +int LLVMFuzzerTestOneInput(const uint8_t Data, size_t Size) {return 0;}]], [[]])], + [], + [fuzzers_excuse="-fsanitize=fuzzer not supported by host compiler"]) + CFLAGS="$SAVED_CFLAGS" + CC="$SAVED_CC" +fi + +if test x"$enable_fuzzers" = xyes && test x"$fuzzers_excuse" != x ; then + AC_MSG_ERROR([fuzzers were explicitly requested but requirements are not satisfied ($fuzzers_excuse)]) +fi + LIBS="" AC_SUBST([FONT_SOURCE])
@@ -2054,6 +2079,8 @@ AM_CONDITIONAL([COND_STARFIELD], [test "x$starfield_excuse" = x]) AM_CONDITIONAL([COND_HAVE_EXEC], [test "x$have_exec" = xy]) AM_CONDITIONAL([COND_HAVE_PCI], [test "x$have_pci" = xy]) +AM_CONDITIONAL([COND_ENABLE_FUZZERS], [test "x$fuzzers_excuse" = x]) + test "x$prefix" = xNONE && prefix="$ac_default_prefix" test "x$exec_prefix" = xNONE && exec_prefix="${prefix}" datarootdir="$(eval echo "$datarootdir")"
@@ -2189,5 +2216,10 @@ echo "With stack smashing protector: Yes" else echo "With stack smashing protector: No" fi +if [ x"$fuzzers_excuse" = x ]; then +echo With fuzzers: Yes +else +echo With fuzzers: No "($fuzzers_excuse)" +fi echo "*******************************************************" ]
Signature · 1 line
2.25.1
cover Support fuzzing grub's image file parsers
This is a cleaned up version of the fuzzers that I used to find CVE-2021-3695, CVE-2021-3696 and CVE-2021-3697.
We're releasing this now because the techniques are not novel, the code is not hard to independently replicate, and it's hard to see what is gained by not releasing them. (I should add that the grub security working group has been supportive of this for some time - any and all responsibility for the delay in getting this posted rests with me.)
Obivously the techniques here are extensible and I'd be very happy to see people add more fuzzers to increase coverage based on this infrastructure.
Kind regards,
Daniel
PATCH 4/6 tests: create a fuzzer for the jpeg image format
As with png, but this time testing jpeg.
--- .gitignore | 1 + Makefile.util.def | 21 +++++++++++++++++++++ grub-core/tests/fuzz/jpeg.c | 3 +++ 3 files changed, 25 insertions(+) create mode 100644 grub-core/tests/fuzz/jpeg.c
.gitignore +1 -0
@@ -37,6 +37,7 @@ grub-emu grub-emu-lite grub-emu-lite.exe grub-emu.exe +grub-fuzz-jpeg grub-fuzz-png grub-macho2img grub_emu_init.c
Makefile.util.def +21 -0
@@ -1487,3 +1487,24 @@ program = { condition = COND_ENABLE_FUZZERS; }; + +program = { + name = grub-fuzz-jpeg; + + nostrip = common; + + common = grub-core/video/readers/jpeg.c; + common = grub-core/video/bitmap.c; + common = grub-core/io/bufio.c; + + extra_dist = grub-core/tests/fuzz/imageXX.c; + common = grub-core/tests/fuzz/jpeg.c; + + ldadd = 'libfuzzkern.a grub-core/lib/gnulib/libgnu.a $(LIBDEVMAPPER)'; + + cppflags = '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION'; + cflags='-fsanitize=fuzzer-no-link'; + ldflags='-fsanitize=fuzzer'; + + condition = COND_ENABLE_FUZZERS; +};
grub-core/tests/fuzz/jpeg.c new file +3 -0
@@ -0,0 +1,3 @@ +#define IMG_EXTENSION "jpg" +#define MODULE_INIT grub_jpeg_init +#include "imageXX.c"
Signature · 1 line
2.25.1
PATCH 2/6 bitmap.h: set smaller image size limit when fuzzing
In reply to Daniel AxtensLarge images mean large memory allocations, which could fail or at least be very slow. Also large images mean large files, which fuzzing generally cannot create. Finally, most parsing bugs (at least that we've found!) don't seem to rely on actually large images. So simply reduce the maximum size of an image to something we're more likely to actually be able to provide.
--- include/grub/bitmap.h | 4 ++++ 1 file changed, 4 insertions(+)
include/grub/bitmap.h +4 -0
@@ -25,7 +25,11 @@#include <grub/video.h> #include <grub/safemath.h> +#ifdef FUZZING_BUILD_MODE_NOT_SAFE_FOR_PRODUCTION +#define IMAGE_HW_MAX_PX 512 +#else #define IMAGE_HW_MAX_PX 16384 +#endif struct grub_video_bitmap {
Signature · 1 line
2.25.1
PATCH 3/6 tests: create a fuzzer for the png image format
In reply to Daniel AxtensCreate a libfuzzer harness for the png image format.
Once built, the fuzzer runs as: ./grub-fuzz-png <corpus-dir>
With some hackery it can also be built with the address sanitizer or afl++.
With regard to the license: I first worked on this while I was at IBM. I believe, looking at the fuzz fixes, that this would have been 2021-2022. The code was released to a cross-company grub security working group at this time. I left IBM in April 2022. Work subsequent to that is my own, and is unrelated to my subsequent employer.
--- .gitignore | 1 + Makefile.util.def | 78 ++++++++++++++++++++++++++++++++++ grub-core/tests/fuzz/imageXX.c | 75 ++++++++++++++++++++++++++++++++ grub-core/tests/fuzz/png.c | 3 ++ 4 files changed, 157 insertions(+) create mode 100644 grub-core/tests/fuzz/imageXX.c create mode 100644 grub-core/tests/fuzz/png.c
.gitignore +1 -0
@@ -37,6 +37,7 @@ grub-emu grub-emu-lite grub-emu-lite.exe grub-emu.exe +grub-fuzz-png grub-macho2img grub_emu_init.c grub_emu_init.h
Makefile.util.def +78 -0
87 lines not on this page · raw message
grub-core/tests/fuzz/imageXX.c new file +75 -0
75 lines not on this page · raw message
grub-core/tests/fuzz/png.c new file +3 -0
@@ -0,0 +1,3 @@ +#define IMG_EXTENSION "png" +#define MODULE_INIT grub_png_init +#include "imageXX.c"
Signature · 1 line
2.25.1
PATCH 5/6 tests: create a fuzzer for the tga image format
In reply to Daniel AxtensTGA, the only parser where I didn't find any memory-corruption bugs. Yet.
--- .gitignore | 1 + Makefile.util.def | 21 +++++++++++++++++++++ grub-core/tests/fuzz/tga.c | 3 +++ 3 files changed, 25 insertions(+) create mode 100644 grub-core/tests/fuzz/tga.c
.gitignore +1 -0
@@ -39,6 +39,7 @@ grub-emu-lite.exe grub-emu.exe grub-fuzz-jpeg grub-fuzz-png +grub-fuzz-tga grub-macho2img grub_emu_init.c grub_emu_init.h
Makefile.util.def +21 -0
@@ -1508,3 +1508,24 @@ program = { condition = COND_ENABLE_FUZZERS; }; + +program = { + name = grub-fuzz-tga; + + nostrip = common; + + common = grub-core/video/readers/tga.c; + common = grub-core/video/bitmap.c; + common = grub-core/io/bufio.c; + + extra_dist = grub-core/tests/fuzz/imageXX.c; + common = grub-core/tests/fuzz/tga.c; + + ldadd = 'libfuzzkern.a grub-core/lib/gnulib/libgnu.a $(LIBDEVMAPPER)'; + + cppflags = '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION'; + cflags='-fsanitize=fuzzer-no-link'; + ldflags='-fsanitize=fuzzer'; + + condition = COND_ENABLE_FUZZERS; +};
grub-core/tests/fuzz/tga.c new file +3 -0
@@ -0,0 +1,3 @@ +#define IMG_EXTENSION "tga" +#define MODULE_INIT grub_tga_init +#include "imageXX.c"
Signature · 1 line
2.25.1
PATCH 6/6 docs: document fuzzer support
In reply to Daniel AxtensThis is basic documentation.
There are a few extra, super-hacky things we can do to get asan or afl++ going which are a bit too messy at the moment to commit to the docs. But, so as they are not lost to time, here's the invocations that currently work for me:
libfuzzer + ASan:
./configure CC=clang --with-platform=emu --enable-fuzzers=yes; make clean ASAN_OPTIONS=detect_leaks=0 make CFLAGS='-fsanitize=address -Wl,--allow-multiple-definition' -j4 -k || \
echo "the compile is expected to fail eventually, but the fuzzers should still build"
AFL++:
./configure CC=clang --with-platform=emu --enable-fuzzers=yes; make clean make CC=/path/to/afl-cc CFLAGS="-Wl,--allow-multiple-definition" -j4 -k || \
echo "the compile is expected to fail eventually, but the fuzzers should still build"
AFL++ + ASan:
./configure CC=clang --with-platform=emu --enable-fuzzers=yes; make clean
AFL_USE_ASAN=1 ASAN_OPTIONS=detect_leaks=0 make CC=/path/to/afl-cc CFLAGS="-Wl,--allow-multiple-definition" -j4 -k || \
echo "the compile is expected to fail eventually, but the fuzzers should still build"
In these cases the compilation of grub-emu(-lite) will fail, but the fuzzers themselves will be built and can be run as desired.
--- docs/grub-dev.texi | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
docs/grub-dev.texi +20 -0
@@ -88,6 +88,7 @@ This edition documents version @value{VERSION}. * Graphical Menu Software Design:: * Verifiers framework:: * Lockdown framework:: +* Fuzzing the GRUB:: * Copying This Manual:: Copying This Manual * Index:: @end menu
@@ -2165,6 +2166,25 @@ when the GRUB is in lockdown mode. @end itemize +@node Fuzzing the GRUB +@chapter Fuzzing the GRUB + +The GRUB has support for in-process fuzzing of various image format +parsers, based on libfuzzer. + +Fuzzers are compiled by default if the host compiler supports the +@code{-fsanitize=fuzzer} compile flag. Fuzzers can be explictly enabled with +@code{./configure --enable-fuzzers=yes}, such that configuration will not +succeed unless they can be built. Fuzzers are built as @code{grub-fuzz-XXX} +binaries in the top-level directory. + +These fuzzers build with clang out of the box. When built with clang they +can be run against a corpus as e.g. @code{./grub-fuzz-jpeg jpeg-corpus}. +Consult the LLVM libfuzzer documentation for more details and options. + +Building the fuzzers with other sanitizers (such as AddressSanitizer) or +with AFL++ can provide better results, but the process is not yet stable. + @node Copying This Manual @appendix Copying This Manual
Signature · 1 line
2.25.1