Skip to contentSkip to conversation

media: rcar-isp: Fix ConfigDMA buffer overflow

PATCH In review Hyeonjun Hong 2 participants 11 messages Dec 2

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

cover media: rcar-isp: Fix ConfigDMA buffer overflow

Hi,

The bounds check that protects the ConfigDMA buffer of the R-Car ISP core does not account for the 8 byte header, so one address/value pair can be written past the end of the 16384 byte buffer. The number of pairs depends on the parameters buffer queued by userspace on the params video node.

Patch 1/2 fixes the check. Patch 2/2 makes buffer preparation fail when the buffer is full, as the result of the write callback is not checked by the rppx1 block handlers.

How this was found and tested:

- Found by reading the callers of the v4l2-isp helpers side by side, with the help of an LLM (Claude).
- I do not have R-Car hardware. The overflow was reproduced only in a userspace model that compiles the unmodified function bodies of risp_conf_dma_prepare(), rppx1_params(), the CCOR block handler and the v4l2-isp validation helpers, built with AddressSanitizer. 170 header-only, disabled CCOR_POST blocks: 2041 pairs, no report. 171 blocks: heap-buffer-overflow, WRITE of size 4, 0 bytes after the 16384 byte region, in risp_conf_dma_prepare().
With the series applied: stops at 2047 pairs (16384 bytes used) and returns -ENOMEM.
- Compile tested for arm64 with W=1.
- Not tested on hardware.

One thing I could not resolve from the code: risp_core_job_prepare() in core.c limits the pair count to (RISP_IO_PARAMS_BUF_SIZE - 8) / 16, i.e. 16 bytes per pair, while core-io.c and vsp1_vspx.c count 8 bytes per pair. With this series a buffer holding 1024..2047 pairs passes buf_prepare and is then rejected when the job is prepared. I left that check alone as I do not know which limit is the intended one.

Hyeonjun Hong (2):
media: rcar-isp: Account for the header in the ConfigDMA bounds check
media: rcar-isp: Fail buffer preparation if the ConfigDMA buffer is
full

 drivers/media/platform/renesas/rcar-isp/core-io.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

base-commit: 62dfd7c0e599b6f02392b117ee77a070796ad765

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 media: rcar-isp: Account for the header in the ConfigDMA bounds check

risp_io_params_buffer_prepare() fills the ConfigDMA buffer with address/value pairs starting 8 bytes into the buffer, the first 8 bytes being reserved for the header. The bounds check in risp_conf_dma_prepare() compares the size of the pairs alone against RISP_IO_PARAMS_BUF_SIZE, so it accepts one pair more than fits and the last accepted pair is written to bytes 16384..16391 of the 16384 byte buffer.

The number of pairs is controlled by the parameters buffer queued by userspace. v4l2_isp_params_validate_buffer() accepts repeated blocks of the same type and a header-only block still results in register writes. For example 171 header-only RPPX1_PARAMS_BLOCK_TYPE_CCOR_POST blocks with V4L2_ISP_PARAMS_FL_BLOCK_DISABLE set (1368 bytes of block data) produce more than the 2047 pairs that fit.

Include the header in the check.

Fixes 2151350f60d1 ("media: rcar-isp: Add support for ISPCORE")
---
 drivers/media/platform/renesas/rcar-isp/core-io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/renesas/rcar-isp/core-io.c +2 -2
index b60b91f43d42..5c6018491160 100644
@@ -507,8 +507,8 @@ static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
 {
 	struct risp_conf_dma_write_desc *desc = priv;
 
-	/* Bounds check, 8 bytes = address (4)+ value (4). */
-	if ((desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
+	/* Bounds check, 8 bytes header + 8 bytes = address (4) + value (4). */
+	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
 		return -ENOMEM;
 
 	(*desc->buf++) = desc->base | offset;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] media: rcar-isp: Account for the header in the ConfigDMA bounds check

Hi Hyeonjun,

Thanks for your work.

Quoted 29 lines · Hyeonjun Hong

risp_io_params_buffer_prepare() fills the ConfigDMA buffer with address/value pairs starting 8 bytes into the buffer, the first 8 bytes being reserved for the header. The bounds check in risp_conf_dma_prepare() compares the size of the pairs alone against RISP_IO_PARAMS_BUF_SIZE, so it accepts one pair more than fits and the last accepted pair is written to bytes 16384..16391 of the 16384 byte buffer.

The number of pairs is controlled by the parameters buffer queued by userspace. v4l2_isp_params_validate_buffer() accepts repeated blocks of the same type and a header-only block still results in register writes. For example 171 header-only RPPX1_PARAMS_BLOCK_TYPE_CCOR_POST blocks with V4L2_ISP_PARAMS_FL_BLOCK_DISABLE set (1368 bytes of block data) produce more than the 2047 pairs that fit.

Include the header in the check.

Fixes 2151350f60d1 ("media: rcar-isp: Add support for ISPCORE")
---
 drivers/media/platform/renesas/rcar-isp/core-io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/renesas/rcar-isp/core-io.c +0 -0
index b60b91f43d42..5c6018491160 100644

drivers/media/platform/renesas/rcar-isp/core-io.c static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value) · line 511

Quoted 7 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +2 -2
@@ -507,8 +507,8 @@ static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
 {
 	struct risp_conf_dma_write_desc *desc = priv;
 
-	/* Bounds check, 8 bytes = address (4)+ value (4). */
-	if ((desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
+	/* Bounds check, 8 bytes header + 8 bytes = address (4) + value (4). */
+	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)

Nice catch, but could you move the magic values to defines, maybe something like,

#define RISP_IO_PARAMS_HEADER_SIZE 8
#define RISP_IO_PARAMS_MAX_PAIRS ((RISP_IO_PARAMS_BUF_SIZE - RISP_IO_PARAMS_HEADER_SIZE) / 8)

And use them here? I really should have added RISP_IO_PARAMS_MAX_PAIRS myself when writing this...

Quoted 6 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +0 -0
 		return -ENOMEM;
 
 	(*desc->buf++) = desc->base | offset;
Signature · 1 line
2.34.1
Signature · 2 lines
Kind Regards,
Niklas Söderlund
#3 lore raw

Re: [PATCH 1/2] media: rcar-isp: Account for the header in the ConfigDMA bounds check

Hi Niklas,

Thanks for the review.

Quoted 7 lines · Niklas Söderlund

Nice catch, but could you move the magic values to defines, maybe something like,

#define RISP_IO_PARAMS_HEADER_SIZE 8
#define RISP_IO_PARAMS_MAX_PAIRS ((RISP_IO_PARAMS_BUF_SIZE - RISP_IO_PARAMS_HEADER_SIZE) / 8)

And use them here?

Will do in v2. The check then becomes

	if (desc->count >= RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;

and I will also update the comment above RISP_IO_PARAMS_BUF_SIZE, which says 2048 pairs.

One related question, also in the cover letter: risp_core_job_prepare() in core.c rejects the buffer if

	pairs > (RISP_IO_PARAMS_BUF_SIZE - 8) / 16

which is 1023, i.e. it counts 16 bytes per pair. Should that become RISP_IO_PARAMS_MAX_PAIRS as well? If so I can add a patch for it to v2.

Thanks,
Hyeonjun

#4 lore raw

Re: [PATCH 1/2] media: rcar-isp: Account for the header in the ConfigDMA bounds check

Quoted 10 lines · Hyeonjun Hong

Hi Niklas,

Thanks for the review.

Quoted 5 lines · Niklas Söderlund

Nice catch, but could you move the magic values to defines, maybe something like,

#define RISP_IO_PARAMS_HEADER_SIZE 8
#define RISP_IO_PARAMS_MAX_PAIRS ((RISP_IO_PARAMS_BUF_SIZE - RISP_IO_PARAMS_HEADER_SIZE) / 8)

d00h this should have been / 16, not / 8. Maybe add a define for that too, RISP_IO_PARAMS_PAIR_SIZE and use that ;-)

Quoted 11 lines
Quoted 2 lines

And use them here?

Will do in v2. The check then becomes

	if (desc->count >= RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;

and I will also update the comment above RISP_IO_PARAMS_BUF_SIZE, which says 2048 pairs.

Quoted 7 lines

One related question, also in the cover letter: risp_core_job_prepare() in core.c rejects the buffer if

	pairs > (RISP_IO_PARAMS_BUF_SIZE - 8) / 16

which is 1023, i.e. it counts 16 bytes per pair. Should that become RISP_IO_PARAMS_MAX_PAIRS as well? If so I can add a patch for it to v2.

Most likely.

Quoted 3 lines

Thanks,
Hyeonjun

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

Re: [PATCH 1/2] media: rcar-isp: Account for the header in the ConfigDMA bounds check

Hi Niklas,

Quoted 2 lines · Niklas Söderlund

d00h this should have been / 16, not / 8. Maybe add a define for that too, RISP_IO_PARAMS_PAIR_SIZE and use that ;-)

Sure, I will add RISP_IO_PARAMS_PAIR_SIZE, and a patch that makes risp_core_job_prepare() use RISP_IO_PARAMS_MAX_PAIRS.

One thing I would like to double check with you, since you know the hardware and I only have the code to go by. I may well be misreading it, but a pair looks like 8 bytes to me: risp_conf_dma_prepare() stores two u32 per pair, and vsp1_vspx.c transfers "pairs * 2 + 2" 32-bit words. The check in risp_core_job_prepare() is the only place I found that counts 16.

Either value fixes the overflow, 16 just gives 1023 pairs instead of 2047, so I am happy to go with whichever you prefer.

Thanks for the quick replies,
Hyeonjun

#6 lore raw

Re: [PATCH 1/2] media: rcar-isp: Account for the header in the ConfigDMA bounds check

Quoted 9 lines · Hyeonjun Hong

Hi Niklas,

Quoted 2 lines · Niklas Söderlund

d00h this should have been / 16, not / 8. Maybe add a define for that too, RISP_IO_PARAMS_PAIR_SIZE and use that ;-)

Sure, I will add RISP_IO_PARAMS_PAIR_SIZE, and a patch that makes risp_core_job_prepare() use RISP_IO_PARAMS_MAX_PAIRS.

Quoted 6 lines

One thing I would like to double check with you, since you know the hardware and I only have the code to go by. I may well be misreading it, but a pair looks like 8 bytes to me: risp_conf_dma_prepare() stores two u32 per pair, and vsp1_vspx.c transfers "pairs * 2 + 2" 32-bit words. The check in risp_core_job_prepare() is the only place I found that counts 16.

Good catch, and really shows why it should have been a defined. A data pair is two 32-bit words, so 8 bytes. Disregard my correction of to / 16.

Quoted 6 lines

Either value fixes the overflow, 16 just gives 1023 pairs instead of 2047, so I am happy to go with whichever you prefer.

Thanks for the quick replies,
Hyeonjun

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

PATCH 2/2 media: rcar-isp: Fail buffer preparation if the ConfigDMA buffer is full

In reply to Hyeonjun Hong

risp_conf_dma_prepare() returns -ENOMEM when the ConfigDMA buffer is full, but the rppx1 block handlers that call it do not check the result of the write callback. rppx1_params() then returns 0, the remaining register writes are dropped without notice and the truncated configuration is queued.

Record the condition in the write descriptor and fail risp_io_params_buffer_prepare() if it was hit, instead of adding error handling to every write in the rppx1 block handlers.

---
 drivers/media/platform/renesas/rcar-isp/core-io.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/media/platform/renesas/rcar-isp/core-io.c +9 -1
index 5c6018491160..a5f18aeb1f28 100644
@@ -501,6 +501,7 @@ struct risp_conf_dma_write_desc {
 	u32 *buf;
 	u32 base;
 	unsigned int count;
+	bool overflow;
 };
 
 static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
@@ -508,8 +509,10 @@ static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
 	struct risp_conf_dma_write_desc *desc = priv;
 
 	/* Bounds check, 8 bytes header + 8 bytes = address (4) + value (4). */
-	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
+	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE) {
+		desc->overflow = true;
 		return -ENOMEM;
+	}
 
 	(*desc->buf++) = desc->base | offset;
 	(*desc->buf++) = value;
@@ -534,6 +537,7 @@ static int risp_io_params_buffer_prepare(struct vb2_buffer *vb)
 	desc.buf = cpu_addr + 2;
 	desc.base = io->core->rppaddr;
 	desc.count = 0;
+	desc.overflow = false;
 
 	/* Fill params body. */
 	ret = rppx1_params(io->core->rpp, vb, io->format.fmt.meta.buffersize,
@@ -541,6 +545,10 @@ static int risp_io_params_buffer_prepare(struct vb2_buffer *vb)
 	if (ret)
 		return ret;
 
+	/* The block handlers do not check the result of the write callback. */
+	if (desc.overflow)
+		return -ENOMEM;
+
 	/* Fill params header. */
 	cpu_addr[0] = desc.count;
 	cpu_addr[1] = 0x0;
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH 2/2] media: rcar-isp: Fail buffer preparation if the ConfigDMA buffer is full

Hi Hyeonjun,

Thanks for your work.

Quoted 20 lines · Hyeonjun Hong

risp_conf_dma_prepare() returns -ENOMEM when the ConfigDMA buffer is full, but the rppx1 block handlers that call it do not check the result of the write callback. rppx1_params() then returns 0, the remaining register writes are dropped without notice and the truncated configuration is queued.

Record the condition in the write descriptor and fail risp_io_params_buffer_prepare() if it was hit, instead of adding error handling to every write in the rppx1 block handlers.

---
 drivers/media/platform/renesas/rcar-isp/core-io.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/media/platform/renesas/rcar-isp/core-io.c +0 -0
index 5c6018491160..a5f18aeb1f28 100644

drivers/media/platform/renesas/rcar-isp/core-io.c struct risp_conf_dma_write_desc { · line 504

Quoted 4 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +1 -0
@@ -501,6 +501,7 @@ struct risp_conf_dma_write_desc {
 	u32 *buf;
 	u32 base;
 	unsigned int count;
+	bool overflow;

I don't like adding a flag for this, and it's not needed...

drivers/media/platform/renesas/rcar-isp/core-io.c static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value) · line 512

Quoted 9 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +1 -1
 };
 
 static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
@@ -508,8 +509,10 @@ static int risp_conf_dma_prepare(void *priv, u32 offset, u32 value)
 	struct risp_conf_dma_write_desc *desc = priv;
 
 	/* Bounds check, 8 bytes header + 8 bytes = address (4) + value (4). */
-	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE)
+	if (8 + (desc->count + 1) * 8 > RISP_IO_PARAMS_BUF_SIZE) {

... This check while also setting the new overflow flag will return -ENOMEM ...

Quoted 15 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +3 -0
+		desc->overflow = true;
 		return -ENOMEM;
+	}
 
 	(*desc->buf++) = desc->base | offset;
 	(*desc->buf++) = value;
@@ -534,6 +537,7 @@ static int risp_io_params_buffer_prepare(struct vb2_buffer *vb)
 	desc.buf = cpu_addr + 2;
 	desc.base = io->core->rppaddr;
 	desc.count = 0;
+	desc.overflow = false;
 
 	/* Fill params body. */
 	ret = rppx1_params(io->core->rpp, vb, io->format.fmt.meta.buffersize,

drivers/media/platform/renesas/rcar-isp/core-io.c static int risp_io_params_buffer_prepare(struct vb2_buffer *vb) · line 546

Quoted 2 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +0 -0
@@ -541,6 +545,10 @@ static int risp_io_params_buffer_prepare(struct vb2_buffer *vb)
 	if (ret)
 		return ret;

... Which will be propagated and returned here ...

drivers/media/platform/renesas/rcar-isp/core-io.c static int risp_io_params_buffer_prepare(struct vb2_buffer *vb) · line 550

Quoted 4 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +3 -0
+	/* The block handlers do not check the result of the write callback. */
+	if (desc.overflow)
+		return -ENOMEM;

... So this check will never be reached.

Quoted 7 lines
drivers/media/platform/renesas/rcar-isp/core-io.c +1 -0
+
 	/* Fill params header. */
 	cpu_addr[0] = desc.count;
 	cpu_addr[1] = 0x0;
Signature · 1 line
2.34.1
Signature · 2 lines
Kind Regards,
Niklas Söderlund
#9 lore raw

Re: [PATCH 2/2] media: rcar-isp: Fail buffer preparation if the ConfigDMA buffer is full

Hi Niklas,

Quoted 2 lines · Niklas Söderlund

... This check while also setting the new overflow flag will return -ENOMEM ...

[...]

Quoted 1 line

... Which will be propagated and returned here ...

I do not think it is propagated. The block handlers ignore the return value of the write callback and return 0, e.g. rppx1_ccor_fill_params():

	write(priv, mod->base + CCOR_COEFF_REG(0), 0x1000);
	[...]
	return 0;

None of the write() calls in the rppx1_*.c handlers is checked, so rppx1_params() returns 0 after the buffer is full. In the userspace model, with only patch 1/2 applied, 700 header-only CCOR_POST blocks give 2047 pairs written and rppx1_params() == 0.

Quoted 1 line

I don't like adding a flag for this, and it's not needed...

Understood. Would you prefer one of these instead?

a) No flag, keep counting in the callback and check the count

    afterwards:

	if (desc->count++ >= RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;

    and in risp_io_params_buffer_prepare():

	if (desc.count > RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;

b) Make the rppx1 block handlers check and return the result of write(). That is the proper fix but touches about 170 call sites in rppx1.

c) Drop patch 2/2 and leave it as is.

Thanks,
Hyeonjun

#10 lore raw

Re: [PATCH 2/2] media: rcar-isp: Fail buffer preparation if the ConfigDMA buffer is full

Hello Hyeonjun,

Quoted 11 lines · Hyeonjun Hong

Hi Niklas,

Quoted 2 lines · Niklas Söderlund

... This check while also setting the new overflow flag will return -ENOMEM ...

[...]

Quoted 1 line

... Which will be propagated and returned here ...

I do not think it is propagated. The block handlers ignore the return value of the write callback and return 0, e.g. rppx1_ccor_fill_params():

Quoted 8 lines
	write(priv, mod->base + CCOR_COEFF_REG(0), 0x1000);
	[...]
	return 0;

None of the write() calls in the rppx1_*.c handlers is checked, so rppx1_params() returns 0 after the buffer is full. In the userspace model, with only patch 1/2 applied, 700 header-only CCOR_POST blocks give 2047 pairs written and rppx1_params() == 0.

You are correct, my bad. And that's not very nice and likely something that should be fixed.. I can't think of a nice idea right now other then patching all rppx1_*.c handlers, let me think on it. Or if you have a nice idea?

I think fixing this design flaw is the right solution. Or if we go another way changing the write() callback to void so it is obvious the return value is not used.

Quoted 16 lines
Quoted 2 lines

I don't like adding a flag for this, and it's not needed...

Understood. Would you prefer one of these instead?

a) No flag, keep counting in the callback and check the count

    afterwards:

	if (desc->count++ >= RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;

    and in risp_io_params_buffer_prepare():

	if (desc.count > RISP_IO_PARAMS_MAX_PAIRS)
		return -ENOMEM;
Quoted 3 lines

b) Make the rppx1 block handlers check and return the result of write(). That is the proper fix but touches about 170 call sites in rppx1.

If we can't think of something smarter I think this. Or do option (a) and switch the writer callback to void to make it clear the return value is not used.

Quoted 5 lines

c) Drop patch 2/2 and leave it as is.

Thanks,
Hyeonjun

Signature · 2 lines
Kind Regards,
Niklas Söderlund