Skip to content

Commit a04f76f

Browse files
ye123321-gifcfriedt
authored andcommitted
Bluetooth: GATT: Enforce characteristic permissions
When a characteristic declaration is passed instead of a value attribute, ensure the associated characteristic value attribute permissions are checked before sending notifications, indications or multiple notifications. Signed-off-by: Yuan Ye <1275552818@qq.com> (cherry picked from commit c3386f9)
1 parent 05bbc63 commit a04f76f

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

‎subsys/bluetooth/host/gatt.c‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,26 @@ struct bt_gatt_attr *bt_gatt_find_by_uuid(const struct bt_gatt_attr *attr,
29082908
return found;
29092909
}
29102910

2911+
/**
2912+
* This function is meant to resolve attr to the characteristic value attribute.
2913+
* If attr is a characteristic declaration, returns the next attribute (the value).
2914+
* Otherwise returns attr unchanged.
2915+
*/
2916+
static const struct bt_gatt_attr *
2917+
bt_gatt_attr_resolve_value(const struct bt_gatt_attr *attr)
2918+
{
2919+
/* Due to the following requirement in the Core Spec Version 6.3 | Vol 3, Part G,
2920+
* Section 3.3 we can always assume that the next attribute is the Characteristic Value:
2921+
* The Characteristic Value declaration shall exist immediately following the
2922+
* characteristic declaration.
2923+
*/
2924+
if (bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CHRC) == 0) {
2925+
return bt_gatt_attr_next(attr);
2926+
}
2927+
2928+
return attr;
2929+
}
2930+
29112931
int bt_gatt_notify_cb(struct bt_conn *conn,
29122932
struct bt_gatt_notify_params *params)
29132933
{
@@ -2951,6 +2971,14 @@ int bt_gatt_notify_cb(struct bt_conn *conn,
29512971
data.handle = bt_gatt_attr_value_handle(data.attr);
29522972
}
29532973

2974+
/* If attribute is a characteristic declaration, resolve to the value
2975+
* attribute to ensure the correct permissions are checked
2976+
*/
2977+
params->attr = bt_gatt_attr_resolve_value(data.attr);
2978+
if (params->attr == NULL) {
2979+
return -EINVAL;
2980+
}
2981+
29542982
if (conn) {
29552983
return gatt_notify(conn, data.handle, params);
29562984
}
@@ -3020,14 +3048,22 @@ static int gatt_notify_multiple_verify_params(struct bt_conn *conn,
30203048
struct bt_gatt_notify_params params[],
30213049
uint16_t num_params, size_t *total_len)
30223050
{
3051+
const struct bt_gatt_attr *attr = NULL;
3052+
30233053
for (uint16_t i = 0; i < num_params; i++) {
30243054
/* Compute the total data length. */
30253055
*total_len += params[i].len;
30263056

3057+
/* If attribute is a characteristic declaration, resolve to the value
3058+
* attribute to ensure the correct permissions are checked
3059+
*/
3060+
attr = bt_gatt_attr_resolve_value(params[i].attr);
3061+
if (attr == NULL) {
3062+
return -EINVAL;
3063+
}
3064+
30273065
/* Confirm that the connection has the correct level of security. */
3028-
if (bt_gatt_check_perm(conn, params[i].attr,
3029-
BT_GATT_PERM_READ_ENCRYPT |
3030-
BT_GATT_PERM_READ_AUTHEN)) {
3066+
if (bt_gatt_check_perm(conn, attr, BT_GATT_PERM_READ_ENCRYPT_MASK)) {
30313067
LOG_DBG("Link %p is not encrypted", (void *)conn);
30323068
return -EPERM;
30333069
}
@@ -3176,6 +3212,14 @@ int bt_gatt_indicate(struct bt_conn *conn,
31763212
data.handle = bt_gatt_attr_value_handle(data.attr);
31773213
}
31783214

3215+
/* If attribute is a characteristic declaration, resolve to the value
3216+
* attribute to ensure the correct permissions are checked
3217+
*/
3218+
params->attr = bt_gatt_attr_resolve_value(data.attr);
3219+
if (params->attr == NULL) {
3220+
return -EINVAL;
3221+
}
3222+
31793223
if (conn) {
31803224
params->_ref = 1;
31813225
return gatt_indicate(conn, data.handle, params);

0 commit comments

Comments
 (0)