Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sofus Forstreuter" <sofus.c@icloud.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org,
	media-ci@linuxtv.org
Subject: Re: [PATCH v4 10/17] media: apple: avd: add av1 support
Date: Sat, 28 Nov 2026 22:39:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-239111@kernel.org> (raw)
In-Reply-To: <20261128-avd-v4-10-35ad7d52c842@icloud.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] media: apple: avd: out-of-bounds memcpy in
  avd_av1_set_prob()
- [High] media: apple: avd: division by zero in calc_upscale() and
  set_refs()
- [High] media: apple: avd: signedness bug and missing tile bounds in
  set_tiles()
- [High] media: apple: avd: 32-bit size overflows in
  avd_av1_alloc_work_bufs()
- [High] media: apple: avd: unallocated seg buffer and sentinel IOVAs in
  set_header()
- [Medium] media: apple: avd: stale shear params across loop in
  set_refs()
- [Medium] media: apple: avd: shift out of bounds in avd_av1_get_dist()
--

commit 3d10bd41d7b615eda63f9315cb2299efb7e6934f
Author: Sofus Forstreuter <sofus.c@icloud.com>

media: apple: avd: add av1 support

This commit adds stateless V4L2 AV1 decoding support and entropy default
tables to the Apple Video Decoder (AVD) driver.

> diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c
> new file mode 100644
> index 0000000000000..c0706d981ce0e
> --- /dev/null
> +++ b/drivers/media/platform/apple/avd/avd-av1.c
[ ... ]
> +static void
> +avd_av1_dec_get_shear_params(const u32 *params, s64 *alpha,
> +			     s64 *beta, s64 *gamma, s64 *delta)
> +{
> +	const int *mat = params;
> +	short shift;
> +	short y;
> +	long long gv, dv;
> +
> +	if (mat[2] <= 0)
> +		return;
[ ... ]
> +static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run)
> +{
> +	struct avd_av1_ctx *av1_ctx = ctx->priv;
> +	const struct v4l2_ctrl_av1_frame *frame = run->frame;
> +	const struct v4l2_av1_global_motion *gm = &frame->global_motion;
> +	int i, ref_idx;
> +	struct avd_decoded_buffer *ref;
> +	bool intrabc = !!(frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC);
> +	int height, width, x_scale, y_scale;
> +	s64 alpha = 0, beta = 0, gamma = 0, delta = 0;
[ ... ]
> +			avd_av1_dec_get_shear_params(&gm->params[ref_idx][0],
> +						     &alpha, &beta, &gamma,
> +						     &delta);
> +			push(AV1_GM_PARAM_SHEAR0(beta) |
> +				     AV1_GM_PARAM_SHEAR1(alpha),
> +			     "ref_gm_beta_alpha");
> +
> +			push(AV1_GM_PARAM_SHEAR0(delta) |
> +				     AV1_GM_PARAM_SHEAR1(gamma),
> +			     "ref_gm_delta_gamma");

[Severity: Medium]
Does set_refs() push stale shear parameters from an earlier reference slot
when a subsequent reference has gm->params[ref_idx][2] <= 0?

alpha, beta, gamma and delta are initialized once outside the loop, and
avd_av1_dec_get_shear_params() returns early without zeroing them when
mat[2] <= 0.  Also, gm->params is __s32, so taking const u32 * in
avd_av1_dec_get_shear_params() triggers a -Wpointer-sign warning.

> +		}
> +		push(AVD_REF_FLAG_CONST |
> +			     AV1_REF_ORDER_HINT(
> +				     intrabc ? 0 : frame->order_hints[ref_idx]),
> +		     "ref_flag");
> +		push(AVD_HDR_HEIGHT(ref->av1.height) |
> +			     AVD_HDR_WIDTH(ref->av1.width),
> +		     "ref_height_width");
> +
> +		x_scale = ((ref->av1.upscaled_width << AV1_REF_SCALE_SHIFT) +
> +			   (width / 2)) /
> +			  width;
> +		y_scale = (((ref->av1.height + 1) << AV1_REF_SCALE_SHIFT) +
> +			   (height / 2)) /
> +			  height;
> +		push(AV1_REF_SCALE_X(x_scale) | AV1_REF_SCALE_Y(y_scale),
> +		     "ref_scale");
> +
> +		push_comp(ctx, ref);
> +	}
> +}
> +
> +static int avd_av1_get_dist(struct avd_av1_run *run, int a, int b)
> +{
> +	const struct v4l2_ctrl_av1_sequence *seq = run->seq;
> +	int bits = seq->order_hint_bits - 1;
> +	int diff, m;
> +
> +	if (!seq->order_hint_bits)
> +		return 0;
> +
> +	diff = a - b;
> +	m = 1 << bits;
> +	diff = (diff & (m - 1)) - (diff & m);
> +
> +	return diff;
> +}

[Severity: Medium]
Can 1 << bits overflow a 32-bit signed int in avd_av1_get_dist()?

Neither v4l2-ctrls-core.c nor avd_av1_validate_sequence() bounds
seq->order_hint_bits to <= 8, so a userspace value above 8 overflows the
3-bit AV1_HDR_ORDER_HINT_BITS() field and a value above 31 triggers a
shift-out-of-bounds in avd_av1_get_dist().

[ ... ]

> +/* 7.16. Upscaling process */
> +static void calc_upscale(int frame_width, int upscaled_width, int sub_x,
> +			 int *step_x, int *initial_subpel_x)
> +{
> +	int downscaled_plane_w, upscaled_plane_w, err;
> +
> +	downscaled_plane_w = AV1_DIV_ROUND_UP_POW2(frame_width, sub_x);
> +	upscaled_plane_w = AV1_DIV_ROUND_UP_POW2(upscaled_width, sub_x);
> +	*step_x = ((downscaled_plane_w << SUPERRES_SCALE_BITS) +
> +		   (upscaled_plane_w / 2)) /
> +		  upscaled_plane_w;
> +	err = (upscaled_plane_w * *step_x) -
> +	      (downscaled_plane_w << SUPERRES_SCALE_BITS);
> +	*initial_subpel_x = (-((upscaled_plane_w - downscaled_plane_w)
> +			       << (SUPERRES_SCALE_BITS - 1)) +
> +			     upscaled_plane_w / 2) /
> +				    upscaled_plane_w +
> +			    (1 << (SUPERRES_EXTRA_BITS - 1)) - err / 2;
> +	*initial_subpel_x &= SUPERRES_SCALE_MASK;
> +}

[Severity: High]
Can calc_upscale() and set_refs() divide by zero when userspace passes an
unvalidated V4L2_CID_STATELESS_AV1_FRAME control?

set_header() unconditionally calls calc_upscale() with
frame->upscaled_width, which is not validated by v4l2-ctrls-core.c or this
driver; if upscaled_width is 0, upscaled_plane_w is 0.  Similarly, in
set_refs(), width and height are computed as frame_width_minus_1 + 1 and
frame_height_minus_1 + 1, which wrap to 0 if either field is U32_MAX.

[ ... ]

> +	pusha(run->addresses.probs_out, "probs_out", 0);
> +	pusha(av1_ctx->bufs.probs.addr, "probs", 1);
> +	pusha(av1_ctx->bufs.above_info.addr, "col", 0);
> +	pusha(av1_ctx->bufs.seg.addr, "seg", 0);
> +	/* make it obvious if its used */
> +	pusha(0xb0b0b0b0, "", 1);
> +	pusha(run->addresses.color, "w_color", 0);
[ ... ]
> +	pusha(av1_ctx->bufs.lf_left.addr, "lf_left", 0);
> +	pusha(av1_ctx->bufs.lf_left_info.addr, "lf_left_info", 1);
> +	/* make it obvious if its used */
> +	pusha(0xa0a0a0a0, "", 0);
> +	pusha(av1_ctx->bufs.rf_above_info.addr, "rf_above_info", 0);

[Severity: High]
What happens if segmentation is enabled without
V4L2_AV1_SEGMENTATION_FLAG_UPDATE_MAP, or if the hardware accesses the
0xb0b0b0b0 and 0xa0a0a0a0 slots?

avd_av1_alloc_work_bufs() only allocates av1_ctx->bufs.seg when
V4L2_AV1_SEGMENTATION_FLAG_UPDATE_MAP is set, so enabling
V4L2_AV1_SEGMENTATION_FLAG_ENABLED without UPDATE_MAP leaves seg.addr at 0
(or undersized after a resolution change), while the second segmentation
map slot (where avd-vp9.c passes seg.addr) and the slot at 0xa0a0a0a0 push
hardcoded bogus IOVAs into the command stream.

[ ... ]

> +static int set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run)
> +{
> +	const struct v4l2_ctrl_av1_frame *frame = run->frame;
> +	const struct v4l2_ctrl_av1_sequence *seq = run->seq;
> +	const struct v4l2_ctrl_av1_tile_group_entry *tile_group;
> +	const struct v4l2_av1_tile_info *tile_info = &frame->tile_info;
> +	dma_addr_t coded_in;
> +	int row, col, sb_row, sb_col, tile_id, tile_row, tile_col;
> +	int sb_shift =
> +		seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 :
> +									     4;
> +
> +	for (row = 0; row < tile_info->tile_rows; row++) {
> +		for (col = 0; col < tile_info->tile_cols; col++) {
> +			tile_id = row * tile_info->tile_cols + col;
> +			tile_group = &run->tile_group[tile_id];
> +			coded_in = run->base.coded_in +
> +					      tile_group->tile_offset;
> +			tile_row = tile_group->tile_row;
> +			tile_col = tile_group->tile_col;
> +
> +			if (tile_col >= V4L2_AV1_MAX_TILE_COLS ||
> +			    tile_row >= V4L2_AV1_MAX_TILE_ROWS)
> +				return -EINVAL;
> +
> +			push(AVD_OP_CODED_DATA | AVD_OP_CODED_IN_HI(coded_in),
> +			     "tile_start");
> +			push(AVD_OP_CODED_IN_LO(coded_in), "tile_addr");
> +			push(tile_group->tile_size, "tile_size");
> +
> +			sb_row =
> +				tile_info->mi_row_starts[tile_row] >> sb_shift;
> +			sb_col =
> +				tile_info->mi_col_starts[tile_col] >> sb_shift;

[Severity: High]
Can a negative tile_row or tile_col index tile_info arrays out of bounds in
set_tiles()?

tile_group->tile_row and tile_group->tile_col are __u32 values from
userspace assigned into signed int variables, so values >= 0x80000000U
become negative, bypass the >= V4L2_AV1_MAX_TILE_ROWS/COLS check, and read
before mi_row_starts[], mi_col_starts[], height_in_sbs_minus_1[] and
width_in_sbs_minus_1[].  In addition, tile_row and tile_col are not checked
against tile_info->tile_rows and tile_info->tile_cols, and tile_offset and
tile_size are not checked against the source buffer payload size.

[ ... ]

> +static int avd_av1_set_prob(struct avd_ctx *ctx, struct avd_av1_run *run)
> +{
[ ... ]
> +		memcpy(av1_ctx->bufs.probs.cpu,
> +		       vaddr + AVD_AV1_CDFS_OFFSET(length, ref->av1.color_size),
> +		       sizeof(struct avd_av1_cdfs));
> +	}
> +
> +	length = dst->base.vb.vb2_buf.planes[0].length;
> +	vaddr = vb2_plane_vaddr(&dst->base.vb.vb2_buf, 0);
> +	if (!vaddr)
> +		return -EINVAL;
> +	/*
> +	 * AVD writes nothing when DISABLE_FRAME_END_UPDATE_CDF is
> +	 * enabled
> +	 */
> +	memcpy(vaddr + AVD_AV1_CDFS_OFFSET(length, dst->av1.color_size),
> +	       av1_ctx->bufs.probs.cpu, sizeof(struct avd_av1_cdfs));
> +	return 0;
> +}
> +
> +static int avd_color_size(int h, int w)
> +{
> +	/*
> +	 * in reality its dependent on quality
> +	 * 64 with lower quality
> +	 */
> +	return ALIGN(w, 128) * ALIGN(h, 128) / 16;
> +}
> +
> +static void update_dec_buf_info(struct avd_decoded_buffer *buf,
> +				const struct v4l2_ctrl_av1_sequence *seq,
> +				const struct v4l2_ctrl_av1_frame *frame)
> +{
> +	int i;
> +
> +	buf->av1.width = frame->frame_width_minus_1;
> +	buf->av1.height = frame->frame_height_minus_1;
> +	buf->av1.upscaled_width = frame->upscaled_width;
> +	buf->av1.bit_depth = seq->bit_depth;
> +	buf->av1.frame_type = frame->frame_type;
> +	buf->av1.intrabc = frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC;
> +
> +	buf->av1.color_size = avd_color_size(frame->frame_width_minus_1 + 1,
> +					     frame->frame_height_minus_1 + 1);

[Severity: Critical]
Can AVD_AV1_CDFS_OFFSET(length, dst->av1.color_size) underflow in
avd_av1_set_prob() and avd_av1_run_preamble(), causing memcpy() and DMA to
access memory outside the capture plane?

The capture buffer is sized from ctx->decoded_fmt.fmt.pix_mp, but unlike
avd-vp9.c and avd-hevc.c, avd-av1.c never checks that
frame->frame_width_minus_1 + 1 and frame->frame_height_minus_1 + 1 fit
within the negotiated format (and avd_color_size() can also overflow 32-bit
signed int).  If userspace submits frame dimensions larger than the
allocated capture buffer, ALIGN(color_size, AVD_ALIGN) +
ALIGN(AVD_CDFS_SIZE, AVD_ALIGN) exceeds planes[0].length and
AVD_AV1_CDFS_OFFSET() wraps around, writing sizeof(struct avd_av1_cdfs)
bytes out of bounds before vaddr.

[ ... ]

> +static int avd_av1_alloc_work_bufs(struct avd_ctx *ctx, struct avd_av1_run *run)
> +{
> +	struct avd_dev *avd = ctx->dev;
> +	struct avd_av1_ctx *av1_ctx = ctx->priv;
> +	const struct v4l2_ctrl_av1_frame *frame = run->frame;
> +	const struct v4l2_ctrl_av1_sequence *seq = run->seq;
> +	const struct v4l2_av1_tile_info *tile_info = &frame->tile_info;
> +	u32 ret, max_sb_col = 0, max_sb_row = 0, i, sb_cols1 = 0, sb_cols2 = 0,
> +		 sb;
> +	u32 w = frame->frame_width_minus_1 + 1;
> +	u32 h = frame->frame_height_minus_1 + 1;
> +	u8 sb_shift =
> +		seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 1 :
> +									     0;
> +	u8 bit_depth = seq->bit_depth;
> +
> +	for (i = 0; i < tile_info->tile_cols; i++) {
> +		sb = tile_info->width_in_sbs_minus_1[i] + 1;
> +		max_sb_col = sb > max_sb_col ? sb : max_sb_col;
> +		sb_cols1 += ALIGN((sb << sb_shift) * 44, 128);
> +		/* wrong alignment? */
> +		sb_cols2 += ALIGN((sb << sb_shift) * 12, 128);
> +	}
> +
> +	max_sb_col <<= sb_shift;
> +
> +	ret = avd_buf_alloc(avd, &av1_ctx->bufs.mv_above_info, max_sb_col * 80);
> +	if (ret)
> +		return ret;
> +
> +	/* first frame this is always much smaller */
> +	ret = avd_buf_alloc(avd, &av1_ctx->bufs.above_info, max_sb_col * 400);

[Severity: High]
Can the 32-bit size calculations in avd_av1_alloc_work_bufs() overflow and
allocate undersized DMA scratch buffers?

width_in_sbs_minus_1[], height_in_sbs_minus_1[], frame_width_minus_1 and
frame_height_minus_1 are unvalidated u32 fields from userspace.  Large
values can wrap 32-bit calculations such as sb_cols1, max_sb_col * 400,
(ALIGN(h, ...) * ALIGN(w, ...)) / 128 * 3 and (max_sb_row * 72 + 1) * 2 *
bit_depth to small non-zero sizes while the hardware is programmed with the
unwrapped superblock dimensions.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-avd-v4-0-35ad7d52c842@icloud.com?part=10

  reply	other threads:[~2026-11-28 22:39 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 22:02 [PATCH v4 00/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 01/17] dt-bindings: media: add apple,avd Sofus Forstreuter
2026-11-28 22:08   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 02/17] media: v4l2: Add P210 pixel format Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 03/17] media: v4l2: Add Apple interchange pixel formats Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 04/17] media: v4l2-ctrls: validate AV1 context_update_tile_id Sofus Forstreuter
2026-11-28 22:39   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 05/17] media: v4l2-ctrls: validate vp9 tile_rows_log2 Sofus Forstreuter
2026-11-28 22:08   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 06/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 07/17] media: apple: avd: add h264 support Sofus Forstreuter
2026-11-28 22:43   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 08/17] media: apple: avd: add vp9 support Sofus Forstreuter
2026-11-28 22:49   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 09/17] media: apple: avd: add hevc support Sofus Forstreuter
2026-11-28 22:46   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 10/17] media: apple: avd: add av1 support Sofus Forstreuter
2026-11-28 22:39   ` sashiko-bot [this message]
2026-11-28 22:02 ` [PATCH v4 11/17] arm64: dts: apple: t8103: add avd nodes Sofus Forstreuter
2026-11-28 22:36   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 12/17] arm64: dts: apple: t8112: " Sofus Forstreuter
2026-11-28 22:21   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 13/17] arm64: dts: apple: t8122: " Sofus Forstreuter
2026-11-28 22:37   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 14/17] arm64: dts: apple: t600x: " Sofus Forstreuter
2026-11-28 22:35   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 15/17] arm64: dts: apple: t602x: " Sofus Forstreuter
2026-11-28 22:20   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 16/17] arm64: dts: apple: t6030: " Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 17/17] arm64: dts: apple: t6031: " Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-239111@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sofus.c@icloud.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox