[PATCH 1/2] Bluetooth: L2CAP: Match configuration responses to channel requests

From: Cen Zhang

Date: Wed Nov 25 2026 - 08:59:54 EST


A peer can delay a BR/EDR Configuration Response until the channel is
deleted. If another channel keeps the ACL connection alive, a new
channel can reuse the SCID. l2cap_config_rsp() selects by SCID without
checking which request the response belongs to:

Signaling worker Local socket
send CONF_REQ(A, i)
disconnect and remove A
create B with A's SCID
send CONF_REQ(B, j)
receive delayed CONF_RSP(A, i)
SCID lookup selects B
update B's options

Record the local identifier at every Configuration Request send and
check it before applying a response. Peer request identifiers must not
overwrite that local identifier. Consume terminal responses before a
retry can publish a new identifier, while retaining it for PENDING and
continuation responses.

Reject zero MPS in RFC response options before publishing it to the
channel. A stale or malformed response must not introduce a zero divisor
to the receive-space calculation. The report below shows the delayed
zero-MPS response racing with receive-space accounting.

The excerpt below retains the headline and stack traces.

Oops report as below:

Oops: divide error: 0000 [#1] SMP KASAN NOPTI

RIP: 0010:l2cap_publish_rx_avail+0x159/0x490

Call Trace:
<TASK>
l2cap_sock_recvmsg+0x442/0xa00
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_l2cap_sock_recvmsg+0x10/0x10
sock_recvmsg+0x1c5/0x210
__sys_recvfrom+0x1d4/0x2c0
? __pfx___sys_recvfrom+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __x64_sys_poll+0x11a/0x430
? srso_alias_return_thunk+0x5/0xfbef5
? xfd_validate_state+0x24/0x150
? __pfx___x64_sys_poll+0x10/0x10
__x64_sys_recvfrom+0xe5/0x1c0
? lockdep_hardirqs_on_prepare+0xea/0x1a0
? 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:0x7fd37c082687

</TASK>

RIP: 0010:l2cap_publish_rx_avail+0x159/0x490

Fixes: 7b1c0049be3a ("Bluetooth: Read RFC conf option on a successful Conf RSP")
Signed-off-by: Cen Zhang <zzzccc427@xxxxxxxxx>
---
This patch was written with assistance from Codex (OpenAI).

net/bluetooth/l2cap_core.c | 50 +++++++++++++++++++++++++++++---------
1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 0d4f3341bd47f..069c0aabae0f1 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1739,7 +1739,8 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
}

set_bit(CONF_REQ_SENT, &chan->conf_state);
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident, L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
chan->num_conf_req++;
}
@@ -3973,6 +3974,10 @@ static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
(chan->mode == L2CAP_MODE_BASIC || chan->state == BT_CONNECTED))
return -ECONNREFUSED;

+ if ((rfc.mode == L2CAP_MODE_ERTM ||
+ rfc.mode == L2CAP_MODE_STREAMING) && !rfc.max_pdu_size)
+ return -ECONNREFUSED;
+
chan->mode = rfc.mode;

if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
@@ -4212,12 +4217,13 @@ void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
return;

- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident, L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
chan->num_conf_req++;
}

-static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
+static int l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
{
int type, olen;
unsigned long val;
@@ -4237,7 +4243,7 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);

if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
- return;
+ return 0;

while (len >= L2CAP_CONF_OPT_SIZE) {
int ret = l2cap_get_conf_opt(&rsp, rsp_end, &type, &olen, &val);
@@ -4260,6 +4266,10 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
}
}

+ if ((rfc.mode == L2CAP_MODE_ERTM ||
+ rfc.mode == L2CAP_MODE_STREAMING) && !rfc.max_pdu_size)
+ return -ECONNREFUSED;
+
switch (rfc.mode) {
case L2CAP_MODE_ERTM:
chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
@@ -4274,6 +4284,8 @@ static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
case L2CAP_MODE_STREAMING:
chan->mps = le16_to_cpu(rfc.max_pdu_size);
}
+
+ return 0;
}

static inline int l2cap_command_rej(struct l2cap_conn *conn,
@@ -4467,7 +4479,8 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
result == L2CAP_CR_SUCCESS) {
u8 buf[128];
set_bit(CONF_REQ_SENT, &chan->conf_state);
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident, L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
chan->num_conf_req++;
}
@@ -4555,7 +4568,8 @@ static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
break;

- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident, L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, req, sizeof(req)), req);
chan->num_conf_req++;
break;
@@ -4679,7 +4693,6 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
goto unlock;
}

- chan->ident = cmd->ident;
l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
if (chan->num_conf_rsp < L2CAP_CONF_MAX_CONF_RSP)
chan->num_conf_rsp++;
@@ -4715,7 +4728,8 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
if ((chan->state == BT_CONFIG || chan->state == BT_CONNECTED) &&
!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
u8 buf[64];
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident, L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
chan->num_conf_req++;
}
@@ -4767,12 +4781,22 @@ static inline int l2cap_config_rsp(struct l2cap_conn *conn,
lockdep_assert_held(&chan->conn->lock);

/* Connected channels may also receive reconfiguration responses. */
- if (chan->state != BT_CONFIG && chan->state != BT_CONNECTED)
+ if ((chan->state != BT_CONFIG && chan->state != BT_CONNECTED) ||
+ !test_bit(CONF_REQ_SENT, &chan->conf_state) ||
+ !chan->ident || chan->ident != cmd->ident)
goto done;

+ /* Consume this request before a retry publishes a new ident. */
+ if (result != L2CAP_CONF_PENDING &&
+ !(flags & L2CAP_CONF_FLAG_CONTINUATION))
+ chan->ident = 0;
+
switch (result) {
case L2CAP_CONF_SUCCESS:
- l2cap_conf_rfc_get(chan, rsp->data, len);
+ if (l2cap_conf_rfc_get(chan, rsp->data, len) < 0) {
+ l2cap_send_disconn_req(chan, ECONNRESET);
+ goto done;
+ }
clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
break;

@@ -4812,7 +4836,8 @@ static inline int l2cap_config_rsp(struct l2cap_conn *conn,
goto done;
}

- l2cap_send_cmd(conn, l2cap_get_ident(conn),
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident,
L2CAP_CONF_REQ, len, req);
chan->num_conf_req++;
if (result != L2CAP_CONF_SUCCESS)
@@ -8171,7 +8196,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
res == L2CAP_CR_SUCCESS) {
char buf[128];
set_bit(CONF_REQ_SENT, &chan->conf_state);
- l2cap_send_cmd(conn, l2cap_get_ident(conn),
+ chan->ident = l2cap_get_ident(conn);
+ l2cap_send_cmd(conn, chan->ident,
L2CAP_CONF_REQ,
l2cap_build_conf_req(chan, buf, sizeof(buf)),
buf);