Linux Media Controller development
 help / color / mirror / Atom feed
From: Haotian Zhang <vulab@iscas.ac.cn>
To: sakari.ailus@linux.intel.com, laurent.pinchart@ideasonboard.com,
	mchehab@kernel.org
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	Haotian Zhang <vulab@iscas.ac.cn>
Subject: [PATCH] media: mc: Fix potential media_device lifetime race
Date: Mon, 17 Aug 2026 15:51:19 +0800	[thread overview]
Message-ID: <20260817075119.2106-1-vulab@iscas.ac.cn> (raw)

__media_device_get() traverses the global media_device_list under
media_device_lock and takes a reference on a matching instance with an
unconditional kref_get(). media_device_delete() drops the last
reference with kref_put() outside the lock, so the refcount can reach
zero while the instance is still on the list; if a concurrent
allocate() attempts to acquire a reference from such an instance,
kref_get() operates on a zero refcount while release() is already in
progress. The allocator may then return an instance that release()
subsequently frees, leading to a use-after-free.

Use kref_get_unless_zero() in __media_device_get() when acquiring the
reference. If the reference count has already dropped to zero, release
is in progress, so skip the entry and allocate a fresh instance
instead.

Fixes: 6e1d824e7a1d ("media: Media Device Allocator API")
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
 drivers/media/mc/mc-dev-allocator.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/media/mc/mc-dev-allocator.c b/drivers/media/mc/mc-dev-allocator.c
index ae17887dec59..8b97372adc83 100644
--- a/drivers/media/mc/mc-dev-allocator.c
+++ b/drivers/media/mc/mc-dev-allocator.c
@@ -68,7 +68,8 @@ static struct media_device *__media_device_get(struct device *dev,
 		if (mdi->mdev.dev != dev)
 			continue;
 
-		kref_get(&mdi->refcount);
+		if (!kref_get_unless_zero(&mdi->refcount))
+			continue;
 
 		/* get module reference for the media_device owner */
 		if (owner != mdi->owner && !try_module_get(mdi->owner))
-- 
2.43.0


                 reply	other threads:[~2026-08-17  7:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260817075119.2106-1-vulab@iscas.ac.cn \
    --to=vulab@iscas.ac.cn \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    /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