[PATCH] Bluetooth: RFCOMM: fix NULL dereference of dlc->session in RFCOMM_CONNINFO
From: Hui Peng
Date: Sat Sep 19 2026 - 05:11:27 EST
The RFCOMM_CONNINFO getsockopt handler accepts a socket that is not
connected as long as deferred setup is enabled:
if (sk->sk_state != BT_CONNECTED &&
!rfcomm_pi(sk)->dlc->defer_setup) {
err = -ENOTCONN;
break;
}
l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
BT_DEFER_SETUP can be set on a listening socket, and it is stored in
rfcomm_pi(sk)->dlc->defer_setup while dlc->session is still NULL. A
local user can therefore create an RFCOMM socket, call listen(), set
BT_DEFER_SETUP, and then call getsockopt(SOL_RFCOMM, RFCOMM_CONNINFO)
to skip the -ENOTCONN path and dereference the NULL session:
Oops: general protection fault, probably for non-canonical address
0xdffffc0000000002
KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]
CPU: 1 UID: 0 PID: 150 Comm: init Tainted: G B 7.3.0-rc3-g5dd1818b15d9 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996)
RIP: 0010:rfcomm_sock_getsockopt+0x529/0x780
Call Trace:
<TASK>
do_sock_getsockopt+0x3ad/0x7d0
__sys_getsockopt+0x10e/0x1b0
__x64_sys_getsockopt+0xc2/0x160
do_syscall_64+0xda/0x4b0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
</TASK>
Deferred setup only leaves a socket usable here once it has reached
BT_CONNECT2, so restrict the exception to that state and check that a
session is actually present before following it.
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@xxxxxxxxx>
---
No Fixes: tag: I could not pin the change that introduced this with
confidence, so I left it out rather than guess.
Reproduced on Linux 7.3.0-rc3 (5dd1818b15d9) with KASAN under QEMU with
a virtual HCI device:
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
bind(s, ...); listen(s, 1);
setsockopt(s, SOL_BLUETOOTH, BT_DEFER_SETUP, &one, 4);
getsockopt(s, SOL_RFCOMM, RFCOMM_CONNINFO, &ci, &len); /* boom */
With this patch the getsockopt() returns -ENOTCONN instead.
net/bluetooth/rfcomm/sock.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -786,8 +786,10 @@ static int rfcomm_sock_getsockopt_old(st
break;
case RFCOMM_CONNINFO:
- if (sk->sk_state != BT_CONNECTED &&
- !rfcomm_pi(sk)->dlc->defer_setup) {
+ if ((sk->sk_state != BT_CONNECTED &&
+ !(sk->sk_state == BT_CONNECT2 &&
+ rfcomm_pi(sk)->dlc->defer_setup)) ||
+ !rfcomm_pi(sk)->dlc->session) {
err = -ENOTCONN;
break;
}
--
2.43.0