make got-index-pack compute and verify the pack file's SHA1 checksum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
diff --git a/lib/got_lib_inflate.h b/lib/got_lib_inflate.h
index 48821e9..0ccf42c 100644
--- a/lib/got_lib_inflate.h
+++ b/lib/got_lib_inflate.h
@@ -14,6 +14,14 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+struct got_inflate_checksum {
+ /* If not NULL, mix input bytes into this CRC checksum. */
+ uint32_t *input_crc;
+
+ /* If not NULL, mix input bytes into this SHA1 context. */
+ SHA1_CTX *input_sha1;
+};
+
struct got_inflate_buf {
z_stream z;
char *inbuf;
@@ -23,13 +31,13 @@ struct got_inflate_buf {
int flags;
#define GOT_INFLATE_F_HAVE_MORE 0x01
#define GOT_INFLATE_F_OWN_OUTBUF 0x02
- uint32_t *input_crc;
+ struct got_inflate_checksum *csum;
};
#define GOT_INFLATE_BUFSIZE 32768
const struct got_error *got_inflate_init(struct got_inflate_buf *, uint8_t *,
- size_t, uint32_t *);
+ size_t, struct got_inflate_checksum *);
const struct got_error *got_inflate_read(struct got_inflate_buf *, FILE *,
size_t *, size_t *);
const struct got_error *got_inflate_read_fd(struct got_inflate_buf *, int,
@@ -40,12 +48,12 @@ void got_inflate_end(struct got_inflate_buf *);
const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, size_t *,
FILE *);
const struct got_error *got_inflate_to_mem_fd(uint8_t **, size_t *, size_t *,
- uint32_t *, size_t, int);
+ struct got_inflate_checksum *, size_t, int);
const struct got_error *got_inflate_to_mem_mmap(uint8_t **, size_t *, size_t *,
- uint32_t *, uint8_t *, size_t, size_t);
+ struct got_inflate_checksum *, uint8_t *, size_t, size_t);
const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *);
-const struct got_error *got_inflate_to_file_fd(size_t *, size_t *, uint32_t *,
- int, FILE *);
+const struct got_error *got_inflate_to_file_fd(size_t *, size_t *,
+ struct got_inflate_checksum *, int, FILE *);
const struct got_error *got_inflate_to_fd(size_t *, FILE *, int);
const struct got_error *got_inflate_to_file_mmap(size_t *, size_t *,
- uint32_t *, uint8_t *, size_t, size_t, FILE *);
+ struct got_inflate_checksum *, uint8_t *, size_t, size_t, FILE *);
diff --git a/lib/inflate.c b/lib/inflate.c
index 50d5e77..fbef58a 100644
--- a/lib/inflate.c
+++ b/lib/inflate.c
@@ -36,7 +36,7 @@
const struct got_error *
got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
- uint32_t *input_crc)
+ struct got_inflate_checksum *csum)
{
const struct got_error *err = NULL;
int zerr;
@@ -75,13 +75,23 @@ got_inflate_init(struct got_inflate_buf *zb, uint8_t *outbuf, size_t bufsize,
} else
zb->outbuf = outbuf;
- zb->input_crc = input_crc;
+ zb->csum = csum;
done:
if (err)
got_inflate_end(zb);
return err;
}
+static void
+csum_input(struct got_inflate_checksum *csum, const char *buf, size_t len)
+{
+ if (csum->input_crc)
+ *csum->input_crc = crc32(*csum->input_crc, buf, len);
+
+ if (csum->input_sha1)
+ SHA1Update(csum->input_sha1, buf, len);
+}
+
const struct got_error *
got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
size_t *consumed)
@@ -98,8 +108,8 @@ got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
if (consumed)
*consumed = 0;
do {
- char *crc_in = NULL;
- size_t crc_avail = 0;
+ char *csum_in = NULL;
+ size_t csum_avail = 0;
if (z->avail_in == 0) {
size_t n = fread(zb->inbuf, 1, zb->inlen, f);
@@ -113,15 +123,13 @@ got_inflate_read(struct got_inflate_buf *zb, FILE *f, size_t *outlenp,
z->next_in = zb->inbuf;
z->avail_in = n;
}
- if (zb->input_crc) {
- crc_in = z->next_in;
- crc_avail = z->avail_in;
+ if (zb->csum) {
+ csum_in = z->next_in;
+ csum_avail = z->avail_in;
}
ret = inflate(z, Z_SYNC_FLUSH);
- if (zb->input_crc) {
- *zb->input_crc = crc32(*zb->input_crc,
- crc_in, crc_avail - z->avail_in);
- }
+ if (zb->csum)
+ csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
} while (ret == Z_OK && z->avail_out > 0);
if (ret == Z_OK || ret == Z_BUF_ERROR) {
@@ -154,8 +162,8 @@ got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
if (consumed)
*consumed = 0;
do {
- char *crc_in = NULL;
- size_t crc_avail = 0;
+ char *csum_in = NULL;
+ size_t csum_avail = 0;
if (z->avail_in == 0) {
ssize_t n = read(fd, zb->inbuf, zb->inlen);
@@ -169,15 +177,13 @@ got_inflate_read_fd(struct got_inflate_buf *zb, int fd, size_t *outlenp,
z->next_in = zb->inbuf;
z->avail_in = n;
}
- if (zb->input_crc) {
- crc_in = z->next_in;
- crc_avail = z->avail_in;
+ if (zb->csum) {
+ csum_in = z->next_in;
+ csum_avail = z->avail_in;
}
ret = inflate(z, Z_SYNC_FLUSH);
- if (zb->input_crc) {
- *zb->input_crc = crc32(*zb->input_crc,
- crc_in, crc_avail - z->avail_in);
- }
+ if (zb->csum)
+ csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
} while (ret == Z_OK && z->avail_out > 0);
if (ret == Z_OK || ret == Z_BUF_ERROR) {
@@ -209,8 +215,8 @@ got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
*consumed = 0;
do {
- char *crc_in = NULL;
- size_t crc_avail = 0;
+ char *csum_in = NULL;
+ size_t csum_avail = 0;
size_t last_total_in = zb->z.total_in;
if (z->avail_in == 0) {
@@ -222,15 +228,13 @@ got_inflate_read_mmap(struct got_inflate_buf *zb, uint8_t *map, size_t offset,
z->next_in = map + offset + *consumed;
z->avail_in = len - *consumed;
}
- if (zb->input_crc) {
- crc_in = z->next_in;
- crc_avail = z->avail_in;
+ if (zb->csum) {
+ csum_in = z->next_in;
+ csum_avail = z->avail_in;
}
ret = inflate(z, Z_SYNC_FLUSH);
- if (zb->input_crc) {
- *zb->input_crc = crc32(*zb->input_crc,
- crc_in, crc_avail - z->avail_in);
- }
+ if (zb->csum)
+ csum_input(zb->csum, csum_in, csum_avail - z->avail_in);
*consumed += z->total_in - last_total_in;
} while (ret == Z_OK && z->avail_out > 0);
@@ -311,7 +315,8 @@ done:
const struct got_error *
got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
- size_t *consumed_total, uint32_t *input_crc, size_t expected_size, int infd)
+ size_t *consumed_total, struct got_inflate_checksum *csum,
+ size_t expected_size, int infd)
{
const struct got_error *err;
size_t avail, consumed;
@@ -328,10 +333,9 @@ got_inflate_to_mem_fd(uint8_t **outbuf, size_t *outlen,
*outbuf = malloc(bufsize);
if (*outbuf == NULL)
return got_error_from_errno("malloc");
- err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
- input_crc);
+ err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
} else
- err = got_inflate_init(&zb, NULL, bufsize, input_crc);
+ err = got_inflate_init(&zb, NULL, bufsize, csum);
if (err)
goto done;
@@ -371,8 +375,8 @@ done:
const struct got_error *
got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
- size_t *consumed_total, uint32_t *input_crc, uint8_t *map, size_t offset,
- size_t len)
+ size_t *consumed_total, struct got_inflate_checksum *csum, uint8_t *map,
+ size_t offset, size_t len)
{
const struct got_error *err;
size_t avail, consumed;
@@ -384,16 +388,14 @@ got_inflate_to_mem_mmap(uint8_t **outbuf, size_t *outlen,
*outbuf = malloc(GOT_INFLATE_BUFSIZE);
if (*outbuf == NULL)
return got_error_from_errno("malloc");
- err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE,
- input_crc);
+ err = got_inflate_init(&zb, *outbuf, GOT_INFLATE_BUFSIZE, csum);
if (err) {
free(*outbuf);
*outbuf = NULL;
return err;
}
} else {
- err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE,
- input_crc);
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
}
*outlen = 0;
@@ -507,13 +509,13 @@ done:
const struct got_error *
got_inflate_to_file_fd(size_t *outlen, size_t *consumed_total,
- uint32_t *input_crc, int infd, FILE *outfile)
+ struct got_inflate_checksum *csum, int infd, FILE *outfile)
{
const struct got_error *err;
size_t avail, consumed;
struct got_inflate_buf zb;
- err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
if (err)
goto done;
@@ -546,14 +548,14 @@ done:
const struct got_error *
got_inflate_to_file_mmap(size_t *outlen, size_t *consumed_total,
- uint32_t *input_crc, uint8_t *map, size_t offset, size_t len,
- FILE *outfile)
+ struct got_inflate_checksum *csum, uint8_t *map, size_t offset,
+ size_t len, FILE *outfile)
{
const struct got_error *err;
size_t avail, consumed;
struct got_inflate_buf zb;
- err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, input_crc);
+ err = got_inflate_init(&zb, NULL, GOT_INFLATE_BUFSIZE, csum);
if (err)
goto done;
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index bfc9107..90e1671 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -130,7 +130,7 @@ get_obj_type_label(const char **label, int obj_type)
}
static const struct got_error *
-read_crc(uint32_t *crc, int fd, size_t len)
+read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
{
uint8_t buf[8192];
size_t n;
@@ -142,7 +142,10 @@ read_crc(uint32_t *crc, int fd, size_t len)
return got_error_from_errno("read");
if (r == 0)
break;
- *crc = crc32(*crc, buf, r);
+ if (crc)
+ *crc = crc32(*crc, buf, r);
+ if (sha1_ctx)
+ SHA1Update(sha1_ctx, buf, r);
}
return NULL;
@@ -169,7 +172,7 @@ read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
static const struct got_error *
read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
- FILE *tmpfile)
+ FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
{
const struct got_error *err = NULL;
SHA1_CTX ctx;
@@ -180,6 +183,10 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
size_t headerlen;
const char *obj_label;
size_t mapoff = obj->off;
+ struct got_inflate_checksum csum;
+
+ csum.input_sha1 = pack_sha1_ctx;
+ csum.input_crc = &obj->crc;
err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
&obj->tslen, pack, obj->off);
@@ -188,12 +195,14 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
if (pack->map) {
obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
+ SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
mapoff += obj->tslen;
} else {
/* XXX Seek back and get the CRC of on-disk type+size bytes. */
if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
return got_error_from_errno("lseek");
- err = read_crc(&obj->crc, pack->fd, obj->tslen);
+ err = read_checksum(&obj->crc, pack_sha1_ctx,
+ pack->fd, obj->tslen);
if (err)
return err;
}
@@ -210,20 +219,20 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
}
if (pack->map) {
err = got_inflate_to_file_mmap(&datalen,
- &obj->len, &obj->crc, pack->map, mapoff,
+ &obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff, tmpfile);
} else {
err = got_inflate_to_file_fd(&datalen,
- &obj->len, &obj->crc, pack->fd, tmpfile);
+ &obj->len, &csum, pack->fd, tmpfile);
}
} else {
if (pack->map) {
err = got_inflate_to_mem_mmap(&data, &datalen,
- &obj->len, &obj->crc, pack->map, mapoff,
+ &obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
} else {
err = got_inflate_to_mem_fd(&data, &datalen,
- &obj->len, &obj->crc, obj->size, pack->fd);
+ &obj->len, &csum, obj->size, pack->fd);
}
}
if (err)
@@ -262,9 +271,11 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
SHA1_DIGEST_LENGTH);
obj->crc = crc32(obj->crc, pack->map + mapoff,
SHA1_DIGEST_LENGTH);
+ SHA1Update(pack_sha1_ctx, pack->map + mapoff,
+ SHA1_DIGEST_LENGTH);
mapoff += SHA1_DIGEST_LENGTH;
err = got_inflate_to_mem_mmap(NULL, &datalen,
- &obj->len, &obj->crc, pack->map, mapoff,
+ &obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
if (err)
break;
@@ -281,8 +292,10 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
}
obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
SHA1_DIGEST_LENGTH);
+ SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
+ SHA1_DIGEST_LENGTH);
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
- &obj->crc, obj->size, pack->fd);
+ &csum, obj->size, pack->fd);
if (err)
break;
}
@@ -299,15 +312,17 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
if (pack->map) {
obj->crc = crc32(obj->crc, pack->map + mapoff,
obj->delta.ofs.base_offsetlen);
+ SHA1Update(pack_sha1_ctx, pack->map + mapoff,
+ obj->delta.ofs.base_offsetlen);
mapoff += obj->delta.ofs.base_offsetlen;
err = got_inflate_to_mem_mmap(NULL, &datalen,
- &obj->len, &obj->crc, pack->map, mapoff,
+ &obj->len, &csum, pack->map, mapoff,
pack->filesize - mapoff);
if (err)
break;
} else {
/*
- * XXX Seek back and get the CRC of on-disk
+ * XXX Seek back and get CRC and SHA1 of on-disk
* offset bytes.
*/
if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
@@ -315,13 +330,13 @@ read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
err = got_error_from_errno("lseek");
break;
}
- err = read_crc(&obj->crc, pack->fd,
- obj->delta.ofs.base_offsetlen);
+ err = read_checksum(&obj->crc, pack_sha1_ctx,
+ pack->fd, obj->delta.ofs.base_offsetlen);
if (err)
break;
err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
- &obj->crc, obj->size, pack->fd);
+ &csum, obj->size, pack->fd);
if (err)
break;
}
@@ -607,6 +622,8 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
struct got_packfile_hdr hdr;
struct got_packidx packidx;
char buf[8];
+ char pack_sha1[SHA1_DIGEST_LENGTH];
+ char pack_sha1_expected[SHA1_DIGEST_LENGTH];
int nobj, nvalid, nloose, nresolved = 0, i;
struct got_indexed_object *objects = NULL, *obj;
SHA1_CTX ctx;
@@ -617,11 +634,12 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
int p_indexed = 0, last_p_indexed = -1;
int p_resolved = 0, last_p_resolved = -1;
- /* Check pack file header. */
+ /* Require that pack file header and SHA1 trailer are present. */
+ if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
+ return got_error_msg(GOT_ERR_BAD_PACKFILE,
+ "short pack file");
+
if (pack->map) {
- if (pack->filesize < sizeof(hdr))
- return got_error_msg(GOT_ERR_BAD_PACKFILE,
- "short packfile header");
memcpy(&hdr, pack->map, sizeof(hdr));
mapoff += sizeof(hdr);
} else {
@@ -630,7 +648,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
return got_error_from_errno("read");
if (r < sizeof(hdr))
return got_error_msg(GOT_ERR_BAD_PACKFILE,
- "short packfile header");
+ "short pack file");
}
if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
@@ -644,6 +662,10 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
return got_error_msg(GOT_ERR_BAD_PACKFILE,
"bad packfile with zero objects");
+ /* We compute the SHA1 of pack file contents and verify later on. */
+ SHA1Init(&ctx);
+ SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
+
/*
* Create an in-memory pack index which will grow as objects
* IDs in the pack file are discovered. Only fields used to
@@ -732,7 +754,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
}
}
- err = read_packed_object(pack, obj, tmpfile);
+ err = read_packed_object(pack, obj, tmpfile, &ctx);
if (err)
goto done;
@@ -761,6 +783,37 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
}
nvalid = nloose;
+ /*
+ * Having done a full pass over the pack file and can now
+ * verify its checksum.
+ */
+ SHA1Final(pack_sha1, &ctx);
+ if (pack->map) {
+ memcpy(pack_sha1_expected, pack->map +
+ pack->filesize - SHA1_DIGEST_LENGTH,
+ SHA1_DIGEST_LENGTH);
+ } else {
+ ssize_t n;
+ if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
+ err = got_error_from_errno("lseek");
+ goto done;
+ }
+ n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
+ if (n == -1) {
+ err = got_error_from_errno("read");
+ goto done;
+ }
+ if (n != SHA1_DIGEST_LENGTH) {
+ err = got_error(GOT_ERR_IO);
+ goto done;
+ }
+ }
+ if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
+ err = got_error_msg(GOT_ERR_BAD_PACKFILE,
+ "pack file checksum mismatch");
+ goto done;
+ }
+
if (first_delta_idx == -1)
first_delta_idx = 0;