Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
wifi stack: an unsigned EAPOL message 1 cannot replace live keys, the handshake result is sticky, GTK length and key id are checked (docs/driver-review/wifi-stack.md)
shimokub8a901b96667
diff --git a/docs/driver-review/wifi-stack.md b/docs/driver-review/wifi-stack.mdindex 165142f..1ca2aaf 100644--- a/docs/driver-review/wifi-stack.md+++ b/docs/driver-review/wifi-stack.md@@ -263,3 +263,20 @@ Concurrency and lock order: no cycle found among `lx_lock`, `bus.service`, `wiph- Side channels: the AES code is table-driven and `times()` has a data-dependent ternary; it runs only for the key unwrap, so only a local cache-timing attacker would matter.- 40/80 MHz width selection and rate/HT/VHT handling were read for bounds only, not for correctness against the standard; power save, roaming, regulatory domain logic and the `wifi_lx=1` register-trace path were skimmed.- Behaviour on hardware (everything marked PLAUSIBLE or NEEDS-HARDWARE).++## Fix status++- W1: FIXED (partly). `lx_wpa.c` `lx_wpa_message()`: an unsigned message 1 is accepted only while no signed message has been accepted yet (`!replay_known`); afterwards it is ignored (`LX_WPA_NOTHING`, no reply, keys untouched). This closes the 30 ms window before `key_set()` and the after-join KCK/KEK overwrite. SKIPPED: the candidate-key scheme for a forged message 1 *before* message 3 (needs new fields in `struct lx_wpa`, i.e. `lx_wpa.h`, which was not in the editable set; the forged frame still makes that join fail, as W2 case 1 describes).+- W2: FIXED. `lx_80211.c` `handshake_message()`: `radio->handshake` is no longer overwritten once it is `LX_WPA_KEYS`; `station_join()`: the wait no longer ends on `LX_WPA_BAD` (only KEYS, the AP's deauthentication or the 8 s timeout), the reason is still logged on timeout. A genuinely wrong passphrase still ends through deauthentication or the timeout.+- W3: SKIPPED. NEEDS-HARDWARE: whether the driver sets `RX_FLAG_DECRYPTED`/`IV_STRIPPED` on every legitimate 802.11-form frame is not known; requiring it could drop real group traffic.+- W4: SKIPPED. Protocol/design change (per-key PN state, and whether `status->extiv` carries the PN depends on the firmware/driver).+- W5: SKIPPED. Replacing the bump allocator with a freeing allocator is a design change and needs knowledge of which ath11k allocations go through it.+- W6: SKIPPED. The seed is in `xnuloader/loader.c`, outside the editable files.+- W7: FIXED (partly). `lx_wpa.c` `group_key()`: the GTK KDE must be 16 bytes and have key number 1 to 3. SKIPPED: the Install/Secure bit checks (supplicants such as wpa_supplicant do not require them; rejecting could break real APs) and the RSNE comparison with the beacon (needs a changed `lx_wpa_message()` signature / `lx_wpa.h`).+- W8: FIXED. `lx_80211.c`: the requested BSSID is stored in `radio->want_bssid` and tested in `heard()`; a new `radio->target_fixed` stops `heard()` from replacing `target` once the join has chosen; the `bzero(target)` is now under `lx_os_lock()`. SKIPPED: `vif->bss_conf.bssid` still points at `target.address` (now stable after the choice).+- W9: SKIPPED. Locking/teardown redesign (PLAUSIBLE, needs a `leaving` protocol across rx, work items and the driver).+- W10: FIXED (partly). `lx_80211.c` `group_rekey()`: on a failed `set_key` the old key is kept and the new configuration is wiped and freed. SKIPPED: not sending group message 2 and not consuming the replay counter on failure (needs a handshake design change).+- W11: SKIPPED. `timer_delete_sync()` waiting risks self-deadlock when called from the ticker's own callback, and bounding `irq_group()` needs knowledge of the vector's (edge/level) re-arm behaviour.+- W12: FIXED. `lx_80211.c` `say_heard()`: the BSSID of the wanted network is not printed. `lx_wpa.c`: the last AES block `t[16]` in `aes_decrypt()`, `a[8]` in `aes_unwrap()` and `data[]` after a failed unwrap are wiped.++Test: `tools/wpa-test` (copied to WSL, built with the sanitizers, no output left in the tree) passes, 195 checks 0 failed (seed 1); with seed 7 and one extra ad-hoc check (a forged message 1 after message 3 changes no key and gets no reply) 207 checks, 0 failed. `lx_80211.c` was only re-read, not compiled.diff --git a/wifi/lx/src/lx_80211.c b/wifi/lx/src/lx_80211.cindex 9a9119b..a9c94da 100644--- a/wifi/lx/src/lx_80211.c+++ b/wifi/lx/src/lx_80211.c@@ -89,6 +89,8 @@ struct lx_radio {unsigned heard_count, heard_lost;struct lx_heard heard[HEARD_MOST];uint8_t want[32], want_length; /* the name of the network that is wanted ... */+ uint8_t want_bssid[6]; /* ... and, if not all zero, the access point of that name that is wanted */+ bool target_fixed; /* the join has chosen: heard() leaves `target` as it is */unsigned width; /* MHz of the channel joined: 20, or with wifi_wide=1 what the access point offers */struct lx_target target; /* ... and the best of it */@@ -564,7 +566,12 @@ heard(struct lx_radio *radio, const uint8_t *frame, unsigned length, unsigned frlx_os_lock();if (name != NULL && radio->want_length != 0 && name[1] == radio->want_length &&memcmp(name + 2, radio->want, radio->want_length) == 0 && (!radio->target.found || signal > radio->target.signal) &&- length - 36 <= ELEMENTS_MOST && radio->sta == NULL) {+ length - 36 <= ELEMENTS_MOST && radio->sta == NULL && !radio->target_fixed &&+ (is_zero_ether_addr(radio->want_bssid) || memcmp(radio->want_bssid, frame + 16, ETH_ALEN) == 0)) {+ /*+ * (Only the access point that was asked for, if one was, and only until the join has chosen: a stronger beacon of+ * the same name must not replace the address, capability and elements of the one the join is made with.)+ */radio->target.found = true;memcpy(radio->target.address, frame + 16, ETH_ALEN);radio->target.frequency = (uint16_t)frequency;@@ -639,7 +646,7 @@ say_heard(struct lx_radio *radio)for (unsigned n = 0; n < radio->heard_count; n++) {const struct lx_heard *network = NULL;- char name[33];+ char name[33], address[18];unsigned which = 0, channel;bool wanted;@@ -666,10 +673,12 @@ say_heard(struct lx_radio *radio)/* (the network that is wanted is the user's own: its name is not said even here) */wanted = radio->want_length != 0 && network->name_length == radio->want_length &&memcmp(network->name, radio->want, radio->want_length) == 0;- printf("lx: heard: channel %3u (%u MHz) %4d dBm %02x:%02x:%02x:%02x:%02x:%02x %s, %u times \"%s\"\n", channel,- network->frequency, network->signal, network->sender[0], network->sender[1], network->sender[2], network->sender[3],- network->sender[4], network->sender[5], !(network->capability & 0x10) ? "open" : network->protected ? "RSN" :- "protected", network->times, wanted ? "(the network that is wanted)" : name);+ /* (nor is its address said: the BSSID of the user's own access point says where the user is) */+ snprintf(address, sizeof(address), "%02x:%02x:%02x:%02x:%02x:%02x", network->sender[0], network->sender[1],+ network->sender[2], network->sender[3], network->sender[4], network->sender[5]);+ printf("lx: heard: channel %3u (%u MHz) %4d dBm %s %s, %u times \"%s\"\n", channel,+ network->frequency, network->signal, wanted ? "xx:xx:xx:xx:xx:xx" : address, !(network->capability & 0x10) ? "open" :+ network->protected ? "RSN" : "protected", network->times, wanted ? "(the network that is wanted)" : name);}for (unsigned channel = 0; channel < nitems(on_channel); channel++) {if (on_channel[channel] != 0) {@@ -805,9 +814,16 @@ handshake_message(struct lx_radio *radio, const uint8_t *frame, unsigned length)queue_work(radio->work, &radio->group_work);}if (said != LX_WPA_NOTHING) {+ /*+ * The result of the handshake is not last-writer-wins: once message 3 has passed its MIC (KEYS) a later frame -- an+ * unsigned one from anyone in range can say BAD or ANSWER -- must not hide it, or the join would time out although+ * the keys are agreed (IEEE 802.11i, 8.5.3: only a verified message counts).+ */lx_os_lock();- radio->handshake = said;- radio->handshake_why = why;+ if (radio->handshake != LX_WPA_KEYS) {+ radio->handshake = said;+ radio->handshake_why = why;+ }lx_os_wakeup(&radio->handshake);lx_os_unlock();}@@ -1418,12 +1434,24 @@ static voidgroup_rekey(struct work_struct *work){struct lx_radio *radio = __containerof(work, struct lx_radio, group_work);- struct ieee80211_key_conf *old = radio->group_key;+ struct ieee80211_key_conf *old = radio->group_key, *fresh;int error = 0;radio->rekeys++;- radio->group_key = key_set(radio, radio->wpa.group, radio->wpa.group_index, false, &error);+ fresh = key_set(radio, radio->wpa.group, radio->wpa.group_index, false, &error);lx_tell("lx: the group's new key (number %u) is given to the driver: %d\n", radio->wpa.group_index, error);+ if (error != 0) {+ /*+ * The firmware did not take the new key: the old one stays (it is not dropped for a key that is not there), and the+ * new configuration, which key_set() returns even then, is wiped and freed; nothing was installed to disable.+ */+ if (fresh != NULL) {+ explicit_bzero(fresh->key, 16);+ kfree(fresh);+ }+ return;+ }+ radio->group_key = fresh;key_drop(radio, &old, false);}@@ -1600,7 +1628,12 @@ station_join(struct lx_radio *radio, const struct hakios_sta_join *what)bzero(radio->ba, sizeof(radio->ba));memcpy(radio->want, name, name_length);radio->want_length = (uint8_t)name_length;+ /* (under the lock: heard() writes the target under it, from the receive thread) */+ lx_os_lock();bzero(target, sizeof(*target));+ memcpy(radio->want_bssid, what->bssid, ETH_ALEN);+ radio->target_fixed = false;+ lx_os_unlock();if ((ret = station_up(radio)) != 0) {return (ret);}@@ -1626,6 +1659,10 @@ station_join(struct lx_radio *radio, const struct hakios_sta_join *what)for (unsigned attempt = 0; attempt < 3 && !target->found; attempt++) {station_scan(radio, name, name_length, false, NULL);}+ /* the choice is made: a beacon heard from here on does not change it (the join goes on with this one's address and elements) */+ lx_os_lock();+ radio->target_fixed = true;+ lx_os_unlock();if (!target->found) {lx_tell("lx: THE NETWORK THAT IS WANTED WAS NOT HEARD (a name of %u bytes), in three scans\n", name_length);return (ENOENT);@@ -1792,7 +1829,11 @@ station_join(struct lx_radio *radio, const struct hakios_sta_join *what)/* the handshake: the access point begins it; its messages come in as frames and are answered where they come in */began = lx_os_nanoseconds();lx_os_lock();- while (radio->handshake != LX_WPA_KEYS && radio->handshake != LX_WPA_BAD && radio->answer != ANSWER_GONE &&+ /*+ * (A message that is not good does not end the wait: it may be a forged frame, and a frame that fails its check is discarded+ * -- IEEE 802.11i, 8.5.3 -- while the genuine messages can still come. The reason is still said below if the time runs out.)+ */+ while (radio->handshake != LX_WPA_KEYS && radio->answer != ANSWER_GONE &&lx_os_nanoseconds() - began < 8000000000ull) {lx_os_sleep(&radio->handshake, 20 * 1000000ull);}diff --git a/wifi/lx/src/lx_wpa.c b/wifi/lx/src/lx_wpa.cindex bf33d77..88859cf 100644--- a/wifi/lx/src/lx_wpa.c+++ b/wifi/lx/src/lx_wpa.c@@ -274,6 +274,7 @@ aes_decrypt(const uint8_t round_keys[176], uint8_t s[16])}if (round == 0) {memcpy(s, t, 16);+ memset(t, 0, sizeof(t)); /* (the last block is part of the key being unwrapped: not left on the stack) */break;}for (unsigned c = 0; c < 4; c++) { /* the columns mixed back */@@ -323,6 +324,7 @@ aes_unwrap(const uint8_t kek[16], const uint8_t *wrapped, unsigned length, uint8}memset(round_keys, 0, sizeof(round_keys));memset(block, 0, sizeof(block));+ memset(a, 0, sizeof(a));return (good == 0 ? 0 : -1);}@@ -421,8 +423,13 @@ group_key(struct lx_wpa *wpa, const uint8_t *data, unsigned length, const uint8_if (e[0] == 0xdd && e[1] == 0) {break; /* (padding from here on) */}+ /*+ * Only a CCMP group key is joined (protection() in lx_80211.c), so the key is 16 bytes -- the length key_set() copies --+ * and its number is 1 to 3 (IEEE 802.11-2020, 12.7.2: number 0 is the pairwise key's): a KDE of another length or number+ * is not installed with stale bytes of `group` filling it out.+ */if (e[0] == 0xdd && e[1] >= 6 + 5 && e[2] == 0x00 && e[3] == 0x0f && e[4] == 0xac && e[5] == 1 &&- e[1] - 6u <= sizeof(wpa->group)) {+ e[1] - 6u == 16 && (e[6] & 3) != 0) {wpa->group_index = e[6] & 3;wpa->group_length = e[1] - 6u;memcpy(wpa->group, e + 8, wpa->group_length);@@ -468,8 +475,17 @@ lx_wpa_message(struct lx_wpa *wpa, const unsigned char *message, unsigned lengthreturn (LX_WPA_BAD);}- /* message 1: the access point's nonce, unsigned */+ /*+ * message 1: the access point's nonce, unsigned. Message 1 starts the four-way handshake and nothing else (IEEE 802.11i,+ * 8.5.3: the supplicant takes it in the states before the pairwise key is installed): once a signed message has been+ * accepted (replay_known) the keys are proven and in use, and an unsigned frame -- anyone in range can send one -- must not+ * re-derive and overwrite the KCK, KEK and TK. It is left alone, as every frame that is not for us to answer is.+ */if ((info & (INFO_PAIRWISE | INFO_ACK | INFO_MIC)) == (INFO_PAIRWISE | INFO_ACK)) {+ if (wpa->replay_known) {+ *why = "it is a message 1 after the keys were proven (an unsigned frame does not change them)";+ return (LX_WPA_NOTHING);+ }memcpy(wpa->peer_nonce, message + AT_NONCE, 32);derive(wpa);answer(wpa, message, 2 | INFO_PAIRWISE | INFO_MIC, wpa->own_nonce, wpa->rsn, wpa->rsn_length, reply, reply_length);@@ -501,6 +517,7 @@ lx_wpa_message(struct lx_wpa *wpa, const unsigned char *message, unsigned lengthreturn (LX_WPA_BAD);}if (!(info & INFO_ENCRYPTED) || aes_unwrap(wpa->kek, message + AT_DATA, data_length, data) != 0) {+ memset(data, 0, sizeof(data)); /* (a failed unwrap leaves partial plaintext of the key data in it) */*why = "its key data does not unwrap";return (LX_WPA_BAD);}