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

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