From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795905582; b=wcdt2AKakbEIvWGd9idyecwUk757ADdG0wXMmg6xg0WSuTTtW8goJtpNYwJo76Xz/7hu UwOwacaKMiXFBvu1Rqz9DyemC5xOSz8yPUhc+TodaH4MgfPKlrIPaj8jOjgxllGfDJOUP NzHNzEyUb6ClNhkl5g7stDfNFta1Eo0fFQ1Z+N30ZojWRVq5Uvvfe2vbNcyO1SmJOfkmu G6R+x5f0i8ffsre0pInGJsv5IFzt12MEAgyHMEl3uaYbP7RtdpFk9+liYqgqE77QqErxR snrarL4F3KybSEcKNfHDodY4JLIEOg6ZOHVRR3ZjjDiTA3fVqLeaO3qxWJM7qmmVbbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795905582; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=i4C0YgGMsXGsJQ6HT/vibHfoosvEq5mPe7WragPxVnQ=; b=XVt0zbgvORQNnRH/gv0L1w/LlDfNL4WJAtCteL+uyEuF/79fraQbMGGO7dVm04vI4DeA PjIZfu4rx4d3rI4+FAl0LwjQOkdzNPrsR4wVyZkItc5R6/f/Hd6zQxKn5UPEt4wOuH8VW zjeZLMUn7P++3o3glYxWlUxjfGCZ+6R8KuuHOVvuWWz5ThZU5kjwQGPMQIKX2lzLYpoZe eddBQH32aTb0trEREmNUxs6Hk5/gUx5K5U8ca4MQOgI9d6SaLxOF13j48mWfp4ASuXANq ULkEZmXk4oPP2ZGMYzfIdZb2gt2YJt/QTjPDhIqmTiK+vdJPQcSkt80+z1Q0h5K2gWg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=b8HsjfZb; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A218606A324 for ; Sat, 28 Nov 2026 22:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=b8HsjfZb Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9255E1F00893; Sat, 28 Nov 2026 22:39:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795905581; bh=i4C0YgGMsXGsJQ6HT/vibHfoosvEq5mPe7WragPxVnQ=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=b8HsjfZbftKs/TTjhht+IToAL10D6wFucL2v4J6pzRdMnwl5s3IcnEP8v21n/zSqA s3j4t6p7jpF4ceCLSDhIObkQ4a06xeBi1DYImDZUXZT+je1y3ObC5QHS3BvyEx3L7n uXueemVNasLo5hg823J4JgUYKcdJsKU8jR65Ya/iM+ZZkJnwLW/3DcLZ16ImgEOy2m vuo79PUqB5w+F2uVeZ/IdQmD8O0oIpFTmj4UdFLZeGvJkVQmUKg8ABJJ+iIPj7N9dj DPbnRay5AZrra7bKQOXmgP65Bs2JHlZm/5hGdiBfkaODJjzywqbhNrk/COkuRsmsZ/ bzBDXzMcl7Gig== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 10/17] media: apple: avd: add av1 support Reply-To: sashiko-reviews@lists.linux.dev To: "Sofus Forstreuter" Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org In-Reply-To: <20261128-avd-v4-10-35ad7d52c842@icloud.com> References: <20261128-avd-v4-0-35ad7d52c842@icloud.com> <20261128-avd-v4-10-35ad7d52c842@icloud.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 28 Nov 2026 22:39:41 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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/p= latform/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 =3D params; > + short shift; > + short y; > + long long gv, dv; > + > + if (mat[2] <=3D 0) > + return; [ ... ] > +static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) > +{ > + struct avd_av1_ctx *av1_ctx =3D ctx->priv; > + const struct v4l2_ctrl_av1_frame *frame =3D run->frame; > + const struct v4l2_av1_global_motion *gm =3D &frame->global_motion; > + int i, ref_idx; > + struct avd_decoded_buffer *ref; > + bool intrabc =3D !!(frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC); > + int height, width, x_scale, y_scale; > + s64 alpha =3D 0, beta =3D 0, gamma =3D 0, delta =3D 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] <=3D 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] <=3D 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 =3D ((ref->av1.upscaled_width << AV1_REF_SCALE_SHIFT) + > + (width / 2)) / > + width; > + y_scale =3D (((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 =3D run->seq; > + int bits =3D seq->order_hint_bits - 1; > + int diff, m; > + > + if (!seq->order_hint_bits) > + return 0; > + > + diff =3D a - b; > + m =3D 1 << bits; > + diff =3D (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 <=3D 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 =3D AV1_DIV_ROUND_UP_POW2(frame_width, sub_x); > + upscaled_plane_w =3D AV1_DIV_ROUND_UP_POW2(upscaled_width, sub_x); > + *step_x =3D ((downscaled_plane_w << SUPERRES_SCALE_BITS) + > + (upscaled_plane_w / 2)) / > + upscaled_plane_w; > + err =3D (upscaled_plane_w * *step_x) - > + (downscaled_plane_w << SUPERRES_SCALE_BITS); > + *initial_subpel_x =3D (-((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 &=3D 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 =3D run->frame; > + const struct v4l2_ctrl_av1_sequence *seq =3D run->seq; > + const struct v4l2_ctrl_av1_tile_group_entry *tile_group; > + const struct v4l2_av1_tile_info *tile_info =3D &frame->tile_info; > + dma_addr_t coded_in; > + int row, col, sb_row, sb_col, tile_id, tile_row, tile_col; > + int sb_shift =3D > + seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : > + 4; > + > + for (row =3D 0; row < tile_info->tile_rows; row++) { > + for (col =3D 0; col < tile_info->tile_cols; col++) { > + tile_id =3D row * tile_info->tile_cols + col; > + tile_group =3D &run->tile_group[tile_id]; > + coded_in =3D run->base.coded_in + > + tile_group->tile_offset; > + tile_row =3D tile_group->tile_row; > + tile_col =3D tile_group->tile_col; > + > + if (tile_col >=3D V4L2_AV1_MAX_TILE_COLS || > + tile_row >=3D 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 =3D > + tile_info->mi_row_starts[tile_row] >> sb_shift; > + sb_col =3D > + 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 >=3D 0x80000000U become negative, bypass the >=3D 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 =3D dst->base.vb.vb2_buf.planes[0].length; > + vaddr =3D 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 =3D frame->frame_width_minus_1; > + buf->av1.height =3D frame->frame_height_minus_1; > + buf->av1.upscaled_width =3D frame->upscaled_width; > + buf->av1.bit_depth =3D seq->bit_depth; > + buf->av1.frame_type =3D frame->frame_type; > + buf->av1.intrabc =3D frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC; > + > + buf->av1.color_size =3D 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_r= un *run) > +{ > + struct avd_dev *avd =3D ctx->dev; > + struct avd_av1_ctx *av1_ctx =3D ctx->priv; > + const struct v4l2_ctrl_av1_frame *frame =3D run->frame; > + const struct v4l2_ctrl_av1_sequence *seq =3D run->seq; > + const struct v4l2_av1_tile_info *tile_info =3D &frame->tile_info; > + u32 ret, max_sb_col =3D 0, max_sb_row =3D 0, i, sb_cols1 =3D 0, sb_cols= 2 =3D 0, > + sb; > + u32 w =3D frame->frame_width_minus_1 + 1; > + u32 h =3D frame->frame_height_minus_1 + 1; > + u8 sb_shift =3D > + seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 1 : > + 0; > + u8 bit_depth =3D seq->bit_depth; > + > + for (i =3D 0; i < tile_info->tile_cols; i++) { > + sb =3D tile_info->width_in_sbs_minus_1[i] + 1; > + max_sb_col =3D sb > max_sb_col ? sb : max_sb_col; > + sb_cols1 +=3D ALIGN((sb << sb_shift) * 44, 128); > + /* wrong alignment? */ > + sb_cols2 +=3D ALIGN((sb << sb_shift) * 12, 128); > + } > + > + max_sb_col <<=3D sb_shift; > + > + ret =3D avd_buf_alloc(avd, &av1_ctx->bufs.mv_above_info, max_sb_col * 8= 0); > + if (ret) > + return ret; > + > + /* first frame this is always much smaller */ > + ret =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261128-avd-v4-0-3= 5ad7d52c842@icloud.com?part=3D10