linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yoochan Lee <yoochan1026@gmail.com>
To: davem@davemloft.net
Cc: sparclinux@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yoochan Lee <yoochan1026@gmail.com>
Subject: [PATCH] drivers: tty: vcc: Fix use-after-free in vcc_open()
Date: Mon,  2 Jan 2023 10:05:28 +0900	[thread overview]
Message-ID: <20230102010528.2868403-1-yoochan1026@gmail.com> (raw)

This bug assumes that the hacker can physically access the
target computer.

A race condition may occur if the user physically removes the
vcc device while calling open().

This is a race condition between vcc_open() function and
the vcc_remove() function, which may lead to Use-After-Free.

Therefore, add a refcount check to vcc_remove() function
to free the "vcc_port" structure after the device is close()d.

---------------CPU 0--------------------CPU 1-----------------
            vcc_open()        |        vcc_remove()
--------------------------------------------------------------
    port = vcc_get_ne(tty->   |
      index); — (1)           |
                              |   struct vcc_port *port =
                              |    dev_get_drvdata(&vdev->dev);
                              |   ...
                              |   kfree(port); — (2)
 vccdbgl(port->vio.lp); — (3) |

This type of race condition is similar with CVE-2022-44032,
CVE-2022-44033, and CVE-2022-44034.
---
 drivers/tty/vcc.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/drivers/tty/vcc.c b/drivers/tty/vcc.c
index 34ba6e54789a..31f274c4aa25 100644
--- a/drivers/tty/vcc.c
+++ b/drivers/tty/vcc.c
@@ -41,6 +41,8 @@ struct vcc_port {
 
 	struct timer_list rx_timer;
 	struct timer_list tx_timer;
+	struct vio_dev *vdev;
+	struct kref refcnt;
 };
 
 /* Microseconds that thread will delay waiting for a vcc port ref */
@@ -104,6 +106,7 @@ static const struct ktermios vcc_tty_termios = {
 	.c_ospeed = 38400
 };
 
+static void vcc_delete(struct kref *kref);
 /**
  * vcc_table_add() - Add VCC port to the VCC table
  * @port: pointer to the VCC port
@@ -586,6 +589,8 @@ static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
 		goto free_port;
 
 	port->vio.debug = vcc_dbg_vio;
+	port->vdev = vdev;
+	kref_init(&port->refcnt);
 	vcc_ldc_cfg.debug = vcc_dbg_ldc;
 
 	rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
@@ -673,6 +678,14 @@ static void vcc_remove(struct vio_dev *vdev)
 {
 	struct vcc_port *port = dev_get_drvdata(&vdev->dev);
 
+	kref_put(&port->refcnt, vcc_delete);
+}
+
+static void vcc_delete(struct kref *kref)
+{
+	struct vcc_port *port = container_of(kref, struct vcc_port, refcnt);
+	struct viod_dev *vdev = port->vdev;
+
 	del_timer_sync(&port->rx_timer);
 	del_timer_sync(&port->tx_timer);
 
@@ -752,12 +765,15 @@ static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
 		pr_err("VCC: open: TTY ops not defined\n");
 		return -ENXIO;
 	}
+	kref_get(&port->refcnt);
 
 	return tty_port_open(tty->port, tty, vcc_file);
 }
 
 static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
 {
+	struct vcc_port *port = vcc_get_ne(tty->index);
+
 	if (unlikely(tty->count > 1))
 		return;
 
@@ -767,6 +783,8 @@ static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
 	}
 
 	tty_port_close(tty->port, tty, vcc_file);
+
+	kref_put(&port->refcnt, vcc_delete);
 }
 
 static void vcc_ldc_hup(struct vcc_port *port)
-- 
2.39.0


                 reply	other threads:[~2023-01-02  1:05 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=20230102010528.2868403-1-yoochan1026@gmail.com \
    --to=yoochan1026@gmail.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sparclinux@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).