Skip to contentSkip to conversation

Bluetooth: L2CAP: Restrict LE credits to connected CoCs

PATCH Superseded Kyle Pepper 2 participants 7 messages Nov 13 – Nov 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v10 1/2 Bluetooth: L2CAP: Restrict LE credits to connected CoCs

Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM

---
Hi Luiz,

I split the credit validation into its own patch and reworked the security handling to use the existing HCI workqueue. There is no new workqueue or pairing timeout. The original l2cap-tester and smp-tester now pass without changing their expectations.

I also checked failure handling, repeated security
requests, key refresh and deferred LE/ECRED setup so fixing that bypass doesn't leave sockets stuck.

Hopefully this brings the patch to a more usable state.

Thanks,
Kyle

v10:
- Separate malformed-credit validation from security request completion.
- Use the existing HCI workqueue and SMP/HCI timeouts.
- Keep pending upgrades gated through failure, refresh and deferred setup.

Previous v9: https://lore.kernel.org/linux-bluetooth/[email protected]/
Threading uses the Message-ID from sent mail; the archive lookup is not yet available. Luiz's reply Message-ID was not supplied.

Validation: original l2cap-tester 111/111 and smp-tester 8/8; 27 private native cases; GCC/Clang/ECRED-off W=1 builds; 42 modeled cases per compiler with ASan/UBSan and 10 expected mutation failures. Guest tests use emulated controllers and checked module identities. Physical hardware, PTS, KCSAN and remote CI were not run. Private fixtures and traces are not included.

 net/bluetooth/l2cap_core.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/l2cap_core.c +5 -0
index 09ff32394..4e8aec98e 100644
@@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
 
 	l2cap_chan_lock(chan);
 
+	if (chan->state != BT_CONNECTED ||
+	    (chan->mode != L2CAP_MODE_LE_FLOWCTL &&
+	     chan->mode != L2CAP_MODE_EXT_FLOWCTL))
+		goto unlock;
+
 	max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
 	if (credits > max_credits) {
 		BT_ERR("LE credits overflow");

base-commit: da3cc02587c629420603df4395179ec82955c941

#1 lore raw

PATCH v10 2/2 Bluetooth: L2CAP: Keep credits from completing security upgrades

A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met.

Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, and fail pending requests even if a later confirmation would otherwise satisfy them. Use the existing pairing and HCI timeouts without a new request deadline.

Drain retained data when security completes or the requested requirement is lowered to an achieved level. Return EACCES and restore the old request when SMP starts no procedure and the requirements remain unmet. Preserve each CoC's requested level across shared-link confirmations.

Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data and reject the batch on security failure. Preserve ordinary deferred rejection behavior.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM
---
 include/net/bluetooth/l2cap.h |   7 +
 net/bluetooth/l2cap_core.c    | 295 ++++++++++++++++++++++++++++++++--
 net/bluetooth/l2cap_sock.c    | 169 +++++++++++--------
 net/bluetooth/smp.c           |   1 +
 4 files changed, 394 insertions(+), 78 deletions(-)
include/net/bluetooth/l2cap.h +7 -0
index cf5c2df68..7b95bd407 100644
@@ -604,6 +604,7 @@ struct l2cap_chan {
 	__u32		remote_flush_to;
 
 	struct delayed_work	chan_timer;
+	struct list_head	security_list;
 	struct delayed_work	retrans_timer;
 	struct delayed_work	monitor_timer;
 	struct delayed_work	ack_timer;
@@ -644,6 +645,8 @@ struct l2cap_conn {
 
 	struct sk_buff_head	pending_rx;
 	struct work_struct	pending_rx_work;
+	struct work_struct	security_work;
+	atomic_t		security_failure;
 	bool			pending_rx_blocked __guarded_by(&lock);
 
 	struct delayed_work	id_addr_timer;
@@ -767,6 +770,8 @@ enum {
 	FLAG_ECRED_CONN_REQ_SENT,
 	FLAG_ECRED_RECONF_REQ_SENT,
 	FLAG_PENDING_SECURITY,
+	FLAG_SECURITY_CFM,
+	FLAG_SECURITY_FAILED,
 	FLAG_HOLD_HCI_CONN,
 	FLAG_DEL,
 };
@@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
 void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon);
 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
+int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len);
+void l2cap_security_failure(struct l2cap_conn *conn);
 void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);
 int l2cap_ertm_init(struct l2cap_chan *chan);
 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
net/bluetooth/l2cap_core.c +282 -13

412 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +104 -65

215 lines not on this page · raw message

net/bluetooth/smp.c +1 -0
index f8f86b8f7..84d85613c 100644
@@ -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);
#2 lore raw

PATCH v11 1/2 Bluetooth: L2CAP: Restrict LE credits to connected CoCs

In reply to Kyle Pepper

Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM

---
Changes in v11:
- Purge retained deferred CoC data before incomplete-configuration teardown.
- Clear pending work flags on close and recheck channel state in the worker.
- Return EBUSY for overlapping SMP/encryption requests, including key distribution; restore pending_sec_level when rejecting an idle request.
- Complete ECRED child cleanup and propagate deferred security rejection.
- Preserve ordinary accept/close behavior and socket/SMP lock ordering.

Previous v10 (this series has no cover letter):
https://lore.kernel.org/linux-bluetooth/[email protected]/

 net/bluetooth/l2cap_core.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/l2cap_core.c +5 -0
index 09ff32394..4e8aec98e 100644
@@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
 
 	l2cap_chan_lock(chan);
 
+	if (chan->state != BT_CONNECTED ||
+	    (chan->mode != L2CAP_MODE_LE_FLOWCTL &&
+	     chan->mode != L2CAP_MODE_EXT_FLOWCTL))
+		goto unlock;
+
 	max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
 	if (credits > max_credits) {
 		BT_ERR("LE credits overflow");

base-commit: da3cc02587c629420603df4395179ec82955c941

#3 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1210580/

---Test result---

Test Summary:

CheckPatch                    FAIL      0.65 seconds
VerifyFixes                   PASS      0.09 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.26 seconds
SubjectPrefix                 PASS      0.08 seconds
BuildKernel                   PASS      30.81 seconds
CheckAllWarning               PASS      34.66 seconds
CheckSparse                   PASS      37.43 seconds
BuildKernel32                 PASS      30.52 seconds
CheckKernelLLVM               PASS      34.69 seconds
TestRunnerSetup               PASS      752.68 seconds
TestRunner_l2cap-tester       PASS      14.88 seconds
IncrementalBuild              PASS      28.59 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v11,1/2] Bluetooth: L2CAP: Restrict LE credits to connected CoCs
WARNING: Non-standard signature: Assisted-by:
#89:

Assisted-by LLM

ERROR: Unrecognized email address: 'LLM'
#89:

Assisted-by LLM

total: 1 errors, 1 warnings, 0 checks, 11 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14980058.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

https://github.com/bluez/bluetooth-next/pull/1127

---
Regards,
Linux Bluetooth

#4 lore raw

PATCH v12 1/2 Bluetooth: L2CAP: Restrict LE credits to connected CoCs

In reply to Kyle Pepper

Do not let credit indications change fixed channels or complete pending connection setup. Validate the channel state and mode before accounting credits, checking overflow or invoking its resume callback.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM

---
Changes in v12:
- Reject the deferred ECRED batch when an accepted child closes before the connection response; close all siblings instead of disconnecting early.
- Patch 1 is unchanged.

Previous v11 (no cover letter):
https://lore.kernel.org/linux-bluetooth/4278a9caf4dc8e949092452c141a5be465ec77e1.1794550328.git.github.filter194@slmail.me/

Review:
https://sashiko.dev/#/patchset/4278a9caf4dc8e949092452c141a5be465ec77e1.1794550328.git.github.filter194%40slmail.me

 net/bluetooth/l2cap_core.c | 5 +++++
 1 file changed, 5 insertions(+)
net/bluetooth/l2cap_core.c +5 -0
index 09ff32394..4e8aec98e 100644
@@ -5464,6 +5464,11 @@ static inline int l2cap_le_credits(struct l2cap_conn *conn,
 
 	l2cap_chan_lock(chan);
 
+	if (chan->state != BT_CONNECTED ||
+	    (chan->mode != L2CAP_MODE_LE_FLOWCTL &&
+	     chan->mode != L2CAP_MODE_EXT_FLOWCTL))
+		goto unlock;
+
 	max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
 	if (credits > max_credits) {
 		BT_ERR("LE credits overflow");

base-commit: da3cc02587c629420603df4395179ec82955c941

#5 lore raw

PATCH v12 2/2 Bluetooth: L2CAP: Keep credits from completing security upgrades

In reply to Kyle Pepper

A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met.

Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline.

Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations.

Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them.

Reject an incoming deferred ECRED batch when an accepted child closes before the connection response, so its siblings cannot stay suspended.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM
---
 include/net/bluetooth/l2cap.h |   9 +-
 net/bluetooth/l2cap_core.c    | 333 +++++++++++++++++++++++++++++++---
 net/bluetooth/l2cap_sock.c    | 181 +++++++++++-------
 net/bluetooth/smp.c           |   1 +
 4 files changed, 435 insertions(+), 89 deletions(-)
include/net/bluetooth/l2cap.h +8 -1
index cf5c2df68..b004cc643 100644
@@ -604,6 +604,7 @@ struct l2cap_chan {
 	__u32		remote_flush_to;
 
 	struct delayed_work	chan_timer;
+	struct list_head	security_list;
 	struct delayed_work	retrans_timer;
 	struct delayed_work	monitor_timer;
 	struct delayed_work	ack_timer;
@@ -644,6 +645,8 @@ struct l2cap_conn {
 
 	struct sk_buff_head	pending_rx;
 	struct work_struct	pending_rx_work;
+	struct work_struct	security_work;
+	atomic_t		security_failure;
 	bool			pending_rx_blocked __guarded_by(&lock);
 
 	struct delayed_work	id_addr_timer;
@@ -767,6 +770,8 @@ enum {
 	FLAG_ECRED_CONN_REQ_SENT,
 	FLAG_ECRED_RECONF_REQ_SENT,
 	FLAG_PENDING_SECURITY,
+	FLAG_SECURITY_CFM,
+	FLAG_SECURITY_FAILED,
 	FLAG_HOLD_HCI_CONN,
 	FLAG_DEL,
 };
@@ -974,7 +979,7 @@ void l2cap_cleanup_sockets(void);
 bool l2cap_is_socket(struct socket *sock);
 
 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
-void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
+int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
 	__must_hold(&chan->lock) __must_hold(&chan->conn->lock);
 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
 
@@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
 void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon);
 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
+int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len);
+void l2cap_security_failure(struct l2cap_conn *conn);
 void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);
 int l2cap_ertm_init(struct l2cap_chan *chan);
 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
net/bluetooth/l2cap_core.c +311 -22

486 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +115 -66

231 lines not on this page · raw message

net/bluetooth/smp.c +1 -0
index f8f86b8f7..84d85613c 100644
@@ -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);
#6 lore raw

PATCH v11 2/2 Bluetooth: L2CAP: Keep credits from completing security upgrades

In reply to Kyle Pepper

A credit indication can drain queued CoC data and clear socket suspension while BT_SECURITY is waiting for stronger security. Keep the request pending until the required encryption, level, key size and MIC are met.

Finish requests on the existing HCI workqueue, outside the confirmation list walk. Hold selected channels before callbacks can remove siblings. Latch SMP failure before admitting a fresh request, clear pending flags on close, and recheck channel state before processing deferred confirmations. Use the existing pairing and HCI timeouts without a new request deadline.

Reject overlapping requests with EBUSY while pairing, key distribution or encryption is in progress. This includes repeated and lowered requests; applications can retry after the current procedure completes. Return EACCES when SMP starts no procedure and the requirements remain unmet, restoring the channel requirements and both pending link requirements. Preserve each CoC's requested level across shared-link confirmations.

Keep deferred reads pending during an upgrade. Send an ECRED response before releasing accepted children's data, let rejection override pending siblings, and close every deleted child. Return the batch's security error to a deferred reader while preserving ordinary accept/close behavior. Purge retained CoC data even if deferred acceptance left configuration incomplete. Acquire the SMP channel lock without holding the socket lock, and suspend pending ATT sockets until their confirmation resumes them.

Fixes fad5fc89594d ("Bluetooth: Add LE flow control discipline")
Assisted-by LLM
---
 include/net/bluetooth/l2cap.h |   9 +-
 net/bluetooth/l2cap_core.c    | 326 +++++++++++++++++++++++++++++++---
 net/bluetooth/l2cap_sock.c    | 181 ++++++++++++-------
 net/bluetooth/smp.c           |   1 +
 4 files changed, 428 insertions(+), 89 deletions(-)
include/net/bluetooth/l2cap.h +8 -1
index cf5c2df68..b004cc643 100644
@@ -604,6 +604,7 @@ struct l2cap_chan {
 	__u32		remote_flush_to;
 
 	struct delayed_work	chan_timer;
+	struct list_head	security_list;
 	struct delayed_work	retrans_timer;
 	struct delayed_work	monitor_timer;
 	struct delayed_work	ack_timer;
@@ -644,6 +645,8 @@ struct l2cap_conn {
 
 	struct sk_buff_head	pending_rx;
 	struct work_struct	pending_rx_work;
+	struct work_struct	security_work;
+	atomic_t		security_failure;
 	bool			pending_rx_blocked __guarded_by(&lock);
 
 	struct delayed_work	id_addr_timer;
@@ -767,6 +770,8 @@ enum {
 	FLAG_ECRED_CONN_REQ_SENT,
 	FLAG_ECRED_RECONF_REQ_SENT,
 	FLAG_PENDING_SECURITY,
+	FLAG_SECURITY_CFM,
+	FLAG_SECURITY_FAILED,
 	FLAG_HOLD_HCI_CONN,
 	FLAG_DEL,
 };
@@ -974,7 +979,7 @@ void l2cap_cleanup_sockets(void);
 bool l2cap_is_socket(struct socket *sock);
 
 void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan);
-void __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
+int __l2cap_ecred_conn_rsp_defer(struct l2cap_chan *chan)
 	__must_hold(&chan->lock) __must_hold(&chan->conn->lock);
 void __l2cap_connect_rsp_defer(struct l2cap_chan *chan);
 
@@ -993,6 +998,8 @@ void l2cap_chan_busy(struct l2cap_chan *chan, int busy);
 void l2cap_chan_rx_avail(struct l2cap_chan *chan, ssize_t rx_avail);
 void l2cap_chan_req_mic_len(struct l2cap_chan *chan, struct hci_conn *hcon);
 int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator);
+int l2cap_chan_set_security(struct l2cap_chan *chan, u8 level, u8 mic_len);
+void l2cap_security_failure(struct l2cap_conn *conn);
 void l2cap_chan_set_defaults(struct l2cap_chan *chan, struct l2cap_chan *pchan);
 int l2cap_ertm_init(struct l2cap_chan *chan);
 void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan);
net/bluetooth/l2cap_core.c +304 -22

473 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +115 -66

231 lines not on this page · raw message

net/bluetooth/smp.c +1 -0
index f8f86b8f7..84d85613c 100644
@@ -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);