1#include "cache.h"2#include "pack.h"3#include "pack-revindex.h"45struct idx_entry6{7off_t offset;8const unsigned char *sha1;9unsigned int nr;10};1112static int compare_entries(const void *e1, const void *e2)13{14const struct idx_entry *entry1 = e1;15const struct idx_entry *entry2 = e2;16if (entry1->offset < entry2->offset)17return -1;18if (entry1->offset > entry2->offset)19return 1;20return 0;21}2223int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,24off_t offset, off_t len, unsigned int nr)25{26const uint32_t *index_crc;27uint32_t data_crc = crc32(0, Z_NULL, 0);2829do {30unsigned int avail;31void *data = use_pack(p, w_curs, offset, &avail);32if (avail > len)33avail = len;34data_crc = crc32(data_crc, data, avail);35offset += avail;36len -= avail;37} while (len);3839index_crc = p->index_data;40index_crc += 2 + 256 + p->num_objects * (20/4) + nr;4142return data_crc != ntohl(*index_crc);43}4445static int verify_packfile(struct packed_git *p,46struct pack_window **w_curs)47{48off_t index_size = p->index_size;49const unsigned char *index_base = p->index_data;50git_SHA_CTX ctx;51unsigned char sha1[20], *pack_sig;52off_t offset = 0, pack_sig_ofs = 0;53uint32_t nr_objects, i;54int err = 0;55struct idx_entry *entries;5657/* Note that the pack header checks are actually performed by58* use_pack when it first opens the pack file. If anything59* goes wrong during those checks then the call will die out60* immediately.61*/6263git_SHA1_Init(&ctx);64do {65unsigned int remaining;66unsigned char *in = use_pack(p, w_curs, offset, &remaining);67offset += remaining;68if (!pack_sig_ofs)69pack_sig_ofs = p->pack_size - 20;70if (offset > pack_sig_ofs)71remaining -= (unsigned int)(offset - pack_sig_ofs);72git_SHA1_Update(&ctx, in, remaining);73} while (offset < pack_sig_ofs);74git_SHA1_Final(sha1, &ctx);75pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);76if (hashcmp(sha1, pack_sig))77err = error("%s SHA1 checksum mismatch",78p->pack_name);79if (hashcmp(index_base + index_size - 40, pack_sig))80err = error("%s SHA1 does not match its inddex",81p->pack_name);82unuse_pack(w_curs);8384/* Make sure everything reachable from idx is valid. Since we85* have verified that nr_objects matches between idx and pack,86* we do not do scan-streaming check on the pack file.87*/88nr_objects = p->num_objects;89entries = xmalloc((nr_objects + 1) * sizeof(*entries));90entries[nr_objects].offset = pack_sig_ofs;91/* first sort entries by pack offset, since unpacking them is more efficient that way */92for (i = 0; i < nr_objects; i++) {93entries[i].sha1 = nth_packed_object_sha1(p, i);94if (!entries[i].sha1)95die("internal error pack-check nth-packed-object");96entries[i].offset = nth_packed_object_offset(p, i);97entries[i].nr = i;98}99qsort(entries, nr_objects, sizeof(*entries), compare_entries);100101for (i = 0; i < nr_objects; i++) {102void *data;103enum object_type type;104unsigned long size;105106if (p->index_version > 1) {107off_t offset = entries[i].offset;108off_t len = entries[i+1].offset - offset;109unsigned int nr = entries[i].nr;110if (check_pack_crc(p, w_curs, offset, len, nr))111err = error("index CRC mismatch for object %s "112"from %s at offset %"PRIuMAX"",113sha1_to_hex(entries[i].sha1),114p->pack_name, (uintmax_t)offset);115}116data = unpack_entry(p, entries[i].offset, &type, &size);117if (!data) {118err = error("cannot unpack %s from %s at offset %"PRIuMAX"",119sha1_to_hex(entries[i].sha1), p->pack_name,120(uintmax_t)entries[i].offset);121break;122}123if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {124err = error("packed %s from %s is corrupt",125sha1_to_hex(entries[i].sha1), p->pack_name);126free(data);127break;128}129free(data);130}131free(entries);132133return err;134}135136int verify_pack_index(struct packed_git *p)137{138off_t index_size;139const unsigned char *index_base;140git_SHA_CTX ctx;141unsigned char sha1[20];142int err = 0;143144if (open_pack_index(p))145return error("packfile %s index not opened", p->pack_name);146index_size = p->index_size;147index_base = p->index_data;148149/* Verify SHA1 sum of the index file */150git_SHA1_Init(&ctx);151git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));152git_SHA1_Final(sha1, &ctx);153if (hashcmp(sha1, index_base + index_size - 20))154err = error("Packfile index for %s SHA1 mismatch",155p->pack_name);156return err;157}158159int verify_pack(struct packed_git *p)160{161int err = 0;162struct pack_window *w_curs = NULL;163164err |= verify_pack_index(p);165if (!p->index_data)166return -1;167168err |= verify_packfile(p, &w_curs);169unuse_pack(&w_curs);170171return err;172}