diff --git a/device/bluetooth/bluetooth_service_record_win.cc b/device/bluetooth/bluetooth_service_record_win.cc index 14936214a74c5..ed1dc6bea39ba 100644 --- a/device/bluetooth/bluetooth_service_record_win.cc +++ b/device/bluetooth/bluetooth_service_record_win.cc @@ -24,6 +24,15 @@ bool AdvanceToSdpType(const SDP_ELEMENT_DATA& sequence_data, SDP_TYPE type, HBLUETOOTH_CONTAINER_ELEMENT* element, SDP_ELEMENT_DATA* sdp_data) { + // `data` is a union: `data.sequence.{value,length}` are only valid when the + // remote peer actually sent a container type. A malicious peer can encode + // attributes 0x0001 / 0x0004 as e.g. UINT128, in which case the 16 attacker + // bytes overlay the LPBYTE/ULONG pair and the call below would dereference + // an attacker-controlled pointer. Reject anything that is not a container. + if (sequence_data.type != SDP_TYPE_SEQUENCE && + sequence_data.type != SDP_TYPE_ALTERNATIVE) { + return false; + } while (ERROR_SUCCESS == BluetoothSdpGetContainerElementData( sequence_data.data.sequence.value, sequence_data.data.sequence.length,