Skip to content

Commit

Permalink
Bluetooth: vhci: Fix race at creating hci device
Browse files Browse the repository at this point in the history
commit c7c999c upstream.

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.

Signed-off-by: Takashi Iwai <[email protected]>
Signed-off-by: Marcel Holtmann <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
tiwai authored and gregkh committed Jun 1, 2016
1 parent 3295bfd commit 2ceff6c
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions drivers/bluetooth/hci_vhci.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -87,12 +88,15 @@ 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;
__u8 dev_type;

if (data->hdev)
return -EBADFD;

/* bits 0-1 are dev_type (BR/EDR or AMP) */
dev_type = opcode & 0x03;

Expand Down Expand Up @@ -151,6 +155,17 @@ 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);
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)
{
Expand Down Expand Up @@ -191,11 +206,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);

Expand Down Expand Up @@ -320,6 +330,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;
Expand Down

0 comments on commit 2ceff6c

Please sign in to comment.