From: "Bin Luo (University of Electronic Science and Technology of China)" <luob87709@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH 2/2] Bluetooth: 6lowpan: unlink closing device and protect RCU lookups
Date: Mon, 30 Nov 2026 18:28:54 +0800 [thread overview]
Message-ID: <20261130102854.1553-3-luob87709@gmail.com> (raw)
In-Reply-To: <20261130102854.1553-1-luob87709@gmail.com>
When the last peer disconnects, chan_close_cb() unlocks devices_lock
without removing the lowpan_btle_dev entry from bt_6lowpan_devices,
then cancels dev->notify_peers, brings the interface down, and
re-initializes and schedules entry->delete_netdev.
Because entry remains visible on bt_6lowpan_devices until delete_netdev()
eventually runs lowpan_unregister_netdev() -> device_event(), a
concurrent chan_ready_cb() for a new L2CAP IPSP channel on the same
hci_dev can look up the dying entry via lookup_dev(), add a peer, and
re-arm dev->notify_peers (100 ms delayed_work) after
cancel_delayed_work_sync() has already returned. delete_netdev() then
frees the net_device (and embedded lowpan_btle_dev) while
dev->notify_peers.timer is still armed, causing a slab-use-after-free
write in __run_timer_base().
In addition, lookup_dev() drops rcu_read_unlock() before returning the
borrowed lowpan_btle_dev pointer, leaving chan_ready_cb(),
chan_recv_cb(), chan_suspend_cb(), and chan_resume_cb() dereferencing
dev and dev->netdev outside RCU protection without a net_device
reference.
Fix both issues:
- Initialize notify_peers and delete_netdev once in setup_netdev().
- Add a closing flag to struct lowpan_btle_dev and mark entry->closing
and unlink entry with list_del_rcu() under devices_lock in
chan_close_cb() (and device_event()) before unlocking and scheduling
delete_netdev.
- Check dev->closing and schedule dev->notify_peers under devices_lock
in add_peer_chan() so notify_peers can never be re-armed once
teardown starts; retry with a new netdev in chan_ready_cb() if the
looked-up device is closing.
- Hold a net_device reference via dev_hold()/dev_put() across
chan_ready_cb() (safe under rcu_read_lock() because list_del_rcu()
always precedes lowpan_unregister_netdev() -> synchronize_net() ->
netdev_run_todo()) and guard dev_open() with reg_state ==
NETREG_REGISTERED.
- Keep rcu_read_lock() across atomic device and peer accesses in
chan_recv_cb(), chan_suspend_cb(), and chan_resume_cb().
Found by static call-chain audit and reproduced under QEMU/KASAN with
hci_vhci.
Fixes: 18722c247023 ("Bluetooth: Enable 6LoWPAN support for BT LE devices")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
net/bluetooth/6lowpan.c | 131 ++++++++++++++++++++++++++++------------
1 file changed, 91 insertions(+), 40 deletions(-)
diff --git a/net/bluetooth/6lowpan.c b/net/bluetooth/6lowpan.c
index 96cb652..31c336d 100644
--- a/net/bluetooth/6lowpan.c
+++ b/net/bluetooth/6lowpan.c
@@ -80,6 +80,7 @@ struct lowpan_btle_dev {
struct work_struct delete_netdev;
struct delayed_work notify_peers;
+ bool closing;
};
static inline struct lowpan_btle_dev *
@@ -221,20 +222,26 @@ static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
return peer;
}
-static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
+static struct lowpan_btle_dev *__lookup_dev(struct l2cap_conn *conn)
{
struct lowpan_btle_dev *entry;
- struct lowpan_btle_dev *dev = NULL;
-
- rcu_read_lock();
list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
- if (conn->hcon->hdev == entry->hdev) {
- dev = entry;
- break;
- }
+ if (conn->hcon->hdev == entry->hdev)
+ return entry;
}
+ return NULL;
+}
+
+static struct lowpan_btle_dev *lookup_dev_get(struct l2cap_conn *conn)
+{
+ struct lowpan_btle_dev *dev;
+
+ rcu_read_lock();
+ dev = __lookup_dev(conn);
+ if (dev)
+ dev_hold(dev->netdev);
rcu_read_unlock();
return dev;
@@ -360,15 +367,21 @@ static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
/* The skb is always consumed, so never return an error that would
* make the caller free it again.
*/
- peer = lookup_peer(chan->conn);
- if (!peer)
+ rcu_read_lock();
+ dev = __lookup_dev(chan->conn);
+ if (!dev || !dev->netdev) {
+ rcu_read_unlock();
goto drop;
+ }
- dev = lookup_dev(chan->conn);
- if (!dev || !dev->netdev)
+ peer = __peer_lookup_conn(dev, chan->conn);
+ if (!peer) {
+ rcu_read_unlock();
goto drop;
+ }
err = recv_pkt(skb, dev->netdev, peer);
+ rcu_read_unlock();
if (err)
BT_DBG("recv pkt %d", err);
@@ -598,9 +611,12 @@ static void ifup(struct net_device *netdev)
int err;
rtnl_lock();
- err = dev_open(netdev, NULL);
- if (err < 0)
- BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
+ if (netdev->reg_state == NETREG_REGISTERED) {
+ err = dev_open(netdev, NULL);
+ if (err < 0)
+ BT_INFO("iface %s cannot be opened (%d)",
+ netdev->name, err);
+ }
rtnl_unlock();
}
@@ -647,15 +663,14 @@ static struct l2cap_chan *chan_create(void)
return chan;
}
-static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
- struct lowpan_btle_dev *dev,
- bool new_netdev)
+static int add_peer_chan(struct l2cap_chan *chan,
+ struct lowpan_btle_dev *dev)
{
struct lowpan_peer *peer;
peer = kzalloc_obj(*peer, GFP_ATOMIC);
if (!peer)
- return NULL;
+ return -ENOMEM;
peer->chan = chan;
@@ -664,18 +679,22 @@ static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
spin_lock(&devices_lock);
+ if (dev->closing) {
+ spin_unlock(&devices_lock);
+ kfree(peer);
+ return -ENODEV;
+ }
INIT_LIST_HEAD(&peer->list);
peer_add(dev, peer);
- spin_unlock(&devices_lock);
-
/* Notifying peers about us needs to be done without locks held */
- if (new_netdev)
- INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
+ spin_unlock(&devices_lock);
- return peer->chan;
+ return 0;
}
+static void delete_netdev(struct work_struct *work);
+
static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
{
struct net_device *netdev;
@@ -700,8 +719,9 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
(*dev)->netdev = netdev;
(*dev)->hdev = chan->conn->hcon->hdev;
INIT_LIST_HEAD(&(*dev)->peers);
-
INIT_LIST_HEAD(&(*dev)->list);
+ INIT_DELAYED_WORK(&(*dev)->notify_peers, do_notify_peers);
+ INIT_WORK(&(*dev)->delete_netdev, delete_netdev);
err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
if (err < 0) {
@@ -710,6 +730,8 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
goto out;
}
+ dev_hold(netdev);
+
spin_lock(&devices_lock);
list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
spin_unlock(&devices_lock);
@@ -730,25 +752,52 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)
__must_hold(&chan->conn->lock)
{
struct lowpan_btle_dev *dev;
- bool new_netdev = false;
+ bool new_netdev;
+ int err;
+
+ if (!try_module_get(THIS_MODULE))
+ return;
- dev = lookup_dev(chan->conn);
+retry:
+ new_netdev = false;
+ dev = lookup_dev_get(chan->conn);
BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
if (!dev) {
if (setup_netdev(chan, &dev) < 0) {
+ module_put(THIS_MODULE);
l2cap_chan_del(chan, -ENOENT);
return;
}
new_netdev = true;
}
- if (!try_module_get(THIS_MODULE))
+ err = add_peer_chan(chan, dev);
+ if (err == -ENODEV) {
+ dev_put(dev->netdev);
+ goto retry;
+ }
+ if (err) {
+ if (new_netdev) {
+ spin_lock(&devices_lock);
+ if (!dev->closing && !atomic_read(&dev->peer_count)) {
+ dev->closing = true;
+ list_del_rcu(&dev->list);
+ spin_unlock(&devices_lock);
+ schedule_work(&dev->delete_netdev);
+ } else {
+ spin_unlock(&devices_lock);
+ }
+ }
+ dev_put(dev->netdev);
+ module_put(THIS_MODULE);
+ l2cap_chan_del(chan, err);
return;
+ }
- add_peer_chan(chan, dev, new_netdev);
ifup(dev->netdev);
+ dev_put(dev->netdev);
}
static void unregister_dev(struct lowpan_btle_dev *dev)
@@ -812,13 +861,14 @@ static void chan_close_cb(struct l2cap_chan *chan)
}
if (!err && last && dev && !atomic_read(&dev->peer_count)) {
+ entry->closing = true;
+ list_del_rcu(&entry->list);
spin_unlock(&devices_lock);
cancel_delayed_work_sync(&dev->notify_peers);
ifdown(dev->netdev);
- INIT_WORK(&entry->delete_netdev, delete_netdev);
schedule_work(&entry->delete_netdev);
} else {
spin_unlock(&devices_lock);
@@ -853,11 +903,11 @@ static void chan_suspend_cb(struct l2cap_chan *chan)
BT_DBG("chan %p suspend", chan);
- dev = lookup_dev(chan->conn);
- if (!dev || !dev->netdev)
- return;
-
- netif_stop_queue(dev->netdev);
+ rcu_read_lock();
+ dev = __lookup_dev(chan->conn);
+ if (dev && dev->netdev)
+ netif_stop_queue(dev->netdev);
+ rcu_read_unlock();
}
static void chan_resume_cb(struct l2cap_chan *chan)
@@ -866,11 +916,11 @@ static void chan_resume_cb(struct l2cap_chan *chan)
BT_DBG("chan %p resume", chan);
- dev = lookup_dev(chan->conn);
- if (!dev || !dev->netdev)
- return;
-
- netif_wake_queue(dev->netdev);
+ rcu_read_lock();
+ dev = __lookup_dev(chan->conn);
+ if (dev && dev->netdev)
+ netif_wake_queue(dev->netdev);
+ rcu_read_unlock();
}
static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
@@ -1279,6 +1329,7 @@ static int device_event(struct notifier_block *unused,
if (entry->netdev == netdev) {
BT_DBG("Unregistered netdev %s %p",
netdev->name, netdev);
+ entry->closing = true;
list_del_rcu(&entry->list);
break;
}
--
2.51.0.windows.1
prev parent reply other threads:[~2026-11-30 10:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 10:28 [PATCH 0/2] Bluetooth: 6lowpan: fix netdev teardown UAF and registration rollback GPF Bin Luo (University of Electronic Science and Technology of China)
2026-11-30 10:28 ` [PATCH 1/2] Bluetooth: 6lowpan: publish netdev after registration in setup_netdev() Bin Luo (University of Electronic Science and Technology of China)
2026-11-30 16:33 ` Bluetooth: 6lowpan: fix netdev teardown UAF and registration rollback GPF bluez.test.bot
2026-11-30 10:28 ` Bin Luo (University of Electronic Science and Technology of China) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130102854.1553-3-luob87709@gmail.com \
--to=luob87709@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.