Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5648443img; Wed, 27 Mar 2019 12:24:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqwWbM5oHYMrb1XlKeaYSzjOWxkeY/ebkcoWyaFb1pUFVzyOtpIKwjM7q6ksWe4AUOeI7YsD X-Received: by 2002:a62:29c5:: with SMTP id p188mr36020787pfp.203.1553714672915; Wed, 27 Mar 2019 12:24:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553714672; cv=none; d=google.com; s=arc-20160816; b=wkRmzTifwYXlbyIspnTdGalbg89uo63WXZJyki6iARGmb4H8prFRbWxlDPpWh+ycff tlGoCU5AcV0kuXBnuRR7p6gkPatP7/OKC9+xMMGz/HukWFnwxhUHhrlAsqjGbuCW15Xo XQzcse8+AxV5UtufnYWFCtDRPkyg1UeLv893t5a4ZGFjZAWgHnkdPcXPluYD+u1xmUtL OfFbDMRv9FG3zKYppJ8KagYj6Gnb/Fwv8QjV+V0Vn5ERpxuAhXc34drjK0eeoC2cM/0Z V0z1/cL6mrXlZRUKyLW+99VzzNbOiShjGyDJJgt3Mn5l/Djiww8Zm6EUsoGnmnYeLjYn Lk+Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jtQ+MtAvDUxsqQiSPTf2DMesOEfwaaOqm3mdWyj+hBo=; b=BQmzmkKqTvRLZJtZaEP9qQg9DE+h5nmpZFh2F74VCBkG1mhj0xbt2ZD36MCUgGm30F 44XaDtO/PyEig2P9lBZRTIeQsESW8W8CtMcx0xeNxHRgIHpbDcqABCp6k1DaU5bbwX7L +h7IezG3l5HaVXz9bpH0XUphrfvD9jpycoobhUVlHR0l04N9xSpCNdIJsaGQ2NIYgLUw Z4UGpHJnRTYVQoaE5UOepMyYmc4cXKfo3c434eHo2B3scnFzv573e6Rs2dbVwBsiXxKf gpfbiwDcapDaYcoNxGMcY77nEd9X5aFDao22uFwPn8NriQThj/JAZaON6xyW3baFaa73 Joyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jDWI3bFa; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si21085116plw.390.2019.03.27.12.24.18; Wed, 27 Mar 2019 12:24:32 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=jDWI3bFa; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731498AbfC0SFz (ORCPT + 99 others); Wed, 27 Mar 2019 14:05:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:47210 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387790AbfC0SFx (ORCPT ); Wed, 27 Mar 2019 14:05:53 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EE09D217F9; Wed, 27 Mar 2019 18:05:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553709952; bh=RjFWTkJ6gpACcsLvmy+kyqlzVKRiTFDE3NNAdDRzJe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jDWI3bFaZ1WSNXvfc83hwzAuKjhu+/63MKSe0/TkawbShUWzPityAHZhRt6HZJLGu JqAcC55fSf9X8VdouQB5I+EbF+1PL9ncBs+xL09sTPDOX9aQq5fxAMTGscHJnwQRXW 6uvicYhUCX7CTexhHO2ppSp7TM/Acnc5bp04oprQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jeremy Cline , Marcel Holtmann , Sasha Levin , linux-bluetooth@vger.kernel.org Subject: [PATCH AUTOSEL 5.0 128/262] Bluetooth: hci_ldisc: Initialize hci_dev before open() Date: Wed, 27 Mar 2019 13:59:43 -0400 Message-Id: <20190327180158.10245-128-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jeremy Cline [ Upstream commit 32a7b4cbe93b0a0ef7e63d31ca69ce54736c4412 ] 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 Signed-off-by: Marcel Holtmann Signed-off-by: Sasha Levin --- 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.19.1