Skip to contentSkip to conversation

media: rppx1: Validate and process a copy of the parameters buffer

PATCH In review Hyeonjun Hong 2 participants 5 messages Dec 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: rppx1: Validate and process a copy of the parameters buffer

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")

---
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/[email protected]/
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(-)
drivers/media/platform/dreamchip/rppx1/rpp_params.c +16 -5
index 44b0c2e673b7..241b92366a9d 100644
@@ -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

Signature · 1 line
2.34.1
#1 lore raw

Hi Hyeonjun,

Thanks for your work.

Quoted 12 lines · Hyeonjun Hong

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.

Quoted 2 lines

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?

Quoted 104 lines
Fixes b562e72bdb98 ("media: rppx1: Add framework to support Dreamchip RPPX1 ISP")

---
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/[email protected]/
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(-)
drivers/media/platform/dreamchip/rppx1/rpp_params.c +16 -5
index 44b0c2e673b7..241b92366a9d 100644
@@ -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

Signature · 1 line
2.34.1
Signature · 2 lines
Kind Regards,
Niklas Söderlund
#2 lore raw

Hi Niklas,

Thanks for the quick review.

Quoted 2 lines · Niklas Söderlund

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

#3 lore raw
Quoted 21 lines · Hyeonjun Hong

Hi Niklas,

Thanks for the quick review.

Quoted 2 lines · Niklas Söderlund

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.

Quoted 5 lines

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?

Quoted 5 lines

Would that work for you?

Thanks,
Hyeonjun

Signature · 2 lines
Kind Regards,
Niklas Söderlund
#4 lore raw

Hi Niklas,

Quoted 1 line · Niklas Söderlund

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