Skip to content

Commit abe27c0

Browse files
committed
vcs-svn: guard against overflow when computing preimage length
Signed integer overflow produces undefined behavior in C and off_t is a signed type. For predictable behavior, add some checks to protect in advance against overflow. On 32-bit systems ftell as called by buffer_tmpfile_prepare_to_read is likely to fail with EOVERFLOW when reading the corresponding postimage, and this patch does not fix that. So it's more of a futureproofing measure than a complete fix. Signed-off-by: Jonathan Nieder <[email protected]>
1 parent 157415a commit abe27c0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

vcs-svn/fast_export.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ static int ends_with(const char *s, size_t len, const char *suffix)
166166
static int parse_cat_response_line(const char *header, off_t *len)
167167
{
168168
size_t headerlen = strlen(header);
169+
uintmax_t n;
169170
const char *type;
170171
const char *end;
171172

@@ -174,14 +175,25 @@ static int parse_cat_response_line(const char *header, off_t *len)
174175
type = memmem(header, headerlen, " blob ", strlen(" blob "));
175176
if (!type)
176177
return error("cat-blob header has wrong object type: %s", header);
177-
*len = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178+
n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178179
if (end == type + strlen(" blob "))
179180
return error("cat-blob header does not contain length: %s", header);
181+
if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
182+
return error("cat-blob header contains negative length: %s", header);
183+
if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
184+
return error("blob too large for current definition of off_t");
185+
*len = n;
180186
if (*end)
181187
return error("cat-blob header contains garbage after length: %s", header);
182188
return 0;
183189
}
184190

191+
static void check_preimage_overflow(off_t a, off_t b)
192+
{
193+
if (signed_add_overflows(a, b))
194+
die("blob too large for current definition of off_t");
195+
}
196+
185197
static long apply_delta(off_t len, struct line_buffer *input,
186198
const char *old_data, uint32_t old_mode)
187199
{
@@ -204,6 +216,7 @@ static long apply_delta(off_t len, struct line_buffer *input,
204216
}
205217
if (old_mode == REPO_MODE_LNK) {
206218
strbuf_addstr(&preimage.buf, "link ");
219+
check_preimage_overflow(preimage_len, strlen("link "));
207220
preimage_len += strlen("link ");
208221
}
209222
if (svndiff0_apply(input, len, &preimage, out))

0 commit comments

Comments
 (0)