Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2439787rdb; Mon, 12 Feb 2024 05:11:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWNAL6ntvIRcE2ATt8+aLwEss8vHf6LkBDuTpv0kSCpA5OmEO6YT3r/LLKIZ9DfamcAK2qaQHD6ucujsYE2H2CrghYxJeYPjQxm39ihVQ== X-Google-Smtp-Source: AGHT+IH0kcVWRInJDZ9quLljiCnUnuonvY80my4tPd97FgSQxZ6IehYk6k4r9btHvrG7T8F3eU1A X-Received: by 2002:a05:620a:361d:b0:785:962d:a6a9 with SMTP id da29-20020a05620a361d00b00785962da6a9mr5393170qkb.45.1707743483603; Mon, 12 Feb 2024 05:11:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707743483; cv=pass; d=google.com; s=arc-20160816; b=kcuO1Cr9w/ImTbbb3A8PGYOHHjXHwOwv4XmMr9DNOqx/m7vOdZpX+IlWGXVkvx39yM 1B5VxtDlUiBeqxpqtbV+n0UGqAvoh1l5ETYZz9mI6uO1Zy3/eJL5phJMBuXRGM9wFGFy tAs2/rb4Sbs+djWom7wrCGV3NlMRYYiHL8NutnZwCB5uis/ML1GyOcTETaIKCUrdUJk3 ZDVI4qK+bN8MWziRU9ky4blXnLQONE6d6yz+9NG6huzfBijhjfvZg1gZnc5agR3I59ZR F1APMSFM/DxQUAuar7F5cD/6F38uQLnHQu92HdHj0vUqW7rGHE8S4Tw20hmaOnurLH28 M2gQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=r1QP9ZE1bngJxixBmkaV6JEGpzo4cjIsH4gilSjWjxI=; fh=52uFK2nx93kEtupQxGeh13cJDx1ZOPnYY/tLUEVqpQo=; b=mT/5VVWnDlWGHqYe/Yc0T5sboKvXrWinhdeZ0est67nQszqLF0nbNu4B0bnsnIOHoJ hPAVoakHxuVtAn9aC4XDBfqIkeWzDaVaY6MK5BU7ti7e+9R894tlgm6qn0oa43eZmGht qCrM6pbTX/sR1YQ2ppEuBWWWgLZCuWnFs2eoDXwgf00boY18iQ8bzPx++G4qXFegcibU p5dJiDf5lZljoZ/wMkPLYA4JvsRH7DaZZmzwXLCxRSr8TO+tZiS+ZDmxDqDTPPSbZOWG MNLVEVE3s2WdFjkfGbw3JPnuVjubgB/cPmULAC1MOyUsv97MCf4bu1S3I5qvbpVVHV4k defQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-bluetooth+bounces-1767-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-1767-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCXKIceC6cFHPmvoN4Ltfdr1x9ILCaIDRjiRv4+q/6ZDR9xJEDBbPOok//QEp1FmuJv+OninECDbygIVOX8XYqAI6CyoVSzYtBjocKybaw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b12-20020a05620a04ec00b007853af18378si5770318qkh.742.2024.02.12.05.11.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 05:11:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-1767-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-bluetooth+bounces-1767-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-1767-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2A7BA1C23A56 for ; Mon, 12 Feb 2024 13:10:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ECD763A1D3; Mon, 12 Feb 2024 13:10:21 +0000 (UTC) X-Original-To: linux-bluetooth@vger.kernel.org Received: from air.basealt.ru (air.basealt.ru [194.107.17.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C31AA3A1CB; Mon, 12 Feb 2024 13:10:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.107.17.39 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707743421; cv=none; b=FaNCrJ4/PHyvTO4jYooh/W/38fbdRb6XaRePFb0mDbg5qFBcd3ASUU4c0qzJOW0ldmwTvJkhxPEldo9jZ5LyTa8K7VV3a/xCSx5fjkfOdBZHkxVXEpobohmrdFI8YYkKSHTYmp0+fUd9Awqq0ROHkOPv/+NZtWfmJB0wfp6ef0U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707743421; c=relaxed/simple; bh=DnGC0CwNjZhsWkEnYbpja1u3lY2fDdY275sfxdNKvrE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UHgYYtdZkRgIwZX01KEN2tY6mv6NfepF/4bGiZAS0KYxrNwZLynos/UeoDG4gBKNFOEoSQhyyDx3uH1UNFmoKb33g3czdEFf1vUj/3gsIdNPe83nPujJJtCy3PLi6F68o0RUWwBdEw+e/9B22BSxg+hD2uy14JJnSoOXICRrEfQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org; spf=pass smtp.mailfrom=altlinux.org; arc=none smtp.client-ip=194.107.17.39 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=altlinux.org Received: by air.basealt.ru (Postfix, from userid 490) id 191522F2023F; Mon, 12 Feb 2024 13:10:11 +0000 (UTC) X-Spam-Level: Received: from shell.ipa.basealt.ru (unknown [176.12.98.74]) by air.basealt.ru (Postfix) with ESMTPSA id 50A862F20241; Mon, 12 Feb 2024 13:10:03 +0000 (UTC) From: oficerovas@altlinux.org To: oficerovas@altlinux.org, stable@vger.kernel.org, linux-bluetooth@vger.kernel.org Cc: Greg Kroah-Hartman , Pauli Virtanen , Luiz Augusto von Dentz , Paolo Abeni , Jakub Kicinski , Eric Dumazet , kovalev@altlinux.org, nickel@altlinux.org, dutyrok@altlinux.org Subject: [PATCH 2/2] Bluetooth: SCO: fix sco_conn related locking and validity issues Date: Mon, 12 Feb 2024 16:09:33 +0300 Message-ID: <20240212130933.3856081-3-oficerovas@altlinux.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240212130933.3856081-1-oficerovas@altlinux.org> References: <20240212130933.3856081-1-oficerovas@altlinux.org> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Alexander Ofitserov From: Pauli Virtanen commit 3dcaa192ac21 ("Bluetooth: SCO: fix sco_conn related locking and validity issues") Operations that check/update sk_state and access conn should hold lock_sock, otherwise they can race. The order of taking locks is hci_dev_lock > lock_sock > sco_conn_lock, which is how it is in connect/disconnect_cfm -> sco_conn_del -> sco_chan_del. Fix locking in sco_connect to take lock_sock around updating sk_state and conn. sco_conn_del must not occur during sco_connect, as it frees the sco_conn. Hold hdev->lock longer to prevent that. sco_conn_add shall return sco_conn with valid hcon. Make it so also when reusing an old SCO connection waiting for disconnect timeout (see __sco_sock_close where conn->hcon is set to NULL). This should not reintroduce the issue fixed in the earlier commit 9a8ec9e8ebb5 ("Bluetooth: SCO: Fix possible circular locking dependency on sco_connect_cfm"), the relevant fix of releasing lock_sock in sco_sock_connect before acquiring hdev->lock is retained. These changes mirror similar fixes earlier in ISO sockets. Fixes: 9a8ec9e8ebb5 ("Bluetooth: SCO: Fix possible circular locking dependency on sco_connect_cfm") Signed-off-by: Pauli Virtanen Signed-off-by: Luiz Augusto von Dentz Signed-off-by: Alexander Ofitserov --- net/bluetooth/sco.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index 0e1f5dde7bfec..99b149261949a 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -126,8 +126,11 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon) struct hci_dev *hdev = hcon->hdev; struct sco_conn *conn = hcon->sco_data; - if (conn) + if (conn) { + if (!conn->hcon) + conn->hcon = hcon; return conn; + } conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL); if (!conn) @@ -268,21 +271,21 @@ static int sco_connect(struct sock *sk) goto unlock; } - hci_dev_unlock(hdev); - hci_dev_put(hdev); - conn = sco_conn_add(hcon); if (!conn) { hci_conn_drop(hcon); - return -ENOMEM; + err = -ENOMEM; + goto unlock; } - err = sco_chan_add(conn, sk, NULL); - if (err) - return err; - lock_sock(sk); + err = sco_chan_add(conn, sk, NULL); + if (err) { + release_sock(sk); + goto unlock; + } + /* Update source addr of the socket */ bacpy(&sco_pi(sk)->src, &hcon->src); @@ -296,8 +299,6 @@ static int sco_connect(struct sock *sk) release_sock(sk); - return err; - unlock: hci_dev_unlock(hdev); hci_dev_put(hdev); -- 2.42.1