add overflow tests on pack offset variables
Change a few size and offset variables to more appropriate type, then add overflow tests on those offsets. This prevents any bad data to be generated/processed if off_t happens to not be large enough to handle some big packs. Better be safe than sorry. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Apr 9, 2007 at 01:06 UTC
d7dd02231f75604e388afb905f7bf8afd1bf4b24
3 files changed
+34
-16
builtin-pack-objects.c
+13
-6
@@ -369,7 +369,7 @@ static int revalidate_loose_object(struct object_entry *entry,
369
return check_loose_inflate(map, mapsize, size);
370
}
371
372
-static off_t write_object(struct sha1file *f,
372
+static unsigned long write_object(struct sha1file *f,
373
struct object_entry *entry)
374
{
375
unsigned long size;
@@ -503,16 +503,23 @@ static off_t write_one(struct sha1file *f,
503
struct object_entry *e,
504
off_t offset)
505
{
506
+ unsigned long size;
507
+
508
+ /* offset is non zero if object is written already. */
509
if (e->offset || e->preferred_base)
507
- /* offset starts from header size and cannot be zero
508
- * if it is written already.
509
- */
510
return offset;
511
- /* if we are deltified, write out its base object first. */
511
+
512
+ /* if we are deltified, write out base object first. */
513
if (e->delta)
514
offset = write_one(f, e->delta, offset);
515
+
516
e->offset = offset;
515
- return offset + write_object(f, e);
517
+ size = write_object(f, e);
518
+
519
+ /* make sure off_t is sufficiently large not to wrap */
520
+ if (offset > offset + size)
521
+ die("pack too large for current definition of off_t");
522
+ return offset + size;
523
}
524
525
static void write_pack_file(void)
builtin-unpack-objects.c
+11
-6
@@ -13,7 +13,8 @@ static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-fil
13
14
/* We always read in 4kB chunks. */
15
static unsigned char buffer[4096];
16
-static unsigned long offset, len, consumed_bytes;
16
+static unsigned int offset, len;
17
+static off_t consumed_bytes;
18
static SHA_CTX ctx;
19
20
/*
@@ -49,6 +50,10 @@ static void use(int bytes)
50
die("used more bytes than were available");
51
len -= bytes;
52
offset += bytes;
53
+
54
+ /* make sure off_t is sufficiently large not to wrap */
55
+ if (consumed_bytes > consumed_bytes + bytes)
56
+ die("pack too large for current definition of off_t");
57
consumed_bytes += bytes;
58
}
59
@@ -88,17 +93,17 @@ static void *get_data(unsigned long size)
93
94
struct delta_info {
95
unsigned char base_sha1[20];
91
- unsigned long base_offset;
96
+ unsigned nr;
97
+ off_t base_offset;
98
unsigned long size;
99
void *delta;
94
- unsigned nr;
100
struct delta_info *next;
101
};
102
103
static struct delta_info *delta_list;
104
105
static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
101
- unsigned long base_offset,
106
+ off_t base_offset,
107
void *delta, unsigned long size)
108
{
109
struct delta_info *info = xmalloc(sizeof(*info));
@@ -113,7 +118,7 @@ static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
118
}
119
120
struct obj_info {
116
- unsigned long offset;
121
+ off_t offset;
122
unsigned char sha1[20];
123
};
124
@@ -200,7 +205,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
205
} else {
206
unsigned base_found = 0;
207
unsigned char *pack, c;
203
- unsigned long base_offset;
208
+ off_t base_offset;
209
unsigned lo, mid, hi;
210
211
pack = fill(1);
index-pack.c
+10
-4
@@ -12,7 +12,7 @@ static const char index_pack_usage[] =
12
13
struct object_entry
14
{
15
- unsigned long offset;
15
+ off_t offset;
16
unsigned long size;
17
unsigned int hdr_size;
18
enum object_type type;
@@ -22,7 +22,7 @@ struct object_entry
22
23
union delta_base {
24
unsigned char sha1[20];
25
- unsigned long offset;
25
+ off_t offset;
26
};
27
28
/*
@@ -83,7 +83,8 @@ static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
83
84
/* We always read in 4kB chunks. */
85
static unsigned char input_buffer[4096];
86
-static unsigned long input_offset, input_len, consumed_bytes;
86
+static unsigned int input_offset, input_len;
87
+static off_t consumed_bytes;
88
static SHA_CTX input_ctx;
89
static int input_fd, output_fd, pack_fd;
90
@@ -129,6 +130,10 @@ static void use(int bytes)
130
die("used more bytes than were available");
131
input_len -= bytes;
132
input_offset += bytes;
133
+
134
+ /* make sure off_t is sufficiently large not to wrap */
135
+ if (consumed_bytes > consumed_bytes + bytes)
136
+ die("pack too large for current definition of off_t");
137
consumed_bytes += bytes;
138
}
139
@@ -216,7 +221,8 @@ static void *unpack_entry_data(unsigned long offset, unsigned long size)
221
static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
222
{
223
unsigned char *p, c;
219
- unsigned long size, base_offset;
224
+ unsigned long size;
225
+ off_t base_offset;
226
unsigned shift;
227
228
obj->offset = consumed_bytes;