Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2255936iof; Wed, 8 Jun 2022 00:36:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoUJMf2i6Bte7A5F+sCKycDiud1heU8yNjw2hA7bOHFjed0LSUZX4jJcC8VsVW6it0wW0J X-Received: by 2002:a05:6a00:248b:b0:51c:4f53:a932 with SMTP id c11-20020a056a00248b00b0051c4f53a932mr3430126pfv.51.1654673813184; Wed, 08 Jun 2022 00:36:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654673813; cv=none; d=google.com; s=arc-20160816; b=MX2G7ZxMuJWhA3+dB0AFjeMRPlS1/rheRJopdSlZ9rXoUjv4XmXz98xjvCdRDmwb59 9R23+M9fqUI7iIDNGsZyddlxkcguGrZS+Q/JI99PmE8wr+6iqC3K59VW4s6iaR3MMQUS WGK4qpOTwsYcUccfuKreVMPExE3XCXb7qhYedP92btSzdcd8RJmEhyMMobKMZVEeOrqF 0qwUD7R3y88gHCInzEsSnuKCOWI+eVYbC35EQ+IDXTmCwai69uP4Rb/MBmmqxFafo1N7 mJbtfE/u9MIsG/QOMdsW08yB4T0uNV8fFUVOgxIrzWIVzBy4wJU2Qtjdw0KeWqM3nv2v J6cQ== 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=YOUCHHjugQfmHkF5V5xPYOFDFx3eqbsG5bU1zno8xhU=; b=R1oW+xkBWobgsWHBNQ8nIcCycFrf3bdEjE4Kb4PwG/mMHg8gxDc5u2o/tUT11xp6ok PiBWzRs/XU2O5MrqLF9aNPTiY36s3pjrIswGhG6LVI27Z4LJa1LsAfCidAC8/ZgiEbhN 5qrFI+o36mFakFdBo2RgPdIt4wmtrx8aoq7pjn1o+QuQp3D7ZeAGK9qtAf9ups038LKq KiCgfuQbr24bxWHF2bDhDs3NTdqIautDeW/nKPfM0nJ96WPBtKZED208pEa1X9FbKuJ5 u9NhRGVckAxdy7Fo+LNH7BIXd0/xqW0fB9bxn/2jyq7Q8hTj+SRIIZPlTrZWIbjV2uwU yRqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tWlQS8s5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id e7-20020a656bc7000000b003fdc8b4d872si12722157pgw.602.2022.06.08.00.36.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 00:36:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tWlQS8s5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3EF7024852A; Wed, 8 Jun 2022 00:07:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386184AbiFGWsa (ORCPT + 99 others); Tue, 7 Jun 2022 18:48:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1381299AbiFGVk0 (ORCPT ); Tue, 7 Jun 2022 17:40:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA2FE1C4F3B; Tue, 7 Jun 2022 12:06:23 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 78674617CC; Tue, 7 Jun 2022 19:06:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85E8FC385A2; Tue, 7 Jun 2022 19:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654628782; bh=fpu6zjifZacXCfoqg3slzQtn/2nAGxzDEeHjW3aa+ms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tWlQS8s5IIgSxZ90xadqixuOMsVh5FW4ILV50+m1Bf1sg87Qa+WBNhmy/V/V48vYb rH1PkSNaJpGIQAqjWkC5uwBSSdblN93hJMk7s/lVRRzMgMDiaUjTreqz+ppsltAd9k K7A3VgOrwECKYLQNWzXCoqYb5zIc21AdGaH9UodY= 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.18 452/879] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Tue, 7 Jun 2022 18:59:30 +0200 Message-Id: <20220607165015.988982549@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 2a58c7d88433..1111da4e2f2b 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -574,19 +574,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