Skip to content

Commit 47c0390

Browse files
committed
Bluetooth: eir: Fix possible crashes on eir_create_adv_data
eir_create_adv_data may attempt to add EIR_FLAGS and EIR_TX_POWER without checking if that would fit. Link: bluez/bluez#1117 (comment) Fixes: 01ce70b ("Bluetooth: eir: Move EIR/Adv Data functions to its own file") Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 5725bc6 commit 47c0390

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

net/bluetooth/eir.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ u8 eir_create_per_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
242242
return ad_len;
243243
}
244244

245-
u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
245+
u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr, u8 size)
246246
{
247247
struct adv_info *adv = NULL;
248248
u8 ad_len = 0, flags = 0;
@@ -286,7 +286,7 @@ u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
286286
/* If flags would still be empty, then there is no need to
287287
* include the "Flags" AD field".
288288
*/
289-
if (flags) {
289+
if (flags && (ad_len + eir_precalc_len(1) <= size)) {
290290
ptr[0] = 0x02;
291291
ptr[1] = EIR_FLAGS;
292292
ptr[2] = flags;
@@ -316,7 +316,8 @@ u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
316316
}
317317

318318
/* Provide Tx Power only if we can provide a valid value for it */
319-
if (adv_tx_power != HCI_TX_POWER_INVALID) {
319+
if (adv_tx_power != HCI_TX_POWER_INVALID &&
320+
(ad_len + eir_precalc_len(1) <= size)) {
320321
ptr[0] = 0x02;
321322
ptr[1] = EIR_TX_POWER;
322323
ptr[2] = (u8)adv_tx_power;

net/bluetooth/eir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
void eir_create(struct hci_dev *hdev, u8 *data);
1111

12-
u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr);
12+
u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr, u8 size);
1313
u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr);
1414
u8 eir_create_per_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr);
1515

net/bluetooth/hci_sync.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,8 @@ static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
18221822
return 0;
18231823
}
18241824

1825-
len = eir_create_adv_data(hdev, instance, pdu->data);
1825+
len = eir_create_adv_data(hdev, instance, pdu->data,
1826+
HCI_MAX_EXT_AD_LENGTH);
18261827

18271828
pdu->length = len;
18281829
pdu->handle = adv ? adv->handle : instance;
@@ -1853,7 +1854,7 @@ static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
18531854

18541855
memset(&cp, 0, sizeof(cp));
18551856

1856-
len = eir_create_adv_data(hdev, instance, cp.data);
1857+
len = eir_create_adv_data(hdev, instance, cp.data, sizeof(cp.data));
18571858

18581859
/* There's nothing to do if the data hasn't changed */
18591860
if (hdev->adv_data_len == len &&

0 commit comments

Comments
 (0)