Blame SOURCES/libreswan-4.3-labeled-ipsec.patch

1e8c82
diff -Naur libreswan-4.3-orig/programs/pluto/connections.c libreswan-4.3/programs/pluto/connections.c
1e8c82
--- libreswan-4.3-orig/programs/pluto/connections.c	2021-02-21 12:03:03.000000000 -0500
1e8c82
+++ libreswan-4.3/programs/pluto/connections.c	2021-02-24 16:28:05.608119041 -0500
1e8c82
@@ -2475,9 +2475,8 @@
1e8c82
 			    endpoint_in_selector(local_client, &sr->this.client) &&
1e8c82
 			    endpoint_in_selector(remote_client, &sr->that.client)
1e8c82
 #ifdef HAVE_LABELED_IPSEC
1e8c82
-			    && ((sec_label.ptr == NULL &&
1e8c82
-			      sr->this.sec_label.ptr == NULL) ||
1e8c82
-			     /* don't call with NULL, it confuses it */
1e8c82
+			    && ((sec_label.ptr == NULL && sr->this.sec_label.ptr == NULL) ||
1e8c82
+			     hunk_eq(sec_label, sr->this.sec_label) || 
1e8c82
 			     within_range((const char *)sec_label.ptr,
1e8c82
 					  (const char *)sr->this.sec_label.ptr, logger))
1e8c82
 #endif
1e8c82
diff -Naur libreswan-4.3-orig/programs/pluto/ikev1_spdb_struct.c libreswan-4.3/programs/pluto/ikev1_spdb_struct.c
1e8c82
--- libreswan-4.3-orig/programs/pluto/ikev1_spdb_struct.c	2021-02-21 12:03:03.000000000 -0500
1e8c82
+++ libreswan-4.3/programs/pluto/ikev1_spdb_struct.c	2021-02-24 16:28:59.819791102 -0500
1e8c82
@@ -113,7 +113,9 @@
1e8c82
 		return false;
1e8c82
 	}
1e8c82
 
1e8c82
-	if (!within_range(sec_label.ptr, /* we ensured NUL termination above */
1e8c82
+
1e8c82
+	 if (!hunk_eq(sec_label, c->spd.this.sec_label) &&
1e8c82
+               !within_range(sec_label.ptr, /* we ensured NUL termination above */
1e8c82
 			  (const char *)c->spd.this.sec_label.ptr,  /* we ensured NUL termination earlier? */
1e8c82
 			  st->st_logger)) {
1e8c82
 		LLOG_JAMBUF(RC_LOG_SERIOUS, st->st_logger, buf) {
1e8c82
diff -Naur libreswan-4.3-orig/programs/pluto/ikev2_ts.c libreswan-4.3/programs/pluto/ikev2_ts.c
1e8c82
--- libreswan-4.3-orig/programs/pluto/ikev2_ts.c	2021-02-21 12:03:03.000000000 -0500
1e8c82
+++ libreswan-4.3/programs/pluto/ikev2_ts.c	2021-02-24 16:30:19.639780631 -0500
1e8c82
@@ -862,7 +862,8 @@
1e8c82
 }
1e8c82
 
1e8c82
 #ifdef HAVE_LABELED_IPSEC
1e8c82
-static bool score_ends_seclabel(const struct ends *ends,
1e8c82
+static bool score_ends_seclabel(const chunk_t **selected_sec_label,
1e8c82
+				const struct ends *ends,
1e8c82
 				const struct connection *d,
1e8c82
 				const struct traffic_selectors *tsi,
1e8c82
 				const struct traffic_selectors *tsr,
1e8c82
@@ -875,6 +876,10 @@
1e8c82
 	bool match_i = false;
1e8c82
 	bool match_r = false;
1e8c82
 
1e8c82
+	if (selected_sec_label != NULL) {
1e8c82
+		*selected_sec_label = NULL;
1e8c82
+	}
1e8c82
+
1e8c82
 	for (unsigned tsi_n = 0; tsi_n < tsi->nr; tsi_n++) {
1e8c82
 		const struct traffic_selector *cur = &tsi->ts[tsi_n];
1e8c82
 		if (cur->ts_type == IKEv2_TS_SECLABEL) {
1e8c82
@@ -883,7 +888,8 @@
1e8c82
 				// complain loudly
1e8c82
 				continue;
1e8c82
 			} else {
1e8c82
-				if (within_range((const char *)cur->sec_label.ptr, (const char *)d->spd.this.sec_label.ptr, logger)) {
1e8c82
+				if (hunk_eq(cur->sec_label, d->spd.this.sec_label) ||
1e8c82
+				    within_range((const char *)cur->sec_label.ptr, (const char *)d->spd.this.sec_label.ptr, logger)) {
1e8c82
 					match_i = true;
1e8c82
 					dbg("ikev2ts #1: received label within range of our security label");
1e8c82
 				} else {
1e8c82
@@ -902,9 +908,13 @@
1e8c82
 						dbg("IKEv2_TS_SECLABEL but zero length cur->sec_label");
1e8c82
 						continue;
1e8c82
 					} else {
1e8c82
-						if (within_range((const char *)ends->r->sec_label.ptr, (const char *)d->spd.this.sec_label.ptr, logger)) {
1e8c82
+						if (hunk_eq(ends->r->sec_label, d->spd.this.sec_label) ||
1e8c82
+						    within_range((const char *)ends->r->sec_label.ptr, (const char *)d->spd.this.sec_label.ptr, logger)) {
1e8c82
 							dbg("ikev2ts #2: received label within range of our security label");
1e8c82
 							match_r = true;
1e8c82
+							if (selected_sec_label != NULL) {
1e8c82
+								*selected_sec_label = &cur->sec_label;
1e8c82
+							}
1e8c82
 						} else {
1e8c82
 							dbg("ikev2ts #2: received label not within range of our security label");
1e8c82
 							DBG_dump_hunk("ends->r->sec_label", ends->r->sec_label);
1e8c82
@@ -926,7 +936,8 @@
1e8c82
 	return require_label == recv_label_i && match_i && match_r;
1e8c82
 }
1e8c82
 #else
1e8c82
-static bool score_ends_seclabel(const struct ends *ends UNUSED,
1e8c82
+static bool score_ends_seclabel(const chunk_t **selected_sec_label,
1e8c82
+				const struct ends *ends UNUSED,
1e8c82
 				const struct connection *d UNUSED,
1e8c82
 				const struct traffic_selectors *tsi UNUSED,
1e8c82
 				const struct traffic_selectors *tsr UNUSED,
1e8c82
@@ -1030,6 +1041,7 @@
1e8c82
 	struct best_score best_score = NO_SCORE;
1e8c82
 	const struct spd_route *best_spd_route = NULL;
1e8c82
 	struct connection *best_connection = c;
1e8c82
+	const chunk_t *best_sec_label = NULL;
1e8c82
 
1e8c82
 	/* find best spd in c */
1e8c82
 
1e8c82
@@ -1042,7 +1054,8 @@
1e8c82
 			.r = &sra->this,
1e8c82
 		};
1e8c82
 
1e8c82
-		if (!score_ends_seclabel(&ends, c, &tsi, &tsr, child->sa.st_logger)) {
1e8c82
+		const chunk_t* selected_sec_label = NULL;
1e8c82
+		if (!score_ends_seclabel(&selected_sec_label, &ends, c, &tsi, &tsr, child->sa.st_logger)) {
1e8c82
 			continue;
1e8c82
 		}
1e8c82
 
1e8c82
@@ -1060,6 +1073,7 @@
1e8c82
 			    score.tsi - tsi.ts, score.tsr - tsr.ts);
1e8c82
 			best_score = score;
1e8c82
 			best_spd_route = sra;
1e8c82
+			best_sec_label = selected_sec_label;
1e8c82
 			passert(best_connection == c);
1e8c82
 		}
1e8c82
 	}
1e8c82
@@ -1143,7 +1157,8 @@
1e8c82
 					? END_NARROWER_THAN_TS
1e8c82
 					: END_EQUALS_TS;
1e8c82
 
1e8c82
-				if (!score_ends_seclabel(&ends, d, &tsi, &tsr,
1e8c82
+				const chunk_t* selected_sec_label = NULL;
1e8c82
+				if (!score_ends_seclabel(&selected_sec_label, &ends, d, &tsi, &tsr,
1e8c82
 					    child->sa.st_logger))
1e8c82
 					continue;
1e8c82
 
1e8c82
@@ -1159,6 +1174,7 @@
1e8c82
 					best_connection = d;
1e8c82
 					best_score = score;
1e8c82
 					best_spd_route = sr;
1e8c82
+					best_sec_label = selected_sec_label;
1e8c82
 				}
1e8c82
 			}
1e8c82
 		}
1e8c82
@@ -1389,6 +1405,13 @@
1e8c82
 	 */
1e8c82
 	update_state_connection(&child->sa, best_connection);
1e8c82
 
1e8c82
+	if (best_sec_label != NULL) {
1e8c82
+		if (child->sa.st_seen_sec_label.len != 0) {
1e8c82
+			free_chunk_content(&child->sa.st_seen_sec_label);
1e8c82
+		}
1e8c82
+		child->sa.st_seen_sec_label = clone_hunk(*best_sec_label, "st_seen_sec_label");
1e8c82
+	}
1e8c82
+
1e8c82
 	child->sa.st_ts_this = ikev2_end_to_ts(&best_spd_route->this, child->sa.st_acquired_sec_label);
1e8c82
 	child->sa.st_ts_that = ikev2_end_to_ts(&best_spd_route->that, child->sa.st_seen_sec_label);
1e8c82
 
1e8c82
@@ -1424,7 +1447,8 @@
1e8c82
 		? END_WIDER_THAN_TS
1e8c82
 		: END_EQUALS_TS;
1e8c82
 
1e8c82
-	if (!score_ends_seclabel(&e, c, &tsi, &tsr, child->sa.st_logger))
1e8c82
+	const chunk_t *selected_sec_label = NULL;
1e8c82
+	if (!score_ends_seclabel(&selected_sec_label, &e, c, &tsi, &tsr, child->sa.st_logger))
1e8c82
 		return false;
1e8c82
 
1e8c82
 	struct best_score best = score_ends_iprange(initiator_widening, c, &e, &tsi, &tsr;;
1e8c82
@@ -1435,6 +1459,13 @@
1e8c82
 		return false;
1e8c82
 	}
1e8c82
 
1e8c82
+	if (selected_sec_label != NULL) {
1e8c82
+		if (child->sa.st_seen_sec_label.len != 0) {
1e8c82
+			free_chunk_content(&child->sa.st_seen_sec_label);
1e8c82
+		}
1e8c82
+		child->sa.st_seen_sec_label = clone_hunk(*selected_sec_label, "st_seen_sec_label");
1e8c82
+	}
1e8c82
+
1e8c82
 	/* XXX: check conversions */
1e8c82
 	dbg("initiator saving acceptable TSi response in this");
1e8c82
 	ts_to_end(best.tsi, &c->spd.this, &child->sa.st_ts_this);
1e8c82
@@ -1489,7 +1520,7 @@
1e8c82
 
1e8c82
 	enum fit fitness = END_NARROWER_THAN_TS;
1e8c82
 
1e8c82
-	if (!score_ends_seclabel(&ends, c, &their_tsis, &their_tsrs,
1e8c82
+	if (!score_ends_seclabel(NULL, &ends, c, &their_tsis, &their_tsrs,
1e8c82
 				 child->sa.st_logger)) {
1e8c82
 		log_state(RC_LOG_SERIOUS, &child->sa,
1e8c82
 			  "rekey: received Traffic Selectors mismatch configured selectors for Security Label");
1e8c82
diff -Naur libreswan-4.3-orig/programs/pluto/ikev2_parent.c libreswan-4.3/programs/pluto/ikev2_parent.c
1e8c82
--- libreswan-4.3-orig/programs/pluto/ikev2_parent.c	2021-02-21 12:03:03.000000000 -0500
1e8c82
+++ libreswan-4.3/programs/pluto/ikev2_parent.c	2021-03-01 10:31:49.667207958 -0500
1e8c82
@@ -5943,8 +5943,6 @@
1e8c82
 			 * from a policy we gave the kernel, so it _should_ be within our range?
1e8c82
 			 */
1e8c82
 			child->sa.st_acquired_sec_label = clone_hunk(p->sec_label, "st_acquired_sec_label");
1e8c82
-			c->spd.this.sec_label = clone_hunk(p->sec_label, "updated conn label");
1e8c82
-			c->spd.that.sec_label = clone_hunk(p->sec_label, "updated conn label");
1e8c82
 		}
1e8c82
 
1e8c82
 	} else {