Skip to contentSkip to conversation

rerere: fix crash in during clear

PATCH Superseded Marcel Röthke 3 participants 15 messages Feb 18, 2024 – Apr 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rerere: fix crash in during clear

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir. This happens because scan_rerere_dir only allocates the status field in struct rerere_dir when a post or preimage was found. In some cases a segfault may happen even if a post or preimage was recorded if it was not for the variant of interest and the number of the variant that is present is lower than the variant of interest.

This patch solves this by making sure the status field is large enough to accommodate for the variant of interest so it can be accesses without checking if it is large enough.

An alternative solution would be to always check before accessing the status field, but I think the chosen solution aligns better with the assumptions made elsewhere in the code.

---
 rerere.c | 3 +++
 1 file changed, 3 insertions(+)
rerere.c +3 -0
index ca7e77ba68..3973ccce37 100644
@@ -219,6 +219,9 @@ static void read_rr(struct repository *r, struct string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/* make sure id->collection->status has enough space
+		 * for the variant we are interested in */
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);
Signature · 1 line
2.43.2
#1 lore raw

Hi

Quoted 1 line

rerere: fix crash in during clear

“in during clear”? Did you mean “during clear”?

Quoted 11 lines · Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir. This happens because scan_rerere_dir only allocates the status field in struct rerere_dir when a post or preimage was found. In some cases a segfault may happen even if a post or preimage was recorded if it was not for the variant of interest and the number of the variant that is present is lower than the variant of interest.

This patch solves this by making sure the status field is large enough

You can simplify “This patch solves this” to “Solve this”; see SubmittingPatches under “imperative-mood”.

Quoted 2 lines

to accommodate for the variant of interest so it can be accesses without checking if it is large enough.

“accessed”

Quoted 15 lines

An alternative solution would be to always check before accessing the status field, but I think the chosen solution aligns better with the assumptions made elsewhere in the code.

---
 rerere.c | 3 +++
 1 file changed, 3 insertions(+)
rerere.c +0 -0
index ca7e77ba68..3973ccce37 100644

rerere.c static void read_rr(struct repository *r, struct · line 224

Quoted 6 lines
rerere.c +2 -0
@@ -219,6 +219,9 @@ static void read_rr(struct repository *r, struct
 string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/* make sure id->collection->status has enough space
+		 * for the variant we are interested in */

Multi-line comments should have the delimiters on separate lines from the text. See CodingGuidelines under “Multi-line comments”.

Quoted 6 lines
rerere.c +1 -0
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}

strbuf_release(&buf);
--
2.43.2

#2 lore raw
Quoted 3 lines · Kristoffer Haugsbakk
Quoted 1 line

rerere: fix crash in during clear

“in during clear”? Did you mean “during clear”?

Yes, thanks.

Quoted 11 lines · Marcel Röthke
Quoted 10 lines

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir. This happens because scan_rerere_dir only allocates the status field in struct rerere_dir when a post or preimage was found. In some cases a segfault may happen even if a post or preimage was recorded if it was not for the variant of interest and the number of the variant that is present is lower than the variant of interest.

Quoted 4 lines
Quoted 1 line

This patch solves this by making sure the status field is large enough

You can simplify “This patch solves this” to “Solve this”; see SubmittingPatches under “imperative-mood”.

done

Quoted 6 lines
Quoted 3 lines

to accommodate for the variant of interest so it can be accesses without checking if it is large enough.

“accessed”

done

Quoted 10 lines
Quoted 10 lines

An alternative solution would be to always check before accessing the status field, but I think the chosen solution aligns better with the assumptions made elsewhere in the code.

---
 rerere.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 14 lines

rerere.c static void read_rr(struct repository *r, struct · line 224

Quoted 11 lines
rerere.c +2 -0
index ca7e77ba68..3973ccce37 100644
@@ -219,6 +219,9 @@ static void read_rr(struct repository *r, struct
 string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/* make sure id->collection->status has enough space
+		 * for the variant we are interested in */

Multi-line comments should have the delimiters on separate lines from the text. See CodingGuidelines under “Multi-line comments”.

done

Thank you for your feedback!

#3 lore raw

PATCH v2 1/1 rerere: fix crash during clear

In reply to Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir. This happens because scan_rerere_dir only allocates the status field in struct rerere_dir when a post or preimage was found. In some cases a segfault may happen even if a post or preimage was recorded if it was not for the variant of interest and the number of the variant that is present is lower than the variant of interest.

Solve this by making sure the status field is large enough to accommodate for the variant of interest so it can be accessed without checking if it is large enough.

An alternative solution would be to always check before accessing the status field, but I think the chosen solution aligns better with the assumptions made elsewhere in the code.

---
Range-diff against v1:
1: 93f982d170 ! 1: 68178298fe rerere: fix crash in during clear
@@ Metadata
Author: Marcel Röthke <[email protected]>

## Commit message ##
- rerere: fix crash in during clear
+ rerere: fix crash during clear

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git
@@ Commit message
the number of the variant that is present is lower than the variant of interest.

- This patch solves this by making sure the status field is large enough
- to accommodate for the variant of interest so it can be accesses without
+ Solve this by making sure the status field is large enough to
+ accommodate for the variant of interest so it can be accessed without
checking if it is large enough.

An alternative solution would be to always check before accessing the
@@ rerere.c: static void read_rr(struct repository *r, struct string_list *rr)
buf.buf[hexsz] = '\0';
id = new_rerere_id_hex(buf.buf);
id->variant = variant;
-+ /* make sure id->collection->status has enough space
-+ * for the variant we are interested in */
++ /*
++ * make sure id->collection->status has enough space
++ * for the variant we are interested in
++ */
+ fit_variant(id->collection, variant);
string_list_insert(rr, path)->util = id;
}

 rerere.c | 5 +++++
 1 file changed, 5 insertions(+)
rerere.c +5 -0
index ca7e77ba68..4683d6cbb1 100644
@@ -219,6 +219,11 @@ static void read_rr(struct repository *r, struct string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/*
+		 * make sure id->collection->status has enough space
+		 * for the variant we are interested in
+		 */
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);
Signature · 1 line
2.43.2
#4 lore raw

Re: [PATCH v2] rerere: fix crash during clear

Quoted 4 lines · Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir.

I had to read this twice before realizing the reason why I found it hard to grok was because of a missing comma between "recorded" and "git".

Quoted 3 lines

This happens because scan_rerere_dir
only allocates the status field in struct rerere_dir when a post or preimage was found.

But that is not really the root cause, no? Readers following the above text are probably wondering why the preimage was not recorded, when a conflict resulted in stopping a mergy-command and invoking rerere machinery, before rerere_clear() got called. Is that something that usually happen? How? Do we have a reproduction sequence of such a state that we can make it into a new test in t4200 where we already have tests for "git rerere clear" and its friends?

Quoted 4 lines

In some cases a segfault may happen even if a post
or preimage was recorded if it was not for the variant of interest and the number of the variant that is present is lower than the variant of interest.

Ditto. What sequence of events would lead to such a state?

The answer *can* be ".git/rr-cache being a normal directory, the user can poke around, removing files randomly, which can create such a problematic situation", and the reproduction test *can* also be to simulate such an end-user action, but I am asking primarily because I want to make sure that we are *not* losing or failing to create necessary preimage files, causing this problem to users who do not muck with files in .git/rr-cache themselves.

Thanks.

#5 lore raw

Re: [PATCH v2] rerere: fix crash during clear

Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir.

I had to read this twice before realizing the reason why I found it hard to grok was because of a missing comma between "recorded" and "git".

fixed

Quoted 4 lines
Quoted 3 lines

This happens because scan_rerere_dir
only allocates the status field in struct rerere_dir when a post or preimage was found.

Quoted 8 lines

But that is not really the root cause, no? Readers following the above text are probably wondering why the preimage was not recorded, when a conflict resulted in stopping a mergy-command and invoking rerere machinery, before rerere_clear() got called. Is that something that usually happen? How? Do we have a reproduction sequence of such a state that we can make it into a new test in t4200 where we already have tests for "git rerere clear" and its friends?

I'm unfortunately not sure how it happened. I do have the initial state of the repository and I think I know the commands that were executed, but I could not reproduce it.

I will look into adding a test case though.

#6 lore raw

Re: [PATCH v2] rerere: fix crash during clear

Quoted 12 lines · Marcel Röthke
Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir.

I had to read this twice before realizing the reason why I found it hard to grok was because of a missing comma between "recorded" and "git".

Quoted 7 lines

fixed
...
I'm unfortunately not sure how it happened. I do have the initial state of the repository and I think I know the commands that were executed, but I could not reproduce it.

I will look into adding a test case though.

It has been about a month. Any new development on this topic?

Thanks.

#7 lore raw

Re: [PATCH v2] rerere: fix crash during clear

Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Marcel Röthke
Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Marcel Röthke

When rerere_clear is called, for instance when aborting a rebase, and the current conflict does not have a pre or postimage recorded git crashes with a SEGFAULT in has_rerere_resolution when accessing the status member of struct rerere_dir.

I had to read this twice before realizing the reason why I found it hard to grok was because of a missing comma between "recorded" and "git".

fixed
...
I'm unfortunately not sure how it happened. I do have the initial state of the repository and I think I know the commands that were executed, but I could not reproduce it.

Quoted 5 lines
Quoted 1 line

I will look into adding a test case though.

It has been about a month. Any new development on this topic?

Thanks.

No, I have been otherwise occupied. But I plan to get back to this soon. Probably this week, next week at the latest.

#8 lore raw

Re: [PATCH v2] rerere: fix crash during clear

In reply to Junio C Hamano

After reproducing this without manually corrupting the database, I noticed that the summary line is no longer accurate. Because it fixes two SEGFAULTs and one of them does not happen during clear. Can I change this in v3 or should I start a new thread?

#9 lore raw

Re: [PATCH v2] rerere: fix crash during clear

Quoted 4 lines · Marcel Röthke

After reproducing this without manually corrupting the database, I noticed that the summary line is no longer accurate. Because it fixes two SEGFAULTs and one of them does not happen during clear. Can I change this in v3 or should I start a new thread?

Updating the title in the course of evolution of a topic/patch is perfectly normal. Please do so in v3 as continuation of the previous round.

Thanks.

#10 lore raw

PATCH v3 1/1 rerere: fix crashes due to unmatched opening conflict markers

In reply to Marcel Röthke

When rerere handles a conflict with an unmatched opening conflict marker in a file with other conflicts, it will fail create a preimage and also fail allocate the status member of struct rerere_dir. Currently the status member is allocated after the error handling. This will lead to a SEGFAULT when the status member is accessed during cleanup of the failed parse.

Additionally, in subsequent executions of rerere, after removing the MERGE_RR.lock manually, rerere crashes for a similar reason. MERGE_RR points to a conflict id that has no preimage, therefore the status member is not allocated and a SEGFAULT happens when trying to check if a preimage exists.

Solve this by making sure the status field is allocated correctly and add tests to prevent the bug from reoccurring.

This does not fix the root cause, failing to parse stray conflict markers, but I don't think we can do much better than recognizing it, printing an error, and moving on gracefully.

---
Interdiff against v2:
diff --git a/t/t4200-rerere.sh b/t/t4200-rerere.sh
index fb53dddf79..1e80f76860 100755
--- a/t/t4200-rerere.sh
+++ b/t/t4200-rerere.sh
@@ -671,4 +671,67 @@ test_expect_success 'test simple stage 1 handling' '
)
'

+test_expect_success 'rerere does not crash with missing preimage' '
+ git config rerere.enabled true &&
+
+ echo bar >test &&
+ git add test &&
+ git commit -m "one" &&
+ git branch rerere_no_crash &&
+
+ echo foo >>test &&
+ git add test &&
+ git commit -m "two" &&
+
+ git checkout rerere_no_crash &&
+ echo "bar" >>test &&
+ git add test &&
+ git commit -m "three" &&
+
+ test_must_fail git rebase main &&
+ rm .git/rr-cache/*/preimage &&
+ git rebase --abort
+'
+
+test_expect_success 'rerere does not crash with unmatched conflict marker' '
+ git config rerere.enabled true &&
+
+ echo bar >test &&
+ git add test &&
+ git commit -m "one" &&
+ git branch rerere_no_preimage &&
+
+ cat >test <<-EOF &&
+ test
+ bar
+ foobar
+ EOF
+ git add test &&
+ git commit -m "two" &&
+
+ git checkout rerere_no_preimage &&
+ echo "bar" >>test &&
+ git add test &&
+ git commit -m "three" &&
+
+ cat >test <<-EOF &&
+ foobar
+ bar
+ bar
+ EOF
+ git add test &&
+ git commit -m "four" &&
+
+ test_must_fail git rebase main &&
+ cat >test <<-EOF &&
+ test
+ bar
+ <<<<<<< HEAD
+ foobar
+ bar
+ EOF
+ git add test &&
+ git rebase --continue
+'
+
test_done

 rerere.c          |  5 ++++
 t/t4200-rerere.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
rerere.c +5 -0
index ca7e77ba68..4683d6cbb1 100644
@@ -219,6 +219,11 @@ static void read_rr(struct repository *r, struct string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/*
+		 * make sure id->collection->status has enough space
+		 * for the variant we are interested in
+		 */
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);
t/t4200-rerere.sh +63 -0

67 lines not on this page · raw message

--
2.44.0

#11 lore raw

Re: [PATCH v3] rerere: fix crashes due to unmatched opening conflict markers

Quoted 6 lines · Marcel Röthke

When rerere handles a conflict with an unmatched opening conflict marker in a file with other conflicts, it will fail create a preimage and also fail allocate the status member of struct rerere_dir. Currently the status member is allocated after the error handling. This will lead to a SEGFAULT when the status member is accessed during cleanup of the failed parse.

Nicely diagnosed. Yes, such a corrupt preimage should not enter the rerere database as it is unusable for future replaying of the resolution. rerere should be prepared to deal with such an input more gracefully.

Quoted 8 lines

Additionally, in subsequent executions of rerere, after removing the MERGE_RR.lock manually, rerere crashes for a similar reason. MERGE_RR points to a conflict id that has no preimage, therefore the status member is not allocated and a SEGFAULT happens when trying to check if a preimage exists.

Solve this by making sure the status field is allocated correctly and add tests to prevent the bug from reoccurring.

Thanks.

Quoted 3 lines

This does not fix the root cause, failing to parse stray conflict markers, but I don't think we can do much better than recognizing it, printing an error, and moving on gracefully.

I somehow doubt that "parse stray markers" is something we _want_ to do in the first place. Is it "the root cause" that we refuse/reject such a corrupt input from entering the rerere database? To me, it seems like that the issue is that we do not do a very good job rejecting them, keeping the internal state consistent.

Quoted 10 lines
 rerere.c          |  5 ++++
 t/t4200-rerere.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
rerere.c +0 -0
index ca7e77ba68..4683d6cbb1 100644
@@ -219,6 +219,11 @@ static void read_rr(struct repository *r, struct string_list *rr)
 		buf.buf[hexsz] = '\0';

rerere.c static void read_rr(struct repository *r, struct string_list *rr) · line 229

Quoted 10 lines
rerere.c +5 -0
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/*
+		 * make sure id->collection->status has enough space
+		 * for the variant we are interested in
+		 */
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);

Both the fix, and the newly added tests, look great to me.

Thanks. Will queue.

#12 lore raw

Re: [PATCH v3] rerere: fix crashes due to unmatched opening conflict markers

In reply to Marcel Röthke

t/t4200-rerere.sh test_expect_success 'test simple stage 1 handling' ' · line 736

Quoted 6 lines · Marcel Röthke
t/t4200-rerere.sh +5 -0
+test_expect_success 'rerere does not crash with unmatched conflict marker' '
+	git config rerere.enabled true &&
...
+	git rebase --continue
+'
+

This one fails, either standalone or when merged to 'seen'.

A sample CI run that failed can be seen here (you probably need to be logged in to view it):

https://github.com/git/git/actions/runs/8694652245/job/23844028985#step:5:1894

#13 lore raw

Re: [PATCH v3] rerere: fix crashes due to unmatched opening conflict markers

Quoted 11 lines · Junio C Hamano

t/t4200-rerere.sh test_expect_success 'test simple stage 1 handling' ' · line 736

Quoted 6 lines · Marcel Röthke
t/t4200-rerere.sh +5 -0
+test_expect_success 'rerere does not crash with unmatched conflict marker' '
+	git config rerere.enabled true &&
...
+	git rebase --continue
+'
+

This one fails, either standalone or when merged to 'seen'.

Quoted 4 lines

A sample CI run that failed can be seen here (you probably need to be logged in to view it):

https://github.com/git/git/actions/runs/8694652245/job/23844028985#step:5:1894

Yes, sorry that I missed that. The last git rebase command in that test needs a test_must_fail because we expect a merge conflict.
Will be fixed in v4.

#14 lore raw

PATCH v4 1/1 rerere: fix crashes due to unmatched opening conflict markers

In reply to Marcel Röthke

When rerere handles a conflict with an unmatched opening conflict marker in a file with other conflicts, it will fail create a preimage and also fail allocate the status member of struct rerere_dir. Currently the status member is allocated after the error handling. This will lead to a SEGFAULT when the status member is accessed during cleanup of the failed parse.

Additionally, in subsequent executions of rerere, after removing the MERGE_RR.lock manually, rerere crashes for a similar reason. MERGE_RR points to a conflict id that has no preimage, therefore the status member is not allocated and a SEGFAULT happens when trying to check if a preimage exists.

Solve this by making sure the status field is allocated correctly and add tests to prevent the bug from reoccurring.

This does not fix the root cause, failing to parse stray conflict markers, but I don't think we can do much better than recognizing it, printing an error, and moving on gracefully.

---
Interdiff against v3:
diff --git a/t/t4200-rerere.sh b/t/t4200-rerere.sh
index 1e80f76860..b0a3e84984 100755
--- a/t/t4200-rerere.sh
+++ b/t/t4200-rerere.sh
@@ -731,7 +731,7 @@ test_expect_success 'rerere does not crash with unmatched conflict marker' '
bar
EOF
git add test &&
- git rebase --continue
+ test_must_fail git rebase --continue
'

test_done

 rerere.c          |  5 ++++
 t/t4200-rerere.sh | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
rerere.c +5 -0
index ca7e77ba68..4683d6cbb1 100644
@@ -219,6 +219,11 @@ static void read_rr(struct repository *r, struct string_list *rr)
 		buf.buf[hexsz] = '\0';
 		id = new_rerere_id_hex(buf.buf);
 		id->variant = variant;
+		/*
+		 * make sure id->collection->status has enough space
+		 * for the variant we are interested in
+		 */
+		fit_variant(id->collection, variant);
 		string_list_insert(rr, path)->util = id;
 	}
 	strbuf_release(&buf);
t/t4200-rerere.sh +63 -0

67 lines not on this page · raw message

--
2.44.0