Gentoo Archives: gentoo-commits

From: Mike Frysinger <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/pax-utils:master commit in: /
Date: Fri, 16 Apr 2021 19:26:53
Message-Id: 1618600921.67f3ba64c91b5e1ac9fbbd0bc039fb8ca653cae1.vapier@gentoo
1 commit: 67f3ba64c91b5e1ac9fbbd0bc039fb8ca653cae1
2 Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
3 AuthorDate: Sat Mar 4 23:46:33 2017 +0000
4 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
5 CommitDate: Fri Apr 16 19:22:01 2021 +0000
6 URL: https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=67f3ba64
7
8 dumpelf: add libFuzzer support
9
10 Now you can build dumpelf with libFuzzer and beat the hell out of it.
11
12 Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>
13
14 Makefile | 2 +-
15 dumpelf.c | 43 ++++++++++++++++++++++++++++++++++---------
16 2 files changed, 35 insertions(+), 10 deletions(-)
17
18 diff --git a/Makefile b/Makefile
19 index 8e7b183..9a2c07c 100644
20 --- a/Makefile
21 +++ b/Makefile
22 @@ -115,7 +115,7 @@ afl-fuzz: clean
23 "afl-fuzz -t 100 -i tests/fuzz/small/ -o findings/ ./scanelf -s '*' -axetrnibSDIYZB @@"
24
25 # Not all objects support libfuzzer.
26 -LIBFUZZER_TARGETS =
27 +LIBFUZZER_TARGETS = dumpelf
28 LIBFUZZER_FLAGS = \
29 -fsanitize=fuzzer \
30 -fsanitize-coverage=edge
31
32 diff --git a/dumpelf.c b/dumpelf.c
33 index bc634f0..342251f 100644
34 --- a/dumpelf.c
35 +++ b/dumpelf.c
36 @@ -11,7 +11,6 @@ const char argv0[] = "dumpelf";
37 #include "paxinc.h"
38
39 /* prototypes */
40 -static void dumpelf(const char *filename, size_t file_cnt);
41 static void dump_ehdr(elfobj *elf, const void *ehdr);
42 static void dump_phdr(elfobj *elf, const void *phdr, size_t phdr_cnt);
43 static void dump_shdr(elfobj *elf, const void *shdr, size_t shdr_cnt, const char *section_name);
44 @@ -31,15 +30,10 @@ static char be_verbose = 0;
45 static const void *phdr_dynamic_void;
46
47 /* dump all internal elf info */
48 -static void dumpelf(const char *filename, size_t file_cnt)
49 +static void dumpelf(elfobj *elf, size_t file_cnt)
50 {
51 - elfobj *elf;
52 size_t i, b;
53
54 - /* verify this is real ELF */
55 - if ((elf = readelf(filename)) == NULL)
56 - return;
57 -
58 phdr_dynamic_void = NULL;
59
60 printf("#include <elf.h>\n");
61 @@ -50,7 +44,7 @@ static void dumpelf(const char *filename, size_t file_cnt)
62 " * ELF dump of '%s'\n"
63 " * %ji (0x%jX) bytes\n"
64 " */\n\n",
65 - filename, elf->len, elf->len);
66 + elf->filename, elf->len, elf->len);
67
68 /* setup the struct to namespace this elf */
69 #define MAKE_STRUCT(B) \
70 @@ -148,6 +142,17 @@ static void dumpelf(const char *filename, size_t file_cnt)
71 printf(" /* no dynamic tags ! */ ");
72 }
73 printf("};\n");
74 +}
75 +
76 +static void dumpelf_file(const char *filename, size_t file_cnt)
77 +{
78 + elfobj *elf = readelf(filename);
79 +
80 + /* verify this is real ELF */
81 + if (elf == NULL)
82 + return;
83 +
84 + dumpelf(elf, file_cnt);
85
86 /* get out of here */
87 unreadelf(elf);
88 @@ -570,10 +575,29 @@ static void parseargs(int argc, char *argv[])
89 size_t file_cnt = 0;
90
91 while (optind < argc)
92 - dumpelf(argv[optind++], file_cnt++);
93 + dumpelf_file(argv[optind++], file_cnt++);
94 }
95 }
96
97 +#if PAX_UTILS_LIBFUZZ
98 +int LLVMFuzzerInitialize(int *argc, char ***argv)
99 +{
100 + (void)argc;
101 + (void)argv;
102 + (void)parseargs;
103 + security_init(false);
104 + return 0;
105 +}
106 +
107 +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
108 +{
109 + elfobj *elf = readelf_buffer("libFuzzer", data, size);
110 + if (elf == NULL)
111 + return 0;
112 + dumpelf(elf, 0);
113 + return 0;
114 +}
115 +#else
116 int main(int argc, char *argv[])
117 {
118 security_init(false);
119 @@ -582,3 +606,4 @@ int main(int argc, char *argv[])
120 parseargs(argc, argv);
121 return EXIT_SUCCESS;
122 }
123 +#endif