1#include "cache.h"2#include "pack.h"3#include "pack-revindex.h"4#include "progress.h"56struct idx_entry {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, NULL, 0);2829do {30unsigned long 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,47verify_fn fn,48struct progress *progress, uint32_t base_count)4950{51off_t index_size = p->index_size;52const unsigned char *index_base = p->index_data;53git_SHA_CTX ctx;54unsigned char sha1[20], *pack_sig;55off_t offset = 0, pack_sig_ofs = 0;56uint32_t nr_objects, i;57int err = 0;58struct idx_entry *entries;5960if (!is_pack_valid(p))61return error("packfile %s cannot be accessed", p->pack_name);6263git_SHA1_Init(&ctx);64do {65unsigned long 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 index",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;89ALLOC_ARRAY(entries, nr_objects + 1);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, compare_entries);100101for (i = 0; i < nr_objects; i++) {102void *data;103enum object_type type;104unsigned long size;105off_t curpos;106int data_valid;107108if (p->index_version > 1) {109off_t offset = entries[i].offset;110off_t len = entries[i+1].offset - offset;111unsigned int nr = entries[i].nr;112if (check_pack_crc(p, w_curs, offset, len, nr))113err = error("index CRC mismatch for object %s "114"from %s at offset %"PRIuMAX"",115sha1_to_hex(entries[i].sha1),116p->pack_name, (uintmax_t)offset);117}118119curpos = entries[i].offset;120type = unpack_object_header(p, w_curs, &curpos, &size);121unuse_pack(w_curs);122123if (type == OBJ_BLOB && big_file_threshold <= size) {124/*125* Let check_sha1_signature() check it with126* the streaming interface; no point slurping127* the data in-core only to discard.128*/129data = NULL;130data_valid = 0;131} else {132data = unpack_entry(p, entries[i].offset, &type, &size);133data_valid = 1;134}135136if (data_valid && !data)137err = error("cannot unpack %s from %s at offset %"PRIuMAX"",138sha1_to_hex(entries[i].sha1), p->pack_name,139(uintmax_t)entries[i].offset);140else if (check_sha1_signature(entries[i].sha1, data, size, typename(type)))141err = error("packed %s from %s is corrupt",142sha1_to_hex(entries[i].sha1), p->pack_name);143else if (fn) {144int eaten = 0;145err |= fn(entries[i].sha1, type, size, data, &eaten);146if (eaten)147data = NULL;148}149if (((base_count + i) & 1023) == 0)150display_progress(progress, base_count + i);151free(data);152153}154display_progress(progress, base_count + i);155free(entries);156157return err;158}159160int verify_pack_index(struct packed_git *p)161{162off_t index_size;163const unsigned char *index_base;164git_SHA_CTX ctx;165unsigned char sha1[20];166int err = 0;167168if (open_pack_index(p))169return error("packfile %s index not opened", p->pack_name);170index_size = p->index_size;171index_base = p->index_data;172173/* Verify SHA1 sum of the index file */174git_SHA1_Init(&ctx);175git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));176git_SHA1_Final(sha1, &ctx);177if (hashcmp(sha1, index_base + index_size - 20))178err = error("Packfile index for %s SHA1 mismatch",179p->pack_name);180return err;181}182183int verify_pack(struct packed_git *p, verify_fn fn,184struct progress *progress, uint32_t base_count)185{186int err = 0;187struct pack_window *w_curs = NULL;188189err |= verify_pack_index(p);190if (!p->index_data)191return -1;192193err |= verify_packfile(p, &w_curs, fn, progress, base_count);194unuse_pack(&w_curs);195196return err;197}