[PATCH] Bluetooth: rfcomm: Fix null-ptr-deref in rfcomm_check_security

From: Yuxuan Hu
Date: Tue Dec 26 2023 - 02:44:25 EST


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.

Adding a check for whether `conn` is a null pointer to fix this bug.

Signed-off-by: Yuxuan Hu <20373622@xxxxxxxxxxx>
---
net/bluetooth/rfcomm/core.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index 053ef8f25fae..5ffa197fdb48 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -228,6 +228,9 @@ static int rfcomm_check_security(struct rfcomm_dlc *d)
auth_type = HCI_AT_NO_BONDING;
break;
}
+
+ if (!conn)
+ return 0;

return hci_conn_security(conn->hcon, d->sec_level, auth_type,
d->out);
--
2.25.1