From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965481AbcHNLwC (ORCPT ); Sun, 14 Aug 2016 07:52:02 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:52363 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965244AbcHNLbE (ORCPT ); Sun, 14 Aug 2016 07:31:04 -0400 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Marcel Holtmann" , "Takashi Iwai" Date: Sat, 13 Aug 2016 18:42:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 021/305] Bluetooth: vhci: Fix race at creating hci device In-Reply-To: X-SA-Exim-Connect-IP: 92.40.249.202 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.37-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Takashi Iwai commit c7c999cb18da88a881e10e07f0724ad0bfaff770 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 Signed-off-by: Marcel Holtmann [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- drivers/bluetooth/hci_vhci.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) --- 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; }; @@ -95,11 +96,14 @@ static int vhci_send_frame(struct hci_de return 0; } -static int vhci_create_device(struct vhci_data *data, __u8 dev_type) +static int __vhci_create_device(struct vhci_data *data, __u8 dev_type) { struct hci_dev *hdev; struct sk_buff *skb; + if (data->hdev) + return -EBADFD; + skb = bt_skb_alloc(4, GFP_KERNEL); if (!skb) return -ENOMEM; @@ -140,6 +144,17 @@ static int vhci_create_device(struct vhc 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, const struct iovec *iov, unsigned long count) @@ -185,11 +200,6 @@ static inline ssize_t vhci_get_user(stru case HCI_VENDOR_PKT: cancel_delayed_work_sync(&data->open_timeout); - if (data->hdev) { - kfree_skb(skb); - return -EBADFD; - } - dev_type = *((__u8 *) skb->data); skb_pull(skb, 1); @@ -318,6 +328,7 @@ static int vhci_open(struct inode *inode 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;