005-hostapd-Avoid-key-reinstallation-in-FT-handshake.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. From: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
  2. Date: Fri, 14 Jul 2017 15:15:35 +0200
  3. Subject: [PATCH] hostapd: Avoid key reinstallation in FT handshake
  4. Do not reinstall TK to the driver during Reassociation Response frame
  5. processing if the first attempt of setting the TK succeeded. This avoids
  6. issues related to clearing the TX/RX PN that could result in reusing
  7. same PN values for transmitted frames (e.g., due to CCM nonce reuse and
  8. also hitting replay protection on the receiver) and accepting replayed
  9. frames on RX side.
  10. This issue was introduced by the commit
  11. 0e84c25434e6a1f283c7b4e62e483729085b78d2 ('FT: Fix PTK configuration in
  12. authenticator') which allowed wpa_ft_install_ptk() to be called multiple
  13. times with the same PTK. While the second configuration attempt is
  14. needed with some drivers, it must be done only if the first attempt
  15. failed.
  16. Signed-off-by: Mathy Vanhoef <Mathy.Vanhoef@cs.kuleuven.be>
  17. ---
  18. --- a/src/ap/ieee802_11.c
  19. +++ b/src/ap/ieee802_11.c
  20. @@ -2154,6 +2154,7 @@ static int add_associated_sta(struct hos
  21. {
  22. struct ieee80211_ht_capabilities ht_cap;
  23. struct ieee80211_vht_capabilities vht_cap;
  24. + int set = 1;
  25. /*
  26. * Remove the STA entry to ensure the STA PS state gets cleared and
  27. @@ -2161,9 +2162,18 @@ static int add_associated_sta(struct hos
  28. * FT-over-the-DS, where a station re-associates back to the same AP but
  29. * skips the authentication flow, or if working with a driver that
  30. * does not support full AP client state.
  31. + *
  32. + * Skip this if the STA has already completed FT reassociation and the
  33. + * TK has been configured since the TX/RX PN must not be reset to 0 for
  34. + * the same key.
  35. */
  36. - if (!sta->added_unassoc)
  37. + if (!sta->added_unassoc &&
  38. + (!(sta->flags & WLAN_STA_AUTHORIZED) ||
  39. + !wpa_auth_sta_ft_tk_already_set(sta->wpa_sm))) {
  40. hostapd_drv_sta_remove(hapd, sta->addr);
  41. + wpa_auth_sm_event(sta->wpa_sm, WPA_DRV_STA_REMOVED);
  42. + set = 0;
  43. + }
  44. #ifdef CONFIG_IEEE80211N
  45. if (sta->flags & WLAN_STA_HT)
  46. @@ -2186,11 +2196,11 @@ static int add_associated_sta(struct hos
  47. sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
  48. sta->flags | WLAN_STA_ASSOC, sta->qosinfo,
  49. sta->vht_opmode, sta->p2p_ie ? 1 : 0,
  50. - sta->added_unassoc)) {
  51. + set)) {
  52. hostapd_logger(hapd, sta->addr,
  53. HOSTAPD_MODULE_IEEE80211, HOSTAPD_LEVEL_NOTICE,
  54. "Could not %s STA to kernel driver",
  55. - sta->added_unassoc ? "set" : "add");
  56. + set ? "set" : "add");
  57. if (sta->added_unassoc) {
  58. hostapd_drv_sta_remove(hapd, sta->addr);
  59. --- a/src/ap/wpa_auth.c
  60. +++ b/src/ap/wpa_auth.c
  61. @@ -1751,6 +1751,9 @@ int wpa_auth_sm_event(struct wpa_state_m
  62. #else /* CONFIG_IEEE80211R_AP */
  63. break;
  64. #endif /* CONFIG_IEEE80211R_AP */
  65. + case WPA_DRV_STA_REMOVED:
  66. + sm->tk_already_set = FALSE;
  67. + return 0;
  68. }
  69. #ifdef CONFIG_IEEE80211R_AP
  70. @@ -3725,6 +3728,14 @@ int wpa_auth_sta_wpa_version(struct wpa_
  71. }
  72. +int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
  73. +{
  74. + if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  75. + return 0;
  76. + return sm->tk_already_set;
  77. +}
  78. +
  79. +
  80. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  81. struct rsn_pmksa_cache_entry *entry)
  82. {
  83. --- a/src/ap/wpa_auth_ft.c
  84. +++ b/src/ap/wpa_auth_ft.c
  85. @@ -794,6 +794,14 @@ void wpa_ft_install_ptk(struct wpa_state
  86. return;
  87. }
  88. + if (sm->tk_already_set) {
  89. + /* Must avoid TK reconfiguration to prevent clearing of TX/RX
  90. + * PN in the driver */
  91. + wpa_printf(MSG_DEBUG,
  92. + "FT: Do not re-install same PTK to the driver");
  93. + return;
  94. + }
  95. +
  96. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  97. * most likely without this.. At the moment, STA entry is added only
  98. * after association has been completed. This function will be called
  99. @@ -806,6 +814,7 @@ void wpa_ft_install_ptk(struct wpa_state
  100. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  101. sm->pairwise_set = TRUE;
  102. + sm->tk_already_set = TRUE;
  103. }
  104. @@ -1002,6 +1011,7 @@ static int wpa_ft_process_auth_req(struc
  105. sm->pairwise = pairwise;
  106. sm->PTK_valid = TRUE;
  107. + sm->tk_already_set = FALSE;
  108. wpa_ft_install_ptk(sm);
  109. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  110. --- a/src/ap/wpa_auth.h
  111. +++ b/src/ap/wpa_auth.h
  112. @@ -268,7 +268,7 @@ void wpa_receive(struct wpa_authenticato
  113. u8 *data, size_t data_len);
  114. enum wpa_event {
  115. WPA_AUTH, WPA_ASSOC, WPA_DISASSOC, WPA_DEAUTH, WPA_REAUTH,
  116. - WPA_REAUTH_EAPOL, WPA_ASSOC_FT
  117. + WPA_REAUTH_EAPOL, WPA_ASSOC_FT, WPA_DRV_STA_REMOVED
  118. };
  119. void wpa_remove_ptk(struct wpa_state_machine *sm);
  120. int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event);
  121. @@ -281,6 +281,7 @@ int wpa_auth_pairwise_set(struct wpa_sta
  122. int wpa_auth_get_pairwise(struct wpa_state_machine *sm);
  123. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm);
  124. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm);
  125. +int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm);
  126. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  127. struct rsn_pmksa_cache_entry *entry);
  128. struct rsn_pmksa_cache_entry *
  129. --- a/src/ap/wpa_auth_i.h
  130. +++ b/src/ap/wpa_auth_i.h
  131. @@ -65,6 +65,7 @@ struct wpa_state_machine {
  132. struct wpa_ptk PTK;
  133. Boolean PTK_valid;
  134. Boolean pairwise_set;
  135. + Boolean tk_already_set;
  136. int keycount;
  137. Boolean Pair;
  138. struct wpa_key_replay_counter {