Blame SOURCES/rh1495527-0002-Prevent-reinstallation-of-an-already-in-use-group-ke.patch

41389a
From 927f891007c402fefd1ff384645b3f07597c3ede Mon Sep 17 00:00:00 2001
41389a
From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
41389a
Date: Wed, 12 Jul 2017 16:03:24 +0200
41389a
Subject: [PATCH 2/8] Prevent reinstallation of an already in-use group key
41389a
41389a
Track the current GTK and IGTK that is in use and when receiving a
41389a
(possibly retransmitted) Group Message 1 or WNM-Sleep Mode Response, do
41389a
not install the given key if it is already in use. This prevents an
41389a
attacker from trying to trick the client into resetting or lowering the
41389a
sequence counter associated to the group key.
41389a
41389a
Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
41389a
---
41389a
 src/common/wpa_common.h |  11 +++++
41389a
 src/rsn_supp/wpa.c      | 116 ++++++++++++++++++++++++++++++------------------
41389a
 src/rsn_supp/wpa_i.h    |   4 ++
41389a
 3 files changed, 87 insertions(+), 44 deletions(-)
41389a
41389a
diff --git a/src/common/wpa_common.h b/src/common/wpa_common.h
41389a
index af1d0f0..d200285 100644
41389a
--- a/src/common/wpa_common.h
41389a
+++ b/src/common/wpa_common.h
41389a
@@ -217,6 +217,17 @@ struct wpa_ptk {
41389a
 	size_t tk_len;
41389a
 };
41389a
 
41389a
+struct wpa_gtk {
41389a
+	u8 gtk[WPA_GTK_MAX_LEN];
41389a
+	size_t gtk_len;
41389a
+};
41389a
+
41389a
+#ifdef CONFIG_IEEE80211W
41389a
+struct wpa_igtk {
41389a
+	u8 igtk[WPA_IGTK_MAX_LEN];
41389a
+	size_t igtk_len;
41389a
+};
41389a
+#endif /* CONFIG_IEEE80211W */
41389a
 
41389a
 /* WPA IE version 1
41389a
  * 00-50-f2:1 (OUI:OUI type)
41389a
diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
41389a
index 3c47879..95bd7be 100644
41389a
--- a/src/rsn_supp/wpa.c
41389a
+++ b/src/rsn_supp/wpa.c
41389a
@@ -714,6 +714,15 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
41389a
 	const u8 *_gtk = gd->gtk;
41389a
 	u8 gtk_buf[32];
41389a
 
41389a
+	/* Detect possible key reinstallation */
41389a
+	if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
41389a
+	    os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
41389a
+		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
41389a
+			"WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
41389a
+			gd->keyidx, gd->tx, gd->gtk_len);
41389a
+		return 0;
41389a
+	}
41389a
+
41389a
 	wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
41389a
 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
41389a
 		"WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
41389a
@@ -748,6 +757,9 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
41389a
 	}
41389a
 	os_memset(gtk_buf, 0, sizeof(gtk_buf));
41389a
 
41389a
+	sm->gtk.gtk_len = gd->gtk_len;
41389a
+	os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
41389a
+
41389a
 	return 0;
41389a
 }
41389a
 
41389a
@@ -854,6 +866,48 @@ static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
41389a
 }
41389a
 
41389a
 
41389a
+#ifdef CONFIG_IEEE80211W
41389a
+static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
41389a
+				       const struct wpa_igtk_kde *igtk)
41389a
+{
41389a
+	size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
41389a
+	u16 keyidx = WPA_GET_LE16(igtk->keyid);
41389a
+
41389a
+	/* Detect possible key reinstallation */
41389a
+	if (sm->igtk.igtk_len == len &&
41389a
+	    os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
41389a
+		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
41389a
+			"WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
41389a
+			keyidx);
41389a
+		return  0;
41389a
+	}
41389a
+
41389a
+	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
41389a
+		"WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
41389a
+		keyidx, MAC2STR(igtk->pn));
41389a
+	wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
41389a
+	if (keyidx > 4095) {
41389a
+		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
41389a
+			"WPA: Invalid IGTK KeyID %d", keyidx);
41389a
+		return -1;
41389a
+	}
41389a
+	if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
41389a
+			   broadcast_ether_addr,
41389a
+			   keyidx, 0, igtk->pn, sizeof(igtk->pn),
41389a
+			   igtk->igtk, len) < 0) {
41389a
+		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
41389a
+			"WPA: Failed to configure IGTK to the driver");
41389a
+		return -1;
41389a
+	}
41389a
+
41389a
+	sm->igtk.igtk_len = len;
41389a
+	os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
41389a
+
41389a
+	return 0;
41389a
+}
41389a
+#endif /* CONFIG_IEEE80211W */
41389a
+
41389a
+
41389a
 static int ieee80211w_set_keys(struct wpa_sm *sm,
41389a
 			       struct wpa_eapol_ie_parse *ie)
41389a
 {
41389a
@@ -864,30 +918,14 @@ static int ieee80211w_set_keys(struct wpa_sm *sm,
41389a
 	if (ie->igtk) {
41389a
 		size_t len;
41389a
 		const struct wpa_igtk_kde *igtk;
41389a
-		u16 keyidx;
41389a
+
41389a
 		len = wpa_cipher_key_len(sm->mgmt_group_cipher);
41389a
 		if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
41389a
 			return -1;
41389a
+
41389a
 		igtk = (const struct wpa_igtk_kde *) ie->igtk;
41389a
-		keyidx = WPA_GET_LE16(igtk->keyid);
41389a
-		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
41389a
-			"pn %02x%02x%02x%02x%02x%02x",
41389a
-			keyidx, MAC2STR(igtk->pn));
41389a
-		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
41389a
-				igtk->igtk, len);
41389a
-		if (keyidx > 4095) {
41389a
-			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
41389a
-				"WPA: Invalid IGTK KeyID %d", keyidx);
41389a
-			return -1;
41389a
-		}
41389a
-		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
41389a
-				   broadcast_ether_addr,
41389a
-				   keyidx, 0, igtk->pn, sizeof(igtk->pn),
41389a
-				   igtk->igtk, len) < 0) {
41389a
-			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
41389a
-				"WPA: Failed to configure IGTK to the driver");
41389a
+		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
41389a
 			return -1;
41389a
-		}
41389a
 	}
41389a
 
41389a
 	return 0;
41389a
@@ -2307,7 +2345,7 @@ void wpa_sm_deinit(struct wpa_sm *sm)
41389a
  */
41389a
 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
41389a
 {
41389a
-	int clear_ptk = 1;
41389a
+	int clear_keys = 1;
41389a
 
41389a
 	if (sm == NULL)
41389a
 		return;
41389a
@@ -2333,11 +2371,11 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
41389a
 		/* Prepare for the next transition */
41389a
 		wpa_ft_prepare_auth_request(sm, NULL);
41389a
 
41389a
-		clear_ptk = 0;
41389a
+		clear_keys = 0;
41389a
 	}
41389a
 #endif /* CONFIG_IEEE80211R */
41389a
 
41389a
-	if (clear_ptk) {
41389a
+	if (clear_keys) {
41389a
 		/*
41389a
 		 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
41389a
 		 * this is not part of a Fast BSS Transition.
41389a
@@ -2347,6 +2385,10 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
41389a
 		os_memset(&sm->ptk, 0, sizeof(sm->ptk));
41389a
 		sm->tptk_set = 0;
41389a
 		os_memset(&sm->tptk, 0, sizeof(sm->tptk));
41389a
+		os_memset(&sm->gtk, 0, sizeof(sm->gtk));
41389a
+#ifdef CONFIG_IEEE80211W
41389a
+		os_memset(&sm->igtk, 0, sizeof(sm->igtk));
41389a
+#endif /* CONFIG_IEEE80211W */
41389a
 	}
41389a
 
41389a
 #ifdef CONFIG_TDLS
41389a
@@ -2877,6 +2919,10 @@ void wpa_sm_drop_sa(struct wpa_sm *sm)
41389a
 	os_memset(sm->pmk, 0, sizeof(sm->pmk));
41389a
 	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
41389a
 	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
41389a
+	os_memset(&sm->gtk, 0, sizeof(sm->gtk));
41389a
+#ifdef CONFIG_IEEE80211W
41389a
+	os_memset(&sm->igtk, 0, sizeof(sm->igtk));
41389a
+#endif /* CONFIG_IEEE80211W */
41389a
 #ifdef CONFIG_IEEE80211R
41389a
 	os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
41389a
 	os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
41389a
@@ -2949,29 +2995,11 @@ int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
41389a
 		os_memset(&gd, 0, sizeof(gd));
41389a
 #ifdef CONFIG_IEEE80211W
41389a
 	} else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
41389a
-		struct wpa_igtk_kde igd;
41389a
-		u16 keyidx;
41389a
-
41389a
-		os_memset(&igd, 0, sizeof(igd));
41389a
-		keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
41389a
-		os_memcpy(igd.keyid, buf + 2, 2);
41389a
-		os_memcpy(igd.pn, buf + 4, 6);
41389a
-
41389a
-		keyidx = WPA_GET_LE16(igd.keyid);
41389a
-		os_memcpy(igd.igtk, buf + 10, keylen);
41389a
-
41389a
-		wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
41389a
-				igd.igtk, keylen);
41389a
-		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
41389a
-				   broadcast_ether_addr,
41389a
-				   keyidx, 0, igd.pn, sizeof(igd.pn),
41389a
-				   igd.igtk, keylen) < 0) {
41389a
-			wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
41389a
-				   "WNM mode");
41389a
-			os_memset(&igd, 0, sizeof(igd));
41389a
+		const struct wpa_igtk_kde *igtk;
41389a
+
41389a
+		igtk = (const struct wpa_igtk_kde *) (buf + 2);
41389a
+		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
41389a
 			return -1;
41389a
-		}
41389a
-		os_memset(&igd, 0, sizeof(igd));
41389a
 #endif /* CONFIG_IEEE80211W */
41389a
 	} else {
41389a
 		wpa_printf(MSG_DEBUG, "Unknown element id");
41389a
diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
41389a
index f653ba6..afc9e37 100644
41389a
--- a/src/rsn_supp/wpa_i.h
41389a
+++ b/src/rsn_supp/wpa_i.h
41389a
@@ -31,6 +31,10 @@ struct wpa_sm {
41389a
 	u8 rx_replay_counter[WPA_REPLAY_COUNTER_LEN];
41389a
 	int rx_replay_counter_set;
41389a
 	u8 request_counter[WPA_REPLAY_COUNTER_LEN];
41389a
+	struct wpa_gtk gtk;
41389a
+#ifdef CONFIG_IEEE80211W
41389a
+	struct wpa_igtk igtk;
41389a
+#endif /* CONFIG_IEEE80211W */
41389a
 
41389a
 	struct eapol_sm *eapol; /* EAPOL state machine from upper level code */
41389a
 
41389a
-- 
41389a
2.7.4
41389a