Skip to contentSkip to conversation

shared/csip: Fix returning invalid data to attribute Size reads

PATCH Applied Luiz Augusto von Dentz 3 participants 8 messages Oct 2, 2023 – Oct 3, 2023 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/6 shared/csip: Fix returning invalid data to attribute Size reads

From: Luiz Augusto von Dentz <[email protected]>

The attribute Size value is stored in the size_val not on size member which represents the attribute object.

---
 src/shared/csip.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/shared/csip.c +2 -2
index 04a8d542b390..24d5d6a323bd 100644
@@ -291,8 +291,8 @@ static void csis_size_read(struct gatt_db_attribute *attrib,
 	struct bt_csis *csis = user_data;
 	struct iovec iov;
 
-	iov.iov_base = &csis->size;
-	iov.iov_len = sizeof(csis->size);
+	iov.iov_base = &csis->size_val;
+	iov.iov_len = sizeof(csis->size_val);
 
 	gatt_db_attribute_read_result(attrib, id, 0, iov.iov_base,
 							iov.iov_len);
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 2/6 shared/csip: Fix not requiring encryption

From: Luiz Augusto von Dentz <[email protected]>

According to CSIS spec all attributes shall require encryption:

'Table 5.1: Coordinated Set Identification Service characteristics'

---
 src/shared/csip.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
src/shared/csip.c +10 -4
index 24d5d6a323bd..eb80bbc3b26c 100644
@@ -721,7 +721,8 @@ static struct csis_sirk *sirk_new(struct bt_csis *csis, struct gatt_db *db,
 	bt_uuid16_create(&uuid, CS_SIRK);
 	csis->sirk = gatt_db_service_add_characteristic(csis->service,
 					&uuid,
-					BT_ATT_PERM_READ,
+					BT_ATT_PERM_READ |
+					BT_ATT_PERM_READ_ENCRYPT,
 					BT_GATT_CHRC_PROP_READ,
 					csis_sirk_read, NULL,
 					csis);
@@ -729,7 +730,8 @@ static struct csis_sirk *sirk_new(struct bt_csis *csis, struct gatt_db *db,
 	bt_uuid16_create(&uuid, CS_SIZE);
 	csis->size = gatt_db_service_add_characteristic(csis->service,
 					&uuid,
-					BT_ATT_PERM_READ,
+					BT_ATT_PERM_READ |
+					BT_ATT_PERM_READ_ENCRYPT,
 					BT_GATT_CHRC_PROP_READ,
 					csis_size_read, NULL,
 					csis);
@@ -737,7 +739,10 @@ static struct csis_sirk *sirk_new(struct bt_csis *csis, struct gatt_db *db,
 	/* Lock */
 	bt_uuid16_create(&uuid, CS_LOCK);
 	csis->lock = gatt_db_service_add_characteristic(csis->service, &uuid,
-					BT_ATT_PERM_READ,
+					BT_ATT_PERM_READ |
+					BT_ATT_PERM_READ_ENCRYPT |
+					BT_ATT_PERM_WRITE |
+					BT_ATT_PERM_WRITE_ENCRYPT,
 					BT_GATT_CHRC_PROP_READ |
 					BT_GATT_CHRC_PROP_WRITE |
 					BT_GATT_CHRC_PROP_NOTIFY,
@@ -751,7 +756,8 @@ static struct csis_sirk *sirk_new(struct bt_csis *csis, struct gatt_db *db,
 	/* Rank */
 	bt_uuid16_create(&uuid, CS_RANK);
 	csis->rank = gatt_db_service_add_characteristic(csis->service, &uuid,
-					BT_ATT_PERM_READ,
+					BT_ATT_PERM_READ |
+					BT_ATT_PERM_READ_ENCRYPT,
 					BT_GATT_CHRC_PROP_READ,
 					csis_rank_read_cb,
 					NULL, csis);
Signature · 1 line
2.41.0
author maintainer
#2 lore raw

PATCH 3/6 shared/csip: Fix not always reading SIRK value

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This makes sure the SIRK value is always read otherwise its value can be outdated or not even read if the connection was interrupted before read procedure was completed.

---
 src/shared/csip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/shared/csip.c +1 -1
index eb80bbc3b26c..85de63ea626b 100644
@@ -597,7 +597,7 @@ static void foreach_csis_char(struct gatt_db_attribute *attr, void *user_data)
 		DBG(csip, "SIRK found: handle 0x%04x", value_handle);
 
 		csis = csip_get_csis(csip);
-		if (!csis || csis->sirk)
+		if (!csis)
 			return;
 
 		csis->sirk = attr;
Signature · 1 line
2.41.0
author maintainer
#3 lore raw

PATCH 4/6 device: Add btd_device_get_ltk

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds btd_device_get_ltk function which can be used by plugins to access the LTK key.

---
 src/device.c | 17 +++++++++++++++++
 src/device.h |  2 ++
 2 files changed, 19 insertions(+)
src/device.c +17 -0
index 35ce1df0fe74..a734fff0dc73 100644
@@ -1938,6 +1938,23 @@ void device_set_ltk(struct btd_device *device, const uint8_t val[16],
 	queue_foreach(device->sirks, add_set, device);
 }
 
+bool btd_device_get_ltk(struct btd_device *device, uint8_t key[16],
+				bool *central, uint8_t *enc_size)
+{
+	if (!device || !device->ltk || !key)
+		return false;
+
+	memcpy(key, device->ltk->key, sizeof(device->ltk->key));
+
+	if (central)
+		*central = device->ltk->central;
+
+	if (enc_size)
+		*enc_size = device->ltk->enc_size;
+
+	return true;
+}
+
 static bool match_sirk(const void *data, const void *match_data)
 {
 	const struct sirk_info *sirk = data;
src/device.h +2 -0
index 0a9e51533ca5..8bb38669d457 100644
@@ -132,6 +132,8 @@ void device_request_disconnect(struct btd_device *device, DBusMessage *msg);
 bool device_is_disconnecting(struct btd_device *device);
 void device_set_ltk(struct btd_device *device, const uint8_t val[16],
 				bool central, uint8_t enc_size);
+bool btd_device_get_ltk(struct btd_device *device, uint8_t val[16],
+				bool *central, uint8_t *enc_size);
 bool btd_device_add_set(struct btd_device *device, bool encrypted,
 				uint8_t sirk[16], uint8_t size, uint8_t rank);
 void device_store_svc_chng_ccc(struct btd_device *device, uint8_t bdaddr_type,
Signature · 1 line
2.41.0
author maintainer
#4 lore raw

PATCH 5/6 csip: Add support for SIRK encryption

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This implements SIRK value encryption using the LTK which is accessed using btd_device_get_ltk.

---
 profiles/audio/csip.c | 36 ++++++++++++++++++++++++++----
 src/shared/csip.c     | 51 ++++---------------------------------------
 src/shared/csip.h     |  5 ++---
 3 files changed, 38 insertions(+), 54 deletions(-)
profiles/audio/csip.c +32 -4
index 05bf588d8d6f..a697ebdfbda0 100644
@@ -40,6 +40,7 @@
 #include "src/shared/gatt-client.h"
 #include "src/shared/gatt-server.h"
 #include "src/shared/csip.h"
+#include "src/shared/crypto.h"
 
 #include "btio/btio.h"
 #include "src/plugin.h"
@@ -313,10 +314,37 @@ static struct btd_profile csip_profile = {
 	.experimental	= true,
 };
 
-static bool csis_ltk_read(struct bt_csip *csip, uint8_t k[16], void *user_data)
+static bool csis_encrypt(struct bt_att *att, uint8_t val[16])
 {
-	/* TODO: Retrieve LTK using device object */
-	return false;
+	struct btd_device *device;
+	struct bt_crypto *crypto;
+	uint8_t ltk[16];
+	bool ret;
+
+	device = btd_adapter_find_device_by_fd(bt_att_get_fd(att));
+	if (!device) {
+		error("Unable to find device");
+		return false;
+	}
+
+	if (!btd_device_get_ltk(device, ltk, NULL, NULL)) {
+		error("Unable to get device LTK");
+		return false;
+	}
+
+	crypto = bt_crypto_new();
+	if (!crypto) {
+		error("Failed to open crypto");
+		return false;
+	}
+
+	ret = bt_crypto_sef(crypto, ltk, val, val);
+	if (!ret)
+		error("Failed to encrypt SIRK using LTK");
+
+	bt_crypto_unref(crypto);
+
+	return ret;
 }
 
 static void csis_data_add(struct csis_data *data)
@@ -332,7 +360,7 @@ static void csis_data_add(struct csis_data *data)
 
 	bt_csip_set_sirk(data->csip, btd_opts.csis.encrypt, btd_opts.csis.sirk,
 				btd_opts.csis.size, btd_opts.csis.rank,
-				csis_ltk_read, data);
+				csis_encrypt);
 
 	if (!servers)
 		servers = queue_new();
src/shared/csip.c +4 -47

87 lines not on this page · raw message

src/shared/csip.h +2 -3
index bc5519cfbc49..81c8954aba8d 100644
@@ -27,8 +27,7 @@ typedef void (*bt_csip_ready_func_t)(struct bt_csip *csip, void *user_data);
 typedef void (*bt_csip_destroy_func_t)(void *user_data);
 typedef void (*bt_csip_debug_func_t)(const char *str, void *user_data);
 typedef void (*bt_csip_func_t)(struct bt_csip *csip, void *user_data);
-typedef bool (*bt_csip_ltk_func_t)(struct bt_csip *csip, uint8_t k[16],
-							void *user_data);
+typedef bool (*bt_csip_encrypt_func_t)(struct bt_att *att, uint8_t k[16]);
 typedef bool (*bt_csip_sirk_func_t)(struct bt_csip *csip, uint8_t type,
 				    uint8_t k[16], uint8_t size, uint8_t rank,
 				    void *user_data);
@@ -54,7 +53,7 @@ struct bt_csip *bt_csip_new(struct gatt_db *ldb, struct gatt_db *rdb);
 
 bool bt_csip_set_sirk(struct bt_csip *csip, bool encrypt,
 				uint8_t k[16], uint8_t size, uint8_t rank,
-				bt_csip_ltk_func_t func, void *user_data);
+				bt_csip_encrypt_func_t func);
 
 bool bt_csip_get_sirk(struct bt_csip *csip, uint8_t *type,
 				uint8_t k[16], uint8_t *size, uint8_t *rank);
Signature · 1 line
2.41.0
author maintainer
#5 lore raw

PATCH 6/6 main.conf: Fix default of CSIS.encrypt

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

CSIS.encrypt setting shall default to true, not false.

---
 src/main.c | 1 +
 1 file changed, 1 insertion(+)
src/main.c +1 -0
index cddf1396197b..ddb73431df94 100644
@@ -1195,6 +1195,7 @@ static void init_defaults(void)
 	btd_opts.avdtp.stream_mode = BT_IO_MODE_BASIC;
 
 	btd_opts.advmon.rssi_sampling_period = 0xFF;
+	btd_opts.csis.encrypt = true;
 }
 
 static void log_handler(const gchar *log_domain, GLogLevelFlags log_level,
Signature · 1 line
2.41.0
#6 lore raw
In reply to Luiz Augusto von Dentz

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=789421

---Test result---

Test Summary:

CheckPatch                    PASS      2.70 seconds
GitLint                       PASS      7.02 seconds
BuildEll                      PASS      28.76 seconds
BluezMake                     PASS      979.14 seconds
MakeCheck                     PASS      12.63 seconds
MakeDistcheck                 PASS      163.32 seconds
CheckValgrind                 PASS      264.15 seconds
CheckSmatch                   PASS      356.79 seconds
bluezmakeextell               PASS      109.03 seconds
IncrementalBuild              PASS      4989.81 seconds
ScanBuild                     PASS      1092.62 seconds

---
Regards,
Linux Bluetooth

#7 lore raw
In reply to Luiz Augusto von Dentz
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 7 lines · On Mon, 2 Oct 2023 16:13:05 -0700 you wrote:

From: Luiz Augusto von Dentz <[email protected]>

The attribute Size value is stored in the size_val not on size member which represents the attribute object.

---
 src/shared/csip.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Here is the summary with links:
- [BlueZ,1/6] shared/csip: Fix returning invalid data to attribute Size reads

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=b938b05559d3
  - [BlueZ,2/6] shared/csip: Fix not requiring encryption
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=a1920af6f81f
  - [BlueZ,3/6] shared/csip: Fix not always reading SIRK value
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=65b53b0d3a88
  - [BlueZ,4/6] device: Add btd_device_get_ltk
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=c35304f32c42
  - [BlueZ,5/6] csip: Add support for SIRK encryption
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=267bf36d844b
  - [BlueZ,6/6] main.conf: Fix default of CSIS.encrypt
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=954b8e5324fd

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html