Skip to content

Commit 0c41b3b

Browse files
committed
Merge branch 'js/fuzzer'
An experiment to fuzz test a few areas, hopefully we can gain more coverage to various areas. * js/fuzzer: fuzz: add fuzz testing for packfile indices. fuzz: add basic fuzz testing target.
2 parents 7752999 + 1127a98 commit 0c41b3b

File tree

6 files changed

+101
-19
lines changed

6 files changed

+101
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/fuzz_corpora
2+
/fuzz-pack-headers
3+
/fuzz-pack-idx
14
/GIT-BUILD-OPTIONS
25
/GIT-CFLAGS
36
/GIT-LDFLAGS

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ XDIFF_OBJS =
590590
VCSSVN_OBJS =
591591
GENERATED_H =
592592
EXTRA_CPPFLAGS =
593+
FUZZ_OBJS =
594+
FUZZ_PROGRAMS =
593595
LIB_OBJS =
594596
PROGRAM_OBJS =
595597
PROGRAMS =
@@ -682,6 +684,14 @@ SCRIPTS = $(SCRIPT_SH_INS) \
682684

683685
ETAGS_TARGET = TAGS
684686

687+
FUZZ_OBJS += fuzz-pack-headers.o
688+
FUZZ_OBJS += fuzz-pack-idx.o
689+
690+
# Always build fuzz objects even if not testing, to prevent bit-rot.
691+
all:: $(FUZZ_OBJS)
692+
693+
FUZZ_PROGRAMS += $(patsubst %.o,%,$(FUZZ_OBJS))
694+
685695
# Empty...
686696
EXTRA_PROGRAMS =
687697

@@ -2253,6 +2263,7 @@ TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST
22532263
OBJECTS := $(LIB_OBJS) $(BUILTIN_OBJS) $(PROGRAM_OBJS) $(TEST_OBJS) \
22542264
$(XDIFF_OBJS) \
22552265
$(VCSSVN_OBJS) \
2266+
$(FUZZ_OBJS) \
22562267
common-main.o \
22572268
git.o
22582269
ifndef NO_CURL
@@ -2951,6 +2962,7 @@ clean: profile-clean coverage-clean cocciclean
29512962
$(RM) $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)
29522963
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
29532964
$(RM) $(TEST_PROGRAMS) $(NO_INSTALL)
2965+
$(RM) $(FUZZ_PROGRAMS)
29542966
$(RM) -r bin-wrappers $(dep_dirs)
29552967
$(RM) -r po/build/
29562968
$(RM) *.pyc *.pyo */*.pyc */*.pyo command-list.h $(ETAGS_TARGET) tags cscope*
@@ -3075,3 +3087,24 @@ cover_db: coverage-report
30753087
cover_db_html: cover_db
30763088
cover -report html -outputdir cover_db_html cover_db
30773089

3090+
3091+
### Fuzz testing
3092+
#
3093+
# Building fuzz targets generally requires a special set of compiler flags that
3094+
# are not necessarily appropriate for general builds, and that vary greatly
3095+
# depending on the compiler version used.
3096+
#
3097+
# An example command to build against libFuzzer from LLVM 4.0.0:
3098+
#
3099+
# make CC=clang CXX=clang++ \
3100+
# CFLAGS="-fsanitize-coverage=trace-pc-guard -fsanitize=address" \
3101+
# LIB_FUZZING_ENGINE=/usr/lib/llvm-4.0/lib/libFuzzer.a \
3102+
# fuzz-all
3103+
#
3104+
.PHONY: fuzz-all
3105+
3106+
$(FUZZ_PROGRAMS): all
3107+
$(QUIET_LINK)$(CXX) $(CFLAGS) $(LIB_OBJS) $(BUILTIN_OBJS) \
3108+
$(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@
3109+
3110+
fuzz-all: $(FUZZ_PROGRAMS)

fuzz-pack-headers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "packfile.h"
2+
3+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
4+
5+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
6+
{
7+
enum object_type type;
8+
unsigned long len;
9+
10+
unpack_object_header_buffer((const unsigned char *)data,
11+
(unsigned long)size, &type, &len);
12+
13+
return 0;
14+
}

fuzz-pack-idx.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "object-store.h"
2+
#include "packfile.h"
3+
4+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
5+
6+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
7+
{
8+
struct packed_git p;
9+
10+
load_idx("fuzz-input", GIT_SHA1_RAWSZ, (void *)data, size, &p);
11+
12+
return 0;
13+
}

packfile.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ void pack_report(void)
8080
static int check_packed_git_idx(const char *path, struct packed_git *p)
8181
{
8282
void *idx_map;
83-
struct pack_idx_header *hdr;
8483
size_t idx_size;
85-
uint32_t version, nr, i, *index;
86-
int fd = git_open(path);
84+
int fd = git_open(path), ret;
8785
struct stat st;
8886
const unsigned int hashsz = the_hash_algo->rawsz;
8987

@@ -101,16 +99,32 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
10199
idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
102100
close(fd);
103101

104-
hdr = idx_map;
102+
ret = load_idx(path, hashsz, idx_map, idx_size, p);
103+
104+
if (ret)
105+
munmap(idx_map, idx_size);
106+
107+
return ret;
108+
}
109+
110+
int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
111+
size_t idx_size, struct packed_git *p)
112+
{
113+
struct pack_idx_header *hdr = idx_map;
114+
uint32_t version, nr, i, *index;
115+
116+
if (idx_size < 4 * 256 + hashsz + hashsz)
117+
return error("index file %s is too small", path);
118+
if (idx_map == NULL)
119+
return error("empty data");
120+
105121
if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
106122
version = ntohl(hdr->idx_version);
107-
if (version < 2 || version > 2) {
108-
munmap(idx_map, idx_size);
123+
if (version < 2 || version > 2)
109124
return error("index file %s is version %"PRIu32
110125
" and is not supported by this binary"
111126
" (try upgrading GIT to a newer version)",
112127
path, version);
113-
}
114128
} else
115129
version = 1;
116130

@@ -120,10 +134,8 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
120134
index += 2; /* skip index header */
121135
for (i = 0; i < 256; i++) {
122136
uint32_t n = ntohl(index[i]);
123-
if (n < nr) {
124-
munmap(idx_map, idx_size);
137+
if (n < nr)
125138
return error("non-monotonic index %s", path);
126-
}
127139
nr = n;
128140
}
129141

@@ -135,10 +147,8 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
135147
* - hash of the packfile
136148
* - file checksum
137149
*/
138-
if (idx_size != 4*256 + nr * (hashsz + 4) + hashsz + hashsz) {
139-
munmap(idx_map, idx_size);
150+
if (idx_size != 4 * 256 + nr * (hashsz + 4) + hashsz + hashsz)
140151
return error("wrong index v1 file size in %s", path);
141-
}
142152
} else if (version == 2) {
143153
/*
144154
* Minimum size:
@@ -157,20 +167,16 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
157167
unsigned long max_size = min_size;
158168
if (nr)
159169
max_size += (nr - 1)*8;
160-
if (idx_size < min_size || idx_size > max_size) {
161-
munmap(idx_map, idx_size);
170+
if (idx_size < min_size || idx_size > max_size)
162171
return error("wrong index v2 file size in %s", path);
163-
}
164172
if (idx_size != min_size &&
165173
/*
166174
* make sure we can deal with large pack offsets.
167175
* 31-bit signed offset won't be enough, neither
168176
* 32-bit unsigned one will be.
169177
*/
170-
(sizeof(off_t) <= 4)) {
171-
munmap(idx_map, idx_size);
178+
(sizeof(off_t) <= 4))
172179
return error("pack too large for current definition of off_t in %s", path);
173-
}
174180
}
175181

176182
p->index_version = version;

packfile.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,17 @@ extern int has_pack_index(const unsigned char *sha1);
164164
*/
165165
extern int is_promisor_object(const struct object_id *oid);
166166

167+
/*
168+
* Expose a function for fuzz testing.
169+
*
170+
* load_idx() parses a block of memory as a packfile index and puts the results
171+
* into a struct packed_git.
172+
*
173+
* This function should not be used directly. It is exposed here only so that we
174+
* have a convenient entry-point for fuzz testing. For real uses, you should
175+
* probably use open_pack_index() or parse_pack_index() instead.
176+
*/
177+
extern int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
178+
size_t idx_size, struct packed_git *p);
179+
167180
#endif

0 commit comments

Comments
 (0)