Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4750091rdb; Fri, 15 Sep 2023 11:04:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGyPUAUs45E7ADS68ddhdDV+IOOH9SnkIRAMJT0y5SqAAe7JKnfmBTwf3GRYKlZt2Rlcbrk X-Received: by 2002:a17:90a:de83:b0:268:2af6:e48c with SMTP id n3-20020a17090ade8300b002682af6e48cmr3274774pjv.4.1694801058833; Fri, 15 Sep 2023 11:04:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694801058; cv=none; d=google.com; s=arc-20160816; b=moeyAmVRGeKb9ZNX8wMDa8IBY0Ty93F65cKKb58Om3qP7/4EfGxXfx5udrIJQDYctR h2io3dqUPUC4sbqQF3HxXJnKpxr/PLpcvAMn9jYoRtFa/KRYrEcSZ0/Suzlx8KWMumR6 eipzjTkPyySAxC/5FN8DmdPglu86YgMQJ9LGgEnWKP1ZSMzTyU0t9KGYIyXaP4tcm8VG McrJMxgDroK+2YrHGQI0AxFG1k4ELSxdjHPwDY1pOyU6BBf1JJ5LdNQgYTOcT4yVD1Jo TFbUEiu/SJW1+SChkIJwxoecGv7edHp4WzjS/JfE/CWHGMnieVfWDAJjvljP6BcL02AO qq1w== 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:to:from:dkim-signature; bh=3ttJo81OzOiXXRPa6WEjkW5TVXXbz7eCrRejkuktRL8=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=gJjK3rfG3Vs6GBMl9CWU6by50/u/FZ1VSv/5YNOU758a/ghV9JjmGQ+O0XVEiilj8Y XXO0Fi/FqMAKgvx3CAr9yO9Ri95VGJiEPWKEch+AsLCM6v38zlWnD1awnukaVCIZH2BD iai8LttYN4npYc8Izm254PPit15kA39vwwqkjA+m6STNK42Fglxrlcw+5Fzhz0W5dK4j L/DvQ+i/AJnGzM9Ev519mk6KuW1Bnu4HPpmOr5vk96QqVsZwYXlG2Y76Z6eyylXoFdT5 EHoqK7/p9zgyNeitsJDgYlrlmv+pVu07H0K7sFsrP8UvbhNzd4E1p82SpIrlREzqY7Xp FI9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=d6NjCWad; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id s9-20020a17090aad8900b00268414272d1si3771953pjq.156.2023.09.15.11.04.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 11:04:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=d6NjCWad; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0F8D282A38EB; Thu, 14 Sep 2023 14:04:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229512AbjINVEF (ORCPT + 99 others); Thu, 14 Sep 2023 17:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229436AbjINVEF (ORCPT ); Thu, 14 Sep 2023 17:04:05 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EF352698 for ; Thu, 14 Sep 2023 14:04:01 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68fdcc37827so1875381b3a.0 for ; Thu, 14 Sep 2023 14:04:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694725440; x=1695330240; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=3ttJo81OzOiXXRPa6WEjkW5TVXXbz7eCrRejkuktRL8=; b=d6NjCWadU8CIvo6T5Cu/guyaDk+RH+bAza6QvxZmTSzUtOTg0R2lQmcJilTRxQXbC7 h03GcrV8DDOcCMI3K9McTLyDA1mFeHepwQWKCyMLeSwjsrBp3eGLDPhclAElFC6dpYCb vwnfWoe8NZYL3o/4wiM1dq+W8LXMIAcx6LPF+KZjkjjH5c6a4LyLOqN71kSbX6ubHeRW p6EIWa9TfEmmR6vWIgGRqqN4OPB5DoJf6urMNX0z6FrloKfj7VDu13SzWTyCqtXs6O4D ftbWtr7ifn96GzIIPcWGD6VCAFu9KwJbMerxQxVMAt23IpvHlrvG0CJmApYSAvjTIMSZ Fljw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694725440; x=1695330240; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3ttJo81OzOiXXRPa6WEjkW5TVXXbz7eCrRejkuktRL8=; b=TS7opNAqpmPRhmES9U+jgs1M/w0Mlj/INxSgcdsFs3g+oiIlNj4LPbyjSC0/IxhyxR WTw4jbz8U+EyOnywldzP4APM4YKdMoi7T0Ebz/Kq6eWT9ZNYvUVqzA+ycSkKt5qWSUjf cUkNgRVbcvRF/3hS34tEjk0EV12fp/s1F7iyL5tPqU7bHk+z/U+kwXKBnzLlOB/4JYSm R1PHxxFQ+JOxxZ2LVL0XRyIpHOLRc0knog1YR6XTL2eRzcZYv/uIa6UxPtTnA8uOI7Cs N6L5Vx5Lc5gaQ+10l+3VatB1gMmtBp8W3p4+8rD7WtIoHOPyRRbSd3GCo5MPr5EDEY1V mSnw== X-Gm-Message-State: AOJu0YxTEfLFahI/jft7egx2AHp/a7xfdG/gtDQCyh0arEUd1kOVtaf1 l12hCuqW7xH2HPHK2PTqUN02ZtZ8YfliCw== X-Received: by 2002:a05:6a20:160c:b0:134:70b7:2386 with SMTP id l12-20020a056a20160c00b0013470b72386mr4009721pzj.9.1694725439916; Thu, 14 Sep 2023 14:03:59 -0700 (PDT) Received: from lvondent-mobl4.. (c-98-232-221-87.hsd1.or.comcast.net. [98.232.221.87]) by smtp.gmail.com with ESMTPSA id z13-20020a63b04d000000b0056c2de1f32esm1570331pgo.78.2023.09.14.14.03.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 14:03:59 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v2] Bluetooth: L2CAP: Fix leaking l2cap_conn objects Date: Thu, 14 Sep 2023 14:03:58 -0700 Message-ID: <20230914210358.3335206-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 14 Sep 2023 14:04:03 -0700 (PDT) From: Luiz Augusto von Dentz l2cap_conn objects must be cleanup whenever its hci_chan is deleted by hci_chan_list_flush since it means the connection attempt is being aborted prematurely thus no calls to connect_cfm/disconnect_cfm would be generated causing the l2cap_conn object to leak. Fixes: 73d80deb7bdf ("Bluetooth: prioritizing data over HCI") Reported-by: Olivier L'Heureux Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/hci_core.h | 2 ++ net/bluetooth/hci_conn.c | 4 ++++ net/bluetooth/l2cap_core.c | 29 ++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index bbad301f5781..21459c38a074 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -795,6 +795,8 @@ struct hci_chan { unsigned int sent; __u8 state; bool amp; + + void (*cleanup)(struct hci_chan *chan); }; struct hci_conn_params { diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index e62a5f368a51..757a5840c978 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -2746,6 +2746,10 @@ void hci_chan_del(struct hci_chan *chan) /* Prevent new hci_chan's to be created for this hci_conn */ set_bit(HCI_CONN_DROP, &conn->flags); + if (chan->cleanup) + chan->cleanup(chan); + + chan->conn = NULL; hci_conn_put(conn); skb_queue_purge(&chan->data_q); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 17ca13e8c044..a791f28ccd6a 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -1896,6 +1896,8 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err) BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); + hcon->l2cap_data = NULL; + kfree_skb(conn->rx_skb); skb_queue_purge(&conn->pending_rx); @@ -1931,13 +1933,15 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err) mutex_unlock(&conn->chan_lock); - hci_chan_del(conn->hchan); + if (conn->hchan) { + conn->hchan->cleanup = NULL; + hci_chan_del(conn->hchan); + conn->hchan = NULL; + } if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) cancel_delayed_work_sync(&conn->info_timer); - hcon->l2cap_data = NULL; - conn->hchan = NULL; l2cap_conn_put(conn); } @@ -7830,6 +7834,24 @@ static void process_pending_rx(struct work_struct *work) l2cap_recv_frame(conn, skb); } +static void l2cap_conn_hchan_cleanup(struct hci_chan *hchan) +{ + struct hci_conn *hcon = hchan->conn; + struct l2cap_conn *conn; + + if (!hcon) + return; + + conn = hcon->l2cap_data; + if (!conn) + return; + + /* hci_chan_del has been called so we shouldn't call it gain. */ + conn->hchan = NULL; + + l2cap_conn_del(hcon, bt_to_errno(HCI_ERROR_LOCAL_HOST_TERM)); +} + static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) { struct l2cap_conn *conn = hcon->l2cap_data; @@ -7852,6 +7874,7 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) hcon->l2cap_data = conn; conn->hcon = hci_conn_get(hcon); conn->hchan = hchan; + hchan->cleanup = l2cap_conn_hchan_cleanup; BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); -- 2.41.0