Skip to content

soc: apple, wifi: bound firmware-supplied sizes and addresses in PMP, AOP2, brcmfmac and centauri - #2894

Closed
RyanTheTide wants to merge 5 commits into
integration/aurorafrom
hardening/soc-wifi-fw-bounds-20261123
Closed

RyanTheTide wants to merge 5 commits into
integration/aurorafrom
hardening/soc-wifi-fw-bounds-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Four independent fixes, one commit each (plus a host-test harness update for the brcmfmac one), where these drivers took sizes, addresses or counts from their coprocessor firmware without checking them.

Problem

  • PMP (soc: apple: pmp): every buffer request and register registration from the firmware was granted and kept, with no bound on the number of live buffers, their total size or the register list. The register reply carries the new index in 16 bits.
  • AOP2 (soc: apple: t6050-aop): every AFK_GETBUF allocated a new device-managed buffer that is only released at unbind, and the buffer and its size were published without ordering against the reader on the sleep/wake path.
  • brcmfmac PCIe: the trap record and the console descriptor, write index and buffer are read at addresses from shared RAM that were not checked against the TCM mapping.
  • centauri: a later statistics reply could leave the station counters marked valid with a length that no longer describes them.

Change

  • PMP: at most 64 live buffers / 64 MiB (four of the largest buffer one request can describe), register list capped so its index fits the reply; refused requests get no reply and are logged.
  • AOP2: one buffer per endpoint for as long as the firmware runs; a repeated request of the same size gets the same IOVA, others are refused (ratelimited log); release/acquire publication.
  • brcmfmac: record the BAR2 length; read the trap record and the console only within the mapping; otherwise the console stays disabled as when the firmware reports none.
  • centauri: keep the accepted counter record's length with its valid flag.
    No gates on chip, board or firmware version; no new parameters.

Testing

  • KUnit (arm64 qemu, LLVM=1): apple_t6050_aop 5/5, brcmfmac-pcie-tcm 1/1, apple-centauri-wifi 1/1 pass. With the fixes reverted (tests kept), 5 cases fail: 3 in apple_t6050_aop, the brcmfmac and the centauri case.
  • PMP host test tools/asahi/pmp-alloc-tests.rs (in run-host-tests.py): passes; with the bounds removed, 4 of 5 checks fail.
  • tools/asahi/brcmf-recovery-tests.py (fake dongle RAM) now models the TCM size: 11/11 runs pass, including two new mutations that drop the trap and console bounds (both caught by the fake RAM's bounds assertion). Full run-host-tests.py locally: 458/464, the 6 others are timeouts in unrelated GPU/DCP/USB tests on a loaded build box.
  • arm64 W=1 build of drivers/soc/apple/, brcmfmac and centauri: no new warnings. rustfmt and checkpatch clean apart from the MAINTAINERS notes.

Still to run

  • Hardware rows on the default command line: J713 (PMP probe, brcmfmac Wi-Fi up, s2idle x2) and the Neo (PMP, s2idle). AOP2 (apple,t6050-aop2) and the centauri path need an M5 row; we don't have one in the lab, a row from someone with one is welcome.
PMP detail

Problem

The PMP driver grants every buffer request (OPC_MALLOC) and every register registration (OPC_REGISTER_IOREG) the firmware sends and keeps them until the firmware frees them. Neither the number of live buffers, their total size, nor the length of the register list was bounded. The register reply also carries the new entry's index in 16 bits, which a longer list cannot represent.

Change

  • New pmp_alloc.rs with AllocBudget: at most 64 live allocations and 64 MiB in total (four of the largest buffer one request can describe, its size field being 24 bits). A freed buffer returns its room. Normal boots use about ten buffers.
  • register_ioreg() refuses a registration once the list holds 0xffff entries, so the index always fits its reply field.
  • A refused request gets no reply and is logged, like other requests the driver cannot handle. Normal operation is unchanged.

Testing

  • Host test tools/asahi/pmp-alloc-tests.rs (wired into run-host-tests.py): 5/5 pass; with the bounds removed, 4 of 5 fail.
  • arm64 W=1 build of drivers/soc/apple/: see details.

Still to run

  • J713 boot with this change (PMP probe and readiness unchanged).
Reviewer detail
  • The allocation now happens under state (a Mutex, so sleeping allocation is fine), which keeps claim, allocation and push atomic with respect to free().
  • Every error path after claim() calls release().
  • The bounds are sanity limits on a kernel resource, not hardware-protection values; they are derived from the message format (24-bit size, 16-bit index) rather than from any board.
AOP2 detail

Problem

Each AFK_GETBUF from the AOP2 firmware allocated a new device-managed coherent buffer and stored it in the endpoint's ring, replacing the previous one. Device-managed buffers are only released at unbind, which this driver does not allow, so repeated requests (for example one per wake) kept adding buffers. The buffer pointer and its size were also published without ordering against t6050_aop2_view(), which reads the rings from the sleep and wake paths on another thread.

Change

  • An endpoint gets one buffer for as long as its firmware runs. A repeated GETBUF of the same size is answered with the same IOVA; a request with no size or a different size is refused and logged (ratelimited).
  • The buffer is published with smp_store_release() after its size and IOVA, and t6050_aop2_view() reads it with smp_load_acquire(). The buffer never changes once set, so no lock is needed.

Testing

  • KUnit suite apple_t6050_aop (CONFIG_APPLE_T6050_AOP_KUNIT_TEST, arm64 qemu): see details. With the old behaviour (every request allocates) the reuse/refusal cases fail.

Still to run

  • T6050 hardware row (AOP2 probe, a sleep/wake cycle, no new errors). We have no T6050 in our lab; a row from someone with one is welcome.
brcmfmac detail

Problem

On a stalled control request, brcmf_pcie_ctl_stall() reads the trap record at an address taken from shared RAM, and the console code (brcmf_pcie_bus_console_init(), brcmf_pcie_bus_console_read(), brcmf_pcie_console_trapped()) reads its descriptor, write index and buffer at addresses taken from shared RAM. None of these offsets was checked against the size of the TCM mapping.

Change

  • Record the BAR2 length (tcm_size) when the TCM is mapped.
  • New helper brcmf_pcie_tcm_range_valid(). The trap record is read only when it lies within the mapping; otherwise its address is logged. At console setup the descriptor and buffer are accepted only when both lie within the mapping; otherwise the console stays disabled, as when the firmware reports none. The console reader and the trap scan stop when the write index is outside the buffer, and reading starts at the beginning of the buffer after each setup.
  • The firmware is still declared dead and recovery proceeds as before.

Testing

  • KUnit brcmfmac-pcie-tcm (range edge cases incl. wrap-around): see details.
  • arm64 W=1 build of brcmfmac.

Still to run

  • J713 boot with this change: Wi-Fi up, no new errors.
centauri detail

Problem

Every reply to the statistics command overwrites stats_length, while station_stats_valid is set only by a station-counter record of a supported length. A later reply carrying another record leaves the counters marked valid with a length that no longer describes them, and the debugfs and connection readers iterate station_stats by that length.

Change

  • Keep the accepted counter record's length in its own field, station_stats_len, set together with station_stats_valid and cleared with it; both readers iterate over it.
  • Move the statistics reply handling into record_stats_reply() so it can be tested. No other behaviour change.

Testing

  • KUnit apple-centauri-wifi (CONFIG_APPLE_CENTAURI_KUNIT_TEST): a valid record followed by a reply with another record keeps the recorded length; unsupported lengths are not recorded. See details.

Still to run

  • J813/J815/J873 row (Wi-Fi join + station_stats read). Not in our lab.

Assisted-by: LLM (Claude:Opus 5.5)

The PMP firmware asks the driver for DMA buffers (OPC_MALLOC) and for
register slots (OPC_REGISTER_IOREG). Every request was granted and kept
until the firmware freed it, so the number of buffers held for it, their
total size and the register list had no bound.

Count the live allocations and their size, and refuse one that would go
over 64 buffers or 64 MiB, four of the largest buffer a single request
can ask for (its size field is 24 bits). These are sanity bounds well
above normal use: the driver's lists are sized for ten buffers and 340
registers, which is what a normal boot uses. A freed buffer gives its
room back.

Refuse a register once the list has 0xffff entries. The reply carries
the new entry's index in 16 bits below the opcode, so a larger index
cannot be returned without corrupting the reply.

A refused request gets no reply and is logged, as other requests the
driver cannot handle are.

The bounds are in pmp_alloc.rs with host tests, run by
tools/asahi/run-host-tests.py as pmp-alloc-tests.rs.

Signed-off-by: Ryan Murray <[email protected]>
Each AFK_GETBUF from the AOP2 firmware allocated a new device-managed
coherent buffer and stored it in the endpoint's ring, replacing the one
before. Those buffers are only released at unbind, which this driver
does not allow, so a repeated GETBUF kept adding buffers. The stores of
the buffer and its size were also not ordered against
t6050_aop2_view(), which reads the rings from the resume path in
another thread, so a reader could pair a new buffer with an old size.

An endpoint now gets one buffer for as long as the firmware runs. A
repeated GETBUF of the same size is answered with the same IOVA; one
with no size or with another size is refused and logged (ratelimited),
without an answer. The buffer is published with smp_store_release()
after its size and IOVA, and t6050_aop2_view() reads it with
smp_load_acquire(), so a reader that finds the buffer also finds its
size. As the buffer never changes once set, no lock is needed between
the RTKit receive worker and the readers.

Add KUnit tests (CONFIG_APPLE_T6050_AOP_KUNIT_TEST, suite
apple_t6050_aop) for the GETBUF decision and the ring view's bounds.

Signed-off-by: Ryan Murray <[email protected]>
…M mapping

When a control request stalls, brcmf_pcie_ctl_stall() reads two words
of the trap record at an address taken from shared RAM, and the console
code reads its descriptor, write index and buffer at addresses taken
from shared RAM. None of them was checked against the size of the TCM
mapping.

Record the BAR2 length at mapping time. Read the trap record only when
it lies within the mapping; the firmware is still declared dead either
way. When the console is set up, accept its descriptor and buffer only
when both lie within the mapping, otherwise leave the console disabled
as when the firmware reports none, and start reading it from the
beginning of the buffer. The console reader and the trap scan stop
when the firmware's write index is outside the buffer.

Add a KUnit case for the range check.

Signed-off-by: Ryan Murray <[email protected]>
Every reply to the statistics command overwrites stats_length, while
station_stats_valid is only set by a station counter record of a
supported length. A later reply carrying another record leaves the
counters marked valid with a length that no longer describes them, and
the debugfs and connection readers then iterate past station_stats.

Record the length of the accepted counter record in its own field, set
together with station_stats_valid, and iterate over that. Move the
statistics reply handling into a helper so that it can be tested,
and add a KUnit case for it (CONFIG_APPLE_CENTAURI_KUNIT_TEST).

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at e0b2c56.

All four fixes are correct as far as they go, and the centauri commit is good as is. The issues below are about how complete the bounds are and how strict they are.

  1. brcmfmac is only partly bounded. The PR title says firmware-supplied addresses are bounded, but ring_info_addr still goes straight into memcpy_fromio/memcpy_toio (pcie.c:1623, :1697). htod_mb_data_addr/dtoh_mb_data_addr (:933, :1115), ringmem and the four idx pointers, and tcm_base_address + offset (:1057, :1144, :1802-1824) are not checked against tcm_size either. Better to check them once in brcmf_pcie_init_share_ram_info() and fail probe with -EINVAL. Otherwise, narrow the PR title to match the commit ("trap and console").
  2. brcmfmac upstreamability: brcmf_pcie_ctl_stall() and brcmf_pcie_console_trapped() are ours (from c55335e), not upstream. Only the tcm_size + console_init/console_read part applies to mainline, and it fixes a real upstream bug: with newidx >= bufsize the read loop at :1202 never ends, because read_idx wraps before it can equal newidx. Split that part into its own minimal patch. Fold the stall/trapped checks into the series that added them.
  3. brcmfmac nit: the descriptor/buffer re-checks in console_trapped() (:1013-1017) are dead code. console_init now zeroes base_addr unless both ranges are valid, and buf_addr/bufsize are cached. Only the existing widx check is needed.
  4. AOP2 may be too strict (t6050-aop.c:149-157). Every GETBUF of a different size is refused with no answer, so that endpoint stays dead until reboot, and its wake waits out the timeout. Hardware has not shown that a wake re-sends GETBUF with the same size: there is no T6050 row. A smaller request fits the existing buffer, so answer size <= r->size with the same IOVA and refuse only larger ones.
  5. AOP2 commit message: "no lock is needed between the RTKit receive worker and the readers" holds only for buf/size. tx_off/tx_size/tx_ready (:211-218) are still plain stores read from the PM path. This is still memory-safe, because t6050_aop2_view() re-checks them against the fixed r->size, but please word the claim more narrowly.
  6. PMP nit: MALLOC_SIZE_MAX = 1 << 24 (pmp_alloc.rs:6) duplicates MALLOC_SIZE_MASK = 0xFFFFFF (pmp.rs:90) and is off by one. The largest size a request can carry is 0xFFFFFF, so size > MALLOC_SIZE_MAX can never be true. Derive one constant from the other.
  7. PMP: on the board that keeps the display bridge, a refused MALLOC/REGISTER goes to fail_display() (pmp.rs:855), so going over the cap kills display power commands. That is fine for a sanity cap, but ioreg_entries is never cleared. Please show that a wake does not re-register (for example, log the list length and the live alloc count/bytes after boot and after s2idle x2). Otherwise the 0xffff cap becomes a failure after many wakes.
  8. Pre-existing, question only: register_ioreg() replies with index = len() (1-based, :466), but set_ioreg() does .get(index) (0-based, :547). Is the firmware subtracting one?
  9. Please split into one PR per subsystem: soc/apple PMP, soc/apple AOP2, brcmfmac (upstream-bound), centauri. The rows differ too. PMP and brcmfmac can land on J713/Neo rows, while AOP2 and centauri wait for an M5 row the lab does not have.

Rows owed at this head (none run yet): J713 boot on default cmdline with PMP probe, brcmfmac Wi-Fi up, and s2idle x2 (plus the PMP counts from item 7). Neo with PMP and s2idle x2. One other BCM43xx Mac (BCM4387/4388) for Wi-Fi up and a console read, because the console_init change reaches every brcmfmac PCIe chip. A T6050 row for AOP2 probe, sensors/mics and a sleep/wake cycle. A J813/J815/J873 row for Wi-Fi join and a station_stats read.

brcmf_pcie_ctl_stall() and brcmf_pcie_console_trapped() now check the
trap record and the console buffer against devinfo->tcm_size with
brcmf_pcie_tcm_range_valid() from pcie.h. Give the fake device that
field (the size of its fake dongle RAM), take the helper from pcie.h,
and add two controls: a trap record and a console buffer that end past
the fake RAM are not read, and the firmware is still declared dead for
the trap. The new mutations trap-bound and console-bound drop the
checks; the fake RAM's bounds assertion then fails.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at da76d20.

The only change since e0b2c56 is one new commit, da76d20, which updates the host harness tools/asahi/brcmf-recovery-tests.py. It gives the fake device a tcm_size, takes brcmf_pcie_tcm_range_valid() from pcie.h, and adds two cases plus the trap-bound/console-bound mutations. The four driver commits are byte-identical. The harness change is fine, with one remark below. None of the nine points from the last review has been answered yet.

  1. brcmfmac is only partly bounded (ring_info_addr, the mailbox addresses, ringmem/idx pointers, tcm_base_address + offset). Still open. Either bound them in brcmf_pcie_init_share_ram_info() or narrow the title to "trap and console".
  2. Split the upstream-bound tcm_size + console_init/console_read fix (the endless read loop when newidx >= bufsize) from the stall/trapped checks. Still open.
  3. Re-checking the buffer in console_trapped() is redundant. This is now partly disputed by the new test. The "console buffer outside the TCM" case sets buf_addr directly, a state that console_init() can no longer produce, and the console-bound mutation exercises exactly that re-check. Keeping it as cheap defence is acceptable. Just note in the commit that console_init() is the real gate.
  4. AOP2 refuses every GETBUF of a different size. Still open: the body still says "others are refused". Please answer size <= r->size with the existing IOVA.
  5. The AOP2 commit message's lock claim should be narrowed to buf/size. Still open.
  6. PMP MALLOC_SIZE_MAX = 1 << 24 duplicates MALLOC_SIZE_MASK and is off by one. Still open.
  7. PMP ioreg_entries is never cleared. Still open: we still need boot vs s2idle x2 counts showing that wakes do not re-register.
  8. The 1-based register_ioreg vs 0-based set_ioreg index question is unanswered.
  9. Split into one PR per subsystem. Still open.

No new issues. There are no agent trailers in the commits, and the body ends correctly.

Verdict: CHANGES (1, 2, 4, 9 must be fixed; 5 and 6 are wording/nits; 7 needs data).

Rows owed (none run yet), all on the default cmdline:

  • RyanTheTide/lab: J713 with PMP probe, Wi-Fi up, s2idle x2 and the item 7 counts.
  • RyanTheTide/lab: Neo with PMP and s2idle x2.
  • A second BCM4387/4388 Mac for Wi-Fi plus a console read.
  • An M5 owner (outside volunteer): T6050 AOP2 row and J813/J815/J873 centauri station_stats row.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR checklist review at da76d208afd980704c5bbe3ee0ece40ce41153a5 (all 7 CI checks green: host-tests fixed by da76d208af, source, rust-build, kunit, and all 3 DWC3/ATC builds pass).

Blockers

  1. §1 / §5 Zero hardware test rows across all 4 touched subsystems:
    Only QEMU KUnit, host tests, and W=1 builds have been run at this head; every hardware row is still listed under "Still to run":
    • PMP (drivers/soc/apple/pmp.rs): Needs J713 and Neo default-cmdline boot + s2idle x2 rows, including live allocation count/bytes and ioreg_entries.len() after boot and after wake (since ioreg_entries is never cleared and a refusal triggers fail_display() on boards with a display bridge).
    • AOP2 (drivers/soc/apple/t6050-aop.c): Needs a T6050 probe + sleep/wake hardware row.
    • brcmfmac (drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c): Needs J713 plus another BCM43xx Mac (BCM4387/4388) Wi-Fi up + console read row.
    • centauri (drivers/net/wireless/apple/centauri/centauri_wifi.c): Needs a J813/J815/J873 Wi-Fi join + station_stats read row.
    • Include §5 regression counts (0 SELinux denials, 0 failed units, 0 new kernel errors) on each row.
  2. §2 AOP2 GETBUF refusal is too strict without T6050 wake evidence (drivers/soc/apple/t6050-aop.c:149-157):
    t6050_aop_getbuf_check() refuses any repeat GETBUF with size != r->size without sending an ACK, leaving that endpoint hung until reboot. Without T6050 wake evidence proving identical sizes on every wake, accepting size <= r->size (reusing the existing larger buffer) and refusing only size > r->size is safer.
  3. §2 Potential u32 wrap in brcmf_pcie_console_trapped() range check (drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c:1013-1014):
    Passing console->base_addr + BRCMF_CONSOLE_WRITEIDX_OFFSET as a u32 offset to brcmf_pcie_tcm_range_valid(..., 4) wraps around if console->base_addr is near U32_MAX. Validate brcmf_pcie_tcm_range_valid(devinfo->tcm_size, console->base_addr, BRCMF_CONSOLE_WRITEIDX_OFFSET + 4) (matching console_init()), or drop the redundant check since console_init() already zeroes base_addr unless both descriptor and buffer lie within tcm_size.

Non-blocking notes

  • Subsystem bundling: Grouping 4 independent subsystems (PMP, AOP2, brcmfmac, centauri) with disjoint hardware requirements blocks lab-testable fixes (PMP, brcmfmac) behind hardware the lab lacks (T6050, J813). Splitting into one PR per subsystem is strongly recommended.
  • brcmfmac scope vs title: Only trap and console offsets are bounded here; ring_info_addr, htod_mb_data_addr/dtoh_mb_data_addr, ringmem, and tcm_base_address offsets remain unchecked against tcm_size. Either validate them in brcmf_pcie_init_share_ram_info() or narrow the PR title.
  • PMP constant duplication (pmp_alloc.rs:6 vs pmp.rs:90): MALLOC_SIZE_MAX = 1 << 24 is off by 1 from MALLOC_SIZE_MASK = 0xFFFFFF, making size > MALLOC_SIZE_MAX unreachable from the masked 24-bit field; derive from one constant.
  • AOP2 commit message: "no lock is needed between the RTKit receive worker and the readers" holds for buf/size, whereas tx_off/tx_size/tx_ready remain plain stores (still memory-safe because t6050_aop2_view() validates them against fixed r->size).
  • centauri (centauri_wifi.c:1291,2307): The dev_info() and seq_printf() headers still report wifi->stats_length (the last reply's length) while the loop iterates wifi->station_stats_len.

Checklist summary

  • §1 No model gating: Pass in code; blocked on multi-Mac hardware coverage.
  • §2 Production-ready: Blocked on AOP2 strict-size refusal and console_trapped() u32 addition wrap.
  • §3 Safety: Pass (defensive bounds derived from wire/BAR formats, not hardcoded hardware-protection limits).
  • §4 Clean-room & attribution: Pass (human author/committer + human Signed-off-by: only across all 5 commits, zero AI trailers in commits, Assisted-by: in PR body).
  • §5 Evidence: Strong KUnit/host-test/mutation coverage and green CI (7/7), blocked on zero hardware rows.
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Split per subsystem as asked (item 9); this PR is superseded by:

All four are on the current integration/aurora tip. Item by item:

  1. brcmfmac: the shared RAM block, both mailbox words, the ring information, the ring memory and the four TCM index arrays are now checked when they are read (brcmf_pcie_init_share_ram_info() / brcmf_pcie_init_ringbuffers()), and the probe fails with -EINVAL. A new host test (brcmf-shared-tcm-tests.py) runs the real setup against a fake TCM that asserts on any access outside it: 17 controls pass, and each of its 11 mutations (one per check) fails.
  2. The console fix (tcm_size + console init/read, including the endless loop on newidx >= bufsize) is its own commit, which also applies to mainline. So are the shared RAM checks. The trap record and scan-index part is a separate commit on our stall/trap code.
  3. The dead descriptor/buffer re-checks in console_trapped() are gone; only the existing write index check remains.
  4. AOP2: a GETBUF that fits the existing buffer gets the same IOVA; only a larger one is refused. KUnit has smaller/larger cases (6/6).
  5. AOP2 commit text now says the release/acquire covers only buf/size, and the ring offsets are re-checked against the fixed size on every read.
  6. PMP: MALLOC_SIZE_MASK is now pmp_alloc::MALLOC_SIZE_MAX (0xff_ffff), so there's one constant and no off-by-one.
  7. Open on hardware: the J713/Neo rows will count PMP MALLOC and REGISTER replies across s2idle x2 (runtime kprobe on apple_rtkit_send_message, nothing added to the driver). If a wake does register again, the cap needs a different answer before soc: apple: pmp: bound the memory held for the firmware #2905 can land.
  8. That's how the driver was first written; this series doesn't touch it. The row will log what the firmware sends back.

Rows are listed under "Still to run" on each PR. AOP2 and centauri need M5 / J8xx rows the lab doesn't have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants