Blame SOURCES/rh1495527-0001-hostapd-Avoid-key-reinstallation-in-FT-handshake.patch

9c84ec
From cf4cab804c7afd5c45505528a8d16e46163243a2 Mon Sep 17 00:00:00 2001
9c84ec
From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
9c84ec
Date: Fri, 14 Jul 2017 15:15:35 +0200
9c84ec
Subject: [PATCH 1/8] hostapd: Avoid key reinstallation in FT handshake
9c84ec
9c84ec
Do not reinstall TK to the driver during Reassociation Response frame
9c84ec
processing if the first attempt of setting the TK succeeded. This avoids
9c84ec
issues related to clearing the TX/RX PN that could result in reusing
9c84ec
same PN values for transmitted frames (e.g., due to CCM nonce reuse and
9c84ec
also hitting replay protection on the receiver) and accepting replayed
9c84ec
frames on RX side.
9c84ec
9c84ec
This issue was introduced by the commit
9c84ec
0e84c25434e6a1f283c7b4e62e483729085b78d2 ('FT: Fix PTK configuration in
9c84ec
authenticator') which allowed wpa_ft_install_ptk() to be called multiple
9c84ec
times with the same PTK. While the second configuration attempt is
9c84ec
needed with some drivers, it must be done only if the first attempt
9c84ec
failed.
9c84ec
9c84ec
Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
9c84ec
---
9c84ec
 src/ap/ieee802_11.c  | 16 +++++++++++++---
9c84ec
 src/ap/wpa_auth.c    | 11 +++++++++++
9c84ec
 src/ap/wpa_auth.h    |  3 ++-
9c84ec
 src/ap/wpa_auth_ft.c | 10 ++++++++++
9c84ec
 src/ap/wpa_auth_i.h  |  1 +
9c84ec
 5 files changed, 37 insertions(+), 4 deletions(-)
9c84ec
9c84ec
diff --git a/src/ap/ieee802_11.c b/src/ap/ieee802_11.c
9c84ec
index 4e04169..333035f 100644
9c84ec
--- a/src/ap/ieee802_11.c
9c84ec
+++ b/src/ap/ieee802_11.c
9c84ec
@@ -1841,6 +1841,7 @@ static int add_associated_sta(struct hostapd_data *hapd,
9c84ec
 {
9c84ec
 	struct ieee80211_ht_capabilities ht_cap;
9c84ec
 	struct ieee80211_vht_capabilities vht_cap;
9c84ec
+	int set = 1;
9c84ec
 
9c84ec
 	/*
9c84ec
 	 * Remove the STA entry to ensure the STA PS state gets cleared and
9c84ec
@@ -1848,9 +1849,18 @@ static int add_associated_sta(struct hostapd_data *hapd,
9c84ec
 	 * FT-over-the-DS, where a station re-associates back to the same AP but
9c84ec
 	 * skips the authentication flow, or if working with a driver that
9c84ec
 	 * does not support full AP client state.
9c84ec
+	 *
9c84ec
+	 * Skip this if the STA has already completed FT reassociation and the
9c84ec
+	 * TK has been configured since the TX/RX PN must not be reset to 0 for
9c84ec
+	 * the same key.
9c84ec
 	 */
9c84ec
-	if (!sta->added_unassoc)
9c84ec
+	if (!sta->added_unassoc &&
9c84ec
+	    (!(sta->flags & WLAN_STA_AUTHORIZED) ||
9c84ec
+	     !wpa_auth_sta_ft_tk_already_set(sta->wpa_sm))) {
9c84ec
 		hostapd_drv_sta_remove(hapd, sta->addr);
9c84ec
+		wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
9c84ec
+		set = 0;
9c84ec
+	}
9c84ec
 
9c84ec
 #ifdef CONFIG_IEEE80211N
9c84ec
 	if (sta->flags & WLAN_STA_HT)
9c84ec
@@ -1873,11 +1883,11 @@ static int add_associated_sta(struct hostapd_data *hapd,
9c84ec
 			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
9c84ec
 			    sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
9c84ec
 			    sta->vht_opmode, sta->p2p_ie ? 1 : 0,
9c84ec
-			    sta->added_unassoc)) {
9c84ec
+			    set)) {
9c84ec
 		hostapd_logger(hapd, sta->addr,
9c84ec
 			       HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
9c84ec
 			       "Could not %s STA to kernel driver",
9c84ec
-			       sta->added_unassoc ? "set" : "add");
9c84ec
+			       set ? "set" : "add");
9c84ec
 
9c84ec
 		if (sta->added_unassoc) {
9c84ec
 			hostapd_drv_sta_remove(hapd, sta->addr);
9c84ec
diff --git a/src/ap/wpa_auth.c b/src/ap/wpa_auth.c
9c84ec
index 3587086..707971d 100644
9c84ec
--- a/src/ap/wpa_auth.c
9c84ec
+++ b/src/ap/wpa_auth.c
9c84ec
@@ -1745,6 +1745,9 @@ int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
9c84ec
 #else /* CONFIG_IEEE80211R */
9c84ec
 		break;
9c84ec
 #endif /* CONFIG_IEEE80211R */
9c84ec
+	case WPA_DRV_STA_REMOVED:
9c84ec
+		sm->tk_already_set = FALSE;
9c84ec
+		return 0;
9c84ec
 	}
9c84ec
 
9c84ec
 #ifdef CONFIG_IEEE80211R
9c84ec
@@ -3250,6 +3253,14 @@ int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
9c84ec
 }
9c84ec
 
9c84ec
 
9c84ec
+int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
9c84ec
+{
9c84ec
+	if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
9c84ec
+		return 0;
9c84ec
+	return sm->tk_already_set;
9c84ec
+}
9c84ec
+
9c84ec
+
9c84ec
 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
9c84ec
 			     struct rsn_pmksa_cache_entry *entry)
9c84ec
 {
9c84ec
diff --git a/src/ap/wpa_auth.h b/src/ap/wpa_auth.h
9c84ec
index 0de8d97..97461b0 100644
9c84ec
--- a/src/ap/wpa_auth.h
9c84ec
+++ b/src/ap/wpa_auth.h
9c84ec
@@ -267,7 +267,7 @@ void wpa_receive(struct wpa_authenticator *wpa_auth,
9c84ec
 		 u8 *data, size_t data_len);
9c84ec
 enum wpa_event {
9c84ec
 	WPA_AUTH, WPA_ASSOC, WPA_DISASSOC, WPA_DEAUTH, WPA_REAUTH,
9c84ec
-	WPA_REAUTH_EAPOL, WPA_ASSOC_FT
9c84ec
+	WPA_REAUTH_EAPOL, WPA_ASSOC_FT, WPA_DRV_STA_REMOVED
9c84ec
 };
9c84ec
 void wpa_remove_ptk(struct wpa_state_machine *sm);
9c84ec
 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event);
9c84ec
@@ -280,6 +280,7 @@ int wpa_auth_pairwise_set(struct wpa_state_machine *sm);
9c84ec
 int wpa_auth_get_pairwise(struct wpa_state_machine *sm);
9c84ec
 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm);
9c84ec
 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm);
9c84ec
+int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm);
9c84ec
 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
9c84ec
 			     struct rsn_pmksa_cache_entry *entry);
9c84ec
 struct rsn_pmksa_cache_entry *
9c84ec
diff --git a/src/ap/wpa_auth_ft.c b/src/ap/wpa_auth_ft.c
9c84ec
index 42242a5..e63b99a 100644
9c84ec
--- a/src/ap/wpa_auth_ft.c
9c84ec
+++ b/src/ap/wpa_auth_ft.c
9c84ec
@@ -780,6 +780,14 @@ void wpa_ft_install_ptk(struct wpa_state_machine *sm)
9c84ec
 		return;
9c84ec
 	}
9c84ec
 
9c84ec
+	if (sm->tk_already_set) {
9c84ec
+		/* Must avoid TK reconfiguration to prevent clearing of TX/RX
9c84ec
+		 * PN in the driver */
9c84ec
+		wpa_printf(MSG_DEBUG,
9c84ec
+			   "FT: Do not re-install same PTK to the driver");
9c84ec
+		return;
9c84ec
+	}
9c84ec
+
9c84ec
 	/* FIX: add STA entry to kernel/driver here? The set_key will fail
9c84ec
 	 * most likely without this.. At the moment, STA entry is added only
9c84ec
 	 * after association has been completed. This function will be called
9c84ec
@@ -792,6 +800,7 @@ void wpa_ft_install_ptk(struct wpa_state_machine *sm)
9c84ec
 
9c84ec
 	/* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
9c84ec
 	sm->pairwise_set = TRUE;
9c84ec
+	sm->tk_already_set = TRUE;
9c84ec
 }
9c84ec
 
9c84ec
 
9c84ec
@@ -898,6 +907,7 @@ static int wpa_ft_process_auth_req(struct wpa_state_machine *sm,
9c84ec
 
9c84ec
 	sm->pairwise = pairwise;
9c84ec
 	sm->PTK_valid = TRUE;
9c84ec
+	sm->tk_already_set = FALSE;
9c84ec
 	wpa_ft_install_ptk(sm);
9c84ec
 
9c84ec
 	buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
9c84ec
diff --git a/src/ap/wpa_auth_i.h b/src/ap/wpa_auth_i.h
9c84ec
index 72b7eb3..7fd8f05 100644
9c84ec
--- a/src/ap/wpa_auth_i.h
9c84ec
+++ b/src/ap/wpa_auth_i.h
9c84ec
@@ -65,6 +65,7 @@ struct wpa_state_machine {
9c84ec
 	struct wpa_ptk PTK;
9c84ec
 	Boolean PTK_valid;
9c84ec
 	Boolean pairwise_set;
9c84ec
+	Boolean tk_already_set;
9c84ec
 	int keycount;
9c84ec
 	Boolean Pair;
9c84ec
 	struct wpa_key_replay_counter {
9c84ec
-- 
9c84ec
2.7.4
9c84ec