public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] Bluetooth: Add more enc key size check
@ 2023-12-11  9:29 Alex Lu
  2023-12-11  9:57 ` [v2] " bluez.test.bot
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Lu @ 2023-12-11  9:29 UTC (permalink / raw)
  To: Marcel Holtmann, Johan Hedberg, Luiz Augusto von Dentz,
	linux-bluetooth, linux-kernel
  Cc: Max Chou, Karen Hsu

From: Alex Lu <alex_lu@realsil.com.cn>

When we are slave role and receives l2cap conn req when encryption has
started, we should check the enc key size to avoid KNOB attack or BLUFFS
attack.
From SIG recommendation, implementations are advised to reject
service-level connections on an encrypted baseband link with key
strengths below 7 octets.

The btmon log below shows the case that lacks enc key size check.

> HCI Event: Connect Request (0x04) plen 10
        Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Class: 0x480104
          Major class: Computer (desktop, notebook, PDA, organizers)
          Minor class: Desktop workstation
          Capturing (Scanner, Microphone)
          Telephony (Cordless telephony, Modem, Headset)
        Link type: ACL (0x01)
< HCI Command: Accept Connection Request (0x01|0x0009) plen 7
        Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Role: Peripheral (0x01)
> HCI Event: Command Status (0x0f) plen 4
      Accept Connection Request (0x01|0x0009) ncmd 2
        Status: Success (0x00)
> HCI Event: Connect Complete (0x03) plen 11
        Status: Success (0x00)
        Handle: 1
        Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Link type: ACL (0x01)
        Encryption: Disabled (0x00)
...

> HCI Event: Encryption Change (0x08) plen 4
        Status: Success (0x00)
        Handle: 1 Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Encryption: Enabled with E0 (0x01)
< HCI Command: Read Encryption Key Size (0x05|0x0008) plen 2
        Handle: 1 Address: BB:22:33:44:55:99 (OUI BB-22-33)
> HCI Event: Command Complete (0x0e) plen 7
      Read Encryption Key Size (0x05|0x0008) ncmd 2
        Status: Success (0x00)
        Handle: 1 Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Key size: 6
...

// We should check the enc key size
> ACL Data RX: Handle 1 flags 0x02 dlen 12
      L2CAP: Connection Request (0x02) ident 3 len 4
        PSM: 25 (0x0019)
        Source CID: 64
< ACL Data TX: Handle 1 flags 0x00 dlen 16
      L2CAP: Connection Response (0x03) ident 3 len 8
        Destination CID: 64
        Source CID: 64
        Result: Connection pending (0x0001)
        Status: Authorization pending (0x0002)
> HCI Event: Number of Completed Packets (0x13) plen 5
        Num handles: 1
        Handle: 1 Address: BB:22:33:44:55:99 (OUI BB-22-33)
        Count: 1
        #35: len 16 (25 Kb/s)
        Latency: 5 msec (2-7 msec ~4 msec)
< ACL Data TX: Handle 1 flags 0x00 dlen 16
      L2CAP: Connection Response (0x03) ident 3 len 8
        Destination CID: 64
        Source CID: 64
        Result: Connection successful (0x0000)
        Status: No further information available (0x0000)

Signed-off-by: Alex Lu <alex_lu@realsil.com.cn>
Signed-off-by: Max Chou <max.chou@realtek.com>
---
Changes in v2:
  - Fix compiling issue reported by sparse

 net/bluetooth/l2cap_core.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 17ca13e8c044..a9809fbe6b00 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1669,7 +1669,13 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
 			rsp.dcid = cpu_to_le16(chan->scid);
 
 			if (l2cap_chan_check_security(chan, false)) {
-				if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
+				if (!l2cap_check_enc_key_size(conn->hcon)) {
+					l2cap_state_change(chan, BT_DISCONN);
+					__set_chan_timer(chan,
+							 L2CAP_DISC_TIMEOUT);
+					rsp.result = cpu_to_le16(L2CAP_CR_SEC_BLOCK);
+					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
+				} else if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
 					chan->ops->defer(chan);
@@ -4202,7 +4208,15 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
 
 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
 		if (l2cap_chan_check_security(chan, false)) {
-			if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
+			/* As slave role, we should check the enc key size when
+			 * l2cap conn req is received.
+			 */
+			if (!l2cap_check_enc_key_size(conn->hcon)) {
+				l2cap_state_change(chan, BT_DISCONN);
+				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
+				result = L2CAP_CR_SEC_BLOCK;
+				status = L2CAP_CS_NO_INFO;
+			} else if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
 				l2cap_state_change(chan, BT_CONNECT2);
 				result = L2CAP_CR_PEND;
 				status = L2CAP_CS_AUTHOR_PEND;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [v2] Bluetooth: Add more enc key size check
  2023-12-11  9:29 [PATCH v2] Bluetooth: Add more enc key size check Alex Lu
@ 2023-12-11  9:57 ` bluez.test.bot
  2023-12-11 17:52   ` Luiz Augusto von Dentz
  0 siblings, 1 reply; 3+ messages in thread
From: bluez.test.bot @ 2023-12-11  9:57 UTC (permalink / raw)
  To: linux-bluetooth, alex_lu

[-- Attachment #1: Type: text/plain, Size: 1422 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=808718

---Test result---

Test Summary:
CheckPatch                    PASS      0.48 seconds
GitLint                       PASS      0.21 seconds
SubjectPrefix                 PASS      0.07 seconds
BuildKernel                   PASS      27.52 seconds
CheckAllWarning               PASS      30.74 seconds
CheckSparse                   PASS      36.91 seconds
CheckSmatch                   PASS      99.35 seconds
BuildKernel32                 PASS      27.19 seconds
TestRunnerSetup               PASS      430.58 seconds
TestRunner_l2cap-tester       PASS      24.23 seconds
TestRunner_iso-tester         PASS      45.04 seconds
TestRunner_bnep-tester        PASS      7.09 seconds
TestRunner_mgmt-tester        PASS      160.98 seconds
TestRunner_rfcomm-tester      PASS      11.03 seconds
TestRunner_sco-tester         PASS      14.59 seconds
TestRunner_ioctl-tester       PASS      12.19 seconds
TestRunner_mesh-tester        PASS      8.72 seconds
TestRunner_smp-tester         PASS      9.77 seconds
TestRunner_userchan-tester    PASS      7.32 seconds
IncrementalBuild              PASS      25.72 seconds



---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [v2] Bluetooth: Add more enc key size check
  2023-12-11  9:57 ` [v2] " bluez.test.bot
@ 2023-12-11 17:52   ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2023-12-11 17:52 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: alex_lu

Hi Alex,

On Mon, Dec 11, 2023 at 4:57 AM <bluez.test.bot@gmail.com> wrote:
>
> This is automated email and please do not reply to this email!
>
> Dear submitter,
>
> Thank you for submitting the patches to the linux bluetooth mailing list.
> This is a CI test results with your patch series:
> PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=808718
>
> ---Test result---
>
> Test Summary:
> CheckPatch                    PASS      0.48 seconds
> GitLint                       PASS      0.21 seconds
> SubjectPrefix                 PASS      0.07 seconds
> BuildKernel                   PASS      27.52 seconds
> CheckAllWarning               PASS      30.74 seconds
> CheckSparse                   PASS      36.91 seconds
> CheckSmatch                   PASS      99.35 seconds
> BuildKernel32                 PASS      27.19 seconds
> TestRunnerSetup               PASS      430.58 seconds
> TestRunner_l2cap-tester       PASS      24.23 seconds
> TestRunner_iso-tester         PASS      45.04 seconds
> TestRunner_bnep-tester        PASS      7.09 seconds
> TestRunner_mgmt-tester        PASS      160.98 seconds
> TestRunner_rfcomm-tester      PASS      11.03 seconds
> TestRunner_sco-tester         PASS      14.59 seconds
> TestRunner_ioctl-tester       PASS      12.19 seconds
> TestRunner_mesh-tester        PASS      8.72 seconds
> TestRunner_smp-tester         PASS      9.77 seconds
> TestRunner_userchan-tester    PASS      7.32 seconds
> IncrementalBuild              PASS      25.72 seconds
>

How about doing the following:

diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 2ad7b9f86f74..f9a8fb9fcce2 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -750,9 +750,12 @@ static u8 hci_cc_read_enc_key_size(struct hci_dev
*hdev, void *data,
        } else {
                conn->enc_key_size = rp->key_size;
                status = 0;
+
+               if (conn->enc_key_size < hdev->min_enc_key_size)
+                       status = HCI_ERROR_AUTH_FAILURE;
        }

-       hci_encrypt_cfm(conn, 0);
+       hci_encrypt_cfm(conn, status);

 done:
        hci_dev_unlock(hdev);

That way we don't have to proliferate the checks over to L2CAP, etc,
so we consider that the encryption itself fails if the
conn->enc_key_size < hdev->min_enc_key_size.

>
> ---
> Regards,
> Linux Bluetooth
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-11 17:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-11  9:29 [PATCH v2] Bluetooth: Add more enc key size check Alex Lu
2023-12-11  9:57 ` [v2] " bluez.test.bot
2023-12-11 17:52   ` Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox