Linux Media Controller development
 help / color / mirror / Atom feed
* [PATCH] media: rppx1: Validate and process a copy of the parameters buffer
@ 2026-12-02 15:31 Hyeonjun Hong
  2026-12-02 16:12 ` Niklas Söderlund
  0 siblings, 1 reply; 5+ messages in thread
From: Hyeonjun Hong @ 2026-12-02 15:31 UTC (permalink / raw)
  To: Jacopo Mondi, Jai Luthra, Niklas Söderlund,
	Mauro Carvalho Chehab
  Cc: linux-media, linux-renesas-soc, linux-kernel, stable

rppx1_params() validates the parameters buffer in place and then walks
it again to process the blocks. The buffer comes from a vb2 queue that
supports VB2_MMAP and VB2_DMABUF, so userspace can keep a mapping and
modify the content while the driver is using it.

Everything that was validated can therefore change before it is used.
The loop in rppx1_params() reads data_size again on each iteration and
advances by a header.size that it reads again, so a larger data_size
makes the driver read past the end of the buffer, and a header.size of
0 in a block that is not handled keeps the loop from advancing.
v4l2_isp_params_validate_buffer() itself reads data_size more than once.

The other users of the v4l2-isp helpers, rkisp1, mali-c55 and c3-isp,
copy the buffer first and only use the copy. Do the same here.

Fixes: b562e72bdb98 ("media: rppx1: Add framework to support Dreamchip RPPX1 ISP")
Cc: stable@vger.kernel.org
Signed-off-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
---
How this was found and tested:

- Found by comparing the callers of the v4l2-isp helpers, with the help
  of an LLM (Claude). Same review pass as
  https://lore.kernel.org/all/20261202151718.694973-1-hyeonjunhong330@gmail.com/
  but independent of that series.
- I do not have R-Car hardware. Tested only in a userspace model that
  compiles the unmodified function bodies of rppx1_params() and the
  v4l2-isp validation helpers with AddressSanitizer, while a second
  thread flips data_size in the shared buffer.
  Before: heap-buffer-overflow READ of size 2 right after the buffer,
  in v4l2_isp_params_validate_buffer(), within the first second. I
  could only reproduce it at -O0; at -O1 the compiler merges the reads.
  I have not checked how many reads the arm64 kernel build ends up
  with.
  After: 400 million iterations without a report.
- Compile tested for arm64 with W=1.
- Not tested on hardware.

This conflicts with "[PATCH 0/7] Introduce v4l2-isp reset buffers",
which changes the same function. I can rebase on top of it if that
series goes in first.

The copy is allocated per call to keep the fix local to rppx1_params().
The other drivers allocate it once in buf_init; that would need a
change in how rcar-isp and rppx1 share the buffer, which I can do in a
follow-up if preferred.

 .../platform/dreamchip/rppx1/rpp_params.c     | 21 ++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/drivers/media/platform/dreamchip/rppx1/rpp_params.c b/drivers/media/platform/dreamchip/rppx1/rpp_params.c
index 44b0c2e673b7..241b92366a9d 100644
--- a/drivers/media/platform/dreamchip/rppx1/rpp_params.c
+++ b/drivers/media/platform/dreamchip/rppx1/rpp_params.c
@@ -5,6 +5,8 @@
  * Copyright (C) 2026 Ragnatech AB
  */
 
+#include <linux/slab.h>
+
 #include <media/v4l2-isp.h>
 #include <media/videobuf2-v4l2.h>
 
@@ -49,13 +51,20 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
 	if (ret)
 		return ret;
 
-	cfg = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
+	/*
+	 * The buffer is shared with userspace, which can modify its content
+	 * at any time. Validate and process a copy.
+	 */
+	cfg = kmemdup(vb2_plane_vaddr(&vbuf->vb2_buf, 0),
+		      vb2_get_plane_payload(vb, 0), GFP_KERNEL);
+	if (!cfg)
+		return -ENOMEM;
 
 	ret = v4l2_isp_params_validate_buffer(rpp->dev, vb, cfg,
 					      rppx1_ext_params_blocks_info,
 					      ARRAY_SIZE(rppx1_ext_params_blocks_info));
 	if (ret)
-		return ret;
+		goto out_free;
 
 	/* Walk the list of parameter blocks and process them. */
 	block_offset = 0;
@@ -63,7 +72,6 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
 		const union rppx1_params_block *block =
 			(const union rppx1_params_block *)&cfg->data[block_offset];
 		struct rpp_module *module;
-		int ret;
 
 		block_offset += block->header.size;
 
@@ -107,13 +115,16 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
 			dev_err(rpp->dev,
 				"Error processing RPPX1 block type: 0x%04x\n",
 				block->header.type);
-			return ret;
+			goto out_free;
 		}
 	}
 
 	/* Trigger update of shadowed registers after frame is done. */
 	write(priv, RPPX1_RPP_HDR_UPD_REG, RPPX1_RPP_HDR_UPD_REGS_GEN_CFG_UPD);
 
-	return 0;
+out_free:
+	kfree(cfg);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(rppx1_params);

base-commit: 62dfd7c0e599b6f02392b117ee77a070796ad765
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] media: rppx1: Validate and process a copy of the parameters buffer
  2026-12-02 15:31 [PATCH] media: rppx1: Validate and process a copy of the parameters buffer Hyeonjun Hong
@ 2026-12-02 16:12 ` Niklas Söderlund
  2026-12-02 16:54   ` Hyeonjun Hong
  0 siblings, 1 reply; 5+ messages in thread
From: Niklas Söderlund @ 2026-12-02 16:12 UTC (permalink / raw)
  To: Hyeonjun Hong
  Cc: Jacopo Mondi, Jai Luthra, Mauro Carvalho Chehab, linux-media,
	linux-renesas-soc, linux-kernel, stable

Hi Hyeonjun,

Thanks for your work.

On 2026-12-03 00:31:37 +0900, Hyeonjun Hong wrote:
> rppx1_params() validates the parameters buffer in place and then walks
> it again to process the blocks. The buffer comes from a vb2 queue that
> supports VB2_MMAP and VB2_DMABUF, so userspace can keep a mapping and
> modify the content while the driver is using it.
> 
> Everything that was validated can therefore change before it is used.
> The loop in rppx1_params() reads data_size again on each iteration and
> advances by a header.size that it reads again, so a larger data_size
> makes the driver read past the end of the buffer, and a header.size of
> 0 in a block that is not handled keeps the loop from advancing.
> v4l2_isp_params_validate_buffer() itself reads data_size more than once.
> 
> The other users of the v4l2-isp helpers, rkisp1, mali-c55 and c3-isp,
> copy the buffer first and only use the copy. Do the same here.

Can't this be fixed in the v4l2-isp helpers instead of fixing every 
user?

> 
> Fixes: b562e72bdb98 ("media: rppx1: Add framework to support Dreamchip RPPX1 ISP")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hyeonjun Hong <hyeonjunhong330@gmail.com>
> ---
> How this was found and tested:
> 
> - Found by comparing the callers of the v4l2-isp helpers, with the help
>   of an LLM (Claude). Same review pass as
>   https://lore.kernel.org/all/20261202151718.694973-1-hyeonjunhong330@gmail.com/
>   but independent of that series.
> - I do not have R-Car hardware. Tested only in a userspace model that
>   compiles the unmodified function bodies of rppx1_params() and the
>   v4l2-isp validation helpers with AddressSanitizer, while a second
>   thread flips data_size in the shared buffer.
>   Before: heap-buffer-overflow READ of size 2 right after the buffer,
>   in v4l2_isp_params_validate_buffer(), within the first second. I
>   could only reproduce it at -O0; at -O1 the compiler merges the reads.
>   I have not checked how many reads the arm64 kernel build ends up
>   with.
>   After: 400 million iterations without a report.
> - Compile tested for arm64 with W=1.
> - Not tested on hardware.
> 
> This conflicts with "[PATCH 0/7] Introduce v4l2-isp reset buffers",
> which changes the same function. I can rebase on top of it if that
> series goes in first.
> 
> The copy is allocated per call to keep the fix local to rppx1_params().
> The other drivers allocate it once in buf_init; that would need a
> change in how rcar-isp and rppx1 share the buffer, which I can do in a
> follow-up if preferred.
> 
>  .../platform/dreamchip/rppx1/rpp_params.c     | 21 ++++++++++++++-----
>  1 file changed, 16 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/media/platform/dreamchip/rppx1/rpp_params.c b/drivers/media/platform/dreamchip/rppx1/rpp_params.c
> index 44b0c2e673b7..241b92366a9d 100644
> --- a/drivers/media/platform/dreamchip/rppx1/rpp_params.c
> +++ b/drivers/media/platform/dreamchip/rppx1/rpp_params.c
> @@ -5,6 +5,8 @@
>   * Copyright (C) 2026 Ragnatech AB
>   */
>  
> +#include <linux/slab.h>
> +
>  #include <media/v4l2-isp.h>
>  #include <media/videobuf2-v4l2.h>
>  
> @@ -49,13 +51,20 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
>  	if (ret)
>  		return ret;
>  
> -	cfg = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
> +	/*
> +	 * The buffer is shared with userspace, which can modify its content
> +	 * at any time. Validate and process a copy.
> +	 */
> +	cfg = kmemdup(vb2_plane_vaddr(&vbuf->vb2_buf, 0),
> +		      vb2_get_plane_payload(vb, 0), GFP_KERNEL);
> +	if (!cfg)
> +		return -ENOMEM;
>  
>  	ret = v4l2_isp_params_validate_buffer(rpp->dev, vb, cfg,
>  					      rppx1_ext_params_blocks_info,
>  					      ARRAY_SIZE(rppx1_ext_params_blocks_info));
>  	if (ret)
> -		return ret;
> +		goto out_free;
>  
>  	/* Walk the list of parameter blocks and process them. */
>  	block_offset = 0;
> @@ -63,7 +72,6 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
>  		const union rppx1_params_block *block =
>  			(const union rppx1_params_block *)&cfg->data[block_offset];
>  		struct rpp_module *module;
> -		int ret;
>  
>  		block_offset += block->header.size;
>  
> @@ -107,13 +115,16 @@ int rppx1_params(struct rppx1 *rpp, struct vb2_buffer *vb, size_t max_size,
>  			dev_err(rpp->dev,
>  				"Error processing RPPX1 block type: 0x%04x\n",
>  				block->header.type);
> -			return ret;
> +			goto out_free;
>  		}
>  	}
>  
>  	/* Trigger update of shadowed registers after frame is done. */
>  	write(priv, RPPX1_RPP_HDR_UPD_REG, RPPX1_RPP_HDR_UPD_REGS_GEN_CFG_UPD);
>  
> -	return 0;
> +out_free:
> +	kfree(cfg);
> +
> +	return ret;
>  }
>  EXPORT_SYMBOL_GPL(rppx1_params);
> 
> base-commit: 62dfd7c0e599b6f02392b117ee77a070796ad765
> -- 
> 2.34.1
> 

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] media: rppx1: Validate and process a copy of the parameters buffer
  2026-12-02 16:12 ` Niklas Söderlund
@ 2026-12-02 16:54   ` Hyeonjun Hong
  2026-12-02 16:57     ` Niklas Söderlund
  0 siblings, 1 reply; 5+ messages in thread
From: Hyeonjun Hong @ 2026-12-02 16:54 UTC (permalink / raw)
  To: Niklas Söderlund
  Cc: Jacopo Mondi, Jai Luthra, Mauro Carvalho Chehab, Daniel Scally,
	linux-media, linux-renesas-soc, linux-kernel, stable

Hi Niklas,

Thanks for the quick review.

On 2026-12-02 17:12:51 +0100, Niklas Söderlund wrote:
> Can't this be fixed in the v4l2-isp helpers instead of fixing every
> user?

Yes, I think it can, and it would be the better end state.

Today the copy is the driver's job. The kernel-doc of
v4l2_isp_params_validate_buffer() says drivers should copy the buffer
to kernel-only memory after v4l2_isp_params_validate_buffer_size() and
then validate the copy. rkisp1, mali-c55 and c3-isp do that, rppx1 is
the only user that passes the vb2 plane directly, so this patch only
fixes one user. But nothing in the helper enforces it.

A helper that does the size check, the copy and the block validation
in one call, with the driver passing the destination, would make it
hard to get wrong.

That is a larger change though: it touches all four drivers and
overlaps with Dan's "Introduce v4l2-isp reset buffers" series, and I
can only compile test the other three drivers. So I would suggest
taking this patch as the minimal fix for now, and I can look into the
helper rework once Dan's series has landed.

Would that work for you?

Thanks,
Hyeonjun

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] media: rppx1: Validate and process a copy of the parameters buffer
  2026-12-02 16:54   ` Hyeonjun Hong
@ 2026-12-02 16:57     ` Niklas Söderlund
  2026-12-02 17:04       ` Hyeonjun Hong
  0 siblings, 1 reply; 5+ messages in thread
From: Niklas Söderlund @ 2026-12-02 16:57 UTC (permalink / raw)
  To: Hyeonjun Hong
  Cc: Jacopo Mondi, Jai Luthra, Mauro Carvalho Chehab, Daniel Scally,
	linux-media, linux-renesas-soc, linux-kernel, stable

On 2026-12-03 01:54:19 +0900, Hyeonjun Hong wrote:
> Hi Niklas,
> 
> Thanks for the quick review.
> 
> On 2026-12-02 17:12:51 +0100, Niklas Söderlund wrote:
> > Can't this be fixed in the v4l2-isp helpers instead of fixing every
> > user?
> 
> Yes, I think it can, and it would be the better end state.
> 
> Today the copy is the driver's job. The kernel-doc of
> v4l2_isp_params_validate_buffer() says drivers should copy the buffer
> to kernel-only memory after v4l2_isp_params_validate_buffer_size() and
> then validate the copy. rkisp1, mali-c55 and c3-isp do that, rppx1 is
> the only user that passes the vb2 plane directly, so this patch only
> fixes one user. But nothing in the helper enforces it.
> 
> A helper that does the size check, the copy and the block validation
> in one call, with the driver passing the destination, would make it
> hard to get wrong.
> 
> That is a larger change though: it touches all four drivers and
> overlaps with Dan's "Introduce v4l2-isp reset buffers" series, and I
> can only compile test the other three drivers. So I would suggest
> taking this patch as the minimal fix for now, and I can look into the
> helper rework once Dan's series has landed.

Can't you base your work on Dan's series and list it as a dependency?

> 
> Would that work for you?
> 
> Thanks,
> Hyeonjun

-- 
Kind Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] media: rppx1: Validate and process a copy of the parameters buffer
  2026-12-02 16:57     ` Niklas Söderlund
@ 2026-12-02 17:04       ` Hyeonjun Hong
  0 siblings, 0 replies; 5+ messages in thread
From: Hyeonjun Hong @ 2026-12-02 17:04 UTC (permalink / raw)
  To: Niklas Söderlund
  Cc: Jacopo Mondi, Jai Luthra, Mauro Carvalho Chehab, Daniel Scally,
	linux-media, linux-renesas-soc, linux-kernel, stable

Hi Niklas,

On 2026-12-02 17:57:31 +0100, Niklas Söderlund wrote:
> Can't you base your work on Dan's series and list it as a dependency?

Yes, I can do that. I will rework this as a series on top of Dan's
that moves the copy into the v4l2-isp helpers and converts the four
drivers, and drop this patch in favour of it.

I have exams for the next two weeks, so I can only start on it after
that. I can only compile test rkisp1, mali-c55 and c3-isp, so I will
ask for testing on those in the cover letter.

Thanks,
Hyeonjun

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02 17:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 15:31 [PATCH] media: rppx1: Validate and process a copy of the parameters buffer Hyeonjun Hong
2026-12-02 16:12 ` Niklas Söderlund
2026-12-02 16:54   ` Hyeonjun Hong
2026-12-02 16:57     ` Niklas Söderlund
2026-12-02 17:04       ` Hyeonjun Hong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox