public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] char: pcmcia: scr24x_cs: Fix use-after-free in scr24x_fops
@ 2022-09-16  5:00 Hyunwoo Kim
  2022-09-16  6:59 ` Arnd Bergmann
  0 siblings, 1 reply; 3+ messages in thread
From: Hyunwoo Kim @ 2022-09-16  5:00 UTC (permalink / raw)
  To: lkundrak, arnd, gregkh; +Cc: linux-kernel, imv4bel

A race condition may occur if the user physically removes the
pcmcia device while calling open() for this char device node.

This is a race condition between the scr24x_open() function and
the scr24x_remove() function, which may eventually result in UAF.

So, add a mutex to the scr24x_open() and scr24x_remove() functions
to avoid race contidion of krefs.

Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
---
 drivers/char/pcmcia/scr24x_cs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/char/pcmcia/scr24x_cs.c b/drivers/char/pcmcia/scr24x_cs.c
index 1bdce08fae3d..319e8d79048e 100644
--- a/drivers/char/pcmcia/scr24x_cs.c
+++ b/drivers/char/pcmcia/scr24x_cs.c
@@ -76,8 +76,10 @@ static int scr24x_open(struct inode *inode, struct file *filp)
 	struct scr24x_dev *dev = container_of(inode->i_cdev,
 				struct scr24x_dev, c_dev);
 
+	mutex_lock(&dev->lock);
 	kref_get(&dev->refcnt);
 	filp->private_data = dev;
+	mutex_unlock(&dev->lock);
 
 	return stream_open(inode, filp);
 }
@@ -298,9 +300,10 @@ static void scr24x_remove(struct pcmcia_device *link)
 	cdev_del(&dev->c_dev);
 	clear_bit(dev->devno, scr24x_minors);
 	dev->dev = NULL;
-	mutex_unlock(&dev->lock);
 
 	kref_put(&dev->refcnt, scr24x_delete);
+
+	mutex_unlock(&dev->lock);
 }
 
 static const struct pcmcia_device_id scr24x_ids[] = {
-- 
2.25.1


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

end of thread, other threads:[~2022-09-16  8:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-16  5:00 [PATCH] char: pcmcia: scr24x_cs: Fix use-after-free in scr24x_fops Hyunwoo Kim
2022-09-16  6:59 ` Arnd Bergmann
2022-09-16  8:11   ` Hyunwoo Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox