Bluetooth: 6LoWPAN lifecycle fixes
Subsystems (3)
cover Bluetooth: 6LoWPAN lifecycle fixes
6LoWPAN shares L2CAP channels between connection setup, published peers and packet transmission, while teardown also schedules asynchronous cleanup. These paths currently disagree on when channel ownership, peer visibility and module lifetime end. The resulting failures range from leaked or unusable connections and peers surviving disable to transmit use-after-free and execution of unloaded module code.
This series establishes a consistent ownership and teardown order across those paths. Shared L2CAP and workqueue changes provide the lifetime guarantees needed by the following 6LoWPAN changes, which make failed setup recoverable and stop users of a peer before releasing its resources. Keeping the changes together avoids fixing one lifetime while shortening another.
The timer support adds one ordered workqueue per L2CAP connection and serializes that connection's channel timers.
PATCH 1/5 Bluetooth: L2CAP: ignore close requests for deleted channels
l2cap_chan_del() keeps chan->conn alive until channel destruction and marks removal with FLAG_DEL, but does not necessarily change the state. A caller retaining a temporary reference can therefore enter l2cap_chan_close_unlocked() after deletion and follow a stale connected state, rearming the channel timer and sending another disconnection request for a channel no longer on the connection list.
Check FLAG_DEL after taking the channel and connection locks so that concurrent deletion is serialized with the decision to close. Return without further timer or signaling work when the channel is deleted.
--- net/bluetooth/l2cap_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_core.c +4 -2
@@ -939,8 +939,10 @@ void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) have_conn =l2cap_chan_lock_conn (chan); -/* Context analysis: consider chan->conn->lock held also if conn NULL */ -context_unsafe (__l2cap_chan_close (chan, reason)); +if (!test_bit (FLAG_DEL , &chan->flags)) { +/* Consider chan->conn->lock held also if conn NULL */ +context_unsafe (__l2cap_chan_close (chan, reason)); + }l2cap_chan_unlock_conn (chan, have_conn); }
base-commit: 019debf20bfd648b40ba10377ee0168db5eb241e
Signature · 1 line
2.43.0
Re: [PATCH 1/5] Bluetooth: L2CAP: ignore close requests for deleted channels
Hi,
ma, 2026-09-21 kello 23:38 +0800, Cen Zhang kirjoitti:
Quoted 23 lines
l2cap_chan_del()keepschan->connalive until channel destruction and marks removal withFLAG_DEL, but does not necessarily change the state. A caller retaining a temporary reference can therefore enterl2cap_chan_close_unlocked()after deletion and follow a stale connected state, rearming the channel timer and sending another disconnection request for a channel no longer on the connection list.Check
FLAG_DELafter taking the channel and connection locks so that concurrent deletion is serialized with the decision to close. Return without further timer or signaling work when the channel is deleted.Fixes b66774b48dd9 ("Bluetooth: L2CAP: Fix UAF in channel timeout by holding conn ref")Assisted-by LLMSigned-off-by Cen Zhang <[email protected]>--- net/bluetooth/l2cap_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/bluetooth/l2cap_core.c +0
-0
net/bluetooth/l2cap_core.c void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) · line 944
Quoted 8 lines
net/bluetooth/l2cap_core.c +3
-2@@ -939,8 +939,10 @@ void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) have_conn =l2cap_chan_lock_conn (chan); -/* Context analysis: consider chan->conn->lock held also if conn NULL */ -context_unsafe (__l2cap_chan_close (chan, reason)); +if (!test_bit (FLAG_DEL , &chan->flags)) { +/* Consider chan->conn->lock held also if conn NULL */ +context_unsafe (__l2cap_chan_close (chan, reason));
This probably would better be further down in __l2cap_chan_close() together with the other state check.
Quoted 6 lines
net/bluetooth/l2cap_core.c +1
-0+ }l2cap_chan_unlock_conn (chan, have_conn); }base-commit:
019debf20bfd648b40ba10377ee0168db5eb241e
Signature · 1 line
Pauli Virtanen
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/1170521/
---Test result---
Test Summary:
CheckPatch PASS 5.37 seconds VerifyFixes PASS 0.14 seconds VerifySignedoff PASS 0.14 seconds GitLint FAIL 1.79 seconds SubjectPrefix FAIL 0.66 seconds BuildKernel PASS 29.34 seconds CheckAllWarning PASS 33.40 seconds CheckSparse PASS 36.26 seconds BuildKernel32 PASS 29.12 seconds CheckKernelLLVM PASS 35.03 seconds TestRunnerSetup PASS 797.01 seconds TestRunner_l2cap-tester PASS 16.57 seconds TestRunner_iso-tester PASS 28.97 seconds TestRunner_bnep-tester PASS 3.11 seconds TestRunner_mgmt-tester PASS 62.84 seconds TestRunner_rfcomm-tester PASS 4.47 seconds TestRunner_sco-tester PASS 7.63 seconds TestRunner_ioctl-tester PASS 4.65 seconds TestRunner_mesh-tester FAIL 7.84 seconds TestRunner_smp-tester PASS 4.38 seconds TestRunner_userchan-tester PASS 3.21 seconds TestRunner_6lowpan-tester PASS 4.43 seconds IncrementalBuild PASS 75.68 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[4/5] Bluetooth: 6lowpan: handle channel setup failure and callback lifetime
29: B1 Line exceeds max length (81>80): " [ 101.222759] BUG: unable to handle page fault for address: ffffffffc040199f"
##############################
Test: SubjectPrefix - FAIL
Desc: Check subject contains "Bluetooth" prefix Output:
"Bluetooth: " prefix is not specified in the subject
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 2.419 seconds Mesh - Send cancel - 2 Timed out 1.998 seconds
https://github.com/bluez/bluetooth-next/pull/808
---
Regards,
Linux Bluetooth
PATCH 2/5 workqueue: add support for module-owned work
In reply to Cen ZhangQueueing a callback on a system workqueue does not take a reference to the module containing that callback. A caller which releases its last module reference after queueing work can therefore leave a callback in unloaded text. Releasing the reference from the callback itself also leaves its return path unprotected.
Add module_work and schedule_module_work() to hold the callback's owner from queueing until the callback returns. Run the dispatch and final module_put() in workqueue core, which remains present when the callback's module is unloaded. Cache the function and owner before invoking the callback so that it can free the containing work item.
6LoWPAN needs this for deferred network-device deletion after removing the last peer.
--- include/linux/workqueue.h | 15 ++++++++++++++ kernel/workqueue.c | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
include/linux/workqueue.h +15 -0
@@ -128,6 +128,14 @@ struct rcu_work {struct workqueue_struct *wq; }; +struct module ; + +struct module_work { +struct work_struct work; +struct module *owner; +work_func_t func; +}; +enum wq_affn_scope {WQ_AFFN_DFL ,/* use system default */ WQ_AFFN_CPU ,/* one pod per CPU */
@@ -220,6 +228,11 @@ static inline struct rcu_work *to_rcu_work(struct work_struct *work) return container_of(work, struct rcu_work, work); } +static inline struct module_work *to_module_work (struct work_struct *work) +{ + return container_of(work, struct module_work, work); +} +struct execute_work {struct work_struct work; };
@@ -634,6 +647,8 @@ extern void __flush_workqueue(struct workqueue_struct *wq);extern void drain_workqueue (struct workqueue_struct *wq);extern int schedule_on_each_cpu (work_func_t func); +bool schedule_module_work (struct module_work *mwork,work_func_t func, +struct module *owner);int execute_in_process_context (work_func_t fn,struct execute_work *);
kernel/workqueue.c +43 -0
55 lines not on this page · raw message
Signature · 1 line
2.43.0
Tejun Heo
maintainerRe: [PATCH 2/5] workqueue: add support for module-owned work
Quoted 12 lines · Cen Zhang
Queueing a callback on a system workqueue does not take a reference to the module containing that callback. A caller which releases its last module reference after queueing work can therefore leave a callback in unloaded text. Releasing the reference from the callback itself also leaves its return path unprotected.
Add
module_workandschedule_module_work()to hold the callback's owner from queueing until the callback returns. Run the dispatch and finalmodule_put()in workqueue core, which remains present when the callback's module is unloaded. Cache the function and owner before invoking the callback so that it can free the containing work item.
Quoted 5 lines
6LoWPAN needs this for deferred network-device deletion after removing the last peer.
Assisted-by LLMSigned-off-by Cen Zhang <[email protected]>
Please provide justification on why this needs to be done this way when everyone else can flush delayed items before unloading.
Thanks.
Signature · 1 line
tejun
Re: [PATCH 2/5] workqueue: add support for module-owned work
Hi Tejun,
Quoted 4 lines · Tejun Heo <[email protected]> 于2026年9月22日周二 00:16写道:
Please provide justification on why this needs to be done this way when everyone else can flush delayed items before unloading.
Thank you for the review, and sorry for the extra noise.
I will revisit whether the 6LoWPAN unload path can use the existing workqueue flush/cancellation primitives while preserving the required module lifetime, rather than adding a new workqueue API.
Thanks again for your patience.
Best regards,
Cen Zhang
PATCH 3/5 Bluetooth: L2CAP: drain channel timers on connection teardown
In reply to Cen ZhangL2CAP channel timers run on system_percpu_wq. Connection deletion cancels them without waiting because callbacks may need conn->lock, but HCI unregister does not drain that workqueue after releasing the lock. A running callback can therefore outlive HCI-driver unregister. If it drops the last channel reference and releases a protocol module, it still has to return through Bluetooth code after those dependencies are gone.
Individual channel deletion has another cancellation race. It cancels timers before calling the socket teardown callback, which can wait for sk lock. A concurrent recvmsg holding that lock can clear local busy and rearm the monitor timer. Deletion then unlinks the channel, so connection teardown can no longer find that delayed work through the channel list.
Give each connection an ordered timer workqueue. Serialize timer queueing with channel and connection stop flags, and stop each channel before canceling its timers or calling teardown. At connection deletion, stop queueing for the connection, cancel all four timer types, then drop conn->lock and drain running callbacks before releasing the connection. This lets callbacks acquire their locks, observe FLAG_DEL and return before HCI unregister completes.
The channel stop flag also ensures that no unlinked channel can leave delayed work behind on the new queue. The queue remains allocated until the drained connection is freed. This adds one workqueue per connection and serializes that connection's channel timers.
--- include/net/bluetooth/l2cap.h | 23 ++++++++++++++++++++++- net/bluetooth/l2cap_core.c | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-)
include/net/bluetooth/l2cap.h +22 -1
@@ -611,6 +611,7 @@ struct l2cap_chan {void *data;const struct l2cap_ops *ops; +bool timers_stopped;/* protected by conn->timer_lock */ struct mutex lock; };
@@ -636,6 +637,10 @@ struct l2cap_conn {struct sk_buff_head pending_rx;struct work_struct pending_rx_work; +struct workqueue_struct *timer_workqueue; +spinlock_t timer_lock;/* protects timer scheduling */ + + bool timers_stopped __guarded_by(&timer_lock);struct delayed_work id_addr_timer;
@@ -856,13 +861,29 @@ static inline void l2cap_chan_unlock(struct l2cap_chan *chan)static inline void l2cap_set_timer (struct l2cap_chan *chan,struct delayed_work *work,long timeout) { +struct l2cap_conn *conn = chan->conn; +unsigned long flags; +bool pending; +BT_DBG ("chan %p state %s timeout %ld" , chan,state_to_string (chan->state), timeout); +if (WARN_ON_ONCE (!conn)) +return ; + +spin_lock_irqsave (&conn->timer_lock, flags); +if (conn->timers_stopped || chan->timers_stopped) { +spin_unlock_irqrestore (&conn->timer_lock, flags); +return ; + } +l2cap_chan_hold (chan);/* put(chan) if timer was already queued so it already has a ref */ -if (mod_delayed_work (system_percpu_wq, work, timeout)) + pending =mod_delayed_work (conn->timer_workqueue, work, timeout); +spin_unlock_irqrestore (&conn->timer_lock, flags); + +if (pending)l2cap_chan_put (chan); }
net/bluetooth/l2cap_core.c +30 -4
@@ -686,9 +686,21 @@ void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)void l2cap_chan_del (struct l2cap_chan *chan,int err) { +struct l2cap_conn *conn = chan->conn; +unsigned long flags; +lockdep_assert (!chan->conn ||lockdep_is_held (&chan->conn->lock)); +if (conn) { +spin_lock_irqsave (&conn->timer_lock, flags); + chan->timers_stopped = true; +spin_unlock_irqrestore (&conn->timer_lock, flags); + } +__clear_chan_timer (chan); +__clear_retrans_timer (chan); +__clear_monitor_timer (chan); +__clear_ack_timer (chan);BT_DBG ("chan %p, err %d, state %s" , chan, err,state_to_string (chan->state));
@@ -723,10 +735,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err)break ;case L2CAP_MODE_ERTM : -__clear_retrans_timer (chan); -__clear_monitor_timer (chan); -__clear_ack_timer (chan); -skb_queue_purge (&chan->srej_q);l2cap_seq_list_free (&chan->srej_list);
@@ -1879,6 +1887,7 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err) {struct l2cap_conn *conn = hcon->l2cap_data;struct l2cap_chan *chan, *l; +unsigned long flags;if (!conn)return ;
@@ -1891,6 +1900,9 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)cancel_work_sync (&conn->pending_rx_work);mutex_lock (&conn->lock); +spin_lock_irqsave (&conn->timer_lock, flags); + conn->timers_stopped = true; +spin_unlock_irqrestore (&conn->timer_lock, flags);kfree_skb (conn->rx_skb);
@@ -1925,6 +1937,11 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)spin_unlock (&hcon->proto_lock);mutex_unlock (&conn->lock); + +/* Channel deletion canceled pending timers. Drop conn->lock before +* waiting for running callbacks so they can acquire it and return. +*/ +drain_workqueue (conn->timer_workqueue);l2cap_conn_put (conn); }
@@ -1932,6 +1949,7 @@ static void l2cap_conn_free(struct kref *ref) { struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); +destroy_workqueue (conn->timer_workqueue);hci_conn_put (conn->hcon);kfree (conn); }
@@ -7416,6 +7434,14 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)return NULL ; } + conn->timer_workqueue =alloc_ordered_workqueue ("l2cap" ,WQ_MEM_RECLAIM ); +if (!conn->timer_workqueue) { +kfree (conn); +hci_chan_del (hchan); +return NULL ; + } +spin_lock_init (&conn->timer_lock); +kref_init (&conn->ref); conn->hchan = hchan;
Signature · 1 line
2.43.0
Luiz Augusto von Dentz
maintainerRe: [PATCH 3/5] Bluetooth: L2CAP: drain channel timers on connection teardown
Hi Cen,
Quoted 8 lines · Cen Zhang
L2CAP channel timers run on
system_percpu_wq. Connection deletion cancels them without waiting because callbacks may needconn->lock, but HCI unregister does not drain that workqueue after releasing the lock. A running callback can therefore outlive HCI-driver unregister. If it drops the last channel reference and releases a protocol module, it still has to return through Bluetooth code after those dependencies are gone.
Quoted 5 lines
Individual channel deletion has another cancellation race. It cancels timers before calling the socket teardown callback, which can wait for sk lock. A concurrent recvmsg holding that lock can clear local busy and rearm the monitor timer. Deletion then unlinks the channel, so connection teardown can no longer find that delayed work through the channel list.
How about disabling the work instead of introducing another workqueue for it?
Quoted 167 lines
Give each connection an ordered timer workqueue. Serialize timer queueing with channel and connection stop flags, and stop each channel before canceling its timers or calling teardown. At connection deletion, stop queueing for the connection, cancel all four timer types, then drop
conn->lockand drain running callbacks before releasing the connection. This lets callbacks acquire their locks, observeFLAG_DELand return before HCI unregister completes.The channel stop flag also ensures that no unlinked channel can leave delayed work behind on the new queue. The queue remains allocated until the drained connection is freed. This adds one workqueue per connection and serializes that connection's channel timers.
Assisted-by LLMSigned-off-by Cen Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 23 ++++++++++++++++++++++- net/bluetooth/l2cap_core.c | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-)include/net/bluetooth/l2cap.h +22
-1@@ -611,6 +611,7 @@ struct l2cap_chan {void *data;const struct l2cap_ops *ops; +bool timers_stopped;/* protected by conn->timer_lock */ struct mutex lock; };@@ -636,6 +637,10 @@ struct l2cap_conn {struct sk_buff_head pending_rx;struct work_struct pending_rx_work; +struct workqueue_struct *timer_workqueue; +spinlock_t timer_lock;/* protects timer scheduling */ + + bool timers_stopped __guarded_by(&timer_lock);struct delayed_work id_addr_timer;@@ -856,13 +861,29 @@ static inline void l2cap_chan_unlock(struct l2cap_chan *chan)static inline void l2cap_set_timer (struct l2cap_chan *chan,struct delayed_work *work,long timeout) { +struct l2cap_conn *conn = chan->conn; +unsigned long flags; +bool pending; +BT_DBG ("chan %p state %s timeout %ld" , chan,state_to_string (chan->state), timeout); +if (WARN_ON_ONCE (!conn)) +return ; + +spin_lock_irqsave (&conn->timer_lock, flags); +if (conn->timers_stopped || chan->timers_stopped) { +spin_unlock_irqrestore (&conn->timer_lock, flags); +return ; + } +l2cap_chan_hold (chan);/* put(chan) if timer was already queued so it already has a ref */ -if (mod_delayed_work (system_percpu_wq, work, timeout)) + pending =mod_delayed_work (conn->timer_workqueue, work, timeout); +spin_unlock_irqrestore (&conn->timer_lock, flags); + +if (pending)l2cap_chan_put (chan); }net/bluetooth/l2cap_core.c +30
-5@@ -686,9 +686,21 @@ void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)void l2cap_chan_del (struct l2cap_chan *chan,int err) { +struct l2cap_conn *conn = chan->conn; +unsigned long flags; +lockdep_assert (!chan->conn ||lockdep_is_held (&chan->conn->lock)); +if (conn) { +spin_lock_irqsave (&conn->timer_lock, flags); + chan->timers_stopped = true; +spin_unlock_irqrestore (&conn->timer_lock, flags); + } +__clear_chan_timer (chan); +__clear_retrans_timer (chan); +__clear_monitor_timer (chan); +__clear_ack_timer (chan);BT_DBG ("chan %p, err %d, state %s" , chan, err,state_to_string (chan->state));@@ -723,10 +735,6 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err)break ;case L2CAP_MODE_ERTM : -__clear_retrans_timer (chan); -__clear_monitor_timer (chan); -__clear_ack_timer (chan); -skb_queue_purge (&chan->srej_q);l2cap_seq_list_free (&chan->srej_list);@@ -1879,6 +1887,7 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err) {struct l2cap_conn *conn = hcon->l2cap_data;struct l2cap_chan *chan, *l; +unsigned long flags;if (!conn)return ;@@ -1891,6 +1900,9 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)cancel_work_sync (&conn->pending_rx_work);mutex_lock (&conn->lock); +spin_lock_irqsave (&conn->timer_lock, flags); + conn->timers_stopped = true; +spin_unlock_irqrestore (&conn->timer_lock, flags);kfree_skb (conn->rx_skb);@@ -1925,6 +1937,11 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)spin_unlock (&hcon->proto_lock);mutex_unlock (&conn->lock); + +/* Channel deletion canceled pending timers. Drop conn->lock before +* waiting for running callbacks so they can acquire it and return. +*/ +drain_workqueue (conn->timer_workqueue);l2cap_conn_put (conn); }@@ -1932,6 +1949,7 @@ static void l2cap_conn_free(struct kref *ref) { struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); +destroy_workqueue (conn->timer_workqueue);hci_conn_put (conn->hcon);kfree (conn); }@@ -7416,6 +7434,14 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)return NULL ; } + conn->timer_workqueue =alloc_ordered_workqueue ("l2cap" ,WQ_MEM_RECLAIM ); +if (!conn->timer_workqueue) { +kfree (conn); +hci_chan_del (hchan); +return NULL ; + } +spin_lock_init (&conn->timer_lock); +kref_init (&conn->ref); conn->hchan = hchan; --2.43.0
Signature · 1 line
Luiz Augusto von Dentz
PATCH 4/5 Bluetooth: 6lowpan: handle channel setup failure and callback lifetime
In reply to Cen ZhangL2CAP marks a channel connected before invoking its ready callback. 6LoWPAN can then fail to allocate or publish a peer, leaving an unusable connected channel or a network device without a peer. Deletion before peer publication also leaks the channel's initial reference: close only releases it after finding a peer.
The peer's module reference ends too early as well. Removing the last peer releases it while the close callback is still executing, allowing bluetooth_6lowpan to be unloaded before that callback returns. Deferred network-device deletion also needs protection through callback return.
Let ready return an error and handle it in the LE CoC and ECRED request and response paths. Allocate the peer before setting up a network device, and publish it before bringing the device up. Track ownership of the initial reference explicitly and release it once from teardown, even when no peer was published. Hold separate request-handler references through rollback, unlock and response construction.
Hold the operations owner's module reference for accepted and outgoing channels until channel destruction. Keep the listener itself unpinned so module exit can close it; check BT_LISTEN under the parent lock and take a temporary owner reference before invoking new_connection. Use module-owned work for deferred network-device deletion.
A close/unload race produced this instruction-fetch fault:
[ 101.222759] BUG: unable to handle page fault for address: ffffffffc040199f
[ 101.224102] #PF: supervisor instruction fetch in kernel mode
[ 101.225119] #PF: error_code(0x0010) - not-present page
[ 101.226980] Oops: Oops: 0010 [#1] SMP KASAN NOPTI
[... omitted ...]
[ 101.232306] Workqueue: hci1 hci_rx_work
[ 101.233015] RIP: 0010:0xffffffffc040199f
[ 101.233780] Code: Unable to access opcode bytes at 0xffffffffc0401975.
[... omitted ...]
[ 101.275265] Modules linked in: [last unloaded: bluetooth_6lowpan(O)]
[ 101.276117] CR2: ffffffffc040199f
[... omitted ...]
[ 101.287138] Kernel panic - not syncing: Fatal exception--- include/net/bluetooth/l2cap.h | 10 +++- net/bluetooth/6lowpan.c | 86 ++++++++++++++++++++------------ net/bluetooth/l2cap_core.c | 94 +++++++++++++++++++++++++++++------ net/bluetooth/l2cap_sock.c | 6 ++- net/bluetooth/smp.c | 4 +- 5 files changed, 148 insertions(+), 52 deletions(-)
include/net/bluetooth/l2cap.h +8 -2
@@ -611,6 +611,8 @@ struct l2cap_chan {void *data;const struct l2cap_ops *ops; +struct module *ops_owner; +bool ops_owner_pinned;bool timers_stopped;/* protected by conn->timer_lock */ struct mutex lock; };
@@ -669,7 +671,7 @@ struct l2cap_ops {__must_hold (&chan->lock);void (*state_change) (struct l2cap_chan *chan,int state,int err); - void (*ready) (struct l2cap_chan *chan) + int (*ready)(struct l2cap_chan *chan) __must_hold(&chan->lock) __must_hold(&chan->conn->lock);void (*defer) (struct l2cap_chan *chan);
@@ -764,6 +766,7 @@ enum {FLAG_ECRED_CONN_REQ_SENT ,FLAG_PENDING_SECURITY ,FLAG_HOLD_HCI_CONN , +FLAG_RELEASE_CREATOR , FLAG_DEL, };
@@ -948,8 +951,9 @@ static inline void l2cap_chan_no_close(struct l2cap_chan *chan) { } -static inline void l2cap_chan_no_ready (struct l2cap_chan *chan) +static inline int l2cap_chan_no_ready (struct l2cap_chan *chan) { +return 0 ; } static inline void l2cap_chan_no_state_change(struct l2cap_chan *chan,
@@ -994,6 +998,8 @@ int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm);int l2cap_add_scid (struct l2cap_chan *chan,__u16 scid);struct l2cap_chan *l2cap_chan_create (void ); +bool l2cap_chan_set_ops (struct l2cap_chan *chan, +const struct l2cap_ops *ops,struct module *owner); void l2cap_chan_close_unlocked(struct l2cap_chan *chan, int reason) __must_not_hold(&chan->lock); int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
net/bluetooth/6lowpan.c +54 -32
189 lines not on this page · raw message
net/bluetooth/l2cap_core.c +79 -15
225 lines not on this page · raw message
net/bluetooth/l2cap_sock.c +4 -2
@@ -1825,13 +1825,13 @@ static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,return skb; } -static void l2cap_sock_ready_cb (struct l2cap_chan *chan) +static int l2cap_sock_ready_cb (struct l2cap_chan *chan) {struct sock *sk = chan->data;struct sock *parent;if (!sk) -return ; +return 0 ;lock_sock (sk);
@@ -1846,6 +1846,8 @@ static void l2cap_sock_ready_cb(struct l2cap_chan *chan) parent->sk_data_ready (parent);release_sock (sk); + +return 0 ; }static void l2cap_sock_defer_cb (struct l2cap_chan *chan)
net/bluetooth/smp.c +3 -1
@@ -3155,7 +3155,7 @@ static void smp_resume_cb(struct l2cap_chan *chan)smp_distribute_keys (smp); } -static void smp_ready_cb (struct l2cap_chan *chan) +static int smp_ready_cb (struct l2cap_chan *chan) {struct l2cap_conn *conn = chan->conn;struct hci_conn *hcon = conn->hcon;
@@ -3172,6 +3172,8 @@ static void smp_ready_cb(struct l2cap_chan *chan)if (hcon->type ==ACL_LINK &&test_bit (HCI_CONN_ENCRYPT , &hcon->flags))bredr_pairing (chan); + +return 0 ; }static int smp_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)
Signature · 1 line
2.43.0
PATCH 5/5 Bluetooth: 6lowpan: quiesce peers before channel deletion
In reply to Cen Zhang6LoWPAN removes a peer and drops its channel reference from the close callback. A transmitter which already observed the RCU-published peer can still use the freed channel. Keeping the allocation alive alone is
insufficient: a transmitter which passed the deletion check can enqueue a packet after L2CAP has purged the transmit queue.
Move peer cleanup to teardown, before FLAG_DEL and the transmit purge. Mark the channel closed, unlink the peer under devices_lock, then drop the lock and wait for network RCU readers with synchronize_net(). Free the peer and release the initial channel reference only after those transmitters have returned. Queue last-peer network-device deletion before releasing the channel's ownership reference.
Disabling 6LoWPAN must also close the listener before sweeping existing peers. Otherwise a request holding the old listener can publish a child after the sweep. Serialize the transition with set_lock: requests which complete admission before listener teardown are included in the sweep, and requests which reach the closed listener are rejected.
The transmit/removal race produced this report:
[ 59.413897] BUG: KASAN: slab-use-after-free in send_pkt+0x3b1/0x3e0
[ 59.415014] Write of size 8 at addr ffff88810cf0e4a0 by task python3/583
[ ... report excerpt omitted ... ]
[ 59.490444] Freed by task 504:
[ ... report excerpt omitted ... ]
[ 59.493046] kfree+0x307/0x580
[ 59.493497] l2cap_chan_put+0x273/0x3a0
[ 59.494020] l2cap_disconnect_req+0x613/0x890
[ ... report excerpt omitted ... ]--- net/bluetooth/6lowpan.c | 64 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 31 deletions(-)
net/bluetooth/6lowpan.c +33 -31
@@ -61,7 +61,6 @@ enum {struct lowpan_peer {struct list_head list; -struct rcu_head rcu;struct l2cap_chan *chan;/* peer addresses in various formats */
@@ -100,7 +99,6 @@ static inline bool peer_del(struct lowpan_btle_dev *dev, struct lowpan_peer *peer) {list_del_rcu (&peer->list); -kfree_rcu (peer, rcu);if (atomic_dec_and_test (&dev->peer_count)) {BT_DBG ("last peer" );
@@ -776,16 +774,15 @@ static void delete_netdev(struct work_struct *work)/* The entry pointer is deleted by the netdev destructor. */ } -static void chan_close_cb (struct l2cap_chan *chan) +static void chan_teardown_cb (struct l2cap_chan *chan,int err) {struct lowpan_btle_dev *entry;struct lowpan_btle_dev *dev =NULL ; -struct lowpan_peer *peer; -int err = -ENOENT ; +struct lowpan_peer *peer =NULL ;bool last = false; -bool queued;BT_DBG ("chan %p conn %p" , chan, chan->conn); + chan->state =BT_CLOSED ;spin_lock (&devices_lock);
@@ -794,7 +791,6 @@ static void chan_close_cb(struct l2cap_chan *chan) peer =__peer_lookup_chan (dev, chan);if (peer) { last =peer_del (dev, peer); - err =0 ;BT_DBG ("dev %p removing %speer %p" , dev, last ?"last " :"1 " , peer);
@@ -802,19 +798,28 @@ static void chan_close_cb(struct l2cap_chan *chan) } } -if (!err && last && dev && !atomic_read (&dev->peer_count)) { -spin_unlock (&devices_lock); +spin_unlock (&devices_lock); -cancel_delayed_work_sync (&dev->notify_peers); +if (peer) { +/* ndo_start_xmit() holds network RCU while using peer->chan. */ +synchronize_net (); +kfree (peer); -ifdown (dev->netdev); +if (last && dev) { +bool queued; - queued =schedule_module_work (&entry->delete_netdev, - delete_netdev,THIS_MODULE ); -WARN_ON_ONCE (!queued); - }else { -spin_unlock (&devices_lock); +cancel_delayed_work_sync (&dev->notify_peers); + +ifdown (dev->netdev); + + queued =schedule_module_work (&entry->delete_netdev, + delete_netdev,THIS_MODULE ); +WARN_ON_ONCE (!queued); + } } + +if (test_and_clear_bit (FLAG_RELEASE_CREATOR , &chan->flags)) +l2cap_chan_put (chan); }static void chan_state_change_cb (struct l2cap_chan *chan,int state,int err)
@@ -873,6 +878,9 @@ static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)static int chan_new_connection_cb (struct l2cap_chan *chan,struct l2cap_chan *new_chan) { +if (chan->state !=BT_LISTEN ) +return -EINVAL ; +if (!l2cap_chan_set_ops (new_chan, &bt_6lowpan_chan_ops,THIS_MODULE ))return -ENODEV ;
@@ -880,19 +888,11 @@ static int chan_new_connection_cb(struct l2cap_chan *chan,return 0 ; } -static void chan_teardown_cb (struct l2cap_chan *chan,int err) -{ - chan->state =BT_CLOSED ; - -if (test_and_clear_bit (FLAG_RELEASE_CREATOR , &chan->flags)) -l2cap_chan_put (chan); -} -static const struct l2cap_ops bt_6lowpan_chan_ops = { .name ="L2CAP 6LoWPAN channel" , .new_connection = chan_new_connection_cb, .recv = chan_recv_cb, - .close = chan_close_cb, + .close = l2cap_chan_no_close, .state_change = chan_state_change_cb, .ready = chan_ready_cb, .resume = chan_resume_cb,
@@ -1103,20 +1103,22 @@ static void disconnect_all_peers(void)static void do_enable_set (bool flag) { -if (!flag || enable_6lowpan != flag) -/* Disconnect existing connections if 6lowpan is -* disabled -*/ -disconnect_all_peers (); +bool disconnect; + +mutex_lock (&set_lock); + disconnect = !flag || enable_6lowpan != flag; enable_6lowpan = flag; -mutex_lock (&set_lock);if (listen_chan) {l2cap_chan_close_unlocked (listen_chan,0 );l2cap_chan_put (listen_chan); + listen_chan =NULL ; } +if (disconnect) +disconnect_all_peers (); + listen_chan =bt_6lowpan_listen ();mutex_unlock (&set_lock); }
Signature · 1 line
2.43.0
Luiz Augusto von Dentz
maintainerHi Cen,
Quoted 15 lines · Cen Zhang
6LoWPAN shares L2CAP channels between connection setup, published peers and packet transmission, while teardown also schedules asynchronous cleanup. These paths currently disagree on when channel ownership, peer visibility and module lifetime end. The resulting failures range from leaked or unusable connections and peers surviving disable to transmit use-after-free and execution of unloaded module code.
This series establishes a consistent ownership and teardown order across those paths. Shared L2CAP and workqueue changes provide the lifetime guarantees needed by the following 6LoWPAN changes, which make failed setup recoverable and stop users of a peer before releasing its resources. Keeping the changes together avoids fixing one lifetime while shortening another.
Quoted 4 lines
The timer support adds one ordered workqueue per L2CAP connection and serializes that connection's channel timers.
Assisted-by LLM
Something suggests none of this was tested, as IPSP was never mentioned once in this set.
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 4 lines · Luiz Augusto von Dentz <[email protected]> 于2026年9月22日周二 00:07写道:
Something suggests none of this was tested, as IPSP was never mentioned once in this set.
Thank you for the feedback, and sorry that the patch series is not clear enough.
This series addresses six Bluetooth 6LoWPAN/IPSP lifetime bugs found by our tools.
They were not submitted as static guesses: each bug was confirmed by dynamic execution in a controlled, instrumented kernel. Two of them produced native reports:
a KASAN use-after-free in send_pkt(), and an instruction-fetch fault after unloadingbluetooth_6lowpan. The other four were confirmed as semantic or state-management bugs where the harmful state or broken invariant was observed at runtime.
Because these bugs are related, I tried to fix them together in one series. However, this turned out to be more difficult than I expected, and the current result is not good enough. I will try either to narrow the fixes or to send detailed bug reports first and work through the solutions together.
Do you think it would be better to start from separate bug reports, or from narrower patches for a smaller subset of these issues?
For reference, the six bugs were:
- A transmit race between ndo_start_xmit() and peer/channel removal, producing a native KASAN use-after-free report in send_pkt().
- A module-lifetime bug where Bluetooth 6LoWPAN callback code could keep executing or return through module text after the module reference was dropped, producing a native instruction-fetch-fault report after unload.
- A pre-publication setup failure where an IPSP channel could be deleted before lowpan_peer publication, leaving the creator-side channel reference without an owner.
- A close-path race where local close could continue from stale connected state after concurrent deletion had set FLAG_DEL, rearming the timer and sending another disconnect for the deleted channel.
- A listener/disable race where a request that had already looked up the listener could still create and publish a child peer after 6LoWPAN had been disabled and existing peers swept.
- A ready/publication failure where add_peer_chan() could fail while the void ready callback returned normally, leaving a connected channel without a peer and running ifup() on a zero-peer netdev.
Best regards,
Cen Zhang