Coverage Report

Created: 2024-09-08 06:23

/src/git/pack-check.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "environment.h"
5
#include "hex.h"
6
#include "repository.h"
7
#include "pack.h"
8
#include "progress.h"
9
#include "packfile.h"
10
#include "object-file.h"
11
#include "object-store-ll.h"
12
13
struct idx_entry {
14
  off_t                offset;
15
  unsigned int nr;
16
};
17
18
static int compare_entries(const void *e1, const void *e2)
19
0
{
20
0
  const struct idx_entry *entry1 = e1;
21
0
  const struct idx_entry *entry2 = e2;
22
0
  if (entry1->offset < entry2->offset)
23
0
    return -1;
24
0
  if (entry1->offset > entry2->offset)
25
0
    return 1;
26
0
  return 0;
27
0
}
28
29
int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
30
       off_t offset, off_t len, unsigned int nr)
31
0
{
32
0
  const uint32_t *index_crc;
33
0
  uint32_t data_crc = crc32(0, NULL, 0);
34
35
0
  do {
36
0
    unsigned long avail;
37
0
    void *data = use_pack(p, w_curs, offset, &avail);
38
0
    if (avail > len)
39
0
      avail = len;
40
0
    data_crc = crc32(data_crc, data, avail);
41
0
    offset += avail;
42
0
    len -= avail;
43
0
  } while (len);
44
45
0
  index_crc = p->index_data;
46
0
  index_crc += 2 + 256 + (size_t)p->num_objects * (the_hash_algo->rawsz/4) + nr;
47
48
0
  return data_crc != ntohl(*index_crc);
49
0
}
50
51
static int verify_packfile(struct repository *r,
52
         struct packed_git *p,
53
         struct pack_window **w_curs,
54
         verify_fn fn,
55
         struct progress *progress, uint32_t base_count)
56
57
0
{
58
0
  off_t index_size = p->index_size;
59
0
  const unsigned char *index_base = p->index_data;
60
0
  git_hash_ctx ctx;
61
0
  unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
62
0
  off_t offset = 0, pack_sig_ofs = 0;
63
0
  uint32_t nr_objects, i;
64
0
  int err = 0;
65
0
  struct idx_entry *entries;
66
67
0
  if (!is_pack_valid(p))
68
0
    return error("packfile %s cannot be accessed", p->pack_name);
69
70
0
  r->hash_algo->init_fn(&ctx);
71
0
  do {
72
0
    unsigned long remaining;
73
0
    unsigned char *in = use_pack(p, w_curs, offset, &remaining);
74
0
    offset += remaining;
75
0
    if (!pack_sig_ofs)
76
0
      pack_sig_ofs = p->pack_size - r->hash_algo->rawsz;
77
0
    if (offset > pack_sig_ofs)
78
0
      remaining -= (unsigned int)(offset - pack_sig_ofs);
79
0
    r->hash_algo->update_fn(&ctx, in, remaining);
80
0
  } while (offset < pack_sig_ofs);
81
0
  r->hash_algo->final_fn(hash, &ctx);
82
0
  pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
83
0
  if (!hasheq(hash, pack_sig, the_repository->hash_algo))
84
0
    err = error("%s pack checksum mismatch",
85
0
          p->pack_name);
86
0
  if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig,
87
0
        the_repository->hash_algo))
88
0
    err = error("%s pack checksum does not match its index",
89
0
          p->pack_name);
90
0
  unuse_pack(w_curs);
91
92
  /* Make sure everything reachable from idx is valid.  Since we
93
   * have verified that nr_objects matches between idx and pack,
94
   * we do not do scan-streaming check on the pack file.
95
   */
96
0
  nr_objects = p->num_objects;
97
0
  ALLOC_ARRAY(entries, nr_objects + 1);
98
0
  entries[nr_objects].offset = pack_sig_ofs;
99
  /* first sort entries by pack offset, since unpacking them is more efficient that way */
100
0
  for (i = 0; i < nr_objects; i++) {
101
0
    entries[i].offset = nth_packed_object_offset(p, i);
102
0
    entries[i].nr = i;
103
0
  }
104
0
  QSORT(entries, nr_objects, compare_entries);
105
106
0
  for (i = 0; i < nr_objects; i++) {
107
0
    void *data;
108
0
    struct object_id oid;
109
0
    enum object_type type;
110
0
    unsigned long size;
111
0
    off_t curpos;
112
0
    int data_valid;
113
114
0
    if (nth_packed_object_id(&oid, p, entries[i].nr) < 0)
115
0
      BUG("unable to get oid of object %lu from %s",
116
0
          (unsigned long)entries[i].nr, p->pack_name);
117
118
0
    if (p->index_version > 1) {
119
0
      off_t offset = entries[i].offset;
120
0
      off_t len = entries[i+1].offset - offset;
121
0
      unsigned int nr = entries[i].nr;
122
0
      if (check_pack_crc(p, w_curs, offset, len, nr))
123
0
        err = error("index CRC mismatch for object %s "
124
0
              "from %s at offset %"PRIuMAX"",
125
0
              oid_to_hex(&oid),
126
0
              p->pack_name, (uintmax_t)offset);
127
0
    }
128
129
0
    curpos = entries[i].offset;
130
0
    type = unpack_object_header(p, w_curs, &curpos, &size);
131
0
    unuse_pack(w_curs);
132
133
0
    if (type == OBJ_BLOB && big_file_threshold <= size) {
134
      /*
135
       * Let stream_object_signature() check it with
136
       * the streaming interface; no point slurping
137
       * the data in-core only to discard.
138
       */
139
0
      data = NULL;
140
0
      data_valid = 0;
141
0
    } else {
142
0
      data = unpack_entry(r, p, entries[i].offset, &type, &size);
143
0
      data_valid = 1;
144
0
    }
145
146
0
    if (data_valid && !data)
147
0
      err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
148
0
            oid_to_hex(&oid), p->pack_name,
149
0
            (uintmax_t)entries[i].offset);
150
0
    else if (data && check_object_signature(r, &oid, data, size,
151
0
              type) < 0)
152
0
      err = error("packed %s from %s is corrupt",
153
0
            oid_to_hex(&oid), p->pack_name);
154
0
    else if (!data && stream_object_signature(r, &oid) < 0)
155
0
      err = error("packed %s from %s is corrupt",
156
0
            oid_to_hex(&oid), p->pack_name);
157
0
    else if (fn) {
158
0
      int eaten = 0;
159
0
      err |= fn(&oid, type, size, data, &eaten);
160
0
      if (eaten)
161
0
        data = NULL;
162
0
    }
163
0
    if (((base_count + i) & 1023) == 0)
164
0
      display_progress(progress, base_count + i);
165
0
    free(data);
166
167
0
  }
168
0
  display_progress(progress, base_count + i);
169
0
  free(entries);
170
171
0
  return err;
172
0
}
173
174
int verify_pack_index(struct packed_git *p)
175
0
{
176
0
  int err = 0;
177
178
0
  if (open_pack_index(p))
179
0
    return error("packfile %s index not opened", p->pack_name);
180
181
  /* Verify SHA1 sum of the index file */
182
0
  if (!hashfile_checksum_valid(p->index_data, p->index_size))
183
0
    err = error("Packfile index for %s hash mismatch",
184
0
          p->pack_name);
185
0
  return err;
186
0
}
187
188
int verify_pack(struct repository *r, struct packed_git *p, verify_fn fn,
189
    struct progress *progress, uint32_t base_count)
190
0
{
191
0
  int err = 0;
192
0
  struct pack_window *w_curs = NULL;
193
194
0
  err |= verify_pack_index(p);
195
0
  if (!p->index_data)
196
0
    return -1;
197
198
0
  err |= verify_packfile(r, p, &w_curs, fn, progress, base_count);
199
0
  unuse_pack(&w_curs);
200
201
0
  return err;
202
0
}