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

[PATCH 2/2] gpg-interface: fix leak of strbufs in get_ssh_key_fingerprint()

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

We read stdout from gpg into a strbuf, then split it into a list of strbufs, pull out one element, and return it. But we don't free either the original stdout buffer, nor the list returned from strbuf_split().

This patch fixes both. Note that we have to detach the returned string from its strbuf before calling strbuf_list_free(), as that would otherwise throw it away.

Signed-off-by: Jeff King <peff@peff.net>
---
 gpg-interface.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/gpg-interface.c b/gpg-interface.c
index c60b9cd19d..800d8caa67 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -711,6 +711,7 @@ static char *get_ssh_key_fingerprint(const char *signing_key)
 	int ret = -1;
 	struct strbuf fingerprint_stdout = STRBUF_INIT;
 	struct strbuf **fingerprint;
+	char *fingerprint_ret;
 
 	/*
 	 * With SSH Signing this can contain a filename or a public key
@@ -737,7 +738,10 @@ static char *get_ssh_key_fingerprint(const char *signing_key)
 		die_errno(_("failed to get the ssh fingerprint for key '%s'"),
 			  signing_key);
 
-	return strbuf_detach(fingerprint[1], NULL);
+	fingerprint_ret = strbuf_detach(fingerprint[1], NULL);
+	strbuf_list_free(fingerprint);
+	strbuf_release(&fingerprint_stdout);
+	return fingerprint_ret;
 }
 
 /* Returns the first public key from an ssh-agent to use for signing */
-- 
2.33.1.1223.g80c1dbe6e5
Previous: Jeff KingNext: Fabian Stelzer
Message 3 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.