Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5584046rwd; Wed, 24 May 2023 04:08:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5fHQuEPJ1wGjmNbHpLEbakHVFYqxWkWnL/HlcK/Fx9rZtyEBtfNdSPO6YRdHz0d4ftz8MG X-Received: by 2002:a05:6a20:841a:b0:10c:37ed:3e94 with SMTP id c26-20020a056a20841a00b0010c37ed3e94mr6816399pzd.28.1684926491162; Wed, 24 May 2023 04:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684926491; cv=none; d=google.com; s=arc-20160816; b=qjrOPD6tvEXNOvhDo85H+TQ5vyM06VLYkmLxRsdMuFpLXTsKAByp7/sxBaZIAIxrwJ 8w46fRD3R0LfZxZRy3KkIPUeuW6oeWnZtBT+h8dMnYsz8+VGXDVWxU3sO5PlsgRN1Ucf osoLoRhztBVvmXkeCIyESdTK517xblG3CiybiWQ1wJP5Y62MNhhRhI8s/69aKP2JV19U c0OJS+/g4XrFAbfONyFTxRDTX+iSH9knw91SR0u4KaoFZoWeUzTziVbcvLLhYJjwv13d qKkFotH61kZf9qHptTviiCYyMFNkz6SjarnyIRR+gg9V1pOkGGmJ94xekgAvVxEmMrjh NGnw== 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=MlhFitbDMZP7hCQbdHVoNuEXl+NL0ZynFialB8QGO0Y=; b=lCuM6Axg2GDWuoRW7wO08LN+LrICwD9St/ykf2yjMrWkYI/MdEmK7E2x7WKQf6hqbw yUsh2wxl7olU+1BNNJQA9YewppTKQ8qCzNF47vGxktj2fteoLl9x0QfSqcQdWwwMmQhC poStDx1UCkoxc/2w70mOR6PKrZD6T9o8fpRKYQ3JXYnpPYJNUGrLwRfCoVttYSLjLEoy GsU1ABiRMOcUmZ3AkSwjacLwL0lOc7RMZE2vw+KQ5ZquwtoZtKIpAYNER+0jDkePc3R9 2Pl/glc5rSvW7BbAvZJphx24PRwxt7DKXQcFKJT0vSOy41RdcD8IFpaifxvYqh+tCUAm sBYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hqgqSp8p; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v10-20020a63f20a000000b0050be7b93e73si4538561pgh.694.2023.05.24.04.07.57; Wed, 24 May 2023 04:08:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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=@chromium.org header.s=google header.b=hqgqSp8p; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234148AbjEXLBj (ORCPT + 99 others); Wed, 24 May 2023 07:01:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233037AbjEXLBd (ORCPT ); Wed, 24 May 2023 07:01:33 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C31AEA3 for ; Wed, 24 May 2023 04:01:29 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-64d44b198baso529591b3a.0 for ; Wed, 24 May 2023 04:01:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684926089; x=1687518089; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MlhFitbDMZP7hCQbdHVoNuEXl+NL0ZynFialB8QGO0Y=; b=hqgqSp8p6fg7qL4ZjMoY2COLrFUlRYy/RquBeTwgV8wDs2/2TSNB26j/fWv3zOXz1X B1AA0Oxx4SdBTE7G6P9LvV/AqTgToRdvfvbRoTgOZ3yWookt66AdHwlaHtko7xK2wuf3 wFWfcYUlYeGGbqA8boIcz3sCNzTzeId4Stvc0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684926089; x=1687518089; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MlhFitbDMZP7hCQbdHVoNuEXl+NL0ZynFialB8QGO0Y=; b=LSbSBqpotW07eu5J4jNST4hcV2JKasPbPVBaqt1CDhIye5A++0YQ0rbmC8MraJefyR ORtE4QrO4lE9Tl2VXHJmn8mj71OidIvZkxvw/o8otDWt5oNaFIeUG0qShtGc7ZuH5Vy/ XfROa80DwksttJamvcyzqcY8YeW9DQLaJl3cVFERYpi0UczrDgMgTFA8PhvzbPFl11NT qJslVM8DU20oXwYmPQRBnSRfIXAplyRcAiumTQbWYOx2r+5Zd5GbwP4+K74PsacFYz9g lp0n6n8+l06pUVW7hEk01SIVicSUhAGLHNkHB70qRiv+Zcyq+NBO3nX/1osYYx91PyA2 3Klw== X-Gm-Message-State: AC+VfDyNsd0XyR1hHPsLE8DVbfvzE7Y3gxrWzfxuEyPnogOkgqH2ia33 6IWvO2CxurkGuDoJjcGMV6R2/cDa3SMXHIrQ90A= X-Received: by 2002:a05:6a20:394e:b0:103:e813:77a1 with SMTP id r14-20020a056a20394e00b00103e81377a1mr21068407pzg.15.1684926088996; Wed, 24 May 2023 04:01:28 -0700 (PDT) Received: from localhost (21.160.199.104.bc.googleusercontent.com. [104.199.160.21]) by smtp.gmail.com with UTF8SMTPSA id 10-20020a63060a000000b00502fd70b0bdsm7627969pgg.52.2023.05.24.04.01.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 24 May 2023 04:01:28 -0700 (PDT) From: Ying Hsu To: linux-bluetooth@vger.kernel.org Cc: chromeos-bluetooth-upstreaming@chromium.org, Ying Hsu , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Johan Hedberg , Luiz Augusto von Dentz , Marcel Holtmann , Paolo Abeni , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3] Bluetooth: Fix l2cap_disconnect_req deadlock Date: Wed, 24 May 2023 11:00:00 +0000 Message-ID: <20230524110119.602679-1-yinghsu@chromium.org> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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-bluetooth@vger.kernel.org L2CAP assumes that the locks conn->chan_lock and chan->lock are acquired in the order conn->chan_lock, chan->lock to avoid potential deadlock. For example, l2sock_shutdown acquires these locks in the order: mutex_lock(&conn->chan_lock) l2cap_chan_lock(chan) However, l2cap_disconnect_req acquires chan->lock in l2cap_get_chan_by_scid first and then acquires conn->chan_lock before calling l2cap_chan_del. This means that these locks are acquired in unexpected order, which leads to potential deadlock: l2cap_chan_lock(c) mutex_lock(&conn->chan_lock) This patch uses __l2cap_get_chan_by_scid to replace l2cap_get_chan_by_scid and adjusts the locking order to avoid the potential deadlock. Fixes: ("a2a9339e1c9d Bluetooth: L2CAP: Fix use-after-free in l2cap_disconnect_{req,rsp}") Signed-off-by: Ying Hsu --- This commit has been tested on a Chromebook device. Changes in v3: - Adding the fixes tag. Changes in v2: - Adding the prefix "Bluetooth:" to subject line. net/bluetooth/l2cap_core.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 376b523c7b26..7374e3d046da 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -4651,8 +4651,15 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn, BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); - chan = l2cap_get_chan_by_scid(conn, dcid); + mutex_lock(&conn->chan_lock); + chan = __l2cap_get_chan_by_scid(conn, dcid); + if (chan) { + chan = l2cap_chan_hold_unless_zero(chan); + if (chan) + l2cap_chan_lock(chan); + } if (!chan) { + mutex_unlock(&conn->chan_lock); cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); return 0; } @@ -4663,14 +4670,13 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn, chan->ops->set_shutdown(chan); - mutex_lock(&conn->chan_lock); l2cap_chan_del(chan, ECONNRESET); - mutex_unlock(&conn->chan_lock); chan->ops->close(chan); l2cap_chan_unlock(chan); l2cap_chan_put(chan); + mutex_unlock(&conn->chan_lock); return 0; } @@ -4691,25 +4697,32 @@ static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); - chan = l2cap_get_chan_by_scid(conn, scid); + mutex_lock(&conn->chan_lock); + chan = __l2cap_get_chan_by_scid(conn, scid); + if (chan) { + chan = l2cap_chan_hold_unless_zero(chan); + if (chan) + l2cap_chan_lock(chan); + } if (!chan) { + mutex_unlock(&conn->chan_lock); return 0; } if (chan->state != BT_DISCONN) { l2cap_chan_unlock(chan); l2cap_chan_put(chan); + mutex_unlock(&conn->chan_lock); return 0; } - mutex_lock(&conn->chan_lock); l2cap_chan_del(chan, 0); - mutex_unlock(&conn->chan_lock); chan->ops->close(chan); l2cap_chan_unlock(chan); l2cap_chan_put(chan); + mutex_unlock(&conn->chan_lock); return 0; } -- 2.40.1.698.g37aff9b760-goog