Skip to contentSkip to conversation

gpg-interface: handle missing " with " gracefully in parse_ssh_output()

PATCH In review René Scharfe 3 participants 8 messages Oct 30, 2021 – Nov 1, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpg-interface: handle missing " with " gracefully in parse_ssh_output()

If the output of ssh-keygen starts with "Good \"git\" signature for ", but is not followed by " with " for some reason, then parse_ssh_output() uses -1 as the len parameter of xmemdupz(), which in turn will end the program. Reject the signature and carry on instead in that case.

---
This code was added after v2.33.0.
Patch formatted with --inter-hunk-context=2 for easier review.

Silly bonus question: What's the purpose of the "+ 1" and "- 1", which seem to cancel each other out?

 gpg-interface.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
gpg-interface.c +6 -4
index 800d8caa67..62d340e78a 100644
@@ -387,17 +387,19 @@ static void parse_ssh_output(struct signature_check *sigc)
 	line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
 
 	if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
-		/* Valid signature and known principal */
-		sigc->result = 'G';
-		sigc->trust_level = TRUST_FULLY;
-
 		/* Search for the last "with" to get the full principal */
 		principal = line;
 		do {
 			search = strstr(line, " with ");
 			if (search)
 				line = search + 1;
 		} while (search != NULL);
+		if (line == principal)
+			goto cleanup;
+
+		/* Valid signature and known principal */
+		sigc->result = 'G';
+		sigc->trust_level = TRUST_FULLY;
 		sigc->signer = xmemdupz(principal, line - principal - 1);
 	} else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
 		/* Valid signature, but key unknown */

--
2.33.1

#1 lore raw

PATCH 2/2 gpg-interface: avoid buffer overrun in parse_ssh_output()

If the string "key" we found in the output of ssh-keygen happens to be located at the very end of the line, then going four characters further leaves us beyond the end of the string. Explicitly search for the space after "key" to handle a missing one gracefully.

---
This code was added after v2.33.0.

 gpg-interface.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
gpg-interface.c +2 -2
index 62d340e78a..3838536f0a 100644
@@ -409,9 +409,9 @@ static void parse_ssh_output(struct signature_check *sigc)
 		goto cleanup;
 	}
 
-	key = strstr(line, "key");
+	key = strstr(line, "key ");
 	if (key) {
-		sigc->fingerprint = xstrdup(strstr(line, "key") + 4);
+		sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
 		sigc->key = xstrdup(sigc->fingerprint);
 	} else {
 		/*

--
2.33.1

#2 lore raw

Re: [PATCH 2/2] gpg-interface: avoid buffer overrun in parse_ssh_output()

Quoted 8 lines · René Scharfe

If the string "key" we found in the output of ssh-keygen happens to be located at the very end of the line, then going four characters further leaves us beyond the end of the string. Explicitly search for the space after "key" to handle a missing one gracefully.

---
This code was added after v2.33.0.

Thanks.

Fabian, we are in -rc phase where we concentrate on fixing bugs in the new code in 'master'. A quick ack, "here is a better way to fix it", or "no, that won't be needed because..." is very much appreciated.

Quoted 21 lines
 gpg-interface.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
gpg-interface.c +2 -3
index 62d340e78a..3838536f0a 100644
@@ -409,9 +409,9 @@ static void parse_ssh_output(struct signature_check *sigc)
 		goto cleanup;
 	}
 
-	key = strstr(line, "key");
+	key = strstr(line, "key ");
 	if (key) {
-		sigc->fingerprint = xstrdup(strstr(line, "key") + 4);
+		sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
 		sigc->key = xstrdup(sigc->fingerprint);
 	} else {
 		/*
--

2.33.1

#3 lore raw

Re: [PATCH 2/2] gpg-interface: avoid buffer overrun in parse_ssh_output()

In reply to René Scharfe
Quoted 29 lines · René Scharfe

If the string "key" we found in the output of ssh-keygen happens to be located at the very end of the line, then going four characters further leaves us beyond the end of the string. Explicitly search for the space after "key" to handle a missing one gracefully.

---
This code was added after v2.33.0.

 gpg-interface.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
gpg-interface.c +2 -3
index 62d340e78a..3838536f0a 100644
@@ -409,9 +409,9 @@ static void parse_ssh_output(struct signature_check *sigc)
 		goto cleanup;
 	}
 
-	key = strstr(line, "key");
+	key = strstr(line, "key ");
 	if (key) {
-		sigc->fingerprint = xstrdup(strstr(line, "key") + 4);
+		sigc->fingerprint = xstrdup(strstr(line, "key ") + 4);
 		sigc->key = xstrdup(sigc->fingerprint);
 	} else {
 		/*
--
Quoted 2 lines

2.33.1

Thanks. This is obviously correct.

#4 lore raw

Re: [PATCH 2/2] gpg-interface: avoid buffer overrun in parse_ssh_output()

Quoted 6 lines · Fabian Stelzer
Quoted 5 lines · René Scharfe

If the string "key" we found in the output of ssh-keygen happens to be located at the very end of the line, then going four characters further leaves us beyond the end of the string. Explicitly search for the space after "key" to handle a missing one gracefully.

Quoted 7 lines
Quoted 5 lines

---
This code was added after v2.33.0.

Thanks. This is obviously correct.

Thanks, both. Will apply.

#5 lore raw
In reply to René Scharfe
Quoted 9 lines · René Scharfe

If the output of ssh-keygen starts with "Good \"git\" signature for ", but is not followed by " with " for some reason, then parse_ssh_output() uses -1 as the len parameter of xmemdupz(), which in turn will end the program. Reject the signature and carry on instead in that case.

---
This code was added after v2.33.0.
Patch formatted with --inter-hunk-context=2 for easier review.

Nice spotting.

Quoted 2 lines

Silly bonus question: What's the purpose of the "+ 1" and "- 1", which seem to cancel each other out?

Fabian, we are in -rc phase where we concentrate on fixing bugs in the new code in 'master'. A quick ack, "here is a better way to fix it", or "no, that won't be needed because..." is very much appreciated.

Thanks.

Quoted 34 lines
 gpg-interface.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
gpg-interface.c +6 -5
index 800d8caa67..62d340e78a 100644
@@ -387,17 +387,19 @@ static void parse_ssh_output(struct signature_check *sigc)
 	line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
 
 	if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
-		/* Valid signature and known principal */
-		sigc->result = 'G';
-		sigc->trust_level = TRUST_FULLY;
-
 		/* Search for the last "with" to get the full principal */
 		principal = line;
 		do {
 			search = strstr(line, " with ");
 			if (search)
 				line = search + 1;
 		} while (search != NULL);
+		if (line == principal)
+			goto cleanup;
+
+		/* Valid signature and known principal */
+		sigc->result = 'G';
+		sigc->trust_level = TRUST_FULLY;
 		sigc->signer = xmemdupz(principal, line - principal - 1);
 	} else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
 		/* Valid signature, but key unknown */
--

2.33.1

#6 lore raw
In reply to René Scharfe
Quoted 5 lines · René Scharfe

If the output of ssh-keygen starts with "Good \"git\" signature for ", but is not followed by " with " for some reason, then parse_ssh_output() uses -1 as the len parameter of xmemdupz(), which in turn will end the program. Reject the signature and carry on instead in that case.

Quoted 7 lines

---
This code was added after v2.33.0.
Patch formatted with --inter-hunk-context=2 for easier review.

Silly bonus question: What's the purpose of the "+ 1" and "- 1", which seem to cancel each other out?

They do. But only for the xmemdupz. It is important for the strstr() to skip over already found strings (" with " could be in the principal name as well - multiple times even). And doing strstr(line +1, ...) can be problematic for the first iteration.

Quoted 33 lines
 gpg-interface.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
gpg-interface.c +6 -5
index 800d8caa67..62d340e78a 100644
@@ -387,17 +387,19 @@ static void parse_ssh_output(struct signature_check *sigc)
 	line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
 
 	if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
-		/* Valid signature and known principal */
-		sigc->result = 'G';
-		sigc->trust_level = TRUST_FULLY;
-
 		/* Search for the last "with" to get the full principal */
 		principal = line;
 		do {
 			search = strstr(line, " with ");
 			if (search)
 				line = search + 1;
 		} while (search != NULL);
+		if (line == principal)
+			goto cleanup;
+
+		/* Valid signature and known principal */
+		sigc->result = 'G';
+		sigc->trust_level = TRUST_FULLY;
 		sigc->signer = xmemdupz(principal, line - principal - 1);
 	} else if (skip_prefix(line, "Good \"git\" signature with ", &line)) {
 		/* Valid signature, but key unknown */
--
Quoted 2 lines

2.33.1

The fix looks good otherwise.
Thanks!

#7 lore raw

Am 01.11.21 um 09:40 schrieb Fabian Stelzer:

Quoted 8 lines · René Scharfe
Quoted 2 lines

Silly bonus question: What's the purpose of the "+ 1" and "- 1", which seem to cancel each other out?

They do. But only for the xmemdupz. It is important for the strstr() to skip over already found strings (" with " could be in the principal name as well - multiple times even). And doing strstr(line +1, ...) can be problematic for the first iteration.

Ah, right.

Quoted 5 lines

gpg-interface.c static void parse_ssh_output(struct signature_check *sigc) · line 396

Quoted 5 lines
gpg-interface.c +0 -0
 		do {
 			search = strstr(line, " with ");
 			if (search)
 				line = search + 1;
 		} while (search != NULL);

This can be shortened to:

		while ((search = strstr(line, " with ")))
			line = search + 1;

I still would have tripped over the +1 / -1, though. Calling an rfind() or strrstr() or find_last() function that deals with it internally would have helped me avoid that, I think, but we don't have use for such a thing anywhere else.

René