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

[PATCH 1/2] gpg-interface: fix leak of "line" in parse_ssh_output()

From
Jeff King <peff@peff.net>
Date
Oct 18, 2021, 17:15 UTC
Message-ID
<YW2rlCAtRumXzVfX@coredump.intra.peff.net>
In-Reply-To
<YW2qpuosxr9PNwVa@coredump.intra.peff.net>

We xmemdupz() this buffer, but never free it. Let's do so. We'll use a cleanup label, since there are multiple exits from the function.

Note that it was also declared a "const char *". We could switch that to "char *" to indicate that it's allocated, but that make it awkward to use with skip_prefix(). So instead, we'll introduce an extra non-const pointer.

Signed-off-by: Jeff King <peff@peff.net>
---
 gpg-interface.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/gpg-interface.c b/gpg-interface.c
index 433482307c..c60b9cd19d 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -365,6 +365,7 @@ static int verify_gpg_signed_buffer(struct signature_check *sigc,
 static void parse_ssh_output(struct signature_check *sigc)
 {
 	const char *line, *principal, *search;
+	char *to_free;
 	char *key = NULL;
 
 	/*
@@ -383,7 +384,7 @@ static void parse_ssh_output(struct signature_check *sigc)
 	sigc->result = 'B';
 	sigc->trust_level = TRUST_NEVER;
 
-	line = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
+	line = to_free = xmemdupz(sigc->output, strcspn(sigc->output, "\n"));
 
 	if (skip_prefix(line, "Good \"git\" signature for ", &line)) {
 		/* Valid signature and known principal */
@@ -403,7 +404,7 @@ static void parse_ssh_output(struct signature_check *sigc)
 		sigc->result = 'G';
 		sigc->trust_level = TRUST_UNDEFINED;
 	} else {
-		return;
+		goto cleanup;
 	}
 
 	key = strstr(line, "key");
@@ -417,6 +418,9 @@ static void parse_ssh_output(struct signature_check *sigc)
 		 */
 		sigc->result = 'B';
 	}
+
+cleanup:
+	free(to_free);
 }
 
 static int verify_ssh_signed_buffer(struct signature_check *sigc,
-- 
2.33.1.1223.g80c1dbe6e5
Previous: Jeff KingNext: Jeff King
Message 2 of 5 in “some leak fixes on fs/ssh-signing-fix”
  1. 0/2 some leak fixes on fs/ssh-signing-fixJeff King, Oct 18, 2021
  2. 1/2 gpg-interface: fix leak of "line" in parse_ssh_output()Jeff King, Oct 18, 2021
  3. 2/2 gpg-interface: fix leak of strbufs in get_ssh_key_fingerprint()Jeff King, Oct 18, 2021
  4. Fabian StelzerOct 19, 2021
  5. Jeff KingOct 19, 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.