Linux bluetooth development
 help / color / mirror / Atom feed
From: Dan Carpenter <error27@gmail.com>
To: Haoxiang Li <haoxiang_li2024@163.com>
Cc: marcel@holtmann.org, luiz.dentz@gmail.com,
	yangyingliang@huawei.com, mst@redhat.com,
	linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH] Bluetooth: virtio_bt: unregister HCI device on open failure
Date: Wed, 24 Jun 2026 13:56:20 +0300	[thread overview]
Message-ID: <aju31PkIMuvyIcCR@stanley.mountain> (raw)
In-Reply-To: <20260624084333.2885144-1-haoxiang_li2024@163.com>

On Wed, Jun 24, 2026 at 04:43:33PM +0800, Haoxiang Li wrote:
> virtbt_probe() registers the HCI device before calling
> virtbt_open_vdev(). If opening the virtio Bluetooth
> device fails, the error path frees the HCI device without
> unregistering it.
> 
> Fixes: dc65b4b0f90a ("Bluetooth: virtio_bt: fix device removal")
> Cc: stable@vger.kernel.org
> Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
> ---
>  drivers/bluetooth/virtio_bt.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/bluetooth/virtio_bt.c b/drivers/bluetooth/virtio_bt.c
> index 140ab55c9fc5..bf6827431bb8 100644
> --- a/drivers/bluetooth/virtio_bt.c
> +++ b/drivers/bluetooth/virtio_bt.c
> @@ -397,6 +397,7 @@ static int virtbt_probe(struct virtio_device *vdev)
>  	return 0;
>  
>  open_failed:
> +	hci_unregister_dev(hdev);
>  	hci_free_dev(hdev);
>  failed:
>  	vdev->config->del_vqs(vdev);

I have written a blog about how to write error handling.
https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/

Originally this code using One Err style error handling where every
error path just did "goto fail".  It's also using ComeFrom label names
which don't say what the goto does only where the goto is...  Ideally if
hci_register_dev() failed it would use the unwind ladder to clean up it
instead calls hci_free_dev() and then goto fail.

The beauty of writing a normal kernel style unwind ladder is that it
writes the cleanup function automatically...  Let's look at the cleanup
function here.

   406  static void virtbt_remove(struct virtio_device *vdev)
   407  {
   408          struct virtio_bluetooth *vbt = vdev->priv;
   409          struct hci_dev *hdev = vbt->hdev;
   410  
   411          hci_unregister_dev(hdev);
   412          virtio_reset_device(vdev);
   413          virtbt_close_vdev(vbt);

I'm really uncomfortable with having the hci_unregister_dev()
before the close.  Potential use after free?

   414  
   415          hci_free_dev(hdev);
   416          vbt->hdev = NULL;
   417  
   418          vdev->config->del_vqs(vdev);
   419          kfree(vbt);

The probe function should free "vbt" but it doesn't so that's
another leak.

   420  }

So this fix is fine but it's also only a partial fix.

regards,
dan carpenter



  parent reply	other threads:[~2026-06-24 10:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24  8:43 [PATCH] Bluetooth: virtio_bt: unregister HCI device on open failure Haoxiang Li
2026-06-24  9:10 ` Paul Menzel
2026-06-24 10:56 ` Dan Carpenter [this message]
2026-06-24 11:57 ` bluez.test.bot

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=aju31PkIMuvyIcCR@stanley.mountain \
    --to=error27@gmail.com \
    --cc=haoxiang_li2024@163.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=mst@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=yangyingliang@huawei.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox