You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The BLE Cordio implementation in Mbed OS utilizes the hciTrSerialRxIncoming function to manage incoming HCI data. However, I have identified and verified a potential issue that could lead to a buffer overflow in hdrRx if packet types are excluded from the valid ones.
To elaborate, when an invalid packet type is encountered, thehdrlen (line 36) remain the inital value (i.e.,0) but the iRxhas been increased (Line 19). Consequently, the condition in Line 41 is not satisfied and the stateRx variable remains in the HCI_RX_STATE_HEADER state. This, in turn, allows incoming data to continuously accumulate in the hdrRx buffer in while loop execution, as shown in line 19. However, it's important to note that the hdrRx's size is constrained by the HCI_ACL_HDR_LEN macro, which is set to a mere 4 bytes. This causes a vulnerability to buffer overflow.
voidhciTrSerialRxIncoming(uint8_t*pBuf, uint8_tlen)
{
......
staticuint8_thdrRx[HCI_ACL_HDR_LEN];
......
/* loop until all bytes of incoming buffer are handled */while (len--)
{
/* read single byte from incoming buffer and advance to next byte */dataByte=*pBuf++;
......
/* --- Header State --- */elseif (stateRx==HCI_RX_STATE_HEADER)
{
uint8_thdrLen=0;
uint16_tdataLen=0;
/* copy current byte into the temp header buffer */hdrRx[iRx++] =dataByte; /*vulnerbility occured: the buffer overflow problem occured here*//* determine header length based on packet type */// pkIndRx is the first byteswitch (pktIndRx)
{
caseHCI_CMD_TYPE:
hdrLen=HCI_CMD_HDR_LEN;
break;
caseHCI_ACL_TYPE:
hdrLen=HCI_ACL_HDR_LEN;
break;
caseHCI_EVT_TYPE:
hdrLen=HCI_EVT_HDR_LEN;
break;
default:
/* invalid packet type */WSF_ASSERT(0);
break;
}
......
/* see if entire header has been read */if (iRx==hdrLen)
{
/* extract data length from header */switch (pktIndRx)
{
caseHCI_CMD_TYPE:
dataLen=hdrRx[2];
break;
caseHCI_ACL_TYPE:
BYTES_TO_UINT16(dataLen, &hdrRx[2]);
break;
caseHCI_EVT_TYPE:
dataLen=hdrRx[1];
break;
default:
break;
}
/* allocate data buffer to hold entire packet */if (pktIndRx==HCI_ACL_TYPE)
{
pPktRx= (uint8_t*)WsfMsgDataAlloc(hdrLen+dataLen, 0);
}
else
{
pPktRx= (uint8_t*)WsfMsgAlloc(hdrLen+dataLen);
}
if (pPktRx!=NULL)
{
pDataRx=pPktRx;
/* copy header into data packet (note: memcpy is not so portable) */
{
uint8_ti;
for (i=0; i<hdrLen; i++)
{
*pDataRx++=hdrRx[i];
}
}
/* save number of bytes left to read */iRx=dataLen;
if (iRx==0)
{
stateRx=HCI_RX_STATE_COMPLETE;
}
else
{
stateRx=HCI_RX_STATE_DATA;
}
}
else
{
WSF_ASSERT(0); /* allocate falied */
}
}
}
In addition, note that WSF_ASSERT is turned off by default. However, even if the WSF_ASSERT is turn on the execution will be simple return or directly hang which depends on how the mbed_error function works, as shown its following defination.
Description of defect
Reference: https://github.com/ARMmbed/mbed-os/blob/master/connectivity/FEATURE_BLE/source/cordio/stack_adaptation/hci_tr.c
Function: hciTrSerialRxIncoming
From: mbed-os/blob/master/connectivity/FEATURE_BLE/source/cordio/stack_adaptation/hci_tr.c Line: 125
mbed-os/connectivity/FEATURE_BLE/source/cordio/stack_adaptation/hci_tr.c
Line 125 in 7c7d20d
Type: Buffer overflow
The BLE Cordio implementation in Mbed OS utilizes the
hciTrSerialRxIncoming
function to manage incoming HCI data. However, I have identified and verified a potential issue that could lead to a buffer overflow inhdrRx
if packet types are excluded from the valid ones.To elaborate, when an invalid packet type is encountered, the
hdrlen
(line 36) remain the inital value (i.e.,0) but theiRx
has been increased (Line 19). Consequently, the condition in Line 41 is not satisfied and thestateRx
variable remains in theHCI_RX_STATE_HEADER
state. This, in turn, allows incoming data to continuously accumulate in the hdrRx buffer inwhile
loop execution, as shown in line 19. However, it's important to note that thehdrRx
's size is constrained by theHCI_ACL_HDR_LEN
macro, which is set to a mere 4 bytes. This causes a vulnerability to buffer overflow.In addition, note that
WSF_ASSERT
is turned off by default. However, even if theWSF_ASSERT
is turn on the execution will be simple return or directly hang which depends on how the mbed_error function works, as shown its following defination.Target(s) affected by this defect ?
MbedOS BLE Cordio stack
Toolchain(s) (name and version) displaying this defect ?
N/A
What version of Mbed-os are you using (tag or sha) ?
mbed-os-6.17.0 (the latest version)
What version(s) of tools are you using. List all that apply (E.g. mbed-cli)
mbed-cli2
How is this defect reproduced ?
Send problematic HCI protocol packets to the target demo board using the Cordio protocol stack.
The text was updated successfully, but these errors were encountered: