From 763a5087d724096cb20ce94fab2dab92deb52aae Mon Sep 17 00:00:00 2001 From: Jacob Hageman Date: Thu, 11 Feb 2021 17:22:50 -0500 Subject: [PATCH] Fix #805, Null terminate when using strncpy --- src/os/shared/src/osapi-binsem.c | 2 +- src/os/shared/src/osapi-countsem.c | 2 +- src/os/shared/src/osapi-errors.c | 3 ++- src/os/shared/src/osapi-file.c | 2 +- src/os/shared/src/osapi-module.c | 6 +++--- src/os/shared/src/osapi-mutex.c | 2 +- src/os/shared/src/osapi-queue.c | 2 +- src/os/shared/src/osapi-sockets.c | 2 +- src/os/shared/src/osapi-time.c | 2 +- src/os/shared/src/osapi-timebase.c | 2 +- src/os/vxworks/src/os-impl-symtab.c | 3 ++- src/ut-stubs/osapi-utstub-binsem.c | 4 ++-- src/ut-stubs/osapi-utstub-countsem.c | 4 ++-- src/ut-stubs/osapi-utstub-filesys.c | 6 ++++-- src/ut-stubs/osapi-utstub-mutex.c | 4 ++-- src/ut-stubs/osapi-utstub-queue.c | 4 ++-- src/ut-stubs/osapi-utstub-sockets.c | 3 ++- src/ut-stubs/osapi-utstub-task.c | 4 ++-- src/ut-stubs/osapi-utstub-timebase.c | 4 ++-- 19 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/os/shared/src/osapi-binsem.c b/src/os/shared/src/osapi-binsem.c index 49510ae1c..d5aa62552 100644 --- a/src/os/shared/src/osapi-binsem.c +++ b/src/os/shared/src/osapi-binsem.c @@ -289,7 +289,7 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop) { record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token); - strncpy(bin_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1); bin_prop->creator = record->creator; return_code = OS_BinSemGetInfo_Impl(&token, bin_prop); diff --git a/src/os/shared/src/osapi-countsem.c b/src/os/shared/src/osapi-countsem.c index 9afcd2850..6cc98ed30 100644 --- a/src/os/shared/src/osapi-countsem.c +++ b/src/os/shared/src/osapi-countsem.c @@ -258,7 +258,7 @@ int32 OS_CountSemGetInfo(osal_id_t sem_id, OS_count_sem_prop_t *count_prop) { record = OS_OBJECT_TABLE_GET(OS_global_count_sem_table, token); - strncpy(count_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(count_prop->name, record->name_entry, sizeof(count_prop->name) - 1); count_prop->creator = record->creator; return_code = OS_CountSemGetInfo_Impl(&token, count_prop); diff --git a/src/os/shared/src/osapi-errors.c b/src/os/shared/src/osapi-errors.c index ddfe12f9b..199093254 100644 --- a/src/os/shared/src/osapi-errors.c +++ b/src/os/shared/src/osapi-errors.c @@ -120,7 +120,8 @@ int32 OS_GetErrorName(int32 error_num, os_err_name_t *err_name) if (Error->Number == error_num && Error->Name != NULL) { - strncpy(*err_name, Error->Name, OS_ERROR_NAME_LENGTH - 1); + strncpy(*err_name, Error->Name, sizeof(*err_name) - 1); + *err_name[sizeof(*err_name) - 1] = 0; return_code = OS_SUCCESS; } else diff --git a/src/os/shared/src/osapi-file.c b/src/os/shared/src/osapi-file.c index 1db28e3da..4de0b195c 100644 --- a/src/os/shared/src/osapi-file.c +++ b/src/os/shared/src/osapi-file.c @@ -605,7 +605,7 @@ int32 OS_FDGetInfo(osal_id_t filedes, OS_file_prop_t *fd_prop) { record = OS_OBJECT_TABLE_GET(OS_global_stream_table, token); - strncpy(fd_prop->Path, record->name_entry, OS_MAX_PATH_LEN - 1); + strncpy(fd_prop->Path, record->name_entry, sizeof(fd_prop->Path) - 1); fd_prop->User = record->creator; fd_prop->IsValid = true; diff --git a/src/os/shared/src/osapi-module.c b/src/os/shared/src/osapi-module.c index 362334115..dffe4482b 100644 --- a/src/os/shared/src/osapi-module.c +++ b/src/os/shared/src/osapi-module.c @@ -255,7 +255,7 @@ int32 OS_ModuleLoad(osal_id_t *module_id, const char *module_name, const char *f else { /* supplied filename was valid, so store a copy for future reference */ - strncpy(module->file_name, filename, OS_MAX_PATH_LEN); + strncpy(module->file_name, filename, sizeof(module->file_name) - 1); module->module_type = OS_MODULE_TYPE_DYNAMIC; /* Now call the OS-specific implementation. This reads info from the module table. */ @@ -333,8 +333,8 @@ int32 OS_ModuleInfo(osal_id_t module_id, OS_module_prop_t *module_prop) record = OS_OBJECT_TABLE_GET(OS_global_module_table, token); module = OS_OBJECT_TABLE_GET(OS_module_table, token); - strncpy(module_prop->name, record->name_entry, OS_MAX_API_NAME - 1); - strncpy(module_prop->filename, module->file_name, OS_MAX_API_NAME - 1); + strncpy(module_prop->name, record->name_entry, sizeof(module_prop->name) - 1); + strncpy(module_prop->filename, module->file_name, sizeof(module_prop->filename) - 1); return_code = OS_ModuleGetInfo_Impl(&token, module_prop); diff --git a/src/os/shared/src/osapi-mutex.c b/src/os/shared/src/osapi-mutex.c index 50d6a1fee..93cc0e92f 100644 --- a/src/os/shared/src/osapi-mutex.c +++ b/src/os/shared/src/osapi-mutex.c @@ -272,7 +272,7 @@ int32 OS_MutSemGetInfo(osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop) { record = OS_OBJECT_TABLE_GET(OS_global_mutex_table, token); - strncpy(mut_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(mut_prop->name, record->name_entry, sizeof(mut_prop->name) - 1); mut_prop->creator = record->creator; return_code = OS_MutSemGetInfo_Impl(&token, mut_prop); diff --git a/src/os/shared/src/osapi-queue.c b/src/os/shared/src/osapi-queue.c index cda46e141..0e03044b5 100644 --- a/src/os/shared/src/osapi-queue.c +++ b/src/os/shared/src/osapi-queue.c @@ -272,7 +272,7 @@ int32 OS_QueueGetInfo(osal_id_t queue_id, OS_queue_prop_t *queue_prop) { record = OS_OBJECT_TABLE_GET(OS_global_queue_table, token); - strncpy(queue_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(queue_prop->name, record->name_entry, sizeof(queue_prop->name) - 1); queue_prop->creator = record->creator; /* diff --git a/src/os/shared/src/osapi-sockets.c b/src/os/shared/src/osapi-sockets.c index f610b8660..7b276f6e0 100644 --- a/src/os/shared/src/osapi-sockets.c +++ b/src/os/shared/src/osapi-sockets.c @@ -473,7 +473,7 @@ int32 OS_SocketGetInfo(osal_id_t sock_id, OS_socket_prop_t *sock_prop) { record = OS_OBJECT_TABLE_GET(OS_global_stream_table, token); - strncpy(sock_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(sock_prop->name, record->name_entry, sizeof(sock_prop->name) - 1); sock_prop->creator = record->creator; return_code = OS_SocketGetInfo_Impl(&token, sock_prop); diff --git a/src/os/shared/src/osapi-time.c b/src/os/shared/src/osapi-time.c index 11e654caa..e830ae980 100644 --- a/src/os/shared/src/osapi-time.c +++ b/src/os/shared/src/osapi-time.c @@ -535,7 +535,7 @@ int32 OS_TimerGetInfo(osal_id_t timer_id, OS_timer_prop_t *timer_prop) timecb = OS_OBJECT_TABLE_GET(OS_timecb_table, token); timebase = OS_OBJECT_TABLE_GET(OS_timebase_table, timecb->timebase_token); - strncpy(timer_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(timer_prop->name, record->name_entry, sizeof(timer_prop->name) - 1); timer_prop->creator = record->creator; timer_prop->interval_time = (uint32)timecb->interval_time; timer_prop->accuracy = timebase->accuracy_usec; diff --git a/src/os/shared/src/osapi-timebase.c b/src/os/shared/src/osapi-timebase.c index affca25fb..661b9e46e 100644 --- a/src/os/shared/src/osapi-timebase.c +++ b/src/os/shared/src/osapi-timebase.c @@ -318,7 +318,7 @@ int32 OS_TimeBaseGetInfo(osal_id_t timebase_id, OS_timebase_prop_t *timebase_pro record = OS_OBJECT_TABLE_GET(OS_global_timebase_table, token); timebase = OS_OBJECT_TABLE_GET(OS_timebase_table, token); - strncpy(timebase_prop->name, record->name_entry, OS_MAX_API_NAME - 1); + strncpy(timebase_prop->name, record->name_entry, sizeof(timebase_prop->name) - 1); timebase_prop->creator = record->creator; timebase_prop->nominal_interval_time = timebase->nominal_interval_time; timebase_prop->freerun_time = timebase->freerun_time; diff --git a/src/os/vxworks/src/os-impl-symtab.c b/src/os/vxworks/src/os-impl-symtab.c index 5dc9f94ee..4694630b2 100644 --- a/src/os/vxworks/src/os-impl-symtab.c +++ b/src/os/vxworks/src/os-impl-symtab.c @@ -196,7 +196,8 @@ BOOL OS_SymTableIterator_Impl(char *name, SYM_VALUE val, SYM_TYPE type, _Vx_usr_ /* ** Copy symbol name */ - strncpy(symRecord.SymbolName, name, OS_MAX_SYM_LEN); + strncpy(symRecord.SymbolName, name, sizeof(symRecord.SymbolName) - 1); + symRecord.SymbolName[sizeof(symRecord.SymbolName) - 1] = 0; /* ** Save symbol address diff --git a/src/ut-stubs/osapi-utstub-binsem.c b/src/ut-stubs/osapi-utstub-binsem.c index e85d4559e..4d78f0069 100644 --- a/src/ut-stubs/osapi-utstub-binsem.c +++ b/src/ut-stubs/osapi-utstub-binsem.c @@ -197,8 +197,8 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop) UT_Stub_CopyToLocal(UT_KEY(OS_BinSemGetInfo), bin_prop, sizeof(*bin_prop)) < sizeof(*bin_prop)) { UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &bin_prop->creator); - strncpy(bin_prop->name, "Name", OS_MAX_API_NAME - 1); - bin_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(bin_prop->name, "Name", sizeof(bin_prop->name) - 1); + bin_prop->name[sizeof(bin_prop->name) - 1] = '\0'; } return status; diff --git a/src/ut-stubs/osapi-utstub-countsem.c b/src/ut-stubs/osapi-utstub-countsem.c index f4c4d3934..bccbe56e3 100644 --- a/src/ut-stubs/osapi-utstub-countsem.c +++ b/src/ut-stubs/osapi-utstub-countsem.c @@ -202,8 +202,8 @@ int32 OS_CountSemGetInfo(osal_id_t sem_id, OS_count_sem_prop_t *count_prop) UT_Stub_CopyToLocal(UT_KEY(OS_CountSemGetInfo), count_prop, sizeof(*count_prop)) < sizeof(*count_prop)) { UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &count_prop->creator); - strncpy(count_prop->name, "Name", OS_MAX_API_NAME - 1); - count_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(count_prop->name, "Name", sizeof(count_prop->name) - 1); + count_prop->name[sizeof(count_prop->name) - 1] = '\0'; } return status; diff --git a/src/ut-stubs/osapi-utstub-filesys.c b/src/ut-stubs/osapi-utstub-filesys.c index f0f4d6175..97634b489 100644 --- a/src/ut-stubs/osapi-utstub-filesys.c +++ b/src/ut-stubs/osapi-utstub-filesys.c @@ -248,7 +248,8 @@ int32 OS_FS_GetPhysDriveName(char *PhysDriveName, const char *MountPoint) int32 status; status = UT_DEFAULT_IMPL(OS_FS_GetPhysDriveName); - strncpy(PhysDriveName, MountPoint, OS_FS_PHYS_NAME_LEN); + strncpy(PhysDriveName, MountPoint, OS_FS_PHYS_NAME_LEN - 1); + PhysDriveName[OS_FS_PHYS_NAME_LEN - 1] = 0; return status; } @@ -287,7 +288,8 @@ int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath) if (status == OS_SUCCESS && VirtualPath != NULL && LocalPath != NULL && UT_Stub_CopyToLocal(UT_KEY(OS_TranslatePath), LocalPath, OS_MAX_LOCAL_PATH_LEN) == 0) { - strncpy(LocalPath, VirtualPath, OS_MAX_LOCAL_PATH_LEN); + strncpy(LocalPath, VirtualPath, OS_MAX_LOCAL_PATH_LEN - 1); + LocalPath[OS_MAX_LOCAL_PATH_LEN - 1] = 0; } return status; diff --git a/src/ut-stubs/osapi-utstub-mutex.c b/src/ut-stubs/osapi-utstub-mutex.c index baf060e93..7e09466db 100644 --- a/src/ut-stubs/osapi-utstub-mutex.c +++ b/src/ut-stubs/osapi-utstub-mutex.c @@ -229,8 +229,8 @@ int32 OS_MutSemGetInfo(osal_id_t sem_id, OS_mut_sem_prop_t *mut_prop) if (status == OS_SUCCESS && UT_Stub_CopyToLocal(UT_KEY(OS_MutSemGetInfo), mut_prop, sizeof(*mut_prop)) < sizeof(*mut_prop)) { - strncpy(mut_prop->name, "Name", OS_MAX_API_NAME - 1); - mut_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(mut_prop->name, "Name", sizeof(mut_prop->name) - 1); + mut_prop->name[sizeof(mut_prop->name) - 1] = '\0'; UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &mut_prop->creator); } diff --git a/src/ut-stubs/osapi-utstub-queue.c b/src/ut-stubs/osapi-utstub-queue.c index 77ed039ac..6bb396f00 100644 --- a/src/ut-stubs/osapi-utstub-queue.c +++ b/src/ut-stubs/osapi-utstub-queue.c @@ -270,8 +270,8 @@ int32 OS_QueueGetInfo(osal_id_t queue_id, OS_queue_prop_t *queue_prop) UT_Stub_CopyToLocal(UT_KEY(OS_QueueGetInfo), queue_prop, sizeof(*queue_prop)) < sizeof(*queue_prop)) { UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &queue_prop->creator); - strncpy(queue_prop->name, "Name", OS_MAX_API_NAME - 1); - queue_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(queue_prop->name, "Name", sizeof(queue_prop->name) - 1); + queue_prop->name[sizeof(queue_prop->name) - 1] = '\0'; } return status; diff --git a/src/ut-stubs/osapi-utstub-sockets.c b/src/ut-stubs/osapi-utstub-sockets.c index 3eb783d15..99024d5e9 100644 --- a/src/ut-stubs/osapi-utstub-sockets.c +++ b/src/ut-stubs/osapi-utstub-sockets.c @@ -237,7 +237,8 @@ int32 OS_SocketGetInfo(osal_id_t sock_id, OS_socket_prop_t *sock_prop) if (CopySize < sizeof(*sock_prop)) { UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &sock_prop->creator); - strncpy(sock_prop->name, "ut", sizeof(sock_prop->name)); + strncpy(sock_prop->name, "ut", sizeof(sock_prop->name) - 1); + sock_prop->name[sizeof(sock_prop->name) - 1] = 0; } } diff --git a/src/ut-stubs/osapi-utstub-task.c b/src/ut-stubs/osapi-utstub-task.c index f29990f8b..f15b77c1a 100644 --- a/src/ut-stubs/osapi-utstub-task.c +++ b/src/ut-stubs/osapi-utstub-task.c @@ -284,8 +284,8 @@ int32 OS_TaskGetInfo(osal_id_t task_id, OS_task_prop_t *task_prop) UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &task_prop->creator); task_prop->stack_size = OSAL_SIZE_C(100); task_prop->priority = OSAL_PRIORITY_C(150); - strncpy(task_prop->name, "UnitTest", OS_MAX_API_NAME - 1); - task_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(task_prop->name, "UnitTest", sizeof(task_prop->name) - 1); + task_prop->name[sizeof(task_prop->name) - 1] = '\0'; } return status; diff --git a/src/ut-stubs/osapi-utstub-timebase.c b/src/ut-stubs/osapi-utstub-timebase.c index 16b490479..da69b4774 100644 --- a/src/ut-stubs/osapi-utstub-timebase.c +++ b/src/ut-stubs/osapi-utstub-timebase.c @@ -144,8 +144,8 @@ int32 OS_TimeBaseGetInfo(osal_id_t timebase_id, OS_timebase_prop_t *timebase_pro UT_Stub_CopyToLocal(UT_KEY(OS_TimeBaseGetInfo), timebase_prop, sizeof(*timebase_prop)) < sizeof(*timebase_prop)) { UT_ObjIdCompose(1, UT_OBJTYPE_TASK, &timebase_prop->creator); - strncpy(timebase_prop->name, "Name", OS_MAX_API_NAME - 1); - timebase_prop->name[OS_MAX_API_NAME - 1] = '\0'; + strncpy(timebase_prop->name, "Name", sizeof(timebase_prop->name) - 1); + timebase_prop->name[sizeof(timebase_prop->name) - 1] = '\0'; } return status;