• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "privacy_controller.h"
17 
18 #include "bundle_mgr_client.h"
19 #include "file_util.h"
20 #include "hiview_config_util.h"
21 #include "hiview_logger.h"
22 #include "plugin_factory.h"
23 #include "privacy_manager.h"
24 #include "string_util.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 REGISTER(PrivacyController);
30 DEFINE_LOG_TAG("PrivacyController");
31 constexpr uint8_t THROW_TYPE_EVENT = 1;
32 
IsPreInstallApp(const std::string & bundleName)33 bool IsPreInstallApp(const std::string& bundleName)
34 {
35     AppExecFwk::BundleInfo info;
36     AppExecFwk::BundleMgrClient client;
37     if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, info,
38         AppExecFwk::Constants::ALL_USERID)) {
39         return false;
40     }
41     return info.isPreInstallApp;
42 }
43 
GetAllowBundleNamesFromFile(const std::string & allowListFile,std::unordered_set<std::string> & bundleNames)44 void GetAllowBundleNamesFromFile(const std::string& allowListFile, std::unordered_set<std::string>& bundleNames)
45 {
46     std::string realPath;
47     const std::string configFilePath = HiViewConfigUtil::GetConfigFilePath("allow_list/" + allowListFile);
48     if (!FileUtil::PathToRealPath(configFilePath, realPath)) {
49         HIVIEW_LOGW("allow list path is invalid: %{public}s", configFilePath.c_str());
50         return;
51     }
52     std::vector<std::string> lines;
53     FileUtil::LoadLinesFromFile(realPath, lines);
54     std::for_each(lines.begin(), lines.end(), [&bundleNames](const std::string& line) {
55         bundleNames.insert(StringUtil::TrimStr(line)); // empty line is included as allowed
56     });
57     HIVIEW_LOGI("load allow list: %{public}s, bundles: %{public}zu", allowListFile.c_str(), bundleNames.size());
58 }
59 }
60 
IsBundleNameAllow(const std::string & bundleName,const std::string & allowListFile)61 bool PrivacyController::IsBundleNameAllow(const std::string& bundleName, const std::string& allowListFile)
62 {
63     if (bundleName.empty()) {
64         return true;
65     }
66     std::lock_guard<std::mutex> lock(bundleMapMutex_);
67     auto iter = allowBundleNameMap_.find(allowListFile);
68     if (iter == allowBundleNameMap_.end()) {
69         std::unordered_set<std::string> bundleNames;
70         GetAllowBundleNamesFromFile(allowListFile, bundleNames);
71         allowBundleNameMap_.insert(std::make_pair(allowListFile, bundleNames));
72         iter = allowBundleNameMap_.find(allowListFile);
73     }
74     if (iter != allowBundleNameMap_.end() && iter->second.find(bundleName) != iter->second.end()) {
75         return true;
76     }
77     // name of pre-installed bundle is always allowed
78     return IsPreInstallApp(bundleName);
79 }
80 
IsValidParam(std::shared_ptr<SysEvent> & sysEvent,const std::string & paramName,const std::string & allowListFile)81 bool PrivacyController::IsValidParam(std::shared_ptr<SysEvent>& sysEvent,
82     const std::string& paramName, const std::string& allowListFile)
83 {
84     std::vector<std::string> values;
85     if (sysEvent->GetEventStringArrayValue(paramName, values)) {
86         bool isNeedHideParam = false;
87         for (auto& value : values) {
88             if (!IsBundleNameAllow(value, allowListFile)) {
89                 value = "*";
90                 isNeedHideParam = true;
91             }
92         }
93         if (isNeedHideParam) {
94             sysEvent->SetEventValue(paramName, values);
95         }
96         return true;
97     } else {
98         return IsBundleNameAllow(sysEvent->GetEventValue(paramName), allowListFile);
99     }
100 }
101 
OnLoad()102 void PrivacyController::OnLoad()
103 {
104     HIVIEW_LOGI("load privacy controller.");
105 }
106 
OnEvent(std::shared_ptr<Event> & event)107 bool PrivacyController::OnEvent(std::shared_ptr<Event>& event)
108 {
109     auto sysEvent = std::static_pointer_cast<SysEvent>(event);
110     if (sysEvent == nullptr) {
111         return false;
112     }
113     PARAM_INFO_MAP_PTR invalidParams = sysEvent->GetInvalidParams();
114     if (invalidParams == nullptr || invalidParams->empty()) {
115         return true;
116     }
117     std::string droppedParam;
118     for (const auto& iter : *invalidParams) {
119         if (!sysEvent->IsParamExist(iter.first)) {
120             // param not exist in this event, no need check
121             continue;
122         }
123         if (iter.second != nullptr) {
124             if (IsValidParam(sysEvent, iter.first, iter.second->allowListFile)) {
125                 continue;
126             }
127             if (iter.second->throwType == THROW_TYPE_EVENT) {
128                 HIVIEW_LOGI("event[%{public}s|%{public}s] is not allowed",
129                     sysEvent->domain_.c_str(), sysEvent->eventName_.c_str());
130                 return sysEvent->OnFinish();
131             }
132         }
133         // remove the param from event and record it
134         sysEvent->RemoveParam(iter.first);
135         if (!droppedParam.empty()) {
136             droppedParam.append(",");
137         }
138         droppedParam.append(iter.first);
139     }
140     if (!droppedParam.empty()) {
141         sysEvent->SetEventValue("DroppedParam", droppedParam);
142     }
143     return true;
144 }
145 
OnConfigUpdate(const std::string & localCfgPath,const std::string & cloudCfgPath)146 void PrivacyController::OnConfigUpdate(const std::string& localCfgPath, const std::string& cloudCfgPath)
147 {
148     std::lock_guard<std::mutex> lock(bundleMapMutex_);
149     HIVIEW_LOGI("update bundle config");
150     allowBundleNameMap_.clear();
151 }
152 } // namespace HiviewDFX
153 } // namespace OHOS
154