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