1 From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
2 Date: Wed, 12 Jul 2017 16:03:24 +0200
3 Subject: [PATCH] Prevent reinstallation of an already in-use group key
5 Track the current GTK and IGTK that is in use and when receiving a
6 (possibly retransmitted) Group Message 1 or WNM-Sleep Mode Response, do
7 not install the given key if it is already in use. This prevents an
8 attacker from trying to trick the client into resetting or lowering the
9 sequence counter associated to the group key.
11 Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
14 --- a/src/common/wpa_common.h
15 +++ b/src/common/wpa_common.h
16 @@ -218,6 +218,17 @@ struct wpa_ptk {
21 + u8 gtk[WPA_GTK_MAX_LEN];
25 +#ifdef CONFIG_IEEE80211W
27 + u8 igtk[WPA_IGTK_MAX_LEN];
30 +#endif /* CONFIG_IEEE80211W */
33 * 00-50-f2:1 (OUI:OUI type)
34 --- a/src/rsn_supp/wpa.c
35 +++ b/src/rsn_supp/wpa.c
36 @@ -800,6 +800,15 @@ static int wpa_supplicant_install_gtk(st
37 const u8 *_gtk = gd->gtk;
40 + /* Detect possible key reinstallation */
41 + if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
42 + os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
43 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
44 + "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
45 + gd->keyidx, gd->tx, gd->gtk_len);
49 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
50 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
51 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
52 @@ -834,6 +843,9 @@ static int wpa_supplicant_install_gtk(st
54 os_memset(gtk_buf, 0, sizeof(gtk_buf));
56 + sm->gtk.gtk_len = gd->gtk_len;
57 + os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
62 @@ -940,6 +952,48 @@ static int wpa_supplicant_pairwise_gtk(s
66 +#ifdef CONFIG_IEEE80211W
67 +static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
68 + const struct wpa_igtk_kde *igtk)
70 + size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
71 + u16 keyidx = WPA_GET_LE16(igtk->keyid);
73 + /* Detect possible key reinstallation */
74 + if (sm->igtk.igtk_len == len &&
75 + os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
76 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
77 + "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
82 + wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
83 + "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
84 + keyidx, MAC2STR(igtk->pn));
85 + wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
86 + if (keyidx > 4095) {
87 + wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
88 + "WPA: Invalid IGTK KeyID %d", keyidx);
91 + if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
92 + broadcast_ether_addr,
93 + keyidx, 0, igtk->pn, sizeof(igtk->pn),
94 + igtk->igtk, len) < 0) {
95 + wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
96 + "WPA: Failed to configure IGTK to the driver");
100 + sm->igtk.igtk_len = len;
101 + os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
105 +#endif /* CONFIG_IEEE80211W */
108 static int ieee80211w_set_keys(struct wpa_sm *sm,
109 struct wpa_eapol_ie_parse *ie)
111 @@ -950,30 +1004,14 @@ static int ieee80211w_set_keys(struct wp
114 const struct wpa_igtk_kde *igtk;
117 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
118 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
121 igtk = (const struct wpa_igtk_kde *) ie->igtk;
122 - keyidx = WPA_GET_LE16(igtk->keyid);
123 - wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
124 - "pn %02x%02x%02x%02x%02x%02x",
125 - keyidx, MAC2STR(igtk->pn));
126 - wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
128 - if (keyidx > 4095) {
129 - wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
130 - "WPA: Invalid IGTK KeyID %d", keyidx);
131 + if (wpa_supplicant_install_igtk(sm, igtk) < 0)
134 - if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
135 - broadcast_ether_addr,
136 - keyidx, 0, igtk->pn, sizeof(igtk->pn),
137 - igtk->igtk, len) < 0) {
138 - wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
139 - "WPA: Failed to configure IGTK to the driver");
145 @@ -2491,7 +2529,7 @@ void wpa_sm_deinit(struct wpa_sm *sm)
147 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
150 + int clear_keys = 1;
154 @@ -2517,7 +2555,7 @@ void wpa_sm_notify_assoc(struct wpa_sm *
155 /* Prepare for the next transition */
156 wpa_ft_prepare_auth_request(sm, NULL);
161 #endif /* CONFIG_IEEE80211R */
163 @@ -2527,11 +2565,11 @@ void wpa_sm_notify_assoc(struct wpa_sm *
164 * AUTHENTICATED state to get the EAPOL port Authorized.
166 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
170 #endif /* CONFIG_FILS */
175 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
176 * this is not part of a Fast BSS Transition.
177 @@ -2541,6 +2579,10 @@ void wpa_sm_notify_assoc(struct wpa_sm *
178 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
180 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
181 + os_memset(&sm->gtk, 0, sizeof(sm->gtk));
182 +#ifdef CONFIG_IEEE80211W
183 + os_memset(&sm->igtk, 0, sizeof(sm->igtk));
184 +#endif /* CONFIG_IEEE80211W */
188 @@ -3117,6 +3159,10 @@ void wpa_sm_drop_sa(struct wpa_sm *sm)
189 os_memset(sm->pmk, 0, sizeof(sm->pmk));
190 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
191 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
192 + os_memset(&sm->gtk, 0, sizeof(sm->gtk));
193 +#ifdef CONFIG_IEEE80211W
194 + os_memset(&sm->igtk, 0, sizeof(sm->igtk));
195 +#endif /* CONFIG_IEEE80211W */
196 #ifdef CONFIG_IEEE80211R
197 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
198 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
199 @@ -3189,29 +3235,11 @@ int wpa_wnmsleep_install_key(struct wpa_
200 os_memset(&gd, 0, sizeof(gd));
201 #ifdef CONFIG_IEEE80211W
202 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
203 - struct wpa_igtk_kde igd;
205 + const struct wpa_igtk_kde *igtk;
207 - os_memset(&igd, 0, sizeof(igd));
208 - keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
209 - os_memcpy(igd.keyid, buf + 2, 2);
210 - os_memcpy(igd.pn, buf + 4, 6);
212 - keyidx = WPA_GET_LE16(igd.keyid);
213 - os_memcpy(igd.igtk, buf + 10, keylen);
215 - wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
217 - if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
218 - broadcast_ether_addr,
219 - keyidx, 0, igd.pn, sizeof(igd.pn),
220 - igd.igtk, keylen) < 0) {
221 - wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
223 - os_memset(&igd, 0, sizeof(igd));
224 + igtk = (const struct wpa_igtk_kde *) (buf + 2);
225 + if (wpa_supplicant_install_igtk(sm, igtk) < 0)
228 - os_memset(&igd, 0, sizeof(igd));
229 #endif /* CONFIG_IEEE80211W */
231 wpa_printf(MSG_DEBUG, "Unknown element id");
232 --- a/src/rsn_supp/wpa_i.h
233 +++ b/src/rsn_supp/wpa_i.h
234 @@ -31,6 +31,10 @@ struct wpa_sm {
235 u8 rx_replay_counter[WPA_REPLAY_COUNTER_LEN];
236 int rx_replay_counter_set;
237 u8 request_counter[WPA_REPLAY_COUNTER_LEN];
238 + struct wpa_gtk gtk;
239 +#ifdef CONFIG_IEEE80211W
240 + struct wpa_igtk igtk;
241 +#endif /* CONFIG_IEEE80211W */
243 struct eapol_sm *eapol; /* EAPOL state machine from upper level code */