Skip to content

media: apple: ave: check the MCTF input picture against its plane without wrapping - #3199

Merged
RyanTheTide merged 2 commits into
mainfrom
fix/ave-mctf-input-offset
Nov 28, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
fix/ave-mctf-input-offset

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: The MCTF copies each OUTPUT picture from the plane's data_offset on. Its bounds check added data_offset and the picture size in 32 bits, so with a plane near 4 GiB and data_offset at its end the sum wrapped, the check passed and the copy read past the plane. The sum is now taken without wrapping, and such a buffer is refused at QBUF with -EINVAL.

Tested: KUnit apple-ave-mctf on arm64 QEMU, with new cases for the wrapped offset at QBUF and at the copy, an offset one byte too far, and pictures that end exactly at the end of the plane. J700 (Neo) at 63fc16c68f29, in the combined candidate 7.3.0-neo-combined1126, which carries it unchanged: H.264 1080p encode 300/300 frames with md5 identical to base, MCTF 120/120 frames, and an MCTF data_offset past the plane refused at QBUF. 0 DART faults. J714s (M5 Pro) at 63fc16c68f29, in the combined candidate 7.3.0-m5-rows-sec2, which carries it unchanged: AVE encode 300 frames, PSNR equal to base. 0 DART faults.

Risk: two-way · Neo, the only Mac whose encoder firmware runs MCTF. Encoder buffers skip the new check.

Not yet: nothing.

Details
  • ave_mctf_input_fits() checks data_offset + bytes <= plane size with check_add_overflow() on size_t. ave_mctf_put_picture() uses it for both input formats (lossless and NV12) before copying.
  • ave_buf_prepare() runs the same check for OUTPUT buffers of an MCTF context, with the size ave_mctf_input_bytes() gives for the negotiated format. The bad buffer is refused at QBUF, and the copy-site check stays as the guard for the copy itself.
  • vb2 only keeps data_offset below bytesused, and the encoder places its own input in ave_sess_input(). Encoder contexts therefore keep the old buf_prepare() behaviour.
Evidence
  • KUnit: ./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 --kunitconfig=drivers/media/platform/apple/ave/.kunitconfig. The new case is apple-ave-mctf.ave_mctf_input_offset.
  • Before the fix, the lossless case with a U32_MAX plane and data_offset = U32_MAX - 1 passed the check, and the copy ran past the plane. The new case refuses it at QBUF and at the copy.

…hout wrapping

The MCTF copies a whole input picture from the OUTPUT plane's data_offset
on. Its bounds check added data_offset and the compressed picture size,
both u32, in 32 bits before comparing with the plane size. vb2 only keeps
data_offset below bytesused, so with a plane near 4 GiB and data_offset
at its end the sum wraps, the check passes and the copy reads past the
plane.

Check the picture with check_add_overflow() on size_t, for both input
formats, and refuse such a buffer already in buf_prepare, so QBUF fails
with -EINVAL instead of the job failing later. The encoder video device
is unchanged: it places its input itself in ave_sess_input().

Add KUnit cases for the wrapped offset at QBUF and at the copy, an offset
one byte too far, and pictures that end exactly at the end of the plane.

Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 06:59
@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at b09c1d7f: not ready yet

Check Detail
CI at head running running: KUnit scaler, KUnit macsmc, KUnit clpc, KUnit trusted-keys
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA b09c1d7f, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · Neo, the only Mac whose encoder firmware runs MCTF. Encoder buffers skip the new check.

Areas: area:media

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

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 63fc16c68f29: ready

Blockers: none

Checked:

  • The copy site cannot wrap any more. ave_mctf_put_picture() now checks data_offset + bytes <= plane size with check_add_overflow() on size_t, for both input formats, before it moves from. bytes is g->bytes for lossless and stride * height * 3 / 2 (already size_t) for NV12, the same amounts the two memcpy()s read.
  • QBUF refuses the buffer early. ave_buf_prepare() runs the same check only for OUTPUT buffers of an MCTF context. ctx->mctf is set at MCTF open, before any queue exists, and vb2 has already copied data_offset into vb->planes by the time buf_prepare runs. The check in the copy path stays the real guard, so a format whose sizeimage differs from the session geometry is still caught there.
  • The encoder path is unchanged and was not affected. ave_sess_input() already adds (size_t)off + frame, and its stride copy is bounded by the payload after the offset. That is the only other reader of an OUTPUT plane's data_offset.
  • Merged-code rules: no runtime switch, no new constant, no SoC or board gate.
Evidence
  • Build at 63fc16c68f29 with the asahi profile (LLVM): drivers/media/platform/apple/ave/, exit 0, no warnings.
  • run-host-tests.py --only ave: 10/10 pass.
  • CI KUnit ave at this head: 50/50, including apple-ave-mctf.ave_mctf_input_offset. The new case checks:
    • U32_MAX - 1 on a U32_MAX plane, refused at QBUF and at the copy;
    • an offset one byte too far;
    • pictures ending exactly at the plane end, accepted;
    • SIZE_MAX, refused.
  • The Neo MCTF row and the M5 Pro encode smoke are still listed under "Not yet".

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row: no regression in AVE encoding. The MCTF bounds check itself isn't exercised.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at b09c1d7f (once cancelled KUnit CI checks are rerun green)

Blockers: none (code, KUnit, and hardware rows are ready; rerun the 12 cancelled KUnit checks before merge).

Checked:

  • drivers/media/platform/apple/ave/ave-mctf.c:158-206: verified ave_mctf_input_fits() uses check_add_overflow((size_t)data_offset, bytes, &end) && end <= plane_size without 32-bit wrap and is enforced in ave_mctf_put_picture() before advancing from for both lossless (APPLE_L8V0) and NV12 formats.
  • drivers/media/platform/apple/ave/ave-v4l2.c:717-720: verified ave_buf_prepare() refuses out-of-range data_offset at QBUF with -EINVAL for MCTF OUTPUT buffers while leaving the encoder path unchanged.
  • ave-mctf-test.c: verified KUnit covers U32_MAX wrap, SIZE_MAX wrap, exact boundary, and +1 byte past boundary at both QBUF and copy sites.
  • Verified J700 (Neo) MCTF + bad-data_offset QBUF refusal + 300/300 H.264 row and J714s (M5 Pro) no-regression row.
Evidence
  • Diff reviewed at b09c1d7f (all 4 files, 235 lines).
  • CI at review time: 7 passed, 0 failed, 12 cancelled.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train status at b09c1d7f (App approval noted; rows: 12 cancelled KUnit; J700 + J714s PASS). @DjDeveloperr code-owner ping (6-point rule, point 6): merge earliest 17:15 AEDT once full CI is green (I re-ran cancelled jobs) and the second-family row point holds. Revert: git revert -m 1 <merge>.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at b09c1d7f. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J700+J714s. (5) There's no blocking review. (6) Dj was pinged here 463 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit b02c185 into main Nov 28, 2026
31 of 100 checks passed
@RyanTheTide
RyanTheTide deleted the fix/ave-mctf-input-offset branch November 28, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:media Area from CODEOWNERS (bot) blast:neo Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants