media: amphion: fix two out-of-bounds copies
Subsystems (2)
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
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.
---
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
@@ -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
Re: [PATCH 1/2] media: amphion: store only whole messages in the instance fifo
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 makeskfifo_out()copy up to 8 KB intopkt->data, a 512-byte array on the stack ofvpu_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")Signed-off-by Maoyi Xie <[email protected]>
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@@ -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
PATCH 2/2 media: amphion: reject encoded frames larger than the stream buffer
In reply to Maoyi Xievenc_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.
---
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
@@ -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
Re: [PATCH 2/2] media: amphion: reject encoded frames larger than the stream buffer
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 makevpu_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")Signed-off-by Maoyi Xie <[email protected]>---
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
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
-0v4l2_m2m_buf_done (vbuf,VB2_BUF_STATE_ERROR );return -ENOMEM ; }Signature · 1 line
2.34.1
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