1 /*
2 * Copyright (c) 2022 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
16 #include "hks_event_observer.h"
17
18 #include "common_event_support.h"
19 #include "rwlock.h"
20 #ifdef HAS_OS_ACCOUNT_PART
21 #include "os_account_manager.h"
22 #endif
23 #include "hks_client_service.h"
24 #include "hks_client_service_common.h"
25 #include "hks_log.h"
26 #include "hks_mem.h"
27 #include "hks_plugin_adapter.h"
28 #include "hks_type_inner.h"
29 #include "hks_template.h"
30 #include "hks_upgrade.h"
31 #include "hks_upgrade_lock.h"
32 #include "hks_report_data_size.h"
33
34 #include "securec.h"
35
36 #define USER_ID_ROOT "0"
37 #ifndef HAS_OS_ACCOUNT_PART
38 constexpr static int UID_TRANSFORM_DIVISOR = 200000;
GetOsAccountIdFromUid(int uid,int & osAccountId)39 static void GetOsAccountIdFromUid(int uid, int &osAccountId)
40 {
41 osAccountId = uid / UID_TRANSFORM_DIVISOR;
42 }
43 #endif // HAS_OS_ACCOUNT_PART
44
GetProcessInfo(int userId,int uid,struct HksProcessInfo * processInfo)45 static void GetProcessInfo(int userId, int uid, struct HksProcessInfo *processInfo)
46 {
47 uint32_t userSize = userId != 0 ? sizeof(userId) : strlen(USER_ID_ROOT);
48 uint8_t *userData = static_cast<uint8_t *>(HksMalloc(userSize));
49 HKS_IF_NULL_LOGE_RETURN_VOID(userData, "user id malloc failed.")
50 (void)memcpy_s(userData, userSize, userId == 0 ? USER_ID_ROOT : reinterpret_cast<const char *>(&userId), userSize);
51 processInfo->userId.size = userSize;
52 processInfo->userId.data = userData;
53 processInfo->userIdInt = userId;
54
55 uint32_t uidSize = sizeof(uid);
56 uint8_t *uidData = static_cast<uint8_t *>(HksMalloc(uidSize));
57 if (uidData == nullptr) {
58 HKS_LOG_E("uid malloc failed.");
59 HKS_FREE(userData);
60 processInfo->userId.data = nullptr;
61 return;
62 }
63 (void)memcpy_s(uidData, uidSize, &uid, uidSize);
64 processInfo->processName.size = uidSize;
65 processInfo->processName.data = uidData;
66 }
67
GetUserId(int userId,struct HksBlob * userIdBlob)68 static void GetUserId(int userId, struct HksBlob *userIdBlob)
69 {
70 uint32_t userIdSize = sizeof(userId);
71 uint8_t *userIdData = static_cast<uint8_t *>(HksMalloc(userIdSize));
72 HKS_IF_NULL_LOGE_RETURN_VOID(userIdData, "uid malloc failed.")
73 (void)memcpy_s(userIdData, userIdSize, &userId, userIdSize);
74 userIdBlob->size = userIdSize;
75 userIdBlob->data = userIdData;
76 }
77
78 namespace OHOS {
79 namespace Security {
80 namespace Hks {
81 std::shared_ptr<SystemEventSubscriber> SystemEventObserver::systemEventSubscriber_ = nullptr;
82 std::shared_ptr<SystemEventSubscriber> SystemEventObserver::backUpEventSubscriber_ = nullptr;
83 const static std::string RESTORE_EVENT_NAME = "usual.event.RESTORE_START";
84 const int32_t BACKUP_UID = 1089;
85
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)86 void SystemEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
87 {
88 struct HksProcessInfo processInfo = { { 0, nullptr }, { 0, nullptr } };
89
90 auto want = data.GetWant();
91 constexpr const char* UID = "uid";
92 std::string action = want.GetAction();
93
94 #ifdef HUKS_ENABLE_UPGRADE_KEY_STORAGE_SECURE_LEVEL
95 // judge whether is upgrading, wait for upgrade finished
96 HKS_IF_NOT_SUCC_LOGE_RETURN_VOID(HksWaitIfPowerOnUpgrading(), "wait on upgrading failed.")
97 HksUpgradeOrRequestLockRead();
98 #endif
99
100 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
101 action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
102 int uid = want.GetIntParam(UID, -1);
103 int userId = -1;
104 #ifdef HAS_OS_ACCOUNT_PART
105 OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
106 #else // HAS_OS_ACCOUNT_PART
107 GetOsAccountIdFromUid(uid, userId);
108 #endif // HAS_OS_ACCOUNT_PART
109 HKS_LOG_I("HksService package removed: uid is %" LOG_PUBLIC "d userId is %" LOG_PUBLIC "d", uid, userId);
110
111 GetProcessInfo(userId, uid, &processInfo);
112 HksServiceDeleteProcessInfo(&processInfo);
113 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
114 int userId = data.GetCode();
115 HKS_LOG_I("HksService user removed: userId is %" LOG_PUBLIC "d", userId);
116
117 GetUserId(userId, &(processInfo.userId));
118 HksServiceDeleteProcessInfo(&processInfo);
119 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
120 HKS_LOG_I("the credential-encrypted storage has become unlocked");
121 int userId = data.GetCode();
122 HKS_LOG_I("user %" LOG_PUBLIC "d unlocked.", userId);
123 HksUpgradeOnUserUnlock(userId);
124 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
125 HksSetScreenState(false);
126 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
127 HksSetScreenState(true);
128 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
129 int userId = data.GetCode();
130 ReportDataSizeEvent(userId);
131 }
132
133 #ifdef HUKS_ENABLE_UPGRADE_KEY_STORAGE_SECURE_LEVEL
134 HksUpgradeOrRequestUnlockRead();
135 #endif
136
137 HKS_FREE_BLOB(processInfo.userId);
138 HKS_FREE_BLOB(processInfo.processName);
139 HksPluginOnReceiveEvent(&data);
140 }
141
~SystemEventObserver()142 SystemEventObserver::~SystemEventObserver()
143 {
144 UnSubscribeEvent();
145 }
146
SubscribeSystemEvent()147 bool SystemEventObserver::SubscribeSystemEvent()
148 {
149 OHOS::EventFwk::MatchingSkills matchingSkills;
150 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
151 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
152 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
153 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
154 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
155 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
156 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
157 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
158 systemEventSubscriber_ = std::make_shared<SystemEventSubscriber>(subscriberInfo);
159
160 HKS_IF_NULL_LOGE_RETURN(systemEventSubscriber_, false, "huks system subscriber nullptr")
161
162 return OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(systemEventSubscriber_);
163 }
164
SubscribeBackUpEvent()165 bool SystemEventObserver::SubscribeBackUpEvent()
166 {
167 OHOS::EventFwk::MatchingSkills matchingSkills;
168 matchingSkills.AddEvent(RESTORE_EVENT_NAME);
169 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
170 subscriberInfo.SetPublisherUid(BACKUP_UID);
171 backUpEventSubscriber_ = std::make_shared<SystemEventSubscriber>(subscriberInfo);
172
173 HKS_IF_NULL_LOGE_RETURN(backUpEventSubscriber_, false, "huks Backup subscriber nullptr")
174
175 return OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(backUpEventSubscriber_);
176 }
177
SubscribeEvent()178 bool SystemEventObserver::SubscribeEvent()
179 {
180 return SubscribeSystemEvent() && SubscribeBackUpEvent();
181 }
182
DoUnSubscribe(std::shared_ptr<SystemEventSubscriber> subscriber)183 bool SystemEventObserver::DoUnSubscribe(std::shared_ptr<SystemEventSubscriber> subscriber)
184 {
185 HKS_IF_NULL_LOGE_RETURN(subscriber, false, "huks system subscriber nullptr");
186 return OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
187 }
188
UnSubscribeEvent()189 bool SystemEventObserver::UnSubscribeEvent()
190 {
191 return DoUnSubscribe(systemEventSubscriber_) && DoUnSubscribe(backUpEventSubscriber_);
192 }
193 } // namespace Hks
194 } // namespace Security
195 } // namespace OHOS
196