!19458 merge refactor/isRestored-wantagent-recovery into master

refactor: add isRestored_ flag for WantAgent FromString recovery

Created-by: RuiChen_01
Commit-by: RuiChen_01
Merged-by: openharmony_ci
Description: **IssueNo**:

**Description**:

**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在lambda表达式中使用引用捕获                                |          |
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串               |          |
| map\vector\list\set等stl模板类使用时需要排查并发                |          |
| 谨慎考虑加锁范围                                               |          |
| 在IPC通信中谨慎使用同步通信方式                                 |          |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务)        |          |
| 禁止将外部传入的裸指针在内部直接构造智能指针                      |          |
| 禁止多个独立创建的智能指针管理同一地址                           |          |
| 禁止在析构函数中抛异步任务                                      |          |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁             |          |
| 禁止在对外接口中未经判空直接使用外部传入的指针                    |          |
| 禁止接口返回局部变量引用                                        |          |
| 禁止在信号函数中加锁                                            |          |
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作           |          |
| 禁止将同一个cpp编译在不同的so中                                 |          |

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

**TDD Result**:

**XTS Result**:

### 是否已执行L0用例
- [ ] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!19458
This commit is contained in:
openharmony_ci
2026-06-01 11:44:39 +08:00
6 changed files with 70 additions and 20 deletions
@@ -152,7 +152,8 @@ public:
public:
sptr<IWantSender> GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp,
const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0,
int32_t publisherUid = 0);
int32_t SendWantSender(sptr<IWantSender> target, SenderInfo &senderInfo);
int32_t SendLocalWantSender(const SenderInfo &senderInfo);
void CancelWantSender(const bool isSystemAppCall, const sptr<IWantSender> &sender);
@@ -179,7 +180,7 @@ public:
int32_t DeviceIdDetermine(const Want &want, const sptr<StartOptions> &startOptions,
const sptr<IRemoteObject> &callerToken, int32_t requestCode, const int32_t callerUid, int32_t callerTokenId,
int32_t userId);
int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t callerUid,
int32_t PendingWantPublishCommonEvent(const Want &want, const SenderInfo &senderInfo, int32_t publisherUid,
int32_t callerTokenId);
void ClearPendingWantRecord(const std::string &bundleName, int32_t uid);
@@ -189,7 +190,8 @@ public:
private:
sptr<IWantSender> GetWantSenderLocked(const int32_t callingUid, const int32_t uid, const int32_t userId,
WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0);
WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex = 0,
int32_t publisherUid = 0);
void MakeWantSenderCanceledLocked(PendingWantRecord &record);
void HandleAddWantAgentNumber(std::shared_ptr<PendingWantKey> pendingKey);
@@ -47,6 +47,8 @@ public:
std::shared_ptr<PendingWantKey> GetKey();
int32_t GetUid() const;
void SetCallerUid(const int32_t callerUid);
void SetPublisherUid(int32_t publisherUid);
int32_t GetPublisherUid() const;
void SetCanceled();
bool GetCanceled();
std::list<sptr<IWantReceiver>> GetCancelCallbacks();
@@ -67,6 +69,7 @@ private:
std::shared_ptr<PendingWantKey> key_ = {};
std::list<sptr<IWantReceiver>> mCancelCallbacks_ = {};
int32_t callerUid_ = 0;
int32_t publisherUid_ = 0;
bool canceled_ = false;
ffrt::mutex lock_ = {};
std::mutex mCancelCallbacksMutex_;
@@ -6643,13 +6643,10 @@ sptr<IWantSender> AbilityManagerService::GetWantSenderByUserId(const WantSenderI
const sptr<IRemoteObject> &callerToken, int32_t uid, int32_t callerUid, int32_t callerUserId)
{
bool isSpecifyUserId = wantSenderInfo.userId >= 0;
std::string bundleName = "";
if (!wantSenderInfo.allWants.empty()) {
bundleName = wantSenderInfo.allWants.back().want.GetBundle();
}
std::string bundleName = wantSenderInfo.allWants.empty() ? "" :
wantSenderInfo.allWants.back().want.GetBundle();
bool isSACall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
bool isSystemApp = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall();
int32_t userId = -1;
int32_t appUid = -1;
int32_t appIndex = 0;
@@ -6684,10 +6681,11 @@ sptr<IWantSender> AbilityManagerService::GetWantSenderByUserId(const WantSenderI
}
const_cast<WantSenderInfo&>(wantSenderInfo).userId = userId;
TAG_LOGI(AAFwkTag::WANTAGENT, "bundleName: %{public}s, appIndex: %{public}d, isSystemApp: %{public}d, "
"isSACall: %{public}d, userId: %{public}d, appUid: %{public}d", bundleName.c_str(), appIndex, isSystemApp,
isSACall, userId, appUid);
return pendingWantManager->GetWantSender(callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex);
"isSACall: %{public}d, userId: %{public}d, appUid: %{public}d", bundleName.c_str(), appIndex,
isSystemApp, isSACall, userId, appUid);
int32_t publisherUid = (uid >= 0 && appUid >= 0) ? appUid : callerUid;
return pendingWantManager->GetWantSender(
callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex, publisherUid);
}
sptr<IWantSender> AbilityManagerService::GetWantSender(
@@ -6750,7 +6748,9 @@ sptr<IWantSender> AbilityManagerService::GetWantSender(
TAG_LOGI(AAFwkTag::WANTAGENT, "%{public}s#%{public}d, isSystemApp: %{public}d, "
"userId: %{public}d", wantSenderInfo.bundleName.c_str(), appIndex, isSystemApp, userId);
return pendingWantManager->GetWantSender(callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex);
int32_t publisherUid = (uid >= 0 && appUid >= 0) ? appUid : callerUid;
return pendingWantManager->GetWantSender(
callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex, publisherUid);
}
int AbilityManagerService::SendWantSender(sptr<IWantSender> target, SenderInfo &senderInfo)
@@ -41,7 +41,8 @@ PendingWantManager::~PendingWantManager()
}
sptr<IWantSender> PendingWantManager::GetWantSender(int32_t callingUid, int32_t uid, const bool isSystemApp,
const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex)
const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex,
int32_t publisherUid)
{
TAG_LOGD(AAFwkTag::WANTAGENT, "begin");
if (wantSenderInfo.type != static_cast<int32_t>(OperationType::SEND_COMMON_EVENT)) {
@@ -73,7 +74,7 @@ sptr<IWantSender> PendingWantManager::GetWantSender(int32_t callingUid, int32_t
}
}
return GetWantSenderLocked(callingUid, uid, wantSenderInfo.userId, info, callerToken, appIndex);
return GetWantSenderLocked(callingUid, uid, wantSenderInfo.userId, info, callerToken, appIndex, publisherUid);
}
void SendWantAgentNumberEvent(std::shared_ptr<PendingWantKey> pendingKey, int32_t wantAgentNumber)
@@ -164,7 +165,8 @@ void PendingWantManager::ReduceWantAgentNumber(std::shared_ptr<PendingWantKey> p
}
sptr<IWantSender> PendingWantManager::GetWantSenderLocked(const int32_t callingUid, const int32_t uid,
const int32_t userId, WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex)
const int32_t userId, WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken, int32_t appIndex,
int32_t publisherUid)
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
TAG_LOGI(AAFwkTag::WANTAGENT, "begin");
@@ -200,6 +202,7 @@ sptr<IWantSender> PendingWantManager::GetWantSenderLocked(const int32_t callingU
wantSenderInfo.allWants.back().resolvedTypes = ref->GetKey()->GetRequestResolvedType();
ref->GetKey()->SetAllWantsInfos(wantSenderInfo.allWants);
ref->SetCallerUid(callingUid);
ref->SetPublisherUid(publisherUid);
}
return ref;
}
@@ -217,6 +220,7 @@ sptr<IWantSender> PendingWantManager::GetWantSenderLocked(const int32_t callingU
callerToken, pendingKey);
if (rec != nullptr) {
rec->SetCallerUid(callingUid);
rec->SetPublisherUid(publisherUid);
pendingKey->SetCode(PendingRecordIdCreate());
AddWantAgentNumber(pendingKey);
wantRecords_.insert(std::make_pair(pendingKey, rec));
@@ -477,7 +481,7 @@ int32_t PendingWantManager::PendingWantStartAbilitys(const std::vector<WantsInfo
}
int32_t PendingWantManager::PendingWantPublishCommonEvent(
const Want &want, const SenderInfo &senderInfo, int32_t callerUid, int32_t callerTokenId)
const Want &want, const SenderInfo &senderInfo, int32_t publisherUid, int32_t callerTokenId)
{
TAG_LOGI(AAFwkTag::WANTAGENT, "publish common event");
@@ -499,7 +503,7 @@ int32_t PendingWantManager::PendingWantPublishCommonEvent(
}
bool result = IN_PROCESS_CALL(DelayedSingleton<EventFwk::CommonEvent>::GetInstance()->PublishCommonEvent(
eventData, eventPublishData, nullptr, callerUid, callerTokenId));
eventData, eventPublishData, nullptr, publisherUid, callerTokenId));
return ((result == true) ? ERR_OK : (-1));
}
@@ -200,9 +200,11 @@ int32_t PendingWantRecord::ExecuteOperation(
SendTriggerFailedEvent(want, key_->GetAppIndex(), callerUid_, res, "Trigger Failed");
}
break;
case static_cast<int32_t>(OperationType::SEND_COMMON_EVENT):
res = pendingWantManager->PendingWantPublishCommonEvent(want, senderInfo, callerUid_, callerTokenId_);
case static_cast<int32_t>(OperationType::SEND_COMMON_EVENT): {
res = pendingWantManager->PendingWantPublishCommonEvent(want, senderInfo, publisherUid_,
callerTokenId_);
break;
}
case static_cast<int32_t>(OperationType::START_SERVICE_EXTENSION):
res = pendingWantManager->PendingWantStartServiceExtension(want, senderInfo.callerToken);
break;
@@ -295,6 +297,16 @@ void PendingWantRecord::SetCallerUid(const int32_t callerUid)
callerUid_ = callerUid;
}
void PendingWantRecord::SetPublisherUid(int32_t publisherUid)
{
publisherUid_ = publisherUid;
}
int32_t PendingWantRecord::GetPublisherUid() const
{
return publisherUid_;
}
std::list<sptr<IWantReceiver>> PendingWantRecord::GetCancelCallbacks()
{
std::lock_guard guard(mCancelCallbacksMutex_);
@@ -1355,5 +1355,34 @@ HWTEST_F(PendingWantRecordTest, ExecuteOperation_EmptyAllWantsInfos_START_ABILIT
TAG_LOGI(AAFwkTag::TEST, "ExecuteOperation_EmptyAllWantsInfos_START_ABILITIES_0100 end");
}
/*
* @tc.number : SetPublisherUid_GetPublisherUid_0100
* @tc.name : SetPublisherUid/GetPublisherUid
* @tc.desc : 1.Default publisherUid_ is 0, set to specific uid values
*/
HWTEST_F(PendingWantRecordTest, SetPublisherUid_GetPublisherUid_0100, TestSize.Level1)
{
TAG_LOGI(AAFwkTag::TEST, "SetPublisherUid_GetPublisherUid_0100 start");
Want want;
ElementName element("device", "com.ix.hiMusic", "MusicSAbility");
want.SetElement(element);
WantSenderInfo wantSenderInfo = MakeWantSenderInfo(want, 0, 0);
pendingManager_ = std::make_shared<PendingWantManager>();
EXPECT_NE(pendingManager_, nullptr);
std::shared_ptr<PendingWantKey> key = MakeWantKey(wantSenderInfo);
std::shared_ptr<PendingWantRecord> pendingWantRecord =
std::make_shared<PendingWantRecord>(pendingManager_, 1, 0, nullptr, key);
EXPECT_NE(pendingWantRecord, nullptr);
EXPECT_EQ(pendingWantRecord->GetPublisherUid(), 0);
pendingWantRecord->SetPublisherUid(100);
EXPECT_EQ(pendingWantRecord->GetPublisherUid(), 100);
pendingWantRecord->SetPublisherUid(200);
EXPECT_EQ(pendingWantRecord->GetPublisherUid(), 200);
TAG_LOGI(AAFwkTag::TEST, "SetPublisherUid_GetPublisherUid_0100 end");
}
} // namespace AAFwk
} // namespace OHOS