From 00642e00dcf0f98415251ba628d872578093bb02 Mon Sep 17 00:00:00 2001 From: wangchen Date: Fri, 4 Jul 2025 16:14:41 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20=E4=BD=8E=E7=BC=96=E8=AF=91=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=80=82=E9=85=8D=20close=20#ICJU61=20Signed-off-by:?= =?UTF-8?q?=20wangchen=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binary_sign_tool/BUILD.gn | 6 +- binary_sign_tool/cmd/src/cmd_util.cpp | 56 ++++++++++++++--- binary_sign_tool/cmd/src/params_run_tool.cpp | 2 - .../hap/provider/include/sign_provider.h | 1 - binary_sign_tool/hap/sign/src/sign_elf.cpp | 1 - binary_sign_tool/main.cpp | 10 +++ binary_sign_tool/utils/src/file_utils.cpp | 62 +++++++------------ hapsigntool_cpp/bundle.json | 3 +- 8 files changed, 87 insertions(+), 54 deletions(-) diff --git a/binary_sign_tool/BUILD.gn b/binary_sign_tool/BUILD.gn index 733ffddd..0b586416 100644 --- a/binary_sign_tool/BUILD.gn +++ b/binary_sign_tool/BUILD.gn @@ -73,14 +73,14 @@ ohos_executable("binary-sign-tool") { sources = signature_tools_main_src deps = [ - "//third_party/elfio:elfio", - "//third_party/openssl:libcrypto_shared", - "//third_party/openssl:libssl_shared", ] external_deps = [ "bounds_checking_function:libsec_static", + "elfio:elfio", "json:nlohmann_json_static", + "openssl:libcrypto_static", + "openssl:libssl_static", ] cflags_cc = [ diff --git a/binary_sign_tool/cmd/src/cmd_util.cpp b/binary_sign_tool/cmd/src/cmd_util.cpp index 6b2b5b15..dd0029ce 100644 --- a/binary_sign_tool/cmd/src/cmd_util.cpp +++ b/binary_sign_tool/cmd/src/cmd_util.cpp @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "cmd_util.h" +#include #include -#include - +#include +#include "cmd_util.h" #include "params_run_tool.h" #include "constant.h" #include "param_constants.h" @@ -162,13 +162,53 @@ static bool UpdateParamForVariantBoolAdHoc(const ParamsSharedPtr& param) return true; } +static std::string GetParentPath(const std::string &outFilePath) +{ + if (outFilePath.size() == 0) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "get_parent_path realpath error, empty input"); + return ""; + } + + if (outFilePath.size() > PATH_MAX) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "get_parent_path realpath error, input too long"); + return ""; + } + + char resolvedPath[PATH_MAX + 1] = {0x00}; + if (realpath(outFilePath.c_str(), resolvedPath) == nullptr) { + SIGNATURE_TOOLS_LOGI("Get realpath from %s may failed", resolvedPath); + return ""; + } + resolvedPath[PATH_MAX] = '\0'; + SIGNATURE_TOOLS_LOGI("GetParentPath, resolvedPath:%s", resolvedPath); + char *parentPath = dirname(resolvedPath); + if (parentPath == nullptr) { + SIGNATURE_TOOLS_LOGI("Get parentPath of %s may failed", resolvedPath); + return ""; + } + + SIGNATURE_TOOLS_LOGI("GetParentPath :%s", parentPath); + return std::string(parentPath); +} + +static std::string GetFileName(const std::string &path) +{ + size_t lastSlash = path.find_last_of('/'); + if (lastSlash == std::string::npos) { + return path; + } + + return path.substr(lastSlash + 1); +} + bool CmdUtil::UpdateParamForCheckOutFile(Options* options, const std::initializer_list& outFileKeys) { for (auto& key : outFileKeys) { if (options->count(key)) { std::string outFilePath = options->GetString(key); - std::filesystem::path filePath = outFilePath; - std::string parentPath = filePath.parent_path(); + std::string parentPath = GetParentPath(outFilePath); // Purpose: To prevent the user output path from passing an empty string. eg " " std::string tmpOutFilePath = outFilePath; @@ -191,11 +231,13 @@ bool CmdUtil::UpdateParamForCheckOutFile(Options* options, const std::initialize return false; } std::string charStr(realFilePath); - std::string fileName = filePath.filename(); + std::string fileName = GetFileName(outFilePath); if (fileName.empty()) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "The file name cannot be empty '" + outFilePath + "', parameter name '-" + key + "'"); return false; + } else { + SIGNATURE_TOOLS_LOGI("UpdateParamForCheckOutFile GetFileName:%s", fileName.c_str()); } (*options)[key] = charStr + "/" + fileName; } @@ -389,4 +431,4 @@ bool CmdUtil::JudgeSignAlgType(const std::string& signAlg) return true; } } // namespace SignatureTools -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/binary_sign_tool/cmd/src/params_run_tool.cpp b/binary_sign_tool/cmd/src/params_run_tool.cpp index 1136bf6d..3f8abc93 100644 --- a/binary_sign_tool/cmd/src/params_run_tool.cpp +++ b/binary_sign_tool/cmd/src/params_run_tool.cpp @@ -16,8 +16,6 @@ #include "params_run_tool.h" #include #include -#include - #include "constant.h" #include "help.h" diff --git a/binary_sign_tool/hap/provider/include/sign_provider.h b/binary_sign_tool/hap/provider/include/sign_provider.h index 7a3a76b8..6bbd6424 100644 --- a/binary_sign_tool/hap/provider/include/sign_provider.h +++ b/binary_sign_tool/hap/provider/include/sign_provider.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/binary_sign_tool/hap/sign/src/sign_elf.cpp b/binary_sign_tool/hap/sign/src/sign_elf.cpp index bc54e3aa..3c59e5d4 100644 --- a/binary_sign_tool/hap/sign/src/sign_elf.cpp +++ b/binary_sign_tool/hap/sign/src/sign_elf.cpp @@ -14,7 +14,6 @@ */ #include "sign_elf.h" -#include #include #include "file_utils.h" diff --git a/binary_sign_tool/main.cpp b/binary_sign_tool/main.cpp index edcb75a1..c3039a36 100644 --- a/binary_sign_tool/main.cpp +++ b/binary_sign_tool/main.cpp @@ -12,10 +12,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include "params_run_tool.h" using namespace OHOS::SignatureTools; int main(int argc, char** argv) { + OSSL_PROVIDER *prov = OSSL_PROVIDER_load(nullptr, "default"); + if (prov == nullptr) { + PrintMsg("ssl load default provider failed"); + } + OSSL_PROVIDER *prov2 = OSSL_PROVIDER_load(nullptr, "legacy"); + if (prov2 == nullptr) { + PrintMsg("ssl load legacy provider failed"); + } + // prepare modes vector by macro DEFINE_MODE which subscribe UPDATER_MAIN_PRE_EVENT event bool isSuccess = ParamsRunTool::ProcessCmd(argv, argc); if (isSuccess) { diff --git a/binary_sign_tool/utils/src/file_utils.cpp b/binary_sign_tool/utils/src/file_utils.cpp index 24ed0f97..1a12b514 100644 --- a/binary_sign_tool/utils/src/file_utils.cpp +++ b/binary_sign_tool/utils/src/file_utils.cpp @@ -14,11 +14,12 @@ */ #include "file_utils.h" #include -#include -#include -#include #include - +#include +#include +#include +#include +#include #include "string_utils.h" #include "signature_tools_errno.h" @@ -227,7 +228,7 @@ int FileUtils::WriteInputToOutPut(std::ifstream& input, std::ofstream& output, s delete[] buf; return IO_ERROR; } - + if (length <= 0) { break; } @@ -302,61 +303,44 @@ bool FileUtils::IsRunnableFile(const std::string& name) bool FileUtils::IsValidFile(std::string file) { - std::filesystem::path filePath = file; - bool flag = std::filesystem::exists(filePath); + struct stat fileStat; + bool flag = (stat(file.c_str(), &fileStat) == 0); if (!flag) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + file + "' file is not exists"); return false; } - flag = std::filesystem::is_directory(filePath); + flag = S_ISDIR(fileStat.st_mode); if (flag) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + file + "' file is a directory not file"); return false; } + SIGNATURE_TOOLS_LOGI("IsValidFile check pass %s", file.c_str()); return true; } -int64_t FileUtils::GetFileLen(const std::string& file) -{ - std::filesystem::path filePath = file; - bool flag = std::filesystem::exists(filePath) && std::filesystem::is_regular_file(filePath); - if (flag) { - return std::filesystem::file_size(filePath); - } - return -1; -} - -void FileUtils::DelDir(const std::string& file) -{ - std::filesystem::path filePath = file; - bool flag = std::filesystem::is_directory(filePath); - if (flag) { - for (auto& p : std::filesystem::recursive_directory_iterator(filePath)) { - DelDir(p.path()); - } - } - std::filesystem::remove(file); - return; -} - bool FileUtils::CopyTmpFileAndDel(const std::string& tmpFile, const std::string& output) { - if (!std::filesystem::exists(tmpFile)) { - SIGNATURE_TOOLS_LOGE("Error: tmpFile not exists"); - return false; - } if (tmpFile == output) { return true; } - bool ret = std::filesystem::copy_file(tmpFile, output, std::filesystem::copy_options::overwrite_existing); - if (!ret) { - SIGNATURE_TOOLS_LOGE("Error: copy tmpFile to output failed"); + std::ifstream src(tmpFile, std::ios::binary); + if (!src) { + PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + tmpFile + "' open failed"); return false; } - if (!std::filesystem::remove(tmpFile)) { + std::ofstream dst(output, std::ios::binary); + if (!dst) { + PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + output + "' open failed"); + return false; + } + SIGNATURE_TOOLS_LOGI("CopyTmpFileAndDel from %s to %s", tmpFile.c_str(), output.c_str()); + dst << src.rdbuf(); + + if (unlink(tmpFile.c_str()) != 0) { SIGNATURE_TOOLS_LOGE("Error: remove tmpFile"); return false; } + SIGNATURE_TOOLS_LOGI("CopyTmpFileAndDel finish"); return true; } } // namespace SignatureTools diff --git a/hapsigntool_cpp/bundle.json b/hapsigntool_cpp/bundle.json index dfd51feb..ea67e671 100644 --- a/hapsigntool_cpp/bundle.json +++ b/hapsigntool_cpp/bundle.json @@ -33,12 +33,13 @@ "components": [ "bounds_checking_function", "c_utils", + "elfio", "json", + "openssl", "zlib", "hilog" ], "third_party": [ - "elfio", "bzip2", "openssl", "jsoncpp" -- Gitee