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