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

[PATCH v3 1/3] sparse-checkout: fix segfault on malformed patterns

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 15, 2021, 13:46 UTC
Message-ID
<1744a26845fbe4d7dbc80f387be1d842b5f8fe94.1639575968.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1069.v3.git.1639575968.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

Then core.sparseCheckoutCone is enabled, the sparse-checkout patterns are used to populate two hashsets that accelerate pattern matching. If the user modifies the sparse-checkout file outside of the 'sparse-checkout' builtin, then strange patterns can happen, triggering some error checks.

One of these error checks is possible to hit when some special characters exist in a line. A warning message is correctly written to stderr, but then there is additional logic that attempts to remove the line from the hashset and free the data. This leads to a segfault in the 'git sparse-checkout list' command because it iterates over the contents of the hashset, which is now invalid.

The fix here is to stop trying to remove from the hashset. Better to leave bad data in the sparse-checkout matching logic (with a warning) than to segfault. If we are in this state, then we are already traversing into undefined behavior, so this change to keep the entry in the hashset is no worse than removing it.

Add a test that triggers the segfault without the code change.
Reported-by: John Burnett <johnburnett@johnburnett.com>
Reviewed-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 dir.c                              |  3 ---
 t/t1091-sparse-checkout-builtin.sh | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/dir.c b/dir.c
index 5aa6fbad0b7..0693c7cb3ee 100644
--- a/dir.c
+++ b/dir.c
@@ -819,9 +819,6 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
 		/* we already included this at the parent level */
 		warning(_("your sparse-checkout file may have issues: pattern '%s' is repeated"),
 			given->pattern);
-		hashmap_remove(&pl->parent_hashmap, &translated->ent, &data);
-		free(data);
-		free(translated);
 	}
 
 	return;
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 272ba1b566b..c72b8ee2e7b 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -708,4 +708,19 @@ test_expect_success 'cone mode clears ignored subdirectories' '
 	test_cmp expect out
 '
 
+test_expect_success 'malformed cone-mode patterns' '
+	git -C repo sparse-checkout init --cone &&
+	mkdir -p repo/foo/bar &&
+	touch repo/foo/bar/x repo/foo/y &&
+	cat >repo/.git/info/sparse-checkout <<-\EOF &&
+	/*
+	!/*/
+	/foo/
+	!/foo/*/
+	/foo/\*/
+	EOF
+	cat repo/.git/info/sparse-checkout &&
+	git -C repo sparse-checkout list
+'
+
 test_done
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Junio C Hamano
Message 13 of 24 in “sparse-checkout: fix segfault on malformed patterns”
  1. 0/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 7, 2021
  2. 1/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 7, 2021
  3. Elijah NewrenDec 7, 2021
  4. 2/3 sparse-checkout: fix OOM error with mixed patternsDerrick Stolee via GitGitGadget, Dec 7, 2021
  5. 3/3 sparse-checkout: refuse to add to bad patternsDerrick Stolee via GitGitGadget, Dec 7, 2021
  6. Elijah NewrenDec 7, 2021
  7. Derrick StoleeDec 8, 2021
  8. 0/4 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 10, 2021
  9. 2/4 sparse-checkout: fix OOM error with mixed patternsDerrick Stolee via GitGitGadget, Dec 10, 2021
  10. 1/4 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 10, 2021
  11. 3/4 sparse-checkout: refuse to add to bad patternsDerrick Stolee via GitGitGadget, Dec 10, 2021
  12. 0/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 15, 2021
  13. 1/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 15, 2021
  14. Junio C HamanoDec 15, 2021
  15. Derrick StoleeDec 16, 2021
  16. 2/3 sparse-checkout: fix OOM error with mixed patternsDerrick Stolee via GitGitGadget, Dec 15, 2021
  17. 3/3 sparse-checkout: refuse to add to bad patternsDerrick Stolee via GitGitGadget, Dec 15, 2021
  18. Junio C HamanoDec 15, 2021
  19. Derrick StoleeDec 16, 2021
  20. Junio C HamanoDec 16, 2021
  21. 0/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 16, 2021
  22. 1/3 sparse-checkout: fix segfault on malformed patternsDerrick Stolee via GitGitGadget, Dec 16, 2021
  23. 2/3 sparse-checkout: fix OOM error with mixed patternsDerrick Stolee via GitGitGadget, Dec 16, 2021
  24. 3/3 sparse-checkout: refuse to add to bad patternsDerrick Stolee via GitGitGadget, Dec 16, 2021

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.