-
Notifications
You must be signed in to change notification settings - Fork 29
acdb: Fix memory-safety, locking and error-path bugs #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -434,6 +434,7 @@ void* ats_server_start_routine(void* arg) | |
| if (AR_FAILED(status)) | ||
| { | ||
| ATS_ERR("Error[%d]: Failed to create transmission thread #%d.\n", status, i); | ||
| ACDB_FREE(gateway_socket_ptr); | ||
| } | ||
| else | ||
| { | ||
|
Comment on lines
434
to
440
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] Medium Severity - Missing Resource Cleanup in Error Path In Issue: After accepting a connection (line 382), if Impact: Each failed thread creation leaks a socket file descriptor, eventually exhausting available file descriptors and preventing new connections. Fixed Code Snippetstatus = ar_osal_thread_create(&thd_ats_transmit, &thd_attr,
routine, gateway_socket_ptr);
if (AR_FAILED(status))
{
ATS_ERR("Error[%d]: Failed to create transmission thread #%d.\n", status, i);
atsclosesocket(*gateway_socket_ptr); // Close the socket before freeing
ACDB_FREE(gateway_socket_ptr);
}Severity: Medium - Causes resource leak that can lead to denial of service over time. |
||
|
|
@@ -452,7 +453,6 @@ void* ats_server_start_routine(void* arg) | |
| for (int thd = 0; thd < MAX_ATS_CLIENTS_ALLOWED; thd++) | ||
| { | ||
| ar_osal_thread_join_destroy(g_transmit_thread_holder[thd]); | ||
| ACDB_FREE(gateway_socket_ptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -301,7 +301,7 @@ int32_t TcpipCmdServer::set_connected_lock(uint8_t value) | |
|
|
||
| is_connected = value; | ||
|
|
||
| status = ar_osal_mutex_lock(connection_lock); | ||
| status = ar_osal_mutex_unlock(connection_lock); | ||
| if (AR_FAILED(status)) | ||
| { | ||
| return status; | ||
|
Comment on lines
301
to
307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] Medium Severity - Incorrect Mutex Operation (Lock Instead of Unlock) In both Issue: The function acquires the mutex at the beginning, modifies Impact: The mutex remains locked after the function completes, causing any subsequent attempt to acquire the same mutex to deadlock. This prevents proper connection state management. Fixed Code Snippetint32_t TcpipCmdServer::set_connected_lock(uint8_t value)
{
int32_t status = AR_EOK;
status = ar_osal_mutex_lock(connection_lock);
if (AR_FAILED(status))
{
return status;
}
is_connected = value;
status = ar_osal_mutex_unlock(connection_lock); // Fixed: was ar_osal_mutex_lock
if (AR_FAILED(status))
{
return status;
}
return status;
}Severity: Medium - Causes mutex deadlock in connection state management. |
||
|
|
@@ -464,19 +464,23 @@ void *TcpipCmdServer::transmit_routine(void *args) | |
| recieve_buffer.buffer_size = TCPIP_CMD_SERVER_RECV_BUFFER_SIZE; | ||
| //Stores one message | ||
| message_buffer.buffer = (char_t*)ar_heap_malloc(message_buffer.buffer_size, &heap_inf); | ||
| if (NULL == message_buffer.buffer) | ||
| if (NULL == message_buffer.buffer) { | ||
| ar_osal_mutex_unlock(connection_lock); | ||
| return 0; | ||
| } | ||
|
|
||
| recieve_buffer.buffer = (char_t*)ar_heap_malloc(recieve_buffer.buffer_size, &heap_inf); | ||
| if (NULL == recieve_buffer.buffer) { | ||
|
Comment on lines
464
to
473
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] High Severity - Missing Mutex Unlock in Error Paths In Issue: When Impact: After the first allocation failure, all subsequent connection attempts will hang indefinitely waiting for the mutex, effectively causing a denial of service. Fixed Code Snippetmessage_buffer.buffer = (char_t*)ar_heap_malloc(message_buffer.buffer_size, &heap_inf);
if (NULL == message_buffer.buffer) {
ar_osal_mutex_unlock(connection_lock);
return 0;
}
recieve_buffer.buffer = (char_t*)ar_heap_malloc(recieve_buffer.buffer_size, &heap_inf);
if (NULL == recieve_buffer.buffer) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}
outbuf = (char_t*)ar_heap_malloc(maxsize, &heap_inf);
if (NULL == outbuf) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_heap_free(recieve_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}Severity: High - Causes deadlock and denial of service. |
||
| ar_heap_free(message_buffer.buffer, &heap_inf); | ||
| ar_osal_mutex_unlock(connection_lock); | ||
| return 0; | ||
| } | ||
|
|
||
| outbuf = (char_t*)ar_heap_malloc(maxsize, &heap_inf); | ||
|
Comment on lines
469
to
479
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] High Severity - Missing Mutex Unlock in Error Paths In Issue: When Impact: After the first allocation failure, all subsequent connection attempts will hang indefinitely waiting for the mutex, effectively causing a denial of service. Fixed Code Snippetmessage_buffer.buffer = (char_t*)ar_heap_malloc(message_buffer.buffer_size, &heap_inf);
if (NULL == message_buffer.buffer) {
ar_osal_mutex_unlock(connection_lock);
return 0;
}
recieve_buffer.buffer = (char_t*)ar_heap_malloc(recieve_buffer.buffer_size, &heap_inf);
if (NULL == recieve_buffer.buffer) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}
outbuf = (char_t*)ar_heap_malloc(maxsize, &heap_inf);
if (NULL == outbuf) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_heap_free(recieve_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}Severity: High - Causes deadlock and denial of service. |
||
| if (NULL == outbuf) { | ||
| ar_heap_free(message_buffer.buffer, &heap_inf); | ||
| ar_heap_free(recieve_buffer.buffer, &heap_inf); | ||
| ar_osal_mutex_unlock(connection_lock); | ||
| return 0; | ||
| } | ||
| ar_mem_set(outbuf, 0, 1); | ||
|
Comment on lines
476
to
486
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] High Severity - Missing Mutex Unlock in Error Paths In Issue: When Impact: After the first allocation failure, all subsequent connection attempts will hang indefinitely waiting for the mutex, effectively causing a denial of service. Fixed Code Snippetmessage_buffer.buffer = (char_t*)ar_heap_malloc(message_buffer.buffer_size, &heap_inf);
if (NULL == message_buffer.buffer) {
ar_osal_mutex_unlock(connection_lock);
return 0;
}
recieve_buffer.buffer = (char_t*)ar_heap_malloc(recieve_buffer.buffer_size, &heap_inf);
if (NULL == recieve_buffer.buffer) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}
outbuf = (char_t*)ar_heap_malloc(maxsize, &heap_inf);
if (NULL == outbuf) {
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_heap_free(recieve_buffer.buffer, &heap_inf);
ar_osal_mutex_unlock(connection_lock);
return 0;
}Severity: High - Causes deadlock and denial of service. |
||
|
|
@@ -542,6 +546,7 @@ void *TcpipCmdServer::transmit_routine(void *args) | |
|
|
||
| ar_heap_free(message_buffer.buffer, &heap_inf); | ||
| ar_heap_free(recieve_buffer.buffer, &heap_inf); | ||
| ar_heap_free(outbuf, &heap_inf); | ||
|
|
||
| is_connected = false; | ||
| ar_osal_mutex_unlock(connection_lock); | ||
|
Comment on lines
546
to
552
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] High Severity - Missing Resource Cleanup on Thread Exit In Issue: The function allocates memory for Impact: Each client connection that completes normally leaks the memory allocated for Fixed Code Snippet//Cleanup
TCPIP_CMD_SVR_DBG("%s: closing thread", thd_name.c_str());
ar_heap_free(message_buffer.buffer, &heap_inf);
ar_heap_free(recieve_buffer.buffer, &heap_inf);
ar_heap_free(outbuf, &heap_inf); // Added: Free outbuf
is_connected = false;
ar_osal_mutex_unlock(connection_lock);
return 0;Severity: High - Causes memory leak on every client connection. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,7 +113,7 @@ int32_t TcpipDlsServer::set_connected_lock(uint8_t value) | |
|
|
||
| is_connected = value; | ||
|
|
||
| status = ar_osal_mutex_lock(connection_lock); | ||
| status = ar_osal_mutex_unlock(connection_lock); | ||
| if (AR_FAILED(status)) | ||
| { | ||
| return status; | ||
|
Comment on lines
113
to
119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] Medium Severity - Incorrect Mutex Operation (Lock Instead of Unlock) In both Issue: The function acquires the mutex at the beginning, modifies Impact: The mutex remains locked after the function completes, causing any subsequent attempt to acquire the same mutex to deadlock. This prevents proper connection state management. Fixed Code Snippetint32_t TcpipCmdServer::set_connected_lock(uint8_t value)
{
int32_t status = AR_EOK;
status = ar_osal_mutex_lock(connection_lock);
if (AR_FAILED(status))
{
return status;
}
is_connected = value;
status = ar_osal_mutex_unlock(connection_lock); // Fixed: was ar_osal_mutex_lock
if (AR_FAILED(status))
{
return status;
}
return status;
}Severity: Medium - Causes mutex deadlock in connection state management. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -647,6 +647,7 @@ int32_t AcdbFileManAddDatabase(acdb_file_man_data_files_t *db_files, | |
| { | ||
| ACDB_ERR("Error[%d]: The database file %s already exists", AR_EALREADY, | ||
| db_file->path); | ||
| ACDB_MUTEX_UNLOCK(acdb_file_man_context.file_man_lock); | ||
| return AR_EALREADY; | ||
| } | ||
| } | ||
|
Comment on lines
647
to
653
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [FUNCTIONALITY] Medium Severity - Missing Mutex Unlock in Error Path In Issue: When a duplicate database file is found, the function logs an error and returns Impact: After detecting a duplicate file, the mutex remains locked, causing any subsequent calls to Fixed Code Snippetif (NULL == ar_strstr(&fm_db_info->database_file.path[0], &db_file->path[0]))
continue;
else
{
ACDB_ERR("Error[%d]: The database file %s already exists", AR_EALREADY,
db_file->path);
ACDB_MUTEX_UNLOCK(acdb_file_man_context.file_man_lock);
return AR_EALREADY;
}Severity: Medium - Causes mutex deadlock when duplicate files are detected. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[SECURITY] High Severity - Buffer Overflow Prevention Added
The PR adds a critical bounds check in the
copy_frame_to_bufferfunction to prevent potential buffer overflow. The check verifies thatframe_ptr->header.frame_offsetis within the bounds ofbuf_cntxt_ptr->buffer_lengthbefore performing memory copy operations.Issue: Without this check, if a malformed frame with an offset beyond the buffer length is received, it could lead to writing beyond allocated memory, causing memory corruption or crashes.
Impact: This is a security vulnerability that could be exploited to cause denial of service or potentially execute arbitrary code.
Fixed Code Snippet
Severity: High - This prevents a potential buffer overflow vulnerability.