All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cen Zhang <zzzccc427@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju1990@gmail.com, jjzuming@gmail.com, zzzccc427@gmail.com
Subject: [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink
Date: Sat, 21 Nov 2026 15:02:51 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0218-v8-da37c8a6b33c9d2d8efc-1@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0218-v8-da37c8a6b33c9d2d8efc-0@gmail.com>

rfcomm_sk_state_change() saves the listener pointer, unlinks a closed
unaccepted child, and then notifies the listener. Once the child is no
longer on the accept queue, listener release can finish without waiting
for that callback:

  RFCOMM callback                   Listener release
  lock child socket
  save parent
  bt_accept_unlink(child)
                                    drain the empty accept queue
                                    drop the last listener reference
  parent->sk_data_ready(parent)

Take a listener reference before unlinking and release it after the
notification. Track that reference separately from SOCK_ZAPPED so a
different state does not release a reference it never acquired. Drop
the DLC lock before either socket can be destroyed.

The excerpt below retains the headline and stack traces.

Keep the socket reference across accept-queue unlink, as commit
4bd0b274054f ("Bluetooth: fix UAF in bt_accept_dequeue()") does for
the dequeued socket.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x3a4/0x3f0
    Read of size 8 at addr ffff88810bc8b188 by task krfcommd/75

    Call Trace:
     <TASK>
     dump_stack_lvl+0x93/0xd0
     print_report+0xce/0x630
     ? rfcomm_sk_state_change+0x3a4/0x3f0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __virt_addr_valid+0x20d/0x410
     ? rfcomm_sk_state_change+0x3a4/0x3f0
     kasan_report+0xe0/0x110
     ? rfcomm_sk_state_change+0x3a4/0x3f0
     rfcomm_sk_state_change+0x3a4/0x3f0
     __rfcomm_dlc_close+0x1a6/0x6d0
     rfcomm_run+0x1b7c/0x5390
     ? __pfx_rfcomm_run+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_woken_wake_function+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? lock_release+0xc8/0x280
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? trace_hardirqs_on+0x18/0x160
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __kthread_parkme+0x175/0x220
     ? __pfx_rfcomm_run+0x10/0x10
     kthread+0x34f/0x460
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_kthread+0x10/0x10
     ret_from_fork+0x659/0x940
     ? __pfx_ret_from_fork+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __switch_to+0x74f/0xf80
     ? __pfx_kthread+0x10/0x10
     ret_from_fork_asm+0x1a/0x30
     </TASK>

    Allocated by task 513:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     __kasan_kmalloc+0xaa/0xb0
     __kmalloc_noprof+0x2d7/0x770
     sk_prot_alloc+0x13e/0x260
     sk_alloc+0x37/0xb40
     bt_sock_alloc+0x40/0x3b0
     rfcomm_sock_alloc.constprop.0+0x56/0x290
     rfcomm_sock_create+0xe6/0x170
     bt_sock_create+0x171/0x330
     __sock_create+0x2c4/0x730
     __sys_socket+0x141/0x210
     __x64_sys_socket+0x77/0xc0
     do_syscall_64+0x115/0x6a0
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

    Freed by task 515:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     kasan_save_free_info+0x3b/0x60
     __kasan_slab_free+0x5f/0x80
     kfree+0x307/0x580
     __sk_destruct+0x64f/0x790
     sk_destruct+0xb3/0xd0
     __sk_free+0xdd/0x370
     sk_free+0x51/0x80
     rfcomm_sock_kill+0x108/0x310
     rfcomm_sock_release+0x122/0x1a0
     __sock_release+0xb8/0x270
     sock_close+0x21/0x30
     __fput+0x39f/0xa60
     fput_close_sync+0xff/0x200
     __x64_sys_close+0x8c/0xf0
     do_syscall_64+0x115/0x6a0
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).

 net/bluetooth/rfcomm/sock.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3ec2fb4ce5804..948f0da417bdc 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -65,7 +65,8 @@ static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
 static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	__must_hold(&d->lock)
 {
-	struct sock *sk = d->owner, *parent;
+	struct sock *sk = d->owner, *parent, *parent_ref = NULL;
+	bool kill;
 
 	if (!sk)
 		return;
@@ -82,6 +83,9 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 	parent = bt_sk(sk)->parent;
 	if (parent) {
 		if (d->state == BT_CLOSED) {
+			/* Keep the listener alive across unlink. */
+			sock_hold(parent);
+			parent_ref = parent;
 			sock_set_flag(sk, SOCK_ZAPPED);
 			bt_accept_unlink(sk);
 		}
@@ -95,11 +99,14 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
 
 	release_sock(sk);
 
-	if (parent && sock_flag(sk, SOCK_ZAPPED)) {
-		/* We have to drop DLC lock here, otherwise
-		 * rfcomm_sock_destruct() will dead lock. */
+	kill = parent && sock_flag(sk, SOCK_ZAPPED);
+	if (kill || parent_ref) {
+		/* Socket destruction takes a DLC lock. */
 		rfcomm_dlc_unlock(d);
-		rfcomm_sock_kill(sk);
+		if (kill)
+			rfcomm_sock_kill(sk);
+		if (parent_ref)
+			sock_put(parent_ref);
 		rfcomm_dlc_lock(d);
 	}
 }

  reply	other threads:[~2026-11-21  7:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  7:02 [PATCH 0/2] Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff Cen Zhang
2026-11-21  7:02 ` Cen Zhang [this message]
2026-11-21  7:02 ` [PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC Cen Zhang

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=pm-bluetooth-objects-candidate-0218-v8-da37c8a6b33c9d2d8efc-1@gmail.com \
    --to=zzzccc427@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=jjzuming@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.