From cab88532699aa83d0c3d95be25655a316724cb28 Mon Sep 17 00:00:00 2001 From: "zhangkaixiang5@huawei.com" Date: Fri, 20 Jan 2023 11:55:18 +0800 Subject: [PATCH] modify the security to upper Signed-off-by: zhangkaixiang5@huawei.com --- .../js/src/mod_securitylabel/security_label.h | 23 ++++++++----------- .../securitylabel_n_exporter.cpp | 10 ++++---- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index e789b7261..e50e99b22 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -16,6 +16,7 @@ #ifndef SECURITY_LABEL_H #define SECURITY_LABEL_H +#include #include #include #include @@ -35,7 +36,9 @@ public: if (DATA_LEVEL.count(dataLevel) != 1) { return false; } - if (setxattr(path.c_str(), XATTR_KEY, dataLevel.c_str(), dataLevel.size(), 0) < 0) { + std::string upperDataLevel = dataLevel; + std::transform(upperDataLevel.begin(), upperDataLevel.end(), upperDataLevel.begin(), ::toupper); + if (setxattr(path.c_str(), XATTR_KEY, upperDataLevel.c_str(), upperDataLevel.size(), 0) < 0) { return false; } return true; @@ -44,25 +47,17 @@ public: static std::string GetSecurityLabel(const std::string &path) { auto xattrValueSize = getxattr(path.c_str(), XATTR_KEY, nullptr, 0); - if (xattrValueSize == -1 || errno == ENOTSUP) { - return ""; - } - if (xattrValueSize <= 0) { + if (xattrValueSize == -1) { return DEFAULT_DATA_LEVEL; } std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); - if (xattrValue == nullptr) { - return ""; - } - xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue.get(), xattrValueSize); - if (xattrValueSize == -1 || errno == ENOTSUP) { - return ""; - } - if (xattrValueSize <= 0) { + std::string result(xattrValue.get()); + std::transform(result.begin(), result.end(), result.begin(), ::tolower); + if (DATA_LEVEL.count(result) != 1) { return DEFAULT_DATA_LEVEL; } - return std::string(xattrValue.get()); + return result; } }; } // namespace ModuleSecurityLabel diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp index da979a107..0bd2dd807 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_n_exporter.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "filemgmt_libhilog.h" #include "security_label.h" @@ -40,11 +41,12 @@ napi_value SetSecurityLabel(napi_env env, napi_callback_info info) std::unique_ptr path; std::unique_ptr dataLevel; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); - if (!succ) { + if (!succ || access(path.get(), F_OK) != 0) { HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; } + tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); if (!succ) { HILOGE("Invalid dataLevel"); @@ -93,7 +95,7 @@ napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) std::unique_ptr path; std::unique_ptr dataLevel; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); - if (!succ) { + if (!succ || access(path.get(), F_OK) != 0) { HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -133,7 +135,7 @@ napi_value GetSecurityLabel(napi_env env, napi_callback_info info) bool succ = false; std::unique_ptr path; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); - if (!succ) { + if (!succ || access(path.get(), F_OK) != 0) { HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -176,7 +178,7 @@ napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) bool succ = false; std::unique_ptr path; tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::FIRST)]).ToUTF8String(); - if (!succ) { + if (!succ || access(path.get(), F_OK) != 0) { HILOGE("Invalid path"); NError(EINVAL).ThrowErr(env); return nullptr; -- Gitee