summaryrefslogtreecommitdiffstats
path: root/hostfile.c
diff options
context:
space:
mode:
authordtucker@openbsd.org <dtucker@openbsd.org>2021-10-02 03:17:01 +0000
committerDamien Miller <djm@mindrot.org>2021-10-06 14:40:32 +1100
commit57680a2ab43518c5ccbd8242c40482106cde6ac1 (patch)
treed4d1dda1c6afccb51152169a1d456deb9eff0f99 /hostfile.c
parente3e62deb549fde215b777d95276c304f84bf00c6 (diff)
upstream: Dynamically allocate encoded HashKnownHosts and free as
appropriate. Saves 1k of static storage and prevents snprintf "possible truncation" warnings from newer compilers (although in this case it's false positive since the actual sizes are limited by the output size of the SHA1). ok djm@ OpenBSD-Commit-ID: e254ae723f7e3dce352c7d5abc4b6d87faf61bf4
Diffstat (limited to 'hostfile.c')
-rw-r--r--hostfile.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/hostfile.c b/hostfile.c
index ce00cd71..a035b381 100644
--- a/hostfile.c
+++ b/hostfile.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hostfile.c,v 1.91 2021/07/05 01:16:46 dtucker Exp $ */
+/* $OpenBSD: hostfile.c,v 1.92 2021/10/02 03:17:01 dtucker Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -118,7 +118,7 @@ host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
struct ssh_hmac_ctx *ctx;
u_char salt[256], result[256];
char uu_salt[512], uu_result[512];
- static char encoded[1024];
+ char *encoded = NULL;
u_int len;
len = ssh_digest_bytes(SSH_DIGEST_SHA1);
@@ -143,9 +143,8 @@ host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
__b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
fatal_f("__b64_ntop failed");
-
- snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
- HASH_DELIM, uu_result);
+ xasprintf(&encoded, "%s%s%c%s", HASH_MAGIC, uu_salt, HASH_DELIM,
+ uu_result);
return (encoded);
}
@@ -456,6 +455,7 @@ write_host_entry(FILE *f, const char *host, const char *ip,
else {
fprintf(f, "%s ", lhost);
}
+ free(hashed_host);
free(lhost);
if ((r = sshkey_write(key, f)) == 0)
success = 1;
@@ -730,8 +730,8 @@ hostfile_replace_entries(const char *filename, const char *host, const char *ip,
static int
match_maybe_hashed(const char *host, const char *names, int *was_hashed)
{
- int hashed = *names == HASH_DELIM;
- const char *hashed_host;
+ int hashed = *names == HASH_DELIM, ret;
+ char *hashed_host = NULL;
size_t nlen = strlen(names);
if (was_hashed != NULL)
@@ -739,8 +739,10 @@ match_maybe_hashed(const char *host, const char *names, int *was_hashed)
if (hashed) {
if ((hashed_host = host_hash(host, names, nlen)) == NULL)
return -1;
- return nlen == strlen(hashed_host) &&
- strncmp(hashed_host, names, nlen) == 0;
+ ret = (nlen == strlen(hashed_host) &&
+ strncmp(hashed_host, names, nlen) == 0);
+ free(hashed_host);
+ return ret;
}
return match_hostname(host, names) == 1;
}