Skip to content

media: apple: avd: bound HEVC, VP9, AV1 and H.264 input against the picture - #2984

Draft
Alyx-ML wants to merge 4 commits into
aurora-silicon:mainfrom
Alyx-ML:aurora/avd-bounds
Draft

Alyx-ML wants to merge 4 commits into
aurora-silicon:mainfrom
Alyx-ML:aurora/avd-bounds

Conversation

@Alyx-ML

@Alyx-ML Alyx-ML commented Nov 23, 2026

Copy link
Copy Markdown

Part of the #2755 split. Draft until it has a run on a Mac (the last Evidence line says which). These are the AVD checks that 259b966, 5e26308 and
966778e do not already make.

Summary

 avd_hevc_compute_tiles()
-  if (avd_hevc_check_tiles(sps, pps))
+  if (avd_hevc_check_tiles(sps, pps) || avd_hevc_check_slices(sps, sl, n))   # slice address < CTBs
 stream_slices()
+  more tile segments than tiles -> -EINVAL
 vp9 set_tiles()            void -> int
+  offset past the payload, or a tile size past the data -> -EINVAL, job fails
 av1 tile entries
+  tile row and column checked against the tile counts
 avd_h264_validate_sps()    (runs at try_ctrl, start and run)
+  SPS picture larger than the OUTPUT format -> -EINVAL, before start() sizes bank->slices

Evidence

  • Before: a stream can point the engine at slice addresses, tile sizes or a picture larger
    than the buffers sized from the format (found by reading; not shown on hardware).
  • After: KUnit avd_hevc_slices_test accepts address 15 on a 4x4-CTB picture and refuses 16
    and U32_MAX. Build and KUnit (avd, avd-pm) on arm64 LLVM=1: passed, Apple build and KUnit.
  • Needed before merging: fluster conformance (HEVC, VP9 and H.264 on M1 or M2; AV1 on M4 Pro
    or A18 Pro), to show that no real stream is wrongly refused.

Merge Danger

Door: two-way

Blast Radius: decoding

A wrong bound would refuse a valid stream with -EINVAL. Each check compares against the picture
or format the stream itself declares, and the core rounds the format up.

Assisted-by: LLM (Claude:Opus 5.5)

avd_hevc_check_tiles() now keeps the tile layout inside the picture, but
stream_slices() still indexes ctb_addr_rs_to_ts[], which has one entry
per CTB of the picture, with each slice's slice_segment_addr. That value
comes from userspace, and the V4L2 core cannot check it against the
picture size. An address past the last CTB reads past the table, and the
value read there then indexes tile_ids[].

Refuse a slice whose address is not a CTB of the picture (H.265 7.4.7.1),
next to the tile check, before the tables are built. stream_slices() also
takes the tile row from the count of tile segments, which follows the
entry points userspace gives, and reads row_bd[row + 1]. Refuse a frame
with more tile segments than the PPS has tiles.

Add a KUnit case for the slice address check.

Tested: checkpatch --strict is clean. Not yet built as a kernel module,
run under KUnit, or run on hardware.

Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com>
avd_vp9_check_tiling() now bounds the tile counts, but set_tiles() still
computes the tile data size as the payload minus the header sizes that
userspace gives, which wraps when the headers claim more than the frame.
It then reads 4-byte tile size markers at offsets past the frame. A tile
size larger than the rest of the frame only stopped the loop, and the
frame was still submitted with fewer tiles than the job counts.

Refuse headers past the payload, a size marker past the remaining data
and a tile size past the frame, and fail the frame with -EINVAL instead
of submitting a partial job. The run frees its control copies and the
error retires the request like any other run error.

Tested: checkpatch --strict is clean. Not yet built as a kernel module,
run under KUnit, or run on hardware.

Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com>
set_tiles() indexes the frame's mi_row_starts[], mi_col_starts[],
height_in_sbs_minus_1[] and width_in_sbs_minus_1[] with tile_row and
tile_col from each tile group entry. The V4L2 core does not validate the
entries, so any u32 from userspace reads far past the frame control.

Refuse an entry whose row or column is outside the frame's tile grid in
avd_av1_check_tiles(), which already checks each entry's offset and size.

Tested with tools/asahi/avd-av1-tail-tests.py (macOS needed extra include
and warning flags): 1804 production controls pass. Not built as a kernel
module or run on hardware.

Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com>
The H.264 scratch buffers (motion vectors and colour data), the capture
buffer, and the slice table that avd_h264_start() allocates are all
sized from the OUTPUT format. The header pushed to the engine takes the
picture size from the SPS, which is not checked against that format. A
stream whose SPS is larger than the format makes the engine work on a
picture bigger than its buffers. HEVC already refuses this case in
avd_hevc_validate_sps().

Do the same in avd_h264_validate_sps(). It runs when the SPS control is
set, when streaming starts and when a frame runs, so start() refuses an
oversized SPS before it allocates the slice table. The V4L2 core rounds
the format up to the 16-pixel height and 64-pixel width steps, so a
stream whose format was set from its own size always passes.

Checked by reading: v4l2_apply_frmsize_constraints() rounds up, so the
check cannot refuse a correctly configured stream. That the engine
writes past its buffers for such an SPS was not confirmed on hardware;
this is a defensive check in the safe direction.

Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant