Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: https://github.com/bluez/bluez/issues/1117#issuecomment-2958244066
Fixes: 01ce70b0a274 ("Bluetooth: eir: Move EIR/Adv Data functions to its own file")
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

+8 -6
+4 -3
net/bluetooth/eir.c
··· 242 242 return ad_len; 243 243 } 244 244 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) 246 246 { 247 247 struct adv_info *adv = NULL; 248 248 u8 ad_len = 0, flags = 0; ··· 286 286 /* If flags would still be empty, then there is no need to 287 287 * include the "Flags" AD field". 288 288 */ 289 - if (flags) { 289 + if (flags && (ad_len + eir_precalc_len(1) <= size)) { 290 290 ptr[0] = 0x02; 291 291 ptr[1] = EIR_FLAGS; 292 292 ptr[2] = flags; ··· 316 316 } 317 317 318 318 /* 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)) { 320 321 ptr[0] = 0x02; 321 322 ptr[1] = EIR_TX_POWER; 322 323 ptr[2] = (u8)adv_tx_power;
+1 -1
net/bluetooth/eir.h
··· 9 9 10 10 void eir_create(struct hci_dev *hdev, u8 *data); 11 11 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); 13 13 u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr); 14 14 u8 eir_create_per_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr); 15 15
+3 -2
net/bluetooth/hci_sync.c
··· 1822 1822 return 0; 1823 1823 } 1824 1824 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); 1826 1827 1827 1828 pdu->length = len; 1828 1829 pdu->handle = adv ? adv->handle : instance; ··· 1854 1853 1855 1854 memset(&cp, 0, sizeof(cp)); 1856 1855 1857 - len = eir_create_adv_data(hdev, instance, cp.data); 1856 + len = eir_create_adv_data(hdev, instance, cp.data, sizeof(cp.data)); 1858 1857 1859 1858 /* There's nothing to do if the data hasn't changed */ 1860 1859 if (hdev->adv_data_len == len &&