• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hisysevent.h"
19 #include "ipc_skeleton.h"
20 #include "sec_comp_err.h"
21 #include "sec_comp_enhance_adapter.h"
22 #include "sec_comp_info_helper.h"
23 #include "sec_comp_log.h"
24 #include "window_info_helper.h"
25 
26 namespace OHOS {
27 namespace Security {
28 namespace SecurityComponent {
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEntity"};
31 static constexpr uint64_t MAX_TOUCH_INTERVAL = 1000000L; // 1000ms
32 static constexpr uint64_t TIME_CONVERSION_UNIT = 1000;
33 }
34 
GrantTempPermission()35 int32_t SecCompEntity::GrantTempPermission()
36 {
37     isGrant_ = true;
38     return SecCompInfoHelper::GrantTempPermission(tokenId_, componentInfo_);
39 }
40 
CompareComponentBasicInfo(SecCompBase * other,bool isRectCheck) const41 bool SecCompEntity::CompareComponentBasicInfo(SecCompBase* other, bool isRectCheck) const
42 {
43     return componentInfo_->CompareComponentBasicInfo(other, isRectCheck);
44 }
45 
CheckPointEvent(const SecCompClickEvent & clickInfo) const46 int32_t SecCompEntity::CheckPointEvent(const SecCompClickEvent& clickInfo) const
47 {
48     auto current = static_cast<uint64_t>(
49         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;
50     if (clickInfo.point.timestamp < current - MAX_TOUCH_INTERVAL || clickInfo.point.timestamp > current) {
51         SC_LOG_ERROR(LABEL, "touch timestamp invalid clickInfo. timestamp: %{public}llu, current: %{public}llu",
52             static_cast<unsigned long long>(clickInfo.point.timestamp), static_cast<unsigned long long>(current));
53         return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
54     }
55 
56     if (!componentInfo_->rect_.IsInRect(clickInfo.point.touchX, clickInfo.point.touchY)) {
57         SC_LOG_ERROR(LABEL, "touch point is not in component rect, %{public}lf, %{public}lf",
58             clickInfo.point.touchX, clickInfo.point.touchY);
59         return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
60     }
61     return SC_OK;
62 }
63 
CheckKeyEvent(const SecCompClickEvent & clickInfo) const64 int32_t SecCompEntity::CheckKeyEvent(const SecCompClickEvent& clickInfo) const
65 {
66     auto current = static_cast<uint64_t>(
67         std::chrono::high_resolution_clock::now().time_since_epoch().count()) / TIME_CONVERSION_UNIT;
68     if (clickInfo.key.timestamp < current - MAX_TOUCH_INTERVAL || clickInfo.key.timestamp > current) {
69         SC_LOG_ERROR(LABEL, "keyboard timestamp invalid clickInfo. timestamp: %{public}llu, current: %{public}llu",
70             static_cast<unsigned long long>(clickInfo.key.timestamp), static_cast<unsigned long long>(current));
71         return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
72     }
73     if ((clickInfo.key.keyCode != KEY_SPACE) && (clickInfo.key.keyCode != KEY_ENTER)) {
74         SC_LOG_ERROR(LABEL, "keyboard keyCode invalid. keyCode: %{public}d", clickInfo.key.keyCode);
75         return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
76     }
77 
78     return SC_OK;
79 }
80 
CheckClickInfo(const SecCompClickEvent & clickInfo) const81 int32_t SecCompEntity::CheckClickInfo(const SecCompClickEvent& clickInfo) const
82 {
83     if (!WindowInfoHelper::CheckOtherWindowCoverComp(componentInfo_->windowId_,
84         componentInfo_->rect_)) {
85         SC_LOG_ERROR(LABEL, "SecurityComponentCheckFail: Component may be covered by other window");
86         return SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
87     }
88 
89     int32_t res = SC_SERVICE_ERROR_CLICK_EVENT_INVALID;
90     if (clickInfo.type == ClickEventType::POINT_EVENT_TYPE) {
91         res = CheckPointEvent(clickInfo);
92     } else if (clickInfo.type == ClickEventType::KEY_EVENT_TYPE) {
93         res = CheckKeyEvent(clickInfo);
94     }
95     if (res != SC_OK) {
96         return res;
97     }
98 
99     res = SecCompEnhanceAdapter::CheckExtraInfo(clickInfo);
100     if ((res != SC_OK) && (res != SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE)) {
101         SC_LOG_ERROR(LABEL, "HMAC checkout failed");
102         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SEC_COMPONENT, "CLICK_INFO_CHECK_FAILED",
103             HiviewDFX::HiSysEvent::EventType::SECURITY, "CALLER_UID", IPCSkeleton::GetCallingUid(),
104             "CALLER_PID", IPCSkeleton::GetCallingPid(), "SC_ID", scId_, "SC_TYPE", componentInfo_->type_);
105         return SC_ENHANCE_ERROR_CLICK_EXTRA_CHECK_FAIL;
106     }
107     return SC_OK;
108 }
109 }  // namespace SecurityComponent
110 }  // namespace Security
111 }  // namespace OHOS
112