Skip to content

Commit fe2033b

Browse files
briantracygitster
authored andcommitted
fuzz: add fuzzer for config parsing
Add a new fuzz target that exercises the parsing of git configs. The existing git_config_from_mem function is a perfect entry point for fuzzing as it exercises the same code paths as the rest of the config parsing functions and offers an easily fuzzable interface. Config parsing is a useful thing to fuzz because it operates on user controlled data and is a central component of many git operations. Signed-off-by: Brian C Tracy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f9b731 commit fe2033b

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ ETAGS_TARGET = TAGS
757757
# runs in the future.
758758
FUZZ_OBJS += oss-fuzz/dummy-cmd-main.o
759759
FUZZ_OBJS += oss-fuzz/fuzz-commit-graph.o
760+
FUZZ_OBJS += oss-fuzz/fuzz-config.o
760761
FUZZ_OBJS += oss-fuzz/fuzz-date.o
761762
FUZZ_OBJS += oss-fuzz/fuzz-pack-headers.o
762763
FUZZ_OBJS += oss-fuzz/fuzz-pack-idx.o

ci/run-build-and-minimal-fuzzers.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ group "Build fuzzers" make \
1212
LIB_FUZZING_ENGINE="-fsanitize=fuzzer,address" \
1313
fuzz-all
1414

15-
for fuzzer in commit-graph date pack-headers pack-idx ; do
15+
for fuzzer in commit-graph config date pack-headers pack-idx ; do
1616
begin_group "fuzz-$fuzzer"
1717
./oss-fuzz/fuzz-$fuzzer -verbosity=0 -runs=1 || exit 1
1818
end_group "fuzz-$fuzzer"

oss-fuzz/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fuzz-commit-graph
2+
fuzz-config
23
fuzz-date
34
fuzz-pack-headers
45
fuzz-pack-idx

oss-fuzz/fuzz-config.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "git-compat-util.h"
2+
#include "config.h"
3+
4+
int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
5+
static int config_parser_callback(const char *, const char *,
6+
const struct config_context *, void *);
7+
8+
static int config_parser_callback(const char *key, const char *value,
9+
const struct config_context *ctx UNUSED,
10+
void *data UNUSED)
11+
{
12+
/*
13+
* Visit every byte of memory we are given to make sure the parser
14+
* gave it to us appropriately. We need to unconditionally return 0,
15+
* but we also want to prevent the strlen from being optimized away.
16+
*/
17+
size_t c = strlen(key);
18+
19+
if (value)
20+
c += strlen(value);
21+
return c == SIZE_MAX;
22+
}
23+
24+
int LLVMFuzzerTestOneInput(const uint8_t *data, const size_t size)
25+
{
26+
struct config_options config_opts = { 0 };
27+
28+
config_opts.error_action = CONFIG_ERROR_SILENT;
29+
git_config_from_mem(config_parser_callback, CONFIG_ORIGIN_BLOB,
30+
"fuzztest-config", (const char *)data, size, NULL,
31+
CONFIG_SCOPE_UNKNOWN, &config_opts);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)