[PATCH] hostfile: fix leak in hostfile_replace_entries

Lidong Yan yldhome2d2 at gmail.com
Sat Nov 22 12:54:11 AEDT 2025


In hostfile_replace_entries(), sshkey_fingerprint() allocates a finger
print. But if following calls to write_host_entry() failed, the finger
print could leak. Add free(fp) below the `fail` label to avoid possible
leaks.

Signed-off-by: Lidong Yan <502024330056 at smail.nju.edu.cn>
---
 hostfile.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/hostfile.c b/hostfile.c
index 4cec57da5..8ea51dda1 100644
--- a/hostfile.c
+++ b/hostfile.c
@@ -626,7 +626,7 @@ hostfile_replace_entries(const char *filename, const char *host, const char *ip,
 	int r, fd, oerrno = 0;
 	int loglevel = quiet ? SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_VERBOSE;
 	struct host_delete_ctx ctx;
-	char *fp, *temp = NULL, *back = NULL;
+	char *fp = NULL, *temp = NULL, *back = NULL;
 	const char *what;
 	mode_t omask;
 	size_t i;
@@ -715,6 +715,7 @@ hostfile_replace_entries(const char *filename, const char *host, const char *ip,
 		    host, ip == NULL ? "" : ",", ip == NULL ? "" : ip, filename,
 		    sshkey_ssh_name(keys[i]), fp);
 		free(fp);
+		fp = NULL;
 		ctx.modified = 1;
 	}
 	fclose(ctx.out);
@@ -755,6 +756,7 @@ hostfile_replace_entries(const char *filename, const char *host, const char *ip,
 		unlink(temp);
 	free(temp);
 	free(back);
+	free(fp);
 	if (ctx.out != NULL)
 		fclose(ctx.out);
 	free(ctx.match_keys);
-- 
2.50.GIT



More information about the openssh-unix-dev mailing list