Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2109801pxb; Fri, 25 Mar 2022 11:15:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIicy+zqO1gW8l6Q7gHLGYI/n861YwfSSp8QroADO9EsE1g48WrRMMNVqDg0xNKpefdRx7 X-Received: by 2002:a05:6a00:228f:b0:4fa:e4c9:7b3b with SMTP id f15-20020a056a00228f00b004fae4c97b3bmr10847149pfe.61.1648232116797; Fri, 25 Mar 2022 11:15:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648232116; cv=none; d=google.com; s=arc-20160816; b=EPW53aViBKWNmz8kPliEKwNuOmbR0yDLqSK68MwSvmDVLFYopM4bpwo6tJBLk9I1RC ke6tpxh0rTclKoF6vhRceid1GiJSgHc+gkrSSMUwonQEX8cyU4RZ7abIdtACxV0TDCmM XMxgJQFDWj9ODyOp0nwC+rK6HDPZpCum7ht+9RFgvtHz4LsQlvE5Qb89L5/TYyZmpRzS xxbWtyY/xdSq/vekOEwosgUPKywjaHvrtACrJjCUQkuJ9zGCP0gsy/rCmxB2mBBgMP/f CcXTFOBXwkF3/Iq4zmiJcY96ZTTlIKasBqcG/Zb9xQySrY8+JZ5v5aBAuJ4IFtYP8w+5 vQdw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=GGiTeRG4IQk7h6C89ytVphusb4+2ymibartIH2Voor0=; b=zzgOGa8oMWttzUReORG+YOxvuZ0hgaZ7Og+/XyFg+mPRPESKNwo2nuOWEbYXLfWccJ Am7k5oU82LUXHvHxuV0oTwnoATZ8M2J6x7mQLg6cHiICrsWKZ5NuQsSat5sM5k0rdzzx hDZplIbXlcgD8NLh3OOrA0AGItsAnznlogAumcSUj5Fg7GRBdvKjslg6pZ3T87JPLiBd 5FEsOpGBPFPmA9xVE7dxTvmyie2Q8TtG6/nps4kZSArSFfXzapuDwJd7Kp4b3TPr2nTf QxG8QV8RGa/SerbZmZiQ9afyJB7RgEA+sm+pMU+GLBLVcuukf7P1tXS/x4WWufo++BiI /cvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jSy5FHrM; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j19-20020a17090a7e9300b001c660f6b972si6088955pjl.143.2022.03.25.11.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 11:15:16 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jSy5FHrM; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5D5E114F12B; Fri, 25 Mar 2022 10:44:36 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231800AbiCYDcg (ORCPT + 99 others); Thu, 24 Mar 2022 23:32:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239983AbiCYDcd (ORCPT ); Thu, 24 Mar 2022 23:32:33 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8156F9F3B5 for ; Thu, 24 Mar 2022 20:31:00 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id b8so6416546pjb.4 for ; Thu, 24 Mar 2022 20:31:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GGiTeRG4IQk7h6C89ytVphusb4+2ymibartIH2Voor0=; b=jSy5FHrMi1uSu+9XPjkA6dqRLZHrwSVVPGi/LShhirbbhIg051NvEtAfXFZQohs/64 XIjdKJvuE5u2BblRvrXXkEA1gfUwsaYQvUresbFcxHuNWXxMLwDOT8Ssi7PwdukIYdB4 28jFf7mVxW74xbdy+ZKfKxYR6FgRn35qxHdOY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GGiTeRG4IQk7h6C89ytVphusb4+2ymibartIH2Voor0=; b=R1lBTxrQElDmblV7VSdGFhsKHzXl8OrkRK3gHnFJGjl4YUMHgoRBrgWQ46TdSKm6WI vgYGYgHZ2kN676WOuAeTyC1Dn3SGfHVWfo/NBm/W9pgTgaFVEVH+YLqoDdGvfHpzN1gI U49KODcdSBrG1NdIG5efF78W7vxSwXe66ir813l2m2o0dZbNKQ2qr6GT8cgZ7khAl33x +sAmnu9Peq8VuuHxhpgBOsOSfvfOsZfqulO1FwneITDaDc9J/7fLCB7GoJhVkxQ5U0xv qmgVW1aHdbPOIdQeAhcbJqusvBxxi62F4YQC5ipCqfh2jU1QeM1fl2kc5LHBDMBWBdbM bx1Q== X-Gm-Message-State: AOAM530pYEdbVwG4RNKxA9eAj1voilDS7Mz0v1EvDDkJ0ZVJiNzCO4hJ FcWKHyBAlzSvoI7Ofwj0ACPIAA== X-Received: by 2002:a17:90b:1809:b0:1c7:28fb:bdd0 with SMTP id lw9-20020a17090b180900b001c728fbbdd0mr10228571pjb.231.1648179060040; Thu, 24 Mar 2022 20:31:00 -0700 (PDT) Received: from localhost (0.223.81.34.bc.googleusercontent.com. [34.81.223.0]) by smtp.gmail.com with UTF8SMTPSA id 21-20020a630115000000b00382a0895661sm3773921pgb.11.2022.03.24.20.30.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Mar 2022 20:30:59 -0700 (PDT) From: Ying Hsu To: marcel@holtmann.org Cc: chromeos-bluetooth-upstreaming@chromium.org, Ying Hsu , Joseph Hwang , "David S. Miller" , Jakub Kicinski , Johan Hedberg , Luiz Augusto von Dentz , Paolo Abeni , linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Fri, 25 Mar 2022 03:30:51 +0000 Message-Id: <20220325033028.1.I67f8ad854ac2f48701902bfb34d6e2070011b779@changeid> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog MIME-Version: 1.0 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 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 Signed-off-by: Ying Hsu Reviewed-by: Joseph Hwang --- Tested this commit using a C reproducer on qemu-x86_64 for 8 hours. 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 8eabf41b2993..380c63194736 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.1021.g381101b075-goog