@@ -224,27 +224,36 @@ IOReturn CIntelBTPatcher::newHostDeviceRequest(void *that, IOService *provider,
224224 if (getKernelVersion () >= KernelVersion::Sequoia)
225225 descriptor->complete (kIODirectionOut );
226226 }
227- hdr = (HciCommandHdr *)hciBuf;
228- if (getKernelVersion () < KernelVersion::Tahoe &&
227+ if (hdrLen >= sizeof (HciCommandHdr))
228+ hdr = reinterpret_cast <HciCommandHdr *>(hciBuf);
229+ if (hdr && getKernelVersion () < KernelVersion::Tahoe &&
230+ hdr->len == 7 && hdrLen >= sizeof (HciCommandHdr) + hdr->len &&
229231 hdr->opcode == HCI_OP_LE_SET_SCAN_PARAM && hdr->data [5 ] == 0x1 ) {
230232 if (!_randomAddressInit) {
231233 randomAddressRequest.bmRequestType = makeDeviceRequestbmRequestType (kRequestDirectionOut , kRequestTypeClass , kRequestRecipientInterface );
232234 randomAddressRequest.bRequest = 0xE0 ;
233235 randomAddressRequest.wIndex = 0 ;
234236 randomAddressRequest.wValue = 0 ;
235- randomAddressRequest.wLength = 9 ;
236- length = 9 ;
237+ randomAddressRequest.wLength = sizeof (randomAddressHci);
237238 if (writeHCIDescriptor == nullptr )
238- writeHCIDescriptor = IOBufferMemoryDescriptor::withBytes (randomAddressHci, 9 , kIODirectionInOut );
239- writeHCIDescriptor->prepare (kIODirectionInOut );
240- IOReturn ret = FunctionCast (newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest )(that, provider, randomAddressRequest, nullptr , writeHCIDescriptor, length, nullptr , timeout);
241- writeHCIDescriptor->complete (kIODirectionInOut );
239+ writeHCIDescriptor = IOBufferMemoryDescriptor::withBytes (randomAddressHci, sizeof (randomAddressHci), kIODirectionInOut );
240+ if (writeHCIDescriptor == nullptr ) {
241+ SYSLOG (DRV_NAME , " failed to allocate random address descriptor" );
242+ return FunctionCast (newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest )(that, provider, request, data, descriptor, length, completion, timeout);
243+ }
244+ unsigned int randomAddressLength = sizeof (randomAddressHci);
245+ IOReturn ret = writeHCIDescriptor->prepare (kIODirectionInOut );
246+ if (ret == kIOReturnSuccess ) {
247+ ret = FunctionCast (newHostDeviceRequest, callbackIBTPatcher->oldHostDeviceRequest )(that, provider, randomAddressRequest, nullptr , writeHCIDescriptor, randomAddressLength, nullptr , timeout);
248+ writeHCIDescriptor->complete (kIODirectionInOut );
249+ }
242250 const char *randAddressDump = _hexDumpHCIData ((uint8_t *)randomAddressHci, 9 );
243251 if (randAddressDump) {
244252 SYSLOG (DRV_NAME , " [PATCH] Sending Random Address HCI %d %s" , ret, randAddressDump);
245253 IOFree ((void *)randAddressDump, 9 * 3 + 1 );
246254 }
247- _randomAddressInit = true ;
255+ if (ret == kIOReturnSuccess )
256+ _randomAddressInit = true ;
248257 SYSLOG (DRV_NAME , " [PATCH] Resend LE SCAN PARAM HCI %d" , ret);
249258 }
250259 } else if (that == _hookDeviceInstance &&
@@ -259,9 +268,9 @@ IOReturn CIntelBTPatcher::newHostDeviceRequest(void *that, IOService *provider,
259268 cancelRemoteFeatures (handle);
260269 return result;
261270 }
262- } else {
263- hdr = ( HciCommandHdr *) data;
264- hdrLen = request.wLength - 3 ;
271+ } else if (request. wLength >= sizeof (HciCommandHdr)) {
272+ hdr = reinterpret_cast < HciCommandHdr *>( data) ;
273+ hdrLen = request.wLength ;
265274 }
266275 if (hdr) {
267276 // HCI reset, we need to send Random address again
@@ -429,24 +438,24 @@ void CIntelBTPatcher::captureIntelInterruptPipe(void *that, StandardUSB::Endpoin
429438 }
430439}
431440
432- int CIntelBTPatcher::newInitPipe (void *that, StandardUSB::EndpointDescriptor const *descriptor,
441+ bool CIntelBTPatcher::newInitPipe (void *that, StandardUSB::EndpointDescriptor const *descriptor,
433442 StandardUSB::SuperSpeedEndpointCompanionDescriptor const *superDescriptor,
434443 AppleUSBHostController *controller, IOUSBHostDevice *device,
435444 IOUSBHostInterface *interface, unsigned char a7, unsigned short a8)
436445{
437- int ret = FunctionCast (newInitPipe, callbackIBTPatcher->oldInitPipe )(that, descriptor, superDescriptor,
446+ bool ret = FunctionCast (newInitPipe, callbackIBTPatcher->oldInitPipe )(that, descriptor, superDescriptor,
438447 controller, device, interface, a7, a8);
439448 if (ret)
440449 captureIntelInterruptPipe (that, descriptor, device, interface);
441450 return ret;
442451}
443452
444- int CIntelBTPatcher::newInitPipeConfig (void *that, StandardUSB::EndpointDescriptor const *descriptor,
453+ bool CIntelBTPatcher::newInitPipeConfig (void *that, StandardUSB::EndpointDescriptor const *descriptor,
445454 StandardUSB::ConfigurationDescriptor const *configDescriptor,
446455 AppleUSBHostController *controller, IOUSBHostDevice *device,
447456 IOUSBHostInterface *interface, unsigned char a7, unsigned short a8)
448457{
449- int ret = FunctionCast (newInitPipeConfig, callbackIBTPatcher->oldInitPipeConfig )(that, descriptor,
458+ bool ret = FunctionCast (newInitPipeConfig, callbackIBTPatcher->oldInitPipeConfig )(that, descriptor,
450459 configDescriptor, controller,
451460 device, interface, a7, a8);
452461 if (ret)
0 commit comments