The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] Bluetooth: 6lowpan: fix netdev teardown UAF and registration rollback GPF
@ 2026-11-30 10:28 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 10:28 ` [PATCH 2/2] Bluetooth: 6lowpan: unlink closing device and protect RCU lookups Bin Luo (University of Electronic Science and Technology of China)
  0 siblings, 2 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-30 10:28 UTC (permalink / raw)
  To: marcel, luiz.dentz; +Cc: linux-bluetooth, linux-kernel

This 2-patch series fixes two lifetime bugs in Bluetooth 6LoWPAN
(net/bluetooth/6lowpan.c) discovered by static call-chain audit and
reproduced live under QEMU (x86_64, KASAN + LOCKDEP, 4 vCPUs) using only
mainline hci_vhci (/dev/vhci) with zero kernel modifications:

1. Patch 1 fixes a double list_del_rcu() general protection fault in
   setup_netdev(): publishing (*dev)->list on bt_6lowpan_devices before
   lowpan_register_netdev() causes device_event(NETDEV_UNREGISTER) to
   unlink the entry on rollback (for example after an addrconf_notify /
   snmp6_register_dev failure), after which setup_netdev()'s error path
   calls list_del_rcu() a second time and faults on LIST_POISON2
   (0xdead000000000122).

2. Patch 2 fixes a slab-use-after-free write in __run_timer_base() when
   the last L2CAP IPSP peer disconnects concurrently with a new channel
   becoming ready on the same hci_dev, as well as unprotected RCU
   lookups in lookup_dev() callers (chan_ready_cb, chan_recv_cb,
   chan_suspend_cb, chan_resume_cb):

   BUG: KASAN: slab-use-after-free in __run_timer_base.part.0+0x897/0x940
   Write of size 8 at addr ffff8880046ed0e8 by task swapper/0/0
   Allocated by task 92:
    alloc_netdev_mqs+0x78/0x10c0
    chan_ready_cb+0x3cf/0xab0
   Freed by task 9:
    kfree+0x2fd/0x530
    device_release+0xc8/0x240
    kobject_put+0x14d/0x280
    netdev_run_todo+0x7a7/0x10f0
    delete_netdev+0x5b/0x80

Verification:
- scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks on both patches
- W=1 KASAN+LOCKDEP build of net/bluetooth/6lowpan.o: 0 warnings, 0 errors
- QEMU KASAN+LOCKDEP hci_vhci reproducer (12 rapid connect/disconnect cycles):
  - Unpatched v7.4-rc5: KASAN slab-use-after-free write on iteration 0,
    followed by double list_del_rcu() GPF on iteration 4
  - With this series: 12/12 iterations pass with 0 KASAN, 0 GPF, 0 Lockdep,
    and 0 warnings

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5

Bin Luo (University of Electronic Science and Technology of China) (2):
  Bluetooth: 6lowpan: publish netdev after registration in
    setup_netdev()
  Bluetooth: 6lowpan: unlink closing device and protect RCU lookups

 net/bluetooth/6lowpan.c | 142 +++++++++++++++++++++++++++-------------
 1 file changed, 95 insertions(+), 47 deletions(-)

-- 
2.51.0.windows.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] Bluetooth: 6lowpan: publish netdev after registration in setup_netdev()
  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 ` Bin Luo (University of Electronic Science and Technology of China)
  2026-11-30 10:28 ` [PATCH 2/2] Bluetooth: 6lowpan: unlink closing device and protect RCU lookups Bin Luo (University of Electronic Science and Technology of China)
  1 sibling, 0 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-30 10:28 UTC (permalink / raw)
  To: marcel, luiz.dentz; +Cc: linux-bluetooth, linux-kernel, stable

setup_netdev() adds (*dev)->list to bt_6lowpan_devices before calling
lowpan_register_netdev(). If lowpan_register_netdev() fails after
NETDEV_REGISTER notifiers have run (for example, -ENOMEM during
addrconf_notify/snmp6_register_dev), register_netdevice() rolls back
and emits NETDEV_UNREGISTER. device_event() then removes (*dev)->list
via list_del_rcu() (poisoning prev with LIST_POISON2), after which
setup_netdev()'s error path calls list_del_rcu(&(*dev)->list) a second
time, causing a general protection fault on 0xdead000000000122.

Publishing the device before registration also exposes an unregistered
net_device on bt_6lowpan_devices to concurrent readers.

Move list_add_rcu(&(*dev)->list, &bt_6lowpan_devices) after
lowpan_register_netdev() succeeds, and simply call free_netdev(netdev)
on registration failure.

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 | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/net/bluetooth/6lowpan.c b/net/bluetooth/6lowpan.c
index 68ca2f4..96cb652 100644
--- a/net/bluetooth/6lowpan.c
+++ b/net/bluetooth/6lowpan.c
@@ -701,22 +701,19 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
 	(*dev)->hdev = chan->conn->hcon->hdev;
 	INIT_LIST_HEAD(&(*dev)->peers);
 
-	spin_lock(&devices_lock);
 	INIT_LIST_HEAD(&(*dev)->list);
-	list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
-	spin_unlock(&devices_lock);
 
 	err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
 	if (err < 0) {
 		BT_INFO("register_netdev failed %d", err);
-		spin_lock(&devices_lock);
-		list_del_rcu(&(*dev)->list);
-		spin_unlock(&devices_lock);
-		synchronize_rcu();
 		free_netdev(netdev);
 		goto out;
 	}
 
+	spin_lock(&devices_lock);
+	list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
+	spin_unlock(&devices_lock);
+
 	BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
 	       netdev->ifindex, &chan->dst, chan->dst_type,
 	       &chan->src, chan->src_type);
-- 
2.51.0.windows.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] Bluetooth: 6lowpan: unlink closing device and protect RCU lookups
  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 10:28 ` Bin Luo (University of Electronic Science and Technology of China)
  1 sibling, 0 replies; 3+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-30 10:28 UTC (permalink / raw)
  To: marcel, luiz.dentz; +Cc: linux-bluetooth, linux-kernel, stable

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-30 10:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 10:28 ` [PATCH 2/2] Bluetooth: 6lowpan: unlink closing device and protect RCU lookups Bin Luo (University of Electronic Science and Technology of China)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox