1 From cf62cadcadc68377d72e2238a0f06b21c0777f90 Mon Sep 17 00:00:00 2001
2 From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
3 Date: Wed, 12 Jul 2017 16:03:24 +0200
4 Subject: [PATCH 2/8] Prevent reinstallation of an already in-use group key
6 Track the current GTK and IGTK that is in use and when receiving a
7 (possibly retransmitted) Group Message 1 or WNM-Sleep Mode Response, do
8 not install the given key if it is already in use. This prevents an
9 attacker from trying to trick the client into resetting or lowering the
10 sequence counter associated to the group key.
12 Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
14 src/common/wpa_common.h | 11 +++++
15 src/rsn_supp/wpa.c | 118 ++++++++++++++++++++++++++++++------------------
16 src/rsn_supp/wpa_i.h | 4 ++
17 3 files changed, 88 insertions(+), 45 deletions(-)
19 diff --git a/src/common/wpa_common.h b/src/common/wpa_common.h
20 index cc8edf8..0872b12 100644
21 --- a/src/common/wpa_common.h
22 +++ b/src/common/wpa_common.h
23 @@ -221,6 +221,17 @@ struct wpa_ptk {
28 + u8 gtk[WPA_GTK_MAX_LEN];
32 +#ifdef CONFIG_IEEE80211W
34 + u8 igtk[WPA_IGTK_MAX_LEN];
37 +#endif /* CONFIG_IEEE80211W */
40 * 00-50-f2:1 (OUI:OUI type)
41 diff --git a/src/rsn_supp/wpa.c b/src/rsn_supp/wpa.c
42 index 739689d..5e5fb2a 100644
43 --- a/src/rsn_supp/wpa.c
44 +++ b/src/rsn_supp/wpa.c
45 @@ -800,6 +800,15 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
46 const u8 *_gtk = gd->gtk;
49 + /* Detect possible key reinstallation */
50 + if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
51 + os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
52 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
53 + "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
54 + gd->keyidx, gd->tx, gd->gtk_len);
58 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
59 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
60 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
61 @@ -834,6 +843,9 @@ static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
63 os_memset(gtk_buf, 0, sizeof(gtk_buf));
65 + sm->gtk.gtk_len = gd->gtk_len;
66 + os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
71 @@ -940,6 +952,48 @@ static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
75 +#ifdef CONFIG_IEEE80211W
76 +static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
77 + const struct wpa_igtk_kde *igtk)
79 + size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
80 + u16 keyidx = WPA_GET_LE16(igtk->keyid);
82 + /* Detect possible key reinstallation */
83 + if (sm->igtk.igtk_len == len &&
84 + os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
85 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
86 + "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
91 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
92 + "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
93 + keyidx, MAC2STR(igtk->pn));
94 + wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
95 + if (keyidx > 4095) {
96 + wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
97 + "WPA: Invalid IGTK KeyID %d", keyidx);
100 + if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
101 + broadcast_ether_addr,
102 + keyidx, 0, igtk->pn, sizeof(igtk->pn),
103 + igtk->igtk, len) < 0) {
104 + wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
105 + "WPA: Failed to configure IGTK to the driver");
109 + sm->igtk.igtk_len = len;
110 + os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
114 +#endif /* CONFIG_IEEE80211W */
117 static int ieee80211w_set_keys(struct wpa_sm *sm,
118 struct wpa_eapol_ie_parse *ie)
120 @@ -950,30 +1004,14 @@ static int ieee80211w_set_keys(struct wpa_sm *sm,
123 const struct wpa_igtk_kde *igtk;
126 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
127 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
130 igtk = (const struct wpa_igtk_kde *) ie->igtk;
131 - keyidx = WPA_GET_LE16(igtk->keyid);
132 - wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
133 - "pn %02x%02x%02x%02x%02x%02x",
134 - keyidx, MAC2STR(igtk->pn));
135 - wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
137 - if (keyidx > 4095) {
138 - wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
139 - "WPA: Invalid IGTK KeyID %d", keyidx);
142 - if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
143 - broadcast_ether_addr,
144 - keyidx, 0, igtk->pn, sizeof(igtk->pn),
145 - igtk->igtk, len) < 0) {
146 - wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
147 - "WPA: Failed to configure IGTK to the driver");
148 + if (wpa_supplicant_install_igtk(sm, igtk) < 0)
154 @@ -2491,7 +2529,7 @@ void wpa_sm_deinit(struct wpa_sm *sm)
156 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
159 + int clear_keys = 1;
163 @@ -2517,7 +2555,7 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
164 /* Prepare for the next transition */
165 wpa_ft_prepare_auth_request(sm, NULL);
170 #endif /* CONFIG_IEEE80211R */
172 @@ -2527,11 +2565,11 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
173 * AUTHENTICATED state to get the EAPOL port Authorized.
175 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
179 #endif /* CONFIG_FILS */
184 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
185 * this is not part of a Fast BSS Transition.
186 @@ -2541,6 +2579,10 @@ void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
187 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
189 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
190 + os_memset(&sm->gtk, 0, sizeof(sm->gtk));
191 +#ifdef CONFIG_IEEE80211W
192 + os_memset(&sm->igtk, 0, sizeof(sm->igtk));
193 +#endif /* CONFIG_IEEE80211W */
197 @@ -3117,6 +3159,10 @@ void wpa_sm_drop_sa(struct wpa_sm *sm)
198 os_memset(sm->pmk, 0, sizeof(sm->pmk));
199 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
200 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
201 + os_memset(&sm->gtk, 0, sizeof(sm->gtk));
202 +#ifdef CONFIG_IEEE80211W
203 + os_memset(&sm->igtk, 0, sizeof(sm->igtk));
204 +#endif /* CONFIG_IEEE80211W */
205 #ifdef CONFIG_IEEE80211R
206 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
207 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
208 @@ -3189,29 +3235,11 @@ int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
209 os_memset(&gd, 0, sizeof(gd));
210 #ifdef CONFIG_IEEE80211W
211 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
212 - struct wpa_igtk_kde igd;
215 - os_memset(&igd, 0, sizeof(igd));
216 - keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
217 - os_memcpy(igd.keyid, buf + 2, 2);
218 - os_memcpy(igd.pn, buf + 4, 6);
220 - keyidx = WPA_GET_LE16(igd.keyid);
221 - os_memcpy(igd.igtk, buf + 10, keylen);
223 - wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
225 - if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
226 - broadcast_ether_addr,
227 - keyidx, 0, igd.pn, sizeof(igd.pn),
228 - igd.igtk, keylen) < 0) {
229 - wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
231 - os_memset(&igd, 0, sizeof(igd));
232 + const struct wpa_igtk_kde *igtk;
234 + igtk = (const struct wpa_igtk_kde *) (buf + 2);
235 + if (wpa_supplicant_install_igtk(sm, igtk) < 0)
238 - os_memset(&igd, 0, sizeof(igd));
239 #endif /* CONFIG_IEEE80211W */
241 wpa_printf(MSG_DEBUG, "Unknown element id");
242 diff --git a/src/rsn_supp/wpa_i.h b/src/rsn_supp/wpa_i.h
243 index 82e1941..2827ed6 100644
244 --- a/src/rsn_supp/wpa_i.h
245 +++ b/src/rsn_supp/wpa_i.h
246 @@ -31,6 +31,10 @@ struct wpa_sm {
247 u8 rx_replay_counter[WPA_REPLAY_COUNTER_LEN];
248 int rx_replay_counter_set;
249 u8 request_counter[WPA_REPLAY_COUNTER_LEN];
250 + struct wpa_gtk gtk;
251 +#ifdef CONFIG_IEEE80211W
252 + struct wpa_igtk igtk;
253 +#endif /* CONFIG_IEEE80211W */
255 struct eapol_sm *eapol; /* EAPOL state machine from upper level code */