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

[PATCH 2/2] builtin/pack-objects.c: ensure pack validity from MIDX bitmap objects

From
Taylor Blau <me@ttaylorr.com>
Date
May 13, 2022, 16:23 UTC
Message-ID
<bcc48004450769410d7e6d8a6e56f08bfa9a02a3.1652458395.git.me@ttaylorr.com>
In-Reply-To
<cover.1652458395.git.me@ttaylorr.com>

When using a multi-pack bitmap, pack-objects will try to perform its traversal using a call to `traverse_bitmap_commit_list()`, which calls `add_object_entry_from_bitmap()` to add each object it finds to its packing list.

This path can cause pack-objects to add objects from packs that don't have open pack_fds on them, by avoiding a call to `is_pack_valid()`. This is because we only call `is_pack_valid()` on the preferred pack (in order to do verbatim reuse via `reuse_partial_packfile_from_bitmap()`) and not others when loading a MIDX bitmap.

In this case, `add_object_entry_from_bitmap()` will check whether it wants each object entry by calling `want_object_in_pack()`, which will call `want_found_object` (since its caller already supplied a `found_pack`). In most cases (particularly without `--local`, and when `ignored_packed_keep_on_disk` and `ignored_packed_keep_in_core` are both "0"), we'll take the entry from the pack contained in the MIDX bitmap, all without an open pack_fd.

When we then try to use that entry later to assemble the actual pack, we'll be susceptible to any simultaneous writers moving that pack out of the way (e.g., due to a concurrent repack) without having an open file descriptor, causing races that result in errors like:

    remote: Enumerating objects: 1498802, done.
    remote: fatal: packfile ./objects/pack/pack-e57d433b5a588daa37fbe946e2b28dfaec03a93e.pack cannot be accessed
    remote: aborting due to possible repository corruption on the remote side.

This race can happen even with multi-pack bitmaps, since we may open a MIDX bitmap that is being rewritten long before its packs are actually unlinked.

Work around this by calling `is_pack_valid()` from within `want_found_object()`, matching the behavior in `want_object_in_pack_one()` (which has an analogous call). Most calls to `is_pack_valid()` should be basically no-ops, since only the first call requires us to open a file (subsequent calls realize the file is already open, and return immediately).

This does require us to make a small change in `want_object_in_pack_one()`, since `want_found_object()` may return `-1` (indicating "keep searching for other packs containing this object") when `*found_pack` is non-NULL. Force `want_object_in_pack_one()` to call `is_pack_valid()` when `p != *found_pack`, not just when `*found_pack` is non-NULL.

An alternative approach to closing this race would have been to call `is_pack_valid()` on _all_ packs in a multi-pack bitmap on load. This has a couple of problems:

  - it is unnecessarily expensive in the cases where we don't actually
    need to open any packs (e.g., in `git rev-list --use-bitmap-index
    --count`)
  - more importantly, it means any time we would have hit this race,
    we'll avoid using bitmaps altogether, leading to significant
    slowdowns by forcing a full object traversal
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 builtin/pack-objects.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index 014dcd4bc9..a243193807 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1357,6 +1357,9 @@ static int want_found_object(const struct object_id *oid, int exclude,
 	if (incremental)
 		return 0;
 
+	if (!is_pack_valid(p))
+		return -1;
+
 	/*
 	 * When asked to do --local (do not include an object that appears in a
 	 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
@@ -1424,14 +1427,15 @@ static int want_object_in_pack_one(struct packed_git *p,
 				   off_t *found_offset)
 {
 	off_t offset;
+	int use_found = p == *found_pack;
 
-	if (p == *found_pack)
+	if (use_found)
 		offset = *found_offset;
 	else
 		offset = find_pack_entry_one(oid->hash, p);
 
 	if (offset) {
-		if (!*found_pack) {
+		if (!use_found) {
 			if (!is_pack_valid(p))
 				return -1;
 			*found_offset = offset;
-- 
2.36.1.76.g80c0bcd80e
Previous: Taylor BlauNext: Jonathan Tan
Message 5 of 29 in “pack-objects: fix a pair of MIDX bitmap-related races”
  1. 0/2 pack-objects: fix a pair of MIDX bitmap-related racesTaylor Blau, May 13, 2022
  2. 1/2 pack-bitmap: check preferred pack validity when opening MIDX bitmapTaylor Blau, May 13, 2022
  3. Junio C HamanoMay 13, 2022
  4. Taylor BlauMay 13, 2022
  5. 2/2 builtin/pack-objects.c: ensure pack validity from MIDX bitmap objectsTaylor Blau, May 13, 2022
  6. Jonathan TanMay 13, 2022
  7. Taylor BlauMay 14, 2022
  8. Jonathan TanMay 16, 2022
  9. Taylor BlauMay 14, 2022
  10. Jonathan TanMay 16, 2022
  11. 0/4 pack-objects: fix a pair of MIDX bitmap-related racesTaylor Blau, May 24, 2022
  12. 1/4 pack-bitmap.c: check preferred pack validity when opening MIDX bitmapTaylor Blau, May 24, 2022
  13. Ævar Arnfjörð BjarmasonMay 24, 2022
  14. Taylor BlauMay 24, 2022
  15. Ævar Arnfjörð BjarmasonMay 24, 2022
  16. 2/4 builtin/pack-objects.c: avoid redundant NULL checkTaylor Blau, May 24, 2022
  17. Junio C HamanoMay 24, 2022
  18. Taylor BlauMay 25, 2022
  19. 3/4 builtin/pack-objects.c: ensure included `--stdin-packs` existTaylor Blau, May 24, 2022
  20. Ævar Arnfjörð BjarmasonMay 24, 2022
  21. Taylor BlauMay 24, 2022
  22. Ævar Arnfjörð BjarmasonMay 24, 2022
  23. Junio C HamanoMay 24, 2022
  24. Taylor BlauMay 25, 2022
  25. Victoria DyeMay 26, 2022
  26. Taylor BlauMay 26, 2022
  27. 4/4 builtin/pack-objects.c: ensure pack validity from MIDX bitmap objectsTaylor Blau, May 24, 2022
  28. Junio C HamanoMay 24, 2022
  29. Taylor BlauMay 25, 2022

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.