Skip to content

Add fuzz testing for BMP header loading#7

Merged
JacobBorden merged 2 commits intodevelopmentfrom
fuzz-load-bmp
May 29, 2025
Merged

Add fuzz testing for BMP header loading#7
JacobBorden merged 2 commits intodevelopmentfrom
fuzz-load-bmp

Conversation

@JacobBorden
Copy link
Owner

This commit introduces fuzz testing for the BmpTool::load() function, specifically targeting 54-byte BMP headers to ensure graceful failure without heap interaction when processing malformed headers.

Key changes:

  • Integrated libFuzzer into the CMake build system with an ENABLE_FUZZING option. This uses Clang and AddressSanitizer when enabled.
  • Created a fuzzing harness (tests/fuzz/fuzz_bitmap.cpp) that feeds 54-byte data chunks from the fuzzer to BmpTool::load().
  • Added a GitHub Actions workflow (.github/workflows/fuzzing.yml) to automate the fuzzing process on pushes and pull requests. The workflow builds with Clang, ASan, and runs the fuzzer for a short duration.
  • Added a unit test (tests/test_bitmap.cpp) to specifically check for correct error handling (BitmapError::NotABmp) when an invalid BMP magic number is encountered.

Manual analysis of BmpTool::load() suggests it is designed to prevent heap allocations if header validation fails for 54-byte inputs. The primary risks identified (integer overflows in validation, mishandling of ih.biSize) are targets for the automated fuzzing setup.

A Clang compiler crash was encountered in the development environment when compiling src/bitmap/bitmap.cpp with fuzzing flags. This prevented direct execution and iteration of the fuzzer during this development, but the fuzzing infrastructure is provided for environments where this compilation succeeds (e.g., the GitHub Actions runner).

This commit introduces fuzz testing for the `BmpTool::load()` function,
specifically targeting 54-byte BMP headers to ensure graceful failure
without heap interaction when processing malformed headers.

Key changes:
- Integrated libFuzzer into the CMake build system with an
  `ENABLE_FUZZING` option. This uses Clang and AddressSanitizer
  when enabled.
- Created a fuzzing harness (`tests/fuzz/fuzz_bitmap.cpp`) that
  feeds 54-byte data chunks from the fuzzer to `BmpTool::load()`.
- Added a GitHub Actions workflow (`.github/workflows/fuzzing.yml`)
  to automate the fuzzing process on pushes and pull requests.
  The workflow builds with Clang, ASan, and runs the fuzzer for a
  short duration.
- Added a unit test (`tests/test_bitmap.cpp`) to specifically check
  for correct error handling (`BitmapError::NotABmp`) when an
  invalid BMP magic number is encountered.

Manual analysis of `BmpTool::load()` suggests it is designed to
prevent heap allocations if header validation fails for 54-byte inputs.
The primary risks identified (integer overflows in validation,
mishandling of `ih.biSize`) are targets for the automated fuzzing
setup.

A Clang compiler crash was encountered in the development environment
when compiling `src/bitmap/bitmap.cpp` with fuzzing flags. This
prevented direct execution and iteration of the fuzzer during this
development, but the fuzzing infrastructure is provided for environments
where this compilation succeeds (e.g., the GitHub Actions runner).
The method `result.has_error()` was incorrectly used in the
`LoadWithInvalidMagicType` test case in `tests/test_bitmap.cpp`.
This has been changed to the correct method `result.isError()`,
as indicated by build failures in the CI.
@JacobBorden JacobBorden merged commit be40d65 into development May 29, 2025
1 check passed
@JacobBorden JacobBorden deleted the fuzz-load-bmp branch May 29, 2025 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant