mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-25 12:23:20 -04:00
8beb5745ad
obs uri check Created-by: shu-xin0115 Commit-by: shu-xin0115 Merged-by: openharmony_ci Description: https://openharmonyinsight.cn/review/review/54276  **IssueNo**:https://gitcode.com/openharmony/distributeddatamgr_data_share/issues/1147 **Description**:obs uri check **稳定性自检:** | 自检项 | 自检结果 | | ------------------------------------------------------------ | -------- | | 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发 | Y | | 成员变量进行赋值或创建需要排查并发 | Y | | 谨慎在lambda表达式中使用引用捕获 | Y | | 谨慎在未经拷贝的情况下使用外部传入的string、C字符串 | Y | | map\vector\list\set等stl模板类使用时需要排查并发 | Y | | 谨慎考虑加锁范围 | Y | | 在IPC通信中谨慎使用同步通信方式 | Y | | 禁止传递this指针至其他模块或线程(特别是eventhandler任务) | Y | | 禁止将外部传入的裸指针在内部直接构造智能指针 | Y | | 禁止多个独立创建的智能指针管理同一地址 | Y | | 禁止在析构函数中抛异步任务 | Y | | 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁 | Y | | 禁止在对外接口中未经判空直接使用外部传入的指针 | Y | | 禁止接口返回局部变量引用 | Y | | 禁止在信号函数中加锁 | Y | | 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作 | Y | | 禁止将同一个cpp编译在不同的so中 | Y | **安全编码自检:** | 自检项 | 自检结果 | | -------------------------------------------------------------- | -------- | | 裸指针避免通过隐式转换构造为sptr | Y | | json对象在取值之前必须先判断类型,避免类型不匹配 | Y | | 序列化时必须对传入的数组大小进行校验,避免出现超大数组 | Y | | 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型 | Y | | 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 | Y | | 指针变量、表示资源描述符的变量、bool变量必须赋初值 | Y | | readParcelable获取的对象使用前需要判空 | Y | | 分配和释放内存的函数需要成对出现 | Y | | 申请内存后异常退出前需要及时进行内存释放 | Y | | 内存申请前必须对内存大小进行合法性校验 | Y | | 内存分配后必须判断是否成功 | Y | | 禁止使用realloc、alloca函数 | Y | | 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰 | Y | | 禁止打印内存地址 | Y | | 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0 | Y | | 禁止对有符号整数进行位操作符运算 | Y | | 禁止对指针进行逻辑或位运算 | Y | | 循环次数如果收外部数据控制,需要检验其合法性 | Y | | 禁止使用内存操作类危险函数,需要使用安全函数 | Y | | 谨慎使用不可重入函数 | Y | | 必须检查安全函数的返回值,并进行正确处理 | Y | | 禁止仅通过TokenType类型判断绕过权限校验 | Y | **TDD Result**:  **XTS Result**:  ### 是否已执行L0用例 - [x] 已验证 - [ ] 不涉及。如不涉及,请写明理由 See merge request: openharmony/ability_ability_runtime!19654
146 lines
6.7 KiB
C++
146 lines
6.7 KiB
C++
/*
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#ifndef OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
|
|
#define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
|
|
|
|
#include <memory>
|
|
#include <list>
|
|
#include <shared_mutex>
|
|
#include <singleton.h>
|
|
#include <thread_ex.h>
|
|
#include "cpp/mutex.h"
|
|
|
|
#include "bundle_mgr_interface.h"
|
|
#include "data_share_permission.h"
|
|
#include "dataobs_mgr_inner.h"
|
|
#include "dataobs_mgr_inner_common.h"
|
|
#include "dataobs_mgr_inner_ext.h"
|
|
#include "dataobs_mgr_inner_pref.h"
|
|
#include "dataobs_mgr_stub.h"
|
|
#include "iremote_object.h"
|
|
#include "system_ability.h"
|
|
#include "task_handler_wrap.h"
|
|
#include "uri.h"
|
|
|
|
namespace OHOS {
|
|
namespace AAFwk {
|
|
using namespace AppExecFwk;
|
|
enum class DataObsServiceRunningState { STATE_NOT_START, STATE_RUNNING };
|
|
constexpr char SHARE_PREFERENCES[] = "sharepreferences";
|
|
constexpr char RELATIONAL_STORE[] = "rdb";
|
|
/**
|
|
* @class DataObsMgrService
|
|
* DataObsMgrService provides a facility for dataobserver.
|
|
*/
|
|
class DataObsMgrService : public SystemAbility,
|
|
public DataObsManagerStub,
|
|
public std::enable_shared_from_this<DataObsMgrService> {
|
|
DECLARE_DELAYED_SINGLETON(DataObsMgrService)
|
|
DECLEAR_SYSTEM_ABILITY(DataObsMgrService)
|
|
public:
|
|
void OnStart() override;
|
|
void OnStop() override;
|
|
DataObsServiceRunningState QueryServiceState() const;
|
|
|
|
std::pair<bool, struct ObserverNode> ConstructObserverNode(sptr<IDataAbilityObserver> dataObserver,
|
|
int32_t userId, uint32_t tokenId, int32_t pid);
|
|
virtual int RegisterObserver(const Uri &uri,
|
|
sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual int RegisterObserverFromExtension(const Uri &uri,
|
|
sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual int UnregisterObserver(const Uri &uri,
|
|
sptr<IDataAbilityObserver> dataObserver, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual int NotifyChange(const Uri &uri, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual int NotifyChangeFromExtension(const Uri &uri, int32_t userId = DATAOBS_DEFAULT_CURRENT_USER,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual Status RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
|
|
bool isDescendants, DataObsOption opt = DataObsOption()) override;
|
|
virtual Status UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual Status UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
virtual Status NotifyChangeExt(const ChangeInfo &changeInfo, DataObsOption opt = DataObsOption()) override;
|
|
virtual Status NotifyProcessObserver(const std::string &key, const sptr<IRemoteObject> &observer,
|
|
DataObsOption opt = DataObsOption()) override;
|
|
|
|
/**
|
|
* @brief DataObs hidumper.
|
|
* @param fd Indicates the fd.
|
|
* @param args Indicates the params.
|
|
* @return Returns the dump result.
|
|
*/
|
|
int Dump(int fd, const std::vector<std::u16string>& args) override;
|
|
|
|
private:
|
|
struct FocusedAppInfo {
|
|
int32_t left = 0;
|
|
int32_t top = 0;
|
|
uint32_t width = 0;
|
|
uint32_t height = 0;
|
|
sptr<IRemoteObject> abilityToken = nullptr;
|
|
};
|
|
bool Init();
|
|
void Dump(const std::vector<std::u16string>& args, std::string& result) const;
|
|
void ShowHelp(std::string& result) const;
|
|
Status DeepCopyChangeInfo(const ChangeInfo &src, ChangeInfo &dst) const;
|
|
FocusedAppInfo GetFocusedWindowInfo() const;
|
|
sptr<IRemoteObject> GetAbilityManagerService() const;
|
|
static int32_t GetCallingUserId(uint32_t tokenId);
|
|
static bool IsSystemApp(uint32_t tokenId, uint64_t fullTokenId);
|
|
static bool IsCallingPermissionValid(DataObsOption &opt, int32_t userId, int32_t callingUserId);
|
|
static bool IsCallingPermissionValid(DataObsOption &opt);
|
|
static bool IsDataMgrService(uint32_t tokenId, int32_t uid);
|
|
static bool IsSystemAppWithAllowedProvider(uint32_t callerTokenId, uint64_t fullTokenId,
|
|
const std::string &callingFunc, const Uri &uri, bool isExtension);
|
|
int32_t RegisterObserverInner(const Uri &uri, sptr<IDataAbilityObserver> dataObserver, int32_t userId,
|
|
DataObsOption opt, bool isExtension);
|
|
std::pair<Status, std::string> GetUriPermission(Uri &uri, bool isRead, ObserverInfo &info);
|
|
int32_t VerifyDataShareExtension(Uri &uri, ObserverInfo &info);
|
|
int32_t VerifyDataSharePermission(Uri &uri, bool isRead, ObserverInfo &info);
|
|
int32_t ConstructRegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
|
|
uint32_t token, int32_t userId, int32_t pid);
|
|
Status VerifyDataSharePermissionInner(Uri &uri, bool isRead, ObserverInfo &info);
|
|
int32_t NotifyChangeInner(Uri &uri, int32_t userId,
|
|
DataObsOption opt, bool isExtension);
|
|
void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
|
|
int32_t CheckAndReportUriSizeFault(const Uri &uri, uint32_t tokenId, const std::string &callingFunc);
|
|
bool IsTaskOverLimit();
|
|
std::pair<Status, std::vector<NotifyInfo>> MakeNotifyInfos(ChangeInfo &changes, DataObsOption opt,
|
|
uint32_t tokenId, int32_t userId);
|
|
void SubmitNotifyChangeTask(Uri &uri, int32_t userId, std::string readPermission,
|
|
ObserverInfo &info);
|
|
void ReduceTaskCount();
|
|
private:
|
|
static constexpr std::uint32_t TASK_COUNT_MAX = 50;
|
|
static constexpr std::uint32_t URI_SIZE_THRESHOLD = 256;
|
|
ffrt::mutex taskCountMutex_;
|
|
std::uint32_t taskCount_ = 0;
|
|
std::shared_ptr<TaskHandlerWrap> handler_;
|
|
std::shared_ptr<DataShare::DataSharePermission> permission_;
|
|
DataObsServiceRunningState state_;
|
|
|
|
std::shared_ptr<DataObsMgrInner> dataObsMgrInner_;
|
|
std::shared_ptr<DataObsMgrInnerExt> dataObsMgrInnerExt_;
|
|
std::shared_ptr<DataObsMgrInnerPref> dataObsMgrInnerPref_;
|
|
};
|
|
} // namespace AAFwk
|
|
} // namespace OHOS
|
|
#endif // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
|