Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/btrCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,13 @@ enBTRCoreRet BTRCore_RegisterConnectionIntimationCb (tBTRCoreHandle hBTRCore, fP
/* BTRCore_RegisterConnectionAuthenticationCallback - callback for receiving a connection request from another device */
enBTRCoreRet BTRCore_RegisterConnectionAuthenticationCb (tBTRCoreHandle hBTRCore, fPtr_BTRCore_ConnAuthCb afpcBBTRCoreConnAuth, void* apUserData);

#ifdef UNIT_TEST
gint btrCore_AddAndGetCurrGenForTest(void);
gint btrCore_GetTerminatorForTest(void);
void btrCore_SetTerminatorForTest(void);
void btrCore_ResetTerminatorForTest(void);
#endif

/* @} */ //BLUETOOTH_APIS

#ifdef __cplusplus
Expand Down
55 changes: 52 additions & 3 deletions src/btrCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ int b_rdk_logger_enabled = 0;
#define BTRCORE_GOOGLE_OUI_LENGTH 8
#define BTCORE_DEFAULT_CONTROLLER_NAME "Game Controller"

/* Prevent UAF during teardown */
static volatile gint gIsBtrCoreTerminating = 0;

/* Track active instance generation - helps to check if current handle is same as that of terminated handle. */
static gint gBtrCoreGenerationCounter = 0;

static char * BTRCORE_REMOTE_OUI_VALUES[] = {
"20:44:41", //LC103
"E8:0F:C8", //EC302
Expand Down Expand Up @@ -214,6 +220,7 @@ typedef struct _stBTRCoreHdl {
GCond hidNameWaitCond;
BOOLEAN hidNameWaitInitialized;
stBTRCorePendingHidNameInfo stPendingHidNameInfo[BTRCORE_MAX_NUM_BT_DISCOVERED_DEVICES];
gint generation;
} stBTRCoreHdl;

typedef struct _stBTRCoreHidNameTimeoutData {
Expand Down Expand Up @@ -278,6 +285,27 @@ STATIC enBTRCoreRet btrCore_BTMediaStatusUpdateCb (stBTRCoreAVMediaStatusUpdate
#endif
STATIC enBTRCoreRet btrCore_BTLeStatusUpdateCb (stBTRCoreLeGattInfo* apstBtrLeInfo, const char* apcBtdevAddr, void* apvUserData);

#ifdef UNIT_TEST
gint btrCore_AddAndGetCurrGenForTest(void) {
/* Return the incremented (current) generation */
return g_atomic_int_add(&gBtrCoreGenerationCounter, 1) + 1;
}

gint btrCore_GetTerminatorForTest(void) {
return g_atomic_int_get(&gIsBtrCoreTerminating);
}

void btrCore_ResetTerminatorForTest(void) {
g_atomic_int_set(&gIsBtrCoreTerminating, 0);
gint val = g_atomic_int_get(&gIsBtrCoreTerminating);
}
Comment on lines +298 to +301

void btrCore_SetTerminatorForTest(void) {
g_atomic_int_set(&gIsBtrCoreTerminating, 1);
gint val = g_atomic_int_get(&gIsBtrCoreTerminating);
}
Comment on lines +303 to +306
#endif

/* Static Function Definition */
static void
btrCore_InitDataSt (
Expand Down Expand Up @@ -1189,8 +1217,8 @@ btrCore_AddDeviceToScannedDevicesArr (

if(0 != apstBTDeviceInfo->saServices[count].len)
{
lstFoundDevice.stAdServiceData[count].len = apstBTDeviceInfo->saServices[count].len;
MEMCPY_S(lstFoundDevice.stAdServiceData[count].pcData,lstFoundDevice.stAdServiceData[count].len, apstBTDeviceInfo->saServices[count].pcData, BTRCORE_MAX_SERVICE_DATA_LEN);
lstFoundDevice.stAdServiceData[count].len = (apstBTDeviceInfo->saServices[count].len < BTRCORE_MAX_SERVICE_DATA_LEN) ? apstBTDeviceInfo->saServices[count].len : BTRCORE_MAX_SERVICE_DATA_LEN;
MEMCPY_S(lstFoundDevice.stAdServiceData[count].pcData, BTRCORE_MAX_SERVICE_DATA_LEN, apstBTDeviceInfo->saServices[count].pcData, lstFoundDevice.stAdServiceData[count].len);

BTRCORELOG_TRACE ("ServiceData from %s\n", __FUNCTION__);
for (int i =0; i < apstBTDeviceInfo->saServices[count].len; i++){
Expand Down Expand Up @@ -1482,9 +1510,21 @@ btrCore_PopulateListOfPairedDevices (
stBTPairedDeviceInfo* pstBTPairedDeviceInfo = NULL;
stBTRCoreBTDevice knownDevicesArr[BTRCORE_MAX_NUM_BT_DEVICES];

if (!apsthBTRCore) {
BTRCORELOG_WARN("apsthBTRCore is null\n");
return enBTRCoreNotInitialized;
}

/* Prevent UAF when worker threads run during teardown */
if(g_atomic_int_get(&gIsBtrCoreTerminating)) {
BTRCORELOG_WARN("btrCore: Ignoring PopulateListOfPairedDevices during termination\n");
return enBTRCoreFailure;
}
Comment on lines +1518 to +1522

if ((pstBTPairedDeviceInfo = g_malloc0(sizeof(stBTPairedDeviceInfo))) == NULL)
if ((pstBTPairedDeviceInfo = g_malloc0(sizeof(stBTPairedDeviceInfo))) == NULL) {
BTRCORELOG_WARN("btrCore: gmalloc0 failed\n");
return enBTRCoreFailure;
}


pstBTPairedDeviceInfo->numberOfDevices = 0;
Expand Down Expand Up @@ -3524,6 +3564,10 @@ BTRCore_Init (
}
MEMSET_S(pstlhBTRCore, sizeof(stBTRCoreHdl), 0, sizeof(stBTRCoreHdl));

/* Assign a new generation for this instance */
pstlhBTRCore->generation = g_atomic_int_add(&gBtrCoreGenerationCounter, 1)+1;

g_atomic_int_set(&gIsBtrCoreTerminating, 0);

pstlhBTRCore->connHdl = BtrCore_BTInitGetConnection();
if (!pstlhBTRCore->connHdl) {
Expand Down Expand Up @@ -3688,6 +3732,11 @@ BTRCore_DeInit (

BTRCORELOG_INFO ("hBTRCore = %8p\n", hBTRCore);

/* Only end global teardown if this is the active generation */
if (pstlhBTRCore->generation == g_atomic_int_get(&gBtrCoreGenerationCounter)) {
g_atomic_int_set(&gIsBtrCoreTerminating, 1);
}

if (pstlhBTRCore->hidNameWaitInitialized) {
GThread* lapPendingThreads[BTRCORE_MAX_NUM_BT_DISCOVERED_DEVICES];
int liNumPendingThreads = 0;
Expand Down
Loading
Loading