]>
Commit | Line | Data |
---|---|---|
a1283866 JK |
1 | #!/bin/sh |
2 | ||
3 | test_description='bounds-checking of access to mmapped on-disk file formats' | |
4 | . ./test-lib.sh | |
5 | ||
6 | clear_base () { | |
7 | test_when_finished 'restore_base' && | |
8 | rm -f $base | |
9 | } | |
10 | ||
11 | restore_base () { | |
12 | cp base-backup/* .git/objects/pack/ | |
13 | } | |
14 | ||
15 | do_pack () { | |
16 | pack_objects=$1; shift | |
17 | sha1=$( | |
18 | for i in $pack_objects | |
19 | do | |
20 | echo $i | |
21 | done | git pack-objects "$@" .git/objects/pack/pack | |
22 | ) && | |
23 | pack=.git/objects/pack/pack-$sha1.pack && | |
24 | idx=.git/objects/pack/pack-$sha1.idx && | |
25 | chmod +w $pack $idx && | |
26 | test_when_finished 'rm -f "$pack" "$idx"' | |
27 | } | |
28 | ||
29 | munge () { | |
30 | printf "$3" | dd of="$1" bs=1 conv=notrunc seek=$2 | |
31 | } | |
32 | ||
33 | # Offset in a v2 .idx to its initial and extended offset tables. For an index | |
34 | # with "nr" objects, this is: | |
35 | # | |
36 | # magic(4) + version(4) + fan-out(4*256) + sha1s(20*nr) + crc(4*nr), | |
37 | # | |
38 | # for the initial, and another ofs(4*nr) past that for the extended. | |
39 | # | |
40 | ofs_table () { | |
a30f93b1 | 41 | echo $((4 + 4 + 4*256 + $(test_oid rawsz)*$1 + 4*$1)) |
a1283866 JK |
42 | } |
43 | extended_table () { | |
44 | echo $(($(ofs_table "$1") + 4*$1)) | |
45 | } | |
46 | ||
a30f93b1 | 47 | test_expect_success 'setup' ' |
a30f93b1 | 48 | test_oid_cache <<-EOF |
49 | oid000 sha1:1485 | |
50 | oid000 sha256:4222 | |
51 | ||
52 | oidfff sha1:74 | |
53 | oidfff sha256:1350 | |
54 | EOF | |
55 | ' | |
56 | ||
a1283866 JK |
57 | test_expect_success 'set up base packfile and variables' ' |
58 | # the hash of this content starts with ff, which | |
59 | # makes some later computations much simpler | |
a30f93b1 | 60 | echo $(test_oid oidfff) >file && |
a1283866 JK |
61 | git add file && |
62 | git commit -m base && | |
63 | git repack -ad && | |
64 | base=$(echo .git/objects/pack/*) && | |
65 | chmod +w $base && | |
66 | mkdir base-backup && | |
67 | cp $base base-backup/ && | |
68 | object=$(git rev-parse HEAD:file) | |
69 | ' | |
70 | ||
71 | test_expect_success 'pack/index object count mismatch' ' | |
72 | do_pack $object && | |
73 | munge $pack 8 "\377\0\0\0" && | |
74 | clear_base && | |
75 | ||
76 | # We enumerate the objects from the completely-fine | |
77 | # .idx, but notice later that the .pack is bogus | |
78 | # and fail to show any data. | |
79 | echo "$object missing" >expect && | |
80 | git cat-file --batch-all-objects --batch-check >actual && | |
81 | test_cmp expect actual && | |
82 | ||
83 | # ...and here fail to load the object (without segfaulting), | |
84 | # but fallback to a good copy if available. | |
85 | test_must_fail git cat-file blob $object && | |
86 | restore_base && | |
87 | git cat-file blob $object >actual && | |
88 | test_cmp file actual && | |
89 | ||
90 | # ...and make sure that index-pack --verify, which has its | |
91 | # own reading routines, does not segfault. | |
92 | test_must_fail git index-pack --verify $pack | |
93 | ' | |
94 | ||
95 | test_expect_success 'matched bogus object count' ' | |
96 | do_pack $object && | |
97 | munge $pack 8 "\377\0\0\0" && | |
98 | munge $idx $((255 * 4)) "\377\0\0\0" && | |
99 | clear_base && | |
100 | ||
101 | # Unlike above, we should notice early that the .idx is totally | |
102 | # bogus, and not even enumerate its contents. | |
a1283866 | 103 | git cat-file --batch-all-objects --batch-check >actual && |
d3c6751b | 104 | test_must_be_empty actual && |
a1283866 JK |
105 | |
106 | # But as before, we can do the same object-access checks. | |
107 | test_must_fail git cat-file blob $object && | |
108 | restore_base && | |
109 | git cat-file blob $object >actual && | |
110 | test_cmp file actual && | |
111 | ||
112 | test_must_fail git index-pack --verify $pack | |
113 | ' | |
114 | ||
115 | # Note that we cannot check the fallback case for these | |
116 | # further .idx tests, as we notice the problem in functions | |
117 | # whose interface doesn't allow an error return (like use_pack()), | |
118 | # and thus we just die(). | |
119 | # | |
120 | # There's also no point in doing enumeration tests, as | |
121 | # we are munging offsets here, which are about looking up | |
122 | # specific objects. | |
123 | ||
124 | test_expect_success 'bogus object offset (v1)' ' | |
125 | do_pack $object --index-version=1 && | |
126 | munge $idx $((4 * 256)) "\377\0\0\0" && | |
127 | clear_base && | |
128 | test_must_fail git cat-file blob $object && | |
129 | test_must_fail git index-pack --verify $pack | |
130 | ' | |
131 | ||
132 | test_expect_success 'bogus object offset (v2, no msb)' ' | |
133 | do_pack $object --index-version=2 && | |
134 | munge $idx $(ofs_table 1) "\0\377\0\0" && | |
135 | clear_base && | |
136 | test_must_fail git cat-file blob $object && | |
137 | test_must_fail git index-pack --verify $pack | |
138 | ' | |
139 | ||
47fe3f6e | 140 | test_expect_success 'bogus offset into v2 extended table' ' |
a1283866 JK |
141 | do_pack $object --index-version=2 && |
142 | munge $idx $(ofs_table 1) "\377\0\0\0" && | |
143 | clear_base && | |
144 | test_must_fail git cat-file blob $object && | |
145 | test_must_fail git index-pack --verify $pack | |
146 | ' | |
147 | ||
13e0b0d3 | 148 | test_expect_success 'bogus offset inside v2 extended table' ' |
a1283866 JK |
149 | # We need two objects here, so we can plausibly require |
150 | # an extended table (if the first object were larger than 2^31). | |
7c2115aa JK |
151 | # |
152 | # Note that the value is important here. We want $object as | |
a30f93b1 | 153 | # the second entry in sorted-hash order. The hash of this object starts |
7c2115aa JK |
154 | # with "000", which sorts before that of $object (which starts |
155 | # with "fff"). | |
a30f93b1 | 156 | second=$(test_oid oid000 | git hash-object -w --stdin) && |
7c2115aa | 157 | do_pack "$object $second" --index-version=2 && |
a1283866 JK |
158 | |
159 | # We have to make extra room for the table, so we cannot | |
160 | # just munge in place as usual. | |
161 | { | |
162 | dd if=$idx bs=1 count=$(($(ofs_table 2) + 4)) && | |
163 | printf "\200\0\0\0" && | |
164 | printf "\377\0\0\0\0\0\0\0" && | |
165 | dd if=$idx bs=1 skip=$(extended_table 2) | |
166 | } >tmp && | |
167 | mv tmp "$idx" && | |
168 | clear_base && | |
169 | test_must_fail git cat-file blob $object && | |
170 | test_must_fail git index-pack --verify $pack | |
171 | ' | |
172 | ||
173 | test_expect_success 'bogus OFS_DELTA in packfile' ' | |
174 | # Generate a pack with a delta in it. | |
c680668d NTND |
175 | base=$(test-tool genrandom foo 3000 | git hash-object --stdin -w) && |
176 | delta=$(test-tool genrandom foo 2000 | git hash-object --stdin -w) && | |
a1283866 JK |
177 | do_pack "$base $delta" --delta-base-offset && |
178 | rm -f .git/objects/??/* && | |
179 | ||
180 | # Double check that we have the delta we expect. | |
181 | echo $base >expect && | |
182 | echo $delta | git cat-file --batch-check="%(deltabase)" >actual && | |
183 | test_cmp expect actual && | |
184 | ||
185 | # Now corrupt it. We assume the varint size for the delta is small | |
186 | # enough to fit in the first byte (which it should be, since it | |
187 | # is a pure deletion from the base), and that original ofs_delta | |
188 | # takes 2 bytes (which it should, as it should be ~3000). | |
189 | ofs=$(git show-index <$idx | grep $delta | cut -d" " -f1) && | |
190 | munge $pack $(($ofs + 1)) "\177\377" && | |
191 | test_must_fail git cat-file blob $delta >/dev/null | |
192 | ' | |
193 | ||
194 | test_done |