kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
7afad6
diff -up openssh-8.0p1/hostfile.c.cve-2020-14145 openssh-8.0p1/hostfile.c
7afad6
--- openssh-8.0p1/hostfile.c.cve-2020-14145	2019-04-18 00:52:57.000000000 +0200
7afad6
+++ openssh-8.0p1/hostfile.c	2021-05-17 16:53:38.694577251 +0200
7afad6
@@ -409,6 +409,18 @@ lookup_key_in_hostkeys_by_type(struct ho
7afad6
 	    found) == HOST_FOUND);
7afad6
 }
7afad6
 
7afad6
+int
7afad6
+lookup_marker_in_hostkeys(struct hostkeys *hostkeys, int want_marker)
7afad6
+{
7afad6
+	u_int i;
7afad6
+
7afad6
+	for (i = 0; i < hostkeys->num_entries; i++) {
7afad6
+		if (hostkeys->entries[i].marker == (HostkeyMarker)want_marker)
7afad6
+			return 1;
7afad6
+	}
7afad6
+	return 0;
7afad6
+}
7afad6
+
7afad6
 static int
7afad6
 write_host_entry(FILE *f, const char *host, const char *ip,
7afad6
     const struct sshkey *key, int store_hash)
7afad6
diff -up openssh-8.0p1/hostfile.h.cve-2020-14145 openssh-8.0p1/hostfile.h
7afad6
--- openssh-8.0p1/hostfile.h.cve-2020-14145	2019-04-18 00:52:57.000000000 +0200
7afad6
+++ openssh-8.0p1/hostfile.h	2021-05-17 16:53:38.694577251 +0200
7afad6
@@ -39,6 +39,7 @@ HostStatus check_key_in_hostkeys(struct
7afad6
     const struct hostkey_entry **);
7afad6
 int	 lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
7afad6
     const struct hostkey_entry **);
7afad6
+int	 lookup_marker_in_hostkeys(struct hostkeys *, int);
7afad6
 
7afad6
 int	 hostfile_read_key(char **, u_int *, struct sshkey *);
7afad6
 int	 add_host_to_hostfile(const char *, const char *,
7afad6
diff -up openssh-8.0p1/sshconnect2.c.cve-2020-14145 openssh-8.0p1/sshconnect2.c
7afad6
--- openssh-8.0p1/sshconnect2.c.cve-2020-14145	2021-05-17 16:53:38.610576561 +0200
7afad6
+++ openssh-8.0p1/sshconnect2.c	2021-05-17 16:54:58.169230103 +0200
7afad6
@@ -98,12 +98,25 @@ verify_host_key_callback(struct sshkey *
7afad6
 	return 0;
7afad6
 }
7afad6
 
7afad6
+/* Returns the first item from a comma-separated algorithm list */
7afad6
+static char *
7afad6
+first_alg(const char *algs)
7afad6
+{
7afad6
+	char *ret, *cp;
7afad6
+
7afad6
+	ret = xstrdup(algs);
7afad6
+	if ((cp = strchr(ret, ',')) != NULL)
7afad6
+		*cp = '\0';
7afad6
+	return ret;
7afad6
+}
7afad6
+
7afad6
 static char *
7afad6
 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
7afad6
 {
7afad6
-	char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
7afad6
+	char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
7afad6
+	char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
7afad6
 	size_t maxlen;
7afad6
-	struct hostkeys *hostkeys;
7afad6
+	struct hostkeys *hostkeys = NULL;
7afad6
 	int ktype;
7afad6
 	u_int i;
7afad6
 
7afad6
@@ -115,6 +128,26 @@ order_hostkeyalgs(char *host, struct soc
7afad6
 	for (i = 0; i < options.num_system_hostfiles; i++)
7afad6
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
7afad6
 
7afad6
+	/*
7afad6
+	 * If a plain public key exists that matches the type of the best
7afad6
+	 * preference HostkeyAlgorithms, then use the whole list as is.
7afad6
+	 * Note that we ignore whether the best preference algorithm is a
7afad6
+	 * certificate type, as sshconnect.c will downgrade certs to
7afad6
+	 * plain keys if necessary.
7afad6
+	 */
7afad6
+	best = first_alg(options.hostkeyalgorithms);
7afad6
+	if (lookup_key_in_hostkeys_by_type(hostkeys,
7afad6
+	    sshkey_type_plain(sshkey_type_from_name(best)), NULL)) {
7afad6
+		debug3("%s: have matching best-preference key type %s, "
7afad6
+		    "using HostkeyAlgorithms verbatim", __func__, best);
7afad6
+		ret = xstrdup(options.hostkeyalgorithms);
7afad6
+		goto out;
7afad6
+	}
7afad6
+
7afad6
+	/*
7afad6
+	 * Otherwise, prefer the host key algorithms that match known keys
7afad6
+	 * while keeping the ordering of HostkeyAlgorithms as much as possible.
7afad6
+	 */
7afad6
 	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
7afad6
 	maxlen = strlen(avail) + 1;
7afad6
 	first = xmalloc(maxlen);
7afad6
@@ -131,11 +164,23 @@ order_hostkeyalgs(char *host, struct soc
7afad6
 	while ((alg = strsep(&avail, ",")) && *alg != '\0') {
7afad6
 		if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
7afad6
 			fatal("%s: unknown alg %s", __func__, alg);
7afad6
+		/*
7afad6
+		 * If we have a @cert-authority marker in known_hosts then
7afad6
+		 * prefer all certificate algorithms.
7afad6
+		 */
7afad6
+		if (sshkey_type_is_cert(ktype) &&
7afad6
+		    lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
7afad6
+			ALG_APPEND(first, alg);
7afad6
+			continue;
7afad6
+		}
7afad6
+		/* If the key appears in known_hosts then prefer it */
7afad6
 		if (lookup_key_in_hostkeys_by_type(hostkeys,
7afad6
-		    sshkey_type_plain(ktype), NULL))
7afad6
+		    sshkey_type_plain(ktype), NULL)) {
7afad6
 			ALG_APPEND(first, alg);
7afad6
-		else
7afad6
-			ALG_APPEND(last, alg);
7afad6
+			continue;
7afad6
+		}
7afad6
+		/* Otherwise, put it last */
7afad6
+		ALG_APPEND(last, alg);
7afad6
 	}
7afad6
 #undef ALG_APPEND
7afad6
 	xasprintf(&ret, "%s%s%s", first,
7afad6
@@ -143,6 +188,8 @@ order_hostkeyalgs(char *host, struct soc
7afad6
 	if (*first != '\0')
7afad6
 		debug3("%s: prefer hostkeyalgs: %s", __func__, first);
7afad6
 
7afad6
+ out:
7afad6
+	free(best);
7afad6
 	free(first);
7afad6
 	free(last);
7afad6
 	free(hostname);