From: Kyle Pepper <cochcoder@proton.me>
To: linux-bluetooth@vger.kernel.org
Cc: luiz.dentz@gmail.com, marcel@holtmann.org, linux-kernel@vger.kernel.org
Subject: [PATCH v16 2/2] Bluetooth: L2CAP: Keep credits from completing security upgrades
Date: Tue, 01 Dec 2026 02:29:34 +0000 [thread overview]
Message-ID: <20261201022855.1483888-2-cochcoder@proton.me> (raw)
In-Reply-To: <6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795807576.git.github.filter194@slmail.me>
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.
Also defer new BT_SECURITY requests until prior link results and
requirements are dispatched. Return synchronous ECRED rejection,
consume its socket error, reject batches on peer child withdrawal,
and count accepted children when checking identifier reuse.
AI assistance: Codex applied the supplied v16 diff and prepared
these separate source exports and validation notes.
Fixes: fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Signed-off-by: Kyle Pepper <cochcoder@proton.me>
---
Local v16 validation (unsigned review only):
- GCC/Clang ASan/UBSan: 172 expected outcomes, including
50 mutation controls.
- GCC/Clang/ECRED-off W=1 objects and sparse pass.
- Smatch retains two inherited diagnostics.
- KASAN: focused 16/16; full 137/142, with all five failures
matching the preserved v14 baseline and no kernel alerts.
- Strict KCSAN: 16/16 focused on the initial two images. First full
run panicked in move_linked_works / queue_work_on. The control
panicked in process_one_work / queue_work_on.
The second control passed 16/16 focused; its full run
panicked in skb_queue_tail / vhci_poll [hci_vhci].
The sanitizer guests use separate test-only workqueue changes.
No v16 remote CI, hardware or PTS result is claimed.
Changes in v16:
- Return EBUSY from BT_SECURITY until a latched SMP result, queued
confirmation or link requirement restore has been dispatched.
- Return a deferred ECRED rejection from BT_SECURITY completion and
consume teardown's duplicate socket error.
- Answer the whole deferred ECRED batch when the peer disconnects one
child, so an accepted sibling is not left without a response.
- Count accepted-but-unanswered children when rejecting a reused ECRED
identifier.
- Wrap validation notes to fix the v15 GitLint B1 failures in PR1250.
https://github.com/bluez/bluetooth-next/pull/1250
- Rebase onto current bluetooth-next, preserving upstream ECRED
key-size validation and the MPS-adjustment removal.
Previous v15: https://lore.kernel.org/linux-bluetooth/6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795807576.git.github.filter194@slmail.me/
include/net/bluetooth/l2cap.h | 14 +-
net/bluetooth/l2cap_core.c | 567 +++++++++++++++++++++++++++++++---
net/bluetooth/l2cap_sock.c | 190 ++++++++----
net/bluetooth/smp.c | 3 +-
4 files changed, 663 insertions(+), 111 deletions(-)
diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
index 72d9d56c6..c21edb426 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 6ee19d98f..e24aee8dd 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,307 @@ 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);
+}
+
+/* Returns -ECONNREFUSED if completing this channel answered its deferred ECRED
+ * batch with a security failure, which also deleted the channel.
+ */
+static int l2cap_chan_security_complete(struct l2cap_chan *chan)
+ __must_hold(&chan->lock) __must_hold(&chan->conn->lock)
+{
+ int err = 0;
+
+ clear_bit(FLAG_PENDING_SECURITY, &chan->flags);
+ clear_bit(FLAG_SECURITY_CFM, &chan->flags);
+
+ if (chan->state == BT_CONNECT2) {
+ chan->ops->resume(chan);
+ return 0;
+ }
+
+ if (chan->mode == L2CAP_MODE_EXT_FLOWCTL)
+ err = __l2cap_ecred_conn_rsp_defer(chan);
+ if (!test_bit(FLAG_DEL, &chan->flags))
+ l2cap_le_flowctl_resume(chan);
+
+ return err;
+}
+
+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);
+}
+
+/* True while an earlier procedure's outcome has not been dispatched by
+ * l2cap_security_work(): a latched SMP result, an unrestored link requirement,
+ * or a sibling whose confirmation is queued. HCI_CONN_ENCRYPT_PEND and the SMP
+ * state are already clear in that window, so l2cap_chan_security_busy() alone
+ * would admit a new request ahead of the old failure or requirement restore.
+ */
+static bool l2cap_conn_security_undispatched(struct l2cap_conn *conn)
+ __must_hold(&conn->lock)
+{
+ struct l2cap_chan *sibling;
+
+ if (atomic_read(&conn->security_failure) ||
+ atomic_read(&conn->security_recheck) ||
+ conn->security_restore)
+ return true;
+
+ list_for_each_entry(sibling, &conn->chan_l, list) {
+ if (test_bit(FLAG_SECURITY_CFM, &sibling->flags))
+ return true;
+ }
+
+ return false;
+}
+
+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 result, confirmation or
+ * requirement restore is dispatched by l2cap_security_work().
+ */
+ if (l2cap_conn_security_undispatched(chan->conn))
+ 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;
+ }
+
+ return l2cap_chan_security_complete(chan);
+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 +3144,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 +4397,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 +4412,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 */
@@ -4065,8 +4422,24 @@ static void l2cap_ecred_list_defer(struct l2cap_chan *chan, void *data)
}
}
+/* Count every channel still owed a response for this ident, including children
+ * that were already accepted and are waiting for their siblings. A peer must
+ * not reuse the ident until the response for the batch has been queued.
+ */
+static void l2cap_ecred_list_ident_busy(struct l2cap_chan *chan, void *data)
+{
+ int *busy = data;
+
+ if (l2cap_chan_ecred_rsp_pending(chan))
+ (*busy)++;
+}
+
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 +4447,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 +4467,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 +4483,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 +4507,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 +4537,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 +4572,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 +4580,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)
@@ -4895,9 +5329,18 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
chan->ops->set_shutdown(chan);
- l2cap_chan_del(chan, ECONNRESET);
+ /* The peer withdrew one child of an unanswered deferred ECRED batch.
+ * Answer the whole batch like a local close does, so an already accepted
+ * sibling is not left waiting for a response that is never sent. This
+ * deletes and closes @chan.
+ */
+ if (l2cap_chan_ecred_rsp_pending(chan))
+ l2cap_chan_ecred_connect_reject(chan);
- chan->ops->close(chan);
+ if (!test_bit(FLAG_DEL, &chan->flags)) {
+ l2cap_chan_del(chan, ECONNRESET);
+ chan->ops->close(chan);
+ }
l2cap_chan_unlock(chan);
l2cap_chan_put(chan);
@@ -5488,10 +5931,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);
@@ -5525,7 +5965,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
}
/* Check if there are no pending channels with the same ident */
- __l2cap_chan_list_id(conn, cmd->ident, l2cap_ecred_list_defer,
+ __l2cap_chan_list_id(conn, cmd->ident, l2cap_ecred_list_ident_busy,
&num_scid);
if (num_scid) {
result = L2CAP_CR_LE_INVALID_PARAMS;
@@ -7644,6 +8084,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;
@@ -7907,6 +8350,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;
@@ -8110,6 +8557,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);
@@ -8117,7 +8566,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)) {
@@ -8125,6 +8577,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);
@@ -8183,6 +8663,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 d5663a464..0b4c20008 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -929,12 +929,69 @@ 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);
+ err = l2cap_chan_set_security(chan, sec->level, sec->mic_len);
+ /* The direct return reports it; drop teardown's socket copy. */
+ if (err == -ECONNREFUSED)
+ cmpxchg(&sk->sk_err, ECONNREFUSED, 0);
+ return err;
+ }
+
+ 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 +1007,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,11 +1322,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 +1828,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 +1867,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 62a6b1e8f..b94182e18 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)
next prev parent reply other threads:[~2026-12-01 2:29 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
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-12-01 2:29 ` [PATCH v16 1/2] " Kyle Pepper
2026-12-01 6:54 ` [v16,1/2] " bluez.test.bot
2026-12-01 2:29 ` Kyle Pepper [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201022855.1483888-2-cochcoder@proton.me \
--to=cochcoder@proton.me \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox