Skip to content

Commit 0b88078

Browse files
HaavardReimmahadevan108
authored andcommitted
Bluetooth: host: Fix unsafe cast in is_subscribed
The current implementation casts the user data to the attribute value, which makes an assumption about the user data. This commit changes the implementation to use the attribute value read function when extracting the characteristic properties. Signed-off-by: Håvard Reierstad <[email protected]>
1 parent 8d07197 commit 0b88078

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

subsys/bluetooth/host/gatt.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,10 +3475,24 @@ bool bt_gatt_is_subscribed(struct bt_conn *conn,
34753475

34763476
/* Check if attribute is a characteristic declaration */
34773477
if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CHRC)) {
3478-
struct bt_gatt_chrc *chrc = attr->user_data;
3478+
uint8_t properties;
3479+
ssize_t len;
3480+
3481+
CHECKIF(!attr->read) {
3482+
LOG_ERR("Read method not set");
3483+
return false;
3484+
}
3485+
/* The charactestic properties is the first byte of the attribute value */
3486+
len = attr->read(NULL, attr, &properties, 1, 0);
3487+
if (len < 0) {
3488+
LOG_ERR("Failed to read attribute (err %zd)", len);
3489+
return false;
3490+
} else if (len != 1) {
3491+
LOG_ERR("Invalid read length: %zd", len);
3492+
return false;
3493+
}
34793494

3480-
if (!(chrc->properties &
3481-
(BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE))) {
3495+
if (!(properties & (BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE))) {
34823496
/* Characteristic doesn't support subscription */
34833497
return false;
34843498
}

0 commit comments

Comments
 (0)