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

[PATCH 2/4] fsck: check rev-index checksums

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 17, 2023, 16:21 UTC
Message-ID
<7db4ec3e327ed3695f4f5409cb2dc80c72688758.1681748502.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1512.git.1681748502.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

The previous change added calls to verify_pack_revindex() in builtin/fsck.c, but the implementation of the method was left empty. Add the first and most-obvious check to this method: checksum verification.

While here, create a helper method in the test script that makes it easy to adjust the .rev file and check that 'git fsck' reports the correct error message.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 pack-revindex.c          | 10 ++++++++++
 t/t5325-reverse-index.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
diff --git a/pack-revindex.c b/pack-revindex.c
index c3f2aaa3fea..007a806994f 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -5,6 +5,7 @@
 #include "packfile.h"
 #include "config.h"
 #include "midx.h"
+#include "csum-file.h"
 
 struct revindex_entry {
 	off_t offset;
@@ -309,6 +310,15 @@ int load_pack_revindex(struct repository *r, struct packed_git *p)
  */
 int verify_pack_revindex(struct packed_git *p)
 {
+	/* Do not bother checking if not initialized. */
+	if (!p->revindex_map)
+		return 0;
+
+	if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) {
+		error(_("invalid checksum"));
+		return -1;
+	}
+
 	return 0;
 }
 
diff --git a/t/t5325-reverse-index.sh b/t/t5325-reverse-index.sh
index 206c412f50b..6b7c709a1f6 100755
--- a/t/t5325-reverse-index.sh
+++ b/t/t5325-reverse-index.sh
@@ -145,4 +145,44 @@ test_expect_success 'fsck succeeds on good rev-index' '
 	)
 '
 
+test_expect_success 'set up rev-index corruption tests' '
+	git init corrupt &&
+	(
+		cd corrupt &&
+
+		test_commit commit &&
+		git -c pack.writeReverseIndex=true repack -ad &&
+
+		revfile=$(ls .git/objects/pack/pack-*.rev) &&
+		chmod a+w $revfile &&
+		cp $revfile $revfile.bak
+	)
+'
+
+corrupt_rev_and_verify () {
+	(
+		pos="$1" &&
+		value="$2" &&
+		error="$3" &&
+
+		cd corrupt &&
+		revfile=$(ls .git/objects/pack/pack-*.rev) &&
+
+		# Reset to original rev-file.
+		cp $revfile.bak $revfile &&
+
+		printf "$value" | dd of=$revfile bs=1 seek="$pos" conv=notrunc &&
+		test_must_fail git fsck 2>err &&
+		grep "$error" err
+	)
+}
+
+test_expect_success 'fsck catches invalid checksum' '
+	revfile=$(ls corrupt/.git/objects/pack/pack-*.rev) &&
+	orig_size=$(wc -c <$revfile) &&
+	hashpos=$((orig_size - 10)) &&
+	corrupt_rev_and_verify $hashpos bogus \
+		"invalid checksum"
+'
+
 test_done
-- 
gitgitgadget
Previous: Taylor BlauNext: Junio C Hamano
Message 4 of 19 in “git fsck: check pack rev-index files”
  1. 0/4 git fsck: check pack rev-index filesDerrick Stolee via GitGitGadget, Apr 17, 2023
  2. 1/4 fsck: create scaffolding for rev-index checksDerrick Stolee via GitGitGadget, Apr 17, 2023
  3. Taylor BlauApr 17, 2023
  4. 2/4 fsck: check rev-index checksumsDerrick Stolee via GitGitGadget, Apr 17, 2023
  5. Junio C HamanoApr 17, 2023
  6. Derrick StoleeApr 18, 2023
  7. Taylor BlauApr 17, 2023
  8. Derrick StoleeApr 18, 2023
  9. Taylor BlauApr 18, 2023
  10. Derrick StoleeApr 18, 2023
  11. Taylor BlauApr 18, 2023
  12. 3/4 fsck: check rev-index position valuesDerrick Stolee via GitGitGadget, Apr 17, 2023
  13. Junio C HamanoApr 17, 2023
  14. Derrick StoleeApr 18, 2023
  15. Taylor BlauApr 17, 2023
  16. 4/4 fsck: validate .rev file headerDerrick Stolee via GitGitGadget, Apr 17, 2023
  17. Junio C HamanoApr 17, 2023
  18. Taylor BlauApr 18, 2023
  19. Junio C HamanoApr 18, 2023

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.