Skip to contentSkip to conversation

media: amphion: fix two out-of-bounds copies

PATCH In review Maoyi Xie 2 participants 6 messages Dec 3

Subsystems (2)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

cover media: amphion: fix two out-of-bounds copies

Two fixes for the amphion VPU driver. The patches are independent.

Found with a CodeQL-based static analysis tool that we are developing. Each case was reproduced in a userspace harness under AddressSanitizer. Not tested on hardware. Compile-tested with W=1 on next-20261116.

I used an LLM to help write the harnesses and these patches.

Maoyi Xie (2):
media: amphion: store only whole messages in the instance fifo
media: amphion: reject encoded frames larger than the stream buffer

 drivers/media/platform/amphion/venc.c     | 3 ++-
 drivers/media/platform/amphion/vpu_msgs.c | 6 +++---
 2 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 media: amphion: store only whole messages in the instance fifo

vpu_inst_handle_msg() stores only the part of a firmware message that fits in the instance fifo. If another message is stored after it, the reader reads payload words as a header. Its num field makes kfifo_out() copy up to 8 KB into pkt->data, a 512-byte array on the stack of vpu_inst_run_work().

Drop a message that does not fit instead of storing part of it.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 61cbf1c1fa6d ("media: amphion: implement vpu core communication based on mailbox")

---
I used an LLM to help write the harness and this patch.

 drivers/media/platform/amphion/vpu_msgs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/platform/amphion/vpu_msgs.c +3 -3
index b74a407a19f2..a5bced4c1c94 100644
@@ -284,15 +284,15 @@ static void vpu_inst_handle_msg(struct vpu_inst *inst, struct vpu_rpc_event *pkt
 {
 	unsigned long bytes;
 	u32 id = pkt->hdr.id;
-	int ret;
 
 	if (!inst->workqueue)
 		return;
 
 	bytes = sizeof(pkt->hdr) + pkt->hdr.num * sizeof(u32);
-	ret = kfifo_in(&inst->msg_fifo, pkt, bytes);
-	if (ret != bytes)
+	if (kfifo_avail(&inst->msg_fifo) < bytes)
 		dev_err(inst->dev, "[%d:%d]overflow: %d\n", inst->core->id, inst->id, id);
+	else
+		kfifo_in(&inst->msg_fifo, pkt, bytes);
 	queue_work(inst->workqueue, &inst->msg_work);
 }
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 2/2 media: amphion: reject encoded frames larger than the stream buffer

In reply to Maoyi Xie

venc_get_one_encoded_frame() checks the frame size from the encoder firmware only against the capture buffer, which can be 8 MB. The stream buffer is 3 MB by default. A larger size can make vpu_helper_copy_from_stream_buffer() copy memory from beyond the stream buffer into the capture buffer.

Also reject a frame that is larger than the stream buffer.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 0401e659c1f9 ("media: amphion: add v4l2 m2m vpu encoder stateful driver")

---
I used an LLM to help write the harness and this patch.

 drivers/media/platform/amphion/venc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/amphion/venc.c +2 -1
index 2ba4ae20f829..3d8d46872bc5 100644
@@ -797,7 +797,8 @@ static int venc_get_one_encoded_frame(struct vpu_inst *inst,
 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
 		return 0;
 	}
-	if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
+	if (frame->bytesused > vbuf->vb2_buf.planes[0].length ||
+	    frame->bytesused > inst->stream_buffer.length) {
 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
 		return -ENOMEM;
 	}
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] media: amphion: store only whole messages in the instance fifo

In reply to Maoyi Xie
Quoted 6 lines · Maoyi Xie

vpu_inst_handle_msg() stores only the part of a firmware message that fits in the instance fifo. If another message is stored after it, the reader reads payload words as a header. Its num field makes kfifo_out() copy up to 8 KB into pkt->data, a 512-byte array on the stack of vpu_inst_run_work().

Quoted 8 lines

Drop a message that does not fit instead of storing part of it.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 61cbf1c1fa6d ("media: amphion: implement vpu core communication based on mailbox")
Quoted 33 lines

---
I used an LLM to help write the harness and this patch.

 drivers/media/platform/amphion/vpu_msgs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/platform/amphion/vpu_msgs.c +3 -3
index b74a407a19f2..a5bced4c1c94 100644
@@ -284,15 +284,15 @@ static void vpu_inst_handle_msg(struct vpu_inst *inst, struct vpu_rpc_event *pkt
 {
 	unsigned long bytes;
 	u32 id = pkt->hdr.id;
-	int ret;
 
 	if (!inst->workqueue)
 		return;
 
 	bytes = sizeof(pkt->hdr) + pkt->hdr.num * sizeof(u32);
-	ret = kfifo_in(&inst->msg_fifo, pkt, bytes);
-	if (ret != bytes)
+	if (kfifo_avail(&inst->msg_fifo) < bytes)
 		dev_err(inst->dev, "[%d:%d]overflow: %d\n", inst->core->id, inst->id, id);
+	else
+		kfifo_in(&inst->msg_fifo, pkt, bytes);
 	queue_work(inst->workqueue, &inst->msg_work);
 }
 
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] media: amphion: reject encoded frames larger than the stream buffer

In reply to Maoyi Xie

Hi,

Quoted 25 lines · Maoyi Xie

venc_get_one_encoded_frame() checks the frame size from the encoder firmware only against the capture buffer, which can be 8 MB. The stream buffer is 3 MB by default. A larger size can make vpu_helper_copy_from_stream_buffer() copy memory from beyond the stream buffer into the capture buffer.

Also reject a frame that is larger than the stream buffer.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 0401e659c1f9 ("media: amphion: add v4l2 m2m vpu encoder stateful driver")

---
I used an LLM to help write the harness and this patch.

 drivers/media/platform/amphion/venc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/platform/amphion/venc.c +0 -0
index 2ba4ae20f829..3d8d46872bc5 100644

drivers/media/platform/amphion/venc.c static int venc_get_one_encoded_frame(struct vpu_inst *inst, · line 801

Quoted 6 lines
drivers/media/platform/amphion/venc.c +2 -1
@@ -797,7 +797,8 @@ static int venc_get_one_encoded_frame(struct vpu_inst *inst,
 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
 		return 0;
 	}
-	if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
+	if (frame->bytesused > vbuf->vb2_buf.planes[0].length ||
+	    frame->bytesused > inst->stream_buffer.length) {

For the Windsor encoder, the hardware writes the encoded bitstream into the stream buffer. If there is insufficient space in the stream buffer, the encoder stalls when the write pointer catches up with the read pointer, rather than completing the frame and reporting a size larger than the stream buffer.

Therefore, the reported frame size should not exceed the stream buffer capacity under normal hardware operation.

Regards,
Ming

Quoted 7 lines
drivers/media/platform/amphion/venc.c +0 -0
 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
 		return -ENOMEM;
 	}
Signature · 1 line
2.34.1
author
#5 lore raw

Re: [PATCH 2/2] media: amphion: reject encoded frames larger than the stream buffer

Hi Ming,

Quoted 2 lines · Ming Qian(OSS)

Therefore, the reported frame size should not exceed the stream buffer capacity under normal hardware operation.

Thanks for the explanation. Please drop this patch.

Thanks,
Maoyi