• 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 
16 #include "security_collector_subscriber_manager.h"
17 #include <cinttypes>
18 #include "security_collector_define.h"
19 #include "security_collector_log.h"
20 #include "data_collection.h"
21 
22 namespace OHOS::Security::SecurityCollector {
23 namespace {
24     constexpr int32_t MAX_APP_SUBSCRIBE_COUNT = 100;
25 }
26 
GetInstance()27 SecurityCollectorSubscriberManager &SecurityCollectorSubscriberManager::GetInstance()
28 {
29     static SecurityCollectorSubscriberManager instance;
30     return instance;
31 }
32 
GetExtraInfo()33 std::string SecurityCollectorSubscriberManager::CollectorListenner::GetExtraInfo()
34 {
35     if (subscriber_) {
36         return subscriber_->GetSecurityCollectorSubscribeInfo().GetEvent().extra;
37     }
38     return {};
39 }
40 
SecurityCollectorSubscriberManager()41 SecurityCollectorSubscriberManager::SecurityCollectorSubscriberManager()
42 {
43     handle_ = dlopen(SECURITY_GUARD_EVENT_FILTER_PATH, RTLD_LAZY);
44     if (handle_ != nullptr) {
45         eventFilter_ = reinterpret_cast<GetEventFilterFunc>(dlsym(handle_, "GetEventFilter"));
46     }
47     wrapperHandle_ = dlopen(SECURITY_GUARD_EVENT_WRAPPER_PATH, RTLD_LAZY);
48     if (wrapperHandle_ != nullptr) {
49         eventWrapper_ = reinterpret_cast<GetEventWrapperFunc>(dlsym(handle_, "GetEventWrapper"));
50     }
51 }
52 
~SecurityCollectorSubscriberManager()53 SecurityCollectorSubscriberManager::~SecurityCollectorSubscriberManager()
54 {
55     if (handle_ != nullptr) {
56         dlclose(handle_);
57         handle_ = nullptr;
58     }
59     if (wrapperHandle_ != nullptr) {
60         dlclose(wrapperHandle_);
61         wrapperHandle_ = nullptr;
62     }
63 }
64 
GetEventId()65 int64_t SecurityCollectorSubscriberManager::CollectorListenner::GetEventId()
66 {
67     if (subscriber_) {
68         return subscriber_->GetSecurityCollectorSubscribeInfo().GetEvent().eventId;
69     }
70     return {};
71 }
72 
OnNotify(const Event & event)73 void SecurityCollectorSubscriberManager::CollectorListenner::OnNotify(const Event &event)
74 {
75     SecurityCollectorSubscriberManager::GetInstance().NotifySubscriber(event);
76 }
77 
NotifySubscriber(const Event & event)78 void SecurityCollectorSubscriberManager::NotifySubscriber(const Event &event)
79 {
80     std::lock_guard<std::mutex> lock(collectorMutex_);
81     LOGD("publish event: eventid:%{public}" PRId64 ", version:%{public}s, extra:%{public}s",
82         event.eventId, event.version.c_str(), event.extra.c_str());
83     for (auto iter : event.eventSubscribes) {
84         LOGD("publish event: subscribe:%{public}s", iter.c_str());
85     }
86     const auto it = eventToSubscribers_.find(event.eventId);
87     if (it == eventToSubscribers_.end()) {
88         return;
89     }
90     for (const auto &subscriber : it->second) {
91         if (subscriber != nullptr) {
92             subscriber->OnChange(event);
93         }
94     }
95 }
96 
GetAppSubscribeCount(const std::string & appName)97 int32_t SecurityCollectorSubscriberManager::GetAppSubscribeCount(const std::string &appName)
98 {
99     int32_t count = 0;
100     for (const auto &element : eventToSubscribers_) {
101         const auto &subscribers = element.second;
102         count = std::count_if(subscribers.begin(), subscribers.end(), [appName] (const auto &subscriber) {
103             return subscriber->GetAppName() == appName;
104         });
105     }
106     LOGI("subcirbipt count, appName=%{private}s, count=%{public}d", appName.c_str(), count);
107     return count;
108 }
109 
GetAppSubscribeCount(const std::string & appName,int64_t eventId)110 int32_t SecurityCollectorSubscriberManager::GetAppSubscribeCount(const std::string &appName, int64_t eventId)
111 {
112     const auto &subscribers = eventToSubscribers_[eventId];
113     if (std::any_of(subscribers.begin(), subscribers.end(), [appName] (const auto &subscriber) {
114             return subscriber->GetAppName() == appName;
115         })) {
116         LOGI("subcirbipt count 1, appName=%{private}s, eventId:%{public}" PRId64, appName.c_str(), eventId);
117         return 1;
118     }
119     LOGI("subcirbipt count 0, appName=%{private}s, eventId:%{public}" PRId64, appName.c_str(), eventId);
120     return 0;
121 }
122 
FindEventIds(const sptr<IRemoteObject> & remote)123 std::set<int64_t> SecurityCollectorSubscriberManager::FindEventIds(const sptr<IRemoteObject> &remote)
124 {
125     std::set<int64_t> eventIds;
126     for (const auto &element : eventToSubscribers_) {
127         const auto &subscribers = element.second;
128         auto it = std::find_if(subscribers.begin(), subscribers.end(),
129             [remote] (const auto &subscriber) { return subscriber->GetRemote() == remote; });
130         if (it != subscribers.end()) {
131             LOGI("Find Event By Callback appName=%{private}s, eventId:%{public}" PRId64,
132                  (*it)->GetAppName().c_str(), element.first);
133             eventIds.emplace(element.first);
134         }
135     }
136     return eventIds;
137 }
138 
FindSecurityCollectorSubscribers(const sptr<IRemoteObject> & remote)139 auto SecurityCollectorSubscriberManager::FindSecurityCollectorSubscribers(const sptr<IRemoteObject> &remote)
140 {
141     std::set<std::shared_ptr<SecurityCollectorSubscriber>> subscribers;
142     for (const auto &element : eventToSubscribers_) {
143         auto it = std::find_if(element.second.begin(), element.second.end(),
144             [remote] (const auto &d) { return d->GetRemote() == remote; });
145         if (it != element.second.end()) {
146             LOGI("Find Event Listenner appName=%{private}s, eventId:%{public}" PRId64,
147                 (*it)->GetAppName().c_str(), element.first);
148             subscribers.emplace(*it);
149         }
150     }
151     return subscribers;
152 }
SubscribeCollector(const std::shared_ptr<SecurityCollectorSubscriber> & subscriber)153 bool SecurityCollectorSubscriberManager::SubscribeCollector(
154     const std::shared_ptr<SecurityCollectorSubscriber> &subscriber)
155 {
156     std::lock_guard<std::mutex> lock(collectorMutex_);
157     if (subscriber == nullptr) {
158         LOGE("subscriber is null");
159         return false;
160     }
161     std::string appName = subscriber->GetAppName();
162     int64_t eventId = subscriber->GetSecurityCollectorSubscribeInfo().GetEvent().eventId;
163     LOGI("appName:%{private}s, eventId:%{public}" PRId64, appName.c_str(), eventId);
164     if (GetAppSubscribeCount(appName) >= MAX_APP_SUBSCRIBE_COUNT) {
165         LOGE("Max count for app name:%{private}s", appName.c_str());
166         return false;
167     }
168     if (GetAppSubscribeCount(appName, eventId) > 0) {
169         LOGE("Already subscribed eventId:%{public}" PRId64, eventId);
170         return false;
171     }
172     if (collectorListenner_ == nullptr) {
173         collectorListenner_ = std::make_shared<SecurityCollectorSubscriberManager::CollectorListenner>(nullptr);
174     }
175     LOGI("Scheduling start collector, eventId:%{public}" PRId64, eventId);
176     if (!DataCollection::GetInstance().SubscribeCollectors(std::vector<int64_t>{eventId}, collectorListenner_)) {
177         LOGE("failed to start collectors");
178         return false;
179     }
180     eventToSubscribers_[eventId].emplace(subscriber);
181     LOGI("eventId:%{public} " PRId64 ", callbackCount:%{public}zu", eventId, eventToSubscribers_[eventId].size());
182     int64_t duration = subscriber->GetSecurityCollectorSubscribeInfo().GetDuration();
183     if (duration > 0) {
184         auto remote = subscriber->GetRemote();
185         auto timer = std::make_shared<CleanupTimer>();
186         timers_.emplace(remote, timer);
187         timer->Start(remote, duration);
188     }
189     return true;
190 }
191 
UnsubscribeCollector(const sptr<IRemoteObject> & remote)192 bool SecurityCollectorSubscriberManager::UnsubscribeCollector(const sptr<IRemoteObject> &remote)
193 {
194     std::lock_guard<std::mutex> lock(collectorMutex_);
195     std::set<int64_t> eventIds = FindEventIds(remote);
196     for (int64_t eventId : eventIds) {
197         LOGI("Remove collecctor, eventId:%{public}" PRId64, eventId);
198         if (eventId == -1) {
199             LOGE("eventId is not found");
200             return false;
201         }
202         auto subscribers = FindSecurityCollectorSubscribers(remote);
203         if (subscribers.size() == 0) {
204             LOGE("subscriber is null");
205             return false;
206         }
207         for (auto subscriber: subscribers) {
208             eventToSubscribers_[eventId].erase(subscriber);
209             if (eventToSubscribers_[eventId].size() == 0) {
210                 LOGI("Scheduling stop collector, eventId:%{public}" PRId64, eventId);
211                 if (!DataCollection::GetInstance().UnsubscribeCollectors(std::vector<int64_t>{eventId})) {
212                     LOGE("failed to stop collectors");
213                 }
214                 eventToSubscribers_.erase(eventId);
215             }
216         }
217     }
218 
219     LOGI("erase timer befoe remoteObject");
220     timers_.erase(remote);
221     LOGI("erase timer after remoteObject");
222     return true;
223 }
224 
AddFilter(const SecurityCollectorEventFilter & subscribeMute)225 int32_t SecurityCollectorSubscriberManager::AddFilter(const SecurityCollectorEventFilter &subscribeMute)
226 {
227     if (eventFilter_ == nullptr) {
228         LOGI("eventFilter_ is null");
229         return NULL_OBJECT;
230     }
231     int32_t ret = eventFilter_()->SetEventFilter(subscribeMute.GetMuteFilter());
232     if (ret != SUCCESS) {
233         LOGI("SetEventFilter failed, ret=%{public}d", ret);
234         return ret;
235     }
236     return SUCCESS;
237 }
238 
239 
RemoveFilter(const SecurityCollectorEventFilter & subscribeMute)240 int32_t SecurityCollectorSubscriberManager::RemoveFilter(const SecurityCollectorEventFilter &subscribeMute)
241 {
242     if (eventFilter_ == nullptr) {
243         LOGI("eventFilter_ is null");
244         return NULL_OBJECT;
245     }
246     int32_t ret = eventFilter_()->RemoveEventFilter(subscribeMute.GetMuteFilter());
247     if (ret != SUCCESS) {
248         LOGI("RemoveFilter failed, ret=%{public}d", ret);
249         return ret;
250     }
251     return SUCCESS;
252 }
253 
RemoveAllFilter()254 void SecurityCollectorSubscriberManager::RemoveAllFilter()
255 {
256     if (eventFilter_ == nullptr) {
257         LOGI("eventFilter_ is null");
258         return;
259     }
260     eventFilter_()->RemoveAllEventFilter();
261 }
262 }