mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changyul Lee <lcy8047@gmail.com>
To: Alexander Usyskin <alexander.usyskin@intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, Ma Ke <make24@iscas.ac.cn>,
	linux-kernel@vger.kernel.org, Changyul Lee <lcy8047@gmail.com>
Subject: [PATCH] mei: do not let mei_open() find a released mei_device
Date: Mon, 16 Nov 2026 22:26:34 -0500	[thread overview]
Message-ID: <20261117032634.1740740-1-lcy8047@gmail.com> (raw)

mei_open() looks up the mei_device in mei_idr without mei_minor_lock or
RCU and only then takes a reference with get_device(). Two things can
go wrong.

On a preemptible kernel the walk can read an IDR node that idr_remove()
for another device has already freed through RCU.

The device can also be freed before the reference is taken. An open()
of an earlier device's character device can be delayed between
chrdev_open() and mei_open() while that device is removed and its minor
is handed to a new device. If mei_register() of the new device fails,
put_device() frees it while mei_idr still points at it. mei_deregister()
has the same window, because device_destroy() runs before
mei_minor_free().

Do the lookup and get_device() under mei_minor_lock, and stop mei_idr
from pointing at the device before its last reference can be dropped.
mei_register() frees the minor before put_device(), and mei_deregister()
clears the entry with idr_replace() before device_destroy(), which keeps
the minor reserved until the old device is gone.

Found with a static analysis tool we are developing. Compile-tested only,
with x86_64_defconfig plus INTEL_MEI, INTEL_MEI_ME and W=1.

Fixes: 7704e6be4ed2 ("mei: hook mei_device on class device")
Fixes: a6dab2f61d23 ("mei: Fix error handling in mei_register")
Signed-off-by: Changyul Lee <lcy8047@gmail.com>
---
 drivers/misc/mei/main.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/mei/main.c b/drivers/misc/mei/main.c
index 4fbf0b323616..5a8570e54801 100644
--- a/drivers/misc/mei/main.c
+++ b/drivers/misc/mei/main.c
@@ -53,10 +53,13 @@ static int mei_open(struct inode *inode, struct file *file)
 
 	int err;
 
+	mutex_lock(&mei_minor_lock);
 	dev = idr_find(&mei_idr, iminor(inode));
+	if (dev)
+		get_device(&dev->dev);
+	mutex_unlock(&mei_minor_lock);
 	if (!dev)
 		return -ENODEV;
-	get_device(&dev->dev);
 
 	mutex_lock(&dev->device_lock);
 
@@ -1324,8 +1327,8 @@ int mei_register(struct mei_device *dev, struct device *parent)
 err_del_cdev:
 	cdev_del(dev->cdev);
 err:
-	put_device(&dev->dev);
 	mei_minor_free(minor);
+	put_device(&dev->dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(mei_register);
@@ -1340,6 +1343,11 @@ void mei_deregister(struct mei_device *dev)
 
 	mei_dbgfs_deregister(dev);
 
+	/* Keep the minor reserved, but stop mei_open() from finding dev. */
+	mutex_lock(&mei_minor_lock);
+	idr_replace(&mei_idr, NULL, minor);
+	mutex_unlock(&mei_minor_lock);
+
 	device_destroy(&mei_class, devno);
 
 	mei_minor_free(minor);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.43.0


                 reply	other threads:[~2026-11-17  3:26 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=20261117032634.1740740-1-lcy8047@gmail.com \
    --to=lcy8047@gmail.com \
    --cc=alexander.usyskin@intel.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=make24@iscas.ac.cn \
    /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

all inboxes | Powered by JetHome®