未验证 提交 76366bc3 编写于 作者: O openharmony_ci 提交者: Gitee

!600 fix: posix线程和LOS_TaskCreate任务不兼容,补齐接口防护,防止访问野指针

Merge pull request !600 from zhushengle/cherry-pick-1645010283
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <unistd.h> #include <unistd.h>
#include <securec.h> #include <securec.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#include "los_config.h" #include "los_config.h"
#include "los_task.h" #include "los_task.h"
#include "los_debug.h" #include "los_debug.h"
...@@ -78,9 +79,17 @@ static void *PthreadEntry(UINT32 param) ...@@ -78,9 +79,17 @@ static void *PthreadEntry(UINT32 param)
return ret; return ret;
} }
static inline int IsPthread(pthread_t thread) static inline bool IsPthread(pthread_t thread)
{ {
return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT); LosTaskCB *tcb = NULL;
if ((UINT32)thread >= LOSCFG_BASE_CORE_TSK_LIMIT) {
return false;
}
tcb = OS_TCB_FROM_TID((UINT32)thread);
if ((UINTPTR)tcb->taskEntry != (UINTPTR)PthreadEntry) {
return false;
}
return true;
} }
static int PthreadCreateAttrInit(const pthread_attr_t *attr, void *(*startRoutine)(void *), void *arg, static int PthreadCreateAttrInit(const pthread_attr_t *attr, void *(*startRoutine)(void *), void *arg,
...@@ -148,8 +157,14 @@ static int CheckForCancel(void) ...@@ -148,8 +157,14 @@ static int CheckForCancel(void)
UINT32 intSave; UINT32 intSave;
LosTaskCB *tcb = NULL; LosTaskCB *tcb = NULL;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return 0;
}
tcb = OS_TCB_FROM_TID((UINT32)thread);
intSave = LOS_IntLock(); intSave = LOS_IntLock();
tcb = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
PthreadData *pthreadData = (PthreadData *)(UINTPTR)tcb->arg; PthreadData *pthreadData = (PthreadData *)(UINTPTR)tcb->arg;
if ((pthreadData->canceled) && (pthreadData->cancelState == PTHREAD_CANCEL_ENABLE)) { if ((pthreadData->canceled) && (pthreadData->cancelState == PTHREAD_CANCEL_ENABLE)) {
LOS_IntRestore(intSave); LOS_IntRestore(intSave);
...@@ -199,8 +214,13 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...@@ -199,8 +214,13 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param) int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
{ {
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if ((param == NULL) || (param->sched_priority < OS_TASK_PRIORITY_HIGHEST) || if ((param == NULL) || (param->sched_priority < OS_TASK_PRIORITY_HIGHEST) ||
(param->sched_priority >= OS_TASK_PRIORITY_LOWEST) || !IsPthread(thread)) { (param->sched_priority >= OS_TASK_PRIORITY_LOWEST)) {
return EINVAL; return EINVAL;
} }
...@@ -218,6 +238,11 @@ int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param ...@@ -218,6 +238,11 @@ int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param
int pthread_setschedprio(pthread_t thread, int prio) int pthread_setschedprio(pthread_t thread, int prio)
{ {
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if (LOS_TaskPriSet((UINT32)thread, (UINT16)prio) != LOS_OK) { if (LOS_TaskPriSet((UINT32)thread, (UINT16)prio) != LOS_OK) {
return EINVAL; return EINVAL;
} }
...@@ -230,6 +255,12 @@ int pthread_once(pthread_once_t *onceControl, void (*initRoutine)(void)) ...@@ -230,6 +255,12 @@ int pthread_once(pthread_once_t *onceControl, void (*initRoutine)(void))
UINT32 intSave; UINT32 intSave;
pthread_once_t old; pthread_once_t old;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if ((onceControl == NULL) || (initRoutine == NULL)) { if ((onceControl == NULL) || (initRoutine == NULL)) {
return EINVAL; return EINVAL;
} }
...@@ -255,12 +286,18 @@ int pthread_setcancelstate(int state, int *oldState) ...@@ -255,12 +286,18 @@ int pthread_setcancelstate(int state, int *oldState)
UINT32 intSave; UINT32 intSave;
LosTaskCB *tcb = NULL; LosTaskCB *tcb = NULL;
PthreadData *pthreadData = NULL; PthreadData *pthreadData = NULL;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if ((state != PTHREAD_CANCEL_ENABLE) && (state != PTHREAD_CANCEL_DISABLE)) { if ((state != PTHREAD_CANCEL_ENABLE) && (state != PTHREAD_CANCEL_DISABLE)) {
return EINVAL; return EINVAL;
} }
tcb = OS_TCB_FROM_TID((UINT32)thread);
intSave = LOS_IntLock(); intSave = LOS_IntLock();
tcb = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
pthreadData = (PthreadData *)(UINTPTR)tcb->arg; pthreadData = (PthreadData *)(UINTPTR)tcb->arg;
if (pthreadData == NULL) { if (pthreadData == NULL) {
LOS_IntRestore(intSave); LOS_IntRestore(intSave);
...@@ -282,12 +319,18 @@ int pthread_setcanceltype(int type, int *oldType) ...@@ -282,12 +319,18 @@ int pthread_setcanceltype(int type, int *oldType)
LosTaskCB *tcb = NULL; LosTaskCB *tcb = NULL;
PthreadData *pthreadData = NULL; PthreadData *pthreadData = NULL;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if ((type != PTHREAD_CANCEL_ASYNCHRONOUS) && (type != PTHREAD_CANCEL_DEFERRED)) { if ((type != PTHREAD_CANCEL_ASYNCHRONOUS) && (type != PTHREAD_CANCEL_DEFERRED)) {
return EINVAL; return EINVAL;
} }
tcb = OS_TCB_FROM_TID((UINT32)thread);
intSave = LOS_IntLock(); intSave = LOS_IntLock();
tcb = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
pthreadData = (PthreadData *)(UINTPTR)tcb->arg; pthreadData = (PthreadData *)(UINTPTR)tcb->arg;
if (pthreadData == NULL) { if (pthreadData == NULL) {
LOS_IntRestore(intSave); LOS_IntRestore(intSave);
...@@ -308,7 +351,12 @@ int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *par ...@@ -308,7 +351,12 @@ int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *par
{ {
UINT32 prio; UINT32 prio;
if ((policy == NULL) || (param == NULL) || !IsPthread(thread)) { if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if ((policy == NULL) || (param == NULL)) {
return EINVAL; return EINVAL;
} }
...@@ -355,6 +403,7 @@ int pthread_cancel(pthread_t thread) ...@@ -355,6 +403,7 @@ int pthread_cancel(pthread_t thread)
LosTaskCB *tcb = NULL; LosTaskCB *tcb = NULL;
PthreadData *pthreadData = NULL; PthreadData *pthreadData = NULL;
if (!IsPthread(thread)) { if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL; return EINVAL;
} }
intSave = LOS_IntLock(); intSave = LOS_IntLock();
...@@ -394,8 +443,13 @@ void pthread_testcancel(void) ...@@ -394,8 +443,13 @@ void pthread_testcancel(void)
int pthread_join(pthread_t thread, void **retval) int pthread_join(pthread_t thread, void **retval)
{ {
UINTPTR result; UINTPTR result;
UINT32 ret;
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
UINT32 ret = LOS_TaskJoin((UINT32)thread, &result); ret = LOS_TaskJoin((UINT32)thread, &result);
if (ret == LOS_ERRNO_TSK_NOT_JOIN_SELF) { if (ret == LOS_ERRNO_TSK_NOT_JOIN_SELF) {
return EDEADLK; return EDEADLK;
} else if ((ret == LOS_ERRNO_TSK_NOT_CREATED) || } else if ((ret == LOS_ERRNO_TSK_NOT_CREATED) ||
...@@ -416,7 +470,13 @@ int pthread_join(pthread_t thread, void **retval) ...@@ -416,7 +470,13 @@ int pthread_join(pthread_t thread, void **retval)
int pthread_detach(pthread_t thread) int pthread_detach(pthread_t thread)
{ {
UINT32 ret = LOS_TaskDetach((UINT32)thread); UINT32 ret;
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
ret = LOS_TaskDetach((UINT32)thread);
if (ret == LOS_ERRNO_TSK_NOT_JOIN) { if (ret == LOS_ERRNO_TSK_NOT_JOIN) {
return ESRCH; return ESRCH;
} else if (ret != LOS_OK) { } else if (ret != LOS_OK) {
...@@ -430,13 +490,19 @@ void pthread_exit(void *retVal) ...@@ -430,13 +490,19 @@ void pthread_exit(void *retVal)
{ {
UINT32 intSave; UINT32 intSave;
LosTaskCB *tcb = OS_TCB_FROM_TID(LOS_CurTaskIDGet()); pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
goto EXIT;
}
LosTaskCB *tcb = OS_TCB_FROM_TID((UINT32)thread);
tcb->joinRetval = (UINTPTR)retVal; tcb->joinRetval = (UINTPTR)retVal;
PthreadData *pthreadData = (PthreadData *)(UINTPTR)tcb->arg; PthreadData *pthreadData = (PthreadData *)(UINTPTR)tcb->arg;
if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0) { if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0) {
PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
} }
if (pthreadData->key != NULL) { if (pthreadData->key != NULL) {
PthreadExitKeyDtor(pthreadData); PthreadExitKeyDtor(pthreadData);
} }
...@@ -447,6 +513,7 @@ void pthread_exit(void *retVal) ...@@ -447,6 +513,7 @@ void pthread_exit(void *retVal)
LOS_IntRestore(intSave); LOS_IntRestore(intSave);
free(pthreadData); free(pthreadData);
(void)LOS_TaskDelete(tcb->taskID); (void)LOS_TaskDelete(tcb->taskID);
EXIT:
while (1) { while (1) {
} }
} }
...@@ -455,8 +522,15 @@ int pthread_setname_np(pthread_t thread, const char *name) ...@@ -455,8 +522,15 @@ int pthread_setname_np(pthread_t thread, const char *name)
{ {
UINT32 intSave; UINT32 intSave;
LosTaskCB *taskCB = NULL; LosTaskCB *taskCB = NULL;
char *taskName = LOS_TaskNameGet((UINT32)thread); char *taskName = NULL;
if (taskName == NULL || !IsPthread(thread)) {
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
taskName = LOS_TaskNameGet((UINT32)thread);
if (taskName == NULL) {
return EINVAL; return EINVAL;
} }
...@@ -485,9 +559,15 @@ int pthread_setname_np(pthread_t thread, const char *name) ...@@ -485,9 +559,15 @@ int pthread_setname_np(pthread_t thread, const char *name)
int pthread_getname_np(pthread_t thread, char *buf, size_t buflen) int pthread_getname_np(pthread_t thread, char *buf, size_t buflen)
{ {
int ret; int ret;
const char *name = NULL;
const char *name = LOS_TaskNameGet((UINT32)thread); if (!IsPthread(thread)) {
if (name == NULL || !IsPthread(thread)) { PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
name = LOS_TaskNameGet((UINT32)thread);
if (name == NULL) {
return EINVAL; return EINVAL;
} }
if (buflen > strlen(name)) { if (buflen > strlen(name)) {
...@@ -531,6 +611,12 @@ int pthread_key_create(pthread_key_t *k, void (*dtor)(void *)) ...@@ -531,6 +611,12 @@ int pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
unsigned int count = 0; unsigned int count = 0;
PthreadKey *keys = NULL; PthreadKey *keys = NULL;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if (k == NULL) { if (k == NULL) {
return EINVAL; return EINVAL;
} }
...@@ -563,6 +649,12 @@ int pthread_key_delete(pthread_key_t k) ...@@ -563,6 +649,12 @@ int pthread_key_delete(pthread_key_t k)
{ {
unsigned int intSave; unsigned int intSave;
pthread_t thread = pthread_self();
if (!IsPthread(thread)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, thread);
return EINVAL;
}
if (k >= PTHREAD_KEYS_MAX) { if (k >= PTHREAD_KEYS_MAX) {
return EINVAL; return EINVAL;
} }
...@@ -595,15 +687,16 @@ int pthread_key_delete(pthread_key_t k) ...@@ -595,15 +687,16 @@ int pthread_key_delete(pthread_key_t k)
int pthread_setspecific(pthread_key_t k, const void *x) int pthread_setspecific(pthread_key_t k, const void *x)
{ {
pthread_t self = pthread_self();
unsigned int intSave; unsigned int intSave;
uintptr_t *key = NULL; uintptr_t *key = NULL;
if (k >= PTHREAD_KEYS_MAX) { pthread_t self = pthread_self();
if (!IsPthread(self)) {
PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, self);
return EINVAL; return EINVAL;
} }
if (!IsPthread(self)) { if (k >= PTHREAD_KEYS_MAX) {
return EINVAL; return EINVAL;
} }
...@@ -639,12 +732,12 @@ void *pthread_getspecific(pthread_key_t k) ...@@ -639,12 +732,12 @@ void *pthread_getspecific(pthread_key_t k)
unsigned int intSave; unsigned int intSave;
void *key = NULL; void *key = NULL;
pthread_t self = pthread_self(); pthread_t self = pthread_self();
if (!IsPthread(self)) {
if (k >= PTHREAD_KEYS_MAX) { PRINT_ERR("[%s:%d] This task %d is not a posix thread!!!\n", __FUNCTION__, __LINE__, self);
return NULL; return NULL;
} }
if (!IsPthread(self)) { if (k >= PTHREAD_KEYS_MAX) {
return NULL; return NULL;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册