pack-objects: re-validate data we copy from elsewhere.
When reusing data from an existing pack and from a new style loose objects, we used to just copy it staight into the resulting pack. Instead make sure they are not corrupt, but do so only when we are not streaming to stdout, in which case the receiving end will do the validation either by unpacking the stream or by constructing the .idx file. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 1, 2006 at 15:05 UTC
df6d61017a17efe67e4709028fea8e820b5efc5e
1 file changed
+63
-4
builtin-pack-objects.c
+63
-4
@@ -65,6 +65,7 @@ static unsigned char pack_file_sha1[20];
65
static int progress = 1;
66
static volatile sig_atomic_t progress_update;
67
static int window = 10;
68
+static int pack_to_stdout;
69
70
/*
71
* The object names in objects array are hashed with this hashtable,
@@ -242,6 +243,58 @@ static int encode_header(enum object_type type, unsigned long size, unsigned cha
243
return n;
244
}
245
246
+static int revalidate_one(struct object_entry *entry,
247
+ void *data, char *type, unsigned long size)
248
+{
249
+ int err;
250
+ if (!data)
251
+ return -1;
252
+ if (size != entry->size)
253
+ return -1;
254
+ err = check_sha1_signature(entry->sha1, data, size,
255
+ type_names[entry->type]);
256
+ free(data);
257
+ return err;
258
+}
259
+
260
+/*
261
+ * we are going to reuse the existing pack entry data. make
262
+ * sure it is not corrupt.
263
+ */
264
+static int revalidate_pack_entry(struct object_entry *entry)
265
+{
266
+ void *data;
267
+ char type[20];
268
+ unsigned long size;
269
+ struct pack_entry e;
270
+
271
+ if (pack_to_stdout)
272
+ return 0;
273
+
274
+ e.p = entry->in_pack;
275
+ e.offset = entry->in_pack_offset;
276
+
277
+ /* the caller has already called use_packed_git() for us */
278
+ data = unpack_entry_gently(&e, type, &size);
279
+ return revalidate_one(entry, data, type, size);
280
+}
281
+
282
+static int revalidate_loose_object(struct object_entry *entry,
283
+ unsigned char *map,
284
+ unsigned long mapsize)
285
+{
286
+ /* we already know this is a loose object with new type header. */
287
+ void *data;
288
+ char type[20];
289
+ unsigned long size;
290
+
291
+ if (pack_to_stdout)
292
+ return 0;
293
+
294
+ data = unpack_sha1_file(map, mapsize, type, &size);
295
+ return revalidate_one(entry, data, type, size);
296
+}
297
+
298
static unsigned long write_object(struct sha1file *f,
299
struct object_entry *entry)
300
{
@@ -276,6 +329,9 @@ static unsigned long write_object(struct sha1file *f,
329
map = map_sha1_file(entry->sha1, &mapsize);
330
if (map && !legacy_loose_object(map)) {
331
/* We can copy straight into the pack file */
332
+ if (revalidate_loose_object(entry, map, mapsize))
333
+ die("corrupt loose object %s",
334
+ sha1_to_hex(entry->sha1));
335
sha1write(f, map, mapsize);
336
munmap(map, mapsize);
337
written++;
@@ -286,7 +342,7 @@ static unsigned long write_object(struct sha1file *f,
342
munmap(map, mapsize);
343
}
344
289
- if (! to_reuse) {
345
+ if (!to_reuse) {
346
buf = read_sha1_file(entry->sha1, type, &size);
347
if (!buf)
348
die("unable to read %s", sha1_to_hex(entry->sha1));
@@ -319,6 +375,9 @@ static unsigned long write_object(struct sha1file *f,
375
376
datalen = find_packed_object_size(p, entry->in_pack_offset);
377
buf = (char *) p->pack_base + entry->in_pack_offset;
378
+
379
+ if (revalidate_pack_entry(entry))
380
+ die("corrupt delta in pack %s", sha1_to_hex(entry->sha1));
381
sha1write(f, buf, datalen);
382
unuse_packed_git(p);
383
hdrlen = 0; /* not really */
@@ -1163,7 +1222,7 @@ static void prepare_pack(int window, int depth)
1222
find_deltas(sorted_by_type, window+1, depth);
1223
}
1224
1166
-static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
1225
+static int reuse_cached_pack(unsigned char *sha1)
1226
{
1227
static const char cache[] = "pack-cache/pack-%s.%s";
1228
char *cached_pack, *cached_idx;
@@ -1247,7 +1306,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1306
{
1307
SHA_CTX ctx;
1308
char line[40 + 1 + PATH_MAX + 2];
1250
- int depth = 10, pack_to_stdout = 0;
1309
+ int depth = 10;
1310
struct object_entry **list;
1311
int num_preferred_base = 0;
1312
int i;
@@ -1367,7 +1426,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1426
if (progress && (nr_objects != nr_result))
1427
fprintf(stderr, "Result has %d objects.\n", nr_result);
1428
1370
- if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
1429
+ if (reuse_cached_pack(object_list_sha1))
1430
;
1431
else {
1432
if (nr_result)