Skip to content

Commit 3c9fc07

Browse files
committed
index-pack --verify: read anomalous offsets from v2 idx file
A pack v2 .idx file usually records offset using 64-bit representation only when the offset does not fit within 31-bit, but you can handcraft your .idx file to record smaller offset using 64-bit, storing all zero in the upper 4-byte. By inspecting the original idx file when running index-pack --verify, encode such low offsets that do not need to be in 64-bit but are encoded using 64-bit just like the original idx file so that we can still validate the pack/idx pair by comparing the idx file recomputed with the original. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb956c1 commit 3c9fc07

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

builtin/index-pack.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,51 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
891891
return git_default_config(k, v, cb);
892892
}
893893

894+
static int cmp_uint32(const void *a_, const void *b_)
895+
{
896+
uint32_t a = *((uint32_t *)a_);
897+
uint32_t b = *((uint32_t *)b_);
898+
899+
return (a < b) ? -1 : (a != b);
900+
}
901+
902+
static void read_v2_anomalous_offsets(struct packed_git *p,
903+
struct pack_idx_option *opts)
904+
{
905+
const uint32_t *idx1, *idx2;
906+
uint32_t i;
907+
908+
/* The address of the 4-byte offset table */
909+
idx1 = (((const uint32_t *)p->index_data)
910+
+ 2 /* 8-byte header */
911+
+ 256 /* fan out */
912+
+ 5 * p->num_objects /* 20-byte SHA-1 table */
913+
+ p->num_objects /* CRC32 table */
914+
);
915+
916+
/* The address of the 8-byte offset table */
917+
idx2 = idx1 + p->num_objects;
918+
919+
for (i = 0; i < p->num_objects; i++) {
920+
uint32_t off = ntohl(idx1[i]);
921+
if (!(off & 0x80000000))
922+
continue;
923+
off = off & 0x7fffffff;
924+
if (idx2[off * 2])
925+
continue;
926+
/*
927+
* The real offset is ntohl(idx2[off * 2]) in high 4
928+
* octets, and ntohl(idx2[off * 2 + 1]) in low 4
929+
* octets. But idx2[off * 2] is Zero!!!
930+
*/
931+
ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
932+
opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
933+
}
934+
935+
if (1 < opts->anomaly_nr)
936+
qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32);
937+
}
938+
894939
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
895940
{
896941
struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
@@ -903,6 +948,9 @@ static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
903948
/* Read the attributes from the existing idx file */
904949
opts->version = p->index_version;
905950

951+
if (opts->version == 2)
952+
read_v2_anomalous_offsets(p, opts);
953+
906954
/*
907955
* Get rid of the idx file as we do not need it anymore.
908956
* NEEDSWORK: extract this bit from free_pack_by_name() in

pack-write.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,25 @@ static int sha1_compare(const void *_a, const void *_b)
1616
return hashcmp(a->sha1, b->sha1);
1717
}
1818

19+
static int cmp_uint32(const void *a_, const void *b_)
20+
{
21+
uint32_t a = *((uint32_t *)a_);
22+
uint32_t b = *((uint32_t *)b_);
23+
24+
return (a < b) ? -1 : (a != b);
25+
}
26+
1927
static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
2028
{
21-
return (offset >> 31) || (opts->off32_limit < offset);
29+
uint32_t ofsval;
30+
31+
if ((offset >> 31) || (opts->off32_limit < offset))
32+
return 1;
33+
if (!opts->anomaly_nr)
34+
return 0;
35+
ofsval = offset;
36+
return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr,
37+
sizeof(ofsval), cmp_uint32);
2238
}
2339

2440
/*

pack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ struct pack_idx_option {
4141

4242
uint32_t version;
4343
uint32_t off32_limit;
44+
45+
/*
46+
* List of offsets that would fit within off32_limit but
47+
* need to be written out as 64-bit entity for byte-for-byte
48+
* verification.
49+
*/
50+
int anomaly_alloc, anomaly_nr;
51+
uint32_t *anomaly;
4452
};
4553

4654
extern void reset_pack_idx_option(struct pack_idx_option *);

t/t5302-pack-index.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
107107
git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
108108
'
109109

110-
test_expect_failure OFF64_T 'index-pack --verify on 64-bit offset v2' '
110+
test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
111111
git index-pack --verify "test-3-${pack3}.pack"
112112
'
113113

0 commit comments

Comments
 (0)