linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Leicheng Xie via B4 Relay <devnull+leicheng.dev.gmail.com@kernel.org>
To: Marcel Holtmann <marcel@holtmann.org>,
	 Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	 Gianluca Anzolin <gianluca@sottospazio.it>
Cc: linux-bluetooth@vger.kernel.org,
	 syzbot+11542ff1d167fdffa1fc@syzkaller.appspotmail.com,
	 stable@vger.kernel.org, Leicheng Xie <leicheng.dev@gmail.com>
Subject: [PATCH] Bluetooth: RFCOMM: Fix slab-use-after-free in rfcomm_dev_add()
Date: Sun, 22 Nov 2026 17:33:29 +0800	[thread overview]
Message-ID: <20261122-fix-rfcomm-dev-add-uaf-v1-1-cac5354752ed@gmail.com> (raw)

From: Leicheng Xie <leicheng.dev@gmail.com>

__rfcomm_dev_add() inserts the newly allocated struct rfcomm_dev into
rfcomm_dev_list and releases rfcomm_dev_lock while holding only the
initial tty_port reference. Back in rfcomm_dev_add(), the device is
registered with tty_port_register_device() and further initialized
(dev->tty_dev assignment, reparenting, drvdata, sysfs attributes, and
reading dev->id).

When RFCOMM_RELEASE_ONHUP is set, a concurrent task can open and close
the RFCOMM TTY device as soon as it becomes visible on rfcomm_dev_list.
rfcomm_tty_install() takes over the initial reference by setting
RFCOMM_TTY_OWNED, and rfcomm_tty_cleanup() drops it to zero, freeing
dev via rfcomm_dev_destruct() while rfcomm_dev_add() is still running:

  BUG: KASAN: slab-use-after-free in rfcomm_dev_add
  Write of size 8 at addr ffff88800e85b3e0 by task rfcomm_uaf_poc/179

Hold an extra tty_port reference in __rfcomm_dev_add() before dropping
rfcomm_dev_lock, and release it at the end of rfcomm_dev_add() after
caching dev->id. On the tty_port_register_device() failure path, use
test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status) before dropping the
initial reference to match __rfcomm_release_dev() and prevent a leak or
double-put if a concurrent open took ownership.

Fixes: 5b899241874d ("Bluetooth: Release RFCOMM port when the last user closes the TTY")
Reported-by: syzbot+11542ff1d167fdffa1fc@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=11542ff1d167fdffa1fc
Cc: stable@vger.kernel.org
Signed-off-by: Leicheng Xie <leicheng.dev@gmail.com>
---
 net/bluetooth/rfcomm/tty.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 198c7dd1a95b..3a61c45b5b51 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -303,6 +303,9 @@ static struct rfcomm_dev *__rfcomm_dev_add(struct rfcomm_dev_req *req,
 	   holds reference to this module. */
 	__module_get(THIS_MODULE);
 
+	/* Hold an extra reference for rfcomm_dev_add() until setup completes */
+	tty_port_get(&dev->port);
+
 	mutex_unlock(&rfcomm_dev_lock);
 	return dev;
 
@@ -316,6 +319,7 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
 {
 	struct rfcomm_dev *dev;
 	struct device *tty;
+	int id;
 
 	BT_DBG("id %d channel %d", req->dev_id, req->channel);
 
@@ -328,6 +332,8 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
 	tty = tty_port_register_device(&dev->port, rfcomm_tty_driver,
 			dev->id, NULL);
 	if (IS_ERR(tty)) {
+		if (!test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status))
+			tty_port_put(&dev->port);
 		tty_port_put(&dev->port);
 		return PTR_ERR(tty);
 	}
@@ -342,7 +348,10 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
 	if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
 		BT_ERR("Failed to create channel attribute");
 
-	return dev->id;
+	id = dev->id;
+	tty_port_put(&dev->port);
+
+	return id;
 }
 
 /* ---- Send buffer ---- */

---
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
change-id: 20261122-fix-rfcomm-dev-add-uaf-cfe483717195

Best regards,
-- 
Leicheng Xie <leicheng.dev@gmail.com>



             reply	other threads:[~2026-11-22  9:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  9:33 Leicheng Xie via B4 Relay [this message]
2026-11-22 17:24 ` Bluetooth: RFCOMM: Fix slab-use-after-free in rfcomm_dev_add() bluez.test.bot

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=20261122-fix-rfcomm-dev-add-uaf-v1-1-cac5354752ed@gmail.com \
    --to=devnull+leicheng.dev.gmail.com@kernel.org \
    --cc=gianluca@sottospazio.it \
    --cc=leicheng.dev@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+11542ff1d167fdffa1fc@syzkaller.appspotmail.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;
as well as URLs for NNTP newsgroup(s).