Skip to content

Commit 378f2f5

Browse files
committed
Fix null pointer dereferencing
Add null check for return values of functions that are mostly (but not always) checked for null. E.g., since 98% of calls to protocol_stack_interface_info_get_by_id check for null, it is likely that the function can return null values in some cases, and omitting the check could crash the program.
1 parent d0ca14e commit 378f2f5

File tree

11 files changed

+43
-8
lines changed

11 files changed

+43
-8
lines changed

connectivity/FEATURE_BLE/libraries/cordio_stack/ble-host/sources/stack/att/att_eatt.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static uint8_t eattL2cCocAcceptCback(dmConnId_t connId, uint8_t numChans)
267267
{
268268
eattConnCb_t *pCcb = eattGetConnCb(connId);
269269

270-
if ((pCcb->state == EATT_CONN_STATE_INITIATING) || (pCcb->state == EATT_CONN_STATE_RECONFIG))
270+
if (!pCcb || (pCcb->state == EATT_CONN_STATE_INITIATING) || (pCcb->state == EATT_CONN_STATE_RECONFIG))
271271
{
272272
// Reject all requests while busy connecting and configuring channels
273273
return 0;
@@ -348,6 +348,10 @@ static void eattReqNextChannels(dmConnId_t connId)
348348
eattConnCb_t *pConnCb = eattGetConnCb(connId);
349349
uint8_t numChans = pEattCfg->numChans - EattGetNumChannelsInUse(connId);
350350

351+
if (!pConnCb) {
352+
return;
353+
}
354+
351355
numChans = (numChans > L2C_MAX_EN_CHAN) ? L2C_MAX_EN_CHAN : numChans;
352356

353357
EATT_TRACE_INFO1("eattReqNextChannels: numChans: %d", numChans);
@@ -783,7 +787,7 @@ static void eattDmCback(dmEvt_t *pDmEvt)
783787
* \param connId DM channel ID.
784788
* \param slot EATT slot.
785789
*
786-
* \return None
790+
* \return L2CAP channel identifier.
787791
*/
788792
/*************************************************************************************************/
789793
uint16_t eattGetCid(dmConnId_t connId, uint8_t slot)
@@ -795,6 +799,7 @@ uint16_t eattGetCid(dmConnId_t connId, uint8_t slot)
795799
else
796800
{
797801
eattConnCb_t *pCcb = eattGetConnCb(connId);
802+
WSF_ASSERT(pCcb);
798803
return pCcb->pChanCb[slot-1].cid;
799804
}
800805
}

connectivity/FEATURE_BLE/source/generic/SecurityDb.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ void SecurityDb::get_entry_local_keys(
6363

6464
/* set flags connected */
6565
SecurityDistributionFlags_t* flags = get_distribution_flags(correct_handle);
66+
if (!flags) {
67+
cb(*db_handle, NULL);
68+
return;
69+
}
6670
flags->connected = true;
6771

6872
/* update peer address */
6973
SecurityDistributionFlags_t* old_flags = get_distribution_flags(*db_handle);
74+
if (!old_flags) {
75+
cb(*db_handle, NULL);
76+
return;
77+
}
7078
flags->peer_address = old_flags->peer_address;
7179
flags->peer_address_is_public = old_flags->peer_address_is_public;
7280

connectivity/FEATURE_BLE/source/generic/SecurityManagerImpl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,9 @@ void SecurityManager::on_connected(
16611661
cb->db_entry = _db->open_entry(peer_address_type, peer_address);
16621662

16631663
SecurityDistributionFlags_t* flags = _db->get_distribution_flags(cb->db_entry);
1664+
if (!flags) {
1665+
return;
1666+
}
16641667

16651668
flags->peer_address = peer_address;
16661669
flags->peer_address_is_public =

connectivity/nanostack/coap-service/source/coap_connection_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static int secure_session_recvfrom(int8_t socket_id, unsigned char *buf, size_t
480480
{
481481
(void)len;
482482
internal_socket_t *sock = int_socket_find_by_socket_id(socket_id);
483-
if (sock->data && sock->data_len > 0) {
483+
if (sock && sock->data && sock->data_len > 0) {
484484
memcpy(buf, sock->data, sock->data_len);
485485
int l = sock->data_len;
486486
ns_dyn_mem_free(sock->data);

connectivity/nanostack/mbed-mesh-api/source/thread_tasklet.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ void thread_tasklet_poll_network_status(void *param)
238238
} else {
239239
memcpy(thread_tasklet_data_ptr->ip, temp_ipv6, 16);
240240
link_configuration_s *link_cfg = thread_management_configuration_get(thread_tasklet_data_ptr->nwk_if_id);
241+
if (!link_cfg) {
242+
return;
243+
}
241244
if (memcmp(thread_tasklet_data_ptr->ip, link_cfg->mesh_local_ula_prefix, 8) == 0) {
242245
thread_tasklet_network_state_changed(MESH_CONNECTED_LOCAL);
243246
} else {

connectivity/nanostack/sal-stack-nanostack/source/6LoWPAN/Thread/thread_bbr_api.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ static void thread_bbr_status_check(thread_bbr_t *this, uint32_t seconds)
682682
}
683683
// Check if network data as border router is possible or modified
684684
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(this->interface_id);
685+
if (!cur) {
686+
return;
687+
}
685688
this->br_hosted = thread_bbr_i_host_prefix(cur, bbr_prefix_ptr, &this->br_count, &br_lowest_host);
686689

687690
if (!this->br_info_published && bbr_prefix_ptr && this->br_count == 0) {

connectivity/nanostack/sal-stack-nanostack/source/6LoWPAN/Thread/thread_bbr_commercial.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ static int thread_pbbr_bb_qry_cb(int8_t service_id, uint8_t source_address[16],
545545
// Test code for b/ba response override
546546
if (ba_response_status_count) {
547547
device_configuration_s *device_config = thread_joiner_application_get_device_config(this->interface_id);
548+
if (!device_config) {
549+
return -1;
550+
}
548551
ml_eid_ptr = device_config->eui64;
549552
last_transaction_time = protocol_core_monotonic_time;
550553
ba_response_status_count--;
@@ -617,7 +620,7 @@ static int thread_pbbr_dua_duplicate_address_detection(int8_t service_id, uint8_
617620
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(this->interface_id);
618621
duplicate_dua_tr_t *tr_ptr = thread_border_router_dup_tr_find(this->interface_id, addr_data_ptr);
619622

620-
if (!tr_ptr) {
623+
if (!cur || !tr_ptr) {
621624
return -1;
622625
}
623626

connectivity/nanostack/sal-stack-nanostack/source/6LoWPAN/Thread/thread_ccm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ static int thread_ccm_reenroll_registrar_addr_resp_cb(int8_t service_id, uint8_t
756756
return -1;
757757
}
758758

759+
if (!cur) {
760+
tr_debug("Protocol stack interface info get failed");
761+
return -1;
762+
}
763+
759764
if (!thread_meshcop_tlv_find(response_ptr->payload_ptr, response_ptr->payload_len, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS, &addr_ptr)) {
760765
tr_debug("Registrar addr get failed");
761766
return -1;

connectivity/nanostack/sal-stack-nanostack/source/6LoWPAN/Thread/thread_common.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,10 @@ void thread_child_id_request_info_init(thread_pending_child_id_req_t *child_info
751751
thread_pending_child_id_req_t *thread_child_id_request_allocate(void)
752752
{
753753
thread_pending_child_id_req_t *req = ns_dyn_mem_alloc(sizeof(thread_pending_child_id_req_t));
754-
memset(req->eiid, 0, 8);
755-
thread_child_id_request_info_init(req);
754+
if (req) {
755+
memset(req->eiid, 0, 8);
756+
thread_child_id_request_info_init(req);
757+
}
756758
return req;
757759
}
758760

connectivity/nanostack/sal-stack-nanostack/source/6LoWPAN/Thread/thread_joiner_application.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,9 @@ static void configuration_set_copy_mandatory(configuration_set_t *destination_pt
719719
static void configuration_set_generate(int8_t interface_id, configuration_set_t *destination_ptr, link_configuration_s *configuration_ptr)
720720
{
721721
uint8_t *response_ptr;
722+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
722723

723-
if (!destination_ptr || !configuration_ptr) {
724+
if (!destination_ptr || !configuration_ptr || !cur) {
724725
return;
725726
}
726727
response_ptr = destination_ptr->data;
@@ -739,7 +740,6 @@ static void configuration_set_generate(int8_t interface_id, configuration_set_t
739740
response_ptr = thread_tmfcop_tlv_data_write(response_ptr, MESHCOP_TLV_PSKC, 16, configuration_ptr->PSKc);
740741
response_ptr = thread_tmfcop_tlv_data_write(response_ptr, MESHCOP_TLV_NETWORK_NAME, stringlen((char *)&configuration_ptr->name, 16), configuration_ptr->name);
741742
*response_ptr++ = MESHCOP_TLV_SECURITY_POLICY; // type
742-
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
743743
if (thread_info(cur)->version >= THREAD_VERSION_1_2) {
744744
*response_ptr++ = 4; // length
745745
response_ptr = common_write_16_bit(configuration_ptr->key_rotation, response_ptr);

0 commit comments

Comments
 (0)