Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp584722iog; Mon, 13 Jun 2022 08:36:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ssjxz89XejuonOLwGUouG1rENt0eecSafn65qxZ6pkS3pmVNlRb5g43Ix4E2RW2jAQ63ha X-Received: by 2002:a17:902:7105:b0:168:dfe3:66e0 with SMTP id a5-20020a170902710500b00168dfe366e0mr258379pll.88.1655134572693; Mon, 13 Jun 2022 08:36:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655134572; cv=none; d=google.com; s=arc-20160816; b=InbikI78ujSkDocf6qOC/C1jpGA8NRVMjUlLkb5Lom7GjTLW9LFBvxG/2SZVj9O8O4 N17kpowiDwy/cj5IHoLrn8NLyGtD+bxKEkBEo7u7Ed7uFPoh+f50Awoj94P6VG/eZhtO AXrmtwuXq9ppsuj4Y9k68PmK4LihTq53W/znMiBnubrxMgrTEn9y1GOTU0Po7HqTqAfB 4IDFcMiGCv0ila6MsTWeKVe0wvMMRio+T9t9MIa0V+Rt3W3iuPzun7adP+fAqVTpVIhW 9S5tdDZEdDkxb4MNY30iWdqB+n/g+n2PCMZziV6HXUiR6Zr4iTFmvWr30GDUH5dt7c5M 4tyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LdToUZc8LUBMrmv4ULehM/teh3acTPniFg0Eygh1Z2o=; b=g2L2OCFkCqGEEhhFhu3eRP25uzIo4DoiL4cZPOXAt5XQAwXpiLoTXXiBUG/4OOduf9 Dk41qpFXwbYDndmYhyTUeLoxr8X6JFRSLZOUSniRI7InIABJGYHqNc0xiKo+nMqw26XL vsfVVDecRkmD+1/R1CgOK8NJHnzrfImQ2+c9ITQ89VMBiD8DTfohTVIf2UyQdFqYKkaM k8ow+BqW9ve4jpo1/8jlCAiWBGQNGm5nMD/BE6L4lL8brbCM+XYQfVSbb77aXxZtZ2XA gQzIkf2PNSVQRsGM/KlW7zvtr2SMAWp8JQVkp2DTG1U8UQ/YkitdhbiVILdRTa2hRZbq kuNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zLZaoBtU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j6-20020a62b606000000b0050fac0168c6si8201373pff.49.2022.06.13.08.36.00; Mon, 13 Jun 2022 08:36:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zLZaoBtU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353300AbiFMLX0 (ORCPT + 99 others); Mon, 13 Jun 2022 07:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353658AbiFMLQK (ORCPT ); Mon, 13 Jun 2022 07:16:10 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 495A312D2F; Mon, 13 Jun 2022 03:38:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0B72FB80EAB; Mon, 13 Jun 2022 10:38:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70A85C34114; Mon, 13 Jun 2022 10:38:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116724; bh=nGXdZSFzDkEWuQCg5DmXXNsQ4HwouMFmglUtOGRhmhc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zLZaoBtUYqmVxJk6mTVOP/XOEtLzrjp3RESc4gq8KCs7NyLTfEDq3w27Xod+Dge94 Xh+oxWvVLGztPtvnufXSVdlBw/D6Bj16YQV9xe0qeq/9QCrKaCIxsVvYfrSirJealg Enenl5c/KIKMvjRf17gqNpl8fwovgdHju+IdZb5k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com, Ying Hsu , Joseph Hwang , Marcel Holtmann , Sasha Levin Subject: [PATCH 5.4 140/411] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Mon, 13 Jun 2022 12:06:53 +0200 Message-Id: <20220613094932.867978425@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ying Hsu [ Upstream commit 7aa1e7d15f8a5b65f67bacb100d8fc033b21efa2 ] Connecting the same socket twice consecutively in sco_sock_connect() could lead to a race condition where two sco_conn objects are created but only one is associated with the socket. If the socket is closed before the SCO connection is established, the timer associated with the dangling sco_conn object won't be canceled. As the sock object is being freed, the use-after-free problem happens when the timer callback function sco_sock_timeout() accesses the socket. Here's the call trace: dump_stack+0x107/0x163 ? refcount_inc+0x1c/ print_address_description.constprop.0+0x1c/0x47e ? refcount_inc+0x1c/0x7b kasan_report+0x13a/0x173 ? refcount_inc+0x1c/0x7b check_memory_region+0x132/0x139 refcount_inc+0x1c/0x7b sco_sock_timeout+0xb2/0x1ba process_one_work+0x739/0xbd1 ? cancel_delayed_work+0x13f/0x13f ? __raw_spin_lock_init+0xf0/0xf0 ? to_kthread+0x59/0x85 worker_thread+0x593/0x70e kthread+0x346/0x35a ? drain_workqueue+0x31a/0x31a ? kthread_bind+0x4b/0x4b ret_from_fork+0x1f/0x30 Link: https://syzkaller.appspot.com/bug?extid=2bef95d3ab4daa10155b Reported-by: syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com Fixes: e1dee2c1de2b ("Bluetooth: fix repeated calls to sco_sock_kill") Signed-off-by: Ying Hsu Reviewed-by: Joseph Hwang Signed-off-by: Marcel Holtmann Signed-off-by: Sasha Levin --- net/bluetooth/sco.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index 2c616c1c6295..fbfb12e43010 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -563,19 +563,24 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen addr->sa_family != AF_BLUETOOTH) return -EINVAL; - if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) - return -EBADFD; + lock_sock(sk); + if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { + err = -EBADFD; + goto done; + } - if (sk->sk_type != SOCK_SEQPACKET) - return -EINVAL; + if (sk->sk_type != SOCK_SEQPACKET) { + err = -EINVAL; + goto done; + } hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR); - if (!hdev) - return -EHOSTUNREACH; + if (!hdev) { + err = -EHOSTUNREACH; + goto done; + } hci_dev_lock(hdev); - lock_sock(sk); - /* Set destination address and psm */ bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr); -- 2.35.1