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