Repository navigation
wifi: brcmfmac: pcie: check firmware-supplied TCM addresses and keep console reads in bounds - #2907
Conversation
…le buffer The firmware console's descriptor address, buffer address, buffer size and write index all come from dongle memory. None of them was checked against the TCM mapping, so console reads could go past the end of the BAR. A write index at or past the buffer size also kept brcmf_pcie_bus_console_read() in its loop for good: the read index wraps at the buffer size and never meets the write index. Record the length of the TCM mapping. 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 at the beginning of the buffer. The reader stops 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]>
The shared RAM block and the addresses it holds (the two mailbox words, the ring information, the ring memory and, when the firmware keeps the ring indices in its own memory, the four index arrays) are offsets into the TCM mapping that come from dongle memory. The driver reads and writes through all of them, but only checked that the shared RAM block starts inside the dongle's RAM. Check each against the length of the TCM mapping when it is read, before it is used, and fail the probe with -EINVAL when one is outside. The index arrays are checked for one index per common ring and flowring. Add tools/asahi/brcmf-shared-tcm-tests.py, which runs the driver's shared RAM, ring and console setup against a fake TCM that asserts on every access outside it; one mutation per check drops it and fails a control. Signed-off-by: Ryan Murray <[email protected]>
When a control request stalls, brcmf_pcie_ctl_stall() reads the trap type and exception PC from a record whose address comes from shared RAM, without checking it against the TCM mapping. Read the record only when it lies within the mapping; the firmware is declared dead either way. The console trap scan starts at the write index the firmware reports when the console is set up. Reset the scan with the rest of the console state, and start it at the beginning of the buffer when that index is outside the buffer, so a scan that could not start before now does. tools/asahi/brcmf-recovery-tests.py gives its fake device the TCM size and adds a control for a trap record past the end of the TCM; the new mutation trap-bound drops the check and the fake's bounds assertion fails. Signed-off-by: Ryan Murray <[email protected]>
|
Review at afa278b. Follow-up to the #2894 review, brcmfmac part. All three points raised there are fixed. Requesting changes for item 1 because it is an out-of-bounds write into host memory, driven by firmware values, on the path this PR sets out to bound. Fixed from #2894:
Still open:
Rows owed at this head (none run): default command line, Wi-Fi up, s2idle x2 and a firmware console read on J713 and on one BCM4387/BCM4388 Mac (M1 Pro or J616s); plus one firmware-death recovery on either, since console init now also resets the read and scan state on a restart. |
There was a problem hiding this comment.
Formal adversarial §1–§6 pre-PR checklist review at afa278b972d13fa675ad82e863e61bddeb36a7a8 (REQUEST_CHANGES — host-memory ring-index bounds blocker, pending CI, and owed §5 hardware rows).
Checklist Summary
- §1 (No model gating): Pass — no chip, SoC, board, or firmware-version gates; clean upstream/Aurora split across the 3 commits.
- §2 (Production-ready, no hacks): Pass — no new parameters or delays.
- §3 (Safety — TCM/host bounds & integer arithmetic): Blocked —
brcmf_pcie_tcm_range_valid()(pcie.h:215-219) is overflow-free, and the TCM checks for shared RAM (BRCMF_SHARED_SIZE= 116 covering up toHOST_CAP2), mailboxes,ringinfo,ringmem, TCM index arrays, console descriptor/buffer/write index, and trap record (pcie.c:1062-1069) are sound. However, the host-memory ring-index path (BRCMF_PCIE_SHARED_DMA_INDEX) in the samebrcmf_pcie_init_ringbuffers()function still permits firmware-driven out-of-bounds reads/writes in host DMA memory (Blocker #1). - §4 (Clean-room & attribution): Pass — all 3 commits carry human author/committer (
Ryan Murray <[email protected]>) and humanSigned-off-by:only (zero AI trailers in commits); PR body includesAssisted-by:. - §5 (Evidence): Blocked — CI still pending and hardware rows unrun.
- §6 (Naming): Pass.
Blockers
- §3 (Safety — Host DMA ring-index buffer under-allocation / OOB read-write,
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c:1646-1778):- When
BRCMF_PCIE_SHARED_DMA_INDEXis enabled (devinfo->dma_idx_sz != 0),brcmf_pcie_init_ringbuffers()allocatesidxbufof(max_submissionrings + max_completionrings) * devinfo->dma_idx_sz * 2bytes using untrusted firmwareu16counts (pcie.c:1624-1627), then hands outBRCMF_NROF_H2D_COMMON_MSGRINGS + max_flowrings(2 + max_flowrings) H2D index slots (:1725-1737, :1763-1778) andBRCMF_NROF_D2H_COMMON_MSGRINGS(3) D2H index slots (:1739-1752) accessed via plain host pointer arithmetic (brcmf_pcie_read_idx()/brcmf_pcie_write_idx(),:639-654). - On
shared.version >= 6, firmware reportingmax_submissionrings < BRCMF_NROF_H2D_COMMON_MSGRINGS + max_flowringsormax_completionrings < BRCMF_NROF_D2H_COMMON_MSGRINGS(including0) causes out-of-bounds 16-bit reads/writes in host DMA memory. - Commit 2 validates those exact slot counts on the TCM-index branch only; validate
max_submissionrings >= BRCMF_NROF_H2D_COMMON_MSGRINGS + max_flowringsandmax_completionrings >= BRCMF_NROF_D2H_COMMON_MSGRINGSbefore allocatingidxbuf, and add aBRCMF_PCIE_SHARED_DMA_INDEXhost-index test case + mutation intools/asahi/brcmf-shared-tcm-tests.py(which currently only exercisesdma_idx_sz == 0).
- When
- §5 (Owed hardware verification rows & pending CI):
- CI at
afa278b972d1: 3/7 green (source,host-tests,rust-build); 4/7 stillqueued(kunit,DWC3/ATC×3). - Owed on hardware at the final head (default command line): Wi-Fi up,
s2idle×2, firmware console read (FWCON), and regression counts (0 SELinux denials, 0 failed units, 0 new kernel errors) on J713 and on one BCM4387/BCM4388 Mac (M1 Pro or J616s), plus a firmware-death recovery test on either.
- CI at
Non-blocking Technical Notes
- Console disabled sentinel (
pcie.c:1142-1151): When firmware reportsconsole->base_addr == 0("no console"),brcmf_pcie_tcm_range_valid(devinfo->tcm_size, 0, 12)succeeds andbrcmf_pcie_bus_console_init()reads offsets0..11(ARM vectors) before later readers ignore the console because!console->base_addr. Guardingif (!console->base_addr || !brcmf_pcie_tcm_range_valid(...))avoids bogus init reads/logs; resettingconsole->log_idx = 0on re-init would also drop any stale partial line across firmware restart. - Ring
r_ptr/w_ptrvalues (pcie.c:1464, 1482→commonring.c:213-221): Firmware-supplied ring pointer values are still not bounds-checked againstcommonring->depth(aw_ptr > depthyields an out-of-boundsn_itemsinbrcmf_commonring_get_read_ptr()). Fine to track in a follow-up patch, provided the PR description makes clear that ring-pointer value clamping is separate.
…dex buffer When the firmware keeps the ring indices in host memory, the index buffer is sized from the submission and completion ring counts it reports, while the driver hands out one index per common ring and flowring. A firmware that reports fewer submission rings than common rings plus flowrings, or fewer completion rings than common completion rings, made the driver read and write indices past the end of the buffer. Check both counts before the buffer is allocated and fail the probe with -EINVAL when they are short. Shared RAM versions before 6 derive the counts from the flowring count and always pass. tools/asahi/brcmf-shared-tcm-tests.py now uses the driver's own host index accessors and has controls for host indices; the mutation dma-counts drops the check. Signed-off-by: Ryan Murray <[email protected]>
A console address of 0 means the firmware has no console, but the console setup still read its descriptor at the start of the TCM before the readers ignored it. Skip the reads in that case, and drop a partial console line left from an earlier firmware when the console is set up again. tools/asahi/brcmf-shared-tcm-tests.py gets a control for a firmware without a console; the mutation console-none drops the check. Signed-off-by: Ryan Murray <[email protected]>
…the ring The read and write pointers of the message rings are read back from the dongle (from TCM or from the host index buffer) and used as ring positions: the common ring code computes item counts and buffer addresses from them. A pointer at or past the ring depth made it reach past the end of the ring buffer. Take a pointer only when it is below the ring depth; otherwise keep the last one, log it (rate-limited) and return -EIO, as the update does when the bus is down. tools/asahi/brcmf-shared-tcm-tests.py gets a control for both updates; the mutations rptr-depth and wptr-depth drop the checks. Signed-off-by: Ryan Murray <[email protected]>
|
Thanks. Addressed at 73d8f7c (three new commits, no history rewrite):
Tests: host Hardware rows (J713 + one BCM4387/4388 Mac: Wi-Fi up, s2idle x2, FWCON console read, regression counts, firmware-death recovery) are next on a kernel at this head. |
|
Review at 73d8f7c. Three new commits on top of afa278b, no history rewrite. Status of the points from my last review:
Dj's console notes are also done (2c9da32): no descriptor reads when the console address is 0, and Nothing new found. Small remark, not blocking: the ring-pointer check is Verdict: APPROVE on code. Rows owed at this head (none run): on the default command line, Wi-Fi up, s2idle x2 and a firmware console read on J713 and on one BCM4387/BCM4388 Mac (M1 Pro or J616s), plus one firmware-death recovery on either. The author has these next. CI is still running (4 pending). |
There was a problem hiding this comment.
Formal adversarial §1–§6 re-review at 73d8f7cde2683357de5cd04e28638f68785626c1 (REQUEST_CHANGES — Wave 60C host idxbuf blocker + both technical notes resolved cleanly with 0 remaining code blockers; still blocked on owed §5 hardware rows and 1 queued CI check).
Wave 60C Fix Verification (3 new commits, no history rewrite)
- Host
idxbufring-count check (6b6878e84275,pcie.c:1666-1683): Fixed. Before allocatingdevinfo->idxbufwhendevinfo->dma_idx_sz != 0, validatesmax_submissionrings >= BRCMF_NROF_H2D_COMMON_MSGRINGS + max_flowringsandmax_completionrings >= BRCMF_NROF_D2H_COMMON_MSGRINGS, failing probe with-EINVALbefore any allocation. Verified overflow-free sizing:max_flowrings <= 512,u16counts yieldbufsz <= (65535 + 65535) * 4 * 2 = 1,048,560bytes, and all H2D (2 + max_flowringswrite + read) and D2H (3write + read) index slots fit withinbufsz. Ifdma_alloc_coherent()fails, fallback todma_idx_sz = 0runs the TCM index range checks.tools/asahi/brcmf-shared-tcm-tests.pynow compiles the realbrcmf_pcie_read_idx()/brcmf_pcie_write_idx()with valid, short-submission, and zero-completion host-index controls plus thedma-countsmutation. - No-console sentinel &
log_idxreset (2c9da3201cd8,pcie.c:1143-1153): Fixed. Resetsbufsize,read_idx,log_idx,scan_idx, andscan_lenon init and skips descriptor reads whenconsole->base_addr == 0, with matching control andconsole-nonemutation. - Dongle
r_ptr/w_ptrbounds vs ring depth (73d8f7cde268,pcie.c:1468-1474, 1495-1501): Fixed.brcmf_pcie_ring_mb_update_rptr()and_wptr()rejectptr >= commonring->depth, preserving the last valid pointer, logging rate-limitedbrcmf_err, and returning-EIO. Verified againstcommonring.c(reserve_for_write*,get_read_ptr): keeping0 <= r_ptr, w_ptr < depthguarantees all ring buffer indexing stays in bounds. Controls andrptr-depth/wptr-depthmutations added tobrcmf-shared-tcm-tests.pyandrun-host-tests.py.
Checklist Summary
- §1 (No model gating): Pass.
- §2 (Production-ready, no hacks): Pass.
- §3 (Safety): Pass — TCM bounds, host
idxbufsizing, console bounds, trap record bounds, and ring pointer depth bounds are all verified sound. - §4 (Clean-room & attribution): Pass — all 6 commits carry human author/committer (
Ryan Murray <[email protected]>) and humanSigned-off-by:only (zero AI trailers in commits); PR body includesAssisted-by:. - §5 (Evidence): Blocked — see Remaining Blockers below.
- §6 (Naming): Pass.
Remaining Blockers
- §5 (Owed hardware verification rows): No hardware rows posted yet at
73d8f7cde268. Owed on the default command line: Wi-Fi up,s2idle×2,FWCONfirmware console read, and regression counts (0 SELinux denials, 0 failed units, 0 new kernel errors) on J713 and on one BCM4387/BCM4388 Mac (M1 Pro or J616s), plus a firmware-death recovery test on either. - CI still finishing: 6/7 green (
host-tests,source,rust-build,kunit,DWC3/ATC builtin-no-atc,DWC3/ATC modular-no-atc); 1/7QUEUED(DWC3/ATC builtin-modular-atc).
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge at 73d8f7cde268: the code is ready; it needs only a row at this head.
Blockers: none in the code. Missing row: one brcmfmac Mac at this head (J713, or a BCM4387/4388 Mac such as the J616s): Wi-Fi up, two s2idle cycles, and the firmware console read working.
Checked: no Apple material, no gates. Firmware-supplied TCM addresses are checked, and console reads stay in bounds. CI green.
Superseded by the review at 73d8f7c
|
|
|
|
|
MERGETRAIN6: HOLD at Cause: the new bounds check Routed to the owner lane (SEC-SOC2). Assisted-by: LLM (Claude:Opus 5.5) |
brcmfmac's PCIe code takes several addresses from dongle memory (shared RAM) and reads and writes through them without checking them against the TCM mapping. This series checks them where they are read, and stops the console reader from looping on a write index outside its buffer.
Problem
brcmf_pcie_bus_console_read()looping forever: the read index wraps at the buffer size and never meets it.Change
keep console reads inside the TCM and the console buffer: record the TCM mapping length. Accept the console only when its descriptor and buffer lie inside the mapping (otherwise it stays off, as when the firmware reports none), start reading at the beginning of the buffer, and stop on an out-of-range write index. This part also applies to mainline.check the shared RAM addresses against the TCM: check the shared RAM block, the mailbox words, the ring information, the ring memory and the TCM index arrays (one index per common ring and flowring) when they are read, and fail the probe with -EINVAL if one is outside. Also applies to mainline.read a trap record only inside the TCM: our stall/trap code. Read the trap record only inside the mapping (the firmware is declared dead either way), reset the trap scan with the console state, and start the scan at the buffer's beginning when the reported index is outside it.check the ring counts before sizing the host index buffer: with host-memory ring indices, fail the probe when the reported submission/completion ring counts are smaller than the rings the driver hands indices to (applies to mainline).read no console when the firmware reports none: no descriptor reads for console address 0; drop a stale partial line at console setup (applies to mainline).take ring pointers from the dongle only inside the ring: a read/write pointer at or past the ring depth is not taken; the last one stays and the update returns -EIO (applies to mainline).No gates; no new parameters. A firmware whose addresses are all inside the mapping sees no change.
Testing
brcmfmac-pcie-tcm(range helper) passes (arm64 qemu,LLVM=1).tools/asahi/brcmf-shared-tcm-tests.pyruns the driver's real shared RAM, ring, host-index, ring-pointer and console code against a fake TCM that asserts on every access outside it: 22 controls pass. Each of its 15 mutations drops one check and fails a control.tools/asahi/brcmf-recovery-tests.pymodels the TCM size and adds a trap-record control; the newtrap-boundmutation fails it.W=1build of brcmfmac: no warnings; checkpatch clean apart from the MAINTAINERS note.Still to run
brcmfmacdebug message level with FWCON). The same on one more BCM4387/BCM4388 Mac (M1 Pro or J616s), since the console change reaches every brcmfmac PCIe chip.Reviewer detail
BRCMF_SHARED_HOST_CAP2_OFFSET, the last field the driver touches.idxbufwas never allocated, so the early return leaks nothing. The ring memory check runs before any ring is allocated.brcmf_pcie_console_trapped()from the earlier version are gone: console init now validates both once, and only the existing write index check remains there.