@matheusbsbs / matheus-bagliano / commits / 9096c660a8
Fork 0

index-pack: more validation checks and cleanups

When appending objects to a pack, make sure the appended data is really what we expect instead of simply loading potentially corrupted objects and legitimating them by computing a SHA1 of that corrupt data. With this the sha1_object() can lose its test_for_collision parameter which is now redundent. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Mar 20, 2007 at 17:07 UTC 9096c660a85c4a3d30f8885c766c34ce0766e869
1 file changed +9 -9
index-pack.c
+9 -9
@@ -345,12 +345,10 @@ static int find_delta_children(const union delta_base *base,
345 }
346
347 static void sha1_object(const void *data, unsigned long size,
348 - enum object_type type, unsigned char *sha1,
349 - int test_for_collision)
348 + enum object_type type, unsigned char *sha1)
349 {
350 hash_sha1_file(data, size, typename(type), sha1);
352 -
353 - if (test_for_collision && has_sha1_file(sha1)) {
351 + if (has_sha1_file(sha1)) {
352 void *has_data;
353 enum object_type has_type;
354 unsigned long has_size;
@@ -381,7 +379,7 @@ static void resolve_delta(struct object_entry *delta_obj, void *base_data,
379 free(delta_data);
380 if (!result)
381 bad_object(delta_obj->offset, "failed to apply delta");
384 - sha1_object(result, result_size, type, delta_obj->sha1, 1);
382 + sha1_object(result, result_size, type, delta_obj->sha1);
383 nr_resolved_deltas++;
384
385 hashcpy(delta_base.sha1, delta_obj->sha1);
@@ -438,7 +436,7 @@ static void parse_pack_objects(unsigned char *sha1)
436 delta->obj_no = i;
437 delta++;
438 } else
441 - sha1_object(data, obj->size, obj->type, obj->sha1, 1);
439 + sha1_object(data, obj->size, obj->type, obj->sha1);
440 free(data);
441 if (verbose)
442 percent = display_progress(i+1, nr_objects, percent);
@@ -541,7 +539,7 @@ static int write_compressed(int fd, void *in, unsigned int size)
539 return size;
540 }
541
544 -static void append_obj_to_pack(void *buf,
542 +static void append_obj_to_pack(const unsigned char *sha1, void *buf,
543 unsigned long size, enum object_type type)
544 {
545 struct object_entry *obj = &objects[nr_objects++];
@@ -559,7 +557,7 @@ static void append_obj_to_pack(void *buf,
557 write_or_die(output_fd, header, n);
558 obj[1].offset = obj[0].offset + n;
559 obj[1].offset += write_compressed(output_fd, buf, size);
562 - sha1_object(buf, size, type, obj->sha1, 0);
560 + hashcpy(obj->sha1, sha1);
561 }
562
563 static int delta_pos_compare(const void *_a, const void *_b)
@@ -612,7 +610,9 @@ static void fix_unresolved_deltas(int nr_unresolved)
610 resolve_delta(child, data, size, type);
611 }
612
615 - append_obj_to_pack(data, size, type);
613 + if (check_sha1_signature(d->base.sha1, data, size, typename(type)))
614 + die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
615 + append_obj_to_pack(d->base.sha1, data, size, type);
616 free(data);
617 if (verbose)
618 percent = display_progress(nr_resolved_deltas,