From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.17 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796092180; b=VMf4+/vM7PAN0GYoEQ64/UVOg70AdJLhPBVBhQC6/zpl/rHlg5mPmBO1FwPREFf9eGWZ joMfBbist8SOkgNIx3KJJsmJUgqorDqXk94EbZ/0cHZ7sPlmeEOvBufJ2aDtCzkVLkw8T NWT1PS90wSPJ1BeXlDKg45Wu3iuWJTArtFbp0hHl/oGiadGm/p3uIFd4W+isOtagoOPs9 mcWYnk3kPLWJdfMYaP+uPLx6sgUPmG5hGltAAAAU36SVQH/ken5/6gSGKKqlsJ81h5v6D fDf/7TtWKRzpYlBRCIMh5xdJV0zBNprvUF3TIDAMNUoGpbxgkMrpORvh0CYt+EyPnzA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796092180; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=rgQabFf9OHfZDrZMJl40dIeB7QRRFpKLdMaQfcqnb2U=; b=sHGmUiQ/Gof/l/YVf2Wkxx+Ru+gsGCJT/RBj4bHWglJSqb6Kptmmqz/W/hxS9pXIWHKA A4dSiPOKMPrvxQn4vsc17yVlVFT7XGi4zMs4qGsss6+i2hHU2ll6+AvYeJnoeyIHGv+eE mIz/nogxEhjzTeShQ9G/rdINqx9tnwKXyUoio4Ou6Najlnjum6EG8c2ci9F3n1B6w/RJb DtSou3UM1F06hNvXMeG068flIfWVbxPQiCf4gaS+FDE0ydlEoNTVuo+rBTb1echcocP4V v9im4tN/txxTBd+CeZvqLXAH0JNfUB5VxDc1M25JEp3vEFX5CeUg1TZVehxrtiZWkMw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=protonmail2 header.b=X+To24jD; arc=none smtp.remote-ip=109.224.244.17 Received: from mail-24417.protonmail.ch (mail-24417.protonmail.ch [109.224.244.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7051606A320 for ; Tue, 1 Dec 2026 02:29:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A7051606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=protonmail2 header.b=X+To24jD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail2; t=1796092175; x=1796351375; bh=rgQabFf9OHfZDrZMJl40dIeB7QRRFpKLdMaQfcqnb2U=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=X+To24jDYaEr11BEbdHKdz4IiswmLNRPEd6dSjFqBpIdV7V6Qd4iJa766VxcjlcrX sM02Ov2RdLgaKsS2n/CW3b+hiRbh5/XRA2d3TmI1nnIfPnV+zNpHLKBZZmx5LqFwg7 E0OeuOBFyec/bt47/G47OCJjN4SpuUO3cELaWPCk/wq+xLTfz0Or6ohjvow+0dVRxh 3IJIuHy9PkWY7ijJJ33MtlUzpJ7YLaKHjpkQmFr0vWzMi9m32hLCcy2g55PougG2tM MH7YCjNqckCSYlrY8RP1bLShMz3dEv7S+OiR4FFMLyimXL3jJcJQLdzKsPTwXDnQfk UwJPeH/GZs9ag== Date: Tue, 01 Dec 2026 02:29:34 +0000 To: linux-bluetooth@vger.kernel.org From: Kyle Pepper 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 Message-ID: <20261201022855.1483888-2-cochcoder@proton.me> In-Reply-To: <6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795807576.git.github.filter194@slmail.me> References: <20261113030556.75484-1-cochcoder@proton.me> <4278a9caf4dc8e949092452c141a5be465ec77e1.1794550328.git.github.filter194@slmail.me> <4278a9caf4dc8e949092452c141a5be465ec77e1.1794617024.git.github.filter194@slmail.me> <9d9d37680044266dbd1d6919b08564026afcd87d.1795528595.git.github.filter194@slmail.me> <6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795650070.git.github.filter194@slmail.me> <6aff9cb5ebb1f1512734d5ecdac876a2cc0c86bc.1795807576.git.github.filter194@slmail.me> Feedback-ID: 97716137:user:proton X-Pm-Message-ID: 176bf3221ebea0251b8dcace5339b84ebae0f1d4 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 --- Local v16 validation (unsigned review only): - GCC/Clang ASan/UBSan: 172 expected outcomes, including 50 mutation controls. - GCC/Clang/ECRED-off W=3D1 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/6aff9cb5ebb1f1512734d= 5ecdac876a2cc0c86bc.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 { =09__u32=09=09remote_flush_to; =20 =09struct delayed_work=09chan_timer; +=09struct list_head=09security_list; =09struct delayed_work=09retrans_timer; =09struct delayed_work=09monitor_timer; =09struct delayed_work=09ack_timer; @@ -649,6 +650,12 @@ struct l2cap_conn { =20 =09struct sk_buff_head=09pending_rx; =09struct work_struct=09pending_rx_work; +=09struct work_struct=09security_work; +=09atomic_t=09=09security_failure; +=09atomic_t=09=09security_recheck; +=09__u8=09=09=09security_pending_sec_level; +=09__u8=09=09=09security_req_mic_len; +=09bool=09=09=09security_restore; =09bool=09=09=09pending_rx_blocked __guarded_by(&lock); =20 =09struct delayed_work=09id_addr_timer; @@ -772,6 +779,8 @@ enum { =09FLAG_ECRED_CONN_REQ_SENT, =09FLAG_ECRED_RECONF_REQ_SENT, =09FLAG_PENDING_SECURITY, +=09FLAG_SECURITY_CFM, +=09FLAG_SECURITY_FAILED, =09FLAG_HOLD_HCI_CONN, =09FLAG_DEL, }; @@ -979,7 +988,7 @@ void l2cap_cleanup_sockets(void); bool l2cap_is_socket(struct socket *sock); =20 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) =09__must_hold(&chan->lock) __must_hold(&chan->conn->lock); void __l2cap_connect_rsp_defer(struct l2cap_chan *chan); =20 @@ -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 *p= chan); 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 l2ca= p_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) =09__must_hold(&chan->lock) __must_hold(&chan->conn->lock); =20 @@ -479,6 +481,7 @@ struct l2cap_chan *l2cap_chan_create(void) =09write_unlock(&chan_list_lock); =20 =09INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); +=09INIT_LIST_HEAD(&chan->security_list); =09INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); =09INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); =09INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); @@ -709,7 +712,10 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) =09BT_DBG("chan %p, err %d, state %s", chan, err, =09 state_to_string(chan->state)); =20 +=09clear_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09clear_bit(FLAG_SECURITY_CFM, &chan->flags); =09chan->ops->teardown(chan, err); +=09clear_bit(FLAG_SECURITY_FAILED, &chan->flags); =20 =09if (!test_and_set_bit(FLAG_DEL, &chan->flags)) { =09=09/* Delete from channel list */ @@ -726,6 +732,12 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) =09=09=09hci_conn_drop(chan->conn->hcon); =09} =20 +=09if (chan->mode =3D=3D L2CAP_MODE_LE_FLOWCTL || +=09 chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL) { +=09=09skb_queue_purge(&chan->tx_q); +=09=09return; +=09} + =09if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) =09=09return; =20 @@ -733,11 +745,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) =09case L2CAP_MODE_BASIC: =09=09break; =20 -=09case L2CAP_MODE_LE_FLOWCTL: -=09case L2CAP_MODE_EXT_FLOWCTL: -=09=09skb_queue_purge(&chan->tx_q); -=09=09break; - =09case L2CAP_MODE_ERTM: =09=09__clear_retrans_timer(chan); =09=09__clear_monitor_timer(chan); @@ -870,6 +877,9 @@ static void __l2cap_chan_close(struct l2cap_chan *chan,= int reason) =20 =09BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); =20 +=09clear_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09clear_bit(FLAG_SECURITY_CFM, &chan->flags); + =09switch (chan->state) { =09case BT_LISTEN: =09=09chan->ops->teardown(chan, 0); @@ -877,6 +887,11 @@ static void __l2cap_chan_close(struct l2cap_chan *chan= , int reason) =20 =09case BT_CONNECTED: =09case BT_CONFIG: +=09=09if (l2cap_chan_ecred_rsp_pending(chan)) { +=09=09=09l2cap_chan_ecred_connect_reject(chan); +=09=09=09return; +=09=09} + =09=09if (chan->chan_type =3D=3D L2CAP_CHAN_CONN_ORIENTED) { =09=09=09__set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); =09=09=09l2cap_send_disconn_req(chan, reason); @@ -2707,12 +2722,47 @@ static int l2cap_segment_le_sdu(struct l2cap_chan *= chan, =09return 0; } =20 -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 resp= onse + * 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 a= fter + * an authentication error, always in BT_CONNECT, a state an incoming chan= nel + * never has. Exclude it so it cannot match an incoming batch. + */ +static bool l2cap_chan_ecred_rsp_pending(struct l2cap_chan *chan) +{ +=09return chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL && chan->ident && +=09 chan->state !=3D BT_CONNECT && +=09 test_bit(FLAG_DEFER_SETUP, &chan->flags) && +=09 !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) +{ +=09return test_bit(FLAG_SECURITY_FAILED, &chan->flags) || +=09 (chan->conn && test_bit(FLAG_PENDING_SECURITY, &chan->flags) && +=09=09atomic_read(&chan->conn->security_failure)); +} + +static bool l2cap_le_flowctl_send(struct l2cap_chan *chan) { =09int sent =3D 0; =20 =09BT_DBG("chan %p", chan); =20 +=09if (chan->state !=3D BT_CONNECTED || +=09 test_bit(FLAG_DEL, &chan->flags) || +=09 test_bit(FLAG_PENDING_SECURITY, &chan->flags) || +=09 test_bit(FLAG_SECURITY_FAILED, &chan->flags) || +=09 l2cap_chan_ecred_rsp_pending(chan)) +=09=09return false; + =09while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { =09=09l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); =09=09chan->tx_credits--; @@ -2721,6 +2771,307 @@ static void l2cap_le_flowctl_send(struct l2cap_chan= *chan) =20 =09BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, =09 skb_queue_len(&chan->tx_q)); + +=09return true; +} + +static void l2cap_le_flowctl_resume(struct l2cap_chan *chan) +=09__must_hold(&chan->lock) +{ +=09if (l2cap_le_flowctl_send(chan) && chan->tx_credits) +=09=09chan->ops->resume(chan); +=09else +=09=09chan->ops->suspend(chan); +} + +static bool l2cap_chan_security_sufficient(struct l2cap_chan *chan) +{ +=09struct hci_conn *hcon =3D chan->conn->hcon; + +=09lockdep_assert_held(&hcon->hdev->lock); +=09lockdep_assert_held(&chan->lock); + +=09return smp_sufficient_security(hcon, chan->sec_level, SMP_ALLOW_STK) && +=09 (chan->sec_level =3D=3D BT_SECURITY_LOW || +=09=09test_bit(HCI_CONN_ENCRYPT, hcon->flags)) && +=09 l2cap_check_enc_key_size(hcon, chan) && +=09 l2cap_check_mic_len(hcon, chan); +} + +static bool l2cap_chan_security_busy(struct l2cap_chan *chan) +{ +=09struct hci_conn *hcon =3D chan->conn->hcon; + +=09lockdep_assert_held(&hcon->hdev->lock); + +=09/* HCI encryption, or SMP pairing including Phase 3 key distribution */ +=09return test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || +=09 smp_pairing_in_progress(hcon); +} + +static bool l2cap_chan_security_ready(struct l2cap_chan *chan) +{ +=09return !l2cap_chan_security_busy(chan) && +=09 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) +=09__must_hold(&chan->lock) __must_hold(&chan->conn->lock) +{ +=09int err =3D 0; + +=09clear_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09clear_bit(FLAG_SECURITY_CFM, &chan->flags); + +=09if (chan->state =3D=3D BT_CONNECT2) { +=09=09chan->ops->resume(chan); +=09=09return 0; +=09} + +=09if (chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL) +=09=09err =3D __l2cap_ecred_conn_rsp_defer(chan); +=09if (!test_bit(FLAG_DEL, &chan->flags)) +=09=09l2cap_le_flowctl_resume(chan); + +=09return err; +} + +static void l2cap_queue_security_work(struct l2cap_conn *conn) +{ +=09struct hci_dev *hdev =3D conn->hcon->hdev; + +=09/* Pair with the RCU grace period before hci_dev_close_sync() drains +=09 * this queue. No security work may be added after that drain. +=09 */ +=09rcu_read_lock(); +=09if (test_bit(HCI_UP, &hdev->flags)) { +=09=09l2cap_conn_get(conn); +=09=09if (!queue_work(hdev->workqueue, &conn->security_work)) +=09=09=09l2cap_conn_put(conn); +=09} +=09rcu_read_unlock(); +} + +void l2cap_security_failure(struct l2cap_conn *conn) +{ +=09/* SMP receive holds conn->lock; management and encryption hold hdev. *= / +=09lockdep_assert(lockdep_is_held(&conn->lock) || +=09=09 lockdep_is_held(&conn->hcon->hdev->lock)); +=09if (conn->hcon->type !=3D LE_LINK) +=09=09return; + +=09atomic_set(&conn->security_failure, 1); +=09l2cap_queue_security_work(conn); +} + +void l2cap_security_recheck(struct l2cap_conn *conn) +{ +=09/* SMP receive holds conn->lock; management and encryption hold hdev. *= / +=09lockdep_assert(lockdep_is_held(&conn->lock) || +=09=09 lockdep_is_held(&conn->hcon->hdev->lock)); +=09if (conn->hcon->type !=3D LE_LINK) +=09=09return; + +=09atomic_set(&conn->security_recheck, 1); +=09l2cap_queue_security_work(conn); +} + +static void l2cap_security_mark_pending(struct l2cap_conn *conn, bool fail= ed) +=09__must_hold(&conn->lock) +{ +=09struct l2cap_chan *chan; + +=09list_for_each_entry(chan, &conn->chan_l, list) { +=09=09l2cap_chan_lock(chan); +=09=09if ((chan->state =3D=3D BT_CONNECTED || chan->state =3D=3D BT_CONNEC= T2) && +=09=09 (chan->mode =3D=3D L2CAP_MODE_LE_FLOWCTL || +=09=09 chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL) && +=09=09 test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { +=09=09=09if (failed) +=09=09=09=09set_bit(FLAG_SECURITY_FAILED, &chan->flags); +=09=09=09set_bit(FLAG_SECURITY_CFM, &chan->flags); +=09=09} +=09=09l2cap_chan_unlock(chan); +=09} +} + +static void l2cap_security_restore(struct l2cap_conn *conn) +=09__must_hold(&conn->lock) +{ +=09struct hci_conn *hcon =3D conn->hcon; +=09struct l2cap_chan *chan; +=09u8 level, mic_len; + +=09lockdep_assert_held(&hcon->hdev->lock); +=09if (!conn->security_restore || +=09 test_bit(HCI_CONN_ENCRYPT_PEND, hcon->flags) || +=09 smp_pairing_in_progress(hcon)) +=09=09return; + +=09level =3D max(conn->security_pending_sec_level, hcon->sec_level); +=09mic_len =3D max(conn->security_req_mic_len, hci_conn_mic_len(hcon)); +=09list_for_each_entry(chan, &conn->chan_l, list) { +=09=09l2cap_chan_lock(chan); +=09=09if (chan->state =3D=3D BT_CONNECTED || chan->state =3D=3D BT_CONNECT= || +=09=09 chan->state =3D=3D BT_CONNECT2 || chan->state =3D=3D BT_CONFIG) = { +=09=09=09if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { +=09=09=09=09l2cap_chan_unlock(chan); +=09=09=09=09return; +=09=09=09} +=09=09=09level =3D max(level, chan->sec_level); +=09=09=09mic_len =3D max(mic_len, chan->mic_len); +=09=09} +=09=09l2cap_chan_unlock(chan); +=09} + +=09/* Drop failed requests without lowering another channel's requirements= . */ +=09hcon->pending_sec_level =3D level; +=09hcon->req_mic_len =3D mic_len; +=09conn->security_restore =3D false; +} + +static void l2cap_security_work(struct work_struct *work) +{ +=09struct l2cap_conn *conn =3D container_of(work, struct l2cap_conn, +=09=09=09=09=09 security_work); +=09struct l2cap_chan *chan, *next; +=09LIST_HEAD(pending); + +=09hci_dev_lock(conn->hcon->hdev); +=09mutex_lock(&conn->lock); +=09if (!test_bit(HCI_UP, &conn->hcon->hdev->flags) || +=09 conn->hcon->l2cap_data !=3D conn) +=09=09goto done; + +=09if (atomic_xchg(&conn->security_failure, 0)) +=09=09l2cap_security_mark_pending(conn, true); +=09if (atomic_xchg(&conn->security_recheck, 0)) +=09=09l2cap_security_mark_pending(conn, false); + +=09/* Hold all selected channels before a callback can remove siblings. */ +=09list_for_each_entry(chan, &conn->chan_l, list) { +=09=09l2cap_chan_lock(chan); +=09=09if (test_and_clear_bit(FLAG_SECURITY_CFM, &chan->flags) && +=09=09 (chan->state =3D=3D BT_CONNECTED || chan->state =3D=3D BT_CONNEC= T2)) { +=09=09=09l2cap_chan_hold(chan); +=09=09=09list_add_tail(&chan->security_list, &pending); +=09=09} +=09=09l2cap_chan_unlock(chan); +=09} + +=09list_for_each_entry_safe(chan, next, &pending, security_list) { +=09=09list_del_init(&chan->security_list); +=09=09l2cap_chan_lock(chan); +=09=09lockdep_assert_held(&chan->conn->lock); +=09=09if ((chan->state =3D=3D BT_CONNECTED || chan->state =3D=3D BT_CONNEC= T2) && +=09=09 test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { +=09=09=09bool failed =3D test_bit(FLAG_SECURITY_FAILED, &chan->flags); + +=09=09=09if (!failed && l2cap_chan_security_busy(chan)) { +=09=09=09=09/* Another procedure, or SMP key distribution, is still +=09=09=09=09 * running. Stay pending: its HCI confirmation, +=09=09=09=09 * l2cap_security_recheck() or l2cap_security_failure() +=09=09=09=09 * queues this work again. +=09=09=09=09 */ +=09=09=09} else if (!failed && +=09=09=09=09 l2cap_chan_security_sufficient(chan)) { +=09=09=09=09l2cap_chan_security_complete(chan); +=09=09=09} else { +=09=09=09=09set_bit(FLAG_SECURITY_FAILED, &chan->flags); +=09=09=09=09if (l2cap_chan_ecred_rsp_pending(chan)) +=09=09=09=09=09l2cap_chan_ecred_connect_reject(chan); +=09=09=09=09else +=09=09=09=09=09__l2cap_chan_close(chan, ECONNREFUSED); +=09=09=09=09chan->ops->close(chan); +=09=09=09} +=09=09} +=09=09l2cap_chan_unlock(chan); +=09=09l2cap_chan_put(chan); +=09} +=09l2cap_security_restore(conn); +done: +=09mutex_unlock(&conn->lock); +=09hci_dev_unlock(conn->hcon->hdev); +=09l2cap_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 require= ment, + * or a sibling whose confirmation is queued. HCI_CONN_ENCRYPT_PEND and th= e SMP + * state are already clear in that window, so l2cap_chan_security_busy() a= lone + * would admit a new request ahead of the old failure or requirement resto= re. + */ +static bool l2cap_conn_security_undispatched(struct l2cap_conn *conn) +=09__must_hold(&conn->lock) +{ +=09struct l2cap_chan *sibling; + +=09if (atomic_read(&conn->security_failure) || +=09 atomic_read(&conn->security_recheck) || +=09 conn->security_restore) +=09=09return true; + +=09list_for_each_entry(sibling, &conn->chan_l, list) { +=09=09if (test_bit(FLAG_SECURITY_CFM, &sibling->flags)) +=09=09=09return true; +=09} + +=09return false; +} + +int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len) +{ +=09struct hci_conn *hcon =3D chan->conn->hcon; +=09u8 old_level =3D chan->sec_level, old_mic =3D chan->mic_len; +=09u8 old_req_mic =3D hcon->req_mic_len; +=09u8 old_pending =3D hcon->pending_sec_level; +=09int err =3D 0; + +=09lockdep_assert_held(&hcon->hdev->lock); +=09lockdep_assert_held(&chan->conn->lock); +=09lockdep_assert_held(&chan->lock); + +=09if (test_bit(FLAG_SECURITY_FAILED, &chan->flags)) +=09=09return -ECONNREFUSED; +=09/* Do not admit a new request before an old SMP result, confirmation or +=09 * requirement restore is dispatched by l2cap_security_work(). +=09 */ +=09if (l2cap_conn_security_undispatched(chan->conn)) +=09=09return -EBUSY; +=09/* Do not change the requirements of a procedure already in progress. *= / +=09if (test_bit(FLAG_PENDING_SECURITY, &chan->flags) || +=09 l2cap_chan_security_busy(chan)) +=09=09return -EBUSY; + +=09chan->sec_level =3D level; +=09chan->mic_len =3D mic_len; +=09if (!l2cap_chan_check_security(chan, true) || +=09 l2cap_chan_security_busy(chan)) { +=09=09if (!chan->conn->security_restore) { +=09=09=09chan->conn->security_pending_sec_level =3D old_pending; +=09=09=09chan->conn->security_req_mic_len =3D old_req_mic; +=09=09=09chan->conn->security_restore =3D true; +=09=09} +=09=09set_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09=09clear_bit(FLAG_SECURITY_CFM, &chan->flags); +=09=09chan->ops->suspend(chan); +=09=09return 0; +=09} else if (!l2cap_chan_security_ready(chan)) { +=09=09/* SMP did not start a procedure that could meet this request. */ +=09=09err =3D -EACCES; +=09=09goto restore; +=09} + +=09return l2cap_chan_security_complete(chan); +restore: +=09chan->sec_level =3D old_level; +=09chan->mic_len =3D old_mic; +=09hcon->req_mic_len =3D old_req_mic; +=09hcon->pending_sec_level =3D old_pending; +=09return err; } =20 static void l2cap_tx_timestamp(struct sk_buff *skb, @@ -2793,9 +3144,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct m= sghdr *msg, size_t len, =20 =09=09skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); =20 -=09=09l2cap_le_flowctl_send(chan); - -=09=09if (!chan->tx_credits) +=09=09if (!l2cap_le_flowctl_send(chan) || !chan->tx_credits) =09=09=09chan->ops->suspend(chan); =20 =09=09err =3D len; @@ -4048,7 +4397,13 @@ static void l2cap_ecred_list_defer(struct l2cap_chan= *chan, void *data) { =09int *result =3D data; =20 -=09if (*result || test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) +=09if (!l2cap_chan_ecred_rsp_pending(chan)) +=09=09return; +=09if (l2cap_chan_security_failed(chan)) { +=09=09*result =3D -ECONNREFUSED; +=09=09return; +=09} +=09if (*result < 0) =09=09return; =20 =09switch (chan->state) { @@ -4057,6 +4412,8 @@ static void l2cap_ecred_list_defer(struct l2cap_chan = *chan, void *data) =09=09(*result)++; =09=09return; =09case BT_CONNECTED: +=09=09if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) +=09=09=09(*result)++; =09=09return; =09default: =09=09/* 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) =09} } =20 +/* Count every channel still owed a response for this ident, including chi= ldren + * that were already accepted and are waiting for their siblings. A peer m= ust + * 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 *dat= a) +{ +=09int *busy =3D data; + +=09if (l2cap_chan_ecred_rsp_pending(chan)) +=09=09(*busy)++; +} + struct l2cap_ecred_rsp_data { =09struct l2cap_chan *locked_chan; +=09struct l2cap_chan *accepted[L2CAP_ECRED_MAX_CID]; +=09struct l2cap_chan *rejected[L2CAP_ECRED_MAX_CID]; +=09int rejected_count; +=09bool security_failed; =09struct { =09=09struct l2cap_ecred_conn_rsp_hdr rsp; =09=09__le16 scid[L2CAP_ECRED_MAX_CID]; @@ -4074,7 +4447,8 @@ struct l2cap_ecred_rsp_data { =09int count; }; =20 -/* Lock @chan if it is not @locked_chan, and has same or lower nesting lev= el. +/* 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 l2ca= p_chan *chan, =09if (chan =3D=3D locked_chan) =09=09return false; =20 -=09if (WARN_ON_ONCE(locked_chan->conn !=3D chan->conn)) -=09=09return false; +=09WARN_ON_ONCE(locked_chan->conn !=3D chan->conn); =20 -=09if (WARN_ON_ONCE(atomic_read(&locked_chan->nesting) -=09=09=09 < atomic_read(&chan->nesting))) -=09=09return false; +=09WARN_ON_ONCE(atomic_read(&locked_chan->nesting) +=09=09 < atomic_read(&chan->nesting)); =20 =09mutex_lock_nest_lock(&chan->lock, &locked_chan->conn->lock); =09return true; @@ -4111,14 +4483,12 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan= *chan, void *data) =09=09container_of(&rsp->pdu.rsp, struct l2cap_ecred_conn_rsp, hdr); =09bool locked; =20 -=09if (chan->mode !=3D L2CAP_MODE_EXT_FLOWCTL) -=09=09return; - -=09/* Check if channel for outgoing connection or if it wasn't deferred -=09 * since in those cases it must be skipped. +=09/* Skip channels for outgoing connections and channels that were not +=09 * deferred. FLAG_DEFER_SETUP and ident are cleared only after the +=09 * response is queued (see __l2cap_ecred_conn_rsp_defer()), so that a +=09 * concurrent l2cap_chan_send() cannot transmit ahead of it. =09 */ -=09if (test_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags) || -=09 !test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) +=09if (!l2cap_chan_ecred_rsp_pending(chan)) =09=09return; =20 =09lockdep_assert_held(&rsp->locked_chan->lock); @@ -4137,18 +4507,28 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan= *chan, void *data) =09lockdep_assert_held(&chan->lock); =09lockdep_assert_held(&chan->conn->lock); =20 -=09/* Reset ident so only one response is sent */ -=09chan->ident =3D 0; - =09/* Unreachable, check in l2cap_ecred_conn_req. If reached, drop rest */ -=09if (WARN_ON_ONCE(rsp->count >=3D ARRAY_SIZE(rsp->pdu.scid))) +=09if (WARN_ON_ONCE(rsp->count + rsp->rejected_count >=3D +=09=09=09 ARRAY_SIZE(rsp->pdu.scid))) { =09=09rsp->pdu.rsp.result =3D cpu_to_le16(L2CAP_CR_LE_NO_MEM); +=09=09goto done; +=09} =20 =09/* Include all channels pending with the same ident */ -=09if (!rsp->pdu.rsp.result) +=09if (!rsp->pdu.rsp.result) { +=09=09rsp->accepted[rsp->count] =3D chan; +=09=09l2cap_chan_hold(chan); =09=09rsp_flex->dcid[rsp->count++] =3D cpu_to_le16(chan->scid); -=09else -=09=09l2cap_chan_del(chan, ECONNRESET); +=09} else { +=09=09/* Keep data blocked after consuming the response identifier. */ +=09=09chan->state =3D BT_DISCONN; +=09=09clear_bit(FLAG_DEFER_SETUP, &chan->flags); +=09=09chan->ident =3D 0; +=09=09if (rsp->security_failed) +=09=09=09set_bit(FLAG_SECURITY_FAILED, &chan->flags); +=09=09rsp->rejected[rsp->rejected_count++] =3D chan; +=09=09l2cap_chan_hold(chan); +=09} =20 done: =09if (locked) @@ -4157,23 +4537,31 @@ static void l2cap_ecred_rsp_defer(struct l2cap_chan= *chan, void *data) =09l2cap_chan_put(chan); } =20 -void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) +int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan) { =09struct l2cap_conn *conn =3D chan->conn; =09struct l2cap_ecred_rsp_data data; +=09struct l2cap_chan *sibling; =09u16 id =3D chan->ident; =09int result =3D 0; +=09int i; =20 =09if (!id) -=09=09return; +=09=09return 0; =09if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) -=09=09return; +=09=09return 0; =20 =09BT_DBG("chan %p id %d", chan, id); =20 =09memset(&data, 0, sizeof(data)); =20 =09data.locked_chan =3D chan; +=09list_for_each_entry(sibling, &conn->chan_l, list) { +=09=09if (sibling->ident =3D=3D id && +=09=09 l2cap_chan_ecred_rsp_pending(sibling) && +=09=09 l2cap_chan_security_failed(sibling)) +=09=09=09data.security_failed =3D true; +=09} =20 =09data.pdu.rsp.mtu =3D cpu_to_le16(chan->imtu); =09data.pdu.rsp.mps =3D cpu_to_le16(chan->mps); @@ -4184,7 +4572,7 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *= chan) =09__l2cap_chan_list_id(conn, id, l2cap_ecred_list_defer, &result); =20 =09if (result > 0) -=09=09return; +=09=09return 0; =20 =09if (result < 0) =09=09data.pdu.rsp.result =3D cpu_to_le16(L2CAP_CR_LE_AUTHORIZATION); @@ -4192,9 +4580,55 @@ void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan = *chan) =09/* Build response */ =09__l2cap_chan_list_id(conn, id, l2cap_ecred_rsp_defer, &data); =20 +=09/* Consume the entire rejected batch before any teardown callback can +=09 * revisit a sibling and send another response for the same ident. +=09 */ +=09for (i =3D 0; i < data.rejected_count; i++) { +=09=09struct l2cap_chan *rejected =3D data.rejected[i]; +=09=09bool locked =3D l2cap_chan_try_sibling_lock(rejected, chan); +=09=09int err =3D data.security_failed ? ECONNREFUSED : ECONNRESET; + +=09=09if (locked) { +=09=09=09lockdep_assert_held(&rejected->conn->lock); +=09=09=09l2cap_chan_del(rejected, err); +=09=09=09rejected->ops->close(rejected); +=09=09=09l2cap_chan_unlock(rejected); +=09=09} else { +=09=09=09l2cap_chan_del(chan, err); +=09=09=09chan->ops->close(chan); +=09=09} +=09=09l2cap_chan_put(rejected); +=09} + =09l2cap_send_cmd(conn, id, L2CAP_ECRED_CONN_RSP, =09=09 sizeof(data.pdu.rsp) + (data.count * sizeof(__le16)), =09=09 &data.pdu); + +=09/* Data must follow the connection response on the wire. */ +=09for (i =3D 0; i < data.count; i++) { +=09=09struct l2cap_chan *accepted =3D data.accepted[i]; +=09=09bool locked =3D l2cap_chan_try_sibling_lock(accepted, chan); + +=09=09/* The response is queued: open the data gate and make later +=09=09 * calls for this batch return early. Both are done under the +=09=09 * channel lock, so no sender sees one without the other. +=09=09 */ +=09=09if (locked) { +=09=09=09clear_bit(FLAG_DEFER_SETUP, &accepted->flags); +=09=09=09accepted->ident =3D 0; +=09=09=09if (!test_bit(FLAG_DEL, &accepted->flags)) +=09=09=09=09l2cap_le_flowctl_resume(accepted); +=09=09=09l2cap_chan_unlock(accepted); +=09=09} else { +=09=09=09clear_bit(FLAG_DEFER_SETUP, &chan->flags); +=09=09=09chan->ident =3D 0; +=09=09=09if (!test_bit(FLAG_DEL, &chan->flags)) +=09=09=09=09l2cap_le_flowctl_resume(chan); +=09=09} +=09=09l2cap_chan_put(accepted); +=09} + +=09return data.security_failed ? -ECONNREFUSED : 0; } =20 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) @@ -4895,9 +5329,18 @@ static inline int l2cap_disconnect_req(struct l2cap_= conn *conn, =20 =09chan->ops->set_shutdown(chan); =20 -=09l2cap_chan_del(chan, ECONNRESET); +=09/* The peer withdrew one child of an unanswered deferred ECRED batch. +=09 * Answer the whole batch like a local close does, so an already accept= ed +=09 * sibling is not left waiting for a response that is never sent. This +=09 * deletes and closes @chan. +=09 */ +=09if (l2cap_chan_ecred_rsp_pending(chan)) +=09=09l2cap_chan_ecred_connect_reject(chan); =20 -=09chan->ops->close(chan); +=09if (!test_bit(FLAG_DEL, &chan->flags)) { +=09=09l2cap_chan_del(chan, ECONNRESET); +=09=09chan->ops->close(chan); +=09} =20 =09l2cap_chan_unlock(chan); =09l2cap_chan_put(chan); @@ -5488,10 +5931,7 @@ static inline int l2cap_le_credits(struct l2cap_conn= *conn, =09chan->tx_credits +=3D credits; =20 =09/* Resume sending */ -=09l2cap_le_flowctl_send(chan); - -=09if (chan->tx_credits) -=09=09chan->ops->resume(chan); +=09l2cap_le_flowctl_resume(chan); =20 unlock: =09l2cap_chan_unlock(chan); @@ -5525,7 +5965,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_c= onn *conn, =09} =20 =09/* Check if there are no pending channels with the same ident */ -=09__l2cap_chan_list_id(conn, cmd->ident, l2cap_ecred_list_defer, +=09__l2cap_chan_list_id(conn, cmd->ident, l2cap_ecred_list_ident_busy, =09=09=09 &num_scid); =09if (num_scid) { =09=09result =3D L2CAP_CR_LE_INVALID_PARAMS; @@ -7644,6 +8084,9 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_c= onn *hcon) =20 =09skb_queue_head_init(&conn->pending_rx); =09INIT_WORK(&conn->pending_rx_work, process_pending_rx); +=09INIT_WORK(&conn->security_work, l2cap_security_work); +=09atomic_set(&conn->security_failure, 0); +=09atomic_set(&conn->security_recheck, 0); =09INIT_DELAYED_WORK(&conn->id_addr_timer, l2cap_conn_update_id_addr); =20 =09conn->disc_reason =3D HCI_ERROR_REMOTE_USER_TERM; @@ -7907,6 +8350,10 @@ static void l2cap_ecred_reconfigure(struct l2cap_cha= n *chan) =20 int l2cap_chan_reconfigure(struct l2cap_chan *chan, __u16 mtu) { +=09/* ident and FLAG_DEFER_SETUP still identify an unsent ECRED response. = */ +=09if (l2cap_chan_ecred_rsp_pending(chan)) +=09=09return -EBUSY; + =09if (chan->imtu > mtu) =09=09return -EINVAL; =20 @@ -8110,6 +8557,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon,= u8 status, u8 encrypt) =09mutex_lock(&conn->lock); =20 =09list_for_each_entry(chan, &conn->chan_l, list) { +=09=09bool le_flowctl; + =09=09l2cap_chan_lock(chan); =20 =09=09lockdep_assert_held(&chan->conn->lock); @@ -8117,7 +8566,10 @@ static void l2cap_security_cfm(struct hci_conn *hcon= , u8 status, u8 encrypt) =09=09BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, =09=09 state_to_string(chan->state)); =20 -=09=09if (!status && encrypt) +=09=09le_flowctl =3D hcon->type =3D=3D LE_LINK && +=09=09=09 (chan->mode =3D=3D L2CAP_MODE_LE_FLOWCTL || +=09=09=09 chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL); +=09=09if (!status && encrypt && !le_flowctl) =09=09=09chan->sec_level =3D hcon->sec_level; =20 =09=09if (!__l2cap_no_conn_pending(chan)) { @@ -8125,6 +8577,34 @@ static void l2cap_security_cfm(struct hci_conn *hcon= , u8 status, u8 encrypt) =09=09=09continue; =09=09} =20 +=09=09if (le_flowctl && (chan->state =3D=3D BT_CONNECTED || +=09=09=09=09 chan->state =3D=3D BT_CONNECT2)) { +=09=09=09bool pending =3D test_bit(FLAG_PENDING_SECURITY, &chan->flags); + +=09=09=09/* Authentication may precede encryption, and encryption may +=09=09=09 * precede SMP key distribution. Stay pending; +=09=09=09 * l2cap_security_recheck() or l2cap_security_failure() queues +=09=09=09 * the work when pairing ends. +=09=09=09 */ +=09=09=09if (pending && !status && l2cap_chan_security_busy(chan)) { +=09=09=09=09l2cap_chan_unlock(chan); +=09=09=09=09continue; +=09=09=09} +=09=09=09if ((status && pending) || +=09=09=09 !l2cap_chan_security_sufficient(chan)) { +=09=09=09=09set_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09=09=09=09set_bit(FLAG_SECURITY_FAILED, &chan->flags); +=09=09=09} +=09=09=09if (test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { +=09=09=09=09set_bit(FLAG_SECURITY_CFM, &chan->flags); +=09=09=09=09l2cap_queue_security_work(conn); +=09=09=09} else if (!status && chan->state =3D=3D BT_CONNECTED) { +=09=09=09=09l2cap_le_flowctl_resume(chan); +=09=09=09} +=09=09=09l2cap_chan_unlock(chan); +=09=09=09continue; +=09=09} + =09=09if (!status && (chan->state =3D=3D BT_CONNECTED || =09=09=09=09chan->state =3D=3D BT_CONFIG)) { =09=09=09chan->ops->resume(chan); @@ -8183,6 +8663,9 @@ static void l2cap_security_cfm(struct hci_conn *hcon,= u8 status, u8 encrypt) =09=09l2cap_chan_unlock(chan); =09} =20 +=09if (conn->security_restore) +=09=09l2cap_queue_security_work(conn); + =09mutex_unlock(&conn->lock); =20 =09/* 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) =09return 0; } =20 +static int l2cap_sock_set_security(struct sock *sk, struct l2cap_chan *cha= n, +=09=09=09=09 const struct bt_security *sec) +{ +=09struct l2cap_conn *conn =3D l2cap_chan_conn(chan); +=09int err =3D 0; + +=09lock_sock(sk); +=09if (conn && conn->hcon->type =3D=3D LE_LINK && +=09 (chan->mode =3D=3D L2CAP_MODE_LE_FLOWCTL || +=09 chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL)) { +=09=09if (sk->sk_state !=3D BT_CONNECTED && +=09=09 !(sk->sk_state =3D=3D BT_CONNECT2 && +=09=09 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { +=09=09=09release_sock(sk); +=09=09=09return -EINVAL; +=09=09} +=09=09release_sock(sk); +=09=09err =3D l2cap_chan_set_security(chan, sec->level, sec->mic_len); +=09=09/* The direct return reports it; drop teardown's socket copy. */ +=09=09if (err =3D=3D -ECONNREFUSED) +=09=09=09cmpxchg(&sk->sk_err, ECONNREFUSED, 0); +=09=09return err; +=09} + +=09chan->sec_level =3D sec->level; +=09chan->mic_len =3D sec->mic_len; +=09if (!conn) +=09=09goto done; +=09if (chan->scid =3D=3D L2CAP_CID_ATT) { +=09=09release_sock(sk); +=09=09l2cap_chan_req_mic_len(chan, conn->hcon); +=09=09if (smp_conn_security(conn->hcon, sec->level)) +=09=09=09return -EINVAL; +=09=09lock_sock(sk); +=09=09set_bit(FLAG_PENDING_SECURITY, &chan->flags); +=09=09set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); +=09=09sk->sk_state =3D BT_CONFIG; +=09=09chan->state =3D BT_CONFIG; +=09} else if ((sk->sk_state =3D=3D BT_CONNECT2 && +=09=09 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || +=09=09 sk->sk_state =3D=3D BT_CONNECTED) { +=09=09bool ready; + +=09=09release_sock(sk); +=09=09ready =3D l2cap_chan_check_security(chan, true); +=09=09lock_sock(sk); +=09=09if (!ready) +=09=09=09set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); +=09=09else +=09=09=09sk->sk_state_change(sk); +=09} else { +=09=09err =3D -EINVAL; +=09} +done: +=09release_sock(sk); +=09return err; +} + static int l2cap_sock_setsockopt(struct socket *sock, int level, int optna= me, =09=09=09=09 sockptr_t optval, unsigned int optlen) { =09struct sock *sk =3D sock->sk; =09struct l2cap_chan *chan =3D l2cap_pi(sk)->chan; -=09struct bt_security sec; =09struct bt_power pwr; =09struct l2cap_conn *conn; =09int err =3D 0; @@ -950,78 +1007,59 @@ static int l2cap_sock_setsockopt(struct socket *sock= , int level, int optname, =09if (level !=3D SOL_BLUETOOTH) =09=09return -ENOPROTOOPT; =20 -=09l2cap_chan_lock(chan); -=09lock_sock(sk); - -=09switch (optname) { -=09case BT_SECURITY: -=09=09if (chan->chan_type !=3D L2CAP_CHAN_CONN_ORIENTED && -=09=09 chan->chan_type !=3D L2CAP_CHAN_FIXED && -=09=09 chan->chan_type !=3D L2CAP_CHAN_RAW) { -=09=09=09err =3D -EINVAL; -=09=09=09break; -=09=09} +=09if (optname =3D=3D BT_SECURITY) { +=09=09struct bt_security sec =3D { .level =3D BT_SECURITY_LOW }; +=09=09bool have_conn; =20 -=09=09/* mic_len was added at the end of struct bt_security, so -=09=09 * accept the shorter struct as well, leaving mic_len unset. -=09=09 */ -=09=09if (optlen < offsetofend(struct bt_security, key_size)) { -=09=09=09err =3D -EINVAL; -=09=09=09break; -=09=09} +=09=09/* Keep accepting the struct size used before mic_len was added. */ +=09=09if (optlen < offsetofend(struct bt_security, key_size)) +=09=09=09return -EINVAL; =20 -=09=09err =3D copy_struct_from_sockptr(&sec, sizeof(sec), optval, -=09=09=09=09=09 optlen); +=09=09err =3D copy_struct_from_sockptr(&sec, sizeof(sec), optval, optlen); =09=09if (err) -=09=09=09break; - -=09=09if (sec.level < BT_SECURITY_LOW || -=09=09 sec.level > BT_SECURITY_FIPS) { -=09=09=09err =3D -EINVAL; -=09=09=09break; -=09=09} +=09=09=09return err; =20 -=09=09/* BLUETOOTH CORE SPECIFICATION Version 7.0 | Vol 4, -=09=09 * Part E, Section 7.7.8: MIC_Length 0x00 is 32 bits, -=09=09 * 0x01 is 64 bits and 0x02 is 128 bits; all other -=09=09 * values are reserved for future use. -=09=09 */ -=09=09if (sec.mic_len > 0x02) { -=09=09=09err =3D -EINVAL; -=09=09=09break; -=09=09} +=09=09if (sec.level < BT_SECURITY_LOW || sec.level > BT_SECURITY_FIPS) +=09=09=09return -EINVAL; =20 -=09=09chan->sec_level =3D sec.level; -=09=09chan->mic_len =3D sec.mic_len; +=09=09/* MIC length encodings 0, 1 and 2 mean 32, 64 and 128 bits. */ +=09=09if (sec.mic_len > 0x02) +=09=09=09return -EINVAL; =20 +=09=09l2cap_chan_lock(chan); +=09=09if (chan->chan_type !=3D L2CAP_CHAN_CONN_ORIENTED && +=09=09 chan->chan_type !=3D L2CAP_CHAN_FIXED && +=09=09 chan->chan_type !=3D L2CAP_CHAN_RAW) { +=09=09=09l2cap_chan_unlock(chan); +=09=09=09return -EINVAL; +=09=09} =09=09conn =3D l2cap_chan_conn(chan); -=09=09if (!conn) -=09=09=09break; +=09=09if (!conn) { +=09=09=09err =3D l2cap_sock_set_security(sk, chan, &sec); +=09=09=09l2cap_chan_unlock(chan); +=09=09=09return err; +=09=09} +=09=09l2cap_conn_get(conn); +=09=09l2cap_chan_unlock(chan); =20 -=09=09/* change security for LE channels */ -=09=09if (chan->scid =3D=3D L2CAP_CID_ATT) { -=09=09=09l2cap_chan_req_mic_len(chan, conn->hcon); -=09=09=09if (smp_conn_security(conn->hcon, sec.level)) { -=09=09=09=09err =3D -EINVAL; -=09=09=09=09break; -=09=09=09} +=09=09/* SMP receive holds conn->lock; management holds hdev->lock. */ +=09=09hci_dev_lock(conn->hcon->hdev); +=09=09have_conn =3D l2cap_chan_lock_conn(chan); =20 -=09=09=09set_bit(FLAG_PENDING_SECURITY, &chan->flags); -=09=09=09sk->sk_state =3D BT_CONFIG; -=09=09=09chan->state =3D BT_CONFIG; +=09=09if (l2cap_chan_conn(chan) !=3D conn) +=09=09=09err =3D -ENOTCONN; +=09=09else +=09=09=09err =3D l2cap_sock_set_security(sk, chan, &sec); +=09=09l2cap_chan_unlock_conn(chan, have_conn); +=09=09hci_dev_unlock(conn->hcon->hdev); +=09=09l2cap_conn_put(conn); +=09=09return err; +=09} =20 -=09=09/* or for ACL link */ -=09=09} else if ((sk->sk_state =3D=3D BT_CONNECT2 && -=09=09=09 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) || -=09=09=09 sk->sk_state =3D=3D BT_CONNECTED) { -=09=09=09if (!l2cap_chan_check_security(chan, true)) -=09=09=09=09set_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); -=09=09=09else -=09=09=09=09sk->sk_state_change(sk); -=09=09} else { -=09=09=09err =3D -EINVAL; -=09=09} -=09=09break; +=09l2cap_chan_lock(chan); +=09lock_sock(sk); + +=09switch (optname) { =20 =09case BT_DEFER_SETUP: =09=09if (sk->sk_state !=3D BT_BOUND && sk->sk_state !=3D BT_LISTEN) { @@ -1284,11 +1322,23 @@ static int l2cap_sock_defer(struct sock *sk) =09=09if (!have_conn) { =09=09=09release_sock(sk); =09=09=09err =3D -ENOTCONN; +=09=09} else if ((chan->mode =3D=3D L2CAP_MODE_LE_FLOWCTL || +=09=09=09 chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL) && +=09=09=09 test_bit(FLAG_PENDING_SECURITY, &chan->flags)) { +=09=09=09release_sock(sk); +=09=09=09err =3D -EAGAIN; =09=09} else if (chan->mode =3D=3D L2CAP_MODE_EXT_FLOWCTL) { +=09=09=09int result; + =09=09=09sk->sk_state =3D BT_CONNECTED; =09=09=09chan->state =3D BT_CONNECTED; =09=09=09release_sock(sk); -=09=09=09__l2cap_ecred_conn_rsp_defer(chan); +=09=09=09result =3D __l2cap_ecred_conn_rsp_defer(chan); +=09=09=09if (result) { +=09=09=09=09err =3D result; +=09=09=09=09if (result =3D=3D -ECONNREFUSED) +=09=09=09=09=09cmpxchg(&sk->sk_err, ECONNREFUSED, 0); +=09=09=09} =09=09} else if (bdaddr_type_is_le(chan->src_type)) { =09=09=09sk->sk_state =3D BT_CONNECTED; =09=09=09chan->state =3D BT_CONNECTED; @@ -1778,7 +1828,10 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan= *chan, int err) =09=09sk->sk_state =3D BT_CLOSED; =09=09chan->state =3D BT_CLOSED; =20 -=09=09sk->sk_err =3D err; +=09=09if (!test_bit(FLAG_SECURITY_FAILED, &chan->flags) || +=09=09 READ_ONCE(sk->sk_err) !=3D ECONNREFUSED) +=09=09=09WRITE_ONCE(sk->sk_err, err); +=09=09clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); =20 =09=09if (parent) { =09=09=09/* @@ -1814,8 +1867,11 @@ static void l2cap_sock_state_change_cb(struct l2cap_= chan *chan, int state, =20 =09sk->sk_state =3D state; =20 -=09if (err) -=09=09sk->sk_err =3D err; +=09if (err) { +=09=09WRITE_ONCE(sk->sk_err, err); +=09=09clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); +=09=09sk->sk_state_change(sk); +=09} =20 =09release_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 rea= son) =09=09=09 &reason); =20 =09mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); +=09l2cap_security_failure(conn); =20 =09if (chan->data) =09=09smp_chan_destroy(conn); @@ -1365,8 +1366,8 @@ static void smp_distribute_keys(struct smp_chan *smp) =20 =09set_bit(SMP_FLAG_COMPLETE, &smp->flags); =09smp_notify_keys(conn); - =09smp_chan_destroy(conn); +=09l2cap_security_recheck(conn); } =20 static void smp_timeout(struct work_struct *work)