From 52be80a966bb53975a67cc88a696680726e67606 Mon Sep 17 00:00:00 2001 From: Zhou Shihui Date: Thu, 7 Nov 2024 15:03:40 +0800 Subject: [PATCH] =?UTF-8?q?cherry=20pick=20f77a844=20from=20https://gitee.?= =?UTF-8?q?com/ImCaO/security=5Fappverify/pulls/242=20HapVerify=E6=8F=90?= =?UTF-8?q?=E4=BE=9Bread=E4=BB=A3=E6=9B=BFmmap=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Zhou Shihui --- .../include/common/random_access_file.h | 4 ++-- .../appverify/include/interfaces/hap_verify.h | 2 +- .../appverify/include/verify/hap_verify_v2.h | 2 +- .../src/common/random_access_file.cpp | 19 ++++++++++++------- .../appverify/src/interfaces/hap_verify.cpp | 4 ++-- .../appverify/src/verify/hap_verify_v2.cpp | 4 ++-- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/interfaces/innerkits/appverify/include/common/random_access_file.h b/interfaces/innerkits/appverify/include/common/random_access_file.h index e58af92..3e7c799 100644 --- a/interfaces/innerkits/appverify/include/common/random_access_file.h +++ b/interfaces/innerkits/appverify/include/common/random_access_file.h @@ -33,7 +33,7 @@ class RandomAccessFile { public: DLL_EXPORT RandomAccessFile(); DLL_EXPORT ~RandomAccessFile(); - DLL_EXPORT bool Init(const std::string& filePath); + DLL_EXPORT bool Init(const std::string& filePath, bool readFile = false); DLL_EXPORT bool InitWithFd(const int32_t fileFd); DLL_EXPORT long long GetLength() const; DLL_EXPORT long long ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset); @@ -50,7 +50,7 @@ private: static int32_t memoryPageSize; int32_t fd = 0; long long fileLength; - bool readFile = false; + bool readFile_ = false; }; } // namespace Verify } // namespace Security diff --git a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h index 2c85893..9197675 100644 --- a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h +++ b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h @@ -26,7 +26,7 @@ namespace Security { namespace Verify { DLL_EXPORT bool EnableDebugMode(); DLL_EXPORT void DisableDebugMode(); -DLL_EXPORT int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult); +DLL_EXPORT int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult, bool readFile = false); DLL_EXPORT int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); DLL_EXPORT int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); extern "C" DLL_EXPORT int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, diff --git a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h index c0ced01..c2aabbb 100644 --- a/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h +++ b/interfaces/innerkits/appverify/include/verify/hap_verify_v2.h @@ -29,7 +29,7 @@ namespace Security { namespace Verify { class HapVerifyV2 { public: - int32_t Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); + int32_t Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result, bool readFile = false); int32_t Verify(const int32_t fileFd, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result); int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureInfo &hapSignInfo); diff --git a/interfaces/innerkits/appverify/src/common/random_access_file.cpp b/interfaces/innerkits/appverify/src/common/random_access_file.cpp index 6418e03..d654f66 100644 --- a/interfaces/innerkits/appverify/src/common/random_access_file.cpp +++ b/interfaces/innerkits/appverify/src/common/random_access_file.cpp @@ -43,7 +43,7 @@ RandomAccessFile::~RandomAccessFile() } } -bool RandomAccessFile::Init(const std::string& filePath) +bool RandomAccessFile::Init(const std::string& filePath, bool readFile) { fd = open(filePath.c_str(), O_RDONLY); if (fd == FILE_OPEN_FAIL_ERROR_NUM) { @@ -60,6 +60,7 @@ bool RandomAccessFile::Init(const std::string& filePath) HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); return false; } + readFile_ = readFile; return true; } @@ -85,7 +86,7 @@ bool RandomAccessFile::InitWithFd(const int32_t fileFd) HAPVERIFY_LOG_ERROR("getting fileLength failed: %{public}lld", fileLength); return false; } - readFile = true; + readFile_ = true; return true; } @@ -131,7 +132,7 @@ long long RandomAccessFile::DoMMap(int32_t bufCapacity, long long offset, MmapIn long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset, int32_t bufCapacity) { - if (readFile) { + if (readFile_) { return ReadFileFullyFromOffsetV2(buf, offset, bufCapacity); } if (buf == nullptr) { @@ -155,7 +156,7 @@ long long RandomAccessFile::ReadFileFullyFromOffset(char buf[], long long offset long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long long offset) { - if (readFile) { + if (readFile_) { return ReadFileFullyFromOffsetV2(buffer, offset); } if (!buffer.HasRemaining()) { @@ -177,7 +178,7 @@ long long RandomAccessFile::ReadFileFullyFromOffset(HapByteBuffer& buffer, long bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize, long long offset) { - if (readFile) { + if (readFile_) { return ReadFileFromOffsetAndDigestUpdateV2(digestParam, chunkSize, offset); } MmapInfo mmapInfo; @@ -221,7 +222,11 @@ long long RandomAccessFile::ReadFileFullyFromOffsetV2(HapByteBuffer& buffer, lon HAPVERIFY_LOG_ERROR("Invalid buffer capacity"); return DEST_BUFFER_IS_NULL; } - char* buf = new char[bufCapacity]; + char* buf = new (std::nothrow) char[bufCapacity]; + if (buf == nullptr) { + HAPVERIFY_LOG_ERROR("Failed to allocate memory for buffer"); + return DEST_BUFFER_IS_NULL; + } long long bytesRead = pread(fd, buf, bufCapacity, offset); if (bytesRead < 0) { @@ -242,7 +247,7 @@ bool RandomAccessFile::ReadFileFromOffsetAndDigestUpdateV2(const DigestParameter HAPVERIFY_LOG_ERROR("Invalid chunkSize"); return false; } - unsigned char* buffer = new unsigned char[chunkSize]; + unsigned char* buffer = new (std::nothrow) unsigned char[chunkSize]; if (buffer == nullptr) { HAPVERIFY_LOG_ERROR("Failed to allocate memory for buffer"); return false; diff --git a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp index 7a9f5e3..e6c109b 100644 --- a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp +++ b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp @@ -84,13 +84,13 @@ void SetDevMode(DevMode mode) g_mtx.unlock(); } -int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult) +int32_t HapVerify(const std::string& filePath, HapVerifyResult& hapVerifyResult, bool readFile) { if (!g_isInit && !HapVerifyInit()) { return VERIFY_SOURCE_INIT_FAIL; } HapVerifyV2 hapVerifyV2; - return hapVerifyV2.Verify(filePath, hapVerifyResult); + return hapVerifyV2.Verify(filePath, hapVerifyResult, readFile); } int32_t ParseHapProfile(const std::string& filePath, HapVerifyResult& hapVerifyV1Result) diff --git a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp index 1e20604..5161f7c 100644 --- a/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp +++ b/interfaces/innerkits/appverify/src/verify/hap_verify_v2.cpp @@ -41,7 +41,7 @@ const std::string HapVerifyV2::HAP_APP_PATTERN = "[^]*.hap$"; const std::string HapVerifyV2::HQF_APP_PATTERN = "[^]*.hqf$"; const std::string HapVerifyV2::HSP_APP_PATTERN = "[^]*.hsp$"; -int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result) +int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVerifyV1Result, bool readFile) { HAPVERIFY_LOG_DEBUG("Start Verify"); std::string standardFilePath; @@ -50,7 +50,7 @@ int32_t HapVerifyV2::Verify(const std::string& filePath, HapVerifyResult& hapVer } RandomAccessFile hapFile; - if (!hapFile.Init(standardFilePath)) { + if (!hapFile.Init(standardFilePath, readFile)) { HAPVERIFY_LOG_ERROR("open standard file failed"); return OPEN_FILE_ERROR; } -- Gitee