Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2162720iof; Tue, 7 Jun 2022 21:50:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweD0gZP5l0TZUPYttnlPc/qwrpX3hWRDFLe3VaDqTkviav0spoxv8lLRujYOq9q2S9lBwm X-Received: by 2002:a63:f09:0:b0:3fd:7e20:6508 with SMTP id e9-20020a630f09000000b003fd7e206508mr16223366pgl.32.1654663842824; Tue, 07 Jun 2022 21:50:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654663842; cv=none; d=google.com; s=arc-20160816; b=UOVQHsvglBN/gq90jCNtxYwJsmbQ1AZ2ttGl5tuRFnoju51P0n3wCK6qsm6dOXqfl9 mh8esQXeu2AyeNUxa9lihRlEvnTmJorjURutiwITrIA3FFU8rmWzIJb22Jh1AYIraO4q Raa3Dxlg2Rbxp2P5k33bNIgRjqIaJePuFB/2F69iLJGXYu62bdqkOk+RQ+Cj6nlzW/cx NQ7xMf8MQKdYR6pKiulZFHEvLlDtiL8zd1ZUGZ4ow+EIIuy4ruMbqpaUOjAeVwVAKIQa Iviqd+6dwhOjYY9lQ0MkuwHD7LOI2vH2naBDeDZNCJGVwrvoqTcZBU6wIgnscWmvXHws JG/w== 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=xBmuK+1ha7Qx9sRTM6eJT1k40l2gmRbe+0IjVXT4BvdT7IXtfZ6VIvq0Hh16qHPBK9 L0zOZWdzSZFbRfdcGwZO33f7RPNAZFqXd+HzEQ+BIkORSS3OALjP6bVmT0Y5G4uR4jMN jlMoaYIjbFW72wYJfkJhGWGOPlMmw5qNGJKVlSdTIheO/D6aAq+DtCWsEXaJ1lw+hDy3 qwyE+kxZCctsLc9W4+Ca3bqODUztemDZ9pKkW2bsB1p7O/ANAdKmZEmgXKgm5A/wvEnd PFSou9ozGNuzmYy19BzSA0axekdDHJuUNiwaNXs+dNZ6V7mrta7iYrVm1t80jMf4gyXM 9HEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gpAEN1d8; 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 u196-20020a6379cd000000b003c6115ef99esi27650983pgc.49.2022.06.07.21.50.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:50:42 -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=gpAEN1d8; 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 E4C92419044; Tue, 7 Jun 2022 21:20:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379498AbiFGVKH (ORCPT + 99 others); Tue, 7 Jun 2022 17:10:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359659AbiFGUMf (ORCPT ); Tue, 7 Jun 2022 16:12:35 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A09B8A3383; Tue, 7 Jun 2022 11:27:53 -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 D0293B82382; Tue, 7 Jun 2022 18:27:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F185C385A5; Tue, 7 Jun 2022 18:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654626470; bh=fpu6zjifZacXCfoqg3slzQtn/2nAGxzDEeHjW3aa+ms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gpAEN1d8OVkY5jfF+XpL49rc9V0aorSiGUnAyKgrzfhB1F6NmWpnaEeTMjn2P7cOc 8LQscEbbk8M4fDcJl+S/829d/1f9Wg7LKQGsJux84dzJb5aTezOHqBJSLS+Jca88dp 0qBFojwNkYBEgQ7H4MsUZz6our8HV40MKT8l+wgc= 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.17 383/772] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Tue, 7 Jun 2022 18:59:35 +0200 Message-Id: <20220607165000.301923894@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164948.980838585@linuxfoundation.org> References: <20220607164948.980838585@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