• 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 "disallowed_usb_devices_plugin.h"
17 
18 #include <algorithm>
19 #include <system_ability_definition.h>
20 #include "array_usb_device_type_serializer.h"
21 #include "edm_constants.h"
22 #include "edm_ipc_interface_code.h"
23 #include "iplugin_manager.h"
24 #include "usb_policy_utils.h"
25 
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedUsbDevicesPlugin::GetPlugin());
29 constexpr int32_t USB_DEVICE_TYPE_BASE_CLASS_STORAGE = 8;
30 
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedUsbDevicesPlugin,std::vector<USB::UsbDeviceType>>> ptr)31 void DisallowedUsbDevicesPlugin::InitPlugin(
32     std::shared_ptr<IPluginTemplate<DisallowedUsbDevicesPlugin, std::vector<USB::UsbDeviceType>>> ptr)
33 {
34     EDMLOGI("DisallowedUsbDevicesPlugin InitPlugin...");
35     ptr->InitAttribute(EdmInterfaceCode::DISALLOWED_USB_DEVICES, "disallowed_usb_devices",
36         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_USB, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
37     ptr->SetSerializer(ArrayUsbDeviceTypeSerializer::GetInstance());
38     ptr->SetOnHandlePolicyListener(&DisallowedUsbDevicesPlugin::OnSetPolicy, FuncOperateType::SET);
39     ptr->SetOnHandlePolicyListener(&DisallowedUsbDevicesPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
40     ptr->SetOnAdminRemoveListener(&DisallowedUsbDevicesPlugin::OnAdminRemove);
41 }
42 
OnSetPolicy(std::vector<USB::UsbDeviceType> & data,std::vector<USB::UsbDeviceType> & currentData,std::vector<USB::UsbDeviceType> & mergeData,int32_t userId)43 ErrCode DisallowedUsbDevicesPlugin::OnSetPolicy(std::vector<USB::UsbDeviceType> &data,
44     std::vector<USB::UsbDeviceType> &currentData, std::vector<USB::UsbDeviceType> &mergeData, int32_t userId)
45 {
46     EDMLOGI("AllowUsbDevicesPlugin OnSetPolicy userId = %{public}d", userId);
47     if (data.empty()) {
48         EDMLOGW("AllowUsbDevicesPlugin OnSetPolicy data is empty");
49         return ERR_OK;
50     }
51     if (data.size() > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
52         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy data size=[%{public}zu] is too large", data.size());
53         return EdmReturnErrCode::PARAM_ERROR;
54     }
55     if (HasConflictPolicy()) {
56         return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
57     }
58 
59     std::vector<USB::UsbDeviceType> afterHandle =
60         ArrayUsbDeviceTypeSerializer::GetInstance()->SetUnionPolicyData(currentData, data);
61     std::vector<USB::UsbDeviceType> afterMerge =
62         ArrayUsbDeviceTypeSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
63 
64     if (afterMerge.size() > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
65         EDMLOGE("AllowUsbDevicesPlugin OnSetPolicy union data size=[%{public}zu] is too large", mergeData.size());
66         return EdmReturnErrCode::PARAM_ERROR;
67     }
68 
69     std::vector<USB::UsbDeviceType> disallowedUsbDeviceTypes;
70     CombineDataWithStorageAccessPolicy(afterMerge, disallowedUsbDeviceTypes);
71     ErrCode ret = UsbPolicyUtils::SetDisallowedUsbDevices(disallowedUsbDeviceTypes);
72     if (ret != ERR_OK) {
73         return ret;
74     }
75     currentData = afterHandle;
76     mergeData = afterMerge;
77     return ERR_OK;
78 }
79 
OnRemovePolicy(std::vector<USB::UsbDeviceType> & data,std::vector<USB::UsbDeviceType> & currentData,std::vector<USB::UsbDeviceType> & mergeData,int32_t userId)80 ErrCode DisallowedUsbDevicesPlugin::OnRemovePolicy(std::vector<USB::UsbDeviceType> &data,
81     std::vector<USB::UsbDeviceType> &currentData, std::vector<USB::UsbDeviceType> &mergeData, int32_t userId)
82 {
83     EDMLOGD("DisallowedUsbDevicesPlugin OnRemovePolicy userId : %{public}d:", userId);
84     if (data.empty()) {
85         EDMLOGW("DisallowedUsbDevicesPlugin OnRemovePolicy data is empty:");
86         return ERR_OK;
87     }
88     if (data.size() > EdmConstants::DISALLOWED_USB_DEVICES_TYPES_MAX_SIZE) {
89         EDMLOGE("DisallowedUsbDevicesPlugin OnRemovePolicy input data is too large");
90         return EdmReturnErrCode::PARAM_ERROR;
91     }
92 
93     std::vector<USB::UsbDeviceType> afterHandle =
94         ArrayUsbDeviceTypeSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
95     std::vector<USB::UsbDeviceType> afterMerge =
96         ArrayUsbDeviceTypeSerializer::GetInstance()->SetUnionPolicyData(mergeData, afterHandle);
97     std::vector<USB::UsbDeviceType> disallowedUsbDeviceTypes;
98     CombineDataWithStorageAccessPolicy(afterMerge, disallowedUsbDeviceTypes);
99     ErrCode ret = ERR_OK;
100     if (disallowedUsbDeviceTypes.empty() && !currentData.empty()) {
101         ret = UsbPolicyUtils::SetUsbDisabled(false);
102         if (ret != ERR_OK) {
103             return ret;
104         }
105     }
106     ret = UsbPolicyUtils::SetDisallowedUsbDevices(disallowedUsbDeviceTypes);
107     if (ret != ERR_OK) {
108         return ret;
109     }
110     currentData = afterHandle;
111     mergeData = afterMerge;
112     return ERR_OK;
113 }
114 
HasConflictPolicy()115 bool DisallowedUsbDevicesPlugin::HasConflictPolicy()
116 {
117     auto policyManager = IPolicyManager::GetInstance();
118     std::string disableUsb;
119     policyManager->GetPolicy("", "disable_usb", disableUsb);
120     if (disableUsb == "true") {
121         EDMLOGE("DisallowedUsbDevicesPlugin policy conflict! Usb is disabled.");
122         return true;
123     }
124     std::string allowUsbDevice;
125     policyManager->GetPolicy("", "allowed_usb_devices", allowUsbDevice);
126     if (!allowUsbDevice.empty()) {
127         EDMLOGE("DisallowedUsbDevicesPlugin policy conflict! AllowedUsbDevice: %{public}s", allowUsbDevice.c_str());
128         return true;
129     }
130     return false;
131 }
132 
CombineDataWithStorageAccessPolicy(std::vector<USB::UsbDeviceType> policyData,std::vector<USB::UsbDeviceType> & combineData)133 void DisallowedUsbDevicesPlugin::CombineDataWithStorageAccessPolicy(std::vector<USB::UsbDeviceType> policyData,
134     std::vector<USB::UsbDeviceType> &combineData)
135 {
136     auto policyManager = IPolicyManager::GetInstance();
137     std::string usbStoragePolicy;
138     policyManager->GetPolicy("", "usb_read_only", usbStoragePolicy);
139     std::vector<USB::UsbDeviceType> usbStorageTypes;
140     if (usbStoragePolicy == std::to_string(EdmConstants::STORAGE_USB_POLICY_DISABLED)) {
141         USB::UsbDeviceType storageType;
142         storageType.baseClass = USB_DEVICE_TYPE_BASE_CLASS_STORAGE;
143         storageType.subClass = USB_DEVICE_TYPE_BASE_CLASS_STORAGE;
144         storageType.protocol = USB_DEVICE_TYPE_BASE_CLASS_STORAGE;
145         storageType.isDeviceType = false;
146         usbStorageTypes.emplace_back(storageType);
147     }
148     combineData = ArrayUsbDeviceTypeSerializer::GetInstance()->SetUnionPolicyData(policyData, usbStorageTypes);
149 }
150 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)151 ErrCode DisallowedUsbDevicesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
152     int32_t userId)
153 {
154     EDMLOGI("DisallowedUsbDevicesPlugin OnGetPolicy: policyData: %{public}s", policyData.c_str());
155     if (policyData.empty()) {
156         EDMLOGW("DisallowedUsbDevicesPlugin OnGetPolicy data is empty:");
157         reply.WriteInt32(ERR_OK);
158         reply.WriteUint32(0);
159         return ERR_OK;
160     }
161     std::vector<USB::UsbDeviceType> disallowedDevices;
162     ArrayUsbDeviceTypeSerializer::GetInstance()->Deserialize(policyData, disallowedDevices);
163     reply.WriteInt32(ERR_OK);
164     reply.WriteUint32(disallowedDevices.size());
165     for (const auto &usbDeviceType : disallowedDevices) {
166         if (!usbDeviceType.Marshalling(reply)) {
167             EDMLOGE("DisallowedUsbDevicesPlugin OnGetPolicy: write parcel failed!");
168             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
169         }
170     }
171     return ERR_OK;
172 }
173 
OnAdminRemove(const std::string & adminName,std::vector<USB::UsbDeviceType> & data,std::vector<USB::UsbDeviceType> & mergeData,int32_t userId)174 ErrCode DisallowedUsbDevicesPlugin::OnAdminRemove(const std::string &adminName, std::vector<USB::UsbDeviceType> &data,
175     std::vector<USB::UsbDeviceType> &mergeData, int32_t userId)
176 {
177     EDMLOGD("DisallowedUsbDevicesPlugin OnAdminRemove");
178     std::vector<USB::UsbDeviceType> disallowedUsbDeviceTypes;
179     CombineDataWithStorageAccessPolicy(mergeData, disallowedUsbDeviceTypes);
180     if (disallowedUsbDeviceTypes.empty()) {
181         return UsbPolicyUtils::SetUsbDisabled(false);
182     }
183     return UsbPolicyUtils::SetDisallowedUsbDevices(disallowedUsbDeviceTypes);
184 }
185 } // namespace EDM
186 } // namespace OHOS
187