Message ID | 20230313154856.3691660-1-zyytlz.wz@163.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1280764wrd; Mon, 13 Mar 2023 09:28:44 -0700 (PDT) X-Google-Smtp-Source: AK7set/vG8kVwgU5F+Oimzta4IvGY77S/rbi6Ep+aHNKdQxdxuRUOvVi89bgMsj9R5JuPB1BPBQD X-Received: by 2002:a05:6a21:33a2:b0:cd:97f3:25e1 with SMTP id yy34-20020a056a2133a200b000cd97f325e1mr45792968pzb.51.1678724923839; Mon, 13 Mar 2023 09:28:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678724923; cv=none; d=google.com; s=arc-20160816; b=YfEkJIG7USA3rMHweuqQfEuy1uvPrICtTN6MoPQcbLoL96W48AXNdMQJCPXVM5A/8h NXu/ta7jQ5hwOStFQzjOK1UlSTgn3ctoGjxFWgIBg1w7bpwSzqoPAtDw4htOw8OOW8P6 Yo6g7RkRPPJpZ0Xp20fuP5tf8RcRbckQqkxHbVKz/tYuzM/HnwFcRS/Bi/zD/SU3qGkO lYJj+OuJQ6QySHHzopyX0pinFkVgHXkBESxBdxf59+kqLSFEHm4VUcwKyo8QnGagA9nn +rrPa2oWhvq1DACyh085JLdsWKCnh1Pv8ASnLsd0deIq73GYBQCrL72GNZhq6yziTbYf 7UIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=zZIaXbZODM+Wxzq0HHWHcgsRoTtcQ0oqs1PXU9bdNqE=; b=Ae73bkJEuiQsYSLt/mZHQi2UKcMWhEOq5JMiXRUnnaRZqTAV6G5rJS+DbrjY0BbMUo KV93DKiOyLAJZsWb7VF2RFiYxLpd3EeoEal0w17fzT1AyKFINvqBrl5OzqhyH93mXjuJ zE21tEuwwoxiKK1Bj1NOzSz1qZp1cZY3PYqjOr0NSYK4Dv7EriraEpeVvw0NwqZap9YQ /0/Hv9/rDEMoTIiupEwcoqbYBM34pt45GCYS5MvUXj6ycL1daMHeeegQ7KwZL8bgh7QU 17remSque//SaG2FyhOdaLYa3T+QHC18Zf27dviTGKG0Afv1qMAONaXCqWy5bxU63MJ7 zXOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=c9O4uZye; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t190-20020a6381c7000000b0050a1fd1a6e7si4033983pgd.737.2023.03.13.09.28.28; Mon, 13 Mar 2023 09:28:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=c9O4uZye; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230033AbjCMPuB (ORCPT <rfc822;realc9580@gmail.com> + 99 others); Mon, 13 Mar 2023 11:50:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229524AbjCMPt7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 13 Mar 2023 11:49:59 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.215]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1BC9442BCE; Mon, 13 Mar 2023 08:49:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=zZIaX bZODM+Wxzq0HHWHcgsRoTtcQ0oqs1PXU9bdNqE=; b=c9O4uZyeymy4ozAsWeZ+9 tH+ZJXubx0BfuhAl/F2uSdKaNYBIXIo/Y+NHm1pgT3HIJ65McCAiNfYqMPp2Om6h 7F3oitdwkm4jYc8QdH2ad/zSELs5v12rhY0iJQKy4LCPM/tZJnl5gYQgbQusxA2X wIOpKmkTOxgEILYJLiB6ec= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g3-2 (Coremail) with SMTP id _____wAnHYLqRQ9kDrURAA--.16196S2; Mon, 13 Mar 2023 23:48:58 +0800 (CST) From: Zheng Wang <zyytlz.wz@163.com> To: ezequiel@vanguardiasur.com.ar Cc: p.zabel@pengutronix.de, mchehab@kernel.org, linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, hverkuil@xs4all.nl, Zheng Wang <zyytlz.wz@163.com> Subject: [PATCH v4] media: hantro: fix use after free bug in hantro_remove due to race condition Date: Mon, 13 Mar 2023 23:48:56 +0800 Message-Id: <20230313154856.3691660-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _____wAnHYLqRQ9kDrURAA--.16196S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Wry3Xw4kJw47Jr4rWr4kZwb_yoW8Zw13pF W3KrW5KrWjqF47KFn7tay09ayfCasFqF4UXrZruw13AF9xtry7urySya18CF98JrZ3ZF4a qF4jqrWrZw42vFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zi-eOJUUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXA4xU1Xl56OqKgABsF 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-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760270665715627796?= X-GMAIL-MSGID: =?utf-8?q?1760270665715627796?= |
Series |
[v4] media: hantro: fix use after free bug in hantro_remove due to race condition
|
|
Commit Message
Zheng Wang
March 13, 2023, 3:48 p.m. UTC
In hantro_probe, vpu->watchdog_work is bound with
hantro_watchdog. Then hantro_end_prepare_run may
be called to start the work.
If we close the file or remove the module which will
call hantro_release and hantro_remove to make cleanup,
there may be an unfinished work. The possible sequence
is as follows, which will cause a typical UAF bug.
The same thing will happen in hantro_release, and use
ctx after freeing it.
Fix it by canceling the work before cleanup in hantro_release.
CPU0 CPU1
|hantro_watchdog
hantro_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>
Fixes: 932a9317ac49 ("media: hantro: Add helpers to prepare/finish a run")
---
v4:
- add Fixes label to help with the fix
v3:
- use cancel_delayed_work_sync instead of cancel_delayed_work and add it to
hantro_release suggested by Hans Verkuil
v2:
- move the cancel-work-related code to hantro_remove suggested by Hans Verkuil
---
drivers/media/platform/verisilicon/hantro_drv.c | 2 ++
1 file changed, 2 insertions(+)
Comments
Hi Zheng, Hans, On Mon, Mar 13, 2023 at 12:49 PM Zheng Wang <zyytlz.wz@163.com> wrote: > > In hantro_probe, vpu->watchdog_work is bound with > hantro_watchdog. Then hantro_end_prepare_run may > be called to start the work. > > If we close the file or remove the module which will > call hantro_release and hantro_remove to make cleanup, > there may be an unfinished work. The possible sequence > is as follows, which will cause a typical UAF bug. > > The same thing will happen in hantro_release, and use > ctx after freeing it. > > Fix it by canceling the work before cleanup in hantro_release. > > CPU0 CPU1 > > |hantro_watchdog > hantro_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> > Fixes: 932a9317ac49 ("media: hantro: Add helpers to prepare/finish a run") Thanks for the patch. Give me a few days to take a closer look. Thanks, Ezequiel > --- > v4: > - add Fixes label to help with the fix > > v3: > - use cancel_delayed_work_sync instead of cancel_delayed_work and add it to > hantro_release suggested by Hans Verkuil > > v2: > - move the cancel-work-related code to hantro_remove suggested by Hans Verkuil > --- > drivers/media/platform/verisilicon/hantro_drv.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c > index b0aeedae7b65..86a4c0fa8c7d 100644 > --- a/drivers/media/platform/verisilicon/hantro_drv.c > +++ b/drivers/media/platform/verisilicon/hantro_drv.c > @@ -597,6 +597,7 @@ static int hantro_release(struct file *filp) > struct hantro_ctx *ctx = > container_of(filp->private_data, struct hantro_ctx, fh); > > + cancel_delayed_work_sync(&ctx->dev->watchdog_work); > /* > * No need for extra locking because this was the last reference > * to this file. > @@ -1099,6 +1100,7 @@ static int hantro_remove(struct platform_device *pdev) > > v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name); > > + cancel_delayed_work_sync(&vpu->watchdog_work); > media_device_unregister(&vpu->mdev); > hantro_remove_dec_func(vpu); > hantro_remove_enc_func(vpu); > -- > 2.25.1 >
Ezequiel Garcia <ezequiel@vanguardiasur.com.ar> 于2023年3月14日周二 20:05写道: > > Hi Zheng, Hans, > > On Mon, Mar 13, 2023 at 12:49 PM Zheng Wang <zyytlz.wz@163.com> wrote: > > > > In hantro_probe, vpu->watchdog_work is bound with > > hantro_watchdog. Then hantro_end_prepare_run may > > be called to start the work. > > > > If we close the file or remove the module which will > > call hantro_release and hantro_remove to make cleanup, > > there may be an unfinished work. The possible sequence > > is as follows, which will cause a typical UAF bug. > > > > The same thing will happen in hantro_release, and use > > ctx after freeing it. > > > > Fix it by canceling the work before cleanup in hantro_release. > > > > CPU0 CPU1 > > > > |hantro_watchdog > > hantro_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> > > Fixes: 932a9317ac49 ("media: hantro: Add helpers to prepare/finish a run") > > Thanks for the patch. > > Give me a few days to take a closer look. > Thanks for your reply. Hope you have a nice day :) Best regards, Zheng > Thanks, > Ezequiel > > > --- > > v4: > > - add Fixes label to help with the fix > > > > v3: > > - use cancel_delayed_work_sync instead of cancel_delayed_work and add it to > > hantro_release suggested by Hans Verkuil > > > > v2: > > - move the cancel-work-related code to hantro_remove suggested by Hans Verkuil > > --- > > drivers/media/platform/verisilicon/hantro_drv.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c > > index b0aeedae7b65..86a4c0fa8c7d 100644 > > --- a/drivers/media/platform/verisilicon/hantro_drv.c > > +++ b/drivers/media/platform/verisilicon/hantro_drv.c > > @@ -597,6 +597,7 @@ static int hantro_release(struct file *filp) > > struct hantro_ctx *ctx = > > container_of(filp->private_data, struct hantro_ctx, fh); > > > > + cancel_delayed_work_sync(&ctx->dev->watchdog_work); > > /* > > * No need for extra locking because this was the last reference > > * to this file. > > @@ -1099,6 +1100,7 @@ static int hantro_remove(struct platform_device *pdev) > > > > v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name); > > > > + cancel_delayed_work_sync(&vpu->watchdog_work); > > media_device_unregister(&vpu->mdev); > > hantro_remove_dec_func(vpu); > > hantro_remove_enc_func(vpu); > > -- > > 2.25.1 > >
diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c index b0aeedae7b65..86a4c0fa8c7d 100644 --- a/drivers/media/platform/verisilicon/hantro_drv.c +++ b/drivers/media/platform/verisilicon/hantro_drv.c @@ -597,6 +597,7 @@ static int hantro_release(struct file *filp) struct hantro_ctx *ctx = container_of(filp->private_data, struct hantro_ctx, fh); + cancel_delayed_work_sync(&ctx->dev->watchdog_work); /* * No need for extra locking because this was the last reference * to this file. @@ -1099,6 +1100,7 @@ static int hantro_remove(struct platform_device *pdev) v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name); + cancel_delayed_work_sync(&vpu->watchdog_work); media_device_unregister(&vpu->mdev); hantro_remove_dec_func(vpu); hantro_remove_enc_func(vpu);