commit
bba5c8a39a
109 files changed
+14085
No files matched your search
@@ -0,0 +1,33 @@
|
||||
# libFuzzer harnesses for VoidLibC format readers.
|
||||
# Requires clang with -fsanitize=fuzzer,address support.
|
||||
# Enable with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
||||
#
|
||||
# Each fuzzer reads arbitrary bytes from stdin (via LLVMFuzzerTestOneInput),
|
||||
# writes them to a memfd, and calls the corresponding parser. A crash means
|
||||
# the parser has undefined behaviour on malformed input.
|
||||
#
|
||||
# Typical usage:
|
||||
# mkdir corpus && ./fuzz_elf_reader corpus/ -max_total_time=3600
|
||||
# ./fuzz_elf_reader -rss_limit_mb=512 corpus/ crashes/
|
||||
|
||||
set(FUZZ_FLAGS -fsanitize=fuzzer,address -g -O1)
|
||||
|
||||
# Reader sources live in voidlink/; elf_reader also needs loader/common/elf64.hpp.
|
||||
set(ELF_READER_SRC ${CMAKE_SOURCE_DIR}/voidlink/elf_reader.cpp)
|
||||
set(COFF_READER_SRC ${CMAKE_SOURCE_DIR}/voidlink/coff_reader.cpp)
|
||||
set(MACHO_READER_SRC ${CMAKE_SOURCE_DIR}/voidlink/macho_reader.cpp)
|
||||
|
||||
function(add_fuzz_target name src)
|
||||
add_executable(${name} ${name}.cpp ${src})
|
||||
target_include_directories(${name} PRIVATE
|
||||
${CMAKE_SOURCE_DIR}
|
||||
${CMAKE_SOURCE_DIR}/voidlink
|
||||
)
|
||||
target_compile_options(${name} PRIVATE ${FUZZ_FLAGS})
|
||||
target_link_options(${name} PRIVATE ${FUZZ_FLAGS})
|
||||
# No install rule — fuzz binaries are for local developer / CI use only.
|
||||
endfunction()
|
||||
|
||||
add_fuzz_target(fuzz_elf_reader ${ELF_READER_SRC})
|
||||
add_fuzz_target(fuzz_coff_reader ${COFF_READER_SRC})
|
||||
add_fuzz_target(fuzz_macho_reader ${MACHO_READER_SRC})
|
||||
@@ -0,0 +1,31 @@
|
||||
// libFuzzer harness for the COFF object file reader.
|
||||
// Build with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
||||
// Run: ./fuzz_coff_reader [-max_total_time=60] [corpus_dir]
|
||||
|
||||
#include "voidlink/coff_reader.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
|
||||
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "fuzz_coff", 0));
|
||||
if (fd < 0) return 0;
|
||||
|
||||
const std::uint8_t* p = data;
|
||||
std::size_t rem = size;
|
||||
while (rem > 0) {
|
||||
const ssize_t n = ::write(fd, p, rem);
|
||||
if (n <= 0) break;
|
||||
p += static_cast<std::size_t>(n);
|
||||
rem -= static_cast<std::size_t>(n);
|
||||
}
|
||||
|
||||
char path[64];
|
||||
std::snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
||||
(void)voidlibc::voidlink::coff::parse_coff_object(path);
|
||||
|
||||
::close(fd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// libFuzzer harness for the ELF object file reader.
|
||||
// Build with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
||||
// Run: ./fuzz_elf_reader [-max_total_time=60] [corpus_dir]
|
||||
|
||||
#include "voidlink/elf_reader.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
|
||||
// Use memfd so no real file I/O hits disk.
|
||||
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "fuzz_elf", 0));
|
||||
if (fd < 0) return 0;
|
||||
|
||||
const std::uint8_t* p = data;
|
||||
std::size_t rem = size;
|
||||
while (rem > 0) {
|
||||
const ssize_t n = ::write(fd, p, rem);
|
||||
if (n <= 0) break;
|
||||
p += static_cast<std::size_t>(n);
|
||||
rem -= static_cast<std::size_t>(n);
|
||||
}
|
||||
|
||||
char path[64];
|
||||
std::snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
||||
(void)voidlibc::voidlink::elf::parse_elf64_object(path);
|
||||
|
||||
::close(fd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// libFuzzer harness for the Mach-O object file reader.
|
||||
// Build with: cmake -DVOIDLIBC_BUILD_FUZZ=ON ...
|
||||
// Run: ./fuzz_macho_reader [-max_total_time=60] [corpus_dir]
|
||||
|
||||
#include "voidlink/macho_reader.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
|
||||
const int fd = static_cast<int>(::syscall(SYS_memfd_create, "fuzz_macho", 0));
|
||||
if (fd < 0) return 0;
|
||||
|
||||
const std::uint8_t* p = data;
|
||||
std::size_t rem = size;
|
||||
while (rem > 0) {
|
||||
const ssize_t n = ::write(fd, p, rem);
|
||||
if (n <= 0) break;
|
||||
p += static_cast<std::size_t>(n);
|
||||
rem -= static_cast<std::size_t>(n);
|
||||
}
|
||||
|
||||
char path[64];
|
||||
std::snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
|
||||
(void)voidlibc::voidlink::macho::parse_macho_object(path);
|
||||
|
||||
::close(fd);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user