Skip to contentSkip to conversation

rerere: avoid buffer overrun

PATCH New Elijah Newren 2 participants 5 messages Sep 5, 2018 – Sep 11, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rerere: avoid buffer overrun

check_one_conflict() compares i to active_nr in two places to avoid buffer overruns, but left out an important third location.

Note that this bug probably cannot be triggered in the current codebase. Existing merge strategies have tended not to create entries at stage #1 that do not have a corresponding entry at either stage #2 or stage #3. (The bug was found while exploring some ideas for modifying conflict resolution.) However, it is technically possible that an external merge strategy could create such entries, so add a check to avoid segfaults.

---
Originally submitted here:
https://public-inbox.org/git/[email protected]/
While I want to push that RFC series more and will get back to it, this patch is independently good so I'm submitting separately.

For some history about how the current code got to where it is today, see:
fb70a06da2f1 ("rerere: fix an off-by-one non-bug", 2015-06-28) 5eda906b2873 ("rerere: handle conflicts with multiple stage #1 entries", 2015-07-24)

 rerere.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rerere.c +1 -1
index c7787aa07f..783d4dae2a 100644
@@ -533,7 +533,7 @@ static int check_one_conflict(int i, int *type)
 	}
 
 	*type = PUNTED;
-	while (ce_stage(active_cache[i]) == 1)
+	while (i < active_nr && ce_stage(active_cache[i]) == 1)
 		i++;
 
 	/* Only handle regular files with both stages #2 and #3 */
Signature · 1 line
2.19.0.rc2
#1 lore raw

v2 cover Fix rerere segfault with specially crafted merge

This series documents and fixes a rerere segfault (dating back to git-2.7.0) when a merge strategy makes the last entry in the index be at stage 1.

Changes since last version:
- In my last submission I only had the code fix and no testcase; I even commented in the commit message that the bug "cannot be triggered in the current codebase" and mentioned that I was fixing it just because an exotic external merge strategy could trigger the bug. I realized later that it is triggerable without any exotic external merge strategies; built-in ones can do it.

Elijah Newren (2):
t4200: demonstrate rerere segfault on specially crafted merge
rerere: avoid buffer overrun

 rerere.c          |  2 +-
 t/t4200-rerere.sh | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
Signature · 1 line
2.19.0.2.gdbd064c81f
#3 lore raw

PATCH v2 2/2 rerere: avoid buffer overrun

check_one_conflict() compares i to active_nr in two places to avoid buffer overruns, but left out an important third location.

The code did used to have a check here comparing i to active_nr, back before commit fb70a06da2f1 ("rerere: fix an off-by-one non-bug", 2015-06-28), however the code at the time used an 'if' rather than a 'while' meaning back then that this loop could not have read past the end of the array, making the check unnecessary and it was removed. Unfortunately, in commit 5eda906b2873 ("rerere: handle conflicts with multiple stage #1 entries", 2015-07-24), the 'if' was changed to a 'while' and the check comparing i and active_nr was not re-instated, leading to this problem.

---
 rerere.c          | 2 +-
 t/t4200-rerere.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
rerere.c +1 -1
index c7787aa07f..783d4dae2a 100644
@@ -533,7 +533,7 @@ static int check_one_conflict(int i, int *type)
 	}
 
 	*type = PUNTED;
-	while (ce_stage(active_cache[i]) == 1)
+	while (i < active_nr && ce_stage(active_cache[i]) == 1)
 		i++;
 
 	/* Only handle regular files with both stages #2 and #3 */
t/t4200-rerere.sh +1 -1
index f9294b7677..313222d0d6 100755
@@ -596,7 +596,7 @@ test_expect_success 'setup simple stage 1 handling' '
 	)
 '
 
-test_expect_failure 'test simple stage 1 handling' '
+test_expect_success 'test simple stage 1 handling' '
 	(
 		cd stage_1_handling &&
 
Signature · 1 line
2.19.0.2.gdbd064c81f
#2 lore raw

PATCH v2 1/2 t4200: demonstrate rerere segfault on specially crafted merge

In reply to Elijah Newren
---
 t/t4200-rerere.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
t/t4200-rerere.sh +29 -0
index 65da74c766..f9294b7677 100755
@@ -577,4 +577,33 @@ test_expect_success 'multiple identical conflicts' '
 	count_pre_post 0 0
 '
 
+test_expect_success 'setup simple stage 1 handling' '
+	test_create_repo stage_1_handling &&
+	(
+		cd stage_1_handling &&
+
+		test_seq 1 10 >original &&
+		git add original &&
+		git commit -m original &&
+
+		git checkout -b A master &&
+		git mv original A &&
+		git commit -m "rename to A" &&
+
+		git checkout -b B master &&
+		git mv original B &&
+		git commit -m "rename to B"
+	)
+'
+
+test_expect_failure 'test simple stage 1 handling' '
+	(
+		cd stage_1_handling &&
+
+		git config rerere.enabled true &&
+		git checkout A^0 &&
+		test_must_fail git merge B^0
+	)
+'
+
 test_done
Signature · 1 line
2.19.0.2.gdbd064c81f
#4 lore raw
In reply to Elijah Newren
Quoted 13 lines · Elijah Newren

check_one_conflict() compares i to active_nr in two places to avoid buffer overruns, but left out an important third location.

The code did used to have a check here comparing i to active_nr, back before commit fb70a06da2f1 ("rerere: fix an off-by-one non-bug", 2015-06-28), however the code at the time used an 'if' rather than a 'while' meaning back then that this loop could not have read past the end of the array, making the check unnecessary and it was removed. Unfortunately, in commit 5eda906b2873 ("rerere: handle conflicts with multiple stage #1 entries", 2015-07-24), the 'if' was changed to a 'while' and the check comparing i and active_nr was not re-instated, leading to this problem.

Quoted 2 lines

---

Thanks. Looks good to me.

Quoted 29 lines
 rerere.c          | 2 +-
 t/t4200-rerere.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
rerere.c +1 -1
index c7787aa07f..783d4dae2a 100644
@@ -533,7 +533,7 @@ static int check_one_conflict(int i, int *type)
 	}
 
 	*type = PUNTED;
-	while (ce_stage(active_cache[i]) == 1)
+	while (i < active_nr && ce_stage(active_cache[i]) == 1)
 		i++;
 
 	/* Only handle regular files with both stages #2 and #3 */
t/t4200-rerere.sh +1 -1
index f9294b7677..313222d0d6 100755
@@ -596,7 +596,7 @@ test_expect_success 'setup simple stage 1 handling' '
 	)
 '
 
-test_expect_failure 'test simple stage 1 handling' '
+test_expect_success 'test simple stage 1 handling' '
 	(
 		cd stage_1_handling &&