Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate LibFuzzer #267

Closed
wants to merge 1 commit into from
Closed

Conversation

henrybear327
Copy link
Collaborator

By leveraging the LLVM's LibFuzzer, we will be able to test our emulator code more thoroughly.

The seed corpus being added automatically are all the elf files in the build folder.

Major changes:

  • Conditional compilation for the main function, as the fuzzer will generate its own main function
  • Add Clang to the Dockerfile
  • Introduce loading binary stream as elf file, as the input from fuzzer will be passed into the emulator directly instead of going through a file

Minor changes:

  • Fixed ELF verification logic

Currently, known improvements are:

  • Fix the Makefile to handle the compilation flags and compile coexisting .c and .cc files correctly

@henrybear327
Copy link
Collaborator Author

henrybear327 commented Nov 17, 2023

Based on the initial fuzzer runs, I think the ELF loader logic doesn't check for invalid ELF strictly yet.

For example, this is one of the crashes produced by the fuzzer

Executed with failure
src/elf.c:145:52: runtime error: member access within misaligned address 0x1000078803437 for type 'const struct Elf32_Shdr', which requires 4 byte alignment
0x1000078803437: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/elf.c:145:52 in 
src/elf.c:145:52: runtime error: load of misaligned address 0x1000078803437 for type 'const Elf32_Word' (aka 'const unsigned int'), which requires 4 byte alignment
0x1000078803437: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/elf.c:145:52 in 
AddressSanitizer:DEADLYSIGNAL

This PR was also created based on the fuzzer's crash log.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cppcheck (reported by Codacy) found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
src/fuzzer/fuzzer.md Fixed Show fixed Hide fixed
@henrybear327 henrybear327 requested a review from jserv November 17, 2023 10:08
@henrybear327 henrybear327 self-assigned this Nov 17, 2023
@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 2 times, most recently from 07fa043 to adeed6e Compare November 17, 2023 10:16
Makefile Outdated Show resolved Hide resolved
run_fuzzer.sh Outdated Show resolved Hide resolved
src/elf.c Outdated Show resolved Hide resolved
src/fuzzer/fuzzer.md Outdated Show resolved Hide resolved
src/riscv.h Outdated Show resolved Hide resolved
jserv

This comment was marked as outdated.

@jserv jserv changed the title Introduce fuzzer Integrate fuzzer Nov 17, 2023
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
docs/fuzzer.md Fixed Show fixed Hide fixed
@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 2 times, most recently from 08b2788 to d8ff776 Compare November 17, 2023 14:18
docs/fuzzer.md Fixed Show resolved Hide resolved
@henrybear327
Copy link
Collaborator Author

Resolve the issues reported by Codacy.

I tried to resolve most, but there are some that are hard to understand what the error is about, or, some don't make sense to fix (single exit point per function, use the output of fprintf, don't use abort() in fuzzer, etc.) .

@henrybear327 henrybear327 requested a review from jserv November 17, 2023 14:58
@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 2 times, most recently from 298a641 to 014255a Compare November 17, 2023 15:11
.ci/riscv-fuzz.sh Outdated Show resolved Hide resolved
docs/fuzzer.md Outdated Show resolved Hide resolved
src/elf.c Outdated Show resolved Hide resolved
src/fuzz_target.cc Outdated Show resolved Hide resolved
.ci/fuzz.sh Outdated Show resolved Hide resolved
docs/fuzzer.md Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 5 times, most recently from 5ec3349 to 7b9fe83 Compare November 23, 2023 06:28
jserv

This comment was marked as outdated.

@jserv
Copy link
Contributor

jserv commented Nov 23, 2023

Rather than creating a separate standalone fuzzer program, we can explore the option of integrating the fuzzing components as a feature activated by a command-line option. This approach aims to minimize the necessary changes for fuzzer integration. If we opt for this method, we will need to address the linkage between the C++-based LibFuzzer and the existing C codebase.

See src/feature.h for the use of macro RV32_HAS.

@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 3 times, most recently from c463882 to a2c318b Compare November 23, 2023 15:33
@henrybear327
Copy link
Collaborator Author

Rather than creating a separate standalone fuzzer program, we can explore the option of integrating the fuzzing components as a feature activated by a command-line option. This approach aims to minimize the necessary changes for fuzzer integration. If we opt for this method, we will need to address the linkage between the C++-based LibFuzzer and the existing C codebase.

See src/feature.h for the use of macro RV32_HAS.

I am not aware of how we can merge both main() functions.

My current understanding is that for the LibFuzzer to work, we will need to provide an implementation of LLVMFuzzerTestOneInput, and during complication, a separate main() will be generated and be used for driving the actions for the fuzzer.

If there is more information or hints for merging the fuzzer into the current binary, I am happy to learn! :)

src/main.c Outdated
char *optarg = args[idx + 1];

switch (opt) {
case 's': // binary string

This comment was marked as outdated.

src/main.c Outdated Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
@jserv
Copy link
Contributor

jserv commented Nov 24, 2023

It seems that C-only source files are compatible with libFuzzer. e.g., libFuzzer-examples.

@henrybear327 henrybear327 force-pushed the feat/fuzzer branch 2 times, most recently from 56cbb36 to 9b68828 Compare November 26, 2023 09:02
elf_strlen = atoi(optarg);
emu_argc++;
break;
case 'k': /* max execution cycle (since some program won't terminate, e.g. while(1) {} */

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
jserv

This comment was marked as outdated.

By leveraging the LLVM's LibFuzzer, we will be able to test our
emulator code more thoroughly.

The seed corpus being added automatically are all the elf files in the
build folder.

Major changes:
- Conditional compilation for the main function, as the fuzzer will
generate its own main function
- Need to use the clang toolchain
- Introduce loading buffer as elf file, as the input from fuzzer will
be passed into the emulator directly instead of going through a file
- Fixed ELF verification logic as the fuzzer already breaks the code

Other minor changes are:
- Fix Codacy issue "rejecting SARIF, as there are more runs than allowed"
@jserv jserv marked this pull request as draft December 6, 2023 21:28
@jserv
Copy link
Contributor

jserv commented Dec 16, 2023

I am closing this pull request because we have not made a clear decision on how we can effectively utilize libFuzzer to identify potential issues.

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.

2 participants