Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7008469rdb; Wed, 3 Jan 2024 01:03:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOLNckGcezKtO1N4scKrTOsH5nZ95fzTHLSAh9OCzqjKsxYhrKX54htzZvTgDXX9wB+YZ7 X-Received: by 2002:a17:907:12c7:b0:a26:975a:fcc8 with SMTP id vp7-20020a17090712c700b00a26975afcc8mr8046058ejb.10.1704272604719; Wed, 03 Jan 2024 01:03:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704272604; cv=none; d=google.com; s=arc-20160816; b=R5jcjHnClDfPXEMvPk+fe7CBeTG9hBcv+NV6V+ZtJ+IJePWVi1EwhF2dwJ1bJ8Rhaf bJGt8zvN3eHhTCSLTIua53TAQjfHDK6YrY7vfYGPzrCbK+chj8FIsMA9gdjj8BDwat9T PZ3dveWYdBnvuB8VKH+zW4+5ZrFDSjmbjjCs7IS3NJIwaLmABpRcmDM8aRnR22jAZhje bYRejf8XnHGzQcDya7SuTutNA2Y3VNAy/mqX/X+KJ0WQ6ZWhYGUmJ/h94LWYalB1WRPx Kh7h+nbXuCsNl28IbRmkpFxC5ui5+JksCXgPnmbOb/CJdWBesCSRF2Csj+C1LH9A2yhz kjTA== 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=hoJ1IZhXIdvBH2NHy/nUS/+Bv6SFG+7M6aXxKVElZRQ=; fh=gqp06kgivJ633N4VR1+T+3Qrb8Xt2AJViznvu6caNBg=; b=VdVVphxTB9usOiLx9L4Iw9PDsDiGTqDi1dPjVKExGmxa73DRrr9UERSfG8hbKt+WMv mNe8q7grV5zJpaVyoBqPN7IbnFyVBDgQN4yAq/TQyueQXFaPsuV/SBEpfvkpG6tzjTbb NAd94o6WRsK8pk3wXvwrdLb0WF5yAydV+3u28cNrKIYCB1iu+KvQUYII0bx/TZzX7Frx WO/abmcuF9yaYG7ybXNgygwJcK5mMWIeTt7DY3ID6Rx3hNNFeSiUW0rvoQQ7e7gO2b4g tgE6knVqx94XP5OgVF2SIFZyCNh9KNs0KZ2Qba3TDDBtkRkEYMCPzH892u13gvXNGOsl Y4sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@buaa.edu.cn header.s=buaa header.b=Q9Mmzw7U; spf=pass (google.com: domain of linux-bluetooth+bounces-843-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-843-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 e11-20020a170906374b00b00a281f50ac4bsi1718001ejc.926.2024.01.03.01.03.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 01:03:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-843-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=Q9Mmzw7U; spf=pass (google.com: domain of linux-bluetooth+bounces-843-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-843-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 73CD91F23DE0 for ; Wed, 3 Jan 2024 09:03:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 06D191862B; Wed, 3 Jan 2024 09:03:01 +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="Q9Mmzw7U" X-Original-To: linux-bluetooth@vger.kernel.org Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [207.46.229.174]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DE478182A8; Wed, 3 Jan 2024 09:02:54 +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=hoJ1IZhXId vBH2NHy/nUS/+Bv6SFG+7M6aXxKVElZRQ=; b=Q9Mmzw7UTMm/4qGdkf6TLeJnZV jdK5DY5wt8CquVteUJ4P3V/xZ1A8U6g6v7EmSoYxTnCp2O2vsX4C8eflsoN73ENK TRNuQeb5taSG+xqn8bt4UEX/jNWd3eU9QirC5shKKH3VEz5VmfYQeEL8BBtiV2fU OQH/mJKpMW/u9K6qc= Received: from localhost.localdomain (unknown [10.130.147.18]) by coremail-app1 (Coremail) with SMTP id OCz+CgBnlli0IpVln02jAA--.47372S2; Wed, 03 Jan 2024 17:02:44 +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, pmenzel@molgen.mpg.de Subject: [PATCH V3] Bluetooth: rfcomm: Fix null-ptr-deref in rfcomm_check_security Date: Wed, 3 Jan 2024 17:02:38 +0800 Message-Id: <20240103090238.3376565-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+CgBnlli0IpVln02jAA--.47372S2 X-Coremail-Antispam: 1UD129KBjvJXoW7WFyDGF1xXr18Zr17Kw47CFg_yoW8tFWxpF W2ya4fGFn7ur15Ar97AF4kAFyrZw1v9r15Kr4kZ3yY93s5Wwn3trWSyr1jkay5CF4qk343 AF18X3yDJrnru3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU901xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AE w4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2 IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2 z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2vYz4IE04k24V AvwVAKI4IrM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xf McIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7 v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lF7I21c0EjII2zVCS5cI20VAGYxC7MxkIecxEwVCm -wCF04k20xvY0x0EwIxGrwCF04k20xvE74AGY7Cv6cx26F1DJr1UJwCFx2IqxVCFs4IE7x kEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E 67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCw CI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1x MIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIda VFxhVjvjDU0xZFpf9x0JUQZ23UUUUU= 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 packets from 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 V2 -> V3: Fixed formatting errors in the commit 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