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.
Unified · Split
| @@ -1233,10 +1233,11 @@ static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, · 1232 unmodified lines | |||
| 1233 | } | 1233 | } |
| 1234 | 1234 | ||
| 1235 | static int | 1235 | 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, |
| 1237 | const struct hci_cp_le_set_ext_adv_params *cp, | 1237 | const struct hci_cp_le_set_ext_adv_params *cp, |
| 1238 | struct hci_rp_le_set_ext_adv_params *rp) | 1238 | struct hci_rp_le_set_ext_adv_params *rp) |
| 1239 | { | 1239 | { |
| 1240 | struct adv_info *adv; | ||
| 1240 | struct sk_buff *skb; | 1241 | struct sk_buff *skb; |
| 1241 | 1242 | ||
| 1242 | skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(*cp), | 1243 | 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 | |||
| 1264 | 1265 | ||
| 1265 | if (!rp->status) { | 1266 | if (!rp->status) { |
| 1266 | hdev->adv_addr_type = cp->own_addr_type; | 1267 | hdev->adv_addr_type = cp->own_addr_type; |
| 1267 | if (!cp->handle) { | 1268 | if (!instance) { |
| 1268 | /* Store in hdev for instance 0 */ | 1269 | /* Store in hdev for instance 0 */ |
| 1269 | hdev->adv_tx_power = rp->tx_power; | 1270 | hdev->adv_tx_power = rp->tx_power; |
| 1270 | } else if (adv) { | 1271 | } else { |
| 1271 | adv->tx_power = rp->tx_power; | 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); | ||
| 1272 | } | 1277 | } |
| 1273 | } | 1278 | } |
| 1274 | 1279 | ||
| @@ -1284,9 +1289,13 @@ static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) · 9 unmodified lines | |||
| 1284 | int err; | 1289 | int err; |
| 1285 | 1290 | ||
| 1286 | if (instance) { | 1291 | if (instance) { |
| 1292 | hci_dev_lock(hdev); | ||
| 1293 | |||
| 1287 | adv = hci_find_adv_instance(hdev, instance); | 1294 | 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); | ||
| 1289 | return 0; | 1297 | return 0; |
| 1298 | } | ||
| 1290 | } | 1299 | } |
| 1291 | 1300 | ||
| 1292 | len = eir_create_adv_data(hdev, instance, pdu->data, | 1301 | 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 | |||
| 1297 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; | 1306 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; |
| 1298 | pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG; | 1307 | pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG; |
| 1299 | 1308 | ||
| 1309 | if (adv) { | ||
| 1310 | adv->adv_data_changed = false; | ||
| 1311 | hci_dev_unlock(hdev); | ||
| 1312 | } | ||
| 1313 | |||
| 1300 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, | 1314 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, |
| 1301 | struct_size(pdu, data, len), pdu, | 1315 | struct_size(pdu, data, len), pdu, |
| 1302 | HCI_CMD_TIMEOUT); | 1316 | 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 | |||
| 1304 | return err; | 1326 | return err; |
| 1327 | } | ||
| 1305 | 1328 | ||
| 1306 | /* Update data if the command succeed */ | 1329 | if (!instance) { |
| 1307 | if (adv) { | ||
| 1308 | adv->adv_data_changed = false; | ||
| 1309 | } else { | ||
| 1310 | memcpy(hdev->adv_data, pdu->data, len); | 1330 | memcpy(hdev->adv_data, pdu->data, len); |
| 1311 | hdev->adv_data_len = len; | 1331 | hdev->adv_data_len = len; |
| 1312 | } | 1332 | } |
| @@ -1360,22 +1380,22 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 47 unmodified lines | |||
| 1360 | struct adv_info *adv; | 1380 | struct adv_info *adv; |
| 1361 | bool secondary_adv; | 1381 | bool secondary_adv; |
| 1362 | 1382 | ||
| 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 | |||
| 1371 | /* Updating parameters of an active instance will return a | 1383 | /* Updating parameters of an active instance will return a |
| 1372 | * Command Disallowed error, so we must first disable the | 1384 | * Command Disallowed error, so disable it before taking a snapshot. |
| 1373 | * instance if it is active. | ||
| 1374 | */ | 1385 | */ |
| 1375 | if (adv) { | 1386 | if (instance > 0) { |
| 1376 | err = hci_disable_ext_adv_instance_sync(hdev, instance); | 1387 | err = hci_disable_ext_adv_instance_sync(hdev, instance); |
| 1377 | if (err) | 1388 | if (err) |
| 1378 | return err; | 1389 | 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; | ||
| 1379 | } | 1399 | } |
| 1380 | 1400 | ||
| 1381 | flags = hci_adv_instance_flags(hdev, instance); | 1401 | 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 | |||
| 1386 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || | 1406 | connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || |
| 1387 | mgmt_get_connectable(hdev); | 1407 | mgmt_get_connectable(hdev); |
| 1388 | 1408 | ||
| 1389 | if (!is_advertising_allowed(hdev, connectable)) | 1409 | if (!is_advertising_allowed(hdev, connectable)) { |
| 1410 | if (instance) | ||
| 1411 | hci_dev_unlock(hdev); | ||
| 1390 | return -EPERM; | 1412 | return -EPERM; |
| 1413 | } | ||
| 1391 | 1414 | ||
| 1392 | /* Set require_privacy to true only when non-connectable | 1415 | /* Set require_privacy to true only when non-connectable |
| 1393 | * advertising is used and it is not periodic. | 1416 | * 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 | |||
| 1398 | err = hci_get_random_address(hdev, require_privacy, | 1421 | err = hci_get_random_address(hdev, require_privacy, |
| 1399 | adv_use_rpa(hdev, flags), adv, | 1422 | adv_use_rpa(hdev, flags), adv, |
| 1400 | &own_addr_type, &random_addr); | 1423 | &own_addr_type, &random_addr); |
| 1401 | if (err < 0) | 1424 | if (err < 0) { |
| 1425 | if (instance) | ||
| 1426 | hci_dev_unlock(hdev); | ||
| 1402 | return err; | 1427 | return err; |
| 1428 | } | ||
| 1403 | 1429 | ||
| 1404 | memset(&cp, 0, sizeof(cp)); | 1430 | memset(&cp, 0, sizeof(cp)); |
| 1405 | 1431 | ||
| @@ -1450,6 +1476,9 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 44 unmodified lines | |||
| 1450 | cp.channel_map = hdev->le_adv_channel_map; | 1476 | cp.channel_map = hdev->le_adv_channel_map; |
| 1451 | cp.handle = adv ? adv->handle : instance; | 1477 | cp.handle = adv ? adv->handle : instance; |
| 1452 | 1478 | ||
| 1479 | if (instance) | ||
| 1480 | hci_dev_unlock(hdev); | ||
| 1481 | |||
| 1453 | if (flags & MGMT_ADV_FLAG_SEC_2M) { | 1482 | if (flags & MGMT_ADV_FLAG_SEC_2M) { |
| 1454 | cp.primary_phy = HCI_ADV_PHY_1M; | 1483 | cp.primary_phy = HCI_ADV_PHY_1M; |
| 1455 | cp.secondary_phy = HCI_ADV_PHY_2M; | 1484 | 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 | |||
| 1462 | cp.secondary_phy = HCI_ADV_PHY_1M; | 1491 | cp.secondary_phy = HCI_ADV_PHY_1M; |
| 1463 | } | 1492 | } |
| 1464 | 1493 | ||
| 1465 | err = hci_set_ext_adv_params_sync(hdev, adv, &cp, &rp); | 1494 | err = hci_set_ext_adv_params_sync(hdev, instance, &cp, &rp); |
| 1466 | if (err) | 1495 | if (err) |
| 1467 | return err; | 1496 | return err; |
| 1468 | 1497 | ||
| 1469 | /* Update adv data as tx power is known now */ | 1498 | /* 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); |
| 1471 | if (err) | 1500 | if (err) |
| 1472 | return err; | 1501 | return err; |
| 1473 | 1502 | ||
| @@ -1475,9 +1504,14 @@ int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) · 1 unmodified line | |||
| 1475 | own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && | 1504 | own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && |
| 1476 | bacmp(&random_addr, BDADDR_ANY)) { | 1505 | bacmp(&random_addr, BDADDR_ANY)) { |
| 1477 | /* Check if random address need to be updated */ | 1506 | /* Check if random address need to be updated */ |
| 1478 | if (adv) { | 1507 | if (instance) { |
| 1479 | if (!bacmp(&random_addr, &adv->random_addr)) | 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); | ||
| 1480 | return 0; | 1512 | return 0; |
| 1513 | } | ||
| 1514 | hci_dev_unlock(hdev); | ||
| 1481 | } else { | 1515 | } else { |
| 1482 | if (!bacmp(&random_addr, &hdev->random_addr)) | 1516 | if (!bacmp(&random_addr, &hdev->random_addr)) |
| 1483 | return 0; | 1517 | return 0; |
| @@ -1499,9 +1533,13 @@ static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) · 15 unmodified lines | |||
| 1499 | int err; | 1533 | int err; |
| 1500 | 1534 | ||
| 1501 | if (instance) { | 1535 | if (instance) { |
| 1536 | hci_dev_lock(hdev); | ||
| 1537 | |||
| 1502 | adv = hci_find_adv_instance(hdev, instance); | 1538 | 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); | ||
| 1504 | return 0; | 1541 | return 0; |
| 1542 | } | ||
| 1505 | } | 1543 | } |
| 1506 | 1544 | ||
| 1507 | len = eir_create_scan_rsp(hdev, instance, pdu->data); | 1545 | 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 | |||
| 1511 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; | 1549 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; |
| 1512 | pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG; | 1550 | pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG; |
| 1513 | 1551 | ||
| 1552 | if (adv) { | ||
| 1553 | adv->scan_rsp_changed = false; | ||
| 1554 | hci_dev_unlock(hdev); | ||
| 1555 | } | ||
| 1556 | |||
| 1514 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, | 1557 | err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, |
| 1515 | struct_size(pdu, data, len), pdu, | 1558 | struct_size(pdu, data, len), pdu, |
| 1516 | HCI_CMD_TIMEOUT); | 1559 | 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 | |||
| 1518 | return err; | 1569 | return err; |
| 1570 | } | ||
| 1519 | 1571 | ||
| 1520 | if (adv) { | 1572 | if (!instance) { |
| 1521 | adv->scan_rsp_changed = false; | ||
| 1522 | } else { | ||
| 1523 | memcpy(hdev->scan_rsp_data, pdu->data, len); | 1573 | memcpy(hdev->scan_rsp_data, pdu->data, len); |
| 1524 | hdev->scan_rsp_data_len = len; | 1574 | hdev->scan_rsp_data_len = len; |
| 1525 | } | 1575 | } |
| @@ -1534,8 +1584,14 @@ static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) · 8 unmodified lines | |||
| 1534 | 1584 | ||
| 1535 | memset(&cp, 0, sizeof(cp)); | 1585 | memset(&cp, 0, sizeof(cp)); |
| 1536 | 1586 | ||
| 1587 | if (instance) | ||
| 1588 | hci_dev_lock(hdev); | ||
| 1589 | |||
| 1537 | len = eir_create_scan_rsp(hdev, instance, cp.data); | 1590 | len = eir_create_scan_rsp(hdev, instance, cp.data); |
| 1538 | 1591 | ||
| 1592 | if (instance) | ||
| 1593 | hci_dev_unlock(hdev); | ||
| 1594 | |||
| 1539 | if (hdev->scan_rsp_data_len == len && | 1595 | if (hdev->scan_rsp_data_len == len && |
| 1540 | !memcmp(cp.data, hdev->scan_rsp_data, len)) | 1596 | !memcmp(cp.data, hdev->scan_rsp_data, len)) |
| 1541 | return 0; | 1597 | return 0; |
| @@ -1670,9 +1726,13 @@ static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) · 128 unmodified lines | |||
| 1670 | struct adv_info *adv = NULL; | 1726 | struct adv_info *adv = NULL; |
| 1671 | 1727 | ||
| 1672 | if (instance) { | 1728 | if (instance) { |
| 1729 | hci_dev_lock(hdev); | ||
| 1730 | |||
| 1673 | adv = hci_find_adv_instance(hdev, instance); | 1731 | adv = hci_find_adv_instance(hdev, instance); |
| 1674 | if (!adv || !adv->periodic) | 1732 | if (!adv || !adv->periodic) { |
| 1733 | hci_dev_unlock(hdev); | ||
| 1675 | return 0; | 1734 | return 0; |
| 1735 | } | ||
| 1676 | } | 1736 | } |
| 1677 | 1737 | ||
| 1678 | len = eir_create_per_adv_data(hdev, instance, pdu->data); | 1738 | 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 | |||
| 1681 | pdu->handle = adv ? adv->handle : instance; | 1741 | pdu->handle = adv ? adv->handle : instance; |
| 1682 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; | 1742 | pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE; |
| 1683 | 1743 | ||
| 1744 | if (adv) | ||
| 1745 | hci_dev_unlock(hdev); | ||
| 1746 | |||
| 1684 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA, | 1747 | return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA, |
| 1685 | struct_size(pdu, data, len), pdu, | 1748 | struct_size(pdu, data, len), pdu, |
| 1686 | HCI_CMD_TIMEOUT); | 1749 | HCI_CMD_TIMEOUT); |
| @@ -6523,7 +6586,7 @@ static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev, · 4836 unmodified lines | |||
| 6523 | if (err) | 6586 | if (err) |
| 6524 | return err; | 6587 | return err; |
| 6525 | 6588 | ||
| 6526 | err = hci_set_ext_adv_params_sync(hdev, NULL, &cp, &rp); | 6589 | err = hci_set_ext_adv_params_sync(hdev, 0, &cp, &rp); |
| 6527 | if (err) | 6590 | if (err) |
| 6528 | return err; | 6591 | return err; |
| 6529 | 6592 | ||