blob: 90c33b1b84ef793bf017927026863de41e31fa2c [file] [log] [blame]
Junio C Hamanof3bf9222005-06-30 17:15:39 -07001#include "cache.h"
2#include "pack.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -05003#include "pack-revindex.h"
Junio C Hamanof3bf9222005-06-30 17:15:39 -07004
Alexandre Julliard3af51922007-06-03 20:21:41 +02005struct idx_entry
6{
Alexandre Julliard3af51922007-06-03 20:21:41 +02007 off_t offset;
Nicolas Pitrec41a4a92008-06-24 23:19:02 -04008 const unsigned char *sha1;
9 unsigned int nr;
Alexandre Julliard3af51922007-06-03 20:21:41 +020010};
11
12static int compare_entries(const void *e1, const void *e2)
13{
14 const struct idx_entry *entry1 = e1;
15 const struct idx_entry *entry2 = e2;
16 if (entry1->offset < entry2->offset)
17 return -1;
18 if (entry1->offset > entry2->offset)
19 return 1;
20 return 0;
21}
22
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040023int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
24 off_t offset, off_t len, unsigned int nr)
25{
26 const uint32_t *index_crc;
27 uint32_t data_crc = crc32(0, Z_NULL, 0);
28
29 do {
30 unsigned int avail;
31 void *data = use_pack(p, w_curs, offset, &avail);
32 if (avail > len)
33 avail = len;
34 data_crc = crc32(data_crc, data, avail);
35 offset += avail;
36 len -= avail;
37 } while (len);
38
39 index_crc = p->index_data;
40 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
41
42 return data_crc != ntohl(*index_crc);
43}
44
Shawn O. Pearce03e79c82006-12-23 02:34:08 -050045static int verify_packfile(struct packed_git *p,
46 struct pack_window **w_curs)
Junio C Hamanof3bf9222005-06-30 17:15:39 -070047{
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050048 off_t index_size = p->index_size;
Nicolas Pitre42873072007-03-16 16:42:50 -040049 const unsigned char *index_base = p->index_data;
Nicolas Pitre9126f002008-10-01 14:05:20 -040050 git_SHA_CTX ctx;
Nicolas Pitre62413602008-05-29 17:34:50 -040051 unsigned char sha1[20], *pack_sig;
52 off_t offset = 0, pack_sig_ofs = p->pack_size - 20;
Shawn O. Pearce326bf392007-03-06 20:44:19 -050053 uint32_t nr_objects, i;
Nicolas Pitre62413602008-05-29 17:34:50 -040054 int err = 0;
Alexandre Julliard3af51922007-06-03 20:21:41 +020055 struct idx_entry *entries;
Junio C Hamanof3bf9222005-06-30 17:15:39 -070056
Shawn O. Pearce079afb12006-12-23 02:34:13 -050057 /* Note that the pack header checks are actually performed by
58 * use_pack when it first opens the pack file. If anything
59 * goes wrong during those checks then the call will die out
60 * immediately.
61 */
Junio C Hamanof3bf9222005-06-30 17:15:39 -070062
Nicolas Pitre9126f002008-10-01 14:05:20 -040063 git_SHA1_Init(&ctx);
Nicolas Pitre62413602008-05-29 17:34:50 -040064 while (offset < pack_sig_ofs) {
Shawn O. Pearce079afb12006-12-23 02:34:13 -050065 unsigned int remaining;
66 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
67 offset += remaining;
Nicolas Pitre62413602008-05-29 17:34:50 -040068 if (offset > pack_sig_ofs)
69 remaining -= (unsigned int)(offset - pack_sig_ofs);
Nicolas Pitre9126f002008-10-01 14:05:20 -040070 git_SHA1_Update(&ctx, in, remaining);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050071 }
Nicolas Pitre9126f002008-10-01 14:05:20 -040072 git_SHA1_Final(sha1, &ctx);
Nicolas Pitre62413602008-05-29 17:34:50 -040073 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
74 if (hashcmp(sha1, pack_sig))
75 err = error("%s SHA1 checksum mismatch",
76 p->pack_name);
77 if (hashcmp(index_base + index_size - 40, pack_sig))
78 err = error("%s SHA1 does not match its inddex",
79 p->pack_name);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050080 unuse_pack(w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070081
82 /* Make sure everything reachable from idx is valid. Since we
83 * have verified that nr_objects matches between idx and pack,
84 * we do not do scan-streaming check on the pack file.
85 */
Nicolas Pitre57059092007-04-09 01:06:28 -040086 nr_objects = p->num_objects;
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040087 entries = xmalloc((nr_objects + 1) * sizeof(*entries));
88 entries[nr_objects].offset = pack_sig_ofs;
Alexandre Julliard3af51922007-06-03 20:21:41 +020089 /* first sort entries by pack offset, since unpacking them is more efficient that way */
90 for (i = 0; i < nr_objects; i++) {
91 entries[i].sha1 = nth_packed_object_sha1(p, i);
92 if (!entries[i].sha1)
93 die("internal error pack-check nth-packed-object");
Nicolas Pitre99093232008-06-24 23:17:12 -040094 entries[i].offset = nth_packed_object_offset(p, i);
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040095 entries[i].nr = i;
Alexandre Julliard3af51922007-06-03 20:21:41 +020096 }
97 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
98
Nicolas Pitre62413602008-05-29 17:34:50 -040099 for (i = 0; i < nr_objects; i++) {
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700100 void *data;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500101 enum object_type type;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500102 unsigned long size;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700103
Nicolas Pitrec41a4a92008-06-24 23:19:02 -0400104 if (p->index_version > 1) {
105 off_t offset = entries[i].offset;
106 off_t len = entries[i+1].offset - offset;
107 unsigned int nr = entries[i].nr;
108 if (check_pack_crc(p, w_curs, offset, len, nr))
109 err = error("index CRC mismatch for object %s "
110 "from %s at offset %"PRIuMAX"",
111 sha1_to_hex(entries[i].sha1),
112 p->pack_name, (uintmax_t)offset);
113 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200114 data = unpack_entry(p, entries[i].offset, &type, &size);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700115 if (!data) {
Nicolas Pitre62413602008-05-29 17:34:50 -0400116 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
117 sha1_to_hex(entries[i].sha1), p->pack_name,
118 (uintmax_t)entries[i].offset);
119 break;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700120 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200121 if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
Junio C Hamano1038f0c2005-07-07 15:12:20 -0700122 err = error("packed %s from %s is corrupt",
Alexandre Julliard3af51922007-06-03 20:21:41 +0200123 sha1_to_hex(entries[i].sha1), p->pack_name);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700124 free(data);
Nicolas Pitre62413602008-05-29 17:34:50 -0400125 break;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700126 }
127 free(data);
128 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200129 free(entries);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700130
131 return err;
132}
133
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400134int verify_pack(struct packed_git *p)
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700135{
Shawn O. Pearced0798372007-05-26 01:24:19 -0400136 off_t index_size;
137 const unsigned char *index_base;
Nicolas Pitre9126f002008-10-01 14:05:20 -0400138 git_SHA_CTX ctx;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700139 unsigned char sha1[20];
Nicolas Pitre62413602008-05-29 17:34:50 -0400140 int err = 0;
141 struct pack_window *w_curs = NULL;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700142
Shawn O. Pearced0798372007-05-26 01:24:19 -0400143 if (open_pack_index(p))
144 return error("packfile %s index not opened", p->pack_name);
145 index_size = p->index_size;
146 index_base = p->index_data;
147
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700148 /* Verify SHA1 sum of the index file */
Nicolas Pitre9126f002008-10-01 14:05:20 -0400149 git_SHA1_Init(&ctx);
150 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
151 git_SHA1_Final(sha1, &ctx);
Nicolas Pitre42873072007-03-16 16:42:50 -0400152 if (hashcmp(sha1, index_base + index_size - 20))
Nicolas Pitre62413602008-05-29 17:34:50 -0400153 err = error("Packfile index for %s SHA1 mismatch",
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700154 p->pack_name);
155
Nicolas Pitre62413602008-05-29 17:34:50 -0400156 /* Verify pack file */
157 err |= verify_packfile(p, &w_curs);
158 unuse_pack(&w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700159
Nicolas Pitre62413602008-05-29 17:34:50 -0400160 return err;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700161}