Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp619110iog; Mon, 13 Jun 2022 09:15:58 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tY2jUmQS43ut/FSCbjP4U1w2wObJ8dUui29fUgzDhHMzL8MGo2WVEwn2s7Rs+fyK3jbPfy X-Received: by 2002:a17:902:dac3:b0:164:13b2:4913 with SMTP id q3-20020a170902dac300b0016413b24913mr356347plx.169.1655136958173; Mon, 13 Jun 2022 09:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655136958; cv=none; d=google.com; s=arc-20160816; b=hQZbV4N5rUx0571pAfyoOg1EyqmeAexQ21DSEZ3ETUkJlKArAnRpZN+RaLbHpXoVKj Et5l2az5s3wHEDds9WRi8tXyloITLBlLK5pAJlkquXIbq1L/qRjsqdy3X2hgTQEiJdVN hTTTepiUzglzYdytIzM4T2ZzzkQudc7VPQmftMYy+mM987bbOeQecoh6TNMEz85tIy89 e0yv9wVC6FJr4BJi0omiEANNxJsva6l4X2Af4NptZoyYrrU6f/CeHOe6t1mtbfxJEnJq 7vhzHM4p56ww6YRNAxmXw2xLqzOitDBXRvsTc6A9gxnEtlOBCIJaOCh0V4JjjZYu0uTi 5trw== 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=f7yeWixOtuE1Fr3riB8de2ySjTCEJj5TJ/UY4Ak6YlM=; b=OJmCshpbHBxHSgSuMWV139Uw2FHoFzu1i6PnTRsV0oiq8tvw0LMi2gPMgdZcdc8oRm kPAWiYx2BaFIqFrDzjC7claK5sGJzRpfqngZNLLtRg48UlqWtYt+CBu7rgw12froYaFZ lSNBnAivgCA48k94trKpRUmCG6DNG69xy9GfaGwDBoVrKg3YTW5q1HHRZa7FNlcvyh1o rkqTo6FQt0X66wIZ8/meevnjk+dG4MNiqSgxiBVu8XoR/QAIngg7tm15zL7eu1pgFzUx cal9H6LY9CukWadmnJdkovxfHlT9qy92pS/0zMiOojPiqaRGj1uOg4/enalImY3U/QEJ yr0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="jvA/Ddch"; 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 m9-20020a17090a2c0900b001e299f85017si14203653pjd.63.2022.06.13.09.15.44; Mon, 13 Jun 2022 09:15:58 -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="jvA/Ddch"; 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 S243025AbiFMKV5 (ORCPT + 99 others); Mon, 13 Jun 2022 06:21:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242439AbiFMKUC (ORCPT ); Mon, 13 Jun 2022 06:20:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E61C19038; Mon, 13 Jun 2022 03:17:04 -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 0C7DB608D6; Mon, 13 Jun 2022 10:17:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F9F6C34114; Mon, 13 Jun 2022 10:17:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115423; bh=V283wF978pJsjQ4E41SQ6PoUgotyKrWyWdqv1ECGuvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jvA/DdchI+58yeLnFHIEvVgcoYf3I/2vqjBREC67ouyUUsQ+Kpw8epYH7yuUFElCI LdyNoW3dTTPqxIcSdGunDFeTksDptESnky+LkS6qk/XwAzkviDlvsvybLdCbRkiAbm hGsk99cwoO8/dEB0HgfyHQwAlZVGld8cLv4Jg638= 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 4.9 057/167] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Mon, 13 Jun 2022 12:08:51 +0200 Message-Id: <20220613094854.233878885@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@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 b3b4ffaa394f..9892ce82cbdf 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -542,19 +542,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