From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 075731382C5 for ; Fri, 16 Apr 2021 19:26:53 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 04C29E081E; Fri, 16 Apr 2021 19:26:52 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id E4E83E081E for ; Fri, 16 Apr 2021 19:26:51 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id C26C0340BDE for ; Fri, 16 Apr 2021 19:26:50 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 78298647 for ; Fri, 16 Apr 2021 19:26:49 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1618600921.67f3ba64c91b5e1ac9fbbd0bc039fb8ca653cae1.vapier@gentoo> Subject: [gentoo-commits] proj/pax-utils:master commit in: / X-VCS-Repository: proj/pax-utils X-VCS-Files: Makefile dumpelf.c X-VCS-Directories: / X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: 67f3ba64c91b5e1ac9fbbd0bc039fb8ca653cae1 X-VCS-Branch: master Date: Fri, 16 Apr 2021 19:26:49 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 6aa9b36f-22f9-4249-b08f-efadaf2caf0c X-Archives-Hash: a1f81805ddc1cc861abb59e6065361bf commit: 67f3ba64c91b5e1ac9fbbd0bc039fb8ca653cae1 Author: Mike Frysinger gentoo org> AuthorDate: Sat Mar 4 23:46:33 2017 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Fri Apr 16 19:22:01 2021 +0000 URL: https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=67f3ba64 dumpelf: add libFuzzer support Now you can build dumpelf with libFuzzer and beat the hell out of it. Signed-off-by: Mike Frysinger gentoo.org> Makefile | 2 +- dumpelf.c | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 8e7b183..9a2c07c 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ afl-fuzz: clean "afl-fuzz -t 100 -i tests/fuzz/small/ -o findings/ ./scanelf -s '*' -axetrnibSDIYZB @@" # Not all objects support libfuzzer. -LIBFUZZER_TARGETS = +LIBFUZZER_TARGETS = dumpelf LIBFUZZER_FLAGS = \ -fsanitize=fuzzer \ -fsanitize-coverage=edge diff --git a/dumpelf.c b/dumpelf.c index bc634f0..342251f 100644 --- a/dumpelf.c +++ b/dumpelf.c @@ -11,7 +11,6 @@ const char argv0[] = "dumpelf"; #include "paxinc.h" /* prototypes */ -static void dumpelf(const char *filename, size_t file_cnt); static void dump_ehdr(elfobj *elf, const void *ehdr); static void dump_phdr(elfobj *elf, const void *phdr, size_t phdr_cnt); static void dump_shdr(elfobj *elf, const void *shdr, size_t shdr_cnt, const char *section_name); @@ -31,15 +30,10 @@ static char be_verbose = 0; static const void *phdr_dynamic_void; /* dump all internal elf info */ -static void dumpelf(const char *filename, size_t file_cnt) +static void dumpelf(elfobj *elf, size_t file_cnt) { - elfobj *elf; size_t i, b; - /* verify this is real ELF */ - if ((elf = readelf(filename)) == NULL) - return; - phdr_dynamic_void = NULL; printf("#include \n"); @@ -50,7 +44,7 @@ static void dumpelf(const char *filename, size_t file_cnt) " * ELF dump of '%s'\n" " * %ji (0x%jX) bytes\n" " */\n\n", - filename, elf->len, elf->len); + elf->filename, elf->len, elf->len); /* setup the struct to namespace this elf */ #define MAKE_STRUCT(B) \ @@ -148,6 +142,17 @@ static void dumpelf(const char *filename, size_t file_cnt) printf(" /* no dynamic tags ! */ "); } printf("};\n"); +} + +static void dumpelf_file(const char *filename, size_t file_cnt) +{ + elfobj *elf = readelf(filename); + + /* verify this is real ELF */ + if (elf == NULL) + return; + + dumpelf(elf, file_cnt); /* get out of here */ unreadelf(elf); @@ -570,10 +575,29 @@ static void parseargs(int argc, char *argv[]) size_t file_cnt = 0; while (optind < argc) - dumpelf(argv[optind++], file_cnt++); + dumpelf_file(argv[optind++], file_cnt++); } } +#if PAX_UTILS_LIBFUZZ +int LLVMFuzzerInitialize(int *argc, char ***argv) +{ + (void)argc; + (void)argv; + (void)parseargs; + security_init(false); + return 0; +} + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + elfobj *elf = readelf_buffer("libFuzzer", data, size); + if (elf == NULL) + return 0; + dumpelf(elf, 0); + return 0; +} +#else int main(int argc, char *argv[]) { security_init(false); @@ -582,3 +606,4 @@ int main(int argc, char *argv[]) parseargs(argc, argv); return EXIT_SUCCESS; } +#endif