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

!1091 LMS特性安全合规整改

Merge pull request !1091 from JerryH/lms
......@@ -58,6 +58,10 @@ APP_SUBDIRS += mksh
APP_SUBDIRS += toybox
endif
ifeq ($(LOSCFG_KERNEL_LMS), y)
APP_SUBDIRS += lms
endif
ifeq ($(LOSCFG_USER_INIT_DEBUG), y)
APP_SUBDIRS += init
endif
......
......@@ -231,7 +231,7 @@ int main(int argc, char * const *argv)
printf("\n############### Lms Test start ###############\n");
char *tmp = (char *)malloc(5000); /* 5000: test mem size */
if (tmp == NULL) {
return;
return -1;
}
LmsMallocTest();
LmsReallocTest();
......@@ -246,4 +246,5 @@ int main(int argc, char * const *argv)
LmsFreeTest();
free(tmp);
printf("\n############### Lms Test End ###############\n");
return 0;
}
......@@ -837,6 +837,9 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
struct OsMemPoolHead *poolHead = (struct OsMemPoolHead *)pool;
struct OsMemNodeHead *newNode = NULL;
struct OsMemNodeHead *endNode = NULL;
(VOID)memset_s(poolHead, sizeof(struct OsMemPoolHead), 0, sizeof(struct OsMemPoolHead));
#ifdef LOSCFG_KERNEL_LMS
UINT32 resize = 0;
if (g_lms != NULL) {
......@@ -848,7 +851,6 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
size = (resize == 0) ? size : resize;
}
#endif
(VOID)memset(poolHead, 0, sizeof(struct OsMemPoolHead));
LOS_SpinInit(&poolHead->spinlock);
poolHead->info.pool = pool;
......@@ -885,9 +887,14 @@ STATIC UINT32 OsMemPoolInit(VOID *pool, UINT32 size)
}
#ifdef LOSCFG_MEM_MUL_POOL
STATIC VOID OsMemPoolDeinit(VOID *pool)
STATIC VOID OsMemPoolDeinit(const VOID *pool, UINT32 size)
{
(VOID)memset(pool, 0, sizeof(struct OsMemPoolHead));
#ifdef LOSCFG_KERNEL_LMS
if (g_lms != NULL) {
g_lms->deInit(pool);
}
#endif
(VOID)memset_s(pool, size, 0, sizeof(struct OsMemPoolHead));
}
STATIC UINT32 OsMemPoolAdd(VOID *pool, UINT32 size)
......@@ -961,7 +968,7 @@ UINT32 LOS_MemInit(VOID *pool, UINT32 size)
#ifdef LOSCFG_MEM_MUL_POOL
if (OsMemPoolAdd(pool, size)) {
(VOID)OsMemPoolDeinit(pool);
(VOID)OsMemPoolDeInit(pool, size);
return OS_ERROR;
}
#endif
......@@ -973,17 +980,21 @@ UINT32 LOS_MemInit(VOID *pool, UINT32 size)
#ifdef LOSCFG_MEM_MUL_POOL
UINT32 LOS_MemDeInit(VOID *pool)
{
if (pool == NULL) {
struct OsMemPoolHead *tmpPool = (struct OsMemPoolHead *)pool;
if ((tmpPool == NULL) ||
(tmpPool->info.pool != pool) ||
(tmpPool->info.totalSize <= OS_MEM_MIN_POOL_SIZE)) {
return OS_ERROR;
}
if (OsMemPoolDelete(pool)) {
if (OsMemPoolDelete(tmpPool)) {
return OS_ERROR;
}
OsMemPoolDeinit(pool);
OsMemPoolDeInit(tmpPool, tmpPool->info.totalSize);
OsHookCall(LOS_HOOK_TYPE_MEM_DEINIT, pool);
OsHookCall(LOS_HOOK_TYPE_MEM_DEINIT, tmpPool);
return LOS_OK;
}
......@@ -1896,7 +1907,7 @@ UINT32 LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)
return LOS_NOK;
}
(VOID)memset(poolStatus, 0, sizeof(LOS_MEM_POOL_STATUS));
(VOID)memset_s(poolStatus, sizeof(LOS_MEM_POOL_STATUS), 0, sizeof(LOS_MEM_POOL_STATUS));
struct OsMemNodeHead *tmpNode = NULL;
struct OsMemNodeHead *endNode = NULL;
......@@ -2070,5 +2081,3 @@ BOOL OsMemIsHeapNode(const VOID *ptr)
#endif
return FALSE;
}
......@@ -80,20 +80,25 @@ EXIT:
STATIC LmsMemListNode *OsLmsGetPoolNodeFromAddr(UINTPTR addr)
{
LmsMemListNode *current = NULL;
LmsMemListNode *previous = NULL;
LOS_DL_LIST *listHead = &g_lmsCheckPoolList;
if (LOS_ListEmpty(&g_lmsCheckPoolList)) {
goto EXIT;
return NULL;
}
LOS_DL_LIST_FOR_EACH_ENTRY(current, listHead, LmsMemListNode, node) {
if ((addr >= current->poolAddr) && (addr < current->poolAddr + current->poolSize)) {
return current;
if ((addr < current->poolAddr) || (addr >= (current->poolAddr + current->poolSize))) {
continue;
}
if ((previous == NULL) ||
((previous->poolAddr <= current->poolAddr) &&
((current->poolAddr + current->poolSize) <= (previous->poolAddr + previous->poolSize)))) {
previous = current;
}
}
EXIT:
return NULL;
return previous;
}
STATIC LmsMemListNode *OsLmsCheckPoolCreate(VOID)
......@@ -123,18 +128,12 @@ UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size)
LMS_LOCK(intSave);
lmsPoolNode = OsLmsGetPoolNodeFromAddr((UINTPTR)pool);
if (lmsPoolNode != NULL) { /* if pool range already on checklist */
if (lmsPoolNode->poolAddr != (UINTPTR)pool) { /* pool is a subset of lmsPoolNode->poolAddr */
/* do not add it again, just return */
PRINT_DEBUG("[LMS]pool %p already on lms checklist !\n", pool);
LMS_UNLOCK(intSave);
return size; /* return size indicate the shadow memory init successful */
} else { /* Re-initialize the same pool, maybe with different size */
/* delete the old node, then add a new one */
lmsPoolNode->used = LMS_POOL_UNUSED;
LOS_ListDelete(&(lmsPoolNode->node));
}
lmsPoolNode = OsLmsGetPoolNode(pool);
if (lmsPoolNode != NULL) { /* if pool already on checklist */
/* Re-initialize the same pool, maybe with different size */
/* delete the old node, then add a new one */
lmsPoolNode->used = LMS_POOL_UNUSED;
LOS_ListDelete(&(lmsPoolNode->node));
}
lmsPoolNode = OsLmsCheckPoolCreate();
......@@ -150,7 +149,8 @@ UINT32 LOS_LmsCheckPoolAdd(const VOID *pool, UINT32 size)
lmsPoolNode->shadowStart = (UINTPTR)poolAddr + realSize;
lmsPoolNode->shadowSize = poolAddr + size - lmsPoolNode->shadowStart;
/* init shadow value */
(VOID)memset((VOID *)lmsPoolNode->shadowStart, LMS_SHADOW_AFTERFREE_U8, lmsPoolNode->shadowSize);
(VOID)memset_s((VOID *)lmsPoolNode->shadowStart, lmsPoolNode->shadowSize,
LMS_SHADOW_AFTERFREE_U8, lmsPoolNode->shadowSize);
LOS_ListAdd(&g_lmsCheckPoolList, &(lmsPoolNode->node));
......@@ -179,10 +179,11 @@ Release:
STATIC UINT32 OsLmsInit(VOID)
{
(VOID)memset(g_lmsCheckPoolArray, 0, sizeof(g_lmsCheckPoolArray));
(VOID)memset_s(g_lmsCheckPoolArray, sizeof(g_lmsCheckPoolArray), 0, sizeof(g_lmsCheckPoolArray));
LOS_ListInit(&g_lmsCheckPoolList);
static LmsHook hook = {
.init = LOS_LmsCheckPoolAdd,
.deInit = LOS_LmsCheckPoolDel,
.mallocMark = OsLmsLosMallocMark,
.freeMark = OsLmsLosFreeMark,
.simpleMark = OsLmsSimpleMark,
......@@ -595,7 +596,7 @@ VOID OsLmsReportError(UINTPTR p, UINT32 size, UINT32 errMod)
(VOID)LOS_AtomicAdd(&g_checkDepth, 1);
LMS_LOCK(intSave);
(VOID)memset(&info, 0, sizeof(LmsAddrInfo));
(VOID)memset_s(&info, sizeof(LmsAddrInfo), 0, sizeof(LmsAddrInfo));
PRINT_ERR("***** Kernel Address Sanitizer Error Detected Start *****\n");
......@@ -766,4 +767,4 @@ VOID __asan_handle_no_return(VOID)
return;
}
LOS_MODULE_INIT(OsLmsInit, LOS_INIT_LEVEL_KMOD_PREVM);
\ No newline at end of file
LOS_MODULE_INIT(OsLmsInit, LOS_INIT_LEVEL_KMOD_PREVM);
......@@ -95,6 +95,7 @@ typedef struct {
typedef struct {
UINT32 (*init)(const VOID *pool, UINT32 size);
VOID (*deInit)(const VOID *pool);
VOID (*mallocMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize);
VOID (*freeMark)(const VOID *curNodeStart, const VOID *nextNodeStart, UINT32 nodeHeadSize);
VOID (*simpleMark)(UINTPTR startAddr, UINTPTR endAddr, UINT32 value);
......@@ -132,4 +133,4 @@ extern SANITIZER_INTERFACE_ATTRIBUTE VOID __asan_handle_no_return(VOID);
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif /* _LOS_LMS_PRI_H */
\ No newline at end of file
#endif /* _LOS_LMS_PRI_H */
......@@ -131,6 +131,10 @@ Lreturn:
pop {r4}
bx lr
Lfunc_end:
#if defined(LOSCFG_KERNEL_LMS)
.size __memset, Lfunc_end - __memset
#else
.size memset, Lfunc_end - memset
#endif
.cantunwind
.fnend @ -- End function
......@@ -139,7 +139,7 @@ SECTIONS
.ctors : ALIGN(0x4) {
__ctor_list__ = .;
KEEP (*(.ctors .init_array))
KEEP (*(.ctors .init_array.* .init_array))
__ctor_end__ = .;
} > ram
.dtors : ALIGN(0x4) {
......
......@@ -45,7 +45,7 @@ SECTIONS
.ctors : ALIGN(0x4) {
__ctor_list__ = .;
KEEP (*(.ctors .init_array))
KEEP (*(.ctors .init_array.* .init_array))
__ctor_end__ = .;
} > ram
.dtors : ALIGN(0x4) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册