Skip to content

Commit c9d372d

Browse files
jhedbergm-alperen-sener
authored andcommitted
[nrf fromtree] Bluetooth: Host: Classic: Fix allocated PSM comparison
The code for checking for allocated BR/EDR PSMs was potentially resulting in an integer overflow, due to doing a <= UINT16_MAX (0xffff) comparison on a uint16_t variable. To avoid this, use a uint32_t variable internally. Signed-off-by: Johan Hedberg <[email protected]> (cherry picked from commit 90367c8)
1 parent 9713a00 commit c9d372d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

subsys/bluetooth/host/classic/l2cap_br.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,45 @@ static void l2cap_br_conf_rsp(struct bt_l2cap_br *l2cap, uint8_t ident,
11541154
}
11551155

11561156
static int bt_l2cap_br_allocate_psm(uint16_t *psm)
1157+
{
1158+
/* DYN_END is UINT16_MAX, so to be able to do a psm <= DYN_END comparison
1159+
* we need to use uint32_t as the type.
1160+
*/
1161+
static uint32_t allocated_psm = L2CAP_BR_PSM_DYN_START;
1162+
1163+
if (allocated_psm < L2CAP_BR_PSM_DYN_END) {
1164+
allocated_psm = allocated_psm + 1;
1165+
} else {
1166+
goto failed;
1167+
}
1168+
1169+
for (; allocated_psm <= L2CAP_BR_PSM_DYN_END; allocated_psm++) {
1170+
/* Bluetooth Core Specification Version 6.0 | Vol 3, Part A, section 4.2
1171+
*
1172+
* The PSM field is at least two octets in length. All PSM values shall have
1173+
* the least significant bit of the most significant octet equal to 0 and the
1174+
* least significant bit of all other octets equal to 1.
1175+
*/
1176+
if ((allocated_psm & 0x0101) != 0x0001) {
1177+
continue;
1178+
}
1179+
1180+
if (l2cap_br_server_lookup_psm((uint16_t)allocated_psm)) {
1181+
LOG_DBG("PSM 0x%04x has been used", allocated_psm);
1182+
continue;
1183+
}
1184+
1185+
LOG_DBG("Allocated PSM 0x%04x for new server", allocated_psm);
1186+
*psm = (uint16_t)allocated_psm;
1187+
return 0;
1188+
}
1189+
1190+
failed:
1191+
LOG_WRN("No free dynamic PSMs available");
1192+
return -EADDRNOTAVAIL;
1193+
}
1194+
1195+
int bt_l2cap_br_server_register(struct bt_l2cap_server *server)
11571196
{
11581197
static uint16_t allocated_psm = L2CAP_BR_PSM_DYN_START;
11591198

0 commit comments

Comments
 (0)