From: Mingshu Liu <26031212613@stu.xidian.edu.cn> A double-free of struct sk_buff and a subsequent use-after-free were observed in virtbt_send_frame() when virtqueue_add_outbuf() fails. hci_send_frame() frees the same skb again after hdev->send() returns an error. This issue was triggered by our custom device emulation and fuzzing framework on Linux 7.3 (7.3.0-00001-g0aef6eb5c859-dirty). The extra local commit only adds kcov and does not touch virtio_bt.c. The same error path is still present on mainline. The failure depends on a broken TX virtqueue: the device first completes a buffer whose used-ring id is not a head, the queue is marked broken, and the next virtqueue_add_outbuf() returns -EIO. The crash trace is as follows: virtio_bt virtio0: tx:id 3 is not a head! Bluetooth: hci0: sending frame failed (-5) BUG: KASAN: slab-use-after-free in sk_skb_reason_drop+0x3c/0x1b0 Read of size 4 at addr ffff88800ae5bee4 by task kworker/u17:0/71 Workqueue: hci0 hci_tx_work Call Trace: sk_skb_reason_drop+0x3c/0x1b0 hci_send_frame+0x34a/0x420 hci_tx_work+0x87a/0xcd0 process_one_work+0x8cd/0x1900 worker_thread+0x5a1/0xd10 kthread+0x325/0x410 ret_from_fork+0x566/0x7f0 ret_from_fork_asm+0x1a/0x30 Allocated by task 50: __alloc_skb+0x14c/0x6e0 l2cap_send_cmd+0x96/0xbf0 l2cap_conn_start+0x33c/0x9e0 l2cap_info_timeout+0x82/0xa0 Freed by task 71: kfree_skbmem+0x18a/0x1f0 sk_skb_reason_drop+0x10e/0x1b0 virtbt_send_frame+0x1bc/0x210 hci_send_frame+0x26a/0x420 hci_tx_work+0x87a/0xcd0 The object belongs to skbuff_head_cache and is 240 bytes. The 4-byte read is skb->users. The issue arises because virtbt_send_frame() calls kfree_skb(skb) when virtqueue_add_outbuf() fails and still returns that error. hci_send_frame() treats a negative return as "the caller still owns the skb" and frees it a second time. Both calls run in the same hci_tx_work. The skb was never queued to the device. Fix this by leaving the skb to hci_send_frame() on the error path. Undo the preceding skb_push() so the buffer handed back matches the one the caller passed in. Fixes: afd2daa26c7a ("Bluetooth: Add support for virtio transport driver") Cc: stable@vger.kernel.org Signed-off-by: Mingshu Liu <26031212613@stu.xidian.edu.cn> --- drivers/bluetooth/virtio_bt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/bluetooth/virtio_bt.c b/drivers/bluetooth/virtio_bt.c index c20d54088..c6bb602b4 100644 --- a/drivers/bluetooth/virtio_bt.c +++ b/drivers/bluetooth/virtio_bt.c @@ -103,7 +103,7 @@ static int virtbt_send_frame(struct hci_dev *hdev, struct sk_buff *skb) err = virtqueue_add_outbuf(vbt->vqs[VIRTBT_VQ_TX], sg, 1, skb, GFP_KERNEL); if (err) { - kfree_skb(skb); + skb_pull(skb, 1); return err; } -- 2.34.1