wok rev 20131

wpa_supplicant: security fix for KRACK
author Richard Dunbar <mojo@slitaz.org>
date Sat Oct 28 02:35:58 2017 -0400 (2017-10-28)
parents f20ce099cf6d
children 29d39c79090b
files wpa_supplicant/receipt wpa_supplicant/stuff/rebased-v2.6-0001-hostapd-Avoid-key-reinstallation-in-FT-handshake.patch wpa_supplicant/stuff/rebased-v2.6-0002-Prevent-reinstallation-of-an-already-in-use-group-ke.patch wpa_supplicant/stuff/rebased-v2.6-0003-Extend-protection-of-GTK-IGTK-reinstallation-of-WNM-.patch wpa_supplicant/stuff/rebased-v2.6-0004-Prevent-installation-of-an-all-zero-TK.patch wpa_supplicant/stuff/rebased-v2.6-0005-Fix-PTK-rekeying-to-generate-a-new-ANonce.patch wpa_supplicant/stuff/rebased-v2.6-0006-TDLS-Reject-TPK-TK-reconfiguration.patch wpa_supplicant/stuff/rebased-v2.6-0007-WNM-Ignore-WNM-Sleep-Mode-Response-without-pending-r.patch wpa_supplicant/stuff/rebased-v2.6-0008-FT-Do-not-allow-multiple-Reassociation-Response-fram.patch
line diff
     1.1 --- a/wpa_supplicant/receipt	Fri Oct 27 12:29:47 2017 -0400
     1.2 +++ b/wpa_supplicant/receipt	Sat Oct 28 02:35:58 2017 -0400
     1.3 @@ -1,7 +1,7 @@
     1.4  # SliTaz package receipt.
     1.5  
     1.6  PACKAGE="wpa_supplicant"
     1.7 -VERSION="2.1"
     1.8 +VERSION="2.6"
     1.9  CATEGORY="utilities"
    1.10  SHORT_DESC="WPA Supplicant with support for WPA and WPA2"
    1.11  MAINTAINER="0dddba11@googlemail.com"
    1.12 @@ -29,7 +29,15 @@
    1.13  # Rules to configure and make the package.
    1.14  compile_rules()
    1.15  {
    1.16 -	cd $src/$PACKAGE
    1.17 +	for i in  $stuff/*.patch
    1.18 +	do
    1.19 +     		if [ -f done.$(basename $i) ]; then
    1.20 +     			continue
    1.21 +		else
    1.22 +     			patch -p1 < $i && touch done.$(basename $i) || return 1
    1.23 +		fi
    1.24 +	done
    1.25 +	cd $src/$PACKAGE 
    1.26  	cp -a ./defconfig ./.config
    1.27  	sed 's/-lnl/-lnl-3/g' -i ../src/drivers/drivers.mak
    1.28  	sed 's/-lnl-3-genl/-lnl-genl-3/g' -i ../src/drivers/drivers.mak
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0001-hostapd-Avoid-key-reinstallation-in-FT-handshake.patch	Sat Oct 28 02:35:58 2017 -0400
     2.3 @@ -0,0 +1,174 @@
     2.4 +From cf4cab804c7afd5c45505528a8d16e46163243a2 Mon Sep 17 00:00:00 2001
     2.5 +From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
     2.6 +Date: Fri, 14 Jul 2017 15:15:35 +0200
     2.7 +Subject: [PATCH 1/8] hostapd: Avoid key reinstallation in FT handshake
     2.8 +
     2.9 +Do not reinstall TK to the driver during Reassociation Response frame
    2.10 +processing if the first attempt of setting the TK succeeded. This avoids
    2.11 +issues related to clearing the TX/RX PN that could result in reusing
    2.12 +same PN values for transmitted frames (e.g., due to CCM nonce reuse and
    2.13 +also hitting replay protection on the receiver) and accepting replayed
    2.14 +frames on RX side.
    2.15 +
    2.16 +This issue was introduced by the commit
    2.17 +0e84c25434e6a1f283c7b4e62e483729085b78d2 ('FT: Fix PTK configuration in
    2.18 +authenticator') which allowed wpa_ft_install_ptk() to be called multiple
    2.19 +times with the same PTK. While the second configuration attempt is
    2.20 +needed with some drivers, it must be done only if the first attempt
    2.21 +failed.
    2.22 +
    2.23 +Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
    2.24 +---
    2.25 + src/ap/ieee802_11.c  | 16 +++++++++++++---
    2.26 + src/ap/wpa_auth.c    | 11 +++++++++++
    2.27 + src/ap/wpa_auth.h    |  3 ++-
    2.28 + src/ap/wpa_auth_ft.c | 10 ++++++++++
    2.29 + src/ap/wpa_auth_i.h  |  1 +
    2.30 + 5 files changed, 37 insertions(+), 4 deletions(-)
    2.31 +
    2.32 +diff --git a/src/ap/ieee802_11.c b/src/ap/ieee802_11.c
    2.33 +index 4e04169..333035f 100644
    2.34 +--- a/src/ap/ieee802_11.c
    2.35 ++++ b/src/ap/ieee802_11.c
    2.36 +@@ -1841,6 +1841,7 @@ static int add_associated_sta(struct hostapd_data *hapd,
    2.37 + {
    2.38 + 	struct ieee80211_ht_capabilities ht_cap;
    2.39 + 	struct ieee80211_vht_capabilities vht_cap;
    2.40 ++	int set = 1;
    2.41 + 
    2.42 + 	/*
    2.43 + 	 * Remove the STA entry to ensure the STA PS state gets cleared and
    2.44 +@@ -1848,9 +1849,18 @@ static int add_associated_sta(struct hostapd_data *hapd,
    2.45 + 	 * FT-over-the-DS, where a station re-associates back to the same AP but
    2.46 + 	 * skips the authentication flow, or if working with a driver that
    2.47 + 	 * does not support full AP client state.
    2.48 ++	 *
    2.49 ++	 * Skip this if the STA has already completed FT reassociation and the
    2.50 ++	 * TK has been configured since the TX/RX PN must not be reset to 0 for
    2.51 ++	 * the same key.
    2.52 + 	 */
    2.53 +-	if (!sta->added_unassoc)
    2.54 ++	if (!sta->added_unassoc &&
    2.55 ++	    (!(sta->flags & WLAN_STA_AUTHORIZED) ||
    2.56 ++	     !wpa_auth_sta_ft_tk_already_set(sta->wpa_sm))) {
    2.57 + 		hostapd_drv_sta_remove(hapd, sta->addr);
    2.58 ++		wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
    2.59 ++		set = 0;
    2.60 ++	}
    2.61 + 
    2.62 + #ifdef CONFIG_IEEE80211N
    2.63 + 	if (sta->flags & WLAN_STA_HT)
    2.64 +@@ -1873,11 +1883,11 @@ static int add_associated_sta(struct hostapd_data *hapd,
    2.65 + 			    sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
    2.66 + 			    sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
    2.67 + 			    sta->vht_opmode, sta->p2p_ie ? 1 : 0,
    2.68 +-			    sta->added_unassoc)) {
    2.69 ++			    set)) {
    2.70 + 		hostapd_logger(hapd, sta->addr,
    2.71 + 			       HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
    2.72 + 			       "Could not %s STA to kernel driver",
    2.73 +-			       sta->added_unassoc ? "set" : "add");
    2.74 ++			       set ? "set" : "add");
    2.75 + 
    2.76 + 		if (sta->added_unassoc) {
    2.77 + 			hostapd_drv_sta_remove(hapd, sta->addr);
    2.78 +diff --git a/src/ap/wpa_auth.c b/src/ap/wpa_auth.c
    2.79 +index 3587086..707971d 100644
    2.80 +--- a/src/ap/wpa_auth.c
    2.81 ++++ b/src/ap/wpa_auth.c
    2.82 +@@ -1745,6 +1745,9 @@ int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
    2.83 + #else /* CONFIG_IEEE80211R */
    2.84 + 		break;
    2.85 + #endif /* CONFIG_IEEE80211R */
    2.86 ++	case WPA_DRV_STA_REMOVED:
    2.87 ++		sm->tk_already_set = FALSE;
    2.88 ++		return 0;
    2.89 + 	}
    2.90 + 
    2.91 + #ifdef CONFIG_IEEE80211R
    2.92 +@@ -3250,6 +3253,14 @@ int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
    2.93 + }
    2.94 + 
    2.95 + 
    2.96 ++int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
    2.97 ++{
    2.98 ++	if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
    2.99 ++		return 0;
   2.100 ++	return sm->tk_already_set;
   2.101 ++}
   2.102 ++
   2.103 ++
   2.104 + int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
   2.105 + 			     struct rsn_pmksa_cache_entry *entry)
   2.106 + {
   2.107 +diff --git a/src/ap/wpa_auth.h b/src/ap/wpa_auth.h
   2.108 +index 0de8d97..97461b0 100644
   2.109 +--- a/src/ap/wpa_auth.h
   2.110 ++++ b/src/ap/wpa_auth.h
   2.111 +@@ -267,7 +267,7 @@ void wpa_receive(struct wpa_authenticator *wpa_auth,
   2.112 + 		 u8 *data, size_t data_len);
   2.113 + enum wpa_event {
   2.114 + 	WPA_AUTH, WPA_ASSOC, WPA_DISASSOC, WPA_DEAUTH, WPA_REAUTH,
   2.115 +-	WPA_REAUTH_EAPOL, WPA_ASSOC_FT
   2.116 ++	WPA_REAUTH_EAPOL, WPA_ASSOC_FT, WPA_DRV_STA_REMOVED
   2.117 + };
   2.118 + void wpa_remove_ptk(struct wpa_state_machine *sm);
   2.119 + int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event);
   2.120 +@@ -280,6 +280,7 @@ int wpa_auth_pairwise_set(struct wpa_state_machine *sm);
   2.121 + int wpa_auth_get_pairwise(struct wpa_state_machine *sm);
   2.122 + int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm);
   2.123 + int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm);
   2.124 ++int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm);
   2.125 + int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
   2.126 + 			     struct rsn_pmksa_cache_entry *entry);
   2.127 + struct rsn_pmksa_cache_entry *
   2.128 +diff --git a/src/ap/wpa_auth_ft.c b/src/ap/wpa_auth_ft.c
   2.129 +index 42242a5..e63b99a 100644
   2.130 +--- a/src/ap/wpa_auth_ft.c
   2.131 ++++ b/src/ap/wpa_auth_ft.c
   2.132 +@@ -780,6 +780,14 @@ void wpa_ft_install_ptk(struct wpa_state_machine *sm)
   2.133 + 		return;
   2.134 + 	}
   2.135 + 
   2.136 ++	if (sm->tk_already_set) {
   2.137 ++		/* Must avoid TK reconfiguration to prevent clearing of TX/RX
   2.138 ++		 * PN in the driver */
   2.139 ++		wpa_printf(MSG_DEBUG,
   2.140 ++			   "FT: Do not re-install same PTK to the driver");
   2.141 ++		return;
   2.142 ++	}
   2.143 ++
   2.144 + 	/* FIX: add STA entry to kernel/driver here? The set_key will fail
   2.145 + 	 * most likely without this.. At the moment, STA entry is added only
   2.146 + 	 * after association has been completed. This function will be called
   2.147 +@@ -792,6 +800,7 @@ void wpa_ft_install_ptk(struct wpa_state_machine *sm)
   2.148 + 
   2.149 + 	/* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
   2.150 + 	sm->pairwise_set = TRUE;
   2.151 ++	sm->tk_already_set = TRUE;
   2.152 + }
   2.153 + 
   2.154 + 
   2.155 +@@ -898,6 +907,7 @@ static int wpa_ft_process_auth_req(struct wpa_state_machine *sm,
   2.156 + 
   2.157 + 	sm->pairwise = pairwise;
   2.158 + 	sm->PTK_valid = TRUE;
   2.159 ++	sm->tk_already_set = FALSE;
   2.160 + 	wpa_ft_install_ptk(sm);
   2.161 + 
   2.162 + 	buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
   2.163 +diff --git a/src/ap/wpa_auth_i.h b/src/ap/wpa_auth_i.h
   2.164 +index 72b7eb3..7fd8f05 100644
   2.165 +--- a/src/ap/wpa_auth_i.h
   2.166 ++++ b/src/ap/wpa_auth_i.h
   2.167 +@@ -65,6 +65,7 @@ struct wpa_state_machine {
   2.168 + 	struct wpa_ptk PTK;
   2.169 + 	Boolean PTK_valid;
   2.170 + 	Boolean pairwise_set;
   2.171 ++	Boolean tk_already_set;
   2.172 + 	int keycount;
   2.173 + 	Boolean Pair;
   2.174 + 	struct wpa_key_replay_counter {
   2.175 +-- 
   2.176 +2.7.4
   2.177 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0002-Prevent-reinstallation-of-an-already-in-use-group-ke.patch	Sat Oct 28 02:35:58 2017 -0400
     3.3 @@ -0,0 +1,250 @@
     3.4 +From 927f891007c402fefd1ff384645b3f07597c3ede Mon Sep 17 00:00:00 2001
     3.5 +From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
     3.6 +Date: Wed, 12 Jul 2017 16:03:24 +0200
     3.7 +Subject: [PATCH 2/8] Prevent reinstallation of an already in-use group key
     3.8 +
     3.9 +Track the current GTK and IGTK that is in use and when receiving a
    3.10 +(possibly retransmitted) Group Message 1 or WNM-Sleep Mode Response, do
    3.11 +not install the given key if it is already in use. This prevents an
    3.12 +attacker from trying to trick the client into resetting or lowering the
    3.13 +sequence counter associated to the group key.
    3.14 +
    3.15 +Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
    3.16 +---
    3.17 + src/common/wpa_common.h |  11 +++++
    3.18 + src/rsn_supp/wpa.c      | 116 ++++++++++++++++++++++++++++++------------------
    3.19 + src/rsn_supp/wpa_i.h    |   4 ++
    3.20 + 3 files changed, 87 insertions(+), 44 deletions(-)
    3.21 +
    3.22 +diff --git a/src/common/wpa_common.h b/src/common/wpa_common.h
    3.23 +index af1d0f0..d200285 100644
    3.24 +--- a/src/common/wpa_common.h
    3.25 ++++ b/src/common/wpa_common.h
    3.26 +@@ -217,6 +217,17 @@ struct wpa_ptk {
    3.27 + 	size_t tk_len;
    3.28 + };
    3.29 + 
    3.30 ++struct wpa_gtk {
    3.31 ++	u8 gtk[WPA_GTK_MAX_LEN];
    3.32 ++	size_t gtk_len;
    3.33 ++};
    3.34 ++
    3.35 ++#ifdef CONFIG_IEEE80211W
    3.36 ++struct wpa_igtk {
    3.37 ++	u8 igtk[WPA_IGTK_MAX_LEN];
    3.38 ++	size_t igtk_len;
    3.39 ++};
    3.40 ++#endif /* CONFIG_IEEE80211W */
    3.41 + 
    3.42 + /* WPA IE version 1
    3.43 +  * 00-50-f2:1 (OUI:OUI type)
    3.44 +diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
    3.45 +index 3c47879..95bd7be 100644
    3.46 +--- a/src/rsn_supp/wpa.c
    3.47 ++++ b/src/rsn_supp/wpa.c
    3.48 +@@ -714,6 +714,15 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
    3.49 + 	const u8 *_gtk = gd->gtk;
    3.50 + 	u8 gtk_buf[32];
    3.51 + 
    3.52 ++	/* Detect possible key reinstallation */
    3.53 ++	if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
    3.54 ++	    os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
    3.55 ++		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    3.56 ++			"WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
    3.57 ++			gd->keyidx, gd->tx, gd->gtk_len);
    3.58 ++		return 0;
    3.59 ++	}
    3.60 ++
    3.61 + 	wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
    3.62 + 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    3.63 + 		"WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
    3.64 +@@ -748,6 +757,9 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
    3.65 + 	}
    3.66 + 	os_memset(gtk_buf, 0, sizeof(gtk_buf));
    3.67 + 
    3.68 ++	sm->gtk.gtk_len = gd->gtk_len;
    3.69 ++	os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
    3.70 ++
    3.71 + 	return 0;
    3.72 + }
    3.73 + 
    3.74 +@@ -854,6 +866,48 @@ static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
    3.75 + }
    3.76 + 
    3.77 + 
    3.78 ++#ifdef CONFIG_IEEE80211W
    3.79 ++static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
    3.80 ++				       const struct wpa_igtk_kde *igtk)
    3.81 ++{
    3.82 ++	size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
    3.83 ++	u16 keyidx = WPA_GET_LE16(igtk->keyid);
    3.84 ++
    3.85 ++	/* Detect possible key reinstallation */
    3.86 ++	if (sm->igtk.igtk_len == len &&
    3.87 ++	    os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
    3.88 ++		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    3.89 ++			"WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
    3.90 ++			keyidx);
    3.91 ++		return  0;
    3.92 ++	}
    3.93 ++
    3.94 ++	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    3.95 ++		"WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
    3.96 ++		keyidx, MAC2STR(igtk->pn));
    3.97 ++	wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
    3.98 ++	if (keyidx > 4095) {
    3.99 ++		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   3.100 ++			"WPA: Invalid IGTK KeyID %d", keyidx);
   3.101 ++		return -1;
   3.102 ++	}
   3.103 ++	if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
   3.104 ++			   broadcast_ether_addr,
   3.105 ++			   keyidx, 0, igtk->pn, sizeof(igtk->pn),
   3.106 ++			   igtk->igtk, len) < 0) {
   3.107 ++		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   3.108 ++			"WPA: Failed to configure IGTK to the driver");
   3.109 ++		return -1;
   3.110 ++	}
   3.111 ++
   3.112 ++	sm->igtk.igtk_len = len;
   3.113 ++	os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
   3.114 ++
   3.115 ++	return 0;
   3.116 ++}
   3.117 ++#endif /* CONFIG_IEEE80211W */
   3.118 ++
   3.119 ++
   3.120 + static int ieee80211w_set_keys(struct wpa_sm *sm,
   3.121 + 			       struct wpa_eapol_ie_parse *ie)
   3.122 + {
   3.123 +@@ -864,30 +918,14 @@ static int ieee80211w_set_keys(struct wpa_sm *sm,
   3.124 + 	if (ie->igtk) {
   3.125 + 		size_t len;
   3.126 + 		const struct wpa_igtk_kde *igtk;
   3.127 +-		u16 keyidx;
   3.128 ++
   3.129 + 		len = wpa_cipher_key_len(sm->mgmt_group_cipher);
   3.130 + 		if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
   3.131 + 			return -1;
   3.132 ++
   3.133 + 		igtk = (const struct wpa_igtk_kde *) ie->igtk;
   3.134 +-		keyidx = WPA_GET_LE16(igtk->keyid);
   3.135 +-		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
   3.136 +-			"pn %02x%02x%02x%02x%02x%02x",
   3.137 +-			keyidx, MAC2STR(igtk->pn));
   3.138 +-		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
   3.139 +-				igtk->igtk, len);
   3.140 +-		if (keyidx > 4095) {
   3.141 +-			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   3.142 +-				"WPA: Invalid IGTK KeyID %d", keyidx);
   3.143 +-			return -1;
   3.144 +-		}
   3.145 +-		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
   3.146 +-				   broadcast_ether_addr,
   3.147 +-				   keyidx, 0, igtk->pn, sizeof(igtk->pn),
   3.148 +-				   igtk->igtk, len) < 0) {
   3.149 +-			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   3.150 +-				"WPA: Failed to configure IGTK to the driver");
   3.151 ++		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
   3.152 + 			return -1;
   3.153 +-		}
   3.154 + 	}
   3.155 + 
   3.156 + 	return 0;
   3.157 +@@ -2307,7 +2345,7 @@ void wpa_sm_deinit(struct wpa_sm *sm)
   3.158 +  */
   3.159 + void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
   3.160 + {
   3.161 +-	int clear_ptk = 1;
   3.162 ++	int clear_keys = 1;
   3.163 + 
   3.164 + 	if (sm == NULL)
   3.165 + 		return;
   3.166 +@@ -2333,11 +2371,11 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
   3.167 + 		/* Prepare for the next transition */
   3.168 + 		wpa_ft_prepare_auth_request(sm, NULL);
   3.169 + 
   3.170 +-		clear_ptk = 0;
   3.171 ++		clear_keys = 0;
   3.172 + 	}
   3.173 + #endif /* CONFIG_IEEE80211R */
   3.174 + 
   3.175 +-	if (clear_ptk) {
   3.176 ++	if (clear_keys) {
   3.177 + 		/*
   3.178 + 		 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
   3.179 + 		 * this is not part of a Fast BSS Transition.
   3.180 +@@ -2347,6 +2385,10 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
   3.181 + 		os_memset(&sm->ptk, 0, sizeof(sm->ptk));
   3.182 + 		sm->tptk_set = 0;
   3.183 + 		os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   3.184 ++		os_memset(&sm->gtk, 0, sizeof(sm->gtk));
   3.185 ++#ifdef CONFIG_IEEE80211W
   3.186 ++		os_memset(&sm->igtk, 0, sizeof(sm->igtk));
   3.187 ++#endif /* CONFIG_IEEE80211W */
   3.188 + 	}
   3.189 + 
   3.190 + #ifdef CONFIG_TDLS
   3.191 +@@ -2877,6 +2919,10 @@ void wpa_sm_drop_sa(struct wpa_sm *sm)
   3.192 + 	os_memset(sm->pmk, 0, sizeof(sm->pmk));
   3.193 + 	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
   3.194 + 	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   3.195 ++	os_memset(&sm->gtk, 0, sizeof(sm->gtk));
   3.196 ++#ifdef CONFIG_IEEE80211W
   3.197 ++	os_memset(&sm->igtk, 0, sizeof(sm->igtk));
   3.198 ++#endif /* CONFIG_IEEE80211W */
   3.199 + #ifdef CONFIG_IEEE80211R
   3.200 + 	os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
   3.201 + 	os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
   3.202 +@@ -2949,29 +2995,11 @@ int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
   3.203 + 		os_memset(&gd, 0, sizeof(gd));
   3.204 + #ifdef CONFIG_IEEE80211W
   3.205 + 	} else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
   3.206 +-		struct wpa_igtk_kde igd;
   3.207 +-		u16 keyidx;
   3.208 +-
   3.209 +-		os_memset(&igd, 0, sizeof(igd));
   3.210 +-		keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
   3.211 +-		os_memcpy(igd.keyid, buf + 2, 2);
   3.212 +-		os_memcpy(igd.pn, buf + 4, 6);
   3.213 +-
   3.214 +-		keyidx = WPA_GET_LE16(igd.keyid);
   3.215 +-		os_memcpy(igd.igtk, buf + 10, keylen);
   3.216 +-
   3.217 +-		wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
   3.218 +-				igd.igtk, keylen);
   3.219 +-		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
   3.220 +-				   broadcast_ether_addr,
   3.221 +-				   keyidx, 0, igd.pn, sizeof(igd.pn),
   3.222 +-				   igd.igtk, keylen) < 0) {
   3.223 +-			wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
   3.224 +-				   "WNM mode");
   3.225 +-			os_memset(&igd, 0, sizeof(igd));
   3.226 ++		const struct wpa_igtk_kde *igtk;
   3.227 ++
   3.228 ++		igtk = (const struct wpa_igtk_kde *) (buf + 2);
   3.229 ++		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
   3.230 + 			return -1;
   3.231 +-		}
   3.232 +-		os_memset(&igd, 0, sizeof(igd));
   3.233 + #endif /* CONFIG_IEEE80211W */
   3.234 + 	} else {
   3.235 + 		wpa_printf(MSG_DEBUG, "Unknown element id");
   3.236 +diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
   3.237 +index f653ba6..afc9e37 100644
   3.238 +--- a/src/rsn_supp/wpa_i.h
   3.239 ++++ b/src/rsn_supp/wpa_i.h
   3.240 +@@ -31,6 +31,10 @@ struct wpa_sm {
   3.241 + 	u8 rx_replay_counter[WPA_REPLAY_COUNTER_LEN];
   3.242 + 	int rx_replay_counter_set;
   3.243 + 	u8 request_counter[WPA_REPLAY_COUNTER_LEN];
   3.244 ++	struct wpa_gtk gtk;
   3.245 ++#ifdef CONFIG_IEEE80211W
   3.246 ++	struct wpa_igtk igtk;
   3.247 ++#endif /* CONFIG_IEEE80211W */
   3.248 + 
   3.249 + 	struct eapol_sm *eapol; /* EAPOL state machine from upper level code */
   3.250 + 
   3.251 +-- 
   3.252 +2.7.4
   3.253 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0003-Extend-protection-of-GTK-IGTK-reinstallation-of-WNM-.patch	Sat Oct 28 02:35:58 2017 -0400
     4.3 @@ -0,0 +1,184 @@
     4.4 +From 8280294e74846ea342389a0cd17215050fa5afe8 Mon Sep 17 00:00:00 2001
     4.5 +From: Jouni Malinen <j@w1.fi>
     4.6 +Date: Sun, 1 Oct 2017 12:12:24 +0300
     4.7 +Subject: [PATCH 3/8] Extend protection of GTK/IGTK reinstallation of WNM-Sleep
     4.8 + Mode cases
     4.9 +
    4.10 +This extends the protection to track last configured GTK/IGTK value
    4.11 +separately from EAPOL-Key frames and WNM-Sleep Mode frames to cover a
    4.12 +corner case where these two different mechanisms may get used when the
    4.13 +GTK/IGTK has changed and tracking a single value is not sufficient to
    4.14 +detect a possible key reconfiguration.
    4.15 +
    4.16 +Signed-off-by: Jouni Malinen <j@w1.fi>
    4.17 +---
    4.18 + src/rsn_supp/wpa.c   | 53 +++++++++++++++++++++++++++++++++++++---------------
    4.19 + src/rsn_supp/wpa_i.h |  2 ++
    4.20 + 2 files changed, 40 insertions(+), 15 deletions(-)
    4.21 +
    4.22 +diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
    4.23 +index 95bd7be..7a2c68d 100644
    4.24 +--- a/src/rsn_supp/wpa.c
    4.25 ++++ b/src/rsn_supp/wpa.c
    4.26 +@@ -709,14 +709,17 @@ struct wpa_gtk_data {
    4.27 + 
    4.28 + static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
    4.29 + 				      const struct wpa_gtk_data *gd,
    4.30 +-				      const u8 *key_rsc)
    4.31 ++				      const u8 *key_rsc, int wnm_sleep)
    4.32 + {
    4.33 + 	const u8 *_gtk = gd->gtk;
    4.34 + 	u8 gtk_buf[32];
    4.35 + 
    4.36 + 	/* Detect possible key reinstallation */
    4.37 +-	if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
    4.38 +-	    os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
    4.39 ++	if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
    4.40 ++	     os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
    4.41 ++	    (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
    4.42 ++	     os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
    4.43 ++		       sm->gtk_wnm_sleep.gtk_len) == 0)) {
    4.44 + 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    4.45 + 			"WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
    4.46 + 			gd->keyidx, gd->tx, gd->gtk_len);
    4.47 +@@ -757,8 +760,14 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
    4.48 + 	}
    4.49 + 	os_memset(gtk_buf, 0, sizeof(gtk_buf));
    4.50 + 
    4.51 +-	sm->gtk.gtk_len = gd->gtk_len;
    4.52 +-	os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
    4.53 ++	if (wnm_sleep) {
    4.54 ++		sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
    4.55 ++		os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
    4.56 ++			  sm->gtk_wnm_sleep.gtk_len);
    4.57 ++	} else {
    4.58 ++		sm->gtk.gtk_len = gd->gtk_len;
    4.59 ++		os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
    4.60 ++	}
    4.61 + 
    4.62 + 	return 0;
    4.63 + }
    4.64 +@@ -852,7 +861,7 @@ static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
    4.65 + 	    (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
    4.66 + 					       gtk_len, gtk_len,
    4.67 + 					       &gd.key_rsc_len, &gd.alg) ||
    4.68 +-	     wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
    4.69 ++	     wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
    4.70 + 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    4.71 + 			"RSN: Failed to install GTK");
    4.72 + 		os_memset(&gd, 0, sizeof(gd));
    4.73 +@@ -868,14 +877,18 @@ static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
    4.74 + 
    4.75 + #ifdef CONFIG_IEEE80211W
    4.76 + static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
    4.77 +-				       const struct wpa_igtk_kde *igtk)
    4.78 ++				       const struct wpa_igtk_kde *igtk,
    4.79 ++				       int wnm_sleep)
    4.80 + {
    4.81 + 	size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
    4.82 + 	u16 keyidx = WPA_GET_LE16(igtk->keyid);
    4.83 + 
    4.84 + 	/* Detect possible key reinstallation */
    4.85 +-	if (sm->igtk.igtk_len == len &&
    4.86 +-	    os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
    4.87 ++	if ((sm->igtk.igtk_len == len &&
    4.88 ++	     os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
    4.89 ++	    (sm->igtk_wnm_sleep.igtk_len == len &&
    4.90 ++	     os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
    4.91 ++		       sm->igtk_wnm_sleep.igtk_len) == 0)) {
    4.92 + 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    4.93 + 			"WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
    4.94 + 			keyidx);
    4.95 +@@ -900,8 +913,14 @@ static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
    4.96 + 		return -1;
    4.97 + 	}
    4.98 + 
    4.99 +-	sm->igtk.igtk_len = len;
   4.100 +-	os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
   4.101 ++	if (wnm_sleep) {
   4.102 ++		sm->igtk_wnm_sleep.igtk_len = len;
   4.103 ++		os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
   4.104 ++			  sm->igtk_wnm_sleep.igtk_len);
   4.105 ++	} else {
   4.106 ++		sm->igtk.igtk_len = len;
   4.107 ++		os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
   4.108 ++	}
   4.109 + 
   4.110 + 	return 0;
   4.111 + }
   4.112 +@@ -924,7 +943,7 @@ static int ieee80211w_set_keys(struct wpa_sm *sm,
   4.113 + 			return -1;
   4.114 + 
   4.115 + 		igtk = (const struct wpa_igtk_kde *) ie->igtk;
   4.116 +-		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
   4.117 ++		if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
   4.118 + 			return -1;
   4.119 + 	}
   4.120 + 
   4.121 +@@ -1574,7 +1593,7 @@ static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
   4.122 + 	if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
   4.123 + 		key_rsc = null_rsc;
   4.124 + 
   4.125 +-	if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
   4.126 ++	if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
   4.127 + 	    wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
   4.128 + 		goto failed;
   4.129 + 	os_memset(&gd, 0, sizeof(gd));
   4.130 +@@ -2386,8 +2405,10 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
   4.131 + 		sm->tptk_set = 0;
   4.132 + 		os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   4.133 + 		os_memset(&sm->gtk, 0, sizeof(sm->gtk));
   4.134 ++		os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
   4.135 + #ifdef CONFIG_IEEE80211W
   4.136 + 		os_memset(&sm->igtk, 0, sizeof(sm->igtk));
   4.137 ++		os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
   4.138 + #endif /* CONFIG_IEEE80211W */
   4.139 + 	}
   4.140 + 
   4.141 +@@ -2920,8 +2941,10 @@ void wpa_sm_drop_sa(struct wpa_sm *sm)
   4.142 + 	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
   4.143 + 	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   4.144 + 	os_memset(&sm->gtk, 0, sizeof(sm->gtk));
   4.145 ++	os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
   4.146 + #ifdef CONFIG_IEEE80211W
   4.147 + 	os_memset(&sm->igtk, 0, sizeof(sm->igtk));
   4.148 ++	os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
   4.149 + #endif /* CONFIG_IEEE80211W */
   4.150 + #ifdef CONFIG_IEEE80211R
   4.151 + 	os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
   4.152 +@@ -2986,7 +3009,7 @@ int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
   4.153 + 
   4.154 + 		wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
   4.155 + 				gd.gtk, gd.gtk_len);
   4.156 +-		if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
   4.157 ++		if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
   4.158 + 			os_memset(&gd, 0, sizeof(gd));
   4.159 + 			wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
   4.160 + 				   "WNM mode");
   4.161 +@@ -2998,7 +3021,7 @@ int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
   4.162 + 		const struct wpa_igtk_kde *igtk;
   4.163 + 
   4.164 + 		igtk = (const struct wpa_igtk_kde *) (buf + 2);
   4.165 +-		if (wpa_supplicant_install_igtk(sm, igtk) < 0)
   4.166 ++		if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
   4.167 + 			return -1;
   4.168 + #endif /* CONFIG_IEEE80211W */
   4.169 + 	} else {
   4.170 +diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
   4.171 +index afc9e37..9a54631 100644
   4.172 +--- a/src/rsn_supp/wpa_i.h
   4.173 ++++ b/src/rsn_supp/wpa_i.h
   4.174 +@@ -32,8 +32,10 @@ struct wpa_sm {
   4.175 + 	int rx_replay_counter_set;
   4.176 + 	u8 request_counter[WPA_REPLAY_COUNTER_LEN];
   4.177 + 	struct wpa_gtk gtk;
   4.178 ++	struct wpa_gtk gtk_wnm_sleep;
   4.179 + #ifdef CONFIG_IEEE80211W
   4.180 + 	struct wpa_igtk igtk;
   4.181 ++	struct wpa_igtk igtk_wnm_sleep;
   4.182 + #endif /* CONFIG_IEEE80211W */
   4.183 + 
   4.184 + 	struct eapol_sm *eapol; /* EAPOL state machine from upper level code */
   4.185 +-- 
   4.186 +2.7.4
   4.187 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0004-Prevent-installation-of-an-all-zero-TK.patch	Sat Oct 28 02:35:58 2017 -0400
     5.3 @@ -0,0 +1,79 @@
     5.4 +From 8f82bc94e8697a9d47fa8774dfdaaede1084912c Mon Sep 17 00:00:00 2001
     5.5 +From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
     5.6 +Date: Fri, 29 Sep 2017 04:22:51 +0200
     5.7 +Subject: [PATCH 4/8] Prevent installation of an all-zero TK
     5.8 +
     5.9 +Properly track whether a PTK has already been installed to the driver
    5.10 +and the TK part cleared from memory. This prevents an attacker from
    5.11 +trying to trick the client into installing an all-zero TK.
    5.12 +
    5.13 +This fixes the earlier fix in commit
    5.14 +ad00d64e7d8827b3cebd665a0ceb08adabf15e1e ('Fix TK configuration to the
    5.15 +driver in EAPOL-Key 3/4 retry case') which did not take into account
    5.16 +possibility of an extra message 1/4 showing up between retries of
    5.17 +message 3/4.
    5.18 +
    5.19 +Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
    5.20 +---
    5.21 + src/common/wpa_common.h | 1 +
    5.22 + src/rsn_supp/wpa.c      | 5 ++---
    5.23 + src/rsn_supp/wpa_i.h    | 1 -
    5.24 + 3 files changed, 3 insertions(+), 4 deletions(-)
    5.25 +
    5.26 +diff --git a/src/common/wpa_common.h b/src/common/wpa_common.h
    5.27 +index d200285..1021ccb 100644
    5.28 +--- a/src/common/wpa_common.h
    5.29 ++++ b/src/common/wpa_common.h
    5.30 +@@ -215,6 +215,7 @@ struct wpa_ptk {
    5.31 + 	size_t kck_len;
    5.32 + 	size_t kek_len;
    5.33 + 	size_t tk_len;
    5.34 ++	int installed; /* 1 if key has already been installed to driver */
    5.35 + };
    5.36 + 
    5.37 + struct wpa_gtk {
    5.38 +diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
    5.39 +index 7a2c68d..0550a41 100644
    5.40 +--- a/src/rsn_supp/wpa.c
    5.41 ++++ b/src/rsn_supp/wpa.c
    5.42 +@@ -510,7 +510,6 @@ static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
    5.43 + 		os_memset(buf, 0, sizeof(buf));
    5.44 + 	}
    5.45 + 	sm->tptk_set = 1;
    5.46 +-	sm->tk_to_set = 1;
    5.47 + 
    5.48 + 	kde = sm->assoc_wpa_ie;
    5.49 + 	kde_len = sm->assoc_wpa_ie_len;
    5.50 +@@ -615,7 +614,7 @@ static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
    5.51 + 	enum wpa_alg alg;
    5.52 + 	const u8 *key_rsc;
    5.53 + 
    5.54 +-	if (!sm->tk_to_set) {
    5.55 ++	if (sm->ptk.installed) {
    5.56 + 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    5.57 + 			"WPA: Do not re-install same PTK to the driver");
    5.58 + 		return 0;
    5.59 +@@ -659,7 +658,7 @@ static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
    5.60 + 
    5.61 + 	/* TK is not needed anymore in supplicant */
    5.62 + 	os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
    5.63 +-	sm->tk_to_set = 0;
    5.64 ++	sm->ptk.installed = 1;
    5.65 + 
    5.66 + 	if (sm->wpa_ptk_rekey) {
    5.67 + 		eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
    5.68 +diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
    5.69 +index 9a54631..41f371f 100644
    5.70 +--- a/src/rsn_supp/wpa_i.h
    5.71 ++++ b/src/rsn_supp/wpa_i.h
    5.72 +@@ -24,7 +24,6 @@ struct wpa_sm {
    5.73 + 	struct wpa_ptk ptk, tptk;
    5.74 + 	int ptk_set, tptk_set;
    5.75 + 	unsigned int msg_3_of_4_ok:1;
    5.76 +-	unsigned int tk_to_set:1;
    5.77 + 	u8 snonce[WPA_NONCE_LEN];
    5.78 + 	u8 anonce[WPA_NONCE_LEN]; /* ANonce from the last 1/4 msg */
    5.79 + 	int renew_snonce;
    5.80 +-- 
    5.81 +2.7.4
    5.82 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0005-Fix-PTK-rekeying-to-generate-a-new-ANonce.patch	Sat Oct 28 02:35:58 2017 -0400
     6.3 @@ -0,0 +1,64 @@
     6.4 +From 12fac09b437a1dc8a0f253e265934a8aaf4d2f8b Mon Sep 17 00:00:00 2001
     6.5 +From: Jouni Malinen <j@w1.fi>
     6.6 +Date: Sun, 1 Oct 2017 12:32:57 +0300
     6.7 +Subject: [PATCH 5/8] Fix PTK rekeying to generate a new ANonce
     6.8 +
     6.9 +The Authenticator state machine path for PTK rekeying ended up bypassing
    6.10 +the AUTHENTICATION2 state where a new ANonce is generated when going
    6.11 +directly to the PTKSTART state since there is no need to try to
    6.12 +determine the PMK again in such a case. This is far from ideal since the
    6.13 +new PTK would depend on a new nonce only from the supplicant.
    6.14 +
    6.15 +Fix this by generating a new ANonce when moving to the PTKSTART state
    6.16 +for the purpose of starting new 4-way handshake to rekey PTK.
    6.17 +
    6.18 +Signed-off-by: Jouni Malinen <j@w1.fi>
    6.19 +---
    6.20 + src/ap/wpa_auth.c | 24 +++++++++++++++++++++---
    6.21 + 1 file changed, 21 insertions(+), 3 deletions(-)
    6.22 +
    6.23 +diff --git a/src/ap/wpa_auth.c b/src/ap/wpa_auth.c
    6.24 +index 707971d..bf10cc1 100644
    6.25 +--- a/src/ap/wpa_auth.c
    6.26 ++++ b/src/ap/wpa_auth.c
    6.27 +@@ -1901,6 +1901,21 @@ SM_STATE(WPA_PTK, AUTHENTICATION2)
    6.28 + }
    6.29 + 
    6.30 + 
    6.31 ++static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
    6.32 ++{
    6.33 ++	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
    6.34 ++		wpa_printf(MSG_ERROR,
    6.35 ++			   "WPA: Failed to get random data for ANonce");
    6.36 ++		sm->Disconnect = TRUE;
    6.37 ++		return -1;
    6.38 ++	}
    6.39 ++	wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
    6.40 ++		    WPA_NONCE_LEN);
    6.41 ++	sm->TimeoutCtr = 0;
    6.42 ++	return 0;
    6.43 ++}
    6.44 ++
    6.45 ++
    6.46 + SM_STATE(WPA_PTK, INITPMK)
    6.47 + {
    6.48 + 	u8 msk[2 * PMK_LEN];
    6.49 +@@ -2458,9 +2473,12 @@ SM_STEP(WPA_PTK)
    6.50 + 		SM_ENTER(WPA_PTK, AUTHENTICATION);
    6.51 + 	else if (sm->ReAuthenticationRequest)
    6.52 + 		SM_ENTER(WPA_PTK, AUTHENTICATION2);
    6.53 +-	else if (sm->PTKRequest)
    6.54 +-		SM_ENTER(WPA_PTK, PTKSTART);
    6.55 +-	else switch (sm->wpa_ptk_state) {
    6.56 ++	else if (sm->PTKRequest) {
    6.57 ++		if (wpa_auth_sm_ptk_update(sm) < 0)
    6.58 ++			SM_ENTER(WPA_PTK, DISCONNECTED);
    6.59 ++		else
    6.60 ++			SM_ENTER(WPA_PTK, PTKSTART);
    6.61 ++	} else switch (sm->wpa_ptk_state) {
    6.62 + 	case WPA_PTK_INITIALIZE:
    6.63 + 		break;
    6.64 + 	case WPA_PTK_DISCONNECT:
    6.65 +-- 
    6.66 +2.7.4
    6.67 +
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0006-TDLS-Reject-TPK-TK-reconfiguration.patch	Sat Oct 28 02:35:58 2017 -0400
     7.3 @@ -0,0 +1,132 @@
     7.4 +From 6c4bed4f47d1960ec04981a9d50e5076aea5223d Mon Sep 17 00:00:00 2001
     7.5 +From: Jouni Malinen <j@w1.fi>
     7.6 +Date: Fri, 22 Sep 2017 11:03:15 +0300
     7.7 +Subject: [PATCH 6/8] TDLS: Reject TPK-TK reconfiguration
     7.8 +
     7.9 +Do not try to reconfigure the same TPK-TK to the driver after it has
    7.10 +been successfully configured. This is an explicit check to avoid issues
    7.11 +related to resetting the TX/RX packet number. There was already a check
    7.12 +for this for TPK M2 (retries of that message are ignored completely), so
    7.13 +that behavior does not get modified.
    7.14 +
    7.15 +For TPK M3, the TPK-TK could have been reconfigured, but that was
    7.16 +followed by immediate teardown of the link due to an issue in updating
    7.17 +the STA entry. Furthermore, for TDLS with any real security (i.e.,
    7.18 +ignoring open/WEP), the TPK message exchange is protected on the AP path
    7.19 +and simple replay attacks are not feasible.
    7.20 +
    7.21 +As an additional corner case, make sure the local nonce gets updated if
    7.22 +the peer uses a very unlikely "random nonce" of all zeros.
    7.23 +
    7.24 +Signed-off-by: Jouni Malinen <j@w1.fi>
    7.25 +---
    7.26 + src/rsn_supp/tdls.c | 38 ++++++++++++++++++++++++++++++++++++--
    7.27 + 1 file changed, 36 insertions(+), 2 deletions(-)
    7.28 +
    7.29 +diff --git a/src/rsn_supp/tdls.c b/src/rsn_supp/tdls.c
    7.30 +index e424168..9eb9738 100644
    7.31 +--- a/src/rsn_supp/tdls.c
    7.32 ++++ b/src/rsn_supp/tdls.c
    7.33 +@@ -112,6 +112,7 @@ struct wpa_tdls_peer {
    7.34 + 		u8 tk[16]; /* TPK-TK; assuming only CCMP will be used */
    7.35 + 	} tpk;
    7.36 + 	int tpk_set;
    7.37 ++	int tk_set; /* TPK-TK configured to the driver */
    7.38 + 	int tpk_success;
    7.39 + 	int tpk_in_progress;
    7.40 + 
    7.41 +@@ -192,6 +193,20 @@ static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
    7.42 + 	u8 rsc[6];
    7.43 + 	enum wpa_alg alg;
    7.44 + 
    7.45 ++	if (peer->tk_set) {
    7.46 ++		/*
    7.47 ++		 * This same TPK-TK has already been configured to the driver
    7.48 ++		 * and this new configuration attempt (likely due to an
    7.49 ++		 * unexpected retransmitted frame) would result in clearing
    7.50 ++		 * the TX/RX sequence number which can break security, so must
    7.51 ++		 * not allow that to happen.
    7.52 ++		 */
    7.53 ++		wpa_printf(MSG_INFO, "TDLS: TPK-TK for the peer " MACSTR
    7.54 ++			   " has already been configured to the driver - do not reconfigure",
    7.55 ++			   MAC2STR(peer->addr));
    7.56 ++		return -1;
    7.57 ++	}
    7.58 ++
    7.59 + 	os_memset(rsc, 0, 6);
    7.60 + 
    7.61 + 	switch (peer->cipher) {
    7.62 +@@ -209,12 +224,15 @@ static int wpa_tdls_set_key(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
    7.63 + 		return -1;
    7.64 + 	}
    7.65 + 
    7.66 ++	wpa_printf(MSG_DEBUG, "TDLS: Configure pairwise key for peer " MACSTR,
    7.67 ++		   MAC2STR(peer->addr));
    7.68 + 	if (wpa_sm_set_key(sm, alg, peer->addr, -1, 1,
    7.69 + 			   rsc, sizeof(rsc), peer->tpk.tk, key_len) < 0) {
    7.70 + 		wpa_printf(MSG_WARNING, "TDLS: Failed to set TPK to the "
    7.71 + 			   "driver");
    7.72 + 		return -1;
    7.73 + 	}
    7.74 ++	peer->tk_set = 1;
    7.75 + 	return 0;
    7.76 + }
    7.77 + 
    7.78 +@@ -696,7 +714,7 @@ static void wpa_tdls_peer_clear(struct wpa_sm *sm, struct wpa_tdls_peer *peer)
    7.79 + 	peer->cipher = 0;
    7.80 + 	peer->qos_info = 0;
    7.81 + 	peer->wmm_capable = 0;
    7.82 +-	peer->tpk_set = peer->tpk_success = 0;
    7.83 ++	peer->tk_set = peer->tpk_set = peer->tpk_success = 0;
    7.84 + 	peer->chan_switch_enabled = 0;
    7.85 + 	os_memset(&peer->tpk, 0, sizeof(peer->tpk));
    7.86 + 	os_memset(peer->inonce, 0, WPA_NONCE_LEN);
    7.87 +@@ -1159,6 +1177,7 @@ skip_rsnie:
    7.88 + 		wpa_tdls_peer_free(sm, peer);
    7.89 + 		return -1;
    7.90 + 	}
    7.91 ++	peer->tk_set = 0; /* A new nonce results in a new TK */
    7.92 + 	wpa_hexdump(MSG_DEBUG, "TDLS: Initiator Nonce for TPK handshake",
    7.93 + 		    peer->inonce, WPA_NONCE_LEN);
    7.94 + 	os_memcpy(ftie->Snonce, peer->inonce, WPA_NONCE_LEN);
    7.95 +@@ -1751,6 +1770,19 @@ static int wpa_tdls_addset_peer(struct wpa_sm *sm, struct wpa_tdls_peer *peer,
    7.96 + }
    7.97 + 
    7.98 + 
    7.99 ++static int tdls_nonce_set(const u8 *nonce)
   7.100 ++{
   7.101 ++	int i;
   7.102 ++
   7.103 ++	for (i = 0; i < WPA_NONCE_LEN; i++) {
   7.104 ++		if (nonce[i])
   7.105 ++			return 1;
   7.106 ++	}
   7.107 ++
   7.108 ++	return 0;
   7.109 ++}
   7.110 ++
   7.111 ++
   7.112 + static int wpa_tdls_process_tpk_m1(struct wpa_sm *sm, const u8 *src_addr,
   7.113 + 				   const u8 *buf, size_t len)
   7.114 + {
   7.115 +@@ -2004,7 +2036,8 @@ skip_rsn:
   7.116 + 	peer->rsnie_i_len = kde.rsn_ie_len;
   7.117 + 	peer->cipher = cipher;
   7.118 + 
   7.119 +-	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0) {
   7.120 ++	if (os_memcmp(peer->inonce, ftie->Snonce, WPA_NONCE_LEN) != 0 ||
   7.121 ++	    !tdls_nonce_set(peer->inonce)) {
   7.122 + 		/*
   7.123 + 		 * There is no point in updating the RNonce for every obtained
   7.124 + 		 * TPK M1 frame (e.g., retransmission due to timeout) with the
   7.125 +@@ -2020,6 +2053,7 @@ skip_rsn:
   7.126 + 				"TDLS: Failed to get random data for responder nonce");
   7.127 + 			goto error;
   7.128 + 		}
   7.129 ++		peer->tk_set = 0; /* A new nonce results in a new TK */
   7.130 + 	}
   7.131 + 
   7.132 + #if 0
   7.133 +-- 
   7.134 +2.7.4
   7.135 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0007-WNM-Ignore-WNM-Sleep-Mode-Response-without-pending-r.patch	Sat Oct 28 02:35:58 2017 -0400
     8.3 @@ -0,0 +1,43 @@
     8.4 +From 53c5eb58e95004f86e65ee9fbfccbc291b139057 Mon Sep 17 00:00:00 2001
     8.5 +From: Jouni Malinen <j@w1.fi>
     8.6 +Date: Fri, 22 Sep 2017 11:25:02 +0300
     8.7 +Subject: [PATCH 7/8] WNM: Ignore WNM-Sleep Mode Response without pending
     8.8 + request
     8.9 +
    8.10 +Commit 03ed0a52393710be6bdae657d1b36efa146520e5 ('WNM: Ignore WNM-Sleep
    8.11 +Mode Response if WNM-Sleep Mode has not been used') started ignoring the
    8.12 +response when no WNM-Sleep Mode Request had been used during the
    8.13 +association. This can be made tighter by clearing the used flag when
    8.14 +successfully processing a response. This adds an additional layer of
    8.15 +protection against unexpected retransmissions of the response frame.
    8.16 +
    8.17 +Signed-off-by: Jouni Malinen <j@w1.fi>
    8.18 +---
    8.19 + wpa_supplicant/wnm_sta.c | 4 +++-
    8.20 + 1 file changed, 3 insertions(+), 1 deletion(-)
    8.21 +
    8.22 +diff --git a/wpa_supplicant/wnm_sta.c b/wpa_supplicant/wnm_sta.c
    8.23 +index 1b3409c..67a07ff 100644
    8.24 +--- a/wpa_supplicant/wnm_sta.c
    8.25 ++++ b/wpa_supplicant/wnm_sta.c
    8.26 +@@ -260,7 +260,7 @@ static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
    8.27 + 
    8.28 + 	if (!wpa_s->wnmsleep_used) {
    8.29 + 		wpa_printf(MSG_DEBUG,
    8.30 +-			   "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode has not been used in this association");
    8.31 ++			   "WNM: Ignore WNM-Sleep Mode Response frame since WNM-Sleep Mode operation has not been requested");
    8.32 + 		return;
    8.33 + 	}
    8.34 + 
    8.35 +@@ -299,6 +299,8 @@ static void ieee802_11_rx_wnmsleep_resp(struct wpa_supplicant *wpa_s,
    8.36 + 		return;
    8.37 + 	}
    8.38 + 
    8.39 ++	wpa_s->wnmsleep_used = 0;
    8.40 ++
    8.41 + 	if (wnmsleep_ie->status == WNM_STATUS_SLEEP_ACCEPT ||
    8.42 + 	    wnmsleep_ie->status == WNM_STATUS_SLEEP_EXIT_ACCEPT_GTK_UPDATE) {
    8.43 + 		wpa_printf(MSG_DEBUG, "Successfully recv WNM-Sleep Response "
    8.44 +-- 
    8.45 +2.7.4
    8.46 +
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/wpa_supplicant/stuff/rebased-v2.6-0008-FT-Do-not-allow-multiple-Reassociation-Response-fram.patch	Sat Oct 28 02:35:58 2017 -0400
     9.3 @@ -0,0 +1,82 @@
     9.4 +From b372ab0b7daea719749194dc554b26e6367603f2 Mon Sep 17 00:00:00 2001
     9.5 +From: Jouni Malinen <j@w1.fi>
     9.6 +Date: Fri, 22 Sep 2017 12:06:37 +0300
     9.7 +Subject: [PATCH 8/8] FT: Do not allow multiple Reassociation Response frames
     9.8 +
     9.9 +The driver is expected to not report a second association event without
    9.10 +the station having explicitly request a new association. As such, this
    9.11 +case should not be reachable. However, since reconfiguring the same
    9.12 +pairwise or group keys to the driver could result in nonce reuse issues,
    9.13 +be extra careful here and do an additional state check to avoid this
    9.14 +even if the local driver ends up somehow accepting an unexpected
    9.15 +Reassociation Response frame.
    9.16 +
    9.17 +Signed-off-by: Jouni Malinen <j@w1.fi>
    9.18 +---
    9.19 + src/rsn_supp/wpa.c    | 3 +++
    9.20 + src/rsn_supp/wpa_ft.c | 8 ++++++++
    9.21 + src/rsn_supp/wpa_i.h  | 1 +
    9.22 + 3 files changed, 12 insertions(+)
    9.23 +
    9.24 +diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
    9.25 +index 0550a41..2a53c6f 100644
    9.26 +--- a/src/rsn_supp/wpa.c
    9.27 ++++ b/src/rsn_supp/wpa.c
    9.28 +@@ -2440,6 +2440,9 @@ void wpa_sm_notify_disassoc(struct wpa_sm *sm)
    9.29 + #ifdef CONFIG_TDLS
    9.30 + 	wpa_tdls_disassoc(sm);
    9.31 + #endif /* CONFIG_TDLS */
    9.32 ++#ifdef CONFIG_IEEE80211R
    9.33 ++	sm->ft_reassoc_completed = 0;
    9.34 ++#endif /* CONFIG_IEEE80211R */
    9.35 + 
    9.36 + 	/* Keys are not needed in the WPA state machine anymore */
    9.37 + 	wpa_sm_drop_sa(sm);
    9.38 +diff --git a/src/rsn_supp/wpa_ft.c b/src/rsn_supp/wpa_ft.c
    9.39 +index 205793e..d45bb45 100644
    9.40 +--- a/src/rsn_supp/wpa_ft.c
    9.41 ++++ b/src/rsn_supp/wpa_ft.c
    9.42 +@@ -153,6 +153,7 @@ static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
    9.43 + 	u16 capab;
    9.44 + 
    9.45 + 	sm->ft_completed = 0;
    9.46 ++	sm->ft_reassoc_completed = 0;
    9.47 + 
    9.48 + 	buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
    9.49 + 		2 + sm->r0kh_id_len + ric_ies_len + 100;
    9.50 +@@ -681,6 +682,11 @@ int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
    9.51 + 		return -1;
    9.52 + 	}
    9.53 + 
    9.54 ++	if (sm->ft_reassoc_completed) {
    9.55 ++		wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
    9.56 ++		return 0;
    9.57 ++	}
    9.58 ++
    9.59 + 	if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
    9.60 + 		wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
    9.61 + 		return -1;
    9.62 +@@ -781,6 +787,8 @@ int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
    9.63 + 		return -1;
    9.64 + 	}
    9.65 + 
    9.66 ++	sm->ft_reassoc_completed = 1;
    9.67 ++
    9.68 + 	if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
    9.69 + 		return -1;
    9.70 + 
    9.71 +diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
    9.72 +index 41f371f..56f88dc 100644
    9.73 +--- a/src/rsn_supp/wpa_i.h
    9.74 ++++ b/src/rsn_supp/wpa_i.h
    9.75 +@@ -128,6 +128,7 @@ struct wpa_sm {
    9.76 + 	size_t r0kh_id_len;
    9.77 + 	u8 r1kh_id[FT_R1KH_ID_LEN];
    9.78 + 	int ft_completed;
    9.79 ++	int ft_reassoc_completed;
    9.80 + 	int over_the_ds_in_progress;
    9.81 + 	u8 target_ap[ETH_ALEN]; /* over-the-DS target AP */
    9.82 + 	int set_ptk_after_assoc;
    9.83 +-- 
    9.84 +2.7.4
    9.85 +