git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] use_pack: handle signed off_t overflow

From
Jeff King <peff@peff.net>
Date
Feb 25, 2016, 14:23 UTC
Message-ID
<20160225142326.GC17811@sigill.intra.peff.net>
In-Reply-To
<20160225142004.GA17678@sigill.intra.peff.net>

A v2 pack index file can specify an offset within a packfile of up to 2^64-1 bytes. On a system with a signed 64-bit off_t, we can represent only up to 2^63-1. This means that a corrupted .idx file can end up with a negative offset in the pack code. Our bounds-checking use_pack function looks for too-large offsets, but not for ones that have wrapped around to negative. Let's do so, which fixes an out-of-bounds access demonstrated in t5313.

Signed-off-by: Jeff King <peff@peff.net>
---
 sha1_file.c                   | 2 ++
 t/t5313-pack-bounds-checks.sh | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/sha1_file.c b/sha1_file.c
index d2ffd92..9d223e6 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1076,6 +1076,8 @@ unsigned char *use_pack(struct packed_git *p,
 		die("packfile %s cannot be accessed", p->pack_name);
 	if (offset > (p->pack_size - 20))
 		die("offset beyond end of packfile (truncated pack?)");
+	if (offset < 0)
+		die("offset before end of packfile (broken .idx?)");
 
 	if (!win || !in_window(win, offset)) {
 		if (win)
diff --git a/t/t5313-pack-bounds-checks.sh b/t/t5313-pack-bounds-checks.sh
index 0717746..a8a587a 100755
--- a/t/t5313-pack-bounds-checks.sh
+++ b/t/t5313-pack-bounds-checks.sh
@@ -136,7 +136,7 @@ test_expect_success 'bogus offset into v2 extended table' '
 	test_must_fail git index-pack --verify $pack
 '
 
-test_expect_failure 'bogus offset inside v2 extended table' '
+test_expect_success 'bogus offset inside v2 extended table' '
 	# We need two objects here, so we can plausibly require
 	# an extended table (if the first object were larger than 2^31).
 	do_pack "$object $(git rev-parse HEAD)" --index-version=2 &&
-- 
2.7.2.695.gf3fde8e
Previous: Jeff KingNext: Nguyễn Thái Ngọc Duy
Message 7 of 10 in “out-of-bounds access from corrupted .idx files”
  1. 0/3 out-of-bounds access from corrupted .idx filesJeff King, Feb 25, 2016
  2. 1/3 t5313: test bounds-checks of corrupted/malicious pack/idx filesJeff King, Feb 25, 2016
  3. Johannes SixtFeb 25, 2016
  4. Junio C HamanoFeb 25, 2016
  5. Jeff KingFeb 25, 2016
  6. 2/3 nth_packed_object_offset: bounds-check extended offsetJeff King, Feb 25, 2016
  7. 3/3 use_pack: handle signed off_t overflowJeff King, Feb 25, 2016
  8. 4/3 sha1_file.c: mark strings for translationNguyễn Thái Ngọc Duy, Feb 27, 2016
  9. Junio C HamanoFeb 27, 2016
  10. Jeff KingFeb 27, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.