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_entity.h"
16
17 #include <chrono>
18 #include "bundle_mgr_client.h"
19 #include "datashare_helper.h"
20 #include "hisysevent.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "i_sec_comp_service.h"
24 #include "sec_comp_err.h"
25 #include "sec_comp_enhance_adapter.h"
26 #include "sec_comp_info_helper.h"
27 #include "sec_comp_log.h"
28 #include "window_info_helper.h"
29
30 namespace OHOS {
31 namespace Security {
32 namespace SecurityComponent {
33 namespace {
34 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntity"};
35 static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000000L; // 1000ms
36 static constexpr uint64_t TIME_CONVERSION_UNIT = 1000;
37 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
38 constexpr const char *SETTINGS_DATASHARE_URI =
39 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
40 constexpr const char *SETTINGS_DATASHARE_SEARCH_URI =
41 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?" \
42 "Proxy=true&key=accessibility_screenreader_enabled";
43 constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD";
44 constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE";
45 constexpr const char *QUERY_KEYWORD = "accessibility_screenreader_enabled";
46 static bool IsScreenReadMode();
47 }
48
GrantTempPermission()49 int32_t SecCompEntity::GrantTempPermission()
50 {
51 isGrant_ = true;
52 return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_);
53 }
54
CompareComponentBasicInfo(SecCompBase * other,bool isRectCheck) const55 bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other, bool isRectCheck) const
56 {
57 return componentInfo_->CompareComponentBasicInfo(other, isRectCheck);
58 }
59
CheckPointEvent(const SecCompClickEvent & clickInfo) const60 int32_t SecCompEntity::CheckPointEvent(const SecCompClickEvent& clickInfo) const
61 {
62 auto current = static_cast<uint64_t>(
63 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;
64 if (clickInfo.point.timestamp < current - MAX_TOUCH_INTERVAL || clickInfo.point.timestamp > current) {
65 SC_LOG_ERROR(LABEL, "touch timestamp invalid clickInfo. timestamp: %{public}llu, current: %{public}llu",
66 static_cast<unsigned long long>(clickInfo.point.timestamp), static_cast<unsigned long long>(current));
67 return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
68 }
69
70 if (!componentInfo_->rect_.IsInRect(clickInfo.point.touchX, clickInfo.point.touchY)) {
71 SC_LOG_ERROR(LABEL, "touch point is not in component rect, %{public}lf, %{public}lf",
72 clickInfo.point.touchX, clickInfo.point.touchY);
73 return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
74 }
75 return SC_OK;
76 }
77
CheckKeyEvent(const SecCompClickEvent & clickInfo) const78 int32_t SecCompEntity::CheckKeyEvent(const SecCompClickEvent& clickInfo) const
79 {
80 auto current = static_cast<uint64_t>(
81 std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;
82 if (clickInfo.key.timestamp < current - MAX_TOUCH_INTERVAL || clickInfo.key.timestamp > current) {
83 SC_LOG_ERROR(LABEL, "keyboard timestamp invalid clickInfo. timestamp: %{public}llu, current: %{public}llu",
84 static_cast<unsigned long long>(clickInfo.key.timestamp), static_cast<unsigned long long>(current));
85 return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
86 }
87 if ((clickInfo.key.keyCode != KEY_SPACE) && (clickInfo.key.keyCode != KEY_ENTER) &&
88 (clickInfo.key.keyCode != KEY_NUMPAD_ENTER)) {
89 SC_LOG_ERROR(LABEL, "keyboard keyCode invalid. keyCode: %{public}d", clickInfo.key.keyCode);
90 return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
91 }
92
93 return SC_OK;
94 }
95
CheckClickInfo(const SecCompClickEvent & clickInfo) const96 int32_t SecCompEntity::CheckClickInfo(const SecCompClickEvent& clickInfo) const
97 {
98 if (!WindowInfoHelper::CheckOtherWindowCoverComp(componentInfo_->windowId_,
99 componentInfo_->rect_)) {
100 SC_LOG_ERROR(LABEL, "Component may be covered by other window");
101 return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
102 }
103
104 int32_t res = SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
105 bool isScreenReadMode = IsScreenReadMode();
106 if (clickInfo.type == ClickEventType::POINT_EVENT_TYPE && !isScreenReadMode) {
107 res = CheckPointEvent(clickInfo);
108 } else if (clickInfo.type == ClickEventType::POINT_EVENT_TYPE && isScreenReadMode) {
109 SC_LOG_WARN(LABEL, "Device is in screen read mode, skip event check.");
110 return SC_OK;
111 } else if (clickInfo.type == ClickEventType::KEY_EVENT_TYPE) {
112 res = CheckKeyEvent(clickInfo);
113 }
114 if (res != SC_OK) {
115 return res;
116 }
117
118 res = SecCompEnhanceAdapter::CheckExtraInfo(clickInfo);
119 if (res == SC_SERVICE_ERROR_CLICK_EVENT_INVALID) {
120 SC_LOG_ERROR(LABEL, "Click ExtraInfo is invalid");
121 return res;
122 }
123
124 if ((res != SC_OK) && (res != SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE)) {
125 SC_LOG_ERROR(LABEL, "HMAC checkout failed");
126 int32_t uid = IPCSkeleton::GetCallingUid();
127 OHOS::AppExecFwk::BundleMgrClient bmsClient;
128 std::string bundleName = "";
129 bmsClient.GetNameForUid(uid, bundleName);
130 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED",
131 HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", uid, "CALLER_BUNDLE_NAME", bundleName,
132 "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId_, "SC_TYPE", componentInfo_->type_);
133 return SC_ENHANCE_ERROR_CLICK_EXTRA_CHECK_FAIL;
134 }
135 return SC_OK;
136 }
137
138 namespace {
IsScreenReadMode()139 static bool IsScreenReadMode()
140 {
141 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
142 if (saManager == nullptr) {
143 SC_LOG_ERROR(LABEL, "Get sa manager is nullptr.");
144 return false;
145 }
146 auto remoteObj = saManager->GetSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE);
147 if (remoteObj == nullptr) {
148 SC_LOG_ERROR(LABEL, "Get remoteObj is nullptr.");
149 return false;
150 }
151 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper =
152 DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATASHARE_URI, SETTINGS_DATA_EXT_URI);
153 if (dataShareHelper == nullptr) {
154 SC_LOG_ERROR(LABEL, "Get dataShareHelper is nullptr.");
155 return false;
156 }
157 DataShare::DataSharePredicates predicates;
158 std::vector<std::string> columns;
159 predicates.EqualTo(ADVANCED_DATA_COLUMN_KEYWORD, QUERY_KEYWORD);
160 OHOS::Uri uri(SETTINGS_DATASHARE_SEARCH_URI);
161 auto result = dataShareHelper->Query(uri, predicates, columns);
162 if (result == nullptr) {
163 SC_LOG_ERROR(LABEL, "Query result is nullptr.");
164 dataShareHelper->Release();
165 return false;
166 }
167 if (result->GoToFirstRow() != DataShare::E_OK) {
168 SC_LOG_ERROR(LABEL, "Query failed, GoToFirstRow error.");
169 result->Close();
170 dataShareHelper->Release();
171 return false;
172 }
173 int32_t columnIndex;
174 result->GetColumnIndex(ADVANCED_DATA_COLUMN_VALUE, columnIndex);
175 std::string value;
176 result->GetString(columnIndex, value);
177 result->Close();
178 dataShareHelper->Release();
179 return value == "1";
180 }
181 }
182 } // namespace SecurityComponent
183 } // namespace Security
184 } // namespace OHOS
185