Skip to content

dmaengine: apple-admac, ASoC: apple: aop_lpai: keep no client pointer across a restore; bound the low-power microphone's periods and ring by its format - #2934

Merged
DjDeveloperr merged 2 commits into
mainfrom
hardening/admac-lpai-20261123
Nov 25, 2026
Merged

DjDeveloperr merged 2 commits into
mainfrom
hardening/admac-lpai-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Two fixes in the Apple audio DMA and low-power microphone paths.

Problem and change

  1. dmaengine: apple-admac: the slave config saved for the restore after a system sleep kept the client's peripheral_config pointer, and the restore read the realign frame size through it again. A client only guarantees that memory for the duration of the call (the AOP audio driver passes the address of a local). The frame size is now taken once when the client configures, the saved copy carries no pointer, and the restore only programs the registers (admac_program_config()).
  2. ASoC: apple: aop_lpai:
    • the PCM's period limits are computed with checked arithmetic, and a format with an empty or oversized shortest period is refused when it is read (before, a division by zero or an overflow at PCM open);
    • the T6041 host ring is bounded before anything is allocated. Version 2 (review): the bound now follows the ring's own format, at most RING_SECONDS_MAX (16 s) of it, four times the 4 s the firmware describes at any rate and channel count, plus a fixed 64 MiB ceiling that is above 4 s of the fastest format the PCM offers. The first version's fixed 1 MiB cap is gone.

No parameters, no chip/board/firmware gates.

Testing

  • KUnit (arm64 qemu) on integration/aurora a7638d5 + this series:
    • C group (admac and the other Apple C tests): 52 run, 51 passed, 1 skipped (unrelated, needs CONFIG_UCLAMP_TASK); admac_restore_keeps_the_frame_size passes and FAILS without the admac fix.
    • Rust group: 60 run, 59 passed, 1 skipped. a_format_without_a_valid_period_range_is_refused and the_ring_geometry_is_bounded_by_its_format pass. The ring test covers 4 s of every rate (8 to 192 kHz) and 1 to 8 channels, 16 s at the time limit, the byte ceiling, and the old interval checks. With the first version's 1 MiB cap put back it FAILS.
  • checkpatch --strict: clean. rustfmt: the file's existing drift is unchanged (18 hunks before and after).
  • Hardware: see the rows below.

Still to run

  • An s2r speaker-sense capture with V/I aligned on a realign_sense board (J813, J815 or J714s); we have none in our lab.

Hardware rows

Pending: one-shot boots of fleet82 + this series on the default cmdline (M1 Pro, M2, J616s, J473), compared with the same rows on the production kernel. They will be added here.

Reviewer detail
  • admac: admac_device_config() calls admac_program_config(), then sets realign_bytes once from the client's config (the duplicate assignment is gone) and saves the copy with peripheral_config = NULL, peripheral_size = 0. admac_restore_chan() calls admac_program_config() on the saved copy, so realign_bytes stays as the client set it. MCA clients pass no peripheral_config and behave as before.
  • aop_lpai: ring_geometry(format, frames, quantum) refuses an empty ring, frames > rate * RING_SECONDS_MAX, bytes > RING_BYTES_MAX and the existing quantum checks, all with checked arithmetic. period_bytes_min() returns None outside 1..=PERIOD_BYTES_MAX; hw() uses the checked value. ring_step() is untouched.
  • Split out of Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 as asked in review; addresses its aop_lpai point (cap tied to the format).

…inter

admac_device_config() saves the whole dma_slave_config so that a channel
can be set up again after a system sleep took its registers. The copy
includes peripheral_config, a pointer to memory the client only
guarantees for the duration of the call: the AOP audio driver passes the
address of a local variable. admac_restore_chan() then calls
admac_device_config() on the saved copy and reads the realign frame size
through that pointer again, long after the client returned.

Split the register programming out into admac_program_config(), which
the restore calls on the saved copy. admac_device_config() takes the
frame size from the client's config once (it was assigned twice) and
stores the copy with peripheral_config cleared, so a restore keeps the
frame size the client configured and never touches the client's memory.

Add a KUnit case that configures a capture channel, overwrites the
client's frame size, marks the channel lost and restores it.

Fixes: d9b438c ("arm64: apple: support coordinated suspend-to-RAM on T8140")
Signed-off-by: Ryan Murray <[email protected]>
…format

The low-power microphone takes its stream format and, on T6041, its
ring geometry from properties of the AOP's audio service.

The PCM limits are computed from the rate: the shortest period is
PERIOD_MS_MIN of frames, and the most periods is the buffer limit divided
by it. A rate that gives no whole frame in that time made the shortest
period zero and the division one by zero, and a rate large enough
overflowed the product; both stop the kernel when the PCM is opened.
Compute the shortest period with checked arithmetic when the format is
read, and refuse a format whose shortest period is empty or longer than
the longest period the PCM offers. Such a format leaves the PCM out, as
the other unusable formats do.

The host ring was sized as frames times frame bytes with only a 4 GiB
limit, and was allocated before the binding. Bound it by the ring's own
format: at most RING_SECONDS_MAX (16) seconds of that format,
four times the four seconds the firmware describes at any rate and
channel count, and at most a fixed 64 MiB ceiling, which is above four
seconds of the fastest format the PCM offers. Refuse a larger ring
before anything is allocated, together with the report-interval checks
that were already there, in one helper.

Add KUnit cases for formats with and without a valid period range and
for ring geometries on both sides of the time and byte limits, and for
four seconds of every rate and channel count the ring takes.

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

Copy link
Copy Markdown
Contributor Author

Review at 85730ab.

  1. admac (apple-admac.c:1561-1565, 840-842): this is the same patch as in Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 (patch-id 546853e0). The saved cfg carries no peripheral_config, realign_bytes is read once from the client, and admac_restore_chan() only reprograms registers through admac_program_config(). No path reads the client's pointer after admac_device_config() returns, so a restore cannot leave a stale client pointer.
  2. aop_lpai ring (aop_lpai.rs:124-128, 436-449): this resolves the Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 cap finding. I recomputed the bound over every format parse_ring() accepts: 4 B container, 1-8 ch, any rate whose 10 ms period is 1..=65536 B. That is 4,452,700 formats, and none has a 4 s ring refused. The largest 4 s ring is 204899 Hz x 8 ch = 26,227,072 B, 2.56x under the 64 MiB ceiling, and the ceiling still leaves >= 10.2 s at the fastest format. The geometries from Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 now pass: 16 kHz x 4 ch is 1024000 B, 48 kHz x 3/4/8 ch is 2.3/3.1/6.1 MB.
  3. Nothing else changed. period_bytes_min() is the same as in Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911, and the KUnit sweep at aop_lpai.rs:1233 covers 8-192 kHz x 1-8 ch.

Rows owed at this head (none posted): an lpai microphone open on J616s and J713 with the dev_dbg geometry line (aop_lpai.rs:688) captured; s2r MCA playback on J616s or M2 (exercises the restore). The realign_sense s2r V/I capture (J813, J815 or J714s) is still open, as the body says.

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

Checklist Summary

  • §1 No model gating: Pass — apple-admac preserves client-supplied realign frame size without chip checks; aop_lpai bounds the ring from the stream's own format (RING_SECONDS_MAX = 16 s) plus a 64 MiB ceiling well above 4 s of the fastest offered PCM format.
  • §2 Production-ready: Pass — no new parameters or hacks.
  • §3 Safety: Pass —
    • apple-admac.c: splits register programming into admac_program_config() (which never touches peripheral_config/peripheral_size), reads adchan->realign_bytes once in admac_device_config() (dropping the duplicate assignment), clears adchan->cfg.peripheral_config = NULL and peripheral_size = 0, and leaves adchan->realign_bytes/adchan->cfg untouched on programming failure. admac_restore_chan() calls admac_program_config() only, closing the dangling stack-pointer UAF on S2R restore.
    • aop_lpai.rs: StreamFormat::period_bytes_min() uses checked u64 arithmetic and enforces 1..=PERIOD_BYTES_MAX (65536), guaranteeing hw() never divides by zero and BUFFER_BYTES_MAX / period_min >= 8 >= PERIODS_MIN (2). ring_geometry() uses checked arithmetic to reject empty rings, frames > rate * 16, bytes > 64 MiB, quantum == 0, and quantum > frames / 2; every valid 4 s format (up to 204899 Hz x 8 ch x 4 B = 26.2 MiB) passes comfortably under the 64 MiB ceiling.
  • §4 Clean-room & attribution: Pass — both commits (fa152b667f91, 85730ab049e3) carry human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by:, zero AI trailers in commits.
  • §5 Evidence: Pending — KUnit C + Rust suites pass (admac_restore_keeps_the_frame_size, a_format_without_a_valid_period_range_is_refused, the_ring_geometry_is_bounded_by_its_format), but hardware rows are still owed (lpai open + dev_dbg geometry on J616s/J713, s2r MCA restore, and realign_sense s2r V/I capture on J813/J815/J714s).
  • §6 Naming: Pass.
  • CI (at review time): host-tests queued.

Verdict

COMMENT — 0 code/policy blockers; waiting on queued host-tests CI to complete green and the owed §5 hardware rows.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent source review at 85730ab049e3. The low-power capture path now derives period and ring limits from the firmware-reported format, and the ADMAC resume path stops using saved client state. Please verify on the J413 that a silent mic capture stopped at suspend can reopen at the advertised format after wake, with no stale samples, xrun or DMA error.

@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 re-review at 85730ab049e3fa178ab7618c1095aa09688c43f8 against live integration/aurora (70a00e583b03).

Status & Code Assessment (0 code, CI, or attribution blockers)

  • Not superseded: Live integration/aurora still retains peripheral_config across S2R restore in drivers/dma/apple-admac.c:839-840,1523-1553 and lacks checked period/ring-geometry bounds in sound/soc/apple/aop_lpai.rs:627-634,788.
  • §1 No model gating: Pass — no chip/SoC/board/firmware gates; aop_lpai derives ring limits from the firmware-reported stream format (RING_SECONDS_MAX = 16 s) plus a 64 MiB upper cap well above 4 s of the fastest valid PCM format (204899 Hz x 8 ch x 4 B = 26.2 MiB).
  • §2 Production-ready & §3 Safety: Pass —
    • apple-admac.c: splits register programming into admac_program_config() (non-sleeping, safe under adchan->lock during admac_restore_chan()), reads realign_bytes once in admac_device_config() (removing the duplicate assignment), clears adchan->cfg.peripheral_config = NULL and peripheral_size = 0, and preserves state on failure — closing the dangling stack-pointer UAF on S2R restore.
    • aop_lpai.rs: StreamFormat::period_bytes_min() enforces 1..=PERIOD_BYTES_MAX with checked u64 arithmetic so hw() cannot divide by zero or invert bounds; ring_geometry() validates frames, bytes, and quantum with checked arithmetic before adata.bound_ring() allocates anything.
  • §4 Clean-room & attribution: Pass — both commits (fa152b667f91, 85730ab049e3) carry human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by: only, zero AI trailers; Assisted-by: is in the PR body.
  • §6 Naming & Live CI: Pass — host-tests is COMPLETED / SUCCESS on 85730ab049e3, and QEMU arm64 KUnit C + Rust suites pass.

Blocker Holding APPROVE / MERGETRAIN-LINUX4

  1. §5 Hardware test & regression rows missing: Zero hardware rows have been posted yet (### Hardware rows still lists fleet82 one-shot boots on M1 Pro, M2, J616s, J473 as Pending). Please post exact-head hardware evidence on the default cmdline covering:
    • aop_lpai low-power microphone open/capture (including dev_dbg ring/format geometry on J616s / J713 or affected AOP Macs);
    • ADMAC restore path across suspend/resume (e.g., audio playback/capture across S2R on J616s/M2/J413, confirming clean reopen with no stale samples, xruns, or DMA errors);
    • §5 regression numbers (0 SELinux denials, 0 failed units, 0 new kernel errors).

@github-actions github-actions Bot added area:audio 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
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at 85730ab0: not ready yet

Check Detail
CI at head ok 1 check passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head no no line with the head SHA 85730ab0, a Mac and a result in the PR body or a comment
Conflicts ok not computed yet
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:audio

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

@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 85730ab049e3: the code is ready; it needs only a row at this head.

Blockers: none in the code. Missing row: one boot at this head with the aop_lpai low-power mic opening and capturing, and an ADMAC audio stream across a restore (an s2idle), on one of the pending Macs (M1 Pro, M2 or J616s).

Checked: no Apple material, no gates. No client pointer is kept across a restore. CI green.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 25, 2026 04:27

Superseded by the review at 85730ab

@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 25, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

85730ab049e3 J316s (M1 Pro, T6000) + J413 (M2 Air, T8112) PASS (guarded one-shot 7.3.0-aurora-pr2934+, default cmdline, silence throughout; both restored to saved 7.3.0-aurora-fleet89):

  • J316s (boot_id=0f6ff7f8): both apple-admac controllers (293980000, 39b400000) and snd-soc-aop (AppleJ316HPAI: hpai hw:0,0 + lpai hw:0,1) bound cleanly (audio_err=0); 2 s silent capture PASS before s2idle (lpai: 64044 B / 32000 frames @ 16000 Hz, hpai: 64044 B / 32000 frames), rtcwake -d rtc1 -m freeze -s 8 (suspend_stats: success=1 fail=0), and 2 s silent capture PASS after resume (lpai: 64044 B / 32000 frames, hpai: 64044 B / 32000 frames); GL (Apple M1 Pro (G13S C0)) + Borealis VK PASS, Enforcing, 0 failed, 0 AVC, 0 new audio/ADMAC/AOP errors, built-in speakers silent. Restored saved fleet89 (boot_id=8583e3cc, trial removed).
  • J413 (boot_id=387f59c7): both apple-admac controllers (238200000, 24a980000) and snd-soc-aop (AppleJ413HPAI: hpai hw:0,0 + lpai hw:0,1) bound cleanly (audio_err=0); 2 s silent capture PASS before s2idle (lpai: 64044 B / 32000 frames @ 16000 Hz, hpai: 64044 B / 32000 frames), rtcwake -d rtc1 -m freeze -s 8 (suspend_stats: success=1 fail=0), and 2 s silent capture PASS after resume (lpai: 64044 B / 32000 frames, hpai: 64044 B / 32000 frames); GL (Apple M2 (G14G B0)) + Borealis VK PASS, Enforcing, 0 failed, 0 AVC, 0 new audio/ADMAC/AOP errors, built-in speakers silent. Restored saved fleet89 (boot_id=15a26ffc, trial removed).

@DjDeveloperr
DjDeveloperr merged commit 42d693c into main Nov 25, 2026
4 checks passed
@RyanTheTide
RyanTheTide deleted the hardening/admac-lpai-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:audio Area from CODEOWNERS (bot) needs-hw-row No hardware row at the PR head (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants