From: "Sönke Huster" <soenke.huster@eknoes.de>
To: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: Marcel Holtmann <marcel@holtmann.org>,
Johan Hedberg <johan.hedberg@gmail.com>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
"linux-bluetooth@vger.kernel.org"
<linux-bluetooth@vger.kernel.org>,
"open list:NETWORKING [GENERAL]" <netdev@vger.kernel.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [RFC PATCH] Bluetooth: hci_event: Ignore multiple conn complete events
Date: Sat, 22 Jan 2022 00:18:01 +0100 [thread overview]
Message-ID: <995e58bb-6dfb-b3db-c8a5-b9e30dbb104d@eknoes.de> (raw)
In-Reply-To: <CABBYNZ+VQ3Gfw0n=PavFhnnOy2=+1OAeV5UT_S25Lz_4gWzWEQ@mail.gmail.com>
Hi Luiz,
On 21.01.22 22:31, Luiz Augusto von Dentz wrote:
> Hi Sönke,
>
> On Fri, Jan 21, 2022 at 10:22 AM Sönke Huster <soenke.huster@eknoes.de> wrote:
>>
>> I just noticed that just checking for handle does not work, as obviously 0x0 could also be a handle value and therefore it can't be distinguished, whether it is not set yet or it is 0x0.
>
> Yep, we should probably check its state, check for state != BT_OPEN
> since that is what hci_conn_add initialize the state.
>
I thought there are more valid connection states for the first HCI_CONNECTION_COMPLETE event, as it also occurs e.g. after an HCI_Create_Connection command, see Core 5.3 p.2170:
> This event also indicates to the Host which issued the HCI_Create_Connection, HCI_Accept_-
> Connection_Request, or HCI_Reject_Connection_Request command, and
> then received an HCI_Command_Status event, if the issued command failed or
> was successful.
For example in hci_conn.c hci_acl_create_connection (which triggers a HCI_Create_Connection command as far as I understand), the state of the connection is changed to BT_CONNECT or BT_CONNECT2.
But as I am quite new in the (Linux) Bluetooth world, I might have a wrong understanding of that.
>> On 21.01.22 18:36, Soenke Huster wrote:
>>> When a HCI_CONNECTION_COMPLETE event is received multiple times
>>> for the same handle, the device is registered multiple times which leads
>>> to memory corruptions. Therefore, consequent events for a single
>>> connection are ignored.
>>>
>>> The conn->state can hold different values so conn->handle is
>>> checked to detect whether a connection is already set up.
>>>
>>> Buglink: https://bugzilla.kernel.org/show_bug.cgi?id=215497
>>> Signed-off-by: Soenke Huster <soenke.huster@eknoes.de>
>>> ---
>>> This fixes the referenced bug and several use-after-free issues I discovered.
>>> I tagged it as RFC, as I am not 100% sure if checking the existence of the
>>> handle is the correct approach, but to the best of my knowledge it must be
>>> set for the first time in this function for valid connections of this event,
>>> therefore it should be fine.
>>>
>>> net/bluetooth/hci_event.c | 11 +++++++++++
>>> 1 file changed, 11 insertions(+)
>>>
>>> diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
>>> index 681c623aa380..71ccb12c928d 100644
>>> --- a/net/bluetooth/hci_event.c
>>> +++ b/net/bluetooth/hci_event.c
>>> @@ -3106,6 +3106,17 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
>>> }
>>> }
>>>
>>> + /* The HCI_Connection_Complete event is only sent once per connection.
>>> + * Processing it more than once per connection can corrupt kernel memory.
>>> + *
>>> + * As the connection handle is set here for the first time, it indicates
>>> + * whether the connection is already set up.
>>> + */
>>> + if (conn->handle) {
>>> + bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
>>> + goto unlock;
>>> + }
>>> +
>>> if (!ev->status) {
>>> conn->handle = __le16_to_cpu(ev->handle);
>>>
>
>
>
Best
Sönke
next prev parent reply other threads:[~2022-01-21 23:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-21 17:36 Soenke Huster
2022-01-21 18:22 ` Sönke Huster
2022-01-21 21:31 ` Luiz Augusto von Dentz
2022-01-21 23:18 ` Sönke Huster [this message]
2022-01-21 23:32 ` Luiz Augusto von Dentz
2022-01-21 23:52 ` Sönke Huster
2022-01-22 0:06 ` Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=995e58bb-6dfb-b3db-c8a5-b9e30dbb104d@eknoes.de \
--to=soenke.huster@eknoes.de \
--cc=davem@davemloft.net \
--cc=johan.hedberg@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®