linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] media: cedrus: fix use after free bug in cedrus_remove due to race condition
@ 2023-03-08  3:23 Zheng Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Zheng Wang @ 2023-03-08  3:23 UTC (permalink / raw)
  To: mchehab
  Cc: wens, jernej.skrabec, samuel, linux-media, linux-staging,
	linux-arm-kernel, linux-sunxi, linux-kernel, hackerzheng666,
	1395428693sheep, alex000young, Zheng Wang

In cedrus_probe, dev->watchdog_work is bound with cedrus_watchdog function.
In cedrus_device_run, it will started by schedule_delayed_work. If there is
unfinished work in cedrus_remove, there may be a race condition and trigger
UAF bug.

CPU0                  CPU1

                    |cedrus_watchdog
cedrus_remove       |
  v4l2_m2m_release  |
  kfree(m2m_dev)    |
                    |
                    | v4l2_m2m_get_curr_priv
                    |   m2m_dev //use

Fix it by canceling the worker in cedrus_remove.

Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
 drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.c b/drivers/staging/media/sunxi/cedrus/cedrus.c
index a43d5ff66716..c95d011c0817 100644
--- a/drivers/staging/media/sunxi/cedrus/cedrus.c
+++ b/drivers/staging/media/sunxi/cedrus/cedrus.c
@@ -546,7 +546,7 @@ static int cedrus_probe(struct platform_device *pdev)
 static int cedrus_remove(struct platform_device *pdev)
 {
 	struct cedrus_dev *dev = platform_get_drvdata(pdev);
-
+	cancel_delayed_work(&dev->watchdog_work);
 	if (media_devnode_is_registered(dev->mdev.devnode)) {
 		media_device_unregister(&dev->mdev);
 		v4l2_m2m_unregister_media_controller(dev->m2m_dev);
-- 
2.25.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* Re: [PATCH] media: cedrus: fix use after free bug in cedrus_remove due to race condition
  2023-09-15 11:40 Ma Ke
@ 2023-09-15 12:33 ` Dan Carpenter
  0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2023-09-15 12:33 UTC (permalink / raw)
  To: Ma Ke
  Cc: mripard, paul.kocialkowski, mchehab, gregkh, wens,
	jernej.skrabec, samuel, linux-media, linux-staging,
	linux-arm-kernel, linux-sunxi

No, the code is okay.  The works is created in probe() and canceled in
remove().  You've added a new cancel in release() which is the wrong
place.  probe() pairs with remove().  open() pairs with release().

regards,
dan carpenter


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

* [PATCH] media: cedrus: fix use after free bug in cedrus_remove due to race condition
@ 2023-09-15 11:40 Ma Ke
  2023-09-15 12:33 ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Ma Ke @ 2023-09-15 11:40 UTC (permalink / raw)
  To: mripard, paul.kocialkowski, mchehab, gregkh, wens,
	jernej.skrabec, samuel
  Cc: linux-media, linux-staging, linux-arm-kernel, linux-sunxi, Ma Ke

In cedrus_probe, dev->watchdog_work is bound with cedrus_watchdog. Then
cedrus_device_run may be called to start the work.

If we close the file or remove the module which will call cedrus_release and
cedrus_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 cedrus_release, and use ctx after freeing it.

Fix it by canceling the work before cleanup in cedrus_release.

Signed-off-by: Ma Ke <make_ruc2021@163.com>
---
 drivers/staging/media/sunxi/cedrus/cedrus.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.c b/drivers/staging/media/sunxi/cedrus/cedrus.c
index 8e248d4a0aec..0a2cb615d717 100644
--- a/drivers/staging/media/sunxi/cedrus/cedrus.c
+++ b/drivers/staging/media/sunxi/cedrus/cedrus.c
@@ -404,6 +404,8 @@ static int cedrus_release(struct file *file)
 	struct cedrus_ctx *ctx = container_of(file->private_data,
 					      struct cedrus_ctx, fh);
 
+	cancel_delayed_work_sync(&ctx->dev->watchdog_work);
+
 	mutex_lock(&dev->dev_mutex);
 
 	v4l2_fh_del(&ctx->fh);
-- 
2.37.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

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

end of thread, other threads:[~2023-09-15 12:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08  3:23 [PATCH] media: cedrus: fix use after free bug in cedrus_remove due to race condition Zheng Wang
2023-09-15 11:40 Ma Ke
2023-09-15 12:33 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).