Blob Blame History Raw
From c964eedcefb52eadcc7b30178378ed7d07b3510f Mon Sep 17 00:00:00 2001
From: Daiki Ueno <dueno@redhat.com>
Date: Tue, 18 Apr 2023 10:43:30 +0900
Subject: [PATCH 1/2] Fix CVE-2023-30570

Signed-off-by: Daiki Ueno <dueno@redhat.com>
---
 programs/pluto/ikev1.c      | 61 +++++++++++++++++++++++++++++++++++--
 programs/pluto/ikev1_aggr.c |  5 +--
 2 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/programs/pluto/ikev1.c b/programs/pluto/ikev1.c
index 9f4847874d..fa7adba2ba 100644
--- a/programs/pluto/ikev1.c
+++ b/programs/pluto/ikev1.c
@@ -1250,10 +1250,20 @@ void process_v1_packet(struct msg_digest *md)
 	struct state *st = NULL;
 	enum state_kind from_state = STATE_UNDEFINED;   /* state we started in */
 
+	/*
+	 * For the initial responses, don't leak the responder's SPI.
+	 * Hence the use of send_v1_notification_from_md().
+	 *
+	 * AGGR mode is a mess in that the R0->R1 transition happens
+	 * well before the transition succeeds.
+	 */
 #define SEND_NOTIFICATION(t)						\
 	{								\
 		pstats(ikev1_sent_notifies_e, t);			\
-		if (st != NULL)						\
+		if (st != NULL &&					\
+		    st->st_state->kind != STATE_AGGR_R0 &&		\
+		    st->st_state->kind != STATE_AGGR_R1 &&		\
+		    st->st_state->kind != STATE_MAIN_R0)		\
 			send_notification_from_state(st, from_state, t); \
 		else							\
 			send_notification_from_md(md, t);		\
@@ -1323,17 +1333,26 @@ void process_v1_packet(struct msg_digest *md)
 			from_state = (md->hdr.isa_xchg == ISAKMP_XCHG_IDPROT ?
 				      STATE_MAIN_R0 : STATE_AGGR_R0);
 		} else {
-			/* not an initial message */
+			/*
+			 * Possibly not an initial message.  Possibly
+			 * from initiator.  Possibly from responder.
+			 *
+			 * Possibly.  Which is probably hopeless.
+			 */
 
 			st = find_state_ikev1(&md->hdr.isa_ike_spis,
 					      md->hdr.isa_msgid);
 
 			if (st == NULL) {
 				/*
-				 * perhaps this is a first message
+				 * Perhaps this is a first message
 				 * from the responder and contains a
 				 * responder cookie that we've not yet
 				 * seen.
+				 *
+				 * Perhaps this is a random message
+				 * with a bogus non-zero responder IKE
+				 * SPI.
 				 */
 				st = find_state_ikev1_init(&md->hdr.isa_ike_initiator_spi,
 							   md->hdr.isa_msgid);
@@ -1344,6 +1363,21 @@ void process_v1_packet(struct msg_digest *md)
 					/* XXX Could send notification back */
 					return;
 				}
+				if (st->st_state->kind == STATE_AGGR_R0) {
+					/*
+					 * The only way for this to
+					 * happen is for the attacker
+					 * to guess the responder's
+					 * IKE SPI that hasn't been
+					 * sent over the wire?
+					 *
+					 * Well that or played 1/2^32
+					 * odds.
+					 */
+					llog_pexpect(md->md_logger, HERE,
+						     "phase 1 message matching AGGR_R0 state");
+					return;
+				}
 			}
 			from_state = st->st_state->kind;
 		}
@@ -3052,7 +3086,28 @@ void complete_v1_state_transition(struct state *st, struct msg_digest *md, stf_s
 			delete_state(st);
 			/* wipe out dangling pointer to st */
 			md->v1_st = NULL;
+		} else if  (st->st_state->kind == STATE_AGGR_R0 ||
+			    st->st_state->kind == STATE_AGGR_R1 ||
+			    st->st_state->kind == STATE_MAIN_R0) {
+			/*
+			 *
+			 * Wipe out the incomplete larval state.
+			 *
+			 * ARGH! In <=v4.10, the aggr code flipped the
+			 * larval state to R1 right at the start of
+			 * the transition and not the end, so using
+			 * state to figure things out is close to
+			 * useless.
+			 *
+			 * Deleting the state means that pluto has no
+			 * way to detect and ignore amplification
+			 * attacks.
+			 */
+			delete_state(st);
+			/* wipe out dangling pointer to st */
+			md->v1_st = NULL;
 		}
+
 		break;
 	}
 	}
diff --git a/programs/pluto/ikev1_aggr.c b/programs/pluto/ikev1_aggr.c
index 8ad4678e1f..47a1eda424 100644
--- a/programs/pluto/ikev1_aggr.c
+++ b/programs/pluto/ikev1_aggr.c
@@ -176,7 +176,7 @@ stf_status aggr_inI1_outR1(struct state *unused_st UNUSED,
 	struct ike_sa *ike = new_v1_rstate(c, md);
 	struct state *st = &ike->sa;
 	md->v1_st = st;  /* (caller will reset cur_state) */
-	change_state(st, STATE_AGGR_R1);
+	change_state(st, STATE_AGGR_R0);
 
 	/* warn for especially dangerous Aggressive Mode and PSK */
 	if (LIN(POLICY_PSK, c->policy) && LIN(POLICY_AGGRESSIVE, c->policy)) {
@@ -193,7 +193,8 @@ stf_status aggr_inI1_outR1(struct state *unused_st UNUSED,
 
 	if (!v1_decode_certs(md)) {
 		log_state(RC_LOG, st, "X509: CERT payload bogus or revoked");
-		return false;
+		/* XXX notification is in order! */
+		return STF_FAIL + INVALID_ID_INFORMATION;
 	}
 
 	/*
-- 
2.39.2


From 1d8b49905b374af294f23dad2e165931ca081d16 Mon Sep 17 00:00:00 2001
From: Andrew Cagney <cagney@gnu.org>
Date: Sat, 25 Mar 2023 19:40:52 -0400
Subject: [PATCH 2/2] ikev1: add --impair
 copy_v1_notify_response_SPIs_to_retransmission

---
 include/impair.h       |  2 ++
 lib/libswan/impair.c   |  6 ++++++
 programs/pluto/ikev1.c | 10 ++++++++++
 3 files changed, 18 insertions(+)

diff --git a/include/impair.h b/include/impair.h
index 15fa2a2dc3..26900146e0 100644
--- a/include/impair.h
+++ b/include/impair.h
@@ -153,6 +153,8 @@ struct impair {
 	bool omit_v2_ike_auth_child;
 	bool ignore_v2_ike_auth_child;
 
+	bool copy_v1_notify_response_SPIs_to_retransmission;
+
 	/*
 	 * add more here
 	 */
diff --git a/lib/libswan/impair.c b/lib/libswan/impair.c
index 404ca96f1a..5d139410ad 100644
--- a/lib/libswan/impair.c
+++ b/lib/libswan/impair.c
@@ -97,6 +97,8 @@ struct impairment impairments[] = {
 
 #define A(WHAT, ACTION, PARAM, HELP, UNSIGNED_HELP, ...) { .what = WHAT, .action = CALL_##ACTION, .param = PARAM, .help = HELP, .unsigned_help = UNSIGNED_HELP, ##__VA_ARGS__, }
 #define V(WHAT, VALUE, HELP, ...) { .what = WHAT, .action = CALL_IMPAIR_UPDATE, .value = &impair.VALUE, .help = HELP, .sizeof_value = sizeof(impair.VALUE), ##__VA_ARGS__, }
+#define B(VALUE, HELP, ...) \
+	{ .what = #VALUE, .action = CALL_IMPAIR_UPDATE, .value = &impair.VALUE, .help = HELP, .sizeof_value = sizeof(impair.VALUE), ##__VA_ARGS__, }
 
 	V("allow-dns-insecure", allow_dns_insecure, "allow IPSECKEY lookups without DNSSEC protection"),
 	V("allow-null-none", allow_null_none, "cause pluto to allow esp=null-none and ah=none for testing"),
@@ -198,6 +200,10 @@ struct impairment impairments[] = {
 	A("event-sa-replace", STATE_EVENT_HANDLER, EVENT_SA_REPLACE,
 	  "trigger the replace event", "SA"),
 
+	B(copy_v1_notify_response_SPIs_to_retransmission,
+	  "copy SPIs in IKEv1 notify response to last sent packet and then retransmit"),
+
+#undef B
 #undef V
 #undef A
 
diff --git a/programs/pluto/ikev1.c b/programs/pluto/ikev1.c
index fa7adba2ba..b822995f1f 100644
--- a/programs/pluto/ikev1.c
+++ b/programs/pluto/ikev1.c
@@ -2398,6 +2398,16 @@ void process_packet_tail(struct msg_digest *md)
 					    enum_show(& ikev1_notify_names,
 						      p->payload.notification.isan_type, &b));
 				} else {
+					if (impair.copy_v1_notify_response_SPIs_to_retransmission) {
+						dbg("IMPAIR: copying notify response SPIs to recorded message and then resending it");
+						/* skip non-ESP marker if needed */
+						size_t skip = (st->st_interface->esp_encapsulation_enabled ? NON_ESP_MARKER_SIZE : 0);
+						size_t spis = sizeof(md->hdr.isa_ike_spis);
+						PASSERT(st->st_logger, st->st_v1_tpacket.len >= skip + spis);
+						memcpy(st->st_v1_tpacket.ptr + skip, &md->hdr.isa_ike_spis, spis);
+						resend_recorded_v1_ike_msg(st, "IMPAIR: retransmitting mangled packet");
+					}
+
 					esb_buf b;
 					LOG_PACKET(RC_LOG_SERIOUS,
 						   "ignoring informational payload %s, msgid=%08" PRIx32 ", length=%d",
-- 
2.39.2