From 665c152c27bb86395ddd0395279255f6cdaf7255 Mon Sep 17 00:00:00 2001 From: Haryslee Date: Sat, 9 Oct 2021 16:10:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A1=AC=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E4=B8=8D=E5=8F=AF=E7=94=A8=E6=97=B6=EF=BC=8C=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E9=9A=8F=E6=9C=BA=E5=8C=96=E4=B8=8D=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 方案:硬随机不可用时,默认使用软随机数代替硬随机数 close #I4D4TK Signed-off-by: Haryslee Change-Id: Ia7d2a9583257d7b8041b8994a70a7c36149c33fb --- kernel/extended/dynload/src/los_load_elf.c | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/kernel/extended/dynload/src/los_load_elf.c b/kernel/extended/dynload/src/los_load_elf.c index 47e4e505..7d7a169a 100644 --- a/kernel/extended/dynload/src/los_load_elf.c +++ b/kernel/extended/dynload/src/los_load_elf.c @@ -47,10 +47,12 @@ #include "tzdriver.h" #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; - int procFd; + INT32 ret = -LOS_NOK; + INT32 procFd; procFd = AllocProcessFd(); if (procFd < 0) { @@ -71,11 +73,11 @@ static int OsELFOpen(const CHAR *fileName, INT32 oflags) return procFd; } -static int OsELFClose(int procFd) +STATIC INT32 OsELFClose(INT32 procFd) { - int ret; + INT32 ret; /* Process procfd convert to system global procfd */ - int sysfd = DisassociateProcessFd(procFd); + INT32 sysfd = DisassociateProcessFd(procFd); if (sysfd < 0) { return -EBADF; } @@ -720,7 +722,7 @@ STATIC UINT32 OsGetRndOffset(const ELFLoadInfo *loadInfo) if (read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32)) == sizeof(UINT32)) { randomValue &= RANDOM_MASK; } else { - randomValue = 0; + randomValue = (UINT32)random() & RANDOM_MASK; } #else (VOID)loadInfo; @@ -791,7 +793,10 @@ STATIC INT32 OsSetArgParams(ELFLoadInfo *loadInfo, CHAR *const *argv, CHAR *cons loadInfo->randomDevFD = open("/dev/urandom", O_RDONLY); 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); @@ -879,7 +884,8 @@ STATIC INT32 OsGetRndNum(const ELFLoadInfo *loadInfo, UINT32 *rndVec, UINT32 vec for (i = 0; i < vecSize; ++i) { ret = read(loadInfo->randomDevFD, &randomValue, sizeof(UINT32)); if (ret != sizeof(UINT32)) { - return -EIO; + rndVec[i] = (UINT32)random(); + continue; } rndVec[i] = randomValue; }