mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolas Dufresne <nicolas@ndufresne.ca>
To: Fan Wu <fanwu01@zju.edu.cn>,
	bin.liu@mediatek.com, mchehab@kernel.org,
	 matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com
Cc: irui.wang@mediatek.com, kyrie.wu@mediatek.com,
	hverkuil@kernel.org,  linux-media@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	 linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org
Subject: Re: [PATCH] media: mtk-jpeg: fix use-after-free in release path due to uncancelled work
Date: Thu, 19 Mar 2026 16:25:52 -0400	[thread overview]
Message-ID: <9816ad4b43c67b3e4dd779b44f5ee0d7d09d5ecd.camel@ndufresne.ca> (raw)
In-Reply-To: <20260304031934.147483-1-fanwu01@zju.edu.cn>

[-- Attachment #1: Type: text/plain, Size: 2596 bytes --]

Le mercredi 04 mars 2026 à 03:19 +0000, Fan Wu a écrit :
> The mtk_jpeg_release() function frees the context structure (ctx) without
> first cancelling any pending or running work in ctx->jpeg_work. This
> creates a race window where the workqueue callback may still be accessing
> the context memory after it has been freed.
> 
> Race condition:
> 
>     CPU 0 (release)                    CPU 1 (workqueue)
>     ----------------                   ------------------
>     close()
>       mtk_jpeg_release()
>                                        mtk_jpegenc_worker()
>                                          ctx = work->data
>                                          // accessing ctx
> 
>         kfree(ctx)  // freed!
>                                          access ctx  // UAF!
> 
> The work is queued via queue_work() during JPEG encode/decode operations
> (via mtk_jpeg_device_run). If the device is closed while work is pending
> or running, the work handler will access freed memory.
> 
> Fix this by calling cancel_work_sync() BEFORE acquiring the mutex. This
> ordering is critical: if cancel_work_sync() is called after mutex_lock(),
> and the work handler also tries to acquire the same mutex, it would cause
> a deadlock.
> 
> Note: The open error path does NOT need cancel_work_sync() because
> INIT_WORK() only initializes the work structure - it does not schedule
> it. Work is only scheduled later during ioctl operations.
> 
> Fixes: 5fb1c2361e56 ("mtk-jpegenc: add jpeg encode worker interface")
> Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>

Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>

> ---
>  drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> index c01124a349f6..8c684756d5fc 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> @@ -1202,6 +1202,7 @@ static int mtk_jpeg_release(struct file *file)
>  	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
>  	struct mtk_jpeg_ctx *ctx = mtk_jpeg_file_to_ctx(file);
>  
> +	cancel_work_sync(&ctx->jpeg_work);
>  	mutex_lock(&jpeg->lock);
>  	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
>  	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2026-03-19 20:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04  3:19 Fan Wu
2026-03-19 20:25 ` Nicolas Dufresne [this message]
2026-03-19 20:27 ` Nicolas Dufresne
2026-06-01  7:03 ` Icenowy Zheng

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=9816ad4b43c67b3e4dd779b44f5ee0d7d09d5ecd.camel@ndufresne.ca \
    --to=nicolas@ndufresne.ca \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=bin.liu@mediatek.com \
    --cc=fanwu01@zju.edu.cn \
    --cc=hverkuil@kernel.org \
    --cc=irui.wang@mediatek.com \
    --cc=kyrie.wu@mediatek.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mchehab@kernel.org \
    /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

all inboxes | Powered by JetHome®