Skip to contentSkip to conversation

Bluetooth: RFCOMM: Fix slab-use-after-free in rfcomm_dev_add()

PATCH Applied Leicheng Xie 3 participants 3 messages Nov 22 – Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bluetooth: RFCOMM: Fix slab-use-after-free in rfcomm_dev_add()

__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 [email protected]
---
 net/bluetooth/rfcomm/tty.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
net/bluetooth/rfcomm/tty.c +10 -1
index 198c7dd1a95b..3a61c45b5b51 100644
@@ -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,

Signature · 1 line
Leicheng Xie <[email protected]>
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1218544/

---Test result---

Test Summary:

CheckPatch                    PASS      0.57 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.24 seconds
SubjectPrefix                 PASS      0.09 seconds
BuildKernel                   PASS      23.53 seconds
CheckAllWarning               PASS      26.76 seconds
CheckSparse                   PASS      28.97 seconds
BuildKernel32                 PASS      23.55 seconds
CheckKernelLLVM               PASS      25.75 seconds
TestRunnerSetup               PASS      557.75 seconds
TestRunner_rfcomm-tester      PASS      6.73 seconds
IncrementalBuild              PASS      22.05 seconds

https://github.com/bluez/bluetooth-next/pull/1179

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Leicheng Xie
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Sun, 22 Nov 2026 17:33:29 +0800 you wrote:

From: Leicheng Xie <[email protected]>

__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).

[...]

Here is the summary with links:
- Bluetooth: RFCOMM: Fix slab-use-after-free in rfcomm_dev_add()
https://git.kernel.org/bluetooth/bluetooth-next/c/cc0b0a62cbde

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html