Skip to content

iio: aop_motion, aop_devmotion: free the IIO device with the device's resources; write the whole buffer scan - #2936

Merged
DjDeveloperr merged 3 commits into
mainfrom
hardening/aop-iio-20261123
Nov 24, 2026
Merged

DjDeveloperr merged 3 commits into
mainfrom
hardening/aop-iio-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Two lifetime/initialisation fixes in the AOP motion sensor drivers.

Problem and change

  1. iio: aop_motion: the IIO device was freed when the driver data was dropped, which happens before the device-managed trigger and triggered buffer are released, and the buffer's release reads the IIO device. The IIO device and its private data now belong to the platform device's resources, registered before the trigger and the buffer, so they are released last.
  2. iio: aop_devmotion: the buffer scan is 32 bytes and the IIO core copies all 32, but only 24 were written. The tail is now an explicit zeroed field, as in the sibling drivers.

No behaviour change beyond the fixes: start/stop and direct-read logic are untouched.

Testing

  • KUnit (arm64 qemu) on integration/aurora a7638d5 + this series: Rust group 60 run, 59 passed, 1 skipped (unrelated). iio_device_is_freed_with_the_device_resources and scan_is_written_in_full pass and each FAILS without its fix.
  • checkpatch --strict: clean.
  • Hardware: see the rows below.

Still to run

  • The same rows under KASAN (our fleet config has no KASAN).

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

The triggered buffer and the trigger of each sensor are resources of the
platform device, and the release of the triggered buffer reads the poll
function and the buffer out of the IIO device. The IIO device, though,
was freed when the driver's data was dropped, which the driver core does
before it releases the resources of the device, both on unbind and when
a probe fails after the buffer is set up. The buffer release then read
an IIO device that was already freed.

Hand the IIO device, its private data and the channel table it points to
to the resources of the platform device, right after the allocation and
before the trigger and the buffer are set up. Resources are released in
reverse order, so the buffer and the trigger now go first and the IIO
device last. Dropping the driver's data only unregisters the device.

MotionIio::new() takes the bound device instead of the platform device,
so that a KUnit test can run it on a faux device: the test checks that
the private data of the IIO device is still there when the release of
the device's resources begins and gone when it has ended.

Signed-off-by: Ryan Murray <[email protected]>
A scan is a quaternion and a timestamp: 24 bytes, which the 16-byte
alignment of the quaternion rounds up to 32. The IIO core computes the
same 32 bytes and copies them all into the buffer, but the last eight
were padding of the scan structure, which nothing wrote.

Make the tail a field and build the scan in one place that zeroes it, as
the accelerometer, gyroscope and temperature drivers already do for
their scans.

Add a KUnit test that derives the scan size from the channels as the IIO
core does, compares it with the structure, and checks that every byte
after the timestamp of a freshly built scan is zero.

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

Copy link
Copy Markdown
Contributor Author

Review at 6d16ae0.

  1. This is the same code as in Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911. Both commits match by patch-id: aop_motion 8004b1c9 (4bc3a3c = 15984b4) and aop_devmotion 02a731ed (6d16ae0 = 2be17cf). No Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 finding applied to them, and the split addresses finding 4.
  2. aop_motion.rs:611-621: IioAlloc is registered right after iio_device_alloc and before the devm trigger and buffer, so devres releases the buffer, then the trigger, then the iio_dev. iio_triggered_buffer_cleanup() therefore no longer reads a freed indio_dev on unbind or on a late probe failure.
  3. aop_devmotion: the 8-byte scan tail is now zeroed (test at aop_devmotion.rs:1037), which closes the stack bytes previously copied to userspace.

Rows owed at this head (none posted): aop_motion unbind/rebind under KASAN, plus a devmotion buffered read, on J616s, J713 and M1 Pro. #2910 touches the same use kernel::{...} line, so whichever PR lands second needs a rebase.

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

Checklist Summary

  • §1 No model gating: Pass.
  • §2 Production-ready: Pass.
  • §3 Safety: Pass —
    • aop_motion.rs: IioAlloc (owning indio_dev, the foreign Arc<State> in priv_, and pinned Spec) is registered with devres::register() immediately after iio_device_alloc() and before __devm_iio_trigger_alloc(), devm_iio_trigger_register(), and devm_iio_triggered_buffer_setup_ext(). If devres::register() fails, IioAlloc::drop() frees priv_ and indio_dev cleanly; otherwise devres unwinds in reverse order (triggered buffer -> trigger -> IioAlloc), eliminating the unbind / late-probe-failure UAF where iio_triggered_buffer_cleanup() read a freed indio_dev. In IioAlloc::drop(), Drop::drop reclaims priv_ and calls iio_device_free(self.dev) before _spec is dropped.
    • aop_devmotion.rs: Scan (#[repr(C, align(16))]) now explicitly declares and zeroes pad: u64 in Scan::new(), closing the 8-byte uninitialized stack-padding leak copied into IIO buffers.
  • §4 Clean-room & attribution: Pass — both commits (4bc3a3cc7f39, 6d16ae09a465) carry human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by:, zero AI trailers in commits.
  • §5 Evidence: Pending — Rust KUnit cases (iio_device_is_freed_with_the_device_resources, scan_is_written_in_full) pass (59/60, 1 unrelated skip), but hardware rows (unbind/rebind + KASAN, and aop_devmotion buffered read on M1 Pro / M2 / J616s / J713) are still owed.
  • §6 Naming: Pass.
  • CI (at review time): host-tests green.

Non-blocking Note

  • As noted in the PR body, this touches the same use kernel::{...} import line in aop_motion.rs as #2910 / #2927; whichever merges second will need a trivial rebase.

Verdict

COMMENT — 0 code/policy blockers and CI is green; waiting on the owed §5 hardware rows before approval.

@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 6d16ae09a465. The lifetime change moves the IIO device below its trigger and buffer in the resource release order. Please extend user-path evidence to an active triggered scan on J413 followed by close or unbind, and check that each scan supplies the full channel layout and timestamp. Coordinate this row with #2927 because both change the same motion drivers.

Keep the device-resource imports for the IIO lifetime fix and the delayed-work import from the current AOP motion driver. Both AOP Rust objects compile for arm64.

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

Copy link
Copy Markdown
Contributor Author

The integration conflict is resolved at dbb9d5a05b87416fac24b48835aad840e6c673be. The resolution keeps both the device-resource and delayed-work imports in aop_motion.rs. The diff against integration remains limited to the two AOP IIO drivers.

Both Rust objects compiled for arm64 using the fleet85 configuration and the kernel's corrected bindgen. The previous KUnit result is documented in the body. The physical unbind/rebind and buffered-read rows on J616s, J713 and J316s are still open.

@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 verified live head dbb9d5a05b87416fac24b48835aad840e6c673be (CI 1/1 green: Host tests / host-tests).

Technical Audit

  • aop_motion.rs lifetime & open-handle safety: IioAlloc (owning indio_dev, the foreign Arc<State> in priv_, and pinned Spec) is registered via devres::register() immediately after iio_device_alloc() and before __devm_iio_trigger_alloc(), devm_iio_trigger_register(), and devm_iio_triggered_buffer_setup_ext(). Reverse-order devres teardown now runs iio_triggered_buffer_cleanup(indio_dev) -> trigger cleanup -> IioAlloc::drop(), eliminating the unbind and late-probe-failure UAF on indio_dev. Verified against drivers/iio/industrialio-core.c and industrialio-buffer.c: MotionIio::drop() calls iio_device_unregister() (cdev_device_del drains sysfs, disables buffers under info_exist_lock, clears indio_dev->info = NULL, wakes pollers), so any open /dev/iio:deviceX handle holds &indio_dev->dev and returns -ENODEV without touching priv_ or Spec, and iio_dev_release() only puts trigger/buffer refs and frees attribute lists.
  • aop_devmotion.rs parity & scan padding: Verified aop_devmotion.rs does not share the indio_dev UAF because devm_iio_kfifo_buffer_setup_ext() registers devm_iio_kfifo_release(buffer) (referencing buffer, not indio_dev, with iio_device_attach_buffer() holding its own iio_buffer_get() ref). Adding explicit zeroed pad: u64 in Scan::new() cleanly closes the 8-byte stack-padding leak into the 32-byte IIO scan buffer, covered by KUnit scan_is_written_in_full.
  • Merge resolution (dbb9d5a05b87): Cleanly keeps both devres and new_delayed_work imports in aop_motion.rs.

Checklist Summary

  • §1 No model gating: Pass.
  • §2 Production-ready: Pass.
  • §3 Safety: Pass (0 code blockers).
  • §4 Clean-room & attribution: Pass (all 3 commits including merge dbb9d5a05b87 carry human author/committer Ryan Murray <[email protected]> and human Signed-off-by: only, zero AI trailers in commits; PR body disclosure present).
  • §5 Evidence: Blocked — see below.
  • §6 Naming: Pass.

Remaining Blocker

  1. §5 — Owed hardware rows & exact-head KUnit rerun: Zero physical hardware rows have been posted yet (both ### Hardware rows and your comment at dbb9d5a05b87 note that physical aop_motion unbind/rebind and aop_devmotion buffered-read rows on J616s, J713/J413, and J316s remain open), and the cited KUnit run predates merge commit dbb9d5a05b87 (CI only ran host-tests). Please post the physical hardware rows with the full §5 regression triad (SELinux Enforcing, 0 AVC denials, 0 failed units, 0 new kernel errors) and confirm the Rust KUnit pass at dbb9d5a05b87.

Non-blocking Nit

  • Pre-existing OOM error-path nit in aop_motion.rs:877-897: (*indio_dev).trig = iio_trigger_get(trig) runs before devm_iio_triggered_buffer_setup_ext() sets INDIO_BUFFER_TRIGGERED in indio_dev->modes; if buffer setup fails before setting that mode bit, iio_dev_release() skips iio_device_unregister_trigger_consumer() and leaks that extra trigger ref. Moving the iio_trigger_get() assignment after devm_iio_triggered_buffer_setup_ext() (or checking cleanup) would close that edge case.

Verdict

REQUEST_CHANGES — code is clean; blocked solely on owed §5 hardware evidence (and exact-head KUnit confirmation at dbb9d5a05b87).

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Physical rows for dbb9d5a05b87416fac24b48835aad840e6c673be. The two signed modules were built from the exact PR sources against fleet85. The Macs used their production kernel command lines.

Mac Accelerometer unbind/rebind Gyroscope unbind/rebind Devmotion buffered read Enforcing / AVC / failed units / new kernel errors
J316s (M1 Pro) 3/3 3/3 Service absent Yes / 0 / 0 / 0
J713 (M4 Air) 3/3 3/3 One 32-byte scan, all 8 tail bytes zero Yes / 0 / 0 / 0
J616s (M4 Pro) 3/3 3/3 One 32-byte scan, all 8 tail bytes zero Yes / 0 / 0 / 0

All 18 open buffered descriptors returned ENODEV after unbind. Both sensors rebound on every cycle. The stock modules and IIO proxy were restored on all three Macs.

The exact-head Rust KUnit build found a fixture regression after the merge. iio_device_is_freed_with_the_device_resources still calls Stream::default() (E0599) and leaves Control.idle_work uninitialized (E0063). A local test-only correction using Stream::new() and new_delayed_work!("Control::idle_work") passed both AOP suites on arm64 QEMU, 2/2, including the lifetime and full-scan tests. The PR head still has the build failure.

Assisted-by: LLM (Codex)

@github-actions github-actions Bot added 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 dbb9d5a0: not ready yet

Check Detail
CI at head ok 1 check passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head no no line with the head SHA dbb9d5a0, 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: none matched in CODEOWNERS

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: MERGE at dbb9d5a05b87

Blockers: none.

Checked: no Apple material, no gates. The IIO device is freed with the device's resources (after the trigger and buffer), and the devmotion scan tail is zeroed. Physical rows from modules built at this head on J616s, J713/J413 and J316s: accelerometer and gyroscope unbind/rebind, devmotion buffered reads, Enforcing, 0 AVC, 0 failed units.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 24, 2026 21:00

Superseded by the review at dbb9d5a

@DjDeveloperr
DjDeveloperr merged commit 7a95696 into main Nov 24, 2026
2 of 3 checks passed
@RyanTheTide
RyanTheTide deleted the hardening/aop-iio-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

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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants