Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp253071rwd; Tue, 30 May 2023 19:58:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5f0jyDbsDTLzxCqx5cUZz/WZghQh0N4j3Hb574bpQZ/+6mWNMCbQXRsH0eTqfkGK70mKF8 X-Received: by 2002:a05:6808:1491:b0:398:4746:202c with SMTP id e17-20020a056808149100b003984746202cmr2854289oiw.36.1685501902539; Tue, 30 May 2023 19:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685501902; cv=none; d=google.com; s=arc-20160816; b=gq49m6EefzZQagYMakMI9fqCR/aJ8Z9KEvjAV0KQKYrucOyQQXGI8UpMj9nOFpgOWK XIQ0Lz9n5kwcxRng5Uu1IAFAWnM+ggj9OQjaiFOYjJRNabUIdEIVoIVKGjzf0Y3KkC8k YlHVNCNugTCIh92SSkynHurubAbRDjtEWPkXm5o7kQOBET2vvYAF8aLHP+5UmUZd69yG POWaC2NU4B/2LTkMi5y7zNBk8/tApyJyghviW2kEl6skG2201Gkv+0kP1osSYnIPfzYB fqhggKqLnHW1w+v4chRFdHVa0xXwM+slH6g2P010GXbq4+CsFe0ILzsGgx9FSbWu3iKd Hosg== 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=tpH4t+aA6VmGuKwExaIddXhyI8sEcDcaGGPTHS+CPmI=; b=MHfOVmQlYp4A0XYYxDl4pke5rjbLbsIE+YPPy3e1J+3K72LOMJazb2eYSIRq5R3C18 W/kJSrZuYll87d5HhX5v90YVUmZ6r9/2FKiwfuUBh7mzzRlpBH67Y1RqLZKOpyDPa+ah k1nRwBM/1mIKllUm/YKqncca2bG/cXuozaK09sx5xk1bxVEa5/+NyCv5n1IZG7vks8DD lMvdhqZoM+OJjUgtSLp531Uvv0CRMhzmReq4Z2v+0NUR6q3mt18lMTa0kZUe7C1gSsJb YzgSF8Lnk/hGrlgMeyANn1WFXk9nXq33QSfETKUNR2p0CV4ajMffoiKqtkyJs2rZReNV 3hXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=g5BFEFHP; 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 140-20020a630192000000b0053b928deb7asi180269pgb.344.2023.05.30.19.58.00; Tue, 30 May 2023 19:58:22 -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=g5BFEFHP; 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 S232268AbjEaCik (ORCPT + 99 others); Tue, 30 May 2023 22:38:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231545AbjEaCij (ORCPT ); Tue, 30 May 2023 22:38:39 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D959118 for ; Tue, 30 May 2023 19:38:38 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1b04949e4e9so16745265ad.3 for ; Tue, 30 May 2023 19:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685500717; x=1688092717; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=tpH4t+aA6VmGuKwExaIddXhyI8sEcDcaGGPTHS+CPmI=; b=g5BFEFHPBgZ4qgHQ1gYDCA8l9KFVdQqB/fixZvBbGIxNlUbVNYI2tDO/GAU0pLDms6 kLUAwWHzku0iuuHwSGQJLAqUALQDwfzADkSn72ivYxbkpbX0lrqCMKceR6AO2ZtRGerD V2WGNkUk/PD4flTllzlIP0O7+kdyqRkRlr1MQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685500717; x=1688092717; 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=tpH4t+aA6VmGuKwExaIddXhyI8sEcDcaGGPTHS+CPmI=; b=aAjO1vtrdnz4fN9Lm2fDWt4W8HlCokX/dNDJGB3e5tCQyWpVH0tgYhdinHiRH1Gk+G 4DVeU/BrAVapIvvH5smdI2BIi/theplrl49FL7pZSm4qX8lLfZUjupXFe4UGHATwyQaL Z7KLL5BiDF1Gbh0C9I9Xl+AUQftg2gWKWFosnSSYIKe/vzCPge4Ak7pxAbAJukKx53at G/TOpeTqTy+x6BVjQmtHEudF7T+pidkgoJVKfIxIJp8YJaRWGKDHCjrkXZz9AaP9kMz4 iNq6uTdR3wigsuDKu2uROBtD/Oz5yt3d3ysbsNJhqhgX+G0Vz9rMryuEZEpBTrt83wJG iM/Q== X-Gm-Message-State: AC+VfDzXTpm8koACFoU6stcAklt+39/sYbo7XJ3ArIXPho53erOtFzXc qXmKv2nz/LWS6NaajKkNKFwX70VH7zfvrkwEk6s= X-Received: by 2002:a17:902:db11:b0:1ae:7421:82b8 with SMTP id m17-20020a170902db1100b001ae742182b8mr4572863plx.28.1685500717586; Tue, 30 May 2023 19:38:37 -0700 (PDT) Received: from localhost (21.160.199.104.bc.googleusercontent.com. [104.199.160.21]) by smtp.gmail.com with UTF8SMTPSA id jk15-20020a170903330f00b001b024ee5f6esm48397plb.81.2023.05.30.19.38.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 30 May 2023 19:38:37 -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 v4] Bluetooth: Fix l2cap_disconnect_req deadlock Date: Wed, 31 May 2023 02:38:16 +0000 Message-ID: <20230531023821.349759-1-yinghsu@chromium.org> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.3 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,URIBL_BLOCKED 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-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 releases chan->lock before acquiring the conn_chan_lock 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 v4: - Using l2cap_get_chan_by_scid to avoid repeated code. - Releasing chan->lock before acquiring conn->chan_lock. Changes in v3: - Adding the fixes tag. Changes in v2: - Adding the prefix "Bluetooth:" to subject line. net/bluetooth/l2cap_core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 376b523c7b26..d9c4d26b2518 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -4663,7 +4663,9 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn, chan->ops->set_shutdown(chan); + l2cap_chan_unlock(chan); mutex_lock(&conn->chan_lock); + l2cap_chan_lock(chan); l2cap_chan_del(chan, ECONNRESET); mutex_unlock(&conn->chan_lock); @@ -4702,7 +4704,9 @@ static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, return 0; } + l2cap_chan_unlock(chan); mutex_lock(&conn->chan_lock); + l2cap_chan_lock(chan); l2cap_chan_del(chan, 0); mutex_unlock(&conn->chan_lock); -- 2.41.0.rc0.172.g3f132b7071-goog