From fcf5daea074c9014777930fcad235b65060e9319 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Thu, 9 Nov 2023 20:26:28 +0100 Subject: [PATCH] Update fuzzing --- src/ELF/Parser.cpp | 7 +++++-- tests/elf/fuzzing.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/ELF/Parser.cpp b/src/ELF/Parser.cpp index d607516d65..f13842c7d0 100644 --- a/src/ELF/Parser.cpp +++ b/src/ELF/Parser.cpp @@ -562,11 +562,14 @@ std::unique_ptr Parser::get_note(uint32_t type, std::string name, ok_error_t Parser::parse_notes(uint64_t offset, uint64_t size) { LIEF_DEBUG("== Parsing note segment =="); - stream_->setpos(offset); uint64_t last_offset = offset + size; - while(stream_->pos() < last_offset) { + if (!*stream_) { + return make_error_code(lief_errors::read_error); + } + + while (*stream_ && stream_->pos() < last_offset) { const auto current_pos = static_cast(stream_->pos()); std::unique_ptr note = Note::create( *stream_, diff --git a/tests/elf/fuzzing.py b/tests/elf/fuzzing.py index 24539f557b..a12b0ed440 100644 --- a/tests/elf/fuzzing.py +++ b/tests/elf/fuzzing.py @@ -37,16 +37,19 @@ def fuzz(melkor, seed, nb): outputdir: Path = generate_samples(melkor, seed, nb) print(outputdir) for file in outputdir.iterdir(): - if not lief.is_elf(file.as_posix()): continue - print(f"Tring to parse {file!s}") - lief.parse(file.as_posix()) - + print(f"Trying to parse {file!s}") + subprocess.check_call( + ( + sys.executable, "-c", + f"import lief;lief.logging.disable();lief.parse('{file.as_posix()}')" + ), + env=os.environ.copy() + ) if __name__ == '__main__': - if not is_linux() and not is_x86_64(): print("Melkor fuzzing is currently only supported on Linux x86-64", file=sys.stderr) @@ -67,6 +70,4 @@ def fuzz(melkor, seed, nb): args = parser.parse_args() fuzz(args.melkor, args.input_seed, args.nb_samples) - print(lief) - - + sys.exit(0)