Skip to content

soc: apple: pmp: bound the memory held for the firmware - #2905

Merged
RyanTheTide merged 1 commit into
mainfrom
hardening/pmp-fw-alloc-bounds-20261123
Nov 25, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
hardening/pmp-fw-alloc-bounds-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Bound the memory and register entries the Apple PMP driver holds for the firmware. Freeing a buffer returns its allocation budget. Register entries are accepted while their index fits the firmware reply.

Tested

J713 (M4 Air) and J616s (M4 Pro), exact aa132a243fcd, on the default kernel command line. Both passed normal PMP probe and two short RTC wakes. Live allocation counts, allocation bytes and register counts remained stable. Both returned to their saved kernels with SELinux enforcing and zero failed units. The arm64 build passed, and the allocation host suite passed 5/5.

Risk

Two-way door for Macs using Apple PMP. A request outside the driver's allocation or register budget is rejected. Test counts on both machines stayed well within the budget.

Not yet

Repeat the same ordinary PMP probe and wake checks on Neo.

Review and test details
  • The budget counts up to 64 live buffers and tracks total allocated bytes based on the message's maximum buffer size. The registration limit follows the width of the reply field. Claim, allocation and release stay under the PMP state mutex.
  • J713 had two live allocations totaling 32,768 bytes and 459 register entries before both wakes and after each. J616s had two allocations totaling 32,768 bytes and 523 register entries at all three checkpoints. Both reported zero PMP refusals.
  • Boot-time register index behavior remains a follow-up.

Exact hardware rows · Independent review

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 four of the largest buffer a single request can ask
for (its size field is 24 bits, so just under 64 MiB). The driver takes
the size out of a request with the same constant. 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. On the board that keeps the display bridge,
that also stops display power commands, as any request the driver cannot
handle does there.

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]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Row for the open question (does a wake register again?): J713 on its production default kernel (fleet81, default cmdline), runtime kprobes on apple_rtkit_send_message and apple_rtkit_rx_dispatch filtered to endpoint 0x20, then s2idle, s2idle, deep (15 s rtc wake each, all succeeded). Across the three sleeps the firmware sent no MALLOC, FREE, REGISTER_IOREG or SET_IOREG request, and the driver sent no reply to one. That holds even though the PMP RTKit re-initialises on the deep wake. So neither the register list nor the allocation budget grows per wake, and sleeping doesn't reach the caps here. The boot-time index question (1-based reply vs 0-based lookup) needs a boot-time trace and is still open. The default-cmdline row with this PR's kernel is still to run.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at aa132a2.

Follow-up to the #2894 review, PMP part. The code is correct at this head; what is left is rows and two small points.

Fixed from #2894:

  • Size constant (item 6): one constant now, MALLOC_SIZE_MAX = 0xff_ffff (pmp_alloc.rs:7), and MALLOC_SIZE_MASK is defined from it (pmp.rs:90). The off-by-one and the duplicate are gone. claim()/release() pair on every error path (pmp.rs:328-355), and free() gives back alloc.size(), which is the requested byte count for a Coherent<[u8]> (pmp.rs:361-362).
  • Register index (item 7, first half): ioreg_fits() (pmp_alloc.rs:49, pmp.rs:397) keeps the index within its 16 bits.

Still open:

  1. ioreg_entries is still only pushed (pmp.rs:465), and allocs is only reduced by an OPC_FREE. The count cap makes the wake question sharper than before: if a wake, or any firmware restart, repeats the boot's MALLOCs without freeing, the 64-allocation cap (pmp_alloc.rs:10) refuses on about the sixth wake, where the old code leaked but kept working, and on the board that keeps the display bridge that latches fail_display() (pmp.rs:855-856). wake() says the firmware keeps what it was given (pmp.rs:599-600), so this should not happen, but nothing measured shows it yet. The rows below must show the counts.
  2. "About ten buffers and 340 registers" is the KVec capacity hint (pmp.rs:147, :150), not a measurement. Please log the peak live count and bytes per board in the rows, so the 64 / 64 MiB bounds rest on numbers.
  3. Nit: a refused request logs two unratelimited dev_err lines per message (pmp.rs:329 or :398, then :858). A firmware that keeps asking fills the log; ratelimit the refusals.
  4. Unchanged, still a question (item 8): the reply index is 1-based (len() after the push, pmp.rs:466) and set_ioreg() looks up 0-based (pmp.rs:474-475). The row should log the indices the firmware sends back.

Rows owed at this head (none run): default command line, PMP probe, s2idle x2, with allocation count/bytes and register count after boot and after each wake, on J713, Neo (t8140-j700.dts:501) and J616s (t604x-j614-j616.dtsi:2769 also enables &pmp; the PR lists only the first two).

@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 adversarial §1–§6 pre-PR checklist review at aa132a243fcd05c7a738252dc8af0ff06f66a351 (REQUEST_CHANGES — 0 code blockers; pending CI and owed §5 exact-head hardware rows).

Checklist Summary

  • §1 (No model gating): Pass — no chip, SoC, board, or firmware-version gates.
  • §2 (Production-ready, no hacks): Pass — no new parameters; bounds derive from the wire format (24-bit size mask, 16-bit ioreg reply index) rather than board tables.
  • §3 (Safety — integer bounds, locking, rollback): Pass — MALLOC_SIZE_MASK shares pmp_alloc::MALLOC_SIZE_MAX = 0xff_ffff (pmp.rs:90, pmp_alloc.rs:7). AllocBudget::claim() (pmp_alloc.rs:140-148) checks size > MALLOC_SIZE_MAX || self.count >= MAX_ALLOCS || size > MAX_ALLOC_BYTES - self.bytes, maintaining self.bytes <= MAX_ALLOC_BYTES without overflow, and release() uses saturating_sub. In PmpData::malloc() (pmp.rs:324-357), self.state.lock() (sleepable Mutex, already held across coherent DMA allocation in get_iova_table()) serializes claim(), Coherent::<u8>::zeroed_slice(), and allocs.push(), and every error path after claim() calls release(size). free() (pmp.rs:360-362) releases freed.alloc.size() as u64. ioreg_fits() (pmp_alloc.rs:158-160, pmp.rs:397) enforces registered < 0xffff, ensuring the pushed 1-based reply index <= 0xffff never collides with OPC_SHIFT (48).
  • §4 (Clean-room & attribution): Pass — commit aa132a243fcd carries human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by: only (zero AI trailers in commits); PR body includes Assisted-by:.
  • §5 (Evidence): Blocked — see Blockers below.
  • §6 (Naming): Pass.

Blockers

  1. §5 (Owed exact-head hardware rows): While the kprobe measurement on J713 fleet81 confirming 0 MALLOC/FREE/REGISTER_IOREG/SET_IOREG traffic across s2idle×2 + deep sleep is valuable supporting evidence, no boot + sleep/wake row has been run at this head (aa132a243fcd) yet. Owed on the default command line: PMP probe + s2idle ×2 with measured peak live allocation count/bytes and register count (at boot and after each wake) plus regression counts (0 SELinux denials, 0 failed units, 0 new kernel errors) on J713, Neo (t8140-j700), and J616s (t604x-j614-j616.dtsi also enables &pmp).
  2. CI pending: 5/7 green (source, host-tests, kunit, rust-build, DWC3/ATC builtin-no-atc); 2/7 still queued (DWC3/ATC modular-no-atc, DWC3/ATC builtin-modular-atc).

Non-blocking Notes

  • Refused OPC_MALLOC / OPC_REGISTER_IOREG requests currently emit two unratelimited dev_err! messages per request (pmp.rs:329/398, then pmp.rs:858); ratelimiting refusals would prevent log flooding on a misbehaving firmware.
  • Pre-existing 1-based register_ioreg() reply (len() after push, pmp.rs:466) vs 0-based set_ioreg() lookup (get(index), pmp.rs:475) is unchanged here; logging the indices firmware returns during the boot row will confirm whether that is a separate bug to fix.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Thanks. Rows on J713, Neo and J616s (PMP probe, s2idle x2, live allocation count/bytes and register count at boot and after each wake, regression counts) will run on a kernel at this head; the kprobe counter is ready. On the non-blocking note: the Rust kernel crate here has no ratelimited dev_err!. A refusal only happens on a firmware asking past the caps, and the generic 'Failed to handle rtkit message' print after it is pre-existing for any message the driver can't handle. I'd rather fix both prints together in a follow-up than add a one-off limiter here.

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026

@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.

Verdict: FINISH, then merge at aa132a243fcd: the code is ready; it needs only a row at this head.

Blockers: none in the code. Missing row: as you said on 11-23: J713, Neo and J616s at this head, each with a PMP probe and s2idle ×2, with the live allocation count and bytes and the register count at boot and after each wake. One of them is enough to merge; the others come after.

Checked: no Apple material, no hardcoded limit from one machine: the bounds cap what the firmware can make the kernel hold, and a request past them is refused. The host test passes 5/5 and fails 4 of 5 with the bounds removed. The J713 kprobe run shows no MALLOC/FREE/REGISTER traffic across sleeps. CI green. This replaces my earlier review.

Evidence

Read aa132a24 (pmp_alloc.rs, pmp.rs, the host test).

@DjDeveloperr
DjDeveloperr dismissed their stale review November 24, 2026 19:57

Superseded by the review at aa132a2

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at aa132a24: not ready yet

Check Detail
CI at head running running: pr-text / pr-text
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at aa132a24
Conflicts ok none
PR text ok Risk: two-way · for Macs using Apple PMP. A request outside the driver's allocation or register budget is rejected. Test counts on both machines stayed well within the budget.

Areas: area:gpu, area:platform

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

aa132a2 J713 (M4 Air T8132, macOS 27.2 fw) + J616s (M4 Pro T6040, macOS 26.6.2 fw) PASS: guarded one-shot 7.3.0-aurora-pr2905+ (exact aa132a2 on ef1c985, default cmdline, boot-ctxr.bin untouched); live PmpState read read-only from /proc/kcore via pmp.o DWARF offsets + runtime kprobe on apple_rtkit_send_message (ep 0x20) across 2x rtc1 s2idle wakes:

  • J713 (boot_id=738dccf8): normal PMP probe (RTKit v12, PMP started, apple-pmp-thermal active, 0 refusals); before wake 1 -> after wake 1 -> after wake 2 (suspend_stats success=2 fail=0): allocs_len=2, alloc_budget.count=2, alloc_budget.bytes=32768, ioreg_entries_len=459, ioreg_ids_len=459 (all identical across both wakes); kprobe ep 0x20: 0 MALLOC (0x13) / FREE (0x15) / REGISTER_IOREG (0x33) / SET_IOREG (0x35) replies, kprobe_remaining=0; Enforcing, 0 failed units, 0 PMP errors; restored and verified 7.3.0-aurora-fleet85.
  • J616s (boot_id=eba6163a): normal PMP probe (RTKit v12, PMP started, apple-pmp-report status 0x1, 0 refusals); before wake 1 -> after wake 1 -> after wake 2 (suspend_stats success=2 fail=0): allocs_len=2, alloc_budget.count=2, alloc_budget.bytes=32768, ioreg_entries_len=523, ioreg_ids_len=523 (all identical across both wakes); kprobe ep 0x20: 0 MALLOC/FREE/REGISTER_IOREG/SET_IOREG replies, kprobe_remaining=0; eDP-1 connected, Borealis gpubench-vk (render.light 106.95 us, compute.light 97.32 us) + gpubench-gl (render.postfx 1080p 2080 fps, crc=2133faaa valid=true) ok; 0 GPU faults, 0 failed units, Enforcing + 0 AVC, 0 PMP errors; restored and verified 7.3.0-aurora-fleet89.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-hw-row No hardware row at the PR head (bot) labels Nov 25, 2026
@github-actions github-actions Bot added door:two-way Revertible or not, from Merge risk (bot) and removed needs-template PR body lacks Tested or Risk (bot) ready-to-merge Meets the merge policy: merge it (bot) labels Nov 25, 2026
@RyanTheTide
RyanTheTide merged commit f408c8d into main Nov 25, 2026
13 of 14 checks passed
@RyanTheTide
RyanTheTide deleted the hardening/pmp-fw-alloc-bounds-20261123 branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) area:platform Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants