* [PATCH v10 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs
@ 2026-11-13 3:06 Kyle Pepper
2026-11-13 3:06 ` [PATCH v10 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Kyle Pepper @ 2026-11-13 3:06 UTC (permalink / raw)
To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel
Do not let credit indications change fixed channels or complete pending
connection setup. Validate the channel state and mode before accounting
credits, checking overflow or invoking its resume callback.
Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by: LLM
Signed-off-by: Kyle Pepper <cochcoder@proton.me>
---
Hi Luiz,
I split the credit validation into its own patch and reworked the security
handling to use the existing HCI workqueue. There is no new workqueue or
pairing timeout. The original l2cap-tester and smp-tester now pass without
changing their expectations.
I also checked failure handling, repeated security
requests, key refresh and deferred LE/ECRED setup so fixing that bypass
doesn't leave sockets stuck.
Hopefully this brings the patch to a more usable state.
Thanks,
Kyle
v10:
- Separate malformed-credit validation from security request completion.
- Use the existing HCI workqueue and SMP/HCI timeouts.
- Keep pending upgrades gated through failure, refresh and deferred setup.
Previous v9: https://lore.kernel.org/linux-bluetooth/20261112022948.187618-1-cochcoder@proton.me/
Threading uses the Message-ID from sent mail; the archive lookup is not yet
available. Luiz's reply Message-ID was not supplied.
Validation: original l2cap-tester 111/111 and smp-tester 8/8; 27 private native
cases; GCC/Clang/ECRED-off W=1 builds; 42 modeled cases per compiler with
ASan/UBSan and 10 expected mutation failures. Guest tests use emulated
controllers and checked module identities. Physical hardware, PTS, KCSAN
and remote CI were not run. Private fixtures and traces are not included.
net/bluetooth/l2cap_core.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 09ff32394..4e8aec98e 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
l2cap_chan_lock(chan);
+ if (chan->state != BT_CONNECTED ||
+ (chan->mode != L2CAP_MODE_LE_FLOWCTL &&
+ chan->mode != L2CAP_MODE_EXT_FLOWCTL))
+ goto unlock;
+
max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
if (credits > max_credits) {
BT_ERR("LE credits overflow");
base-commit: da3cc02587c629420603df4395179ec82955c941
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v10 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-13 3:06 [PATCH v10 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper @ 2026-11-13 3:06 ` Kyle Pepper 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 14:00 ` [PATCH v11 " Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-13 3:06 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, and fail pending requests even if a later confirmation would otherwise satisfy them. Use the existing pairing and HCI timeouts without a new request deadline. Drain retained data when security completes or the requested requirement is lowered to an achieved level. Return EACCES and restore the old request when SMP starts no procedure and the requirements remain unmet. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data and reject the batch on security failure. Preserve ordinary deferred rejection behavior. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- include/net/bluetooth/l2cap.h | 7 + net/bluetooth/l2cap_core.c | 295 ++++++++++++++++++++++++++++++++-- net/bluetooth/l2cap_sock.c | 169 +++++++++++-------- net/bluetooth/smp.c | 1 + 4 files changed, 394 insertions(+), 78 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index cf5c2df68..7b95bd407 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -604,6 +604,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -644,6 +645,8 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -767,6 +770,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 4e8aec98e..3736eda47 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,7 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +480,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +711,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -2705,12 +2710,21 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + (chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2719,6 +2733,192 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !test_bit(HCI_CONN_ENCRYPT_PEND, chan->conn->hcon->flags) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) { + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } + } + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) && + l2cap_chan_security_ready(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL && + chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + if (!l2cap_chan_security_ready(chan)) { + err = -EBUSY; + goto restore; + } + } else if (!l2cap_chan_check_security(chan, true) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2791,9 +2991,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4043,7 +4241,14 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (chan->mode != L2CAP_MODE_EXT_FLOWCTL || + test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + return; + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) { + *result = -ECONNREFUSED; + return; + } + if (*result) return; switch (chan->state) { @@ -4052,6 +4257,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4062,6 +4269,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4140,10 +4349,16 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + l2cap_chan_del(chan, rsp->security_failed ? + ECONNREFUSED : ECONNRESET); + if (rsp->security_failed && chan != rsp->locked_chan) + chan->ops->close(chan); + } done: if (locked) @@ -4156,8 +4371,10 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) return; @@ -4169,6 +4386,13 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + sibling->mode == L2CAP_MODE_EXT_FLOWCTL && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &sibling->flags) && + test_bit(FLAG_SECURITY_FAILED, &sibling->flags)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4190,6 +4414,22 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + if (locked) { + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else if (accepted == chan && + !test_bit(FLAG_DEL, &chan->flags)) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5483,10 +5723,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7623,6 +7860,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -8089,6 +8328,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8096,7 +8337,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8104,6 +8348,31 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* An authentication event may precede encryption completion. */ + if (pending && !status && + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d8a7b5f4b..ad38fba6a 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,59 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + 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 done; + } + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } 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; + } +done: + release_sock(sk); + 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,78 +997,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - 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; - } + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - /* 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,6 +1312,11 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1774,7 +1807,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1810,8 +1846,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) + if (err) { sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index f8f86b8f7..84d85613c 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-13 3:06 [PATCH v10 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 3:06 ` [PATCH v10 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper @ 2026-11-13 14:00 ` Kyle Pepper 2026-11-13 17:10 ` [v11,1/2] " bluez.test.bot ` (2 more replies) 2026-11-13 14:00 ` [PATCH v11 " Kyle Pepper 2 siblings, 3 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-13 14:00 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Changes in v11: - Purge retained deferred CoC data before incomplete-configuration teardown. - Clear pending work flags on close and recheck channel state in the worker. - Return EBUSY for overlapping SMP/encryption requests, including key distribution; restore pending_sec_level when rejecting an idle request. - Complete ECRED child cleanup and propagate deferred security rejection. - Preserve ordinary accept/close behavior and socket/SMP lock ordering. Previous v10 (this series has no cover letter): https://lore.kernel.org/linux-bluetooth/20261113030556.75484-1-cochcoder@proton.me/ net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 09ff32394..4e8aec98e 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, l2cap_chan_lock(chan); + if (chan->state != BT_CONNECTED || + (chan->mode != L2CAP_MODE_LE_FLOWCTL && + chan->mode != L2CAP_MODE_EXT_FLOWCTL)) + goto unlock; + max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; if (credits > max_credits) { BT_ERR("LE credits overflow"); base-commit: da3cc02587c629420603df4395179ec82955c941 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* RE: [v11,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper @ 2026-11-13 17:10 ` bluez.test.bot 2026-11-14 6:05 ` [PATCH v12 1/2] " Kyle Pepper 2026-11-14 6:05 ` [PATCH v12 " Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: bluez.test.bot @ 2026-11-13 17:10 UTC (permalink / raw) To: linux-bluetooth, cochcoder [-- Attachment #1: Type: text/plain, Size: 1861 bytes --] This is automated email and please do not reply to this email! Dear submitter, Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series: PW Link:https://patchwork.kernel.org/series/1210580/ ---Test result--- Test Summary: CheckPatch FAIL 0.65 seconds VerifyFixes PASS 0.09 seconds VerifySignedoff PASS 0.09 seconds GitLint PASS 0.26 seconds SubjectPrefix PASS 0.08 seconds BuildKernel PASS 30.81 seconds CheckAllWarning PASS 34.66 seconds CheckSparse PASS 37.43 seconds BuildKernel32 PASS 30.52 seconds CheckKernelLLVM PASS 34.69 seconds TestRunnerSetup PASS 752.68 seconds TestRunner_l2cap-tester PASS 14.88 seconds IncrementalBuild PASS 28.59 seconds Details ############################## Test: CheckPatch - FAIL Desc: Run checkpatch.pl script Output: [v11,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs WARNING: Non-standard signature: Assisted-by: #89: Assisted-by: LLM ERROR: Unrecognized email address: 'LLM' #89: Assisted-by: LLM total: 1 errors, 1 warnings, 0 checks, 11 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. /home/runner/work/bluetooth-next/bluetooth-next/src/patch/14980058.patch has style problems, please review. NOTE: Ignored message types: UNKNOWN_COMMIT_ID NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS. https://github.com/bluez/bluetooth-next/pull/1127 --- Regards, Linux Bluetooth ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v12 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 17:10 ` [v11,1/2] " bluez.test.bot @ 2026-11-14 6:05 ` Kyle Pepper 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-14 6:05 ` [PATCH v12 " Kyle Pepper 2 siblings, 2 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-14 6:05 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Changes in v12: - Reject the deferred ECRED batch when an accepted child closes before the connection response; close all siblings instead of disconnecting early. - Patch 1 is unchanged. Previous v11 (no cover letter): https://lore.kernel.org/linux-bluetooth/4278a9caf4dc8e949092452c141a5be465ec77e1.1794550328.git.github.filter194@slmail.me/ Review: https://sashiko.dev/#/patchset/4278a9caf4dc8e949092452c141a5be465ec77e1.1794550328.git.github.filter194%40slmail.me net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 09ff32394..4e8aec98e 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, l2cap_chan_lock(chan); + if (chan->state != BT_CONNECTED || + (chan->mode != L2CAP_MODE_LE_FLOWCTL && + chan->mode != L2CAP_MODE_EXT_FLOWCTL)) + goto unlock; + max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; if (credits > max_credits) { BT_ERR("LE credits overflow"); base-commit: da3cc02587c629420603df4395179ec82955c941 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v13 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-14 6:05 ` [PATCH v12 1/2] " Kyle Pepper @ 2026-11-24 14:02 ` Kyle Pepper 2026-11-24 18:48 ` [v13,1/2] " bluez.test.bot ` (2 more replies) 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 1 sibling, 3 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-24 14:02 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Changes in v13: - Rebase on current bluetooth-next; patch 1's code is unchanged. - Keep deferred ECRED data gated until the response is queued. - Exclude outgoing authentication retries from incoming ECRED batches. - Wait for a new shared-link encryption procedure before completing security. - Preserve rejection cleanup and explicit per-channel locking. Previous v12 (no cover letter): https://lore.kernel.org/linux-bluetooth/4278a9caf4dc8e949092452c141a5be465ec77e1.1794617024.git.github.filter194@slmail.me/ AI assistance: Codex prepared and tested the supplied follow-up fixes and updated this revision's source, commit explanation and revision notes. net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 943effc86..8229bddca 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -5469,6 +5469,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, l2cap_chan_lock(chan); + if (chan->state != BT_CONNECTED || + (chan->mode != L2CAP_MODE_LE_FLOWCTL && + chan->mode != L2CAP_MODE_EXT_FLOWCTL)) + goto unlock; + max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; if (credits > max_credits) { BT_ERR("LE credits overflow"); base-commit: ffb95458a0f70dcab620eee0ecf5ef7750971b2f ^ permalink raw reply related [flat|nested] 16+ messages in thread
* RE: [v13,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper @ 2026-11-24 18:48 ` bluez.test.bot 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper 2026-11-25 23:44 ` [PATCH v14 " Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: bluez.test.bot @ 2026-11-24 18:48 UTC (permalink / raw) To: linux-bluetooth, cochcoder [-- Attachment #1: Type: text/plain, Size: 1861 bytes --] This is automated email and please do not reply to this email! Dear submitter, Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series: PW Link:https://patchwork.kernel.org/series/1220931/ ---Test result--- Test Summary: CheckPatch FAIL 0.59 seconds VerifyFixes PASS 0.09 seconds VerifySignedoff PASS 0.10 seconds GitLint PASS 0.27 seconds SubjectPrefix PASS 0.09 seconds BuildKernel PASS 24.49 seconds CheckAllWarning PASS 27.91 seconds CheckSparse PASS 29.82 seconds BuildKernel32 PASS 25.18 seconds CheckKernelLLVM PASS 24.71 seconds TestRunnerSetup PASS 572.47 seconds TestRunner_l2cap-tester PASS 15.27 seconds IncrementalBuild PASS 23.86 seconds Details ############################## Test: CheckPatch - FAIL Desc: Run checkpatch.pl script Output: [v13,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs WARNING: Non-standard signature: Assisted-by: #92: Assisted-by: LLM ERROR: Unrecognized email address: 'LLM' #92: Assisted-by: LLM total: 1 errors, 1 warnings, 0 checks, 11 lines checked NOTE: For some of the reported defects, checkpatch may be able to mechanically convert to the typical style using --fix or --fix-inplace. /home/runner/work/bluetooth-next/bluetooth-next/src/patch/15017328.patch has style problems, please review. NOTE: Ignored message types: UNKNOWN_COMMIT_ID NOTE: If any of the errors are false positives, please report them to the maintainer, see CHECKPATCH in MAINTAINERS. https://github.com/bluez/bluetooth-next/pull/1217 --- Regards, Linux Bluetooth ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v14 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-24 18:48 ` [v13,1/2] " bluez.test.bot @ 2026-11-25 23:44 ` Kyle Pepper 2026-11-26 3:40 ` [v14,1/2] " bluez.test.bot ` (2 more replies) 2026-11-25 23:44 ` [PATCH v14 " Kyle Pepper 2 siblings, 3 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-25 23:44 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback. AI assistance: Codex prepared this attribution revision and assisted with validation and commit text. Earlier revisions disclosed LLM assistance for the credit-state guard. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Changes in v14: - Replace the obsolete Assisted-by trailer with AI disclosure in each commit message, following current kernel tool-generated content guidance. - Rebase on current bluetooth-next; both patches' code is unchanged. Previous v13 (no cover letter): https://lore.kernel.org/linux-bluetooth/9d9d37680044266dbd1d6919b08564026afcd87d.1795528595.git.github.filter194@slmail.me/ V13 CI passed all 12 other jobs, including the kernel builds and l2cap-tester. CheckPatch rejected the obsolete Assisted-by: LLM trailer. Those are v13 results; v14 remote CI has not run. net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 943effc867402e232d4d4c4a93d4b7ba5d376367..8229bddca5df761dd1359b88752e3d5f6d6090cf 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -5469,6 +5469,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, l2cap_chan_lock(chan); + if (chan->state != BT_CONNECTED || + (chan->mode != L2CAP_MODE_LE_FLOWCTL && + chan->mode != L2CAP_MODE_EXT_FLOWCTL)) + goto unlock; + max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; if (credits > max_credits) { BT_ERR("LE credits overflow"); base-commit: f57f78086ab54f3b3122fbaeba2c842ba7bb6d00 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* RE: [v14,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper @ 2026-11-26 3:40 ` bluez.test.bot 2026-11-30 0:05 ` [PATCH v15 1/2] " Kyle Pepper 2026-11-30 0:06 ` [PATCH v15 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: bluez.test.bot @ 2026-11-26 3:40 UTC (permalink / raw) To: linux-bluetooth, cochcoder [-- Attachment #1: Type: text/plain, Size: 1074 bytes --] This is automated email and please do not reply to this email! Dear submitter, Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series: PW Link:https://patchwork.kernel.org/series/1222694/ ---Test result--- Test Summary: CheckPatch PASS 0.38 seconds VerifyFixes PASS 0.05 seconds VerifySignedoff PASS 0.04 seconds GitLint PASS 0.15 seconds SubjectPrefix PASS 0.04 seconds BuildKernel PASS 20.00 seconds CheckAllWarning PASS 23.08 seconds CheckSparse PASS 24.55 seconds BuildKernel32 PASS 20.45 seconds CheckKernelLLVM PASS 21.50 seconds TestRunnerSetup PASS 482.98 seconds TestRunner_l2cap-tester PASS 13.70 seconds IncrementalBuild PASS 19.11 seconds https://github.com/bluez/bluetooth-next/pull/1231 --- Regards, Linux Bluetooth ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v15 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper 2026-11-26 3:40 ` [v14,1/2] " bluez.test.bot @ 2026-11-30 0:05 ` Kyle Pepper 2026-11-30 5:03 ` [v15,1/2] " bluez.test.bot 2026-11-30 0:06 ` [PATCH v15 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper 2 siblings, 1 reply; 16+ messages in thread From: Kyle Pepper @ 2026-11-30 0:05 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback. AI assistance: Codex prepared this attribution revision and assisted with validation and commit text. Earlier revisions disclosed LLM assistance for the credit-state guard. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Local validation at mail preparation: - GCC/Clang sanitizer models: 134 expected outcomes, plus two preceding-source comparisons. - GCC/Clang/ECRED-off W=1 objects, sparse and strict CheckPatch pass. Smatch retains two inherited v14 diagnostics. - KCSAN and KASAN guest reruns use a separate workqueue prerequisite: work_static() reads work->data through atomic_long_read(). This fixes the preserved strict KCSAN report against queue_work_on(). The prerequisite is outside this two-patch Bluetooth series; the four reviewed Bluetooth files match the submitted source hashes. - KCSAN focused: 16/16; no kernel alerts. - KCSAN full: 138/142; no kernel alerts. All remaining failures match preserved v14 failures. One prior timeout now passes. - KASAN focused: 16/16; no kernel alerts. - KASAN full: 137/142; no kernel alerts. All remaining failures match preserved v14 failures. The private tester waits for old-channel failure dispatch before requiring a new request to succeed. The earlier premature-admission test is preserved. The isolated checks ran alongside a kernel package build while its exclusive trial lock excluded hardware capture; they did not acquire that lock or delay the package install handoff. Lock protection was checked throughout. The original exact-match comparison rejected the improved KCSAN case; its raw FAIL record remains preserved. Strict KCSAN, weak-memory checks, lockdep and panic-on-warning remain active. The unmodified base still has the preserved KCSAN failure. Full validation remains incomplete. No v15 remote CI or hardware/PTS result is claimed. Inherited suite failures and Smatch findings remain. Changes in v15: - Keep a security upgrade pending while SMP key distribution runs; complete or fail it when pairing ends (new l2cap_security_recheck()). - Treat a latched SMP failure as failing every pending CoC upgrade before the work item marks them. - Consume the ECONNREFUSED returned to a deferred reader so it is not reported twice. - Reject BT_RCVMTU reconfiguration with EBUSY while a deferred ECRED response is unsent. - Restore shared-link pending security and MIC requirements after the last pending upgrade, preserving achieved and surviving requirements; revisit restoration on encryption-only confirmations too. - Acquire every distinct sibling's nested lock before batch completion; only the channel already locked by the caller skips acquisition (H1). - Consume the whole rejected batch before teardown callbacks can revisit another member and send a duplicate response (H2). - Patch 1's code and commit message are unchanged; only its mail version and series revision notes change. Previous v14 (no cover letter): https://lore.kernel.org/linux-bluetooth/6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795650070.git.github.filter194@slmail.me/ Previous patch 2: https://lore.kernel.org/linux-bluetooth/fc7072a78de49b72d9034349512983d95be4ba1a.1795650070.git.github.filter194@slmail.me/ V15 remote CI has not been run or verified. Local validation and limits are recorded separately; these unsigned exports require human review and DCO certification before sending. net/bluetooth/l2cap_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 943effc867402e232d4d4c4a93d4b7ba5d376367..8229bddca5df761dd1359b88752e3d5f6d6090cf 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -5469,6 +5469,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, l2cap_chan_lock(chan); + if (chan->state != BT_CONNECTED || + (chan->mode != L2CAP_MODE_LE_FLOWCTL && + chan->mode != L2CAP_MODE_EXT_FLOWCTL)) + goto unlock; + max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; if (credits > max_credits) { BT_ERR("LE credits overflow"); base-commit: f57f78086ab54f3b3122fbaeba2c842ba7bb6d00 ^ permalink raw reply related [flat|nested] 16+ messages in thread
* RE: [v15,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 2026-11-30 0:05 ` [PATCH v15 1/2] " Kyle Pepper @ 2026-11-30 5:03 ` bluez.test.bot 0 siblings, 0 replies; 16+ messages in thread From: bluez.test.bot @ 2026-11-30 5:03 UTC (permalink / raw) To: linux-bluetooth, cochcoder [-- Attachment #1: Type: text/plain, Size: 1530 bytes --] This is automated email and please do not reply to this email! Dear submitter, Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series: PW Link:https://patchwork.kernel.org/series/1226096/ ---Test result--- Test Summary: CheckPatch PASS 0.49 seconds VerifyFixes PASS 0.06 seconds VerifySignedoff PASS 0.06 seconds GitLint FAIL 0.19 seconds SubjectPrefix PASS 0.05 seconds BuildKernel PASS 24.05 seconds CheckAllWarning PASS 27.42 seconds CheckSparse PASS 30.61 seconds BuildKernel32 PASS 24.35 seconds CheckKernelLLVM PASS 26.15 seconds TestRunnerSetup PASS 564.97 seconds TestRunner_l2cap-tester PASS 15.42 seconds IncrementalBuild PASS 23.31 seconds Details ############################## Test: GitLint - FAIL Desc: Run gitlint Output: [v15,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs 24: B1 Line exceeds max length (123>80): "- KCSAN full: 138/142; no kernel alerts. All remaining failures match preserved v14 failures. One prior timeout now passes." 26: B1 Line exceeds max length (93>80): "- KASAN full: 137/142; no kernel alerts. All remaining failures match preserved v14 failures." https://github.com/bluez/bluetooth-next/pull/1250 --- Regards, Linux Bluetooth ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v15 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper 2026-11-26 3:40 ` [v14,1/2] " bluez.test.bot 2026-11-30 0:05 ` [PATCH v15 1/2] " Kyle Pepper @ 2026-11-30 0:06 ` Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-30 0:06 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline. Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them. Reject an incoming deferred ECRED batch when an accepted child closes before the connection response, so its siblings cannot stay suspended. Keep deferred ECRED identifiers and flags until the response is queued, then release accepted children under their own channel locks. Clear those fields on rejection too. Exclude outgoing authentication retries from incoming batches even when their signaling identifiers collide. Always acquire a distinct sibling's nested channel lock before completing the batch. Only the channel already locked by the caller skips acquisition. Consume every rejected sibling's identifier and deferred flag before any teardown callback, preventing callback re-entry from answering it again. If another shared-link encryption procedure starts after a confirmation was queued, keep security pending until its confirmation arrives rather than closing a channel whose requirements may still be satisfied. Keep upgrades pending through SMP Phase 3 key distribution, even after encryption completes. Recheck pending channels after successful pairing destroys its SMP state; a pairing failure closes them with ECONNREFUSED. Treat a latched SMP failure as failing every pending upgrade before the worker marks the channels. Consume the ECONNREFUSED returned to a deferred reader so teardown's socket error does not report the same failure twice. Reject BT_RCVMTU reconfiguration with EBUSY while a deferred ECRED response is unsent, preserving its identifier and receive MTU until the batch is answered. Restore pending link requirements after upgrades finish without lowering the achieved security or a surviving channel's requirements. Revisit restoration on encryption-only confirmations after failed channels have closed, even when no channel remains pending. AI assistance: Codex prepared and tested the security-upgrade and deferred-ECRED changes in prior revisions and these supplied v15 Phase 3, latched-failure, deferred-reader and receive-MTU revisions. It also hardened sibling locking and rejected-batch consumption, repaired shared-link requirement restoration, and assisted with this commit message and revision notes. Work followed the reported failures and supplied review findings; validation uses compiler builds and source-extracted sanitizer models with mutation controls. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Local validation at mail preparation: - GCC/Clang sanitizer models: 134 expected outcomes, plus two preceding-source comparisons. - GCC/Clang/ECRED-off W=1 objects, sparse and strict CheckPatch pass. Smatch retains two inherited v14 diagnostics. - KCSAN and KASAN guest reruns use a separate workqueue prerequisite: work_static() reads work->data through atomic_long_read(). This fixes the preserved strict KCSAN report against queue_work_on(). The prerequisite is outside this two-patch Bluetooth series; the four reviewed Bluetooth files match the submitted source hashes. - KCSAN focused: 16/16; no kernel alerts. - KCSAN full: 138/142; no kernel alerts. All remaining failures match preserved v14 failures. One prior timeout now passes. - KASAN focused: 16/16; no kernel alerts. - KASAN full: 137/142; no kernel alerts. All remaining failures match preserved v14 failures. The private tester waits for old-channel failure dispatch before requiring a new request to succeed. The earlier premature-admission test is preserved. The isolated checks ran alongside a kernel package build while its exclusive trial lock excluded hardware capture; they did not acquire that lock or delay the package install handoff. Lock protection was checked throughout. The original exact-match comparison rejected the improved KCSAN case; its raw FAIL record remains preserved. Strict KCSAN, weak-memory checks, lockdep and panic-on-warning remain active. The unmodified base still has the preserved KCSAN failure. Full validation remains incomplete. No v15 remote CI or hardware/PTS result is claimed. Inherited suite failures and Smatch findings remain. Changes in v15: - Keep a security upgrade pending while SMP key distribution runs; complete or fail it when pairing ends (new l2cap_security_recheck()). - Treat a latched SMP failure as failing every pending CoC upgrade before the work item marks them. - Consume the ECONNREFUSED returned to a deferred reader so it is not reported twice. - Reject BT_RCVMTU reconfiguration with EBUSY while a deferred ECRED response is unsent. - Restore shared-link pending security and MIC requirements after the last pending upgrade, preserving achieved and surviving requirements; revisit restoration on encryption-only confirmations too. - Acquire every distinct sibling's nested lock before batch completion; only the channel already locked by the caller skips acquisition (H1). - Consume the whole rejected batch before teardown callbacks can revisit another member and send a duplicate response (H2). - Patch 1's code and commit message are unchanged; only its mail version and series revision notes change. Previous v14 (no cover letter): https://lore.kernel.org/linux-bluetooth/6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795650070.git.github.filter194@slmail.me/ Previous patch 2: https://lore.kernel.org/linux-bluetooth/fc7072a78de49b72d9034349512983d95be4ba1a.1795650070.git.github.filter194@slmail.me/ V15 remote CI has not been run or verified. Local validation and limits are recorded separately; these unsigned exports require human review and DCO certification before sending. include/net/bluetooth/l2cap.h | 14 +- net/bluetooth/l2cap_core.c | 508 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 186 ++++++++----- net/bluetooth/smp.c | 3 +- 4 files changed, 603 insertions(+), 108 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 72d9d56c6f2a2e513acd41f9d1e0d08d99d9cdf8..c21edb4260e88539188a695a402c4e68ac6f3bd9 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -609,6 +609,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -649,6 +650,12 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; + atomic_t security_recheck; + __u8 security_pending_sec_level; + __u8 security_req_mic_len; + bool security_restore; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -772,6 +779,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -979,7 +988,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); @@ -998,6 +1007,9 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); +void l2cap_security_recheck(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 8229bddca5df761dd1359b88752e3d5f6d6090cf..7aeb8d8f55e42fef1aaa2cf5eaf33a6c557cb53f 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,8 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +481,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +712,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -726,6 +732,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) hci_conn_drop(chan->conn->hcon); } + if (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + skb_queue_purge(&chan->tx_q); + return; + } + if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) return; @@ -733,11 +745,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) case L2CAP_MODE_BASIC: break; - case L2CAP_MODE_LE_FLOWCTL: - case L2CAP_MODE_EXT_FLOWCTL: - skb_queue_purge(&chan->tx_q); - break; - case L2CAP_MODE_ERTM: __clear_retrans_timer(chan); __clear_monitor_timer(chan); @@ -870,6 +877,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -877,6 +887,11 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) case BT_CONNECTED: case BT_CONFIG: + if (l2cap_chan_ecred_rsp_pending(chan)) { + l2cap_chan_ecred_connect_reject(chan); + return; + } + if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); l2cap_send_disconn_req(chan, reason); @@ -2707,12 +2722,47 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +/* True for an incoming deferred ECRED channel whose L2CAP_ECRED_CONN_RSP has + * not been queued yet. FLAG_DEFER_SETUP and ident stay set until the response + * is queued, so data cannot overtake it. + * + * Local and remote idents share one number space, and a bound socket with + * BT_DEFER_SETUP can later connect out. l2cap_ecred_conn_rsp() clears + * FLAG_ECRED_CONN_REQ_SENT and keeps ident while such a channel retries after + * an authentication error, always in BT_CONNECT, a state an incoming channel + * never has. Exclude it so it cannot match an incoming batch. + */ +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) +{ + return chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + chan->state != BT_CONNECT && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); +} + +/* A latched SMP failure applies to every pending CoC upgrade before + * l2cap_security_work() has marked FLAG_SECURITY_FAILED on the channel. + */ +static bool l2cap_chan_security_failed(struct l2cap_chan *chan) +{ + return test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + (chan->conn && test_bit(FLAG_PENDING_SECURITY, &chan->flags) && + atomic_read(&chan->conn->security_failure)); +} + +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + l2cap_chan_ecred_rsp_pending(chan)) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2721,6 +2771,275 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_busy(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + + /* HCI encryption, or SMP pairing including Phase 3 key distribution */ + return test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !l2cap_chan_security_busy(chan) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +void l2cap_security_recheck(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_recheck, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_mark_pending(struct l2cap_conn *conn, bool failed) + __must_hold(&conn->lock) +{ + struct l2cap_chan *chan; + + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + if (failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } +} + +static void l2cap_security_restore(struct l2cap_conn *conn) + __must_hold(&conn->lock) +{ + struct hci_conn *hcon = conn->hcon; + struct l2cap_chan *chan; + u8 level, mic_len; + + lockdep_assert_held(&hcon->hdev->lock); + if (!conn->security_restore || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon)) + return; + + level = max(conn->security_pending_sec_level, hcon->sec_level); + mic_len = max(conn->security_req_mic_len, hci_conn_mic_len(hcon)); + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (chan->state == BT_CONNECTED || chan->state == BT_CONNECT || + chan->state == BT_CONNECT2 || chan->state == BT_CONFIG) { + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + l2cap_chan_unlock(chan); + return; + } + level = max(level, chan->sec_level); + mic_len = max(mic_len, chan->mic_len); + } + l2cap_chan_unlock(chan); + } + + /* Drop failed requests without lowering another channel's requirements. */ + hcon->pending_sec_level = level; + hcon->req_mic_len = mic_len; + conn->security_restore = false; +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) + l2cap_security_mark_pending(conn, true); + if (atomic_xchg(&conn->security_recheck, 0)) + l2cap_security_mark_pending(conn, false); + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && + (chan->state == BT_CONNECTED || chan->state == BT_CONNECT2)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + bool failed = test_bit(FLAG_SECURITY_FAILED, &chan->flags); + + if (!failed && l2cap_chan_security_busy(chan)) { + /* Another procedure, or SMP key distribution, is still + * running. Stay pending: its HCI confirmation, + * l2cap_security_recheck() or l2cap_security_failure() + * queues this work again. + */ + } else if (!failed && + l2cap_chan_security_sufficient(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (l2cap_chan_ecred_rsp_pending(chan)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } + l2cap_security_restore(conn); +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + u8 old_pending = hcon->pending_sec_level; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + /* Do not change the requirements of a procedure already in progress. */ + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + l2cap_chan_security_busy(chan)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (!l2cap_chan_check_security(chan, true) || + l2cap_chan_security_busy(chan)) { + if (!chan->conn->security_restore) { + chan->conn->security_pending_sec_level = old_pending; + chan->conn->security_req_mic_len = old_req_mic; + chan->conn->security_restore = true; + } + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + hcon->pending_sec_level = old_pending; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2793,9 +3112,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4048,7 +4365,13 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) + return; + if (l2cap_chan_security_failed(chan)) { + *result = -ECONNREFUSED; + return; + } + if (*result < 0) return; switch (chan->state) { @@ -4057,6 +4380,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4067,6 +4392,10 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + struct l2cap_chan *rejected[L2CAP_ECRED_MAX_CID]; + int rejected_count; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4074,7 +4403,8 @@ struct l2cap_ecred_rsp_data { int count; }; -/* Lock @chan if it is not @locked_chan, and has same or lower nesting level. +/* Lock @chan unless it is @locked_chan; only that case returns false. + * @chan must have the same or lower nesting level. * * They must have the same chan->conn, and conn->lock must be held. * @@ -4093,12 +4423,10 @@ static bool l2cap_chan_try_sibling_lock(struct l2cap_chan *chan, if (chan == locked_chan) return false; - if (WARN_ON_ONCE(locked_chan->conn != chan->conn)) - return false; + WARN_ON_ONCE(locked_chan->conn != chan->conn); - if (WARN_ON_ONCE(atomic_read(&locked_chan->nesting) - < atomic_read(&chan->nesting))) - return false; + WARN_ON_ONCE(atomic_read(&locked_chan->nesting) + < atomic_read(&chan->nesting)); mutex_lock_nest_lock(&chan->lock, &locked_chan->conn->lock); return true; @@ -4111,14 +4439,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) container_of(&rsp->pdu.rsp, struct l2cap_ecred_conn_rsp, hdr); bool locked; - if (chan->mode != L2CAP_MODE_EXT_FLOWCTL) - return; - - /* Check if channel for outgoing connection or if it wasn't deferred - * since in those cases it must be skipped. + /* Skip channels for outgoing connections and channels that were not + * deferred. FLAG_DEFER_SETUP and ident are cleared only after the + * response is queued (see __l2cap_ecred_conn_rsp_defer()), so that a + * concurrent l2cap_chan_send() cannot transmit ahead of it. */ - if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || - !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) return; lockdep_assert_held(&rsp->locked_chan->lock); @@ -4137,18 +4463,28 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) lockdep_assert_held(&chan->lock); lockdep_assert_held(&chan->conn->lock); - /* Reset ident so only one response is sent */ - chan->ident = 0; - /* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ - if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) + if (WARN_ON_ONCE(rsp->count + rsp->rejected_count >= + ARRAY_SIZE(rsp->pdu.scid))) { rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); + goto done; + } /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + /* Keep data blocked after consuming the response identifier. */ + chan->state = BT_DISCONN; + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (rsp->security_failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + rsp->rejected[rsp->rejected_count++] = chan; + l2cap_chan_hold(chan); + } done: if (locked) @@ -4157,23 +4493,31 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) l2cap_chan_put(chan); } -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) - return; + return 0; if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) - return; + return 0; BT_DBG("chan %p id %d", chan, id); memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + l2cap_chan_ecred_rsp_pending(sibling) && + l2cap_chan_security_failed(sibling)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4184,7 +4528,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); if (result > 0) - return; + return 0; if (result < 0) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4192,9 +4536,55 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) /* Build response */ __l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data); + /* Consume the entire rejected batch before any teardown callback can + * revisit a sibling and send another response for the same ident. + */ + for (i = 0; i < data.rejected_count; i++) { + struct l2cap_chan *rejected = data.rejected[i]; + bool locked = l2cap_chan_try_sibling_lock(rejected, chan); + int err = data.security_failed ? ECONNREFUSED : ECONNRESET; + + if (locked) { + lockdep_assert_held(&rejected->conn->lock); + l2cap_chan_del(rejected, err); + rejected->ops->close(rejected); + l2cap_chan_unlock(rejected); + } else { + l2cap_chan_del(chan, err); + chan->ops->close(chan); + } + l2cap_chan_put(rejected); + } + l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + /* The response is queued: open the data gate and make later + * calls for this batch return early. Both are done under the + * channel lock, so no sender sees one without the other. + */ + if (locked) { + clear_bit(FLAG_DEFER_SETUP, &accepted->flags); + accepted->ident = 0; + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else { + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } + + return data.security_failed ? -ECONNREFUSED : 0; } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5488,10 +5878,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7648,6 +8035,9 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); + atomic_set(&conn->security_recheck, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -7911,6 +8301,10 @@ static void l2cap_ecred_reconfigure(struct l2cap_chan *chan) int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu) { + /* ident and FLAG_DEFER_SETUP still identify an unsent ECRED response. */ + if (l2cap_chan_ecred_rsp_pending(chan)) + return -EBUSY; + if (chan->imtu > mtu) return -EINVAL; @@ -8114,6 +8508,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8121,7 +8517,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8129,6 +8528,34 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* Authentication may precede encryption, and encryption may + * precede SMP key distribution. Stay pending; + * l2cap_security_recheck() or l2cap_security_failure() queues + * the work when pairing ends. + */ + if (pending && !status && l2cap_chan_security_busy(chan)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); @@ -8187,6 +8614,9 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) l2cap_chan_unlock(chan); } + if (conn->security_restore) + l2cap_queue_security_work(conn); + mutex_unlock(&conn->lock); /* Process data held back while the encryption key size was read */ diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d5663a4642a9c0fd1b4f262701f2addb4d3d8d03..bc39f24d4b56353b9fbd14bc27412e3de05de846 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,65 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + if (chan->scid == L2CAP_CID_ATT) { + release_sock(sk); + l2cap_chan_req_mic_len(chan, conn->hcon); + if (smp_conn_security(conn->hcon, sec->level)) + return -EINVAL; + lock_sock(sk); + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } else if ((sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || + sk->sk_state == BT_CONNECTED) { + bool ready; + + release_sock(sk); + ready = l2cap_chan_check_security(chan, true); + lock_sock(sk); + if (!ready) + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + else + sk->sk_state_change(sk); + } else { + err = -EINVAL; + } +done: + release_sock(sk); + 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,78 +1003,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - l2cap_chan_lock(chan); - lock_sock(sk); + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,11 +1318,23 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + int result; + sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; release_sock(sk); - __l2cap_ecred_conn_rsp_defer(chan); + result = __l2cap_ecred_conn_rsp_defer(chan); + if (result) { + err = result; + if (result == -ECONNREFUSED) + cmpxchg(&sk->sk_err, ECONNREFUSED, 0); + } } else if (bdaddr_type_is_le(chan->src_type)) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1778,7 +1824,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + WRITE_ONCE(sk->sk_err, err); + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1814,8 +1863,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) - sk->sk_err = err; + if (err) { + WRITE_ONCE(sk->sk_err, err); + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 62a6b1e8f7cb11eed874dbacf211fe861513f2fa..b94182e1870d363a86fb845e134794dcfbc08e80 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); @@ -1365,8 +1366,8 @@ static void smp_distribute_keys(struct smp_chan *smp) set_bit(SMP_FLAG_COMPLETE, &smp->flags); smp_notify_keys(conn); - smp_chan_destroy(conn); + l2cap_security_recheck(conn); } static void smp_timeout(struct work_struct *work) ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v14 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-24 18:48 ` [v13,1/2] " bluez.test.bot 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper @ 2026-11-25 23:44 ` Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-25 23:44 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline. Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them. Reject an incoming deferred ECRED batch when an accepted child closes before the connection response, so its siblings cannot stay suspended. Keep deferred ECRED identifiers and flags until the response is queued, then release accepted children under their own channel locks. Clear those fields on rejection too. Exclude outgoing authentication retries from incoming batches even when their signaling identifiers collide. If another shared-link encryption procedure starts after a confirmation was queued, keep security pending until its confirmation arrives rather than closing a channel whose requirements may still be satisfied. AI assistance: Codex prepared and tested the security-upgrade and deferred-ECRED changes in prior revisions, including the supplied v13 follow-up fixes, and assisted with this commit message and revision notes. Work followed the reported credit/security failures and supplied review findings; validation used compiler builds and source-extracted sanitizer models with mutation controls. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- Changes in v14: - Replace the obsolete Assisted-by trailer with AI disclosure in each commit message, following current kernel tool-generated content guidance. - Rebase on current bluetooth-next; both patches' code is unchanged. Previous v13 (no cover letter): https://lore.kernel.org/linux-bluetooth/9d9d37680044266dbd1d6919b08564026afcd87d.1795528595.git.github.filter194@slmail.me/ V13 CI passed all 12 other jobs, including the kernel builds and l2cap-tester. CheckPatch rejected the obsolete Assisted-by: LLM trailer. Those are v13 results; v14 remote CI has not run. include/net/bluetooth/l2cap.h | 9 +- net/bluetooth/l2cap_core.c | 378 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 181 ++++++++++------ net/bluetooth/smp.c | 1 + 4 files changed, 470 insertions(+), 99 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 72d9d56c6f2a2e513acd41f9d1e0d08d99d9cdf8..8519099c0a701422c2a0270c98cc35e4f67c3a6f 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -609,6 +609,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -649,6 +650,8 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -772,6 +775,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -979,7 +984,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); @@ -998,6 +1003,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 8229bddca5df761dd1359b88752e3d5f6d6090cf..29fbe6de913cde1adb667cd316b20bf5223d2bb5 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,8 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +481,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +712,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -726,6 +732,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) hci_conn_drop(chan->conn->hcon); } + if (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + skb_queue_purge(&chan->tx_q); + return; + } + if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) return; @@ -733,11 +745,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) case L2CAP_MODE_BASIC: break; - case L2CAP_MODE_LE_FLOWCTL: - case L2CAP_MODE_EXT_FLOWCTL: - skb_queue_purge(&chan->tx_q); - break; - case L2CAP_MODE_ERTM: __clear_retrans_timer(chan); __clear_monitor_timer(chan); @@ -870,6 +877,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -877,6 +887,11 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) case BT_CONNECTED: case BT_CONFIG: + if (l2cap_chan_ecred_rsp_pending(chan)) { + l2cap_chan_ecred_connect_reject(chan); + return; + } + if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); l2cap_send_disconn_req(chan, reason); @@ -2707,12 +2722,37 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +/* True for an incoming deferred ECRED channel whose L2CAP_ECRED_CONN_RSP has + * not been queued yet. FLAG_DEFER_SETUP and ident stay set until the response + * is queued, so data cannot overtake it. + * + * Local and remote idents share one number space, and a bound socket with + * BT_DEFER_SETUP can later connect out. l2cap_ecred_conn_rsp() clears + * FLAG_ECRED_CONN_REQ_SENT and keeps ident while such a channel retries after + * an authentication error, always in BT_CONNECT, a state an incoming channel + * never has. Exclude it so it cannot match an incoming batch. + */ +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) +{ + return chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + chan->state != BT_CONNECT && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); +} + +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + l2cap_chan_ecred_rsp_pending(chan)) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2721,6 +2761,205 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !test_bit(HCI_CONN_ENCRYPT_PEND, chan->conn->hcon->flags) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) { + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } + } + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && + (chan->state == BT_CONNECTED || chan->state == BT_CONNECT2)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + bool failed = test_bit(FLAG_SECURITY_FAILED, &chan->flags); + bool encrypting = test_bit(HCI_CONN_ENCRYPT_PEND, + chan->conn->hcon->flags); + + if (!failed && encrypting) { + /* Another procedure started on the shared link + * after the confirmation was queued. Stay + * pending: its confirmation sets + * FLAG_SECURITY_CFM and queues this work again. + */ + } else if (!failed && + l2cap_chan_security_sufficient(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (l2cap_chan_ecred_rsp_pending(chan)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + u8 old_pending = hcon->pending_sec_level; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + /* Do not change the requirements of a procedure already in progress. */ + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (!l2cap_chan_check_security(chan, true) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + hcon->pending_sec_level = old_pending; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2793,9 +3032,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4048,7 +4285,13 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) + return; + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) { + *result = -ECONNREFUSED; + return; + } + if (*result < 0) return; switch (chan->state) { @@ -4057,6 +4300,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4067,6 +4312,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4111,14 +4358,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) container_of(&rsp->pdu.rsp, struct l2cap_ecred_conn_rsp, hdr); bool locked; - if (chan->mode != L2CAP_MODE_EXT_FLOWCTL) - return; - - /* Check if channel for outgoing connection or if it wasn't deferred - * since in those cases it must be skipped. + /* Skip channels for outgoing connections and channels that were not + * deferred. FLAG_DEFER_SETUP and ident are cleared only after the + * response is queued (see __l2cap_ecred_conn_rsp_defer()), so that a + * concurrent l2cap_chan_send() cannot transmit ahead of it. */ - if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || - !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) return; lockdep_assert_held(&rsp->locked_chan->lock); @@ -4137,18 +4382,24 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) lockdep_assert_held(&chan->lock); lockdep_assert_held(&chan->conn->lock); - /* Reset ident so only one response is sent */ - chan->ident = 0; - /* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (rsp->security_failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + l2cap_chan_del(chan, rsp->security_failed ? + ECONNREFUSED : ECONNRESET); + chan->ops->close(chan); + } done: if (locked) @@ -4157,23 +4408,31 @@ done: l2cap_chan_put(chan); } -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) - return; + return 0; if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) - return; + return 0; BT_DBG("chan %p id %d", chan, id); memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + l2cap_chan_ecred_rsp_pending(sibling) && + test_bit(FLAG_SECURITY_FAILED, &sibling->flags)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4184,7 +4443,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); if (result > 0) - return; + return 0; if (result < 0) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4195,6 +4454,32 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + /* The response is queued: open the data gate and make later + * calls for this batch return early. Both are done under the + * channel lock, so no sender sees one without the other. + */ + if (locked) { + clear_bit(FLAG_DEFER_SETUP, &accepted->flags); + accepted->ident = 0; + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else if (accepted == chan) { + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } + + return data.security_failed ? -ECONNREFUSED : 0; } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5488,10 +5773,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7648,6 +7930,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -8114,6 +8398,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8121,7 +8407,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8129,6 +8418,31 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* An authentication event may precede encryption completion. */ + if (pending && !status && + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d5663a4642a9c0fd1b4f262701f2addb4d3d8d03..4db1e9f5d15937000177903f76ab54af1795ceab 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,65 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + if (chan->scid == L2CAP_CID_ATT) { + release_sock(sk); + l2cap_chan_req_mic_len(chan, conn->hcon); + if (smp_conn_security(conn->hcon, sec->level)) + return -EINVAL; + lock_sock(sk); + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } else if ((sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || + sk->sk_state == BT_CONNECTED) { + bool ready; + + release_sock(sk); + ready = l2cap_chan_check_security(chan, true); + lock_sock(sk); + if (!ready) + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + else + sk->sk_state_change(sk); + } else { + err = -EINVAL; + } +done: + release_sock(sk); + 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,78 +1003,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - l2cap_chan_lock(chan); - lock_sock(sk); + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,11 +1318,20 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + int result; + sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; release_sock(sk); - __l2cap_ecred_conn_rsp_defer(chan); + result = __l2cap_ecred_conn_rsp_defer(chan); + if (result) + err = result; } else if (bdaddr_type_is_le(chan->src_type)) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1778,7 +1821,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1814,8 +1860,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) + if (err) { sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 62a6b1e8f7cb11eed874dbacf211fe861513f2fa..bc54e0e65e19624c775edb94a0e8fca2c8fc5305 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v13 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-14 6:05 ` [PATCH v12 1/2] " Kyle Pepper 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper @ 2026-11-24 14:02 ` Kyle Pepper 1 sibling, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-24 14:02 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline. Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them. Reject an incoming deferred ECRED batch when an accepted child closes before the connection response, so its siblings cannot stay suspended. Keep deferred ECRED identifiers and flags until the response is queued, then release accepted children under their own channel locks. Clear those fields on rejection too. Exclude outgoing authentication retries from incoming batches even when their signaling identifiers collide. If another shared-link encryption procedure starts after a confirmation was queued, keep security pending until its confirmation arrives rather than closing a channel whose requirements may still be satisfied. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- include/net/bluetooth/l2cap.h | 9 +- net/bluetooth/l2cap_core.c | 378 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 181 ++++++++++------ net/bluetooth/smp.c | 1 + 4 files changed, 470 insertions(+), 99 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 72d9d56c6..8519099c0 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -609,6 +609,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -649,6 +650,8 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -772,6 +775,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -979,7 +984,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); @@ -998,6 +1003,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 8229bddca..29fbe6de9 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,8 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +481,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +712,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -726,6 +732,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) hci_conn_drop(chan->conn->hcon); } + if (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + skb_queue_purge(&chan->tx_q); + return; + } + if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) return; @@ -733,11 +745,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) case L2CAP_MODE_BASIC: break; - case L2CAP_MODE_LE_FLOWCTL: - case L2CAP_MODE_EXT_FLOWCTL: - skb_queue_purge(&chan->tx_q); - break; - case L2CAP_MODE_ERTM: __clear_retrans_timer(chan); __clear_monitor_timer(chan); @@ -870,6 +877,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -877,6 +887,11 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) case BT_CONNECTED: case BT_CONFIG: + if (l2cap_chan_ecred_rsp_pending(chan)) { + l2cap_chan_ecred_connect_reject(chan); + return; + } + if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); l2cap_send_disconn_req(chan, reason); @@ -2707,12 +2722,37 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +/* True for an incoming deferred ECRED channel whose L2CAP_ECRED_CONN_RSP has + * not been queued yet. FLAG_DEFER_SETUP and ident stay set until the response + * is queued, so data cannot overtake it. + * + * Local and remote idents share one number space, and a bound socket with + * BT_DEFER_SETUP can later connect out. l2cap_ecred_conn_rsp() clears + * FLAG_ECRED_CONN_REQ_SENT and keeps ident while such a channel retries after + * an authentication error, always in BT_CONNECT, a state an incoming channel + * never has. Exclude it so it cannot match an incoming batch. + */ +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) +{ + return chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + chan->state != BT_CONNECT && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags); +} + +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + l2cap_chan_ecred_rsp_pending(chan)) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2721,6 +2761,205 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !test_bit(HCI_CONN_ENCRYPT_PEND, chan->conn->hcon->flags) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) { + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } + } + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && + (chan->state == BT_CONNECTED || chan->state == BT_CONNECT2)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + bool failed = test_bit(FLAG_SECURITY_FAILED, &chan->flags); + bool encrypting = test_bit(HCI_CONN_ENCRYPT_PEND, + chan->conn->hcon->flags); + + if (!failed && encrypting) { + /* Another procedure started on the shared link + * after the confirmation was queued. Stay + * pending: its confirmation sets + * FLAG_SECURITY_CFM and queues this work again. + */ + } else if (!failed && + l2cap_chan_security_sufficient(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (l2cap_chan_ecred_rsp_pending(chan)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + u8 old_pending = hcon->pending_sec_level; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + /* Do not change the requirements of a procedure already in progress. */ + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (!l2cap_chan_check_security(chan, true) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + hcon->pending_sec_level = old_pending; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2793,9 +3032,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4048,7 +4285,13 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) + return; + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) { + *result = -ECONNREFUSED; + return; + } + if (*result < 0) return; switch (chan->state) { @@ -4057,6 +4300,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4067,6 +4312,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4111,14 +4358,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) container_of(&rsp->pdu.rsp, struct l2cap_ecred_conn_rsp, hdr); bool locked; - if (chan->mode != L2CAP_MODE_EXT_FLOWCTL) - return; - - /* Check if channel for outgoing connection or if it wasn't deferred - * since in those cases it must be skipped. + /* Skip channels for outgoing connections and channels that were not + * deferred. FLAG_DEFER_SETUP and ident are cleared only after the + * response is queued (see __l2cap_ecred_conn_rsp_defer()), so that a + * concurrent l2cap_chan_send() cannot transmit ahead of it. */ - if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || - !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + if (!l2cap_chan_ecred_rsp_pending(chan)) return; lockdep_assert_held(&rsp->locked_chan->lock); @@ -4137,18 +4382,24 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) lockdep_assert_held(&chan->lock); lockdep_assert_held(&chan->conn->lock); - /* Reset ident so only one response is sent */ - chan->ident = 0; - /* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ if (WARN_ON_ONCE(rsp->count >= ARRAY_SIZE(rsp->pdu.scid))) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (rsp->security_failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + l2cap_chan_del(chan, rsp->security_failed ? + ECONNREFUSED : ECONNRESET); + chan->ops->close(chan); + } done: if (locked) @@ -4157,23 +4408,31 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) l2cap_chan_put(chan); } -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) - return; + return 0; if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) - return; + return 0; BT_DBG("chan %p id %d", chan, id); memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + l2cap_chan_ecred_rsp_pending(sibling) && + test_bit(FLAG_SECURITY_FAILED, &sibling->flags)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4184,7 +4443,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); if (result > 0) - return; + return 0; if (result < 0) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4195,6 +4454,32 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + /* The response is queued: open the data gate and make later + * calls for this batch return early. Both are done under the + * channel lock, so no sender sees one without the other. + */ + if (locked) { + clear_bit(FLAG_DEFER_SETUP, &accepted->flags); + accepted->ident = 0; + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else if (accepted == chan) { + clear_bit(FLAG_DEFER_SETUP, &chan->flags); + chan->ident = 0; + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } + + return data.security_failed ? -ECONNREFUSED : 0; } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5488,10 +5773,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7648,6 +7930,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -8114,6 +8398,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8121,7 +8407,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8129,6 +8418,31 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* An authentication event may precede encryption completion. */ + if (pending && !status && + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d5663a464..4db1e9f5d 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,65 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + if (chan->scid == L2CAP_CID_ATT) { + release_sock(sk); + l2cap_chan_req_mic_len(chan, conn->hcon); + if (smp_conn_security(conn->hcon, sec->level)) + return -EINVAL; + lock_sock(sk); + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } else if ((sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || + sk->sk_state == BT_CONNECTED) { + bool ready; + + release_sock(sk); + ready = l2cap_chan_check_security(chan, true); + lock_sock(sk); + if (!ready) + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + else + sk->sk_state_change(sk); + } else { + err = -EINVAL; + } +done: + release_sock(sk); + 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,78 +1003,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - l2cap_chan_lock(chan); - lock_sock(sk); + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,11 +1318,20 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + int result; + sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; release_sock(sk); - __l2cap_ecred_conn_rsp_defer(chan); + result = __l2cap_ecred_conn_rsp_defer(chan); + if (result) + err = result; } else if (bdaddr_type_is_le(chan->src_type)) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1778,7 +1821,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1814,8 +1860,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) + if (err) { sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index 62a6b1e8f..bc54e0e65 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v12 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 17:10 ` [v11,1/2] " bluez.test.bot 2026-11-14 6:05 ` [PATCH v12 1/2] " Kyle Pepper @ 2026-11-14 6:05 ` Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-14 6:05 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline. Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them. Reject an incoming deferred ECRED batch when an accepted child closes before the connection response, so its siblings cannot stay suspended. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- include/net/bluetooth/l2cap.h | 9 +- net/bluetooth/l2cap_core.c | 333 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 181 +++++++++++------- net/bluetooth/smp.c | 1 + 4 files changed, 435 insertions(+), 89 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index cf5c2df68..b004cc643 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -604,6 +604,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -644,6 +645,8 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -767,6 +770,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -974,7 +979,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); @@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 4e8aec98e..b3d2052fa 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,7 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +480,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +711,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -726,6 +731,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) hci_conn_drop(chan->conn->hcon); } + if (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + skb_queue_purge(&chan->tx_q); + return; + } + if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) return; @@ -733,11 +744,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) case L2CAP_MODE_BASIC: break; - case L2CAP_MODE_LE_FLOWCTL: - case L2CAP_MODE_EXT_FLOWCTL: - skb_queue_purge(&chan->tx_q); - break; - case L2CAP_MODE_ERTM: __clear_retrans_timer(chan); __clear_monitor_timer(chan); @@ -870,6 +876,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -877,6 +886,13 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) case BT_CONNECTED: case BT_CONFIG: + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) { + l2cap_chan_ecred_connect_reject(chan); + return; + } + if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); l2cap_send_disconn_req(chan, reason); @@ -2705,12 +2721,21 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + (chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2719,6 +2744,198 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !test_bit(HCI_CONN_ENCRYPT_PEND, chan->conn->hcon->flags) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) { + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } + } + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && + (chan->state == BT_CONNECTED || chan->state == BT_CONNECT2)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) && + l2cap_chan_security_ready(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL && + chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + u8 old_pending = hcon->pending_sec_level; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + /* Do not change the requirements of a procedure already in progress. */ + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (!l2cap_chan_check_security(chan, true) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + hcon->pending_sec_level = old_pending; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2791,9 +3008,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4043,7 +4258,14 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (chan->mode != L2CAP_MODE_EXT_FLOWCTL || + test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + return; + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) { + *result = -ECONNREFUSED; + return; + } + if (*result < 0) return; switch (chan->state) { @@ -4052,6 +4274,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4062,6 +4286,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4140,10 +4366,17 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + if (rsp->security_failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + l2cap_chan_del(chan, rsp->security_failed ? + ECONNREFUSED : ECONNRESET); + chan->ops->close(chan); + } done: if (locked) @@ -4152,23 +4385,32 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) l2cap_chan_put(chan); } -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) - return; + return 0; if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) - return; + return 0; BT_DBG("chan %p id %d", chan, id); memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + sibling->mode == L2CAP_MODE_EXT_FLOWCTL && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &sibling->flags) && + test_bit(FLAG_SECURITY_FAILED, &sibling->flags)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4179,7 +4421,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); if (result > 0) - return; + return 0; if (result < 0) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4190,6 +4432,24 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + if (locked) { + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else if (accepted == chan && + !test_bit(FLAG_DEL, &chan->flags)) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } + + return data.security_failed ? -ECONNREFUSED : 0; } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5483,10 +5743,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7623,6 +7880,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -8089,6 +8348,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8096,7 +8357,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8104,6 +8368,31 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* An authentication event may precede encryption completion. */ + if (pending && !status && + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d8a7b5f4b..c12bd17a4 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,65 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + if (chan->scid == L2CAP_CID_ATT) { + release_sock(sk); + l2cap_chan_req_mic_len(chan, conn->hcon); + if (smp_conn_security(conn->hcon, sec->level)) + return -EINVAL; + lock_sock(sk); + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } else if ((sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || + sk->sk_state == BT_CONNECTED) { + bool ready; + + release_sock(sk); + ready = l2cap_chan_check_security(chan, true); + lock_sock(sk); + if (!ready) + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + else + sk->sk_state_change(sk); + } else { + err = -EINVAL; + } +done: + release_sock(sk); + 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,78 +1003,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - l2cap_chan_lock(chan); - lock_sock(sk); + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,11 +1318,20 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + int result; + sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; release_sock(sk); - __l2cap_ecred_conn_rsp_defer(chan); + result = __l2cap_ecred_conn_rsp_defer(chan); + if (result) + err = result; } else if (bdaddr_type_is_le(chan->src_type)) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1774,7 +1817,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1810,8 +1856,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) + if (err) { sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index f8f86b8f7..84d85613c 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v11 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades 2026-11-13 3:06 [PATCH v10 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 3:06 ` [PATCH v10 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper @ 2026-11-13 14:00 ` Kyle Pepper 2 siblings, 0 replies; 16+ messages in thread From: Kyle Pepper @ 2026-11-13 14:00 UTC (permalink / raw) To: linux-bluetooth; +Cc: luiz.dentz, marcel, linux-kernel A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met. Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline. Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations. Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them. Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline") Assisted-by: LLM Signed-off-by: Kyle Pepper <cochcoder@proton.me> --- include/net/bluetooth/l2cap.h | 9 +- net/bluetooth/l2cap_core.c | 326 +++++++++++++++++++++++++++++++--- net/bluetooth/l2cap_sock.c | 181 ++++++++++++------- net/bluetooth/smp.c | 1 + 4 files changed, 428 insertions(+), 89 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index cf5c2df68..b004cc643 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -604,6 +604,7 @@ struct l2cap_chan { __u32 remote_flush_to; struct delayed_work chan_timer; + struct list_head security_list; struct delayed_work retrans_timer; struct delayed_work monitor_timer; struct delayed_work ack_timer; @@ -644,6 +645,8 @@ struct l2cap_conn { struct sk_buff_head pending_rx; struct work_struct pending_rx_work; + struct work_struct security_work; + atomic_t security_failure; bool pending_rx_blocked __guarded_by(&lock); struct delayed_work id_addr_timer; @@ -767,6 +770,8 @@ enum { FLAG_ECRED_CONN_REQ_SENT, FLAG_ECRED_RECONF_REQ_SENT, FLAG_PENDING_SECURITY, + FLAG_SECURITY_CFM, + FLAG_SECURITY_FAILED, FLAG_HOLD_HCI_CONN, FLAG_DEL, }; @@ -974,7 +979,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan); -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); @@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy); void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail); void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon); int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator); +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len); +void l2cap_security_failure(struct l2cap_conn *conn); void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 4e8aec98e..87e5d107a 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -59,6 +59,7 @@ static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, static void l2cap_retrans_timeout(struct work_struct *work); static void l2cap_monitor_timeout(struct work_struct *work); static void l2cap_ack_timeout(struct work_struct *work); +static void l2cap_security_work(struct work_struct *work); static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) __must_hold(&chan->lock) __must_hold(&chan->conn->lock); @@ -479,6 +480,7 @@ struct l2cap_chan *l2cap_chan_create(void) write_unlock(&chan_list_lock); INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); + INIT_LIST_HEAD(&chan->security_list); INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +711,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) BT_DBG("chan %p, err %d, state %s", chan, err, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); chan->ops->teardown(chan, err); + clear_bit(FLAG_SECURITY_FAILED, &chan->flags); if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { /* Delete from channel list */ @@ -726,6 +731,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) hci_conn_drop(chan->conn->hcon); } + if (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + skb_queue_purge(&chan->tx_q); + return; + } + if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) return; @@ -733,11 +744,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) case L2CAP_MODE_BASIC: break; - case L2CAP_MODE_LE_FLOWCTL: - case L2CAP_MODE_EXT_FLOWCTL: - skb_queue_purge(&chan->tx_q); - break; - case L2CAP_MODE_ERTM: __clear_retrans_timer(chan); __clear_monitor_timer(chan); @@ -870,6 +876,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan, int reason) BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + switch (chan->state) { case BT_LISTEN: chan->ops->teardown(chan, 0); @@ -2705,12 +2714,21 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *chan, return 0; } -static void l2cap_le_flowctl_send(struct l2cap_chan *chan) +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { int sent = 0; BT_DBG("chan %p", chan); + if (chan->state != BT_CONNECTED || + test_bit(FLAG_DEL, &chan->flags) || + test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + (chan->mode == L2CAP_MODE_EXT_FLOWCTL && chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags))) + return false; + while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); chan->tx_credits--; @@ -2719,6 +2737,198 @@ static void l2cap_le_flowctl_send(struct l2cap_chan *chan) BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, skb_queue_len(&chan->tx_q)); + + return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) + __must_hold(&chan->lock) +{ + if (l2cap_le_flowctl_send(chan) && chan->tx_credits) + chan->ops->resume(chan); + else + chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ + struct hci_conn *hcon = chan->conn->hcon; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->lock); + + return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && + (chan->sec_level == BT_SECURITY_LOW || + test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && + l2cap_check_enc_key_size(hcon, chan) && + l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ + return !test_bit(HCI_CONN_ENCRYPT_PEND, chan->conn->hcon->flags) && + l2cap_chan_security_sufficient(chan); +} + +static void l2cap_chan_security_complete(struct l2cap_chan *chan) + __must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ + clear_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + + if (chan->state == BT_CONNECT2) { + chan->ops->resume(chan); + return; + } + + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) + __l2cap_ecred_conn_rsp_defer(chan); + if (!test_bit(FLAG_DEL, &chan->flags)) + l2cap_le_flowctl_resume(chan); +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ + struct hci_dev *hdev = conn->hcon->hdev; + + /* Pair with the RCU grace period before hci_dev_close_sync() drains + * this queue. No security work may be added after that drain. + */ + rcu_read_lock(); + if (test_bit(HCI_UP, &hdev->flags)) { + l2cap_conn_get(conn); + if (!queue_work(hdev->workqueue, &conn->security_work)) + l2cap_conn_put(conn); + } + rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ + /* SMP receive holds conn->lock; management and encryption hold hdev. */ + lockdep_assert(lockdep_is_held(&conn->lock) || + lockdep_is_held(&conn->hcon->hdev->lock)); + if (conn->hcon->type != LE_LINK) + return; + + atomic_set(&conn->security_failure, 1); + l2cap_queue_security_work(conn); +} + +static void l2cap_security_work(struct work_struct *work) +{ + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + security_work); + struct l2cap_chan *chan, *next; + LIST_HEAD(pending); + + hci_dev_lock(conn->hcon->hdev); + mutex_lock(&conn->lock); + if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || + conn->hcon->l2cap_data != conn) + goto done; + + if (atomic_xchg(&conn->security_failure, 0)) { + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if ((chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2) && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + set_bit(FLAG_SECURITY_CFM, &chan->flags); + } + l2cap_chan_unlock(chan); + } + } + + /* Hold all selected channels before a callback can remove siblings. */ + list_for_each_entry(chan, &conn->chan_l, list) { + l2cap_chan_lock(chan); + if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && + (chan->state == BT_CONNECTED || chan->state == BT_CONNECT2)) { + l2cap_chan_hold(chan); + list_add_tail(&chan->security_list, &pending); + } + l2cap_chan_unlock(chan); + } + + list_for_each_entry_safe(chan, next, &pending, security_list) { + list_del_init(&chan->security_list); + l2cap_chan_lock(chan); + lockdep_assert_held(&chan->conn->lock); + if ((chan->state == BT_CONNECTED || chan->state == BT_CONNECT2) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) && + l2cap_chan_security_ready(chan)) { + l2cap_chan_security_complete(chan); + } else { + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL && + chan->ident && + test_bit(FLAG_DEFER_SETUP, &chan->flags) && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + l2cap_chan_ecred_connect_reject(chan); + else + __l2cap_chan_close(chan, ECONNREFUSED); + chan->ops->close(chan); + } + } + l2cap_chan_unlock(chan); + l2cap_chan_put(chan); + } +done: + mutex_unlock(&conn->lock); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ + struct hci_conn *hcon = chan->conn->hcon; + u8 old_level = chan->sec_level, old_mic = chan->mic_len; + u8 old_req_mic = hcon->req_mic_len; + u8 old_pending = hcon->pending_sec_level; + int err = 0; + + lockdep_assert_held(&hcon->hdev->lock); + lockdep_assert_held(&chan->conn->lock); + lockdep_assert_held(&chan->lock); + + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) + return -ECONNREFUSED; + /* Do not admit a new request before an old SMP failure is dispatched. */ + if (atomic_read(&chan->conn->security_failure)) + return -EBUSY; + /* Do not change the requirements of a procedure already in progress. */ + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || + smp_pairing_in_progress(hcon)) + return -EBUSY; + + chan->sec_level = level; + chan->mic_len = mic_len; + if (!l2cap_chan_check_security(chan, true) || + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + clear_bit(FLAG_SECURITY_CFM, &chan->flags); + chan->ops->suspend(chan); + return 0; + } else if (!l2cap_chan_security_ready(chan)) { + /* SMP did not start a procedure that could meet this request. */ + err = -EACCES; + goto restore; + } + + l2cap_chan_security_complete(chan); + return 0; +restore: + chan->sec_level = old_level; + chan->mic_len = old_mic; + hcon->req_mic_len = old_req_mic; + hcon->pending_sec_level = old_pending; + return err; } static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2791,9 +3001,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); - l2cap_le_flowctl_send(chan); - - if (!chan->tx_credits) + if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) chan->ops->suspend(chan); err = len; @@ -4043,7 +4251,14 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) { int *result = data; - if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + if (chan->mode != L2CAP_MODE_EXT_FLOWCTL || + test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + return; + if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) { + *result = -ECONNREFUSED; + return; + } + if (*result < 0) return; switch (chan->state) { @@ -4052,6 +4267,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) (*result)++; return; case BT_CONNECTED: + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) + (*result)++; return; default: /* If not connected or pending accept it has been refused */ @@ -4062,6 +4279,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data) struct l2cap_ecred_rsp_data { struct l2cap_chan *locked_chan; + struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; + bool security_failed; struct { struct l2cap_ecred_conn_rsp_hdr rsp; __le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4140,10 +4359,17 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) rsp->pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_NO_MEM); /* Include all channels pending with the same ident */ - if (!rsp->pdu.rsp.result) + if (!rsp->pdu.rsp.result) { + rsp->accepted[rsp->count] = chan; + l2cap_chan_hold(chan); rsp_flex->dcid[rsp->count++] = cpu_to_le16(chan->scid); - else - l2cap_chan_del(chan, ECONNRESET); + } else { + if (rsp->security_failed) + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + l2cap_chan_del(chan, rsp->security_failed ? + ECONNREFUSED : ECONNRESET); + chan->ops->close(chan); + } done: if (locked) @@ -4152,23 +4378,32 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan *chan, void *data) l2cap_chan_put(chan); } -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { struct l2cap_conn *conn = chan->conn; struct l2cap_ecred_rsp_data data; + struct l2cap_chan *sibling; u16 id = chan->ident; int result = 0; + int i; if (!id) - return; + return 0; if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) - return; + return 0; BT_DBG("chan %p id %d", chan, id); memset(&data, 0, sizeof(data)); data.locked_chan = chan; + list_for_each_entry(sibling, &conn->chan_l, list) { + if (sibling->ident == id && + sibling->mode == L2CAP_MODE_EXT_FLOWCTL && + !test_bit(FLAG_ECRED_CONN_REQ_SENT, &sibling->flags) && + test_bit(FLAG_SECURITY_FAILED, &sibling->flags)) + data.security_failed = true; + } data.pdu.rsp.mtu = cpu_to_le16(chan->imtu); data.pdu.rsp.mps = cpu_to_le16(chan->mps); @@ -4179,7 +4414,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) __l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); if (result > 0) - return; + return 0; if (result < 0) data.pdu.rsp.result = cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4190,6 +4425,24 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), &data.pdu); + + /* Data must follow the connection response on the wire. */ + for (i = 0; i < data.count; i++) { + struct l2cap_chan *accepted = data.accepted[i]; + bool locked = l2cap_chan_try_sibling_lock(accepted, chan); + + if (locked) { + if (!test_bit(FLAG_DEL, &accepted->flags)) + l2cap_le_flowctl_resume(accepted); + l2cap_chan_unlock(accepted); + } else if (accepted == chan && + !test_bit(FLAG_DEL, &chan->flags)) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_put(accepted); + } + + return data.security_failed ? -ECONNREFUSED : 0; } void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -5483,10 +5736,7 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn, chan->tx_credits += credits; /* Resume sending */ - l2cap_le_flowctl_send(chan); - - if (chan->tx_credits) - chan->ops->resume(chan); + l2cap_le_flowctl_resume(chan); unlock: l2cap_chan_unlock(chan); @@ -7623,6 +7873,8 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) skb_queue_head_init(&conn->pending_rx); INIT_WORK(&conn->pending_rx_work, process_pending_rx); + INIT_WORK(&conn->security_work, l2cap_security_work); + atomic_set(&conn->security_failure, 0); INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; @@ -8089,6 +8341,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) mutex_lock(&conn->lock); list_for_each_entry(chan, &conn->chan_l, list) { + bool le_flowctl; + l2cap_chan_lock(chan); lockdep_assert_held(&chan->conn->lock); @@ -8096,7 +8350,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, state_to_string(chan->state)); - if (!status && encrypt) + le_flowctl = hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL); + if (!status && encrypt && !le_flowctl) chan->sec_level = hcon->sec_level; if (!__l2cap_no_conn_pending(chan)) { @@ -8104,6 +8361,31 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) continue; } + if (le_flowctl && (chan->state == BT_CONNECTED || + chan->state == BT_CONNECT2)) { + bool pending = test_bit(FLAG_PENDING_SECURITY, &chan->flags); + + /* An authentication event may precede encryption completion. */ + if (pending && !status && + test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags)) { + l2cap_chan_unlock(chan); + continue; + } + if ((status && pending) || + !l2cap_chan_security_sufficient(chan)) { + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(FLAG_SECURITY_FAILED, &chan->flags); + } + if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + set_bit(FLAG_SECURITY_CFM, &chan->flags); + l2cap_queue_security_work(conn); + } else if (!status && chan->state == BT_CONNECTED) { + l2cap_le_flowctl_resume(chan); + } + l2cap_chan_unlock(chan); + continue; + } + if (!status && (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)) { chan->ops->resume(chan); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index d8a7b5f4b..c12bd17a4 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -929,12 +929,65 @@ static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) return 0; } +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *chan, + const struct bt_security *sec) +{ + struct l2cap_conn *conn = l2cap_chan_conn(chan); + int err = 0; + + lock_sock(sk); + if (conn && conn->hcon->type == LE_LINK && + (chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL)) { + if (sk->sk_state != BT_CONNECTED && + !(sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { + release_sock(sk); + return -EINVAL; + } + release_sock(sk); + return l2cap_chan_set_security(chan, sec->level, sec->mic_len); + } + + chan->sec_level = sec->level; + chan->mic_len = sec->mic_len; + if (!conn) + goto done; + if (chan->scid == L2CAP_CID_ATT) { + release_sock(sk); + l2cap_chan_req_mic_len(chan, conn->hcon); + if (smp_conn_security(conn->hcon, sec->level)) + return -EINVAL; + lock_sock(sk); + set_bit(FLAG_PENDING_SECURITY, &chan->flags); + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state = BT_CONFIG; + chan->state = BT_CONFIG; + } else if ((sk->sk_state == BT_CONNECT2 && + test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || + sk->sk_state == BT_CONNECTED) { + bool ready; + + release_sock(sk); + ready = l2cap_chan_check_security(chan, true); + lock_sock(sk); + if (!ready) + set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + else + sk->sk_state_change(sk); + } else { + err = -EINVAL; + } +done: + release_sock(sk); + 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,78 +1003,59 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, if (level != SOL_BLUETOOTH) return -ENOPROTOOPT; - l2cap_chan_lock(chan); - lock_sock(sk); + if (optname == BT_SECURITY) { + struct bt_security sec = { .level = BT_SECURITY_LOW }; + bool have_conn; - 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; - } + /* Keep accepting the struct size used before mic_len was added. */ + if (optlen < offsetofend(struct bt_security, key_size)) + return -EINVAL; - err = copy_struct_from_sockptr(&sec, sizeof(sec), optval, - optlen); + 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; - } + return err; - /* 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; - } + if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) + return -EINVAL; - chan->sec_level = sec.level; - chan->mic_len = sec.mic_len; + /* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ + if (sec.mic_len > 0x02) + return -EINVAL; + l2cap_chan_lock(chan); + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED && + chan->chan_type != L2CAP_CHAN_FIXED && + chan->chan_type != L2CAP_CHAN_RAW) { + l2cap_chan_unlock(chan); + return -EINVAL; + } conn = l2cap_chan_conn(chan); - if (!conn) - break; + if (!conn) { + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock(chan); + return err; + } + l2cap_conn_get(conn); + l2cap_chan_unlock(chan); - /* 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; - } + /* SMP receive holds conn->lock; management holds hdev->lock. */ + hci_dev_lock(conn->hcon->hdev); + have_conn = l2cap_chan_lock_conn(chan); - set_bit(FLAG_PENDING_SECURITY, &chan->flags); - sk->sk_state = BT_CONFIG; - chan->state = BT_CONFIG; + if (l2cap_chan_conn(chan) != conn) + err = -ENOTCONN; + else + err = l2cap_sock_set_security(sk, chan, &sec); + l2cap_chan_unlock_conn(chan, have_conn); + hci_dev_unlock(conn->hcon->hdev); + l2cap_conn_put(conn); + return err; + } - /* 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; + l2cap_chan_lock(chan); + lock_sock(sk); + + switch (optname) { case BT_DEFER_SETUP: if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { @@ -1284,11 +1318,20 @@ static int l2cap_sock_defer(struct sock *sk) if (!have_conn) { release_sock(sk); err = -ENOTCONN; + } else if ((chan->mode == L2CAP_MODE_LE_FLOWCTL || + chan->mode == L2CAP_MODE_EXT_FLOWCTL) && + test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { + release_sock(sk); + err = -EAGAIN; } else if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + int result; + sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; release_sock(sk); - __l2cap_ecred_conn_rsp_defer(chan); + result = __l2cap_ecred_conn_rsp_defer(chan); + if (result) + err = result; } else if (bdaddr_type_is_le(chan->src_type)) { sk->sk_state = BT_CONNECTED; chan->state = BT_CONNECTED; @@ -1774,7 +1817,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err) sk->sk_state = BT_CLOSED; chan->state = BT_CLOSED; - sk->sk_err = err; + if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || + READ_ONCE(sk->sk_err) != ECONNREFUSED) + sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); if (parent) { /* @@ -1810,8 +1856,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state, sk->sk_state = state; - if (err) + if (err) { sk->sk_err = err; + clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); + sk->sk_state_change(sk); + } release_sock(sk); } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index f8f86b8f7..84d85613c 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -785,6 +785,7 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason) &reason); mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); + l2cap_security_failure(conn); if (chan->data) smp_chan_destroy(conn); ^ permalink raw reply related [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-11-30 5:03 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-13 3:06 [PATCH v10 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 3:06 ` [PATCH v10 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper 2026-11-13 14:00 ` [PATCH v11 1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs Kyle Pepper 2026-11-13 17:10 ` [v11,1/2] " bluez.test.bot 2026-11-14 6:05 ` [PATCH v12 1/2] " Kyle Pepper 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-24 18:48 ` [v13,1/2] " bluez.test.bot 2026-11-25 23:44 ` [PATCH v14 1/2] " Kyle Pepper 2026-11-26 3:40 ` [v14,1/2] " bluez.test.bot 2026-11-30 0:05 ` [PATCH v15 1/2] " Kyle Pepper 2026-11-30 5:03 ` [v15,1/2] " bluez.test.bot 2026-11-30 0:06 ` [PATCH v15 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades Kyle Pepper 2026-11-25 23:44 ` [PATCH v14 " Kyle Pepper 2026-11-24 14:02 ` [PATCH v13 " Kyle Pepper 2026-11-14 6:05 ` [PATCH v12 " Kyle Pepper 2026-11-13 14:00 ` [PATCH v11 " Kyle Pepper
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox