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: bring-up: remote-controlled lengths and offsets are checked (docs/driver-review/wifi-bringup.md)
shimoku79b00afb708a
diff --git a/docs/driver-review/wifi-bringup.md b/docs/driver-review/wifi-bringup.mdindex 0d71d94..d5c9071 100644--- a/docs/driver-review/wifi-bringup.md+++ b/docs/driver-review/wifi-bringup.md@@ -210,3 +210,21 @@ In `glink_pump()` lines 2600-2616, a complete DATA message is copied into a `kal- Whether TrustZone expects `PAS_SHUTDOWN` after a failed start (W9) and whether the DSP's SMP2P "restart" handshake in `dsp_listen()` matches Linux in every case.- Modem code (out of scope) and the audio loader beyond the shared-lock declarations.- No build, run, or runtime test was done.++## Fix status++All edits are in `drivers/wifi/hakios.wifi.c`; nothing was compiled or run. Line numbers are from the file after the edits.++- W1 FIXED: `glink_pump()` (about 2505-2535) sums the message length in a `uint64_t` (`wide`), so OPEN, INTENT and DATA lengths can no longer wrap to 0; the existing `> theirs_size - 8` check now rejects them (queue marked broken), and `length` is narrowed only after that check. This also bounds the INTENT loop. Not done: a per-call iteration cap in the pump (design change).+- W2 FIXED: `smem_items()` ends the walk for an entry with `padding_data > size` or one that reaches past `free_uncached` (bottom), or `padding_data > size` (top); `glink_find()` requires the descriptor and both queues to lie inside the 2 MB mapping; `smem_poke()` and `smem_word()` ignore/return 0 for an offset outside the mapping or while it is not mapped.+- W3 FIXED: the mailbox check is now `box > aoss.size || size > aoss.size - box` in both `step_mailbox()` and `step_mailbox_open_locked()`.+- W4 FIXED: `qrtr_input()` checks `header.size > length || at > length - header.size` instead of the wrapping sum.+- W5 SKIPPED: the fix is a new "dead" state that stops the driver and changes what `glink_send()` and `hakios_wifi_dsp_is_up()` return; it is a design change, and what the radio does after err-fatal needs hardware.+- W6 PARTLY FIXED: `link_thread_is` is cleared when the link thread ends. SKIPPED: the shared `static found[256]` in `smem_item()` (a mutex cannot be used, because the panic path calls it with no lock taken; a 3 KB stack array needs a decision on stack use), and the `dsp_words()` static `at` (idempotent).+- W7 FIXED: step 16 requires `size <= 4 * RADIO_WINDOW` (2 MB) and `address + size <= 4 GB`, and returns false if the map result is 0.+- W8 PARTLY FIXED: `step_image()` unmaps the 25 MB window on the misaligned-segment return and returns false if the map is 0; `glink_offer()` frees a kept intent buffer of another size before replacing it; `smem_peek()` and `step_registers()` check the map result. SKIPPED: the page of `step_header()` that is "never freed" on purpose (TrustZone may still use it; whether it can be freed needs hardware knowledge), and `need()` (it returns void and its callers cannot handle a failure; whether `ml_io_map` can return 0 is not known).+- W9 SKIPPED: needs knowledge of what TrustZone expects after a failed start, and a new "starting" state (design change).+- W10 SKIPPED: waiting without a bound or a "stop wanted" flag changes the shutdown flow (design change).+- W11 SKIPPED: capping the list and delaying DONE_REUSE changes the protocol flow (design change); a plain cap would drop packets.+- W12 SKIPPED: whether the WCOMB mapping reorders the loads is hardware behaviour, and a barrier there is a memory-ordering decision.+- W13 SKIPPED: reading the MAC from somewhere other than a boot-arg is a new feature, and the logged lengths of the name and passphrase are deliberate and documented in the comments (they help to diagnose a bad `fw/network`); no secret reaches the log.diff --git a/drivers/wifi/hakios.wifi.c b/drivers/wifi/hakios.wifi.cindex f7e0ad1..a079d33 100644--- a/drivers/wifi/hakios.wifi.c+++ b/drivers/wifi/hakios.wifi.c@@ -501,7 +501,8 @@ smem_peek(uint64_t offset, void *to, uint32_t size)/* memory, not registers; and from the kernel's own map: where ml_io_map() puts things there are 8 MB in all */smem = (const volatile uint8_t *)ml_io_map_unmappable(SMEM_PHYS, SMEM_SIZE, VM_WIMG_WCOMB);}- if (offset + size > SMEM_SIZE) {+ /* A failed map must not be read through: that would be a load from a low address. */+ if (smem == NULL || offset + size > SMEM_SIZE) {return false;}for (uint32_t i = 0; i < size; i++) {@@ -514,6 +515,10 @@ smem_peek(uint64_t offset, void *to, uint32_t size)static voidsmem_poke(uint32_t offset, uint32_t value){+ /* An offset that came from an item of another processor must not store outside the 2 MB mapping. */+ if (smem == NULL || offset > SMEM_SIZE - 4) {+ return;+ }wr((vm_offset_t)smem + offset, value);}@@ -566,6 +571,15 @@ smem_items(uint32_t partition, uint32_t cacheline, struct smem_item *found, unsiif (!smem_peek(partition + at, &entry, sizeof(entry)) || entry.canary != SMEM_CANARY || entry.size > header.size) {break;}+ /*+ * An entry whose data padding is larger than its data would make the item's size wrap to about 4 GB,+ * and one that reaches past the used part of the partition would put the item outside it (and+ * possibly outside the mapping): the walk ends there.+ */+ if (entry.padding_data > entry.size ||+ (uint64_t)at + sizeof(entry) + entry.padding_header + entry.size > header.free_uncached) {+ break;+ }if (count < max) {found[count].item = entry.item;found[count].offset = partition + at + sizeof(entry) + entry.padding_header;@@ -583,6 +597,10 @@ smem_items(uint32_t partition, uint32_t cacheline, struct smem_item *found, unsiif (!smem_peek(partition + at, &entry, sizeof(entry)) || entry.canary != SMEM_CANARY || entry.size > at) {break;}+ /* A data padding larger than the data would make the item's size wrap to about 4 GB. */+ if (entry.padding_data > entry.size) {+ break;+ }if (count < max) {found[count].item = entry.item;found[count].offset = partition + at - entry.size;@@ -863,7 +881,8 @@ step_mailbox(void)printf("wifi: %u: that is not the mailbox's description\n", step);return false;}- if (size >= QMP_MESSAGE + 4 && box >= 0x3c && box + size <= aoss.size) {+ /* (box + size would wrap for a huge size: the check is made as a subtraction, after box is known to be inside) */+ if (size >= QMP_MESSAGE + 4 && box >= 0x3c && box <= aoss.size && size <= aoss.size - box) {get(&aoss, "the length of the message in our mailbox", box);}@@ -1477,7 +1496,8 @@ step_mailbox_open_locked(void)}box = get(&aoss, "our mailbox", QMP_OUR_BOX);size = get(&aoss, "our mailbox's size", QMP_OUR_BOX_SIZE);- if (size < QMP_MESSAGE + 4 || box < 0x3c || box + size > aoss.size || (box & 3)) {+ /* The AOP chooses box and size: box + size would wrap for a huge size and let our_box lie outside the mapped block. */+ if (size < QMP_MESSAGE + 4 || box < 0x3c || box > aoss.size || size > aoss.size - box || (box & 3)) {printf("wifi: %u: our mailbox is not where a message fits\n", step);return false;}@@ -1887,12 +1907,19 @@ step_image(void)* is mapped for as long as this takes: 25 MB do not fit where ml_io_map() puts what stays.)*/dsp_memory = (volatile uint64_t *)ml_io_map_unmappable(WPSS_MEMORY, WPSS_MEMORY_SIZE, VM_WIMG_WCOMB);+ /* A failed map must not be read or written through. */+ if (dsp_memory == NULL) {+ printf("wifi: %u: the DSP's memory could not be mapped\n", step);+ return false;+ }printf("wifi: %u: the memory begins %#llx, the image %#llx\n", step, dsp_memory[0], segment_word(0, 0));for (unsigned s = 0; s < image.segments; s++) {volatile uint64_t *to = dsp_memory + image.segment[s].at / 8;if ((image.segment[s].at | image.segment[s].memory_size) & 7) {printf("wifi: %u: a segment that does not begin and end at a multiple of eight bytes: not written\n", step);+ /* The 25 MB mapping is given back on this way out too, or every failed try would keep it. */+ ml_io_unmap((vm_offset_t)dsp_memory, WPSS_MEMORY_SIZE);return false;}printf("wifi: %u: about to write %#x bytes at %#x (%#x of them the file's)\n", step, image.segment[s].memory_size,@@ -2299,6 +2326,10 @@ static struct {static uint32_tsmem_word(uint32_t offset){+ /* An offset outside the 2 MB mapping reads as zero instead of loading from outside it. */+ if (smem == NULL || offset > SMEM_SIZE - 4) {+ return 0;+ }return rd((vm_offset_t)smem + offset);}@@ -2314,8 +2345,11 @@ glink_find(void)}glink.ours = smem_item(ITEM_GLINK_OUR_QUEUE, &glink.ours_size);glink.theirs = smem_item(ITEM_GLINK_THEIR_QUEUE, &glink.theirs_size);+ /* Each item must lie wholly inside the 2 MB mapping: the queues are indexed by remote-chosen head and tail words. */return glink.ours != 0 && glink.theirs != 0 && glink.ours_size >= 1024 && glink.theirs_size >= 1024 &&- !(glink.ours_size & 7) && !(glink.theirs_size & 7);+ !(glink.ours_size & 7) && !(glink.theirs_size & 7) &&+ (uint64_t)glink.descriptor + 32 <= SMEM_SIZE && (uint64_t)glink.ours + glink.ours_size <= SMEM_SIZE &&+ (uint64_t)glink.theirs + glink.theirs_size <= SMEM_SIZE;}/* Our two marks to zero, if they are not: before the DSP starts (see above). */@@ -2437,7 +2471,12 @@ glink_offer(uint32_t size)if (i >= sizeof(glink.our_intents) / sizeof(glink.our_intents[0]) || size == 0 || size > 0x10000) {return false;}- if (glink.our_intents[i].data == NULL || glink.our_intents[i].size != size) {+ /* A kept buffer of another size is given back before it is replaced: its pointer would be lost, and the memory with it. */+ if (glink.our_intents[i].data != NULL && glink.our_intents[i].size != size) {+ kfree_data(glink.our_intents[i].data, glink.our_intents[i].size);+ glink.our_intents[i].data = NULL;+ }+ if (glink.our_intents[i].data == NULL) {glink.our_intents[i].data = kalloc_data(size, Z_WAITOK | Z_ZERO); /* (kept for good: a later link uses it again) */}if (glink.our_intents[i].data == NULL) {@@ -2467,30 +2506,33 @@ glink_pump(void)char name[33];while (glink.up && !glink.broken) {- uint32_t waiting = glink_waiting(), length = sizeof(message);+ uint32_t waiting = glink_waiting(), length;+ /* 64 bits: the lengths below come from the DSP, and in 32 bits their sums wrap to 0 and the message is never consumed */+ uint64_t wide = sizeof(message);if (waiting < sizeof(message)) {return;}glink_peek(&message, 0, sizeof(message));if (message.command == GLINK_OPEN) {- length += message.param2;+ wide += message.param2;} else if (message.command == GLINK_INTENT) {- length += 8 * message.param2;+ wide += 8ull * message.param2;} else if (message.command == GLINK_DATA || message.command == GLINK_DATA_MORE) {if (waiting < 16) {return;}glink_peek(words, 8, 8); /* the size of this piece, and what is still to come */- length = 16 + words[0];+ wide = 16ull + words[0];}- length = (length + 7) & ~7u;- if (length > glink.theirs_size - 8) {- printf("wifi: glink: A MESSAGE OF %u BYTES (%s %u %#x): not believed, and nothing more is read\n", length,+ wide = (wide + 7) & ~7ull;+ if (wide > glink.theirs_size - 8) {+ printf("wifi: glink: A MESSAGE OF %llu BYTES (%s %u %#x): not believed, and nothing more is read\n", wide,glink_name(message.command), message.param1, message.param2);glink.broken = true;return;}+ length = (uint32_t)wide; /* (it fits the queue now, and is at least eight) */if (waiting < length) {return; /* (the rest of it is on its way) */}@@ -2912,7 +2954,8 @@ qrtr_input(const uint8_t *data, uint32_t length)printf("wifi: qrtr: %u bytes that are no datagram (they begin %#x)\n", length, length ? data[0] : 0);return;}- if (header.size == 0 || at + header.size > length) {+ /* (at + header.size would wrap for a huge size and pass: the check is a subtraction, after the size is known to fit) */+ if (header.size == 0 || header.size > length || at > length - header.size) {printf("wifi: qrtr: a datagram of %u bytes in a message of %u: dropped\n", header.size, length);return;}@@ -3110,6 +3153,8 @@ link_thread(__unused void *parameter, __unused wait_result_t result)qrtr_fresh();lck_mtx_unlock(&qrtr_calling);}+ /* This thread's handle is no longer one once it ends: a new thread at the same address must not be taken for it (qrtr_close()). */+ link_thread_is = NULL;link_runs = false;thread_terminate(current_thread());}@@ -3434,7 +3479,12 @@ step_registers(void)uint32_t size = 0, asked = 0;bool mission = false;- if (!hakios_wlfw_registers(&address, &size, &mission) || address > 0xffffffffull || size < 3 * RADIO_WINDOW) {+ /*+ * The firmware chooses the window: it must be inside the first 4 GB (radio.phys is 32 bits) and no larger than the+ * 2 MB it is said to be, or the map below would cover whatever lies there.+ */+ if (!hakios_wlfw_registers(&address, &size, &mission) || address > 0xffffffffull || size < 3 * RADIO_WINDOW ||+ size > 4 * RADIO_WINDOW || address + size > 0x100000000ull) {printf("wifi: %u: the firmware has not said where the radio's registers are (step 15)\n", step);return false;}@@ -3452,6 +3502,11 @@ step_registers(void)radio.phys = (uint32_t)address;radio.size = size;radio.base = ml_io_map_unmappable(radio.phys, radio.size, VM_WIMG_IO);+ /* A failed map must not be read through. */+ if (radio.base == 0) {+ printf("wifi: %u: the radio's registers could not be mapped\n", step);+ return false;+ }for (unsigned i = 0; i < sizeof(words) / sizeof(words[0]); i++) {uint32_t chip = words[i].chip, at;