@@ -3910,7 +3910,7 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu,
3910
3910
struct bt_gatt_discover_params * params ,
3911
3911
uint16_t length )
3912
3912
{
3913
- const struct bt_att_read_type_rsp * rsp = pdu ;
3913
+ const struct bt_att_read_type_rsp * rsp ;
3914
3914
uint16_t handle = 0U ;
3915
3915
struct bt_gatt_include value ;
3916
3916
union {
@@ -3919,6 +3919,13 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu,
3919
3919
struct bt_uuid_128 u128 ;
3920
3920
} u ;
3921
3921
3922
+ if (length < sizeof (* rsp )) {
3923
+ LOG_WRN ("Parse err" );
3924
+ goto done ;
3925
+ }
3926
+
3927
+ rsp = pdu ;
3928
+
3922
3929
/* Data can be either in UUID16 or UUID128 */
3923
3930
switch (rsp -> len ) {
3924
3931
case 8 : /* UUID16 */
@@ -4003,14 +4010,21 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu,
4003
4010
struct bt_gatt_discover_params * params ,
4004
4011
uint16_t length )
4005
4012
{
4006
- const struct bt_att_read_type_rsp * rsp = pdu ;
4013
+ const struct bt_att_read_type_rsp * rsp ;
4007
4014
uint16_t handle = 0U ;
4008
4015
union {
4009
4016
struct bt_uuid uuid ;
4010
4017
struct bt_uuid_16 u16 ;
4011
4018
struct bt_uuid_128 u128 ;
4012
4019
} u ;
4013
4020
4021
+ if (length < sizeof (* rsp )) {
4022
+ LOG_WRN ("Parse err" );
4023
+ goto done ;
4024
+ }
4025
+
4026
+ rsp = pdu ;
4027
+
4014
4028
/* Data can be either in UUID16 or UUID128 */
4015
4029
switch (rsp -> len ) {
4016
4030
case 7 : /* UUID16 */
@@ -4084,7 +4098,7 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
4084
4098
struct bt_gatt_discover_params * params ,
4085
4099
uint16_t length )
4086
4100
{
4087
- const struct bt_att_read_type_rsp * rsp = pdu ;
4101
+ const struct bt_att_read_type_rsp * rsp ;
4088
4102
uint16_t handle = 0U ;
4089
4103
uint16_t uuid_val ;
4090
4104
@@ -4094,6 +4108,13 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
4094
4108
4095
4109
uuid_val = BT_UUID_16 (params -> uuid )-> val ;
4096
4110
4111
+ if (length < sizeof (* rsp )) {
4112
+ LOG_WRN ("Parse err" );
4113
+ goto done ;
4114
+ }
4115
+
4116
+ rsp = pdu ;
4117
+
4097
4118
/* Parse characteristics found */
4098
4119
for (length -- , pdu = rsp -> data ; length >= rsp -> len ;
4099
4120
length -= rsp -> len , pdu = (const uint8_t * )pdu + rsp -> len ) {
@@ -4103,9 +4124,16 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
4103
4124
struct bt_gatt_cep cep ;
4104
4125
struct bt_gatt_scc scc ;
4105
4126
} value ;
4106
- const struct bt_att_data * data = pdu ;
4127
+ const struct bt_att_data * data ;
4107
4128
struct bt_gatt_attr attr ;
4108
4129
4130
+ if (length < sizeof (* data )) {
4131
+ LOG_WRN ("Parse err dat" );
4132
+ goto done ;
4133
+ }
4134
+
4135
+ data = pdu ;
4136
+
4109
4137
handle = sys_le16_to_cpu (data -> handle );
4110
4138
/* Handle 0 is invalid */
4111
4139
if (!handle ) {
@@ -4114,17 +4142,39 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
4114
4142
4115
4143
switch (uuid_val ) {
4116
4144
case BT_UUID_GATT_CEP_VAL :
4145
+ if (length < sizeof (* data ) + sizeof (uint16_t )) {
4146
+ LOG_WRN ("Parse err cep" );
4147
+ goto done ;
4148
+ }
4149
+
4117
4150
value .cep .properties = sys_get_le16 (data -> value );
4118
4151
break ;
4119
4152
case BT_UUID_GATT_CCC_VAL :
4153
+ if (length < sizeof (* data ) + sizeof (uint16_t )) {
4154
+ LOG_WRN ("Parse err ccc" );
4155
+ goto done ;
4156
+ }
4157
+
4120
4158
value .ccc .flags = sys_get_le16 (data -> value );
4121
4159
break ;
4122
4160
case BT_UUID_GATT_SCC_VAL :
4161
+ if (length < sizeof (* data ) + sizeof (uint16_t )) {
4162
+ LOG_WRN ("Parse err scc" );
4163
+ goto done ;
4164
+ }
4165
+
4123
4166
value .scc .flags = sys_get_le16 (data -> value );
4124
4167
break ;
4125
4168
case BT_UUID_GATT_CPF_VAL :
4126
4169
{
4127
- struct gatt_cpf * cpf = (struct gatt_cpf * )data -> value ;
4170
+ struct gatt_cpf * cpf ;
4171
+
4172
+ if (length < sizeof (* data ) + sizeof (* cpf )) {
4173
+ LOG_WRN ("Parse err cpf" );
4174
+ goto done ;
4175
+ }
4176
+
4177
+ cpf = (void * )data -> value ;
4128
4178
4129
4179
value .cpf .format = cpf -> format ;
4130
4180
value .cpf .exponent = cpf -> exponent ;
@@ -4227,14 +4277,21 @@ static uint16_t parse_service(struct bt_conn *conn, const void *pdu,
4227
4277
struct bt_gatt_discover_params * params ,
4228
4278
uint16_t length )
4229
4279
{
4230
- const struct bt_att_read_group_rsp * rsp = pdu ;
4280
+ const struct bt_att_read_group_rsp * rsp ;
4231
4281
uint16_t start_handle , end_handle = 0U ;
4232
4282
union {
4233
4283
struct bt_uuid uuid ;
4234
4284
struct bt_uuid_16 u16 ;
4235
4285
struct bt_uuid_128 u128 ;
4236
4286
} u ;
4237
4287
4288
+ if (length < sizeof (* rsp )) {
4289
+ LOG_WRN ("Parse err" );
4290
+ goto done ;
4291
+ }
4292
+
4293
+ rsp = pdu ;
4294
+
4238
4295
/* Data can be either in UUID16 or UUID128 */
4239
4296
switch (rsp -> len ) {
4240
4297
case 6 : /* UUID16 */
@@ -4365,7 +4422,7 @@ static void gatt_find_info_rsp(struct bt_conn *conn, int err,
4365
4422
const void * pdu , uint16_t length ,
4366
4423
void * user_data )
4367
4424
{
4368
- const struct bt_att_find_info_rsp * rsp = pdu ;
4425
+ const struct bt_att_find_info_rsp * rsp ;
4369
4426
struct bt_gatt_discover_params * params = user_data ;
4370
4427
uint16_t handle = 0U ;
4371
4428
uint16_t len ;
@@ -4387,6 +4444,13 @@ static void gatt_find_info_rsp(struct bt_conn *conn, int err,
4387
4444
goto done ;
4388
4445
}
4389
4446
4447
+ if (length < sizeof (* rsp )) {
4448
+ LOG_WRN ("Parse err" );
4449
+ goto done ;
4450
+ }
4451
+
4452
+ rsp = pdu ;
4453
+
4390
4454
/* Data can be either in UUID16 or UUID128 */
4391
4455
switch (rsp -> format ) {
4392
4456
case BT_ATT_INFO_16 :
@@ -4992,7 +5056,7 @@ static void gatt_prepare_write_rsp(struct bt_conn *conn, int err,
4992
5056
void * user_data )
4993
5057
{
4994
5058
struct bt_gatt_write_params * params = user_data ;
4995
- const struct bt_att_prepare_write_rsp * rsp = pdu ;
5059
+ const struct bt_att_prepare_write_rsp * rsp ;
4996
5060
size_t len ;
4997
5061
bool data_valid ;
4998
5062
@@ -5004,6 +5068,13 @@ static void gatt_prepare_write_rsp(struct bt_conn *conn, int err,
5004
5068
return ;
5005
5069
}
5006
5070
5071
+ if (length < sizeof (* rsp )) {
5072
+ LOG_WRN ("Parse err" );
5073
+ goto fail ;
5074
+ }
5075
+
5076
+ rsp = pdu ;
5077
+
5007
5078
len = length - sizeof (* rsp );
5008
5079
if (len > params -> length ) {
5009
5080
LOG_ERR ("Incorrect length, canceling write" );
0 commit comments