!651 fix: 修复硬随机不可用时,地址随机化不可用问题

Merge pull request !651 from Harylee/shm
This commit is contained in:
openharmony_ci 2021-10-12 03:25:40 +00:00 committed by Gitee
commit 8096d9f575
1 changed files with 15 additions and 9 deletions

View File

@ -47,10 +47,12 @@
#include "tzdriver.h" #include "tzdriver.h"
#endif #endif
static int OsELFOpen(const CHAR *fileName, INT32 oflags) STATIC BOOL g_srandInit;
STATIC INT32 OsELFOpen(const CHAR *fileName, INT32 oflags)
{ {
int ret = -LOS_NOK; INT32 ret = -LOS_NOK;
int procFd; INT32 procFd;
procFd = AllocProcessFd(); procFd = AllocProcessFd();
if (procFd < 0) { if (procFd < 0) {
@ -71,11 +73,11 @@ static int OsELFOpen(const CHAR *fileName, INT32 oflags)
return procFd; return procFd;
} }
static int OsELFClose(int procFd) STATIC INT32 OsELFClose(INT32 procFd)
{ {
int ret; INT32 ret;
/* Process procfd convert to system global procfd */ /* Process procfd convert to system global procfd */
int sysfd = DisassociateProcessFd(procFd); INT32 sysfd = DisassociateProcessFd(procFd);
if (sysfd < 0) { if (sysfd < 0) {
return -EBADF; return -EBADF;
} }
@ -720,7 +722,7 @@ STATIC UINT32 OsGetRndOffset(const ELFLoadInfo *loadInfo)
if (read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32)) == sizeof(UINT32)) { if (read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32)) == sizeof(UINT32)) {
randomValue &= RANDOM_MASK; randomValue &= RANDOM_MASK;
} else { } else {
randomValue = 0; randomValue = (UINT32)random() & RANDOM_MASK;
} }
#else #else
(VOID)loadInfo; (VOID)loadInfo;
@ -791,7 +793,10 @@ STATIC INT32 OsSetArgParams(ELFLoadInfo *loadInfo, CHAR *const *argv, CHAR *cons
loadInfo->randomDevFD = open("/dev/urandom", O_RDONLY); loadInfo->randomDevFD = open("/dev/urandom", O_RDONLY);
if (loadInfo->randomDevFD < 0) { if (loadInfo->randomDevFD < 0) {
PRINT_ERR("%s: open /dev/urandom failed\n", __FUNCTION__); if (!g_srandInit) {
srand((UINT32)time(NULL));
g_srandInit = TRUE;
}
} }
(VOID)OsGetStackProt(loadInfo); (VOID)OsGetStackProt(loadInfo);
@ -879,7 +884,8 @@ STATIC INT32 OsGetRndNum(const ELFLoadInfo *loadInfo, UINT32 *rndVec, UINT32 vec
for (i = 0; i < vecSize; ++i) { for (i = 0; i < vecSize; ++i) {
ret = read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32)); ret = read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32));
if (ret != sizeof(UINT32)) { if (ret != sizeof(UINT32)) {
return -EIO; rndVec[i] = (UINT32)random();
continue;
} }
rndVec[i] = randomValue; rndVec[i] = randomValue;
} }