Bluetooth: L2CAP: Validate responses and serialize security changes
Subsystems (2)
cover Bluetooth: L2CAP: Validate responses and serialize security changes
Channel operations must refer to the intended live channel state. Match Configuration Responses to the outstanding local request before applying their options, and hold the connection serializer while a socket security change uses its fixed SMP child.
Cen Zhang (2):
Bluetooth: L2CAP: Match configuration responses to channel requests
Bluetooth: L2CAP: Serialize BT_SECURITY with connection teardown
net/bluetooth/l2cap_core.c | 50 +++++++++--- net/bluetooth/l2cap_sock.c | 158 +++++++++++++++++++++---------------- 2 files changed, 126 insertions(+), 82 deletions(-)
---
PATCH 1/2 Bluetooth: L2CAP: Match configuration responses to channel requests
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---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/l2cap_core.c | 50 +++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-)
net/bluetooth/l2cap_core.c +38 -12
@@ -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);
PATCH 2/2 Bluetooth: L2CAP: Serialize BT_SECURITY with connection teardown
In reply to Cen ZhangThe connected socket BT_SECURITY path calls smp_conn_security() while holding only its socket and channel locks. The callee requires exclusion from l2cap_conn_del(), but the fixed SMP child can be torn down before teardown reaches the later socket channel:
BT_SECURITY syscall HCI disconnect
hold socket and channel locks
smp_conn_security()
chan = conn->smp
delete SMP child wait for the socket channel
l2cap_chan_lock(chan)
Use l2cap_chan_lock_conn() for BT_SECURITY and its matching unlock helper on exit. This takes the existing connection mutex before the channel and socket locks, excluding SMP child teardown across the security call.
The excerpt below retains the headline and stack traces.
The receive path uses the same helper in
commit 5beb86f98c3d4 ("Bluetooth: L2CAP: return LE credits from recvmsg under the conn lock"). The nested connection, channel and socket lock order followsl2cap_sock_defer().
KASAN report as below:
BUG: KASAN: slab-use-after-free in smp_conn_security+0x268/0x960
Read of size 4 at addr ffff888110c2700c by task pmbd0211-client/530
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? smp_conn_security+0x268/0x960
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? smp_conn_security+0x268/0x960
kasan_report+0xe0/0x110
? smp_conn_security+0x268/0x960
kasan_check_range+0x105/0x1b0
smp_conn_security+0x268/0x960
? find_held_lock+0x2b/0x80
? __pfx_smp_conn_security+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lock_release+0xc8/0x280
? __might_fault+0xdd/0x190
l2cap_chan_check_security+0x16a/0x2b0
l2cap_sock_setsockopt+0xc88/0x2270
? __pfx_sock_has_perm+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_l2cap_sock_setsockopt+0x10/0x10
? free_zapped_rcu+0xd0/0x1e0
? srso_alias_return_thunk+0x5/0xfbef5
? selinux_socket_setsockopt+0x66/0x80
? __pfx_l2cap_sock_setsockopt+0x10/0x10
do_sock_setsockopt+0x1fb/0x430
? __pfx_do_sock_setsockopt+0x10/0x10
__sys_setsockopt+0x106/0x180
__x64_sys_setsockopt+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:0x7fdfe600a2ba
</TASK>
Allocated by task 514:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_cache_noprof+0x251/0x610
l2cap_chan_create+0x47/0x930
l2cap_new_connection+0x1f/0x280
l2cap_connect_cfm+0x46c/0xe60
hci_le_remote_feat_complete_evt+0x417/0x680
hci_le_meta_evt+0x30c/0x5c0
hci_event_packet+0x4e8/0xc70
hci_rx_work+0x367/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
Freed by task 514:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
l2cap_chan_put+0x273/0x3a0
l2cap_conn_del+0x41f/0x860
l2cap_disconn_cfm+0x87/0xd0
hci_disconn_complete_evt+0x30f/0x990
hci_event_packet+0x894/0xc70
hci_rx_work+0x367/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/l2cap_sock.c | 158 +++++++++++++++++++++---------------- 1 file changed, 88 insertions(+), 70 deletions(-)
net/bluetooth/l2cap_sock.c +88 -70
@@ -929,12 +929,96 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode)return 0 ; } +static int l2cap_sock_set_security (struct sock *sk,sockptr_t optval, +unsigned int optlen) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; +struct l2cap_conn *conn; +struct bt_security sec; +bool have_conn; +int err =0 ; + + have_conn =l2cap_chan_lock_conn (chan); +lock_sock (sk); + +if (chan->chan_type !=L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type !=L2CAP_CHAN_FIXED && + chan->chan_type !=L2CAP_CHAN_RAW ) { + err = -EINVAL ; + goto unlock; + } + +/* mic_len was added at the end of struct bt_security, so +* accept the shorter struct as well, leaving mic_len unset. +*/ + if (optlen < offsetofend(struct bt_security, key_size)) { + err = -EINVAL ; + goto unlock; + } + + err =copy_struct_from_sockptr (&sec,sizeof (sec), optval, + optlen); +if (err) + goto unlock; + +if (sec.level <BT_SECURITY_LOW || + sec.level >BT_SECURITY_FIPS ) { + err = -EINVAL ; + goto unlock; + } + +/* BLUETOOTH CORE SPECIFICATION Version 7.0 | Vol 4, +* Part E, Section 7.7.8: MIC_Length 0x00 is 32 bits, +* 0x01 is 64 bits and 0x02 is 128 bits; all other +* values are reserved for future use. +*/ +if (sec.mic_len >0x02 ) { + err = -EINVAL ; + goto unlock; + } + + chan->sec_level = sec.level; + chan->mic_len = sec.mic_len; + + conn =l2cap_chan_conn (chan); +if (!conn) + goto unlock; + +/* change security for LE channels */ +if (chan->scid ==L2CAP_CID_ATT ) { +l2cap_chan_req_mic_len (chan, conn->hcon); +if (smp_conn_security (conn->hcon, sec.level)) { + err = -EINVAL ; + goto unlock; + } + +set_bit (FLAG_PENDING_SECURITY , &chan->flags); + sk->sk_state =BT_CONFIG ; + chan->state =BT_CONFIG ; + +/* or for ACL link */ + }else if ((sk->sk_state ==BT_CONNECT2 && +test_bit (BT_SK_DEFER_SETUP , &bt_sk (sk)->flags)) || + sk->sk_state ==BT_CONNECTED ) { +if (!l2cap_chan_check_security (chan, true)) +set_bit (BT_SK_SUSPEND , &bt_sk (sk)->flags); +else + sk->sk_state_change (sk); + }else { + err = -EINVAL ; + } + +unlock: +release_sock (sk); +l2cap_chan_unlock_conn (chan, have_conn); +return err; +} +static int l2cap_sock_setsockopt (struct socket *sock,int level,int optname,sockptr_t optval,unsigned int optlen) {struct sock *sk = sock->sk;struct l2cap_chan *chan =l2cap_pi (sk)->chan; -struct bt_security sec;struct bt_power pwr;struct l2cap_conn *conn;int err =0 ;
@@ -950,79 +1034,13 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,if (level !=SOL_BLUETOOTH )return -ENOPROTOOPT ; +if (optname ==BT_SECURITY ) +return l2cap_sock_set_security (sk, optval, optlen); +l2cap_chan_lock (chan);lock_sock (sk);switch (optname) { -case BT_SECURITY : -if (chan->chan_type !=L2CAP_CHAN_CONN_ORIENTED && - chan->chan_type !=L2CAP_CHAN_FIXED && - chan->chan_type !=L2CAP_CHAN_RAW ) { - err = -EINVAL ; -break ; - } - -/* mic_len was added at the end of struct bt_security, so -* accept the shorter struct as well, leaving mic_len unset. -*/ - if (optlen < offsetofend(struct bt_security, key_size)) { - err = -EINVAL ; -break ; - } - - err =copy_struct_from_sockptr (&sec,sizeof (sec), optval, - optlen); -if (err) -break ; - -if (sec.level <BT_SECURITY_LOW || - sec.level >BT_SECURITY_FIPS ) { - err = -EINVAL ; -break ; - } - -/* BLUETOOTH CORE SPECIFICATION Version 7.0 | Vol 4, -* Part E, Section 7.7.8: MIC_Length 0x00 is 32 bits, -* 0x01 is 64 bits and 0x02 is 128 bits; all other -* values are reserved for future use. -*/ -if (sec.mic_len >0x02 ) { - err = -EINVAL ; -break ; - } - - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; - - conn =l2cap_chan_conn (chan); -if (!conn) -break ; - -/* change security for LE channels */ -if (chan->scid ==L2CAP_CID_ATT ) { -l2cap_chan_req_mic_len (chan, conn->hcon); -if (smp_conn_security (conn->hcon, sec.level)) { - err = -EINVAL ; -break ; - } - -set_bit (FLAG_PENDING_SECURITY , &chan->flags); - sk->sk_state =BT_CONFIG ; - chan->state =BT_CONFIG ; - -/* or for ACL link */ - }else if ((sk->sk_state ==BT_CONNECT2 && -test_bit (BT_SK_DEFER_SETUP , &bt_sk (sk)->flags)) || - sk->sk_state ==BT_CONNECTED ) { -if (!l2cap_chan_check_security (chan, true)) -set_bit (BT_SK_SUSPEND , &bt_sk (sk)->flags); -else - sk->sk_state_change (sk); - }else { - err = -EINVAL ; - } -break ; -case BT_DEFER_SETUP :if (sk->sk_state !=BT_BOUND && sk->sk_state !=BT_LISTEN ) { err = -EINVAL ;