1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "sec_comp_kit.h"
16
17 #include "hisysevent.h"
18 #include "ipc_skeleton.h"
19 #include "sec_comp_caller_authorization.h"
20 #include "sec_comp_client.h"
21 #include "sec_comp_enhance_adapter.h"
22 #include "sec_comp_log.h"
23
24 namespace OHOS {
25 namespace Security {
26 namespace SecurityComponent {
27 namespace {
28 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompKit"};
29 } // namespace
30
RegisterSecurityComponent(SecCompType type,std::string & componentInfo,int32_t & scId)31 int32_t SecCompKit::RegisterSecurityComponent(SecCompType type,
32 std::string& componentInfo, int32_t& scId)
33 {
34 if (!SecCompCallerAuthorization::GetInstance().IsKitCaller(
35 reinterpret_cast<uintptr_t>(__builtin_return_address(0)))) {
36 SC_LOG_ERROR(LABEL, "register security component fail, caller invalid");
37 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED",
38 HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(),
39 "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "REGITSTER");
40 return SC_SERVICE_ERROR_CALLER_INVALID;
41 }
42
43 if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(componentInfo)) {
44 SC_LOG_ERROR(LABEL, "Preprocess security component fail");
45 return SC_ENHANCE_ERROR_VALUE_INVALID;
46 }
47
48 int32_t res = SecCompClient::GetInstance().RegisterSecurityComponent(type, componentInfo, scId);
49 if (res != SC_OK) {
50 SC_LOG_ERROR(LABEL, "register security component fail, error: %{public}d", res);
51 return res;
52 }
53 SecCompEnhanceAdapter::RegisterScIdEnhance(scId);
54 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "REGISTER_SUCCESS",
55 HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(),
56 "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId, "SC_TYPE", type);
57 return res;
58 }
59
UpdateSecurityComponent(int32_t scId,std::string & componentInfo)60 int32_t SecCompKit::UpdateSecurityComponent(int32_t scId, std::string& componentInfo)
61 {
62 if (!SecCompCallerAuthorization::GetInstance().IsKitCaller(
63 reinterpret_cast<uintptr_t>(__builtin_return_address(0)))) {
64 SC_LOG_ERROR(LABEL, "update security component fail, caller invalid");
65 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED",
66 HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(),
67 "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "UPDATE");
68 return SC_SERVICE_ERROR_CALLER_INVALID;
69 }
70
71 if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo)) {
72 SC_LOG_ERROR(LABEL, "Preprocess security component fail");
73 return SC_ENHANCE_ERROR_VALUE_INVALID;
74 }
75
76 int32_t res = SecCompClient::GetInstance().UpdateSecurityComponent(scId, componentInfo);
77 if (res != SC_OK) {
78 SC_LOG_ERROR(LABEL, "update security component fail, error: %{public}d", res);
79 }
80 return res;
81 }
82
UnregisterSecurityComponent(int32_t scId)83 int32_t SecCompKit::UnregisterSecurityComponent(int32_t scId)
84 {
85 int32_t res = SecCompClient::GetInstance().UnregisterSecurityComponent(scId);
86 SecCompEnhanceAdapter::UnregisterScIdEnhance(scId);
87 if (res != SC_OK) {
88 SC_LOG_ERROR(LABEL, "unregister security component fail, error: %{public}d", res);
89 return res;
90 }
91 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "UNREGISTER_SUCCESS",
92 HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CALLER_UID", IPCSkeleton::GetCallingUid(),
93 "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId);
94 return res;
95 }
96
ReportSecurityComponentClickEvent(int32_t scId,std::string & componentInfo,const SecCompClickEvent & touchInfo,sptr<IRemoteObject> callerToken)97 int32_t SecCompKit::ReportSecurityComponentClickEvent(int32_t scId,
98 std::string& componentInfo, const SecCompClickEvent& touchInfo, sptr<IRemoteObject> callerToken)
99 {
100 if (!SecCompCallerAuthorization::GetInstance().IsKitCaller(
101 reinterpret_cast<uintptr_t>(__builtin_return_address(0)))) {
102 SC_LOG_ERROR(LABEL, "report click event fail, caller invalid");
103 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CALLER_CHECK_FAILED",
104 HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(),
105 "CALLER_PID", IPCSkeleton::GetCallingPid(), "CALL_SCENE", "CLICK");
106 return SC_SERVICE_ERROR_CALLER_INVALID;
107 }
108
109 if (!SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo)) {
110 SC_LOG_ERROR(LABEL, "Preprocess security component fail");
111 return SC_ENHANCE_ERROR_VALUE_INVALID;
112 }
113
114 int32_t res =
115 SecCompClient::GetInstance().ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, callerToken);
116 if (res != SC_OK) {
117 SC_LOG_ERROR(LABEL, "report click event fail, error: %{public}d", res);
118 }
119 return res;
120 }
121
ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId)122 bool SecCompKit::ReduceAfterVerifySavePermission(AccessToken::AccessTokenID tokenId)
123 {
124 bool res =
125 SecCompClient::GetInstance().ReduceAfterVerifySavePermission(tokenId);
126 if (!res) {
127 SC_LOG_ERROR(LABEL, "verify temp save permission, error: %{public}d", res);
128 }
129 return res;
130 }
131
GetEnhanceRemoteObject(bool isLoad)132 sptr<IRemoteObject> SecCompKit::GetEnhanceRemoteObject(bool isLoad)
133 {
134 return SecCompClient::GetInstance().GetEnhanceRemoteObject(isLoad);
135 }
136 } // namespace SecurityComponent
137 } // namespace Security
138 } // namespace OHOS
139