Message ID | 20230302093715.811758-1-zyytlz.wz@163.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Hans Verkuil |
Headers |
Received: from vger.kernel.org ([23.128.96.18]) by www.linuxtv.org with esmtp (Exim 4.92) (envelope-from <linux-media-owner@vger.kernel.org>) id 1pXfOg-00A1az-Rg; Thu, 02 Mar 2023 09:38:47 +0000 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229945AbjCBJio (ORCPT <rfc822;mkrufky@linuxtv.org> + 1 other); Thu, 2 Mar 2023 04:38:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbjCBJim (ORCPT <rfc822;linux-media@vger.kernel.org>); Thu, 2 Mar 2023 04:38:42 -0500 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.215]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 939B04EDF; Thu, 2 Mar 2023 01:38:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=y+5KY JiUtormz/jrT3bvfPhN0fTpLqDXBrS1X7+kgf8=; b=Luv9SL5uJJDc9RSVtLhqv H9FG72kJFfqQAOmyzaVfLaS/CFaD6A9hWOTI0mplCS4xd1hJpyNtV36t7IiUt8lj KEVn5axzv/lW9q8UksXsGSVD6IRLnj8RA4MFC2/5PgAQD1wCULqBGilUSTQvpy+b D95rK4oukGMRnYo6HvacS8= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g0-1 (Coremail) with SMTP id _____wC3v4NNbgBkGKUVBw--.35501S2; Thu, 02 Mar 2023 17:37:17 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: mchehab@kernel.org Cc: bin.liu@mediatek.com, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH] media: mtk-jpeg: Fix use after free bug due to uncanceled work Date: Thu, 2 Mar 2023 17:37:15 +0800 Message-Id: <20230302093715.811758-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wC3v4NNbgBkGKUVBw--.35501S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF17uw15Jr4xtw4xCFy7trb_yoW8Xry7pr ZxK3yDCrWUWrs0qr1UJ3WUAF1rCw1rKa1xWr17uw4Iv3y3Jrs7JryFya48tFWIyF92k3Wr Jr10q3s7GrWDZFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zi-6pkUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/xtbBzg8mU2I0XgIo4gAAs0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-media.vger.kernel.org> X-Mailing-List: linux-media@vger.kernel.org X-LSpam-Score: -2.5 (--) X-LSpam-Report: No, score=-2.5 required=5.0 tests=BAYES_00=-1.9,DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,FREEMAIL_FORGED_FROMDOMAIN=0.001,FREEMAIL_FROM=0.001,HEADER_FROM_DIFFERENT_DOMAINS=0.5,MAILING_LIST_MULTI=-1 autolearn=ham autolearn_force=no |
Series |
media: mtk-jpeg: Fix use after free bug due to uncanceled work
|
|
Commit Message
Zheng Wang
March 2, 2023, 9:37 a.m. UTC
In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with
mtk_jpeg_job_timeout_work. Then mtk_jpeg_dec_device_run
and mtk_jpeg_enc_device_run may be called to start the
work.
If we remove the module which will call mtk_jpeg_remove
to make cleanup, there may be a unfinished work. The
possible sequence is as follows, which will cause a
typical UAF bug.
Fix it by canceling the work before cleanup in the mtk_jpeg_remove
CPU0 CPU1
|mtk_jpeg_job_timeout_work
mtk_jpeg_remove |
v4l2_m2m_release |
kfree(m2m_dev); |
|
| v4l2_m2m_get_curr_priv
| m2m_dev->curr_ctx //use
Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Thu, Mar 02, 2023 at 05:37:15PM +0800, Zheng Wang wrote: > In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with > mtk_jpeg_job_timeout_work. Then mtk_jpeg_dec_device_run > and mtk_jpeg_enc_device_run may be called to start the > work. > If we remove the module which will call mtk_jpeg_remove > to make cleanup, there may be a unfinished work. The > possible sequence is as follows, which will cause a > typical UAF bug. > > Fix it by canceling the work before cleanup in the mtk_jpeg_remove > > CPU0 CPU1 > > |mtk_jpeg_job_timeout_work > mtk_jpeg_remove | > v4l2_m2m_release | > kfree(m2m_dev); | > | > | v4l2_m2m_get_curr_priv > | m2m_dev->curr_ctx //use > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > --- > drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 2 +- > 1 file changed, 1 insertion(+), 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 969516a940ba..364513e7897e 100644 > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c > @@ -1793,7 +1793,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev) > static int mtk_jpeg_remove(struct platform_device *pdev) > { > struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); > - > + cancel_delayed_work(&jpeg->job_timeout_work); The empty line is needed (coding style). Also, this doesn't cancel the worker if it is already running. This should probably be cancel_delayed_work_sync(). Even then the question is if it is possible that new work is queued before the device is unregistered. Guenter > pm_runtime_disable(&pdev->dev); > video_unregister_device(jpeg->vdev); > v4l2_m2m_release(jpeg->m2m_dev); > -- > 2.25.1 >
Hi, Thanks for your reply. I think you're right. I don't know if there is other method to stop new work from enqueing. Could you please give me some advice about the fix? Regards, Zheng Guenter Roeck <linux@roeck-us.net> 于2023年3月9日周四 08:27写道: > > On Thu, Mar 02, 2023 at 05:37:15PM +0800, Zheng Wang wrote: > > In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with > > mtk_jpeg_job_timeout_work. Then mtk_jpeg_dec_device_run > > and mtk_jpeg_enc_device_run may be called to start the > > work. > > If we remove the module which will call mtk_jpeg_remove > > to make cleanup, there may be a unfinished work. The > > possible sequence is as follows, which will cause a > > typical UAF bug. > > > > Fix it by canceling the work before cleanup in the mtk_jpeg_remove > > > > CPU0 CPU1 > > > > |mtk_jpeg_job_timeout_work > > mtk_jpeg_remove | > > v4l2_m2m_release | > > kfree(m2m_dev); | > > | > > | v4l2_m2m_get_curr_priv > > | m2m_dev->curr_ctx //use > > > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com> > > --- > > drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 2 +- > > 1 file changed, 1 insertion(+), 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 969516a940ba..364513e7897e 100644 > > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c > > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c > > @@ -1793,7 +1793,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev) > > static int mtk_jpeg_remove(struct platform_device *pdev) > > { > > struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); > > - > > + cancel_delayed_work(&jpeg->job_timeout_work); > > The empty line is needed (coding style). Also, this doesn't cancel > the worker if it is already running. This should probably be > cancel_delayed_work_sync(). Even then the question is if it is > possible that new work is queued before the device is unregistered. > > Guenter > > > pm_runtime_disable(&pdev->dev); > > video_unregister_device(jpeg->vdev); > > v4l2_m2m_release(jpeg->m2m_dev); > > -- > > 2.25.1 > >
On 3/8/23 19:58, Zheng Hacker wrote: > Hi, > > Thanks for your reply. I think you're right. I don't know if there is > other method to stop new work from enqueing. Could you please give me > some advice about the fix? > Top-posting is discouraged. Anyway - I don't know the code well enough to suggest a solution. It all depends on the driver architecture. The maintainers might have a better idea. A worse problem appears to be that the worker is also canceled from mtk_jpeg_enc_irq() and mtk_jpeg_dec_irq(). Those are non-threaded interrupt handlers which, as far as I know, must not sleep and thus can not call cancel_delayed_work_sync(). I have no idea how to solve that problem either. Guenter > Regards, > Zheng > > Guenter Roeck <linux@roeck-us.net> 于2023年3月9日周四 08:27写道: >> >> On Thu, Mar 02, 2023 at 05:37:15PM +0800, Zheng Wang wrote: >>> In mtk_jpeg_probe, &jpeg->job_timeout_work is bound with >>> mtk_jpeg_job_timeout_work. Then mtk_jpeg_dec_device_run >>> and mtk_jpeg_enc_device_run may be called to start the >>> work. >>> If we remove the module which will call mtk_jpeg_remove >>> to make cleanup, there may be a unfinished work. The >>> possible sequence is as follows, which will cause a >>> typical UAF bug. >>> >>> Fix it by canceling the work before cleanup in the mtk_jpeg_remove >>> >>> CPU0 CPU1 >>> >>> |mtk_jpeg_job_timeout_work >>> mtk_jpeg_remove | >>> v4l2_m2m_release | >>> kfree(m2m_dev); | >>> | >>> | v4l2_m2m_get_curr_priv >>> | m2m_dev->curr_ctx //use >>> >>> Signed-off-by: Zheng Wang <zyytlz.wz@163.com> >>> --- >>> drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 2 +- >>> 1 file changed, 1 insertion(+), 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 969516a940ba..364513e7897e 100644 >>> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c >>> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c >>> @@ -1793,7 +1793,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev) >>> static int mtk_jpeg_remove(struct platform_device *pdev) >>> { >>> struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); >>> - >>> + cancel_delayed_work(&jpeg->job_timeout_work); >> >> The empty line is needed (coding style). Also, this doesn't cancel >> the worker if it is already running. This should probably be >> cancel_delayed_work_sync(). Even then the question is if it is >> possible that new work is queued before the device is unregistered. >> >> Guenter >> >>> pm_runtime_disable(&pdev->dev); >>> video_unregister_device(jpeg->vdev); >>> v4l2_m2m_release(jpeg->m2m_dev); >>> -- >>> 2.25.1 >>>
Guenter Roeck <linux@roeck-us.net> 于2023年3月9日周四 13:30写道: > > On 3/8/23 19:58, Zheng Hacker wrote: > > Hi, > > > > Thanks for your reply. I think you're right. I don't know if there is > > other method to stop new work from enqueing. Could you please give me > > some advice about the fix? > > > > Top-posting is discouraged. > Sorry I forgot that. Thanks for the kind reminder. > Anyway - > I don't know the code well enough to suggest a solution. > It all depends on the driver architecture. The maintainers might > have a better idea. > Yes, some related developers have reached out to me and discussed fixes with me. > A worse problem appears to be that the worker is also canceled > from mtk_jpeg_enc_irq() and mtk_jpeg_dec_irq(). Those are non-threaded > interrupt handlers which, as far as I know, must not sleep and thus > can not call cancel_delayed_work_sync(). I have no idea how to solve > that problem either. > I'd be glad to pass along your thoughts and recommendations to the relevant parties. Best regards, Zheng
diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c index 969516a940ba..364513e7897e 100644 --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c @@ -1793,7 +1793,7 @@ static int mtk_jpeg_probe(struct platform_device *pdev) static int mtk_jpeg_remove(struct platform_device *pdev) { struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); - + cancel_delayed_work(&jpeg->job_timeout_work); pm_runtime_disable(&pdev->dev); video_unregister_device(jpeg->vdev); v4l2_m2m_release(jpeg->m2m_dev);