Skip to content

Commit 797bd6f

Browse files
author
Junio C Hamano
committed
check_packed_git_idx(): check integrity of the idx file itself.
Although pack-check.c had routine to verify the checksum for the pack index file itself, the core did not check it before using it. This is stolen from the patch to tighten packname requirements. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 5f94c73 commit 797bd6f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

sha1_file.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,16 @@ struct packed_git *packed_git;
321321
static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
322322
void **idx_map_)
323323
{
324+
SHA_CTX ctx;
325+
unsigned char sha1[20];
324326
void *idx_map;
325327
unsigned int *index;
326328
unsigned long idx_size;
327329
int nr, i;
328-
int fd = open(path, O_RDONLY);
330+
int fd;
329331
struct stat st;
332+
333+
fd = open(path, O_RDONLY);
330334
if (fd < 0)
331335
return -1;
332336
if (fstat(fd, &st)) {
@@ -364,6 +368,16 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
364368
if (idx_size != 4*256 + nr * 24 + 20 + 20)
365369
return error("wrong index file size");
366370

371+
/*
372+
* File checksum.
373+
*/
374+
SHA1_Init(&ctx);
375+
SHA1_Update(&ctx, idx_map, idx_size-20);
376+
SHA1_Final(sha1, &ctx);
377+
378+
if (memcmp(sha1, idx_map + idx_size - 20, 20))
379+
return error("index checksum mismatch");
380+
367381
return 0;
368382
}
369383

t/t5300-pack-object.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ test_expect_success \
163163
else :;
164164
fi &&
165165
166+
cp test-1-${packname_1}.pack test-3.pack &&
167+
dd if=/dev/zero of=test-3.idx count=1 bs=1 conv=notrunc seek=1200 &&
168+
if git-verify-pack test-3.pack
169+
then false
170+
else :;
171+
fi &&
172+
166173
:'
167174

168175
test_expect_success \

0 commit comments

Comments
 (0)