Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Kyrie Wu" <kyrie.wu@mediatek.com>
Cc: conor+dt@kernel.org, linux-media@vger.kernel.org,
	devicetree@vger.kernel.org, robh@kernel.org
Subject: Re: [PATCH v5 03/14] media: mediatek: vcodec: add driver to support vcp encoder
Date: Wed, 03 Jun 2026 11:01:22 +0000	[thread overview]
Message-ID: <20260603110123.541FB1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260603084045.17488-4-kyrie.wu@mediatek.com>

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Untrusted coprocessor input `obj->id` is used directly as an array index without bounds checking, causing out-of-bounds memory access and potential arbitrary code execution.
- [High] The kthread_run() task pointer is discarded, causing a memory leak and a fatal use-after-free when the device is unbound.
- [High] The msg_signaled flag is not reset before waiting, allowing a stale, delayed ACK to trigger a false positive for a subsequent IPC message.
- [High] The driver registers an IPC mailbox and a notifier but fails to unregister them during device teardown, leading to a Use-After-Free.
- [High] The message processing thread exits permanently upon receiving an unhandled message ID, hanging the entire driver.
- [High] The driver fails to initialize the `ipi_desc` mutex for the CORE decoder, leading to lockdep warnings and potential crashes.
--

commit 7793240a286886003391171d60e17e6a2e3e89f8
Author: Yunfei Dong <yunfei.dong@mediatek.com>

media: mediatek: vcodec: add driver to support vcp encoder

Encoder also need to call vcp interface to communicate with vcp,
add driver to support encoder.

> diff --git a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c
> --- a/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c
> +++ b/drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c
> @@ -226,8 +228,13 @@ static int mtk_vcodec_vcp_msg_ipi_send(struct mtk_vcodec_fw *fw, int id, void *b
>  	}
>  
>  	mutex_lock(&vcp->ipi_mutex);
> -	feature_id = VDEC_FEATURE_ID;
> -	mailbox_id = IPI_OUT_VDEC_1;
> +	if (fw->fw_use == ENCODER) {
> +		feature_id = VENC_FEATURE_ID;
> +		mailbox_id = IPI_OUT_VENC_0;
> +	} else {
> +		feature_id = VDEC_FEATURE_ID;
> +		mailbox_id = IPI_OUT_VDEC_1;
> +	}

[Severity: High]
This is a pre-existing issue, but does mtk_vcodec_vcp_msg_ipi_send()
properly handle stale delayed ACKs?

If wait_event_timeout() times out later in this function, it appears it sets
the msg_signaled flag to false and returns. However, if a delayed ACK arrives
afterward, the worker thread will set the flag to true.

Since mtk_vcodec_vcp_msg_ipi_send() does not re-initialize the msg_signaled
flag to false before sending the next IPC message, could a subsequent wait
immediately return success without actually waiting for the new message to
be acknowledged?

[ ... ]

> @@ -383,6 +397,20 @@ static int mtk_vcodec_vcp_load_firmware(struct mtk_vcodec_fw *fw)
>  	mutex_init(&fw->vcp->ipi_desc[ipi_id].lock);
>  	mutex_init(&fw->vcp->ipi_mutex);

[Severity: High]
This isn't a bug introduced by this patch, but are all the ipi_desc mutexes
properly initialized?

Here, mutex_init() is only called for a single ipi_id. If this path is taken
for the decoder, it initializes the lock for VCP_IPI_LAT_DECODER, but it omits
VCP_IPI_CORE_DECODER.

When the decoder client later registers its CORE handler,
mtk_vcodec_vcp_ipi_register() will attempt to acquire the lock. Could locking
the uninitialized CORE mutex lead to lockdep splats or undefined behavior?

>  
> +	if (fw->fw_use == ENCODER) {
> +		kthread_run(mtk_vcodec_vcp_msg_process_thread, fw, "vcp_enc_msq_thread");
> +

[Severity: High]
This is a pre-existing issue, but is it safe to discard the task pointer
returned by kthread_run()?

If the pointer is not saved, kthread_stop() cannot be called in
mtk_vcodec_vcp_release() during device teardown. Because the fw structure
is allocated with devm_kzalloc, the memory will be automatically freed when
the driver unbinds.

Could this cause a use-after-free when the unstopped thread wakes up
and accesses the freed fw->vcp structures?

[Severity: Critical]
This is a pre-existing issue, but does mtk_vcodec_vcp_msg_process_thread()
validate the obj->id from the coprocessor?

Looking at the thread function, it appears the ID from the IPC payload is
used directly as an array index:

handler = fw->vcp->ipi_desc[obj->id].handler;

If a compromised or malicious coprocessor sends an out-of-bounds ID, could
this read out-of-bounds function pointers and execute them when calling
handler(), leading to arbitrary kernel code execution?

[Severity: High]
This isn't a bug introduced by this patch, but how does
mtk_vcodec_vcp_msg_process_thread() handle invalid message IDs?

If the handler is null for a message, the function executes return -EINVAL;
instead of using continue; to process the next message.

Does this permanently terminate the kthread loop? If the thread exits,
wouldn't all subsequent valid IPC messages be queued but never processed,
permanently hanging active decode or encode instances?

[ ... ]

> @@ -438,8 +466,8 @@ static void mtk_vcodec_vcp_release(struct mtk_vcodec_fw *fw)
>  	if (!fw->vcp->is_register_done)
>  		return;
>  
> -	feature_id = VDEC_FEATURE_ID;
> -	ret = vcp_device->ops->vcp_deregister_feature(vcp_device, VDEC_FEATURE_ID);
> +	feature_id = fw->fw_use == ENCODER ? VENC_FEATURE_ID : VDEC_FEATURE_ID;
> +	ret = vcp_device->ops->vcp_deregister_feature(vcp_device, feature_id);
>  	if (ret < 0) {
>  		dev_err(dev, "deregister feature_id(%d) fail(%d)\n", feature_id, ret);
>  		return;

[Severity: High]
This is a pre-existing issue, but does this release function completely
tear down the IPC mailbox and notifier?

The driver registers an IPC mailbox via mtk_vcp_mbox_ipc_register() and a
notifier via vcp_register_notify() in mtk_vcodec_vcp_load_firmware().
However, these do not appear to be unregistered here during teardown.

Because fw->vcp is managed by devm_kzalloc, could this leave dangling
callbacks in the IPC or notifier frameworks that might trigger a
use-after-free if an event arrives after the device is unbound?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260603084045.17488-1-kyrie.wu@mediatek.com?part=3

  reply	other threads:[~2026-06-03 11:01 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-03  8:40 [PATCH v5 00/14] media: mediatek: vcodec: support video decoder in mt8196 Kyrie Wu
2026-06-03  8:40 ` [PATCH v5 01/14] media: mediatek: vcodec: support vcp architecture Kyrie Wu
2026-06-03  8:40 ` [PATCH v5 02/14] media: mediatek: vcodec: add driver to support vcp Kyrie Wu
2026-06-03  8:58   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 03/14] media: mediatek: vcodec: add driver to support vcp encoder Kyrie Wu
2026-06-03 11:01   ` sashiko-bot [this message]
2026-06-03  8:40 ` [PATCH v5 04/14] media: mediatek: vcodec: get different firmware ipi id Kyrie Wu
2026-06-03  8:40 ` [PATCH v5 05/14] media: mediatek: vcodec: get share memory address Kyrie Wu
2026-06-03  8:59   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 06/14] media: mediatek: vcodec: define MT8196 vcodec levels Kyrie Wu
2026-06-03  8:51   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 07/14] media: mediatek: vcodec: support 36bit iova address Kyrie Wu
2026-06-03  8:52   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 08/14] media: mediatek: vcodec: clean xpc status Kyrie Wu
2026-06-03  8:54   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 09/14] media: mediatek: vcodec: add debug information Kyrie Wu
2026-06-03  8:54   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 10/14] media: mediatek: vcodec: send share memory address to vcp Kyrie Wu
2026-06-03 11:09   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 11/14] dt-bindings: media: mediatek: vcodec: add decoder dt-bindings for mt8196 Kyrie Wu
2026-06-03  9:03   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 12/14] media: mediatek: vcodec: add decoder compatible to support mt8196 Kyrie Wu
2026-06-03  8:59   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 13/14] media: mediatek: decoder: fill av1 buffer size with picinfo Kyrie Wu
2026-06-03  9:07   ` sashiko-bot
2026-06-03  8:40 ` [PATCH v5 14/14] media: mediatek: decoder: support av1 extend vsi Kyrie Wu
2026-06-03  9:10   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260603110123.541FB1F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=kyrie.wu@mediatek.com \
    --cc=linux-media@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox