summaryrefslogtreecommitdiff
path: root/usr.bin/ssh
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@cvs.openbsd.org>2021-10-02 03:17:02 +0000
committerDarren Tucker <dtucker@cvs.openbsd.org>2021-10-02 03:17:02 +0000
commit34070cd061ff03e27c8460473f217c16070539b0 (patch)
treed897f02d4f6566ae87c64ca199fed226165e094f /usr.bin/ssh
parent15feef28d52b0e29e3beb8d727956432de339d2b (diff)
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@
Diffstat (limited to 'usr.bin/ssh')
-rw-r--r--usr.bin/ssh/hostfile.c20
-rw-r--r--usr.bin/ssh/ssh-keygen.c3
-rw-r--r--usr.bin/ssh/ssh-keyscan.c7
3 files changed, 17 insertions, 13 deletions
diff --git a/usr.bin/ssh/hostfile.c b/usr.bin/ssh/hostfile.c
index b92788ad86c..7736a980caf 100644
--- a/usr.bin/ssh/hostfile.c
+++ b/usr.bin/ssh/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
@@ -116,7 +116,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);
@@ -141,9 +141,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);
}
@@ -454,6 +453,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;
@@ -723,8 +723,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)
@@ -732,8 +732,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;
}
diff --git a/usr.bin/ssh/ssh-keygen.c b/usr.bin/ssh/ssh-keygen.c
index d73b3d7d9b0..7745addaee0 100644
--- a/usr.bin/ssh/ssh-keygen.c
+++ b/usr.bin/ssh/ssh-keygen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keygen.c,v 1.437 2021/09/08 03:23:44 djm Exp $ */
+/* $OpenBSD: ssh-keygen.c,v 1.438 2021/10/02 03:17:01 dtucker Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1180,6 +1180,7 @@ known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
if ((hashed = host_hash(cp, NULL, 0)) == NULL)
fatal("hash_host failed");
fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
+ free(hashed);
ctx->has_unhashed = 1;
}
free(ohosts);
diff --git a/usr.bin/ssh/ssh-keyscan.c b/usr.bin/ssh/ssh-keyscan.c
index bae2d58f4e9..222dbdf5df5 100644
--- a/usr.bin/ssh/ssh-keyscan.c
+++ b/usr.bin/ssh/ssh-keyscan.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keyscan.c,v 1.139 2021/01/27 09:26:54 djm Exp $ */
+/* $OpenBSD: ssh-keyscan.c,v 1.140 2021/10/02 03:17:01 dtucker Exp $ */
/*
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
*
@@ -287,8 +287,8 @@ keygrab_ssh2(con *c)
static void
keyprint_one(const char *host, struct sshkey *key)
{
- char *hostport;
- const char *known_host, *hashed;
+ char *hostport = NULL, *hashed = NULL;
+ const char *known_host;
found_one = 1;
@@ -306,6 +306,7 @@ keyprint_one(const char *host, struct sshkey *key)
fprintf(stdout, "%s ", known_host);
sshkey_write(key, stdout);
fputs("\n", stdout);
+ free(hashed);
free(hostport);
}