Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker
@ 2023-11-06 14:48 Zheng Wang
  2023-11-06 14:48 ` [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the crash of multi-core JPEG devices Zheng Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Zheng Wang @ 2023-11-06 14:48 UTC (permalink / raw)
  To: dmitry.osipenko
  Cc: Kyrie.Wu, bin.liu, mchehab, matthias.bgg,
	angelogioacchino.delregno, linux-media, linux-kernel,
	linux-arm-kernel, linux-mediatek, Irui.Wang, security,
	hackerzheng666, amergnat, wenst, stable, Zheng Wang

Hello,

This v2 series fixes the use-after-free bug in mtk_jpeg_dec_device_run.
This patch fixes the security bug in chrome-os.
It inclues reverting the incomplete fix before and make the right fix.
Also,it fixes the error of timeout-worker-schedule in multiple-core
devices.

1. Remove cancel worker in mtk_jpeg_remove for the worker is only
registered in single-core device but we try to cacnel it in both
single-core and multiple-core devices.

2. Fix use-after-free bug by delay the schedule_delayed_work only if
mtk_jpeg_set_dec_dst runs successfully.

3. Delay the schedule_delayed_work in mtk_jpegdec_worker as it has same
code logic in mtk_jpeg_dec_device_run.

version 2 changes

-put the patches into on series suggested by Dmitry

Zheng Wang (3):
  media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the
    crash of multi-core JPEG devices
  media: mtk-jpeg: Fix use after free bug due to error path handling
    in mtk_jpeg_dec_device_run
  media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker.

 .../media/platform/mediatek/jpeg/mtk_jpeg_core.c    | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to  avoid the crash of multi-core JPEG devices
  2023-11-06 14:48 [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker Zheng Wang
@ 2023-11-06 14:48 ` Zheng Wang
  2023-11-06 15:30   ` kernel test robot
  2023-11-06 14:48 ` [RESEND PATCH v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Zheng Wang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Zheng Wang @ 2023-11-06 14:48 UTC (permalink / raw)
  To: dmitry.osipenko
  Cc: Kyrie.Wu, bin.liu, mchehab, matthias.bgg,
	angelogioacchino.delregno, linux-media, linux-kernel,
	linux-arm-kernel, linux-mediatek, Irui.Wang, security,
	hackerzheng666, amergnat, wenst, stable, Zheng Wang

This patch reverts commit c677d7ae8314
("media: mtk-jpeg: Fix use after free bug due to uncanceled work").
The job_timeout_work is initialized only for
the single-core JPEG device so it will cause the crash for multi-core
JPEG devices.

Fix it by removing the cancel_delayed_work_sync function.

Fixes: c677d7ae8314 ("media: mtk-jpeg: Fix use after free bug due to uncanceled work")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
v2:
- put the patches into a single series suggested by Dmitry
---
 drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
index 7194f88edc0f..60425c99a2b8 100644
--- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
@@ -1403,7 +1403,6 @@ static void mtk_jpeg_remove(struct platform_device *pdev)
 {
 	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
 
-	cancel_delayed_work_sync(&jpeg->job_timeout_work);
 	pm_runtime_disable(&pdev->dev);
 	video_unregister_device(jpeg->vdev);
 	v4l2_m2m_release(jpeg->m2m_dev);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [RESEND PATCH v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run
  2023-11-06 14:48 [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker Zheng Wang
  2023-11-06 14:48 ` [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the crash of multi-core JPEG devices Zheng Wang
@ 2023-11-06 14:48 ` Zheng Wang
  2023-11-06 14:48 ` [RESEND PATCH v2 3/3] media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker Zheng Wang
  2023-11-07 10:14 ` [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix " Hans Verkuil
  3 siblings, 0 replies; 7+ messages in thread
From: Zheng Wang @ 2023-11-06 14:48 UTC (permalink / raw)
  To: dmitry.osipenko
  Cc: Kyrie.Wu, bin.liu, mchehab, matthias.bgg,
	angelogioacchino.delregno, linux-media, linux-kernel,
	linux-arm-kernel, linux-mediatek, Irui.Wang, security,
	hackerzheng666, amergnat, wenst, stable, Zheng Wang

In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with
mtk_jpeg_job_timeout_work.

In mtk_jpeg_dec_device_run, if error happens in
mtk_jpeg_set_dec_dst, it will finally start the worker while
mark the job as finished by invoking v4l2_m2m_job_finish.

There are two methods to trigger the bug. If we remove the
module, it which will call mtk_jpeg_remove to make cleanup.
The possible sequence is as follows, which will cause a
use-after-free bug.

CPU0                  CPU1
mtk_jpeg_dec_...    |
  start worker	    |
                    |mtk_jpeg_job_timeout_work
mtk_jpeg_remove     |
  v4l2_m2m_release  |
    kfree(m2m_dev); |
                    |
                    | v4l2_m2m_get_curr_priv
                    |   m2m_dev->curr_ctx //use

If we close the file descriptor, which will call mtk_jpeg_release,
it will have a similar sequence.

Fix this bug by starting timeout worker only if started jpegdec worker
successfully. Then v4l2_m2m_job_finish will only be called in
either mtk_jpeg_job_timeout_work or mtk_jpeg_dec_device_run.
Fixes: b2f0d2724ba4 ("[media] vcodec: mediatek: Add Mediatek JPEG Decoder Driver")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: stable@vger.kernel.org
---
v2:
- put the patches into a single series suggested by Dmitry
---
 drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
index 60425c99a2b8..a39acde2724a 100644
--- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
@@ -1021,13 +1021,13 @@ static void mtk_jpeg_dec_device_run(void *priv)
 	if (ret < 0)
 		goto dec_end;
 
-	schedule_delayed_work(&jpeg->job_timeout_work,
-			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
-
 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
 	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
 		goto dec_end;
 
+	schedule_delayed_work(&jpeg->job_timeout_work,
+			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
+
 	spin_lock_irqsave(&jpeg->hw_lock, flags);
 	mtk_jpeg_dec_reset(jpeg->reg_base);
 	mtk_jpeg_dec_set_config(jpeg->reg_base,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [RESEND PATCH v2 3/3] media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker.
  2023-11-06 14:48 [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker Zheng Wang
  2023-11-06 14:48 ` [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the crash of multi-core JPEG devices Zheng Wang
  2023-11-06 14:48 ` [RESEND PATCH v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Zheng Wang
@ 2023-11-06 14:48 ` Zheng Wang
  2023-11-07 10:14 ` [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix " Hans Verkuil
  3 siblings, 0 replies; 7+ messages in thread
From: Zheng Wang @ 2023-11-06 14:48 UTC (permalink / raw)
  To: dmitry.osipenko
  Cc: Kyrie.Wu, bin.liu, mchehab, matthias.bgg,
	angelogioacchino.delregno, linux-media, linux-kernel,
	linux-arm-kernel, linux-mediatek, Irui.Wang, security,
	hackerzheng666, amergnat, wenst, stable, Zheng Wang

In mtk_jpegdec_worker, if error occurs in mtk_jpeg_set_dec_dst, it
will start the timeout worker and invoke v4l2_m2m_job_finish at
the same time. This will break the logic of design for there should
be only one function to call v4l2_m2m_job_finish. But now the timeout
handler and mtk_jpegdec_worker will both invoke it.

Fix it by start the worker only if mtk_jpeg_set_dec_dst successfully
finished.

Fixes: da4ede4b7fd6 ("media: mtk-jpeg: move data/code inside CONFIG_OF blocks")
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: stable@vger.kernel.org
---
v2:
- put the patches into a single series suggested by Dmitry
---
 drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
index a39acde2724a..c3456c700c07 100644
--- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
+++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
@@ -1749,9 +1749,6 @@ static void mtk_jpegdec_worker(struct work_struct *work)
 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 
-	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
-			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
-
 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
 	if (mtk_jpeg_set_dec_dst(ctx,
 				 &jpeg_src_buf->dec_param,
@@ -1761,6 +1758,9 @@ static void mtk_jpegdec_worker(struct work_struct *work)
 		goto setdst_end;
 	}
 
+	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
+			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
+
 	spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags);
 	ctx->total_frame_num++;
 	mtk_jpeg_dec_reset(comp_jpeg[hw_id]->reg_base);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to  avoid the crash of multi-core JPEG devices
  2023-11-06 14:48 ` [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the crash of multi-core JPEG devices Zheng Wang
@ 2023-11-06 15:30   ` kernel test robot
  0 siblings, 0 replies; 7+ messages in thread
From: kernel test robot @ 2023-11-06 15:30 UTC (permalink / raw)
  To: Zheng Wang; +Cc: stable, oe-kbuild-all

Hi,

Thanks for your patch.

FYI: kernel test robot notices the stable kernel rule is not satisfied.

The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html#option-1

Rule: add the tag "Cc: stable@vger.kernel.org" in the sign-off area to have the patch automatically included in the stable tree.
Subject: [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to  avoid the crash of multi-core JPEG devices
Link: https://lore.kernel.org/stable/20231106144811.868127-2-zyytlz.wz%40163.com

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker
  2023-11-06 14:48 [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker Zheng Wang
                   ` (2 preceding siblings ...)
  2023-11-06 14:48 ` [RESEND PATCH v2 3/3] media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker Zheng Wang
@ 2023-11-07 10:14 ` Hans Verkuil
  2023-11-08  2:56   ` Zheng Hacker
  3 siblings, 1 reply; 7+ messages in thread
From: Hans Verkuil @ 2023-11-07 10:14 UTC (permalink / raw)
  To: Zheng Wang, dmitry.osipenko
  Cc: Kyrie.Wu, bin.liu, mchehab, matthias.bgg,
	angelogioacchino.delregno, linux-media, linux-kernel,
	linux-arm-kernel, linux-mediatek, Irui.Wang, security,
	hackerzheng666, amergnat, wenst, stable

On 06/11/2023 15:48, Zheng Wang wrote:
> Hello,
> 
> This v2 series fixes the use-after-free bug in mtk_jpeg_dec_device_run.
> This patch fixes the security bug in chrome-os.
> It inclues reverting the incomplete fix before and make the right fix.
> Also,it fixes the error of timeout-worker-schedule in multiple-core
> devices.
> 
> 1. Remove cancel worker in mtk_jpeg_remove for the worker is only
> registered in single-core device but we try to cacnel it in both
> single-core and multiple-core devices.
> 
> 2. Fix use-after-free bug by delay the schedule_delayed_work only if
> mtk_jpeg_set_dec_dst runs successfully.
> 
> 3. Delay the schedule_delayed_work in mtk_jpegdec_worker as it has same
> code logic in mtk_jpeg_dec_device_run.
> 
> version 2 changes
> 
> -put the patches into on series suggested by Dmitry
> 
> Zheng Wang (3):
>   media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the
>     crash of multi-core JPEG devices
>   media: mtk-jpeg: Fix use after free bug due to error path handling
>     in mtk_jpeg_dec_device_run
>   media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker.
> 
>  .../media/platform/mediatek/jpeg/mtk_jpeg_core.c    | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 

Please don't resend. If it is in patchwork.linuxtv.org then it is good.
Fixes (unless they fix something really nasty) can take several weeks before
they are picked up. I usually schedule 2 or 3 rounds per kernel cycle where
I go through all the pending patches with fixes.

Regards,

	Hans

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker
  2023-11-07 10:14 ` [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix " Hans Verkuil
@ 2023-11-08  2:56   ` Zheng Hacker
  0 siblings, 0 replies; 7+ messages in thread
From: Zheng Hacker @ 2023-11-08  2:56 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Zheng Wang, dmitry.osipenko, Kyrie.Wu, bin.liu, mchehab,
	matthias.bgg, angelogioacchino.delregno, linux-media,
	linux-kernel, linux-arm-kernel, linux-mediatek, Irui.Wang,
	security, amergnat, wenst, stable

Hans Verkuil <hverkuil@xs4all.nl> 于2023年11月7日周二 18:14写道:
>
> On 06/11/2023 15:48, Zheng Wang wrote:
> > Hello,
> >
> > This v2 series fixes the use-after-free bug in mtk_jpeg_dec_device_run.
> > This patch fixes the security bug in chrome-os.
> > It inclues reverting the incomplete fix before and make the right fix.
> > Also,it fixes the error of timeout-worker-schedule in multiple-core
> > devices.
> >
> > 1. Remove cancel worker in mtk_jpeg_remove for the worker is only
> > registered in single-core device but we try to cacnel it in both
> > single-core and multiple-core devices.
> >
> > 2. Fix use-after-free bug by delay the schedule_delayed_work only if
> > mtk_jpeg_set_dec_dst runs successfully.
> >
> > 3. Delay the schedule_delayed_work in mtk_jpegdec_worker as it has same
> > code logic in mtk_jpeg_dec_device_run.
> >
> > version 2 changes
> >
> > -put the patches into on series suggested by Dmitry
> >
> > Zheng Wang (3):
> >   media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the
> >     crash of multi-core JPEG devices
> >   media: mtk-jpeg: Fix use after free bug due to error path handling
> >     in mtk_jpeg_dec_device_run
> >   media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker.
> >
> >  .../media/platform/mediatek/jpeg/mtk_jpeg_core.c    | 13 ++++++-------
> >  1 file changed, 6 insertions(+), 7 deletions(-)
> >
>
> Please don't resend. If it is in patchwork.linuxtv.org then it is good.
> Fixes (unless they fix something really nasty) can take several weeks before
> they are picked up. I usually schedule 2 or 3 rounds per kernel cycle where
> I go through all the pending patches with fixes.
>

Oh Sorry for my rudeness. And I'm deeply appreciative of your contributions
to the community.

Best regards,
Zheng


> Regards,
>
>         Hans

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-11-08  2:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-06 14:48 [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix schedule error in mtk_jpegdec_worker Zheng Wang
2023-11-06 14:48 ` [RESEND PATCH v2 1/3] media: mtk-jpeg: Remove cancel worker in mtk_jpeg_remove to avoid the crash of multi-core JPEG devices Zheng Wang
2023-11-06 15:30   ` kernel test robot
2023-11-06 14:48 ` [RESEND PATCH v2 2/3] media: mtk-jpeg: Fix use after free bug due to error path handling in mtk_jpeg_dec_device_run Zheng Wang
2023-11-06 14:48 ` [RESEND PATCH v2 3/3] media: mtk-jpeg: Fix timeout schedule error in mtk_jpegdec_worker Zheng Wang
2023-11-07 10:14 ` [RESEND PATCH v2 0/3] Fix use-after-free bug in mtk_jpeg_dec_device_run and fix " Hans Verkuil
2023-11-08  2:56   ` Zheng Hacker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox