From 5344d6bec6e19e23d66c37abbce4ec391704e1f1 Mon Sep 17 00:00:00 2001 From: dtucker Date: Thu, 9 Feb 2023 09:54:11 +0000 Subject: [PATCH] Ensure that there is a terminating newline when adding a new entry to known_hosts. bz#3529, with git+openssh at limpsquid.nl, ok deraadt@ markus@ --- usr.bin/ssh/hostfile.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/usr.bin/ssh/hostfile.c b/usr.bin/ssh/hostfile.c index e193d421734..47b4adea852 100644 --- a/usr.bin/ssh/hostfile.c +++ b/usr.bin/ssh/hostfile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hostfile.c,v 1.93 2022/01/06 22:02:52 djm Exp $ */ +/* $OpenBSD: hostfile.c,v 1.94 2023/02/09 09:54:11 dtucker Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -513,9 +513,17 @@ add_host_to_hostfile(const char *filename, const char *host, if (key == NULL) return 1; /* XXX ? */ hostfile_create_user_ssh_dir(filename, 0); - f = fopen(filename, "a"); + f = fopen(filename, "a+"); if (!f) return 0; + /* Make sure we have a terminating newline. */ + if (fseek(f, -1L, SEEK_END) == 0 && fgetc(f) != '\n') + if (fputc('\n', f) != '\n') { + error("Failed to add terminating newline to %s: %s", + filename, strerror(errno)); + fclose(f); + return 0; + } success = write_host_entry(f, host, NULL, key, store_hash); fclose(f); return success; -- 2.20.1