Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6919192rdb; Tue, 2 Jan 2024 20:06:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IFphfk/LLFbcPig4rlNO/XsE/gVHq7MpTuy0aEXuw7CsNk/Qq/IVzW7/xehNFc5uZNqgQkH X-Received: by 2002:a17:906:6d56:b0:a26:ee9b:74da with SMTP id a22-20020a1709066d5600b00a26ee9b74damr362667ejt.7.1704254796528; Tue, 02 Jan 2024 20:06:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704254796; cv=none; d=google.com; s=arc-20160816; b=tLIZxk/oL0WKjz5ZLXmCNRcPZ1qh75Yww/Xpu8fQ0JK62w5gfLtCr2Re2hUz7sQsbi XJjNRznWUekXSRhs9wO094ckn4UaZgXgUjgAVkaURbh5yixpZjayP8q5w5mAApowRDuc n+UzrI7+cbayImxuOhfuinO21anQWavnjybeeVz6JVQhbWqvyu09Mc7cLtv0Lm0Ea0VV 6CaOybHi1X+Wc6vZfci252sMApHWgZXkP5TM+7u13P3Ic8e8/uQZ+Y/KooaLxEJwqNxH NrCS4G476f3qMI+uO5KLCPct8/xmH3NbMGiYAwVpaqiCqd0nwZrPnUG9l01ozYDOaJhP Nifg== ARC-Message-Signature: i=1; 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:message-id:date:subject:cc:to :from:dkim-signature; bh=UBDlzz6WsfvPV73RH7b/bcn+16PBcKw0N4bCbDc+87o=; fh=Cvy8LhuL3SMNQgx9pUbNgwDelKK0ol/tT2M3Ag+PuCU=; b=ENarx8xOjfy3KgsIC/gHiEa4lYdyyM1xH9UfAoaHdp0lH7bVfum04uqtu08jRCbOn8 FRFkoyymQrqDFHSbDO0Bei4AVKc26m9gSMmSbP0QnD4HIprx0Wg+jejpvgJ4NRVi3DtW QWpjbiDLJbay/QiZ0JyIiBte6WC1nqaAa25jVle8+Q7psYDH9+amG5bMx6V1P8Yt9CoB LL/gipwSoSrrEaWtfSfsDLuETWAv1NhQFfLp5b9LpzsqyKKEWdVvZVWpKFriyb7pzqzl Zdmu7avrfORW9T9XyYjqu+Nfh8ZneWktzNT6YTzB7Mtr9TaT26N+kTBxu/hI6i3QJyQV IMqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@buaa.edu.cn header.s=buaa header.b="09dKia/m"; spf=pass (google.com: domain of linux-bluetooth+bounces-839-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-839-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=buaa.edu.cn Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id gi38-20020a1709070ca600b00a27cc05dd59si2860014ejc.521.2024.01.02.20.06.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 20:06:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-839-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@buaa.edu.cn header.s=buaa header.b="09dKia/m"; spf=pass (google.com: domain of linux-bluetooth+bounces-839-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-839-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=buaa.edu.cn 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 448131F2386D for ; Wed, 3 Jan 2024 04:06:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D55B2156E3; Wed, 3 Jan 2024 04:06:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=buaa.edu.cn header.i=@buaa.edu.cn header.b="09dKia/m" X-Original-To: linux-bluetooth@vger.kernel.org Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [52.237.72.81]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30E3E17980; Wed, 3 Jan 2024 04:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=buaa.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=buaa.edu.cn DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=buaa.edu.cn; s=buaa; h=Received:From:To:Cc:Subject:Date: Message-Id:MIME-Version:Content-Transfer-Encoding; bh=UBDlzz6Wsf vPV73RH7b/bcn+16PBcKw0N4bCbDc+87o=; b=09dKia/mTaKW0HG1EnmPWWcEgo nAI/wYbKIB37OzKox+E22TDDx8qGvgsN24bUYG+iR2wg1z7pz8Q7osECF0zDrFJl EZWiTzE65TLQoaWVIIFbJ8EzKwS2bd/UmbDNapr6RjD06+ep8eI9tIn2I3ES1lRq MS4mRSGfg6bF8pSlo= Received: from localhost.localdomain (unknown [10.130.147.18]) by coremail-app1 (Coremail) with SMTP id OCz+CgC35Vc43ZRlU5uiAA--.46624S2; Wed, 03 Jan 2024 12:06:16 +0800 (CST) From: Yuxuan Hu <20373622@buaa.edu.cn> To: marcel@holtmann.org, johan.hedberg@gmail.com, luiz.dentz@gmail.com Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, baijiaju1990@gmail.com, sy2239101@buaa.edu.cn, 20373622@buaa.edu.cn, buaazhr@buaa.edu.cn Subject: [PATCH V2] Bluetooth: rfcomm: Fix null-ptr-deref in rfcomm_check_security Date: Wed, 3 Jan 2024 12:06:11 +0800 Message-Id: <20240103040611.3279681-1-20373622@buaa.edu.cn> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:OCz+CgC35Vc43ZRlU5uiAA--.46624S2 X-Coremail-Antispam: 1UD129KBjvJXoW7WFyDGF1xXr18Aw4UCw4rXwb_yoW8trW3pF W2ya4xGFn7ur15Ar97AF4kAFyrZw1v9r15Kw4ku3yY93s5Wwn7trWSyr1jyay5CF4qk343 AF10qw4DGrnru37anT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBY1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1l84ACjcxK6I8E 87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1lnxkEFVAIw20F6c xK64vIFxWle2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2Wl Yx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbV WUJVW8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lc7CjxVAaw2AF wI0_GFv_Wrylc2xSY4AK6svPMxAIw28IcxkI7VAKI48JMxAIw28IcVCjz48v1sIEY20_Aw 1UJr1UMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAI w20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x 0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7sRiL0e3UUUUU== X-CM-SenderInfo: ysqtljawssquxxddhvlgxou0/ During our fuzz testing of the connection and disconnection process at the RFCOMM layer,we discovered this bug.By comparing the packetsfrom a normal connection and disconnection process with the testcase that triggered a KASAN report, we analyzed the cause of this bug as follows: 1. In the packets captured during a normal connection, the host sends a `Read Encryption Key Size` type of `HCI_CMD` packet(Command Opcode: 0x1408) to the controller to inquire the length of encryption key.After receiving this packet, the controller immediately replies with a Command Complete packet (Event Code: 0x0e) to return the Encryption Key Size. 2. In our fuzz test case, the timing of the controller's response to this packet was delayed to an unexpected point: after the RFCOMM and L2CAP layers had disconnected but before the HCI layer had disconnected. 3. After receiving the Encryption Key Size Response at the time described in point 2, the host still called the rfcomm_check_security function. However, by this time `struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;` had already been released, and when the function executed `return hci_conn_security(conn->hcon, d->sec_level, auth_type, d->out);`, specifically when accessing `conn->hcon`, a null-ptr-deref error occurred. To fix this bug, check if `sk->sk_state` is BT_CLOSED before calling rfcomm_recv_frame in rfcomm_process_rx. Signed-off-by: Yuxuan Hu <20373622@buaa.edu.cn> --- V1 -> V2: Check earlier on rfcomm_process_rx net/bluetooth/rfcomm/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c index 053ef8f25fae..1d34d8497033 100644 --- a/net/bluetooth/rfcomm/core.c +++ b/net/bluetooth/rfcomm/core.c @@ -1941,7 +1941,7 @@ static struct rfcomm_session *rfcomm_process_rx(struct rfcomm_session *s) /* Get data directly from socket receive queue without copying it. */ while ((skb = skb_dequeue(&sk->sk_receive_queue))) { skb_orphan(skb); - if (!skb_linearize(skb)) { + if (!skb_linearize(skb) && sk->sk_state != BT_CLOSED) { s = rfcomm_recv_frame(s, skb); if (!s) break; -- 2.25.1