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 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC
Date: Sat, 21 Nov 2026 15:02:52 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0218-v8-da37c8a6b33c9d2d8efc-2@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0218-v8-da37c8a6b33c9d2d8efc-0@gmail.com>

RFCOMM_REUSE_DLC changes the DLC owner and callbacks to the TTY before
marking the old socket closed, without waiting for that socket's lock.
RFCOMM_CONNINFO can have passed its state check before this handoff:

  Socket getter                     TTY handoff / RFCOMM worker
  lock socket; check CONNECTED
                                    replace owner and callbacks
                                    close and free the session
  follow dlc->session->sock

The replacement TTY callback does not serialize with the old socket, so
the getter's socket lock no longer protects session teardown. Close the
old socket under its lock before replacing the callbacks. Existing
getters finish first and later getters reject the closed socket.

The excerpt below retains the headline and stack traces.

KASAN report as below:

    KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]

    RIP: 0010:rfcomm_sock_getsockopt+0x60a/0x950

    Call Trace:
     <TASK>
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_rfcomm_sock_getsockopt+0x10/0x10
     ? __might_fault+0x135/0x190
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? free_zapped_rcu+0xd0/0x1e0
     ? __might_fault+0xdd/0x190
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __might_fault+0x14e/0x190
     do_sock_getsockopt+0x342/0xa20
     ? __pfx_do_sock_getsockopt+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? _copy_to_user+0x7b/0x90
     ? srso_alias_return_thunk+0x5/0xfbef5
     __sys_getsockopt+0x113/0x1a0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __x64_sys_getsockopt+0xc2/0x160
     ? srso_alias_return_thunk+0x5/0xfbef5
     __x64_sys_getsockopt+0xc2/0x160
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? trace_hardirqs_on+0x18/0x160
     do_syscall_64+0x115/0x6a0
     entry_SYSCALL_64_after_hwframe+0x77/0x7f
    RIP: 0033:0x7ff8b2e9007a

     </TASK>

    RIP: 0010:rfcomm_sock_getsockopt+0x60a/0x950

Fixes: 08d1914293da ("Bluetooth: RFCOMM: FIX possible deadlock in rfcomm_sk_state_change")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).

 net/bluetooth/rfcomm/tty.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 198c7dd1a95b4..676fade1ee8dd 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -286,6 +286,13 @@ static struct rfcomm_dev *__rfcomm_dev_add(struct rfcomm_dev_req *req,
 			skb_queue_tail(&dev->pending, skb);
 			atomic_sub(skb->len, &sk->sk_rmem_alloc);
 		}
+
+		/* Finish RFCOMM_CONNINFO before replacing the callback
+		 * which serializes session teardown with the socket.
+		 */
+		lock_sock(sk);
+		sk->sk_state = BT_CLOSED;
+		release_sock(sk);
 	}
 
 	dlc->data_ready   = rfcomm_dev_data_ready;
@@ -389,7 +396,6 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
 {
 	struct rfcomm_dev_req req;
 	struct rfcomm_dlc *dlc;
-	int id;
 
 	if (copy_from_user(&req, arg, sizeof(req)))
 		return -EFAULT;
@@ -418,17 +424,7 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
 			return -ENOMEM;
 	}
 
-	id = rfcomm_dev_add(&req, dlc);
-	if (id < 0)
-		return id;
-
-	if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
-		/* DLC is now used by device.
-		 * Socket must be disconnected */
-		sk->sk_state = BT_CLOSED;
-	}
-
-	return id;
+	return rfcomm_dev_add(&req, dlc);
 }
 
 static int __rfcomm_release_dev(void __user *arg)

      parent 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 ` [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink Cen Zhang
2026-11-21  7:02 ` Cen Zhang [this message]

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-2@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.