!641 feat: 支持AT_RANDOM以增强用户态栈保护能力
Merge pull request !641 from Harylee/shm
This commit is contained in:
commit
56a92f9207
|
@ -79,6 +79,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#define STACK_ALIGN_SIZE 0x10
|
||||
#define RANDOM_VECTOR_SIZE 1
|
||||
|
||||
/* The permissions on sections in the program header. */
|
||||
#define PF_R 0x4
|
||||
|
@ -99,23 +100,22 @@ typedef struct {
|
|||
CHAR *execName;
|
||||
INT32 argc;
|
||||
INT32 envc;
|
||||
CHAR *const *argv;
|
||||
CHAR *const *envp;
|
||||
CHAR * const *argv;
|
||||
CHAR * const *envp;
|
||||
UINTPTR stackTop;
|
||||
UINTPTR stackTopMax;
|
||||
UINTPTR stackBase;
|
||||
UINTPTR stackParamBase;
|
||||
UINT32 stackSize;
|
||||
INT32 stackProt;
|
||||
UINTPTR argStart;
|
||||
UINTPTR loadAddr;
|
||||
UINTPTR elfEntry;
|
||||
UINTPTR topOfMem;
|
||||
UINTPTR oldFiles;
|
||||
LosVmSpace *newSpace;
|
||||
LosVmSpace *oldSpace;
|
||||
#ifdef LOSCFG_ASLR
|
||||
INT32 randomDevFD;
|
||||
#endif
|
||||
} ELFLoadInfo;
|
||||
|
||||
STATIC INLINE BOOL OsIsBadUserAddress(VADDR_T vaddr)
|
||||
|
|
|
@ -789,12 +789,10 @@ STATIC INT32 OsSetArgParams(ELFLoadInfo *loadInfo, CHAR *const *argv, CHAR *cons
|
|||
UINT32 vmFlags;
|
||||
INT32 ret;
|
||||
|
||||
#ifdef LOSCFG_ASLR
|
||||
loadInfo->randomDevFD = open("/dev/urandom", O_RDONLY);
|
||||
if (loadInfo->randomDevFD < 0) {
|
||||
PRINT_ERR("%s: open /dev/urandom failed\n", __FUNCTION__);
|
||||
}
|
||||
#endif
|
||||
|
||||
(VOID)OsGetStackProt(loadInfo);
|
||||
if (((UINT32)loadInfo->stackProt & (PROT_READ | PROT_WRITE)) != (PROT_READ | PROT_WRITE)) {
|
||||
|
@ -831,13 +829,13 @@ STATIC INT32 OsSetArgParams(ELFLoadInfo *loadInfo, CHAR *const *argv, CHAR *cons
|
|||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
loadInfo->argStart = loadInfo->topOfMem;
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
STATIC INT32 OsPutParamToStack(ELFLoadInfo *loadInfo, const UINTPTR *auxVecInfo, INT32 vecIndex)
|
||||
{
|
||||
UINTPTR argStart = loadInfo->topOfMem;
|
||||
UINTPTR *topMem = (UINTPTR *)ROUNDDOWN(loadInfo->topOfMem, sizeof(UINTPTR));
|
||||
UINTPTR *argsPtr = NULL;
|
||||
INT32 items = (loadInfo->argc + 1) + (loadInfo->envc + 1) + 1;
|
||||
|
@ -858,8 +856,8 @@ STATIC INT32 OsPutParamToStack(ELFLoadInfo *loadInfo, const UINTPTR *auxVecInfo,
|
|||
|
||||
argsPtr++;
|
||||
|
||||
if ((OsPutUserArgv(&argStart, &argsPtr, loadInfo->argc) != LOS_OK) ||
|
||||
(OsPutUserArgv(&argStart, &argsPtr, loadInfo->envc) != LOS_OK)) {
|
||||
if ((OsPutUserArgv(&loadInfo->argStart, &argsPtr, loadInfo->argc) != LOS_OK) ||
|
||||
(OsPutUserArgv(&loadInfo->argStart, &argsPtr, loadInfo->envc) != LOS_OK)) {
|
||||
PRINT_ERR("%s[%d], Failed to put argv or envp to user stack!\n", __FUNCTION__, __LINE__);
|
||||
return -EFAULT;
|
||||
}
|
||||
|
@ -873,16 +871,46 @@ STATIC INT32 OsPutParamToStack(ELFLoadInfo *loadInfo, const UINTPTR *auxVecInfo,
|
|||
return LOS_OK;
|
||||
}
|
||||
|
||||
STATIC INT32 OsGetRndNum(const ELFLoadInfo *loadInfo, UINT32 *rndVec, UINT32 vecSize)
|
||||
{
|
||||
UINT32 randomValue = 0;
|
||||
UINT32 i, ret;
|
||||
|
||||
for (i = 0; i < vecSize; ++i) {
|
||||
ret = read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32));
|
||||
if (ret != sizeof(UINT32)) {
|
||||
return -EIO;
|
||||
}
|
||||
rndVec[i] = randomValue;
|
||||
}
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
STATIC INT32 OsMakeArgsStack(ELFLoadInfo *loadInfo, UINTPTR interpMapBase)
|
||||
{
|
||||
UINTPTR auxVector[AUX_VECTOR_SIZE] = { 0 };
|
||||
UINTPTR *auxVecInfo = (UINTPTR *)auxVector;
|
||||
INT32 vecIndex = 0;
|
||||
UINT32 rndVec[RANDOM_VECTOR_SIZE];
|
||||
UINTPTR rndVecStart;
|
||||
INT32 ret;
|
||||
#ifdef LOSCFG_KERNEL_VDSO
|
||||
vaddr_t vdsoLoadAddr;
|
||||
#endif
|
||||
|
||||
ret = OsGetRndNum(loadInfo, rndVec, sizeof(rndVec));
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
loadInfo->topOfMem -= sizeof(rndVec);
|
||||
rndVecStart = loadInfo->topOfMem;
|
||||
|
||||
ret = LOS_ArchCopyToUser((VOID *)loadInfo->topOfMem, rndVec, sizeof(rndVec));
|
||||
if (ret != 0) {
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_PHDR, loadInfo->loadAddr + loadInfo->execInfo.elfEhdr.elfPhoff);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_PHENT, sizeof(LD_ELF_PHDR));
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_PHNUM, loadInfo->execInfo.elfEhdr.elfPhNum);
|
||||
|
@ -897,7 +925,7 @@ STATIC INT32 OsMakeArgsStack(ELFLoadInfo *loadInfo, UINTPTR interpMapBase)
|
|||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_HWCAP, 0);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_CLKTCK, 0);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_SECURE, 0);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_RANDOM, 0);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_RANDOM, rndVecStart);
|
||||
AUX_VEC_ENTRY(auxVector, vecIndex, AUX_EXECFN, (UINTPTR)loadInfo->execName);
|
||||
|
||||
#ifdef LOSCFG_KERNEL_VDSO
|
||||
|
@ -988,9 +1016,7 @@ STATIC VOID OsFlushAspace(ELFLoadInfo *loadInfo)
|
|||
|
||||
STATIC VOID OsDeInitLoadInfo(ELFLoadInfo *loadInfo)
|
||||
{
|
||||
#ifdef LOSCFG_ASLR
|
||||
(VOID)close(loadInfo->randomDevFD);
|
||||
#endif
|
||||
|
||||
if (loadInfo->execInfo.elfPhdr != NULL) {
|
||||
(VOID)LOS_MemFree(m_aucSysMem0, loadInfo->execInfo.elfPhdr);
|
||||
|
|
Loading…
Reference in New Issue