From 1102fb0c23713f66c04fbb8b273caccc3b53773f Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Sat, 17 Dec 2022 22:10:23 +0800 Subject: [PATCH] modify the security to upper Signed-off-by: zhangkaixiang --- .../js/src/mod_securitylabel/security_label.h | 23 +++++++--------- .../securitylabel_n_exporter.cpp | 26 ++++++++++++++++++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index 5e47c8be9..627626a3a 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 88ee86e83..d2caece0e 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 "../common/napi/n_class.h" #include "../common/napi/n_func_arg.h" @@ -47,6 +48,12 @@ napi_value SetSecurityLabel(napi_env env, napi_callback_info info) UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } + + if(access(path.get(), F_OK) != 0) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); @@ -103,6 +110,11 @@ napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) return nullptr; } + if(access(path.get(), F_OK) != 0) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast(NARG_POS::SECOND)]).ToUTF8String(); if (!succ) { UniError(EINVAL).ThrowErr(env, "Invalid dataLevel"); @@ -116,7 +128,8 @@ napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info) bool ret = SecurityLabel::SetSecurityLabel(path.get(), dataLevel.get()); if (!ret) { - return UniError(errno).GetNapiErr(env); + UniError(errno).ThrowErr(env); + return nullptr; } return NVal::CreateUndefined(env).val_; @@ -137,6 +150,12 @@ napi_value GetSecurityLabel(napi_env env, napi_callback_info info) UniError(EINVAL).ThrowErr(env, "Invalid path"); return nullptr; } + + if(access(path.get(), F_OK) != 0) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + auto result = std::make_shared(); std::string pathString(path.get()); auto cbExec = [pathString, result](napi_env env) -> UniError { @@ -179,6 +198,11 @@ napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info) return nullptr; } + if(access(path.get(), F_OK) != 0) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + std::string result = SecurityLabel::GetSecurityLabel(path.get()); return NVal::CreateUTF8String(env, result).val_; } -- Gitee