History

Bluetooth: hci_sync: Fix advertising data UAFs

hci_find_adv_instance() returns an adv_info pointer that is valid only while hdev->lock is held. The advertising command-sync paths perform instance lookups without that lock and, in some cases, retain the pointer while waiting for a controller response. An advertising termination event can therefore interleave as follows: hci_cmd_sync_work hci_rx_work hci_find_adv_instance() __hci_cmd_sync_status() wait for controller reply hci_dev_lock() hci_remove_adv_instance() kfree(adv) adv->scan_rsp_changed = false KASAN reported: BUG: KASAN: slab-use-after-free in hci_set_ext_scan_rsp_data_sync+0x2e1/0x300 Write of size 1 at addr ffff88810a45d21d by task kworker/u17:0/88 Workqueue: hci0 hci_cmd_sync_work Call Trace: hci_set_ext_scan_rsp_data_sync+0x2e1/0x300 hci_schedule_adv_instance_sync+0x390/0x4c0 hci_cmd_sync_work+0x173/0x300 Allocated by task 87: hci_add_adv_instance+0x538/0xac0 add_advertising+0x885/0x1160 Freed by task 89: kfree+0x131/0x3c0 hci_remove_adv_instance+0x1d8/0x3b0 hci_le_ext_adv_term_evt+0x17b/0x730 Protect the instance lookup and payload construction in the extended advertising, scan response, and periodic advertising data paths. Snapshot the advertising parameters under hdev->lock, but release the lock before waiting for the controller. Clear advertising-data dirty bits before issuing their commands and restore them after a failure using a fresh lookup. Likewise, update the reported transmit power through a fresh lookup after the parameter command completes. No adv_info pointer then survives an HCI command wait.

Chengfeng Ye committed 3 months ago

Commit cdc36db1 parent b230e5b1 file · +97 −34Browse files

Unified · Split

net/bluetooth/hci_sync.c+97−34
@@ -1233,10 +1233,11 @@ static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, · 1232 unmodified lines
12331233 }
12341234
12351235 static int
1236−hci_set_ext_adv_params_sync(struct hci_dev *hdev, struct adv_info *adv,
1236+hci_set_ext_adv_params_sync(struct hci_dev *hdev, u8 instance,
12371237 const struct hci_cp_le_set_ext_adv_params *cp,
12381238 struct hci_rp_le_set_ext_adv_params *rp)
12391239 {
1240+ struct adv_info *adv;
12401241 struct sk_buff *skb;
12411242
12421243 skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(*cp),
@@ -1264,11 +1265,15 @@ hci_set_ext_adv_params_sync(struct hci_dev *hdev, struct adv_info *adv, · 21 unmodified lines
12641265
12651266 if (!rp->status) {
12661267 hdev->adv_addr_type = cp->own_addr_type;
1267− if (!cp->handle) {
1268+ if (!instance) {
12681269 /* Store in hdev for instance 0 */
12691270 hdev->adv_tx_power = rp->tx_power;
1270− } else if (adv) {
1271− adv->tx_power = rp->tx_power;
1271+ } else {
1272+ hci_dev_lock(hdev);
1273+ adv = hci_find_adv_instance(hdev, instance);
1274+ if (adv)
1275+ adv->tx_power = rp->tx_power;
1276+ hci_dev_unlock(hdev);
12721277 }
12731278 }
12741279
@@ -1284,9 +1289,13 @@ static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) · 9 unmodified lines
12841289 int err;
12851290
12861291 if (instance) {
1292+ hci_dev_lock(hdev);
1293+
12871294 adv = hci_find_adv_instance(hdev, instance);
1288− if (!adv || !adv->adv_data_changed)
1295+ if (!adv || !adv->adv_data_changed) {
1296+ hci_dev_unlock(hdev);
12891297 return 0;
1298+ }
12901299 }
12911300
12921301 len = eir_create_adv_data(hdev, instance, pdu->data,
@@ -1297,16 +1306,27 @@ static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) · 4 unmodified lines
12971306 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
12981307 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
12991308
1309+ if (adv) {
1310+ adv->adv_data_changed = false;
1311+ hci_dev_unlock(hdev);
1312+ }
1313+
13001314 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
13011315 struct_size(pdu, data, len), pdu,
13021316 HCI_CMD_TIMEOUT);
1303− if (err)
1317+ if (err) {
1318+ if (instance) {
1319+ hci_dev_lock(hdev);
1320+ adv = hci_find_adv_instance(hdev, instance);
1321+ if (adv)
1322+ adv->adv_data_changed = true;
1323+ hci_dev_unlock(hdev);
1324+ }
1325+
13041326 return err;
1327+ }
13051328
1306− /* Update data if the command succeed */
1307− if (adv) {
1308− adv->adv_data_changed = false;
1309− } else {
1329+ if (!instance) {
13101330 memcpy(hdev->adv_data, pdu->data, len);
13111331 hdev->adv_data_len = len;
13121332 }
@@ -1360,22 +1380,22 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 47 unmodified lines
13601380 struct adv_info *adv;
13611381 bool secondary_adv;
13621382
1363− if (instance > 0) {
1364− adv = hci_find_adv_instance(hdev, instance);
1365− if (!adv)
1366− return -EINVAL;
1367− } else {
1368− adv = NULL;
1369− }
1370−
13711383 /* Updating parameters of an active instance will return a
1372− * Command Disallowed error, so we must first disable the
1373− * instance if it is active.
1384+ * Command Disallowed error, so disable it before taking a snapshot.
13741385 */
1375− if (adv) {
1386+ if (instance > 0) {
13761387 err = hci_disable_ext_adv_instance_sync(hdev, instance);
13771388 if (err)
13781389 return err;
1390+
1391+ hci_dev_lock(hdev);
1392+ adv = hci_find_adv_instance(hdev, instance);
1393+ if (!adv) {
1394+ hci_dev_unlock(hdev);
1395+ return -EINVAL;
1396+ }
1397+ } else {
1398+ adv = NULL;
13791399 }
13801400
13811401 flags = hci_adv_instance_flags(hdev, instance);
@@ -1386,8 +1406,11 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 4 unmodified lines
13861406 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
13871407 mgmt_get_connectable(hdev);
13881408
1389− if (!is_advertising_allowed(hdev, connectable))
1409+ if (!is_advertising_allowed(hdev, connectable)) {
1410+ if (instance)
1411+ hci_dev_unlock(hdev);
13901412 return -EPERM;
1413+ }
13911414
13921415 /* Set require_privacy to true only when non-connectable
13931416 * advertising is used and it is not periodic.
@@ -1398,8 +1421,11 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 4 unmodified lines
13981421 err = hci_get_random_address(hdev, require_privacy,
13991422 adv_use_rpa(hdev, flags), adv,
14001423 &own_addr_type, &random_addr);
1401− if (err < 0)
1424+ if (err < 0) {
1425+ if (instance)
1426+ hci_dev_unlock(hdev);
14021427 return err;
1428+ }
14031429
14041430 memset(&cp, 0, sizeof(cp));
14051431
@@ -1450,6 +1476,9 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 44 unmodified lines
14501476 cp.channel_map = hdev->le_adv_channel_map;
14511477 cp.handle = adv ? adv->handle : instance;
14521478
1479+ if (instance)
1480+ hci_dev_unlock(hdev);
1481+
14531482 if (flags & MGMT_ADV_FLAG_SEC_2M) {
14541483 cp.primary_phy = HCI_ADV_PHY_1M;
14551484 cp.secondary_phy = HCI_ADV_PHY_2M;
@@ -1462,12 +1491,12 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 6 unmodified lines
14621491 cp.secondary_phy = HCI_ADV_PHY_1M;
14631492 }
14641493
1465− err = hci_set_ext_adv_params_sync(hdev, adv, &cp, &rp);
1494+ err = hci_set_ext_adv_params_sync(hdev, instance, &cp, &rp);
14661495 if (err)
14671496 return err;
14681497
14691498 /* Update adv data as tx power is known now */
1470− err = hci_set_ext_adv_data_sync(hdev, cp.handle);
1499+ err = hci_set_ext_adv_data_sync(hdev, instance);
14711500 if (err)
14721501 return err;
14731502
@@ -1475,9 +1504,14 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 1 unmodified line
14751504 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
14761505 bacmp(&random_addr, BDADDR_ANY)) {
14771506 /* Check if random address need to be updated */
1478− if (adv) {
1479− if (!bacmp(&random_addr, &adv->random_addr))
1507+ if (instance) {
1508+ hci_dev_lock(hdev);
1509+ adv = hci_find_adv_instance(hdev, instance);
1510+ if (!adv || !bacmp(&random_addr, &adv->random_addr)) {
1511+ hci_dev_unlock(hdev);
14801512 return 0;
1513+ }
1514+ hci_dev_unlock(hdev);
14811515 } else {
14821516 if (!bacmp(&random_addr, &hdev->random_addr))
14831517 return 0;
@@ -1499,9 +1533,13 @@ static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) · 15 unmodified lines
14991533 int err;
15001534
15011535 if (instance) {
1536+ hci_dev_lock(hdev);
1537+
15021538 adv = hci_find_adv_instance(hdev, instance);
1503− if (!adv || !adv->scan_rsp_changed)
1539+ if (!adv || !adv->scan_rsp_changed) {
1540+ hci_dev_unlock(hdev);
15041541 return 0;
1542+ }
15051543 }
15061544
15071545 len = eir_create_scan_rsp(hdev, instance, pdu->data);
@@ -1511,15 +1549,27 @@ static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) · 3 unmodified lines
15111549 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
15121550 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
15131551
1552+ if (adv) {
1553+ adv->scan_rsp_changed = false;
1554+ hci_dev_unlock(hdev);
1555+ }
1556+
15141557 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
15151558 struct_size(pdu, data, len), pdu,
15161559 HCI_CMD_TIMEOUT);
1517− if (err)
1560+ if (err) {
1561+ if (instance) {
1562+ hci_dev_lock(hdev);
1563+ adv = hci_find_adv_instance(hdev, instance);
1564+ if (adv)
1565+ adv->scan_rsp_changed = true;
1566+ hci_dev_unlock(hdev);
1567+ }
1568+
15181569 return err;
1570+ }
15191571
1520− if (adv) {
1521− adv->scan_rsp_changed = false;
1522− } else {
1572+ if (!instance) {
15231573 memcpy(hdev->scan_rsp_data, pdu->data, len);
15241574 hdev->scan_rsp_data_len = len;
15251575 }
@@ -1534,8 +1584,14 @@ static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) · 8 unmodified lines
15341584
15351585 memset(&cp, 0, sizeof(cp));
15361586
1587+ if (instance)
1588+ hci_dev_lock(hdev);
1589+
15371590 len = eir_create_scan_rsp(hdev, instance, cp.data);
15381591
1592+ if (instance)
1593+ hci_dev_unlock(hdev);
1594+
15391595 if (hdev->scan_rsp_data_len == len &&
15401596 !memcmp(cp.data, hdev->scan_rsp_data, len))
15411597 return 0;
@@ -1670,9 +1726,13 @@ static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) · 128 unmodified lines
16701726 struct adv_info *adv = NULL;
16711727
16721728 if (instance) {
1729+ hci_dev_lock(hdev);
1730+
16731731 adv = hci_find_adv_instance(hdev, instance);
1674− if (!adv || !adv->periodic)
1732+ if (!adv || !adv->periodic) {
1733+ hci_dev_unlock(hdev);
16751734 return 0;
1735+ }
16761736 }
16771737
16781738 len = eir_create_per_adv_data(hdev, instance, pdu->data);
@@ -1681,6 +1741,9 @@ static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) · 2 unmodified lines
16811741 pdu->handle = adv ? adv->handle : instance;
16821742 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
16831743
1744+ if (adv)
1745+ hci_dev_unlock(hdev);
1746+
16841747 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
16851748 struct_size(pdu, data, len), pdu,
16861749 HCI_CMD_TIMEOUT);
@@ -6523,7 +6586,7 @@ static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev, · 4836 unmodified lines
65236586 if (err)
65246587 return err;
65256588
6526− err = hci_set_ext_adv_params_sync(hdev, NULL, &cp, &rp);
6589+ err = hci_set_ext_adv_params_sync(hdev, 0, &cp, &rp);
65276590 if (err)
65286591 return err;
65296592

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t