9fd6981
diff --color -rup a/compat.c b/compat.c
9fd6981
--- a/compat.c	2021-08-20 06:03:49.000000000 +0200
9fd6981
+++ b/compat.c	2022-07-14 17:39:23.770268440 +0200
9fd6981
@@ -157,11 +157,12 @@ compat_banner(struct ssh *ssh, const cha
9fd6981
 	debug_f("no match: %s", version);
9fd6981
 }
9fd6981
 
9fd6981
+/* Always returns pointer to allocated memory, caller must free. */
9fd6981
 char *
9fd6981
 compat_cipher_proposal(struct ssh *ssh, char *cipher_prop)
9fd6981
 {
9fd6981
 	if (!(ssh->compat & SSH_BUG_BIGENDIANAES))
9fd6981
-		return cipher_prop;
9fd6981
+		return xstrdup(cipher_prop);
9fd6981
 	debug2_f("original cipher proposal: %s", cipher_prop);
9fd6981
 	if ((cipher_prop = match_filter_denylist(cipher_prop, "aes*")) == NULL)
9fd6981
 		fatal("match_filter_denylist failed");
9fd6981
@@ -171,11 +172,12 @@ compat_cipher_proposal(struct ssh *ssh,
9fd6981
 	return cipher_prop;
9fd6981
 }
9fd6981
 
9fd6981
+/* Always returns pointer to allocated memory, caller must free. */
9fd6981
 char *
9fd6981
 compat_pkalg_proposal(struct ssh *ssh, char *pkalg_prop)
9fd6981
 {
9fd6981
 	if (!(ssh->compat & SSH_BUG_RSASIGMD5))
9fd6981
-		return pkalg_prop;
9fd6981
+		return xstrdup(pkalg_prop);
9fd6981
 	debug2_f("original public key proposal: %s", pkalg_prop);
9fd6981
 	if ((pkalg_prop = match_filter_denylist(pkalg_prop, "ssh-rsa")) == NULL)
9fd6981
 		fatal("match_filter_denylist failed");
9fd6981
@@ -185,21 +187,26 @@ compat_pkalg_proposal(struct ssh *ssh, c
9fd6981
 	return pkalg_prop;
9fd6981
 }
9fd6981
 
9fd6981
+/* Always returns pointer to allocated memory, caller must free. */
9fd6981
 char *
9fd6981
 compat_kex_proposal(struct ssh *ssh, char *p)
9fd6981
 {
9fd6981
+	char *cp = NULL;
9fd6981
+
9fd6981
 	if ((ssh->compat & (SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX)) == 0)
9fd6981
-		return p;
9fd6981
+		return xstrdup(p);
9fd6981
 	debug2_f("original KEX proposal: %s", p);
9fd6981
 	if ((ssh->compat & SSH_BUG_CURVE25519PAD) != 0)
9fd6981
 		if ((p = match_filter_denylist(p,
9fd6981
 		    "curve25519-sha256@libssh.org")) == NULL)
9fd6981
 			fatal("match_filter_denylist failed");
9fd6981
 	if ((ssh->compat & SSH_OLD_DHGEX) != 0) {
9fd6981
+		cp = p;
9fd6981
 		if ((p = match_filter_denylist(p,
9fd6981
 		    "diffie-hellman-group-exchange-sha256,"
9fd6981
 		    "diffie-hellman-group-exchange-sha1")) == NULL)
9fd6981
 			fatal("match_filter_denylist failed");
9fd6981
+		free(cp);
9fd6981
 	}
9fd6981
 	debug2_f("compat KEX proposal: %s", p);
9fd6981
 	if (*p == '\0')
9fd6981
diff --color -rup a/sshconnect2.c b/sshconnect2.c
9fd6981
--- a/sshconnect2.c	2022-07-14 17:38:43.241496549 +0200
9fd6981
+++ b/sshconnect2.c	2022-07-14 17:39:23.772268479 +0200
9fd6981
@@ -222,6 +222,7 @@ ssh_kex2(struct ssh *ssh, char *host, st
9fd6981
 {
9fd6981
 	char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
9fd6981
 	char *s, *all_key;
9fd6981
+	char *prop_kex = NULL, *prop_enc = NULL, *prop_hostkey = NULL;
9fd6981
 	int r, use_known_hosts_order = 0;
9fd6981
 
9fd6981
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
9fd6981
@@ -252,10 +253,9 @@ ssh_kex2(struct ssh *ssh, char *host, st
9fd6981
 
9fd6981
 	if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
9fd6981
 		fatal_f("kex_names_cat");
9fd6981
-	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(ssh, s);
9fd6981
+	myproposal[PROPOSAL_KEX_ALGS] = prop_kex = compat_kex_proposal(ssh, s);
9fd6981
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
9fd6981
-	    compat_cipher_proposal(ssh, options.ciphers);
9fd6981
-	myproposal[PROPOSAL_ENC_ALGS_STOC] =
9fd6981
+	    myproposal[PROPOSAL_ENC_ALGS_STOC] = prop_enc =
9fd6981
 	    compat_cipher_proposal(ssh, options.ciphers);
9fd6981
 	myproposal[PROPOSAL_COMP_ALGS_CTOS] =
9fd6981
 	    myproposal[PROPOSAL_COMP_ALGS_STOC] =
9fd6981
@@ -264,12 +264,12 @@ ssh_kex2(struct ssh *ssh, char *host, st
9fd6981
 	    myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
9fd6981
 	if (use_known_hosts_order) {
9fd6981
 		/* Query known_hosts and prefer algorithms that appear there */
9fd6981
-		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
9fd6981
+		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = prop_hostkey =
9fd6981
 		    compat_pkalg_proposal(ssh,
9fd6981
 		    order_hostkeyalgs(host, hostaddr, port, cinfo));
9fd6981
 	} else {
9fd6981
 		/* Use specified HostkeyAlgorithms exactly */
9fd6981
-		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
9fd6981
+		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = prop_hostkey =
9fd6981
 		    compat_pkalg_proposal(ssh, options.hostkeyalgorithms);
9fd6981
 	}
9fd6981
 
9fd6981
@@ -383,6 +383,10 @@ ssh_kex2(struct ssh *ssh, char *host, st
9fd6981
 	    (r = ssh_packet_write_wait(ssh)) != 0)
9fd6981
 		fatal_fr(r, "send packet");
9fd6981
 #endif
9fd6981
+	/* Free only parts of proposal that were dynamically allocated here. */
9fd6981
+	free(prop_kex);
9fd6981
+	free(prop_enc);
9fd6981
+	free(prop_hostkey);
9fd6981
 }
9fd6981
 
9fd6981
 /*
9fd6981
diff --color -rup a/sshd.c b/sshd.c
9fd6981
--- a/sshd.c	2022-07-14 17:38:43.242496568 +0200
9fd6981
+++ b/sshd.c	2022-07-14 17:42:07.616388978 +0200
9fd6981
@@ -2493,14 +2493,15 @@ do_ssh2_kex(struct ssh *ssh)
9fd6981
 {
9fd6981
 	char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
9fd6981
 	struct kex *kex;
9fd6981
+	char *hostkey_types = NULL;
9fd6981
+	char *prop_kex = NULL, *prop_enc = NULL, *prop_hostkey = NULL;
9fd6981
 	int r;
9fd6981
 
9fd6981
-	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(ssh,
9fd6981
+	myproposal[PROPOSAL_KEX_ALGS] = prop_kex = compat_kex_proposal(ssh,
9fd6981
 	    options.kex_algorithms);
9fd6981
-	myproposal[PROPOSAL_ENC_ALGS_CTOS] = compat_cipher_proposal(ssh,
9fd6981
-	    options.ciphers);
9fd6981
-	myproposal[PROPOSAL_ENC_ALGS_STOC] = compat_cipher_proposal(ssh,
9fd6981
-	    options.ciphers);
9fd6981
+	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
9fd6981
+	    myproposal[PROPOSAL_ENC_ALGS_STOC] = prop_enc =
9fd6981
+	    compat_cipher_proposal(ssh, options.ciphers);
9fd6981
 	myproposal[PROPOSAL_MAC_ALGS_CTOS] =
9fd6981
 	    myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
9fd6981
 
9fd6981
@@ -2513,8 +2514,10 @@ do_ssh2_kex(struct ssh *ssh)
9fd6981
 		ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
9fd6981
 		    options.rekey_interval);
9fd6981
 
9fd6981
-	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = compat_pkalg_proposal(
9fd6981
-	    ssh, list_hostkey_types());
9fd6981
+	hostkey_types = list_hostkey_types();
9fd6981
+	myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = prop_hostkey =
9fd6981
+	    compat_pkalg_proposal(ssh, hostkey_types);
9fd6981
+	free(hostkey_types);
9fd6981
 
9fd6981
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
9fd6981
 	{
9fd6981
@@ -2606,6 +2609,9 @@ do_ssh2_kex(struct ssh *ssh)
9fd6981
 	    (r = ssh_packet_write_wait(ssh)) != 0)
9fd6981
 		fatal_fr(r, "send test");
9fd6981
 #endif
9fd6981
+	free(prop_kex);
9fd6981
+	free(prop_enc);
9fd6981
+	free(prop_hostkey);
9fd6981
 	debug("KEX done");
9fd6981
 }
9fd6981