Received: by 2002:ac0:8c8e:0:0:0:0:0 with SMTP id r14csp901324ima; Wed, 6 Feb 2019 10:09:27 -0800 (PST) X-Google-Smtp-Source: AHgI3IY1wBRixopC9pB633QvtBhtIbfpx85mZSy0xgRQWPV7dngcKad4UyW86nWgeETKl5U2iafk X-Received: by 2002:a63:5026:: with SMTP id e38mr10738589pgb.123.1549476567247; Wed, 06 Feb 2019 10:09:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549476567; cv=none; d=google.com; s=arc-20160816; b=QUdyIHWS7lyJHwX2R/LzKPbcTtN9qTH7Fr5ZNsPb6KJYGPj/4BXML8Q+P+CRre02wq MU2F1qtxqx7lyXXgvedYMOyW5ZHqICj2XF+35h7YbZ5plMbUkVDHbjR7ELsMONpnDQFP LtyrNlFG/Yxd3h+AtIkTi2RjSIVCedzFv62FOf3xxX6jeMGufv/VDqpQuqNN/xAqT+AF j1jBuGh0EsDQA7cg+D2wINyousC3R1QCgL3PYyXf9MfrHwL9a22PlgAWXbgkD2kgT0HF pv7OcRH3atDxoIGmDZUn6c++xV9VGfmC9vhuFlQIm63oclKC0rNV87vpunmKs3+uTtS2 W8qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=irct3nXYNu5nWtJVO5NLT+HtUuxdkLPl0Wh5vVXTwjY=; b=V5/QzPp4c8jir57VeUFa+YHRpRKJvVPkTn417FltwVYAdw+a7hKW5zl9xPnVSZozcg e5/XmDBmUjDtNBD1Np0+wykyCp8K/PGNCLGA8z+kIfGx+jtmXmgaS1txrzgT350/8lSw QIC6J/NaHo9fEvdnVn5J0uuEBLePcCKHJ8cPvixYkh78n5sfu9z9lhNMfPw4D1j1+/Qa UWCbYvUl15tAY1pElUY/SP7FeBjD+YQIXrf5qToRK+RcL7Mux0PzbUaBbs9+Pb2rgBIT 05Uf+yXTwp0lS3AQusBY9GCl1Acu4VjHTm6hkhtiINrJuV3O3URGKqQH+Pz6KXPgharG 9HZw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g31si6798846pld.358.2019.02.06.10.09.09; Wed, 06 Feb 2019 10:09:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730851AbfBFRyY (ORCPT + 99 others); Wed, 6 Feb 2019 12:54:24 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49516 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726270AbfBFRyY (ORCPT ); Wed, 6 Feb 2019 12:54:24 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F2C8BC065F8D; Wed, 6 Feb 2019 17:54:23 +0000 (UTC) Received: from laptop.jcline.org (ovpn-124-222.rdu2.redhat.com [10.10.124.222]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9BA105D9D6; Wed, 6 Feb 2019 17:54:23 +0000 (UTC) Received: from laptop.jcline.org.com (localhost [IPv6:::1]) by laptop.jcline.org (Postfix) with ESMTP id 9E66A7045B06; Wed, 6 Feb 2019 12:54:22 -0500 (EST) From: Jeremy Cline To: Marcel Holtmann , Johan Hedberg Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, Jeremy Cline , syzbot+257790c15bcdef6fe00c@syzkaller.appspotmail.com Subject: [PATCH] Bluetooth: hci_ldisc: Initialize hci_dev before open() Date: Wed, 6 Feb 2019 12:54:16 -0500 Message-Id: <20190206175416.4627-1-jcline@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 06 Feb 2019 17:54:24 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The hci_dev struct hdev is referenced in work queues and timers started by open() in some protocols. This creates a race between the initialization function and the work or timer which can result hdev being dereferenced while it is still null. The syzbot report contains a reliable reproducer which causes a null pointer dereference of hdev in hci_uart_write_work() by making the memory allocation for hdev fail. To fix this, ensure hdev is valid from before calling a protocol's open() until after calling a protocol's close(). Reported-by: syzbot+257790c15bcdef6fe00c@syzkaller.appspotmail.com Signed-off-by: Jeremy Cline --- drivers/bluetooth/hci_ldisc.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/drivers/bluetooth/hci_ldisc.c b/drivers/bluetooth/hci_ldisc.c index fbf7b4df23ab..4918fefc4a6f 100644 --- a/drivers/bluetooth/hci_ldisc.c +++ b/drivers/bluetooth/hci_ldisc.c @@ -207,11 +207,11 @@ void hci_uart_init_work(struct work_struct *work) err = hci_register_dev(hu->hdev); if (err < 0) { BT_ERR("Can't register HCI device"); + clear_bit(HCI_UART_PROTO_READY, &hu->flags); + hu->proto->close(hu); hdev = hu->hdev; hu->hdev = NULL; hci_free_dev(hdev); - clear_bit(HCI_UART_PROTO_READY, &hu->flags); - hu->proto->close(hu); return; } @@ -616,6 +616,7 @@ static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, static int hci_uart_register_dev(struct hci_uart *hu) { struct hci_dev *hdev; + int err; BT_DBG(""); @@ -659,11 +660,22 @@ static int hci_uart_register_dev(struct hci_uart *hu) else hdev->dev_type = HCI_PRIMARY; + /* Only call open() for the protocol after hdev is fully initialized as + * open() (or a timer/workqueue it starts) may attempt to reference it. + */ + err = hu->proto->open(hu); + if (err) { + hu->hdev = NULL; + hci_free_dev(hdev); + return err; + } + if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags)) return 0; if (hci_register_dev(hdev) < 0) { BT_ERR("Can't register HCI device"); + hu->proto->close(hu); hu->hdev = NULL; hci_free_dev(hdev); return -ENODEV; @@ -683,17 +695,12 @@ static int hci_uart_set_proto(struct hci_uart *hu, int id) if (!p) return -EPROTONOSUPPORT; - err = p->open(hu); - if (err) - return err; - hu->proto = p; set_bit(HCI_UART_PROTO_READY, &hu->flags); err = hci_uart_register_dev(hu); if (err) { clear_bit(HCI_UART_PROTO_READY, &hu->flags); - p->close(hu); return err; } -- 2.20.1