mac80211: backport upstream fixes for FragAttacks
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / subsys / 386-mac80211-check-defrag-PN-against-current-frame.patch
1 From: Johannes Berg <johannes.berg@intel.com>
2 Date: Tue, 11 May 2021 20:02:48 +0200
3 Subject: [PATCH] mac80211: check defrag PN against current frame
4
5 As pointed out by Mathy Vanhoef, we implement the RX PN check
6 on fragmented frames incorrectly - we check against the last
7 received PN prior to the new frame, rather than to the one in
8 this frame itself.
9
10 Prior patches addressed the security issue here, but in order
11 to be able to reason better about the code, fix it to really
12 compare against the current frame's PN, not the last stored
13 one.
14
15 Cc: stable@vger.kernel.org
16 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
17 ---
18
19 --- a/net/mac80211/ieee80211_i.h
20 +++ b/net/mac80211/ieee80211_i.h
21 @@ -227,8 +227,15 @@ struct ieee80211_rx_data {
22 */
23 int security_idx;
24
25 - u32 tkip_iv32;
26 - u16 tkip_iv16;
27 + union {
28 + struct {
29 + u32 iv32;
30 + u16 iv16;
31 + } tkip;
32 + struct {
33 + u8 pn[IEEE80211_CCMP_PN_LEN];
34 + } ccm_gcm;
35 + };
36 };
37
38 struct ieee80211_csa_settings {
39 --- a/net/mac80211/rx.c
40 +++ b/net/mac80211/rx.c
41 @@ -2318,7 +2318,6 @@ ieee80211_rx_h_defragment(struct ieee802
42 if (entry->check_sequential_pn) {
43 int i;
44 u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
45 - int queue;
46
47 if (!requires_sequential_pn(rx, fc))
48 return RX_DROP_UNUSABLE;
49 @@ -2333,8 +2332,8 @@ ieee80211_rx_h_defragment(struct ieee802
50 if (pn[i])
51 break;
52 }
53 - queue = rx->security_idx;
54 - rpn = rx->key->u.ccmp.rx_pn[queue];
55 +
56 + rpn = rx->ccm_gcm.pn;
57 if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
58 return RX_DROP_UNUSABLE;
59 memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
60 --- a/net/mac80211/wpa.c
61 +++ b/net/mac80211/wpa.c
62 @@ -3,6 +3,7 @@
63 * Copyright 2002-2004, Instant802 Networks, Inc.
64 * Copyright 2008, Jouni Malinen <j@w1.fi>
65 * Copyright (C) 2016-2017 Intel Deutschland GmbH
66 + * Copyright (C) 2020-2021 Intel Corporation
67 */
68
69 #include <linux/netdevice.h>
70 @@ -167,8 +168,8 @@ ieee80211_rx_h_michael_mic_verify(struct
71
72 update_iv:
73 /* update IV in key information to be able to detect replays */
74 - rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32;
75 - rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16;
76 + rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip.iv32;
77 + rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip.iv16;
78
79 return RX_CONTINUE;
80
81 @@ -294,8 +295,8 @@ ieee80211_crypto_tkip_decrypt(struct iee
82 key, skb->data + hdrlen,
83 skb->len - hdrlen, rx->sta->sta.addr,
84 hdr->addr1, hwaccel, rx->security_idx,
85 - &rx->tkip_iv32,
86 - &rx->tkip_iv16);
87 + &rx->tkip.iv32,
88 + &rx->tkip.iv16);
89 if (res != TKIP_DECRYPT_OK)
90 return RX_DROP_UNUSABLE;
91
92 @@ -552,6 +553,8 @@ ieee80211_crypto_ccmp_decrypt(struct iee
93 }
94
95 memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
96 + if (unlikely(ieee80211_is_frag(hdr)))
97 + memcpy(rx->ccm_gcm.pn, pn, IEEE80211_CCMP_PN_LEN);
98 }
99
100 /* Remove CCMP header and MIC */
101 @@ -782,6 +785,8 @@ ieee80211_crypto_gcmp_decrypt(struct iee
102 }
103
104 memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN);
105 + if (unlikely(ieee80211_is_frag(hdr)))
106 + memcpy(rx->ccm_gcm.pn, pn, IEEE80211_CCMP_PN_LEN);
107 }
108
109 /* Remove GCMP header and MIC */