From bb918c04158e5a0fe250a71153c59ddf524dd338 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:15:18 +0000 Subject: [PATCH 1/4] media: apple: avd: hevc: check slice addresses against the picture 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> --- .../media/platform/apple/avd/avd-hevc-test.c | 18 +++++++++++ drivers/media/platform/apple/avd/avd-hevc.c | 32 ++++++++++++++++++- drivers/media/platform/apple/avd/avd.h | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c index 31aee3e1d542b..71aecba433ef3 100644 --- a/drivers/media/platform/apple/avd/avd-hevc-test.c +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -166,11 +166,29 @@ static void avd_hevc_no_tiles_test(struct kunit *test) 0); } +/* The 4x4-CTB test picture has CTB addresses 0 to 15. */ +static void avd_hevc_slices_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_slice_params sl[2] = {}; + + avd_hevc_test_sps(&sps); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, sl, 2), 0); + sl[1].slice_segment_addr = 15; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, sl, 2), 0); + sl[1].slice_segment_addr = 16; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, sl, 2), -EINVAL); + sl[1].slice_segment_addr = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, sl, 2), -EINVAL); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, sl, 1), 0); +} + static struct kunit_case avd_hevc_test_cases[] = { KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), KUNIT_CASE(avd_hevc_tiles_array_test), KUNIT_CASE(avd_hevc_no_tiles_test), + KUNIT_CASE(avd_hevc_slices_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index d655ba8c648bf..e1ee055349715 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -958,6 +958,12 @@ static int stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) row = pos / num_cols; col = pos % num_cols; + /* Each segment starts a tile: no more than the PPS has. */ + if (tiles_enabled && row > pps->num_tile_rows_minus1) { + dev_err_ratelimited(ctx->dev->dev, + "HEVC: more tile segments than tiles\n"); + return -EINVAL; + } /* * not a fan of this, in JCT-VC-HEVC_V1 only TILES_B_Cisco_1 @@ -1121,6 +1127,29 @@ VISIBLE_IF_KUNIT int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, } EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_tiles); +/* + * stream_slices() indexes the per-CTB tables with each slice's + * slice_segment_addr, which the V4L2 core cannot check against the picture. + * It must name a CTB of the picture (H.265 7.4.7.1). + * avd_hevc_check_sps_geometry() must have accepted the SPS. + */ +VISIBLE_IF_KUNIT int +avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_slice_params *sl, + int num_slices) +{ + u32 width, height; + int i; + + avd_hevc_pic_in_ctbs(sps, &width, &height); + for (i = 0; i < num_slices; i++) + if (sl[i].slice_segment_addr >= width * height) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_slices); + static int avd_hevc_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_hevc_sps *sps) { @@ -1369,7 +1398,8 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) tiles_enabled = !!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED); - if (avd_hevc_check_tiles(sps, pps)) + if (avd_hevc_check_tiles(sps, pps) || + avd_hevc_check_slices(sps, run->sl, run->num_slices)) return -EINVAL; tile_info->ctb_addr_rs_to_ts = kzalloc( diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 6fee27521eed2..2e7c20b8463bb 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -380,6 +380,9 @@ bool avd_h264_scaling_is_flat(const struct v4l2_ctrl_h264_pps *pps, int avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps); int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps); +int avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_slice_params *sl, + int num_slices); int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps, u32 *tile_ids, size_t n); From 0e60bdf48ce1fe961a9c91c2fb1c5895874f07ff Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:16:04 +0000 Subject: [PATCH 2/4] media: apple: avd: vp9: check the tile sizes against the frame 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> --- drivers/media/platform/apple/avd/avd-vp9.c | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 71b80796fd090..8e2ef301c7a55 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -493,20 +493,29 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) set_refs(ctx, run); } -static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) +static int set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) { const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; struct avd_vp9_ctx *vp9_ctx = ctx->priv; struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); - + u32 payload = vb2_get_plane_payload(&src->vb2_buf, 0); u32 offset = frame->uncompressed_header_size + frame->compressed_header_size; - u32 size = vb2_get_plane_payload(&src->vb2_buf, 0) - offset; + u32 size; u32 num_tile_rows = 1 << frame->tile_rows_log2; u32 num_tile_cols = 1 << frame->tile_cols_log2; u32 tile_size; + /* The header sizes come from userspace and may claim more than the frame. */ + if (!data || offset > payload) { + dev_err_ratelimited(ctx->dev->dev, + "VP9: headers of %u bytes in a %u byte frame\n", + offset, payload); + return -EINVAL; + } + size = payload - offset; + /* 6.2.6 Compute image size syntax */ u32 sb_64_cols = (((frame->frame_width_minus_1 + 8) >> 3) + 7) >> 3; u32 sb_64_rows = (((frame->frame_height_minus_1 + 8) >> 3) + 7) >> 3; @@ -518,10 +527,15 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) col == num_tile_cols - 1) { tile_size = size; } else { + /* + * A size marker or a tile past the frame would + * make the engine read past it: fail the frame. + */ + if (size < 4) + return -EINVAL; tile_size = get_unaligned_be32(&data[offset]); - /* i have crashed my computer because of this */ if (tile_size > size - 4) - return; + return -EINVAL; offset += 4; size -= 4; } @@ -550,6 +564,8 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) size -= tile_size; vp9_ctx->b->submit_num++; } + + return 0; } static void update_dec_buf_info(struct avd_decoded_buffer *buf, @@ -940,8 +956,10 @@ static int avd_vp9_run(struct avd_ctx *ctx) set_header(ctx, &run); vp9_ctx->b->submit_num = 0; - set_tiles(ctx, &run); + ret = set_tiles(ctx, &run); avd_run_postamble(ctx, &run.base); + if (ret) + goto out; if (avd_backend_is_qfw(ctx->dev)) { /* Forward updates need no hardware result; publish before next prepare. */ From c5ce6a12f7a22f8a3a8b5fc3205b6415a726a298 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:44 +0000 Subject: [PATCH 3/4] media: apple: avd: check the AV1 tile entry row and column 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> --- drivers/media/platform/apple/avd/avd-av1.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 8b3c2f34c542c..380b559c24cff 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -1421,6 +1421,15 @@ static int avd_av1_check_tiles(struct avd_ctx *ctx, struct avd_av1_run *run, t, e->tile_offset, e->tile_size, payload); return -EINVAL; } + /* set_tiles() indexes the frame's tile start and size arrays with these */ + if (e->tile_row >= run->frame->tile_info.tile_rows || + e->tile_col >= run->frame->tile_info.tile_cols) { + dev_err_ratelimited(ctx->dev->dev, "AV1: tile %u at row %u col %u of %ux%u\n", + t, e->tile_row, e->tile_col, + run->frame->tile_info.tile_cols, + run->frame->tile_info.tile_rows); + return -EINVAL; + } } return 0; } From cc247ae904645097b08cc620d2ca941aeb567a79 Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:44 +0000 Subject: [PATCH 4/4] media: apple: avd: refuse H.264 pictures larger than the OUTPUT format 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> --- drivers/media/platform/apple/avd/avd-h264.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 6d2b4ff0643c3..1846ae3e5bb24 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -694,6 +694,19 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)) /* no interlaced support */ return -EINVAL; + /* + * The scratch and capture buffers, and the slice table start() sizes, + * follow the OUTPUT format, which the core rounds up to whole + * macroblocks. Refuse a larger picture, as HEVC does, so the engine + * stays inside them. Frame MBs only: map units are macroblock rows. + */ + if ((sps->pic_width_in_mbs_minus1 + 1) * 16 > fmt_width(ctx) || + (sps->pic_height_in_map_units_minus1 + 1) * 16 > fmt_height(ctx)) { + dev_err_ratelimited(ctx->dev->dev, + "H.264 picture larger than the %ux%u format\n", + fmt_width(ctx), fmt_height(ctx)); + return -EINVAL; + } return 0; }