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; } 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; } 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-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. */ 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);