!382 merge rwlock into master

修改rwlock锁

Created-by: AXYChen
Commit-by: AXYChen
Merged-by: openharmony_ci
Description: ### 关联的issue:https://gitcode.com/openharmony/security_security_component_manager/issues/346

### 修改描述:

### 测试用例(附上截图):
TDD:
![image.png](https://raw.gitcode.com/user-images/assets/4378957/bb4aec3b-8c95-4584-bad2-98fa28a05bec/image.png 'image.png')

XTS:
![image.png](https://raw.gitcode.com/user-images/assets/4378957/2475a691-b4dc-4334-b388-d83a6d024258/image.png 'image.png')

手工用例:

### 兼容性上库自检:
- [x] 不涉及组件视觉效果发生变化
- [x] 不涉及组件布局调整
- [x] 不涉及组件交互体验发生变化
- [x] 不涉及组件动效发生变化
- [x] 不涉及组件无障碍、适老化、全球化规格变化
- [x] 不涉及API功能发生变化
- [x] 不涉及改变回调函数或生命周期的触发时机或时序,或删除生命周期和回调函数
- [x] 不涉及参数的规格发生了变化,例如取值范围缩小等
- [x] 不涉及对外接口新增权限校验
- [x] 不涉及接口的使用约束规格收紧,例如权限开放范围变化
- [x] 不涉及系统可创建的实例数量收紧
- [x] 不涉及修改了接口返回的数据
- [x] 未新增错误抛出,包括新增错误码、对已有的场景从不会抛错误码变成会抛出错误
- [x] 未修改已有的错误码,对于相同的输入原本抛出A错误码变成了抛出B错误码
- [x] 接口性能未出现明显劣化

### 日志规范自检:
- [x] 【规则】高频代码的正常流程中禁止打印日志
- [x] 【规则】在基本不可能发生的点必须要打印日志
- [x] 【规则】事件记录的日志使用who do what 主谓宾的形式打印
- [x] 【规则】状态变化的日志打印使用state_name:s1->s2, reason:msg的形式打印
- [x] 【规则】参数值的日志打印使用name1=value1, name2=value2…的形式打印
- [x] 【规则】代码运行成功的日志使用xxx successful的形式打印
- [x] 【规则】代码运行失败的日志使用xxx failed, please xxx的形式打印

### 安全编码自检:
- [x] 裸指针避免通过隐式转换构造为sptr
- [x] json对象在取值之前必须先判断类型,避免类型不匹配
- [x] 序列化时必须对传入的数组大小进行校验,避免出现超大数组
- [x] 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型
- [x] 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验
- [x] 指针变量、表示资源描述符的变量、bool变量必须赋初值
- [x] readParcelable获取的对象使用前需要判空
- [x] 分配和释放内存的函数需要成对出现
- [x] 申请内存后异常退出前需要及时进行内存释放
- [x] 内存申请前必须对内存大小进行合法性校验
- [x] 内存分配后必须判断是否成功
- [x] 禁止使用realloc、alloc函数
- [x] 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰
- [x] 禁止打印内存地址
- [x] 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0
- [x] 禁止对有符号整数进行位操作符运算
- [x] 禁止对指针进行逻辑或位运算
- [x] 循环次数如果收外部数据控制,需要检验其合法性
- [x] 禁止使用内存操作类危险函数,需要使用安全函数
- [x] 谨慎使用不可重入函数
- [x] 必须检查安全函数的返回值,并进行正确处理
- [x] 禁止仅通过TokenType类型判断绕过权限校验


See merge request: openharmony/security_security_component_manager!382
This commit is contained in:
openharmony_ci
2026-01-13 21:50:17 +08:00
9 changed files with 26 additions and 21 deletions
+1
View File
@@ -31,6 +31,7 @@
"c_utils",
"data_share",
"eventhandler",
"ffrt",
"hilog",
"hisysevent",
"hitrace",
@@ -267,6 +267,7 @@ ohos_source_set("security_component_service_src_set") {
"c_utils:utils",
"data_share:datashare_consumer",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"graphic_2d:librender_service_client",
"hilog:libhilog",
"hisysevent:libhisysevent",
@@ -35,7 +35,7 @@ AppStateObserver::~AppStateObserver()
bool AppStateObserver::IsProcessForeground(int32_t pid, int32_t uid)
{
Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->fgProcLock_);
std::unique_lock<ffrt::shared_mutex> infoGuard(this->fgProcLock_);
for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) {
if (pid == iter->pid) {
return true;
@@ -51,7 +51,7 @@ bool AppStateObserver::IsProcessForeground(int32_t pid, int32_t uid)
void AppStateObserver::AddProcessToForegroundSet(int32_t pid, const SecCompProcessData& data)
{
Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->fgProcLock_);
std::unique_lock<ffrt::shared_mutex> infoGuard(this->fgProcLock_);
for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) {
if (pid == -1) {
if (iter->uid == data.uid) {
@@ -88,7 +88,7 @@ void AppStateObserver::AddProcessToForegroundSet(const AppExecFwk::ProcessData &
void AppStateObserver::RemoveProcessFromForegroundSet(int32_t pid)
{
Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->fgProcLock_);
std::unique_lock<ffrt::shared_mutex> infoGuard(this->fgProcLock_);
for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) {
if (pid == iter->pid) {
foregrandProcList_.erase(iter);
@@ -129,7 +129,7 @@ void AppStateObserver::OnAppCacheStateChanged(const AppExecFwk::AppStateData &ap
void AppStateObserver::DumpProcess(std::string& dumpStr)
{
Utils::UniqueWriteGuard<Utils::RWLock> infoGuard(this->fgProcLock_);
std::unique_lock<ffrt::shared_mutex> infoGuard(this->fgProcLock_);
for (auto iter = foregrandProcList_.begin(); iter != foregrandProcList_.end(); ++iter) {
dumpStr.append("uid:" + std::to_string(iter->uid) + ", pid:" + std::to_string(iter->pid));
dumpStr.append(", procName:" + iter->bundleName + "\n");
@@ -19,8 +19,8 @@
#include <vector>
#include "app_mgr_interface.h"
#include "application_state_observer_stub.h"
#include "ffrt.h"
#include "iremote_object.h"
#include "rwlock.h"
namespace OHOS {
namespace Security {
@@ -48,7 +48,7 @@ public:
private:
void RemoveProcessFromForegroundSet(int32_t pid);
std::vector<SecCompProcessData> foregrandProcList_;
OHOS::Utils::RWLock fgProcLock_;
ffrt::shared_mutex fgProcLock_;
};
} // namespace SecurityComponent
} // namespace Security
@@ -63,7 +63,7 @@ SecCompManager& SecCompManager::GetInstance()
}
bool SecCompManager::IsScIdExist(int32_t scId)
{
OHOS::Utils::UniqueReadGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::shared_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
for (auto it = componentMap_.begin(); it != componentMap_.end(); ++it) {
for (auto iter = it->second.compList.begin(); iter != it->second.compList.end(); ++iter) {
std::shared_ptr<SecCompEntity> sc = *iter;
@@ -91,7 +91,7 @@ int32_t SecCompManager::CreateScId()
int32_t SecCompManager::AddSecurityComponentToList(int32_t pid,
AccessToken::AccessTokenID tokenId, std::shared_ptr<SecCompEntity> newEntity)
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
if (isSaExit_) {
SC_LOG_ERROR(LABEL, "SA is exiting, retry...");
return SC_SERVICE_ERROR_SERVICE_NOT_EXIST;
@@ -120,7 +120,7 @@ int32_t SecCompManager::AddSecurityComponentToList(int32_t pid,
int32_t SecCompManager::DeleteSecurityComponentFromList(int32_t pid, int32_t scId)
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
auto iter = componentMap_.find(pid);
if (iter == componentMap_.end()) {
SC_LOG_ERROR(LABEL, "Can not find registered process");
@@ -208,7 +208,7 @@ bool SecCompManager::IsCompExist()
void SecCompManager::NotifyProcessForeground(int32_t pid)
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
auto iter = componentMap_.find(pid);
if (iter == componentMap_.end()) {
return;
@@ -221,7 +221,7 @@ void SecCompManager::NotifyProcessForeground(int32_t pid)
void SecCompManager::NotifyProcessBackground(int32_t pid)
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
auto iter = componentMap_.find(pid);
if (iter == componentMap_.end()) {
return;
@@ -239,7 +239,7 @@ void SecCompManager::NotifyProcessDied(int32_t pid, bool isProcessCached)
SecCompEnhanceAdapter::NotifyProcessDied(pid);
malicious_.RemoveAppFromMaliciousAppList(pid);
}
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
auto iter = componentMap_.find(pid);
if (iter == componentMap_.end()) {
return;
@@ -258,7 +258,7 @@ void SecCompManager::NotifyProcessDied(int32_t pid, bool isProcessCached)
void SecCompManager::ExitSaProcess()
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
if (IsCompExist()) {
SC_LOG_INFO(LABEL, "Apps using security component still exist, no exit sa");
return;
@@ -284,7 +284,7 @@ void SecCompManager::ExitSaProcess()
void SecCompManager::ExitWhenAppMgrDied()
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) {
iter->second.compList.clear();
SecCompPermManager::GetInstance().RevokeAppPermissions(iter->second.tokenId);
@@ -333,7 +333,7 @@ int32_t SecCompManager::AddSecurityComponentProcess(const SecCompCallerInfo& cal
{
DelayExitTask::GetInstance().Stop();
{
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
if (isSaExit_) {
SC_LOG_ERROR(LABEL, "SA is exiting, retry...");
return SC_SERVICE_ERROR_SERVICE_NOT_EXIST;
@@ -408,7 +408,7 @@ int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::js
return SC_ENHANCE_ERROR_IN_MALICIOUS_LIST;
}
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
std::shared_ptr<SecCompEntity> sc = GetSecurityComponentFromList(caller.pid, scId);
if (sc == nullptr) {
SC_LOG_ERROR(LABEL, "Can not find target component");
@@ -595,7 +595,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(SecCompInfo& info, con
if (res != SC_OK) {
return res;
}
OHOS::Utils::UniqueWriteGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::unique_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
std::shared_ptr<SecCompEntity> sc = GetSecurityComponentFromList(caller.pid, info.scId);
if (sc == nullptr) {
SC_LOG_ERROR(LABEL, "Can not find target component");
@@ -629,7 +629,7 @@ int32_t SecCompManager::ReportSecurityComponentClickEvent(SecCompInfo& info, con
void SecCompManager::DumpSecComp(std::string& dumpStr)
{
OHOS::Utils::UniqueReadGuard<OHOS::Utils::RWLock> lk(this->componentInfoLock_);
std::shared_lock<ffrt::shared_mutex> lk(this->componentInfoLock_);
for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) {
AccessToken::AccessTokenID tokenId = iter->second.tokenId;
bool locationPerm = SecCompPermManager::GetInstance().VerifyPermission(tokenId, LOCATION_COMPONENT);
@@ -23,9 +23,9 @@
#include <vector>
#include "accesstoken_kit.h"
#include "app_state_observer.h"
#include "ffrt.h"
#include "first_use_dialog.h"
#include "nocopyable.h"
#include "rwlock.h"
#include "sec_comp_base.h"
#include "sec_comp_entity.h"
#include "sec_comp_info.h"
@@ -88,7 +88,7 @@ private:
int32_t CreateScId();
void GetFoldOffsetY(const CrossAxisState crossAxisState);
OHOS::Utils::RWLock componentInfoLock_;
ffrt::shared_mutex componentInfoLock_;
std::mutex scIdMtx_;
std::mutex superFoldOffsetMtx_;
std::unordered_map<int32_t, ProcessCompInfos> componentMap_;
@@ -19,7 +19,7 @@
#include <map>
#include <set>
#include "accesstoken_kit.h"
#include "rwlock.h"
#include "ffrt.h"
#include "sec_comp_base.h"
#include "sec_event_handler.h"
@@ -89,6 +89,7 @@ ohos_unittest("sec_comp_service_test") {
"access_token:libtokenid_sdk",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"googletest:gmock_main",
"graphic_2d:librender_service_client",
"hilog:libhilog",
@@ -169,6 +170,7 @@ ohos_unittest("sec_comp_service_mock_test") {
"access_token:libtokenid_sdk",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"googletest:gmock_main",
"graphic_2d:librender_service_client",
"hilog:libhilog",
@@ -45,6 +45,7 @@ sc_external_deps = [
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"graphic_2d:librender_service_client",
"hilog:libhilog",
"hisysevent:libhisysevent",