From: sashiko-bot@kernel.org
To: "Sven Püschel" <s.pueschel@pengutronix.de>
Cc: robh@kernel.org, devicetree@vger.kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v7 15/28] media: rockchip: rga: align stride to 4 bytes
Date: Wed, 20 May 2026 23:56:32 +0000 [thread overview]
Message-ID: <20260520235633.0440A1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260521-spu-rga3-v7-15-3f33e8c7145f@pengutronix.de>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] The `vir_stride` bitfield in `union rga_src_vir_info` is 10 bits, causing silent truncation for valid V4L2 widths.
- [High] In `rga_open()`, the return value of `rga_setup_ctrls(ctx)` is completely ignored. If control setup fails, subsequent code uses a destroyed control handler and locks a destroyed mutex.
- [High] An off-by-one error in the bounds check of `fill_descriptors` combined with an under-allocation of the descriptor array causes a heap out-of-bounds write of 4 bytes when handling multi-planar formats.
--
commit c43e898ca5762d2604bb3d4b71e3a6c220a67ef8
Author: Sven Püschel <s.pueschel@pengutronix.de>
media: rockchip: rga: align stride to 4 bytes
Add an alignment setting to rga_hw to set the desired stride alignment.
As the RGA2 register for the stride counts in word units, the code
already divides the bytesperline value by 4 when writing it into the
register. Therefore fix the alignment to a multiple of 4 to avoid
potential off by one errors due from the division.
> diff --git a/drivers/media/platform/rockchip/rga/rga-hw.c b/drivers/media/platform/rockchip/rga/rga-hw.c
> index 9881c14f908d5..dac3cb6aa17d3 100644
> --- a/drivers/media/platform/rockchip/rga/rga-hw.c
> +++ b/drivers/media/platform/rockchip/rga/rga-hw.c
> @@ -580,6 +580,7 @@ const struct rga_hw rga2_hw = {
> .max_width = MAX_WIDTH,
> .min_height = MIN_HEIGHT,
> .max_height = MAX_HEIGHT,
> + .stride_alignment = 4,
[Severity: High]
This is a pre-existing issue, but the vir_stride bitfield in
union rga_src_vir_info is 10 bits, causing silent truncation for valid V4L2
widths.
The hardware requires the source stride in 4-byte words, which the driver
calculates as ctx->in.stride >> 2:
drivers/media/platform/rockchip/rga/rga-hw.c:rga_hw_start() {
...
/*
* SRC_VIR_INFO
* note that the step of vir_stride / vir_width is 4 byte words
*/
src_vir_info.data.vir_stride = ctx->in.stride >> 2;
...
}
For a 1920x1080 image in a 32-bit format (like ARGB32), the stride is 7680
bytes, which equals 1920 words. However, vir_stride is defined as a 10-bit
bitfield in union rga_src_vir_info:
drivers/media/platform/rockchip/rga/rga-hw.h:
union rga_src_vir_info {
unsigned int val;
struct {
/* [0:15] */
unsigned int vir_width:15;
unsigned int reserved:1;
/* [16:25] */
unsigned int vir_stride:10;
/* [26:31] */
unsigned int reserved1:6;
} data;
};
A 10-bit field can only hold values up to 1023. When 1920 is assigned to it,
it silently overflows and truncates to 896 (1920 & 1023).
This truncated stride is then programmed into the hardware via the
RGA_SRC_VIR_INFO register. Will this cause the hardware to read from
incorrect memory offsets for all lines after the first, resulting in severe
data corruption for common high resolutions like 1080p?
>
> .start = rga_hw_start,
> .handle_irq = rga_handle_irq,
> diff --git a/drivers/media/platform/rockchip/rga/rga.c b/drivers/media/platform/rockchip/rga/rga.c
> index bf6bbcbfc869b..d080cb672740b 100644
> --- a/drivers/media/platform/rockchip/rga/rga.c
> +++ b/drivers/media/platform/rockchip/rga/rga.c
> @@ -234,10 +234,10 @@ static int rga_open(struct file *file)
> ctx->in = def_frame;
> ctx->out = def_frame;
>
> - v4l2_fill_pixfmt_mp(&ctx->in.pix,
> - ctx->in.fmt->fourcc, def_width, def_height);
> - v4l2_fill_pixfmt_mp(&ctx->out.pix,
> - ctx->out.fmt->fourcc, def_width, def_height);
> + v4l2_fill_pixfmt_mp_aligned(&ctx->in.pix, ctx->in.fmt->fourcc,
> + def_width, def_height, rga->hw->stride_alignment);
> + v4l2_fill_pixfmt_mp_aligned(&ctx->out.pix, ctx->out.fmt->fourcc,
> + def_width, def_height, rga->hw->stride_alignment);
[Severity: High]
This isn't a bug introduced by this patch, but in rga_open(), the return
value of rga_setup_ctrls(ctx) is completely ignored.
If rga_setup_ctrls() encounters an error (e.g., -ENOMEM during
v4l2_ctrl_handler_init), it handles its own failure by calling
v4l2_ctrl_handler_free(&ctx->ctrl_handler) before returning the error code.
This free operation invokes mutex_destroy(&ctx->ctrl_handler._lock).
However, rga_open() completely ignores the returned error code and proceeds:
drivers/media/platform/rockchip/rga/rga.c:rga_open() {
...
rga_setup_ctrls(ctx);
/* Write the default values to the ctx struct */
v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
ctx->fh.ctrl_handler = &ctx->ctrl_handler;
mutex_unlock(&rga->mutex);
...
}
It immediately calls v4l2_ctrl_handler_setup(&ctx->ctrl_handler), which
inadvertently calls mutex_lock() on the destroyed mutex. Furthermore, it
assigns ctx->fh.ctrl_handler = &ctx->ctrl_handler, exposing the broken,
destroyed handler to userspace ioctls like VIDIOC_S_CTRL.
Could this result in a reliable use-after-destroy vulnerability if a user can
induce out-of-memory conditions during device open?
[ ... ]
> @@ -393,7 +393,8 @@ static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
> fmt = &hw->formats[0];
>
> v4l2_apply_frmsize_constraints(&pix_fmt->width, &pix_fmt->height, &frmsize);
> - v4l2_fill_pixfmt_mp(pix_fmt, fmt->fourcc, pix_fmt->width, pix_fmt->height);
> + v4l2_fill_pixfmt_mp_aligned(pix_fmt, fmt->fourcc,
> + pix_fmt->width, pix_fmt->height, hw->stride_alignment);
[Severity: High]
This is a pre-existing issue, but an off-by-one error in the bounds check of
fill_descriptors() combined with an under-allocation of the descriptor array
causes a heap out-of-bounds write of 4 bytes when handling multi-planar
formats.
In rga_buf_init(), the DMA descriptor array is allocated based on the total
size of the frame:
drivers/media/platform/rockchip/rga/rga-buf.c:rga_buf_init() {
...
n_desc = DIV_ROUND_UP(f->size, PAGE_SIZE);
rbuf->n_desc = n_desc;
rbuf->dma_desc = dma_alloc_coherent(rga->dev,
rbuf->n_desc * sizeof(*rbuf->dma_desc),
&rbuf->dma_desc_pa, GFP_KERNEL);
...
}
However, videobuf2 allocates each plane independently and pads each plane's
allocation to a page boundary. This means the sum of pages in the scatterlist
tables across all planes can exceed the total allocated descriptors (e.g., if
two planes are 4098 bytes each, they require 2 pages each (4 total), but
f->size is 8196 bytes, so n_desc is only 3).
When rga_buf_prepare() iterates through the planes and calls
fill_descriptors(), it passes the remaining capacity as max_desc. Due to an
off-by-one error in fill_descriptors() (if (n_desc > max_desc) instead of >=),
the loop permits writing exactly one element past max_desc before returning
-EINVAL:
drivers/media/platform/rockchip/rga/rga-buf.c:fill_descriptors() {
...
for_each_sgtable_dma_page(sgt, &iter, 0) {
if (n_desc > max_desc)
return -EINVAL;
addr = sg_page_iter_dma_address(&iter);
tmp->addr = lower_32_bits(addr);
tmp++;
n_desc++;
}
...
}
If the remaining capacity is 1 but the scatterlist table provides 2 pages,
the loop writes to tmp[0] (valid) and tmp[1] (out-of-bounds). Can this result
in a reliable 4-byte heap buffer overflow?
> pix_fmt->field = V4L2_FIELD_NONE;
>
> return 0;
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260521-spu-rga3-v7-0-3f33e8c7145f@pengutronix.de?part=15
next prev parent reply other threads:[~2026-05-20 23:56 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-20 22:44 [PATCH v7 00/28] media: platform: rga: Add RGA3 support Sven Püschel
2026-05-20 22:44 ` [PATCH v7 01/28] media: dt-bindings: media: rockchip-rga: add rockchip,rk3588-rga3 Sven Püschel
2026-05-20 22:44 ` [PATCH v7 02/28] media: v4l2-common: sort RGB formats in v4l2_format_info Sven Püschel
2026-05-20 22:44 ` [PATCH v7 03/28] media: v4l2-common: add missing 1 and 2 byte RGB formats to v4l2_format_info Sven Püschel
2026-05-20 22:44 ` [PATCH v7 04/28] media: v4l2-common: add has_alpha " Sven Püschel
2026-05-20 22:44 ` [PATCH v7 05/28] media: v4l2-common: add v4l2_fill_pixfmt_mp_aligned helper Sven Püschel
2026-05-20 23:48 ` Nicolas Dufresne
2026-05-20 22:44 ` [PATCH v7 06/28] media: rockchip: rga: fix too small buffer size Sven Püschel
2026-05-20 23:43 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 07/28] media: rockchip: rga: use clk_bulk api Sven Püschel
2026-05-20 23:27 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 08/28] media: rockchip: rga: use stride for offset calculation Sven Püschel
2026-05-20 23:38 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 09/28] media: rockchip: rga: remove redundant rga_frame variables Sven Püschel
2026-05-20 23:37 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 10/28] media: rockchip: rga: announce and sync colorimetry Sven Püschel
2026-05-20 23:45 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 11/28] media: rockchip: rga: move hw specific parts to a dedicated struct Sven Püschel
2026-05-20 23:30 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 12/28] media: rockchip: rga: avoid odd frame sizes for YUV formats Sven Püschel
2026-05-20 23:32 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 13/28] media: rockchip: rga: calculate x_div/y_div using v4l2_format_info Sven Püschel
2026-05-20 22:44 ` [PATCH v7 14/28] media: rockchip: rga: move cmdbuf to rga_ctx Sven Püschel
2026-05-20 23:44 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 15/28] media: rockchip: rga: align stride to 4 bytes Sven Püschel
2026-05-20 23:56 ` sashiko-bot [this message]
2026-05-20 22:44 ` [PATCH v7 16/28] media: rockchip: rga: reuse cmdbuf contents Sven Püschel
2026-05-20 23:30 ` sashiko-bot
2026-05-20 23:55 ` Nicolas Dufresne
2026-05-20 22:44 ` [PATCH v7 17/28] media: rockchip: rga: check scaling factor Sven Püschel
2026-05-20 23:42 ` sashiko-bot
2026-05-20 23:58 ` Nicolas Dufresne
2026-05-20 22:44 ` [PATCH v7 18/28] media: rockchip: rga: use card type to specify rga type Sven Püschel
2026-05-20 23:29 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 19/28] media: rockchip: rga: change offset to dma_addresses Sven Püschel
2026-05-20 22:44 ` [PATCH v7 20/28] media: rockchip: rga: support external iommus Sven Püschel
2026-05-20 23:43 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 21/28] media: rockchip: rga: share the interrupt when an external iommu is used Sven Püschel
2026-05-20 23:33 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 22/28] media: rockchip: rga: remove size from rga_frame Sven Püschel
2026-05-20 23:35 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 23/28] media: rockchip: rga: remove stride " Sven Püschel
2026-05-20 22:44 ` [PATCH v7 24/28] media: rockchip: rga: move rga_fmt to rga-hw.h Sven Püschel
2026-05-20 22:44 ` [PATCH v7 25/28] media: rockchip: rga: add feature flags Sven Püschel
2026-05-20 23:42 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 26/28] media: rockchip: rga: disable multi-core support Sven Püschel
2026-05-20 22:44 ` [PATCH v7 27/28] media: rockchip: rga: add rga3 support Sven Püschel
2026-05-21 0:08 ` sashiko-bot
2026-05-20 22:44 ` [PATCH v7 28/28] arm64: dts: rockchip: add rga3 dt nodes Sven Püschel
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=20260520235633.0440A1F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=robh@kernel.org \
--cc=s.pueschel@pengutronix.de \
--cc=sashiko-reviews@lists.linux.dev \
/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