All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: vhci: Fix race at creating hci device
@ 2016-04-14 15:32 Takashi Iwai
  2016-04-20 13:16 ` Marcel Holtmann
  2016-04-20 14:32 ` Marcel Holtmann
  0 siblings, 2 replies; 5+ messages in thread
From: Takashi Iwai @ 2016-04-14 15:32 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: Marcel Holtmann, Gustavo Padovan, Johan Hedberg, Jiri Slaby,
	linux-kernel

hci_vhci driver creates a hci device object dynamically upon each
HCI_VENDOR_PKT write.  Although it checks the already created object
and returns an error, it's still racy and may build multiple hci_dev
objects concurrently when parallel writes are performed, as the device
tracks only a single hci_dev object.

This patch introduces a mutex to protect against the concurrent device
creations.

Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 drivers/bluetooth/hci_vhci.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c
index f67ea1c090cb..39230f30f544 100644
--- a/drivers/bluetooth/hci_vhci.c
+++ b/drivers/bluetooth/hci_vhci.c
@@ -50,6 +50,7 @@ struct vhci_data {
 	wait_queue_head_t read_wait;
 	struct sk_buff_head readq;
 
+	struct mutex open_mutex;
 	struct delayed_work open_timeout;
 };
 
@@ -87,7 +88,7 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
 	return 0;
 }
 
-static int vhci_create_device(struct vhci_data *data, __u8 opcode)
+static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
 {
 	struct hci_dev *hdev;
 	struct sk_buff *skb;
@@ -151,6 +152,19 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
 	return 0;
 }
 
+static int vhci_create_device(struct vhci_data *data, __u8 opcode)
+{
+	int err;
+
+	mutex_lock(&data->open_mutex);
+	if (data->hdev)
+		err = -EBADFD;
+	else
+		err = __vhci_create_device(data, opcode);
+	mutex_unlock(&data->open_mutex);
+	return err;
+}
+
 static inline ssize_t vhci_get_user(struct vhci_data *data,
 				    struct iov_iter *from)
 {
@@ -191,11 +205,6 @@ static inline ssize_t vhci_get_user(struct vhci_data *data,
 	case HCI_VENDOR_PKT:
 		cancel_delayed_work_sync(&data->open_timeout);
 
-		if (data->hdev) {
-			kfree_skb(skb);
-			return -EBADFD;
-		}
-
 		opcode = *((__u8 *) skb->data);
 		skb_pull(skb, 1);
 
@@ -320,6 +329,7 @@ static int vhci_open(struct inode *inode, struct file *file)
 	skb_queue_head_init(&data->readq);
 	init_waitqueue_head(&data->read_wait);
 
+	mutex_init(&data->open_mutex);
 	INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
 
 	file->private_data = data;
-- 
2.8.1

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

* Re: [PATCH] Bluetooth: vhci: Fix race at creating hci device
  2016-04-14 15:32 [PATCH] Bluetooth: vhci: Fix race at creating hci device Takashi Iwai
@ 2016-04-20 13:16 ` Marcel Holtmann
  2016-04-20 13:35   ` Takashi Iwai
  2016-04-20 14:32 ` Marcel Holtmann
  1 sibling, 1 reply; 5+ messages in thread
From: Marcel Holtmann @ 2016-04-20 13:16 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Linux Bluetooth, Gustavo F. Padovan, Johan Hedberg, Jiri Slaby,
	linux-kernel

Hi Takashi,

> hci_vhci driver creates a hci device object dynamically upon each
> HCI_VENDOR_PKT write.  Although it checks the already created object
> and returns an error, it's still racy and may build multiple hci_dev
> objects concurrently when parallel writes are performed, as the device
> tracks only a single hci_dev object.
> 
> This patch introduces a mutex to protect against the concurrent device
> creations.
> 
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
> drivers/bluetooth/hci_vhci.c | 22 ++++++++++++++++------
> 1 file changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c
> index f67ea1c090cb..39230f30f544 100644
> --- a/drivers/bluetooth/hci_vhci.c
> +++ b/drivers/bluetooth/hci_vhci.c
> @@ -50,6 +50,7 @@ struct vhci_data {
> 	wait_queue_head_t read_wait;
> 	struct sk_buff_head readq;
> 
> +	struct mutex open_mutex;
> 	struct delayed_work open_timeout;
> };
> 
> @@ -87,7 +88,7 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
> 	return 0;
> }
> 
> -static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> +static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
> {
> 	struct hci_dev *hdev;
> 	struct sk_buff *skb;
> @@ -151,6 +152,19 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> 	return 0;
> }
> 
> +static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> +{
> +	int err;
> +
> +	mutex_lock(&data->open_mutex);
> +	if (data->hdev)
> +		err = -EBADFD;
> +	else
> +		err = __vhci_create_device(data, opcode);
> +	mutex_unlock(&data->open_mutex);
> +	return err;
> +}
> +
> static inline ssize_t vhci_get_user(struct vhci_data *data,
> 				    struct iov_iter *from)
> {
> @@ -191,11 +205,6 @@ static inline ssize_t vhci_get_user(struct vhci_data *data,
> 	case HCI_VENDOR_PKT:
> 		cancel_delayed_work_sync(&data->open_timeout);
> 
> -		if (data->hdev) {
> -			kfree_skb(skb);
> -			return -EBADFD;
> -		}
> -

why not just have the mutex around this block and the vhci_create_device in the timeout. Wouldn't that achieve exactly the same.

Since when you actually remove this check, then you still can create another hci_dev by just writing another vendor packet. That is actually something we want to avoid.

> 		opcode = *((__u8 *) skb->data);
> 		skb_pull(skb, 1);
> 
> @@ -320,6 +329,7 @@ static int vhci_open(struct inode *inode, struct file *file)
> 	skb_queue_head_init(&data->readq);
> 	init_waitqueue_head(&data->read_wait);
> 
> +	mutex_init(&data->open_mutex);
> 	INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
> 
> 	file->private_data = data;

Regards

Marcel

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

* Re: [PATCH] Bluetooth: vhci: Fix race at creating hci device
  2016-04-20 13:16 ` Marcel Holtmann
@ 2016-04-20 13:35   ` Takashi Iwai
  0 siblings, 0 replies; 5+ messages in thread
From: Takashi Iwai @ 2016-04-20 13:35 UTC (permalink / raw)
  To: Marcel Holtmann
  Cc: Linux Bluetooth, Gustavo F. Padovan, Johan Hedberg, Jiri Slaby,
	linux-kernel

On Wed, 20 Apr 2016 15:16:57 +0200,
Marcel Holtmann wrote:
> 
> Hi Takashi,
> 
> > hci_vhci driver creates a hci device object dynamically upon each
> > HCI_VENDOR_PKT write.  Although it checks the already created object
> > and returns an error, it's still racy and may build multiple hci_dev
> > objects concurrently when parallel writes are performed, as the device
> > tracks only a single hci_dev object.
> > 
> > This patch introduces a mutex to protect against the concurrent device
> > creations.
> > 
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > ---
> > drivers/bluetooth/hci_vhci.c | 22 ++++++++++++++++------
> > 1 file changed, 16 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c
> > index f67ea1c090cb..39230f30f544 100644
> > --- a/drivers/bluetooth/hci_vhci.c
> > +++ b/drivers/bluetooth/hci_vhci.c
> > @@ -50,6 +50,7 @@ struct vhci_data {
> > 	wait_queue_head_t read_wait;
> > 	struct sk_buff_head readq;
> > 
> > +	struct mutex open_mutex;
> > 	struct delayed_work open_timeout;
> > };
> > 
> > @@ -87,7 +88,7 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
> > 	return 0;
> > }
> > 
> > -static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > +static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
> > {
> > 	struct hci_dev *hdev;
> > 	struct sk_buff *skb;
> > @@ -151,6 +152,19 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > 	return 0;
> > }
> > 
> > +static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > +{
> > +	int err;
> > +
> > +	mutex_lock(&data->open_mutex);
> > +	if (data->hdev)
> > +		err = -EBADFD;
> > +	else
> > +		err = __vhci_create_device(data, opcode);
> > +	mutex_unlock(&data->open_mutex);
> > +	return err;
> > +}
> > +
> > static inline ssize_t vhci_get_user(struct vhci_data *data,
> > 				    struct iov_iter *from)
> > {
> > @@ -191,11 +205,6 @@ static inline ssize_t vhci_get_user(struct vhci_data *data,
> > 	case HCI_VENDOR_PKT:
> > 		cancel_delayed_work_sync(&data->open_timeout);
> > 
> > -		if (data->hdev) {
> > -			kfree_skb(skb);
> > -			return -EBADFD;
> > -		}
> > -
> 
> why not just have the mutex around this block and the vhci_create_device in the timeout. Wouldn't that achieve exactly the same.

It's just a matter of taste :)  I prefer avoiding the duplicated
codes; instead of open-coding mutex_lock/unlock and data->hdev check
in two places, do it in the common helper.  If you prefer other way,
I'm fine with it.  Just let me know.  I'll resubmit the patch.

> Since when you actually remove this check, then you still can create another hci_dev by just writing another vendor packet. That is actually something we want to avoid.

No, it won't happen.  The removal of data->hdev in the above is merely
moving the check into the mutex protection in vhci_create_device().


thanks,

Takashi

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

* Re: [PATCH] Bluetooth: vhci: Fix race at creating hci device
  2016-04-14 15:32 [PATCH] Bluetooth: vhci: Fix race at creating hci device Takashi Iwai
  2016-04-20 13:16 ` Marcel Holtmann
@ 2016-04-20 14:32 ` Marcel Holtmann
  2016-04-20 14:55   ` Takashi Iwai
  1 sibling, 1 reply; 5+ messages in thread
From: Marcel Holtmann @ 2016-04-20 14:32 UTC (permalink / raw)
  To: Takashi Iwai
  Cc: Linux Bluetooth, Gustavo F. Padovan, Johan Hedberg, Jiri Slaby,
	linux-kernel

Hi Takashi,

> hci_vhci driver creates a hci device object dynamically upon each
> HCI_VENDOR_PKT write.  Although it checks the already created object
> and returns an error, it's still racy and may build multiple hci_dev
> objects concurrently when parallel writes are performed, as the device
> tracks only a single hci_dev object.
> 
> This patch introduces a mutex to protect against the concurrent device
> creations.
> 
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> ---
> drivers/bluetooth/hci_vhci.c | 22 ++++++++++++++++------
> 1 file changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c
> index f67ea1c090cb..39230f30f544 100644
> --- a/drivers/bluetooth/hci_vhci.c
> +++ b/drivers/bluetooth/hci_vhci.c
> @@ -50,6 +50,7 @@ struct vhci_data {
> 	wait_queue_head_t read_wait;
> 	struct sk_buff_head readq;
> 
> +	struct mutex open_mutex;
> 	struct delayed_work open_timeout;
> };
> 
> @@ -87,7 +88,7 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
> 	return 0;
> }
> 
> -static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> +static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
> {
> 	struct hci_dev *hdev;
> 	struct sk_buff *skb;
> @@ -151,6 +152,19 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> 	return 0;
> }
> 
> +static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> +{
> +	int err;
> +
> +	mutex_lock(&data->open_mutex);
> +	if (data->hdev)
> +		err = -EBADFD;

I moved this check into __vhci_create_device after applying your patch to bluetooth-next tree. I think that is a lot cleaner and no need for you to respin it.

Regards

Marcel

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

* Re: [PATCH] Bluetooth: vhci: Fix race at creating hci device
  2016-04-20 14:32 ` Marcel Holtmann
@ 2016-04-20 14:55   ` Takashi Iwai
  0 siblings, 0 replies; 5+ messages in thread
From: Takashi Iwai @ 2016-04-20 14:55 UTC (permalink / raw)
  To: Marcel Holtmann
  Cc: Linux Bluetooth, Gustavo F. Padovan, Johan Hedberg, Jiri Slaby,
	linux-kernel

On Wed, 20 Apr 2016 16:32:24 +0200,
Marcel Holtmann wrote:
> 
> Hi Takashi,
> 
> > hci_vhci driver creates a hci device object dynamically upon each
> > HCI_VENDOR_PKT write.  Although it checks the already created object
> > and returns an error, it's still racy and may build multiple hci_dev
> > objects concurrently when parallel writes are performed, as the device
> > tracks only a single hci_dev object.
> > 
> > This patch introduces a mutex to protect against the concurrent device
> > creations.
> > 
> > Cc: <stable@vger.kernel.org>
> > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > ---
> > drivers/bluetooth/hci_vhci.c | 22 ++++++++++++++++------
> > 1 file changed, 16 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/bluetooth/hci_vhci.c b/drivers/bluetooth/hci_vhci.c
> > index f67ea1c090cb..39230f30f544 100644
> > --- a/drivers/bluetooth/hci_vhci.c
> > +++ b/drivers/bluetooth/hci_vhci.c
> > @@ -50,6 +50,7 @@ struct vhci_data {
> > 	wait_queue_head_t read_wait;
> > 	struct sk_buff_head readq;
> > 
> > +	struct mutex open_mutex;
> > 	struct delayed_work open_timeout;
> > };
> > 
> > @@ -87,7 +88,7 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
> > 	return 0;
> > }
> > 
> > -static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > +static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
> > {
> > 	struct hci_dev *hdev;
> > 	struct sk_buff *skb;
> > @@ -151,6 +152,19 @@ static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > 	return 0;
> > }
> > 
> > +static int vhci_create_device(struct vhci_data *data, __u8 opcode)
> > +{
> > +	int err;
> > +
> > +	mutex_lock(&data->open_mutex);
> > +	if (data->hdev)
> > +		err = -EBADFD;
> 
> I moved this check into __vhci_create_device after applying your patch to bluetooth-next tree. I think that is a lot cleaner and no need for you to respin it.

OK, thanks!


Takashi

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

end of thread, other threads:[~2016-04-20 14:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-14 15:32 [PATCH] Bluetooth: vhci: Fix race at creating hci device Takashi Iwai
2016-04-20 13:16 ` Marcel Holtmann
2016-04-20 13:35   ` Takashi Iwai
2016-04-20 14:32 ` Marcel Holtmann
2016-04-20 14:55   ` Takashi Iwai

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.