Skip to content

Commit 1127a98

Browse files
steadmongitster
authored andcommitted
fuzz: add fuzz testing for packfile indices.
Breaks the majority of check_packed_git_idx() into a separate function, load_idx(). The latter function operates on arbitrary buffers, which makes it suitable as a fuzzing test target. Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5e47215 commit 1127a98

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/fuzz_corpora
22
/fuzz-pack-headers
3+
/fuzz-pack-idx
34
/GIT-BUILD-OPTIONS
45
/GIT-CFLAGS
56
/GIT-LDFLAGS

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ SCRIPTS = $(SCRIPT_SH_INS) \
685685
ETAGS_TARGET = TAGS
686686

687687
FUZZ_OBJS += fuzz-pack-headers.o
688+
FUZZ_OBJS += fuzz-pack-idx.o
688689

689690
# Always build fuzz objects even if not testing, to prevent bit-rot.
690691
all:: $(FUZZ_OBJS)

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)