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

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

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 16, 2021, 16:13 UTC
Message-ID
<5353c541d9f72aa0a14cd2b02e5ddcc0b6a4dae1.1639671222.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1069.v4.git.1639671222.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. In addition, we disable cone mode sparse-checkout because of the malformed data. This results in the pattern-matching working with a possibly-slower algorithm, but using the patterns as they are in the sparse-checkout file.

This also changes the behavior of commands such as 'git sparse-checkout list' because the output patterns will be the contents of the sparse-checkout file instead of the list of directories. This is an existing behavior for other types of bad patterns.

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                              |  4 +---
 t/t1091-sparse-checkout-builtin.sh | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/dir.c b/dir.c
index 5aa6fbad0b7..7e72958d51d 100644
--- a/dir.c
+++ b/dir.c
@@ -819,9 +819,7 @@ 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);
+		goto clear_hashmaps;
 	}
 
 	return;
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 272ba1b566b..3921ea80138 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -708,4 +708,25 @@ 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
+
+	# Listing the patterns will notice the duplicate pattern and
+	# emit a warning. It will list the patterns directly instead
+	# of using the cone-mode translation to a set of directories.
+	git -C repo sparse-checkout list >actual 2>err &&
+	test_cmp repo/.git/info/sparse-checkout actual &&
+	grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
+	grep "warning: disabling cone pattern matching" err
+'
+
 test_done
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee via GitGitGadget
Message 22 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.