Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp3027444rwd; Mon, 22 May 2023 07:43:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6qADXm3shJFyR7U0krtEaV/hr1s2wn9NbxmM37GtMvgAQI0pRO8osm37OeVD57gjGpplBv X-Received: by 2002:a05:6a20:54a4:b0:10b:2203:6ab1 with SMTP id i36-20020a056a2054a400b0010b22036ab1mr5166823pzk.4.1684766594350; Mon, 22 May 2023 07:43:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684766594; cv=none; d=google.com; s=arc-20160816; b=ueyP7TDmpS7hQ+AUTOi75DIfP6ygW+6Qk5pfAOWgrSJ6h0pgMaxbjqPgjgHO4DwV16 wQrVdZ/CJNJkrTzOEWzEbNPJ9wtsEBoCoBCcoLoVIowFx3AOTL946JZ6DcsQtR1qSsYI A3rVFbafb6eO0gUyqMJQy6C5Ep3IjFKtNngQN/DeysMrElR5D6xZf9+5/H7P/7IsKjqX qZtaRtmNkZbiTH0QbywwwGb1IyZT9tJ1UHB4nxe3f3P6iATYVjMnuY4G0ZQ6zBAlI0Gp YIaMYDcQARC9IQNiijaCFEQU6w8Zj8jFMCzo4K/WAgC4MXvipqHCxb/XwdXd7dDO+UU9 IBqg== 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=8YPyrsVUlvNm+NcxG8kD6DkQObDPDskSuRSFzaPM82o=; b=K8OUAd6UCS7wOIgMA58iYhpg8J2Ca7HL9+iPGERR8ipigQ0F28XjE+UEcNz47sqAid DjcRDm/VwE/yH/4123TWzNPjItBhLqr3ovucc7CDvhx1afuPEn3+UGoizRRzQL50NKOT BnEQrm2AsgFHlRHA9FEo5p1MGBQ3BqAmH+dOLMfYmC86dqSXArtV8rEYfWhkDiEvjHlw H4qe/3dOu2TZP6kivzPdSxfhiM/A+FoE3ilpXIyn+GNdll67RaEwbvuWNbDVF/6FVui7 X2SdOCeGeiumvj5G2PK5ZPzKolA+o/g/BxFv439gDJIZB1K/nDIZr06MEjVHPqkXKILG /TZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SvgVkEoj; 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 i4-20020a63e444000000b005347ac15c20si256747pgk.437.2023.05.22.07.42.49; Mon, 22 May 2023 07:43:14 -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=SvgVkEoj; 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 S233801AbjEVOiX (ORCPT + 99 others); Mon, 22 May 2023 10:38:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231909AbjEVOiW (ORCPT ); Mon, 22 May 2023 10:38:22 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE13ACA for ; Mon, 22 May 2023 07:38:20 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-64d2b42a8f9so3183652b3a.3 for ; Mon, 22 May 2023 07:38:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684766300; x=1687358300; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8YPyrsVUlvNm+NcxG8kD6DkQObDPDskSuRSFzaPM82o=; b=SvgVkEojKc7aZ48xKy9aZ7SG3oJmaOkHmKB7MBBQZ9tS5lY0KUAoOSlyzRJvBS+kmu C9Tc+q58SigKg1HoGaQd6hpXtUVAWH2LZEi12DenO35F4SR/KMeaT1+9fiHt6eK2s8kz 6o4USrC36jBVli59P4EhI2H9BwaLvqg+Efsd0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684766300; x=1687358300; 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=8YPyrsVUlvNm+NcxG8kD6DkQObDPDskSuRSFzaPM82o=; b=BKMkHAmmPErkpTsu3j+hy5RJX+ssV0k5jik5hRpY2nwxnGZlMOjtX6P+STvof7uhXd Mypemht7MmCVGlpfZVNruAiX6briXT6jIqpHF3L9/nu6dft/pnETpEvDoUI2rH3KEv0e nxZ5IeNo5RXhYLJf0GlnACWYTynrtBgRFUf1A5h3shlhLU2Tq3Hip+b9dZD6wmj4gDH6 Rh5N79/zgS4NRwcu4lr05iHnn3pB9+bwabcg+5xzb192MCkpfGiTTxQlhG18OQ7wzzu9 r0mXNcenJI/qLepM/SN335ND4wvty4h09ComlE9eF/JNrIheMXl4dOt/SLhFuDXkGITZ BXJw== X-Gm-Message-State: AC+VfDzgY95CdBZZ6inq3tJdJ+adhVnH/ukC//bCmUX4tlCkQmPJ7DJ9 tjuSxAEbDnIdzGITre1GzNqqZVrGa+UGsa4tie8= X-Received: by 2002:a05:6a00:1486:b0:63f:120a:1dc3 with SMTP id v6-20020a056a00148600b0063f120a1dc3mr16165748pfu.0.1684766299980; Mon, 22 May 2023 07:38:19 -0700 (PDT) Received: from localhost (21.160.199.104.bc.googleusercontent.com. [104.199.160.21]) by smtp.gmail.com with UTF8SMTPSA id d18-20020aa78152000000b0062e0c39977csm4208846pfn.139.2023.05.22.07.38.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 22 May 2023 07:38:19 -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] Fix l2cap_disconnect_req deadlock Date: Mon, 22 May 2023 14:37:55 +0000 Message-ID: <20230522143759.2880743-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=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 uses __l2cap_get_chan_by_scid to replace l2cap_get_chan_by_scid and adjusts the locking order to avoid the potential deadlock. Signed-off-by: Ying Hsu --- This commit has been tested on a Chromebook device. net/bluetooth/l2cap_core.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 376b523c7b26..8f08192b8fb1 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -4651,8 +4651,16 @@ 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 +4671,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 +4698,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