Skip to content

Commit d61434a

Browse files
peffgitster
authored andcommitted
http-walker: fix buffer underflow processing remote alternates
If we parse a remote alternates (or http-alternates), we expect relative lines like: ../../foo.git/objects which we convert into "$URL/../foo.git/" (and then use that as a base for fetching more objects). But if the remote feeds us nonsense like just: ../ we will try to blindly strip the last 7 characters, assuming they contain the string "objects". Since we don't _have_ 7 characters at all, this results in feeding a small negative value to strbuf_add(), which converts it to a size_t, resulting in a big positive value. This should consistently fail (since we can't generall allocate the max size_t minus 7 bytes), so there shouldn't be any security implications. Let's fix this by using strbuf_strip_suffix() to drop the characters we want. If they're not present, we'll ignore the alternate (in theory we could use it as-is, but the rest of the http-walker code unconditionally tacks "objects/" back on, so it is it not prepared to handle such a case). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e7e07d5 commit d61434a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

http-walker.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,16 @@ static void process_alternates_response(void *callback_data)
296296
okay = 1;
297297
}
298298
}
299-
/* skip "objects\n" at end */
300299
if (okay) {
301300
struct strbuf target = STRBUF_INIT;
302301
strbuf_add(&target, base, serverlen);
303-
strbuf_add(&target, data + i, posn - i - 7);
304-
305-
if (is_alternate_allowed(target.buf)) {
302+
strbuf_add(&target, data + i, posn - i);
303+
if (!strbuf_strip_suffix(&target, "objects")) {
304+
warning("ignoring alternate that does"
305+
" not end in 'objects': %s",
306+
target.buf);
307+
strbuf_release(&target);
308+
} else if (is_alternate_allowed(target.buf)) {
306309
warning("adding alternate object store: %s",
307310
target.buf);
308311
newalt = xmalloc(sizeof(*newalt));

0 commit comments

Comments
 (0)