From 13fb071ec347a0aa20d2d6db23c37d9e433364e6 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 16:15:53 +0800 Subject: [PATCH 01/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- interfaces/innerkits/IUsbServer.idl | 1 + interfaces/innerkits/UsbServerTypes.idl | 5 + .../innerkits/native/include/usb_device.h | 18 ++ .../innerkits/native/include/usb_interface.h | 11 ++ .../innerkits/native/include/usb_srv_client.h | 2 + .../innerkits/native/src/usb_srv_client.cpp | 26 +++ services/native/include/usb_host_manager.h | 4 + services/native/include/usb_service.h | 3 + services/native/src/usb_host_manager.cpp | 166 +++++++++++------- services/native/src/usb_service.cpp | 28 ++- 10 files changed, 199 insertions(+), 65 deletions(-) diff --git a/interfaces/innerkits/IUsbServer.idl b/interfaces/innerkits/IUsbServer.idl index bdc4b14d..4ca3fad7 100755 --- a/interfaces/innerkits/IUsbServer.idl +++ b/interfaces/innerkits/IUsbServer.idl @@ -34,6 +34,7 @@ interface OHOS.USB.IUsbServer { [macrodef USB_MANAGER_FEATURE_HOST] void SetActiveConfig([in]unsigned char busNum, [in]unsigned char devAddr, [in]unsigned char configId); [macrodef USB_MANAGER_FEATURE_HOST] void ManageGlobalInterface([in]boolean disable); [macrodef USB_MANAGER_FEATURE_HOST] void ManageDevice([in]int vendorId, [in]int productId, [in]boolean disable); + [macrodef USB_MANAGER_FEATURE_HOST] void ManageDevicePolicy([in]UsbDeviceIdInfo[] whiteList); [macrodef USB_MANAGER_FEATURE_HOST] void ManageInterfaceType([in]UsbDeviceTypeInfo[] disableType, [in]boolean disable); [macrodef USB_MANAGER_FEATURE_HOST] void UsbAttachKernelDriver([in]unsigned char busNum, [in]unsigned char devAddr, [in]unsigned char interfaceid); [macrodef USB_MANAGER_FEATURE_HOST] void UsbDetachKernelDriver([in]unsigned char busNum, [in]unsigned char devAddr, [in]unsigned char interfaceid); diff --git a/interfaces/innerkits/UsbServerTypes.idl b/interfaces/innerkits/UsbServerTypes.idl index 440d7d84..b151eef2 100755 --- a/interfaces/innerkits/UsbServerTypes.idl +++ b/interfaces/innerkits/UsbServerTypes.idl @@ -39,3 +39,8 @@ int protocol; boolean isDeviceType; }; + + struct UsbDeviceIdInfo { + int productId; + int vendorId; + }; diff --git a/interfaces/innerkits/native/include/usb_device.h b/interfaces/innerkits/native/include/usb_device.h index 40a3b7fd..bc759dc8 100644 --- a/interfaces/innerkits/native/include/usb_device.h +++ b/interfaces/innerkits/native/include/usb_device.h @@ -30,6 +30,13 @@ namespace OHOS { namespace USB { constexpr uint32_t USB_CONFIG_MAX_NUM = 128; + +enum AuthorizeStatus { + DISABLED = 0, + ENABLED = 1, + NEW_ARRIVED = 2, +} + class UsbDevice : public Parcelable { public: UsbDevice(std::string name, std::string manufacturerName, std::string productName, std::string version, @@ -410,6 +417,16 @@ public: return this->bcdDevice_; } + AuthorizeStatus GetAuthorizeStatus() + { + return this->authorizeStatus_; + } + + void SetAuthorizeStatus(AuthorizeStatus authorized) + { + this->authorizeStatus_ = authorized; + } + const std::string getJsonString() const { cJSON* device = cJSON_CreateObject(); @@ -457,6 +474,7 @@ private: std::string productName_; std::string version_; std::string serial_; + AuthorizeStatus authorizeStatus_ = ENABLED; uint8_t devAddr_ = UINT8_MAX; uint8_t busNum_ = UINT8_MAX; uint8_t descConfigCount_ = UINT8_MAX; diff --git a/interfaces/innerkits/native/include/usb_interface.h b/interfaces/innerkits/native/include/usb_interface.h index f2a17cf9..a29b6038 100644 --- a/interfaces/innerkits/native/include/usb_interface.h +++ b/interfaces/innerkits/native/include/usb_interface.h @@ -255,6 +255,16 @@ public: return this->iInterface_; } + bool GetAuthorizeStatus() + { + return authorized_; + } + + void SetAuthorizeStatus(bool authorized) + { + authorized_ = authorized; + } + const std::string getJsonString() const { cJSON* interface = cJSON_CreateObject(); @@ -300,6 +310,7 @@ private: std::string name_; std::vector endpoints_; uint8_t iInterface_ = UINT8_MAX; + bool authorized_ = true; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index 102f2b15..3df467a5 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -99,6 +99,7 @@ public: int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName); int32_t ManageGlobalInterface(bool disable); int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); + int32_t ManageDevicePolicy(std::vector &whiteList); int32_t ManageInterfaceType(const std::vector &disableType, bool disable); int32_t ClearHalt(USBDevicePipe &pipe, const USBEndpoint &endpoint); int32_t AddAccessoryRight(const uint32_t tokenId, const USBAccessory &access); @@ -131,6 +132,7 @@ private: void UsbCtrlTransferChange(const HDI::Usb::V1_0::UsbCtrlTransfer ¶m, UsbCtlSetUp &ctlSetup); void UsbCtrlTransferChange(const HDI::Usb::V1_2::UsbCtrlTransferParams ¶m, UsbCtlSetUp &ctlSetup); void UsbTransInfoChange(const HDI::Usb::V1_2::USBTransferInfo ¶m, UsbTransInfo &info); + void UsbDeviceIdChange(const std:: &deviceIdList, std::vector &deviceIdInfoList); class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { public: UsbSrvDeathRecipient() = default; diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index a9c5c4d8..8cd8a6f2 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -524,6 +524,31 @@ int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool dis } return ret; } + +void UsbSrvClient::UsbDeviceIdChange(const std:: &deviceIdList, + std::vector &deviceIdInfoList) +{ + for (auto &deviceId : deviceIdList) { + UsbDeviceIdInfo info; + info.productId = deviceId.productId; + info.vendorId = deviceId.vendorId; + deviceIdInfoList.emplace_back(info); + } + return; +} + +int32_t UsbSrvClient::ManageDevicePolicy(std::vector &whiteList) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT); + std::vector deviceIdInfoList{}; + UsbDeviceIdChange(whiteList, deviceIdInfoList); + int32_t ret = proxy_->ManageDevicePolicy(deviceIdInfoList); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret); + } + return ret; +} + void UsbSrvClient::UsbDeviceTypeChange(const std::vector &disableType, std::vector &deviceTypes) { @@ -537,6 +562,7 @@ void UsbSrvClient::UsbDeviceTypeChange(const std::vector &disable } return; } + int32_t UsbSrvClient::ManageInterfaceType(const std::vector &disableType, bool disable) { RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT); diff --git a/services/native/include/usb_host_manager.h b/services/native/include/usb_host_manager.h index a6a3799b..07d72294 100644 --- a/services/native/include/usb_host_manager.h +++ b/services/native/include/usb_host_manager.h @@ -73,10 +73,14 @@ public: int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex); int32_t ManageGlobalInterface(bool disable); int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); + int32_t ManageDevicePolicy(std::vector &whiteList); int32_t ManageInterfaceType(const std::vector &disableType, bool disable); int32_t UsbAttachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); int32_t UsbDetachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); int32_t ClearHalt(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointId); + int32_t UsbDeviceAuthorize(uint8_t busNum, uint8_t devAddr, bool authorized, const std::string &operationType); + int32_t UsbInterfaceAuthorize( + const HDI::Usb::V1_0::UsbDev &dev, uint8_t configId, uint8_t interfaceId, bool authorized); int32_t GetDevices(std::vector &deviceList); int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev); diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 5ef7b4d4..61b66be3 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -102,6 +102,7 @@ public: int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override; int32_t ManageGlobalInterface(bool disable) override; int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) override; + int32_t ManageDevicePolicy(const std::vector &whilteList) override; int32_t ManageInterfaceType(const std::vector &devTypeInfo, bool disable) override; int32_t UsbAttachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; int32_t UsbDetachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; @@ -272,6 +273,8 @@ private: bool GetBundleInfo(std::string &tokenId, int32_t &userId); void UsbCtrlTransferChange(HDI::Usb::V1_0::UsbCtrlTransfer ¶m, const UsbCtlSetUp &ctlSetup); void UsbCtrlTransferChange(HDI::Usb::V1_2::UsbCtrlTransferParams ¶m, const UsbCtlSetUp &ctlSetup); + void UsbDeviceIdChange(const std::vector &deviceIdInfoList, + std::vector &deviceIdList); void UsbDeviceTypeChange(std::vector &disableType, const std::vector &deviceTypes); void UsbTransInfoChange(HDI::Usb::V1_2::USBTransferInfo &info, const UsbTransInfo ¶m); diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 6226685d..415c75d3 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -380,6 +380,11 @@ int32_t UsbHostManager::ManageDevice(int32_t vendorId, int32_t productId, bool d return ManageDeviceImpl(vendorId, productId, disable); } +int32_t UsbHostManager::ManageDevicePolicy(std::vector &whiteList) +{ + return ExecuteManageDevicePolicy(whiteList); +} + int32_t UsbHostManager::ManageInterfaceType(const std::vector &disableType, bool disable) { return ExecuteManageInterfaceType(disableType, disable); @@ -451,7 +456,8 @@ int32_t UsbHostManager::GetDevices(std::vector &deviceList) USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu", devices_.size()); bool isSystemAppOrSa = usbRightManager_->IsSystemAppOrSa(); for (auto it = devices_.begin(); it != devices_.end(); ++it) { - if (it->second->GetClass() == BASE_CLASS_HUB && !isSystemAppOrSa) { + if ((it->second->GetClass() == BASE_CLASS_HUB && !isSystemAppOrSa) || + it->second->GetAuthorizeStatus() != ENABLED) { continue; } auto dev = UsbDevice(*it->second); @@ -1094,9 +1100,12 @@ bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum) return false; } - auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED, *devOld); - if (!isSuccess) { - USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed"); + if (devOld->GetAuthorizeStatus() == ENABLED) { + // if enabled, then broadcast common event; o.w. dev is already unseen + auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED, *devOld); + if (!isSuccess) { + USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed"); + } } delete devOld; @@ -1132,9 +1141,19 @@ bool UsbHostManager::AddDevice(UsbDevice *dev) USB_HILOGI(MODULE_SERVICE, "device:%{public}s bus:%{public}hhu dev:%{public}hhu insert, cur device size: %{public}zu", name.c_str(), busNum, devNum, devices_.size()); - auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED, *dev); - if (!isSuccess) { - USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed"); + + // will update disable status in ExecuteStrategy + dev->SetAuthorizeStatus(NEW_ARRIVED); + ExecuteStrategy(dev); + + if (dev->GetAuthorizeStatus() == DISABLED) { + USB_HILOGI(MODULE_SERVICE, "device is disallowed by EDM, skip common event broadcast"); + } else { + dev->SetAuthorizeStatus(ENABLED); + auto isSuccess = PublishCommonEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED, *dev); + if (!isSuccess) { + USB_HILOGW(MODULE_SERVICE, "send device attached broadcast failed"); + } } return true; } @@ -1393,6 +1412,62 @@ bool UsbHostManager::IsEdmEnabled() return edmParaValue == "true"; } +int32_t UsbHostManager::UsbDeviceAuthorize( + uint8_t busNum, uint8_t devAddr, bool authorized, const std::string &operationType) +{ + USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize: set authorized=%{public}d, operationType=%{public}s", + int(authorized), operationType.c_str()); + std::string name = std::to_string(busNum) + "-" + std::to_string(devAddr); + auto iterDev = devices_.find(name); + if (iterDev == devices_.end()) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceAuthorize: dev %{public}s not found", name.c_str()); + return UEC_SERVICE_INVALID_VALUE; + } + auto authorizeStatus = iterDev->second->GetAuthorizeStatus(); + if ((authorized && authorizeStatus != DISABLED) || (!authorized && authorizeStatus == DISABLED)) { + USB_HILOGI(MODULE_USB_SERVICE, "no need to change dev %{public}s authorize state", name.c_str()); + return UEC_OK; + } + + UsbDev dev = {busNum, devAddr}; + USBConfig config; + uint8_t configIndex = 0; + if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1) || + it->second->GetConfig(static_cast(configIndex) - 1, config)) { + USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); + return UEC_SERVICE_INVALID_VALUE; + } + for (auto &interface : config.GetInterfaces()) { + interface.SetAuthorizeStatus(authorized); + (void)ManageInterface(dev, interface.GetId(), !authorized); + std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); + } + + if (!authorized) { + ReportManageDeviceInfo(operationType, iterDev->second, nullptr, false); + } + if (authorizedStatus != NEW_ARRIVED) { // skip for newly arrived device here (send in AddDevice if not disabled) + auto eventType = authorized? CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED : + CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED; + auto isSuccess = PublishCommonEvent(eventType, *iterDev->second); + if (!isSuccess) { + USB_HILOGW(MODULE_SERVICE, "send device attached/detached broadcast failed"); + } + } + iterDev->second->SetAuthorizeStatus(authorized? ENABLED : DISABLED); // authorized==true -> ENABLED + std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); + return UEC_OK; +} + +int32_t UsbHostManager::UsbInterfaceAuthorize( + const HDI::Usb::V1_0::UsbDev &dev, uint8_t configId, uint8_t interfaceId, bool authorized) +{ + USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize: set authorized=%{public}d", int(authorized)); + auto ret = ManageInterface(dev, interfaceId, !authorized); + std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); + return ret; +} + int32_t UsbHostManager::ExecuteManageDevicePolicy(std::vector &whiteList) { int32_t ret = UEC_INTERFACE_NO_MEMORY; @@ -1405,7 +1480,7 @@ int32_t UsbHostManager::ExecuteManageDevicePolicy(std::vector &whit break; } } - if (inWhiteList) { + if (inWhiteList || whiteList.empty()) { ret = ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), false); } else { ret = ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), true); @@ -1710,32 +1785,14 @@ int32_t UsbHostManager::ManageGlobalInterfaceImpl(bool disable) if (it->second->GetClass() == BASE_CLASS_HUB) { continue; } - UsbDev dev = {it->second->GetBusNum(), it->second->GetDevAddr()}; - uint8_t configIndex = 0; - if (OpenDevice(dev.busNum, dev.devAddr) != UEC_OK) { - USB_HILOGW(MODULE_USB_SERVICE, "ManageGlobalInterfaceImpl OpenDevice failed"); - continue; - } - if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1)) { - USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); - Close(dev.busNum, dev.devAddr); - continue; - } - USBConfig configs; - if (it->second->GetConfig(static_cast(configIndex) - 1, configs)) { - USB_HILOGW(MODULE_USB_SERVICE, "get device config info failed."); - Close(dev.busNum, dev.devAddr); + int32_t ret = OpenDevice(it->second->GetBusNum(), it->second->GetDevAddr()); + if (ret != UEC_OK) { + USB_HILOGW(MODULE_USB_SERVICE, "ManageGlobalInterfaceImpl open fail ret = %{public}d", ret); continue; } + ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "GlobalType"); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); - std::vector interfaces = configs.GetInterfaces(); - for (uint32_t i = 0; i < interfaces.size(); i++) { - ManageInterface(dev, interfaces[i].GetId(), disable); - std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); - } - if (disable) { - ReportManageDeviceInfo("GlobalType", it->second, nullptr, false); - } if (Close(dev.busNum, dev.devAddr) != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageGlobalInterfaceImpl CloseDevice fail"); } @@ -1752,31 +1809,13 @@ int32_t UsbHostManager::ManageDeviceImpl(int32_t vendorId, int32_t productId, bo continue; } if ((it->second->GetVendorId() == vendorId) && (it->second->GetProductId() == productId)) { - UsbDev dev = {it->second->GetBusNum(), it->second->GetDevAddr()}; - uint8_t configIndex = 0; - if (OpenDevice(dev.busNum, dev.devAddr) != UEC_OK) { - USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceImpl open fail"); - continue; - } - if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1)) { - USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); - Close(dev.busNum, dev.devAddr); - continue; - } - USBConfig configs; - if (it->second->GetConfig(static_cast(configIndex) - 1, configs)) { - USB_HILOGW(MODULE_USB_SERVICE, "get device config info failed."); - Close(dev.busNum, dev.devAddr); - continue; - } - std::vector interfaces = configs.GetInterfaces(); - for (uint32_t i = 0; i < interfaces.size(); i++) { - ManageInterface(dev, interfaces[i].GetId(), disable); - std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); - } - if (disable) { - ReportManageDeviceInfo("DeviceType", it->second, nullptr, false); + int32_t ret = OpenDevice(it->second->GetBusNum(), it->second->GetDevAddr()); + if (ret != UEC_OK) { + USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceImpl open fail ret = %{public}d", ret); + return ret; } + ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "DeviceType"); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); if (Close(dev.busNum, dev.devAddr) != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceImpl Close fail"); } @@ -1808,20 +1847,21 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo continue; } std::vector interfaces = configs.GetInterfaces(); - for (uint32_t i = 0; i < interfaces.size(); i++) { int32_t ret = RANDOM_VALUE_INDICATE; + bool last_authorize_status = interfaces[i].GetAuthorizeStatus(); // 0 indicate base class, 1 indicate subclass, 2 indicate protocal. -1 indicate any value. if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && (interfaces[i].GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && (interfaces[i].GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { - ManageInterface(dev, interfaces[i].GetId(), disable); USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", devices_.size(), static_cast(interfaceType), disable); - std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); + ret = UsbInterfaceAuthorize(dev, configs.GetId(), interfaces[i].GetId(), !disable); + interfaces[i].SetAuthorizeStatus(!disable); + USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); } - if (disable && ret == UEC_OK) { + if (disable && ret == UEC_OK && last_authorize_status == true) { ReportManageDeviceInfo("InterfaceType", it->second, &interfaces[i], true); } } @@ -1832,6 +1872,7 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo int32_t UsbHostManager::ManageDeviceTypeImpl(InterfaceType interfaceType, bool disable) { auto iterInterface = d_typeMap.find(interfaceType); + int32_t ret; if (iterInterface == d_typeMap.end()) { USB_HILOGE(MODULE_USB_SERVICE, "UsbHostManager::not find interface type"); return UEC_SERVICE_INVALID_VALUE; @@ -1841,13 +1882,10 @@ int32_t UsbHostManager::ManageDeviceTypeImpl(InterfaceType interfaceType, bool d iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && (it->second->GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { - ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), disable); USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", devices_.size(), static_cast(interfaceType), disable); - std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); - } - if (disable) { - ReportManageDeviceInfo("InterfaceType", it->second, nullptr, false); + ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "InterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); } } return UEC_OK; diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index b6f762df..8d9fc57d 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -628,6 +628,33 @@ int32_t UsbService::ManageDevice(int32_t vendorId, int32_t productId, bool disab return usbHostManager_->ManageDevice(vendorId, productId, disable); } +void UsbService::UsbDeviceIdChange(const std::vector &deviceIdInfoList, + std::vector &deviceIdList) +{ + for (auto &deviceInfo : deviceIdInfoList) { + UsbDeviceId devId; + devId.productId = deviceInfo.productId; + devId.vendorId = deviceInfo.vendorId; + deviceIdList.emplace_back(devId); + } + return; +} + +int32_t UsbService::ManageDevicePolicy(const std::vector &whiteList) +{ + if (!IsCallerValid()) { + USB_HILOGE(MODULE_USB_SERVICE, "not root or edm process."); + return UEC_SERVICE_INVALID_OPERATION; + } + if (PreCallFunction() != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "PreCallFunction failed"); + return UEC_SERVICE_PRE_MANAGE_INTERFACE_FAILED; + } + std::vector devIdList; + UsbDeviceIdChange(whiteList, devIdList); + return usbHostManager_->ManageDevicePolicy(devIdList); +} + int32_t UsbService::ManageInterfaceType(const std::vector &devTypeInfo, bool disable) { if (!IsCallerValid()) { @@ -732,7 +759,6 @@ bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr) } usbHostManager_->AddDevice(devInfo); - usbHostManager_->ExecuteStrategy(devInfo); return true; } // LCOV_EXCL_STOP -- Gitee From e94d408bd28520b334a15aafdf459f915600a160 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 16:34:41 +0800 Subject: [PATCH 02/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- interfaces/innerkits/native/include/usb_device.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/include/usb_device.h b/interfaces/innerkits/native/include/usb_device.h index bc759dc8..ac0ba8f7 100644 --- a/interfaces/innerkits/native/include/usb_device.h +++ b/interfaces/innerkits/native/include/usb_device.h @@ -35,7 +35,7 @@ enum AuthorizeStatus { DISABLED = 0, ENABLED = 1, NEW_ARRIVED = 2, -} +}; class UsbDevice : public Parcelable { public: -- Gitee From 041882326c3c1f875c3f0359fe7da63772011b03 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 16:45:45 +0800 Subject: [PATCH 03/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- interfaces/innerkits/native/include/usb_srv_client.h | 2 +- interfaces/innerkits/native/src/usb_srv_client.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index 3df467a5..b4620dbe 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -132,7 +132,7 @@ private: void UsbCtrlTransferChange(const HDI::Usb::V1_0::UsbCtrlTransfer ¶m, UsbCtlSetUp &ctlSetup); void UsbCtrlTransferChange(const HDI::Usb::V1_2::UsbCtrlTransferParams ¶m, UsbCtlSetUp &ctlSetup); void UsbTransInfoChange(const HDI::Usb::V1_2::USBTransferInfo ¶m, UsbTransInfo &info); - void UsbDeviceIdChange(const std:: &deviceIdList, std::vector &deviceIdInfoList); + void UsbDeviceIdChange(const std::vector &deviceIdList, std::vector &deviceIdInfoList); class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { public: UsbSrvDeathRecipient() = default; diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index 8cd8a6f2..e0f2e6ec 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -525,7 +525,7 @@ int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool dis return ret; } -void UsbSrvClient::UsbDeviceIdChange(const std:: &deviceIdList, +void UsbSrvClient::UsbDeviceIdChange(const std::vector &deviceIdList, std::vector &deviceIdInfoList) { for (auto &deviceId : deviceIdList) { -- Gitee From 05a17b76bfe49f327a777b2d370f846bd4d2fccd Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 16:55:09 +0800 Subject: [PATCH 04/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 415c75d3..362ca195 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1433,7 +1433,7 @@ int32_t UsbHostManager::UsbDeviceAuthorize( USBConfig config; uint8_t configIndex = 0; if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1) || - it->second->GetConfig(static_cast(configIndex) - 1, config)) { + iterDev->second->GetConfig(static_cast(configIndex) - 1, config)) { USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); return UEC_SERVICE_INVALID_VALUE; } @@ -1446,7 +1446,7 @@ int32_t UsbHostManager::UsbDeviceAuthorize( if (!authorized) { ReportManageDeviceInfo(operationType, iterDev->second, nullptr, false); } - if (authorizedStatus != NEW_ARRIVED) { // skip for newly arrived device here (send in AddDevice if not disabled) + if (authorizeStatus != NEW_ARRIVED) { // skip for newly arrived device here (send in AddDevice if not disabled) auto eventType = authorized? CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED : CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED; auto isSuccess = PublishCommonEvent(eventType, *iterDev->second); @@ -1793,7 +1793,7 @@ int32_t UsbHostManager::ManageGlobalInterfaceImpl(bool disable) ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "GlobalType"); USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); - if (Close(dev.busNum, dev.devAddr) != UEC_OK) { + if (Close(it->second->GetBusNum(), it->second->GetDevAddr()) != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageGlobalInterfaceImpl CloseDevice fail"); } } @@ -1816,7 +1816,7 @@ int32_t UsbHostManager::ManageDeviceImpl(int32_t vendorId, int32_t productId, bo } ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "DeviceType"); USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); - if (Close(dev.busNum, dev.devAddr) != UEC_OK) { + if (Close(it->second->GetBusNum(), it->second->GetDevAddr()) != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceImpl Close fail"); } } -- Gitee From 85533236e889d122ef26b2da5445338caeb0adde Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 17:18:56 +0800 Subject: [PATCH 05/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- interfaces/innerkits/native/include/usb_srv_client.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index b4620dbe..a3eec601 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -132,7 +132,8 @@ private: void UsbCtrlTransferChange(const HDI::Usb::V1_0::UsbCtrlTransfer ¶m, UsbCtlSetUp &ctlSetup); void UsbCtrlTransferChange(const HDI::Usb::V1_2::UsbCtrlTransferParams ¶m, UsbCtlSetUp &ctlSetup); void UsbTransInfoChange(const HDI::Usb::V1_2::USBTransferInfo ¶m, UsbTransInfo &info); - void UsbDeviceIdChange(const std::vector &deviceIdList, std::vector &deviceIdInfoList); + void UsbDeviceIdChange(const std::vector &deviceIdList, + std::vector &deviceIdInfoList); class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { public: UsbSrvDeathRecipient() = default; -- Gitee From eb3a84318572f25a14b2367411857f147722f668 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 20:52:43 +0800 Subject: [PATCH 06/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- test/fuzztest/BUILD.gn | 23 ++ .../corpus/init | 14 ++ .../project.xml | 25 +++ .../usbmgrmanagedevicepolicy_fuzzer.cpp | 67 ++++++ .../usbmgrmanagedevicepolicy_fuzzer.h | 21 ++ test/native/service_unittest/BUILD.gn | 36 ++++ .../include/usb_manage_device_policy_test.h | 34 +++ .../src/usb_manage_device_policy_test.cpp | 200 ++++++++++++++++++ 8 files changed, 420 insertions(+) create mode 100644 test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/corpus/init create mode 100644 test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/project.xml create mode 100644 test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp create mode 100644 test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.h create mode 100644 test/native/service_unittest/include/usb_manage_device_policy_test.h create mode 100644 test/native/service_unittest/src/usb_manage_device_policy_test.cpp diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 28346c3a..8ea1a4bc 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -978,6 +978,28 @@ ohos_fuzztest("UsbMgrManageDeviceFuzzTest") { sources = [ "usbmgrmanagedevice_fuzzer/usbmgrmanagedevice_fuzzer.cpp" ] } +ohos_fuzztest("UsbMgrManageDevicePolicyFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${usb_manager_path}/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer" + + configs = [ ":module_private_config" ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + ] + + external_deps = [ + "c_utils:utils", + "drivers_interface_usb:libusb_proxy_2.0", + "hilog:libhilog", + "ipc:ipc_core", + ] + + sources = [ "usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp" ] +} ohos_fuzztest("UsbMgrManageManageInterfaceTypeFuzzTest") { module_out_path = module_output_path @@ -1430,6 +1452,7 @@ group("fuzztest") { ":UsbMgrHasRightFuzzTest", ":UsbMgrHasSerialRightFuzzTest", ":UsbMgrManageDeviceFuzzTest", + ":UsbMgrManageDevicePolicyFuzzTest", ":UsbMgrManageGlobalInterfaceFuzzTest", ":UsbMgrManageManageInterfaceTypeFuzzTest", ":UsbMgrPipeRequestWaitFuzzTest", diff --git a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/corpus/init b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/corpus/init new file mode 100644 index 00000000..65af8ee8 --- /dev/null +++ b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/project.xml b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/project.xml new file mode 100644 index 00000000..2eb360c2 --- /dev/null +++ b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 120 + + 2048 + + diff --git a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp new file mode 100644 index 00000000..5d78c38a --- /dev/null +++ b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usbmgrmanagedevicepolicy_fuzzer.h" +#include "usb_srv_client.h" +#include "usb_errors.h" + +namespace { + //const int32_t MAX_FUNC_NUM = 6; +} + +namespace OHOS { +constexpr int32_t OFFSET = 4; +constexpr size_t THRESHOLD = 10; +namespace USB { +bool UsbMgrManageDevicePolicyFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < OFFSET + sizeof(int32_t)) { + USB_HILOGE(MODULE_USB_SERVICE, "data size is insufficient!"); + return false; + } + std::vector devList; + std::vector whiteList; + UsbDeviceId devId{*reinterpret_cast(data), *reinterpret_cast(data + OFFSET)}; + auto &usbSrvClient = UsbSrvClient::GetInstance(); + auto ret = usbSrvClient.GetDevices(devList); + if (ret != UEC_OK || devList.empty()) { + USB_HILOGE(MODULE_USB_SERVICE, "get devices failed ret=%{public}d", ret); + return false; + } + ret = usbSrvClient.ManageDevicePolicy(whiteList); + if (ret != UEC_OK) { + return false; + } + whiteList.emplace_back(devId); + ret = usbSrvClient.ManageDevicePolicy(whiteList); + if (ret != UEC_OK) { + return false; + } + return true; +} +} // USB +} // OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (size < OHOS::THRESHOLD) { + return 0; + } + /* Run your code on data */ + OHOS::USB::UsbMgrManageDevicePolicyFuzzTest(data, size); + return 0; +} + diff --git a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.h b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.h new file mode 100644 index 00000000..70991844 --- /dev/null +++ b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USBMGRMANAGEDEVICEPOLICY_FUZZER_H +#define USBMGRMANAGEDEVICEPOLICY_FUZZER_H + +#define FUZZ_PROJECT_NAME "usbmgrmanagedevicepolicy_fuzzer" + +#endif // USBMGRMANAGEDEVICEPOLICY_FUZZER_H diff --git a/test/native/service_unittest/BUILD.gn b/test/native/service_unittest/BUILD.gn index 63e966cc..17924335 100644 --- a/test/native/service_unittest/BUILD.gn +++ b/test/native/service_unittest/BUILD.gn @@ -394,6 +394,41 @@ ohos_unittest("test_usbmanageinterface") { ] } +ohos_unittest("test_usbmanagedevicepolicy") { + module_out_path = module_output_path + sources = [ + "src/usb_common_test.cpp", + "src/usb_manage_device_policy_test.cpp", + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "drivers_interface_usb:libusb_proxy_1.0", + "eventhandler:libeventhandler", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + ohos_unittest("test_usbdevicestatus") { module_out_path = module_output_path sources = [ @@ -479,6 +514,7 @@ group("unittest") { ":test_usbevent", ":test_usbhubdevice", ":test_usbmanageinterface", + ":test_usbmanagedevicepolicy", ":test_usbrequest", ] } diff --git a/test/native/service_unittest/include/usb_manage_device_policy_test.h b/test/native/service_unittest/include/usb_manage_device_policy_test.h new file mode 100644 index 00000000..91087f21 --- /dev/null +++ b/test/native/service_unittest/include/usb_manage_device_policy_test.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USB_MANAGE_DEVICE_POLICY_TEST_H +#define USB_MANAGE_DEVICE_POLICY_TEST_H + +#include + +namespace OHOS { +namespace USB { +namespace ManageDevicePolicy { +class UsbManageDevicePolicyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +} // DevicePipe +} // USB +} // OHOS +#endif // USB_MANAGE_DEVICE_POLICY_TEST_H \ No newline at end of file diff --git a/test/native/service_unittest/src/usb_manage_device_policy_test.cpp b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp new file mode 100644 index 00000000..11a9ee5e --- /dev/null +++ b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usb_manage_device_policy_test.h" + +#include +#include +#include + +#include "delayed_sp_singleton.h" +#include "hilog_wrapper.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "usb_common_test.h" +#include "usb_srv_client.h" +#include "usb_errors.h" +#include "usb_interface_type.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; +using namespace OHOS::HDI::Usb::V1_0; +using namespace OHOS::USB::Common; + + + +namespace OHOS { +namespace USB { +namespace ManageDevicePolicy { +constexpr int32_t SLEEP_TIME = 3; + +void UsbManageDevicePolicyTest::SetUpTestCase(void) +{ + UsbCommonTest::GrantPermissionSysNative(); + auto &srvClient = UsbSrvClient::GetInstance(); + auto ret = srvClient.SetPortRole(1, 1, 1); + sleep(SLEEP_TIME); + USB_HILOGI(MODULE_USB_SERVICE, "UsbManageDevicePolicyTest:: [Device] SetPortRole=%{public}d", ret); + ret = UsbCommonTest::SwitchErrCode(ret); + ASSERT_TRUE(ret == 0); + if (ret != 0) { + exit(0); + } + + std::cout << "please connect device, press enter to continue" << std::endl; + int32_t c; + while ((c = getchar()) != '\n' && c != EOF) { + ; + } + USB_HILOGI(MODULE_USB_SERVICE, "Start UsbManageDevicePolicyTest"); +} + +void UsbManageDevicePolicyTest::TearDownTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "End UsbManageDevicePolicyTest"); +} + +void UsbManageDevicePolicyTest::SetUp(void) {} + +void UsbManageDevicePolicyTest::TearDown(void) {} + +/** + * @tc.name: ManageDevicePolicy001 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageGlobalInterface001, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy001 : ManageDevicePolicy"); + auto &client = UsbSrvClient::GetInstance(); + std::vector whiteList{}; + auto ret = client.ManageDevicePolicy(whiteList); + ASSERT_EQ(ret, 0); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy001 : ManageDevicePolicy"); +} + +/** + * @tc.name: ManageDevicePolicy002 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy002, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy002 : ManageDevicePolicy"); + UsbCommonTest::GrantPermissionNormalNative(); + auto &client = UsbSrvClient::GetInstance(); + std::vector whiteList{}; + auto ret = client.ManageDevicePolicy(whiteList); + ASSERT_NE(ret, 0); + UsbCommonTest::GrantPermissionSysNative(); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy002 : ManageDevicePolicy"); +} + +/** + * @tc.name: ManageDevicePolicy003 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy003, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy003 : ManageDevicePolicy"); + auto &client = UsbSrvClient::GetInstance(); + std::vector devList; + std::vector whiteList; + auto ret = client.GetDevices(devList); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(!devList.empty()); + UsbDeviceId devId = {0, 0}; + whilteList.emplace_back(devId); + ret = client.ManageDevicePolicy(whiteList); + ASSERT_EQ(ret, 0); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy003 : ManageDevicePolicy"); +} + +/** + * @tc.name: ManageDevicePolicy004 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy004, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy004 : ManageDevicePolicy"); + auto &client = UsbSrvClient::GetInstance(); + std::vector devList; + std::vector whiteList; + auto ret = client.GetDevices(devList); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(!devList.empty()); + UsbDeviceId devId; + devId.productId = devList.at(0).GetProductId(); + devId.vendorId = devList.at(0).GetVendorId(); + whilteList.emplace_back(devId); + ret = client.ManageDevicePolicy(whiteList); + ASSERT_EQ(ret, 0); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy004 : ManageDevicePolicy"); +} + +/** + * @tc.name: ManageDevicePolicy005 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy005, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy005 : ManageDevicePolicy"); + UsbCommonTest::GrantPermissionNormalNative(); + auto &client = UsbSrvClient::GetInstance(); + std::vector devList; + std::vector whiteList; + auto ret = client.GetDevices(devList); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(!devList.empty()); + UsbDeviceId devId = {0, 0}; + whilteList.emplace_back(devId); + ret = client.ManageDevicePolicy(whiteList); + ASSERT_NE(ret, 0); + UsbCommonTest::GrantPermissionSysNative(); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy005 : ManageDevicePolicy"); +} + +/** + * @tc.name: ManageDevicePolicy006 + * @tc.desc: Test functions to ManageDevicePolicy(std::vector &whiteList); + * @tc.type: FUNC + */ +HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy006, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageDevicePolicy006 : ManageDevicePolicy"); + UsbCommonTest::GrantPermissionNormalNative(); + auto &client = UsbSrvClient::GetInstance(); + std::vector devList; + std::vector whiteList; + auto ret = client.GetDevices(devList); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(!devList.empty()); + UsbDeviceId devId; + devId.productId = devList.at(0).GetProductId(); + devId.vendorId = devList.at(0).GetVendorId(); + whilteList.emplace_back(devId); + ret = client.ManageDevicePolicy(whiteList); + ASSERT_NE(ret, 0); + UsbCommonTest::GrantPermissionSysNative(); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy006 : ManageDevicePolicy"); +} +} // ManagerInterface +} // USB +} // OHOS -- Gitee From cf81a56db81546cbf17bd983caa7e6a18c484d92 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 21:36:10 +0800 Subject: [PATCH 07/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- .../src/usb_manage_device_policy_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/native/service_unittest/src/usb_manage_device_policy_test.cpp b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp index 11a9ee5e..7308235d 100644 --- a/test/native/service_unittest/src/usb_manage_device_policy_test.cpp +++ b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp @@ -119,7 +119,7 @@ HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy003, TestSize.Level1) ASSERT_EQ(ret, 0); ASSERT_TRUE(!devList.empty()); UsbDeviceId devId = {0, 0}; - whilteList.emplace_back(devId); + whiteList.emplace_back(devId); ret = client.ManageDevicePolicy(whiteList); ASSERT_EQ(ret, 0); USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy003 : ManageDevicePolicy"); @@ -142,7 +142,7 @@ HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy004, TestSize.Level1) UsbDeviceId devId; devId.productId = devList.at(0).GetProductId(); devId.vendorId = devList.at(0).GetVendorId(); - whilteList.emplace_back(devId); + whiteList.emplace_back(devId); ret = client.ManageDevicePolicy(whiteList); ASSERT_EQ(ret, 0); USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageDevicePolicy004 : ManageDevicePolicy"); @@ -164,7 +164,7 @@ HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy005, TestSize.Level1) ASSERT_EQ(ret, 0); ASSERT_TRUE(!devList.empty()); UsbDeviceId devId = {0, 0}; - whilteList.emplace_back(devId); + whiteList.emplace_back(devId); ret = client.ManageDevicePolicy(whiteList); ASSERT_NE(ret, 0); UsbCommonTest::GrantPermissionSysNative(); @@ -189,7 +189,7 @@ HWTEST_F(UsbManageDevicePolicyTest, ManageDevicePolicy006, TestSize.Level1) UsbDeviceId devId; devId.productId = devList.at(0).GetProductId(); devId.vendorId = devList.at(0).GetVendorId(); - whilteList.emplace_back(devId); + whiteList.emplace_back(devId); ret = client.ManageDevicePolicy(whiteList); ASSERT_NE(ret, 0); UsbCommonTest::GrantPermissionSysNative(); -- Gitee From e8619053dbea4696c4e76a32f4fab7c1f073c090 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 21 Aug 2025 22:03:31 +0800 Subject: [PATCH 08/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- test/fuzztest/BUILD.gn | 14 +++++++++++++- .../usbmgrmanagedevicepolicy_fuzzer.cpp | 6 ++---- .../src/usb_manage_device_policy_test.cpp | 6 +++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 8ea1a4bc..88a7562c 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -993,12 +993,24 @@ ohos_fuzztest("UsbMgrManageDevicePolicyFuzzTest") { external_deps = [ "c_utils:utils", + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "common_event_service:cesfwk_innerkits", "drivers_interface_usb:libusb_proxy_2.0", + "eventhandler:libeventhandler", "hilog:libhilog", "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", ] - sources = [ "usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp" ] + sources = [ + "../native/service_unittest/src/usb_common_test.cpp", + "usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp" + ] } ohos_fuzztest("UsbMgrManageManageInterfaceTypeFuzzTest") { module_out_path = module_output_path diff --git a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp index 5d78c38a..9556264c 100644 --- a/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp +++ b/test/fuzztest/usbmgrmanagedevicepolicy_fuzzer/usbmgrmanagedevicepolicy_fuzzer.cpp @@ -16,10 +16,7 @@ #include "usbmgrmanagedevicepolicy_fuzzer.h" #include "usb_srv_client.h" #include "usb_errors.h" - -namespace { - //const int32_t MAX_FUNC_NUM = 6; -} +#include "usb_common_test.h" namespace OHOS { constexpr int32_t OFFSET = 4; @@ -27,6 +24,7 @@ constexpr size_t THRESHOLD = 10; namespace USB { bool UsbMgrManageDevicePolicyFuzzTest(const uint8_t* data, size_t size) { + Common::UsbCommonTest::GrantPermissionSysNative(); if (data == nullptr || size < OFFSET + sizeof(int32_t)) { USB_HILOGE(MODULE_USB_SERVICE, "data size is insufficient!"); return false; diff --git a/test/native/service_unittest/src/usb_manage_device_policy_test.cpp b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp index 7308235d..9289b469 100644 --- a/test/native/service_unittest/src/usb_manage_device_policy_test.cpp +++ b/test/native/service_unittest/src/usb_manage_device_policy_test.cpp @@ -35,8 +35,6 @@ using namespace std; using namespace OHOS::HDI::Usb::V1_0; using namespace OHOS::USB::Common; - - namespace OHOS { namespace USB { namespace ManageDevicePolicy { @@ -58,7 +56,9 @@ void UsbManageDevicePolicyTest::SetUpTestCase(void) std::cout << "please connect device, press enter to continue" << std::endl; int32_t c; while ((c = getchar()) != '\n' && c != EOF) { - ; + if (c == '\n' || c == EOF) { + break; + } } USB_HILOGI(MODULE_USB_SERVICE, "Start UsbManageDevicePolicyTest"); } -- Gitee From 1282e2d1c63c32a4c63d6cb632adbbaf5cf29446 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 22 Aug 2025 11:51:43 +0800 Subject: [PATCH 09/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 48 ++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 362ca195..ad5ca27d 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1434,8 +1434,8 @@ int32_t UsbHostManager::UsbDeviceAuthorize( uint8_t configIndex = 0; if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1) || iterDev->second->GetConfig(static_cast(configIndex) - 1, config)) { - USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); - return UEC_SERVICE_INVALID_VALUE; + USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); + return UEC_SERVICE_INVALID_VALUE; } for (auto &interface : config.GetInterfaces()) { interface.SetAuthorizeStatus(authorized); @@ -1849,19 +1849,20 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo std::vector interfaces = configs.GetInterfaces(); for (uint32_t i = 0; i < interfaces.size(); i++) { int32_t ret = RANDOM_VALUE_INDICATE; - bool last_authorize_status = interfaces[i].GetAuthorizeStatus(); + bool lastAuthorizeStatus = interfaces[i].GetAuthorizeStatus(); // 0 indicate base class, 1 indicate subclass, 2 indicate protocal. -1 indicate any value. - if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && (interfaces[i].GetSubClass() == - iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == - RANDOM_VALUE_INDICATE) && (interfaces[i].GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || + if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && + (interfaces[i].GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || + iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && + (interfaces[i].GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { - USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices_.size(), static_cast(interfaceType), disable); - ret = UsbInterfaceAuthorize(dev, configs.GetId(), interfaces[i].GetId(), !disable); - interfaces[i].SetAuthorizeStatus(!disable); - USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); + USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", + devices_.size(), static_cast(interfaceType), disable); + ret = UsbInterfaceAuthorize(dev, configs.GetId(), interfaces[i].GetId(), !disable); + interfaces[i].SetAuthorizeStatus(!disable); + USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); } - if (disable && ret == UEC_OK && last_authorize_status == true) { + if (disable && ret == UEC_OK && lastAuthorizeStatus) { ReportManageDeviceInfo("InterfaceType", it->second, &interfaces[i], true); } } @@ -1878,14 +1879,23 @@ int32_t UsbHostManager::ManageDeviceTypeImpl(InterfaceType interfaceType, bool d return UEC_SERVICE_INVALID_VALUE; } for (auto it = devices_.begin(); it != devices_.end(); ++it) { - if ((it->second->GetClass() == iterInterface->second[BASECLASS_INDEX]) && (it->second->GetSubclass() == - iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == - RANDOM_VALUE_INDICATE) && (it->second->GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || + if ((it->second->GetClass() == iterInterface->second[BASECLASS_INDEX]) && + (it->second->GetSubclass() == iterInterface->second[SUBCLASS_INDEX] || + iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && + (it->second->GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { - USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices_.size(), static_cast(interfaceType), disable); - ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "InterfaceType"); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); + USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", + devices_.size(), static_cast(interfaceType), disable); + int32_t ret = OpenDevice(it->second->GetBusNum(), it->second->GetDevAddr()); + if (ret != UEC_OK) { + USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceTypeImpl open fail ret = %{public}d", ret); + continue; + } + ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "GlobalType"); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); + if (Close(it->second->GetBusNum(), it->second->GetDevAddr()) != UEC_OK) { + USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceTypeImpl CloseDevice fail"); + } } } return UEC_OK; -- Gitee From b1902932d4676a258934a4bbf3599251d56c4974 Mon Sep 17 00:00:00 2001 From: Anduin1109 <53133285+Anduin1109@users.noreply.github.com> Date: Fri, 22 Aug 2025 12:49:38 +0800 Subject: [PATCH 10/15] =?UTF-8?q?USB=20EDM=E8=A7=84=E6=A0=BC=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index ad5ca27d..761b92a9 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1886,7 +1886,7 @@ int32_t UsbHostManager::ManageDeviceTypeImpl(InterfaceType interfaceType, bool d iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", devices_.size(), static_cast(interfaceType), disable); - int32_t ret = OpenDevice(it->second->GetBusNum(), it->second->GetDevAddr()); + ret = OpenDevice(it->second->GetBusNum(), it->second->GetDevAddr()); if (ret != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceTypeImpl open fail ret = %{public}d", ret); continue; -- Gitee From 8bec00fce2d101e93fdc90a7846dbb8d63bfa2db Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 22 Aug 2025 15:26:55 +0800 Subject: [PATCH 11/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 761b92a9..69e12b22 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1832,7 +1832,7 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo return UEC_SERVICE_INVALID_VALUE; } for (auto it = devices_.begin(); it != devices_.end(); ++it) { - if (it->second->GetClass() == BASE_CLASS_HUB) { + if (it->second->GetClass() == BASE_CLASS_HUB || it->second->GetAuthorizeStatus() == DISABLED) { continue; } UsbDev dev = {it->second->GetBusNum(), it->second->GetDevAddr()}; @@ -1849,7 +1849,9 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo std::vector interfaces = configs.GetInterfaces(); for (uint32_t i = 0; i < interfaces.size(); i++) { int32_t ret = RANDOM_VALUE_INDICATE; - bool lastAuthorizeStatus = interfaces[i].GetAuthorizeStatus(); + if (interfaces[i].GetAuthorizeStatus() == !disable) { + continue; + } // 0 indicate base class, 1 indicate subclass, 2 indicate protocal. -1 indicate any value. if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && (interfaces[i].GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || @@ -1862,7 +1864,7 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo interfaces[i].SetAuthorizeStatus(!disable); USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); } - if (disable && ret == UEC_OK && lastAuthorizeStatus) { + if (disable && ret == UEC_OK) { ReportManageDeviceInfo("InterfaceType", it->second, &interfaces[i], true); } } @@ -1891,7 +1893,7 @@ int32_t UsbHostManager::ManageDeviceTypeImpl(InterfaceType interfaceType, bool d USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceTypeImpl open fail ret = %{public}d", ret); continue; } - ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "GlobalType"); + ret = UsbDeviceAuthorize(it->second->GetBusNum(), it->second->GetDevAddr(), !disable, "InterfaceType"); USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceAuthorize ret = %{public}d", ret); if (Close(it->second->GetBusNum(), it->second->GetDevAddr()) != UEC_OK) { USB_HILOGW(MODULE_USB_SERVICE, "ManageDeviceTypeImpl CloseDevice fail"); -- Gitee From bc9bd32d454569525f9a934e8bfed30e19ded5fd Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 22 Aug 2025 15:59:43 +0800 Subject: [PATCH 12/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- test/fuzztest/BUILD.gn | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 88a7562c..0575b38f 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -992,19 +992,22 @@ ohos_fuzztest("UsbMgrManageDevicePolicyFuzzTest") { ] external_deps = [ - "c_utils:utils", "ability_base:want", + "ability_runtime:ability_manager", "access_token:libaccesstoken_sdk", "access_token:libnativetoken", "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", + "c_utils:utils", "common_event_service:cesfwk_innerkits", - "drivers_interface_usb:libusb_proxy_2.0", + "drivers_interface_usb:libusb_proxy_1.2", + "drivers_interface_usb:usb_idl_headers_1.2", "eventhandler:libeventhandler", "hilog:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "selinux_adapter:librestorecon", ] sources = [ -- Gitee From 3b61fde533f2c01670904c8ae08256b8953b4090 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 22 Aug 2025 23:18:39 +0800 Subject: [PATCH 13/15] =?UTF-8?q?USB=20EDM=E8=A7=84=E6=A0=BC=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 69e12b22..c9901810 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1846,26 +1846,25 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo USB_HILOGW(MODULE_USB_SERVICE, "get device config info failed."); continue; } - std::vector interfaces = configs.GetInterfaces(); - for (uint32_t i = 0; i < interfaces.size(); i++) { + for (auto &interface : configs.GetInterfaces()) { int32_t ret = RANDOM_VALUE_INDICATE; - if (interfaces[i].GetAuthorizeStatus() == !disable) { + if (interface.GetAuthorizeStatus() == !disable) { continue; } // 0 indicate base class, 1 indicate subclass, 2 indicate protocal. -1 indicate any value. - if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && - (interfaces[i].GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || + if ((interface.GetClass() == iterInterface->second[BASECLASS_INDEX]) && + (interface.GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && - (interfaces[i].GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || + (interface.GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", devices_.size(), static_cast(interfaceType), disable); - ret = UsbInterfaceAuthorize(dev, configs.GetId(), interfaces[i].GetId(), !disable); - interfaces[i].SetAuthorizeStatus(!disable); + ret = UsbInterfaceAuthorize(dev, configs.GetId(), interface.GetId(), !disable); + interface.SetAuthorizeStatus(disable ? DISABLED : ENABLED); USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); } if (disable && ret == UEC_OK) { - ReportManageDeviceInfo("InterfaceType", it->second, &interfaces[i], true); + ReportManageDeviceInfo("InterfaceType", it->second, &interface, true); } } } -- Gitee From a5355d0e3d6da0fb93c9ccd9c9d34bbed63ef047 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Sat, 23 Aug 2025 11:41:45 +0800 Subject: [PATCH 14/15] =?UTF-8?q?USB=E4=BC=81=E4=B8=9A=E7=AE=A1=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E6=A0=BC=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index c9901810..ca29e617 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -1430,14 +1430,17 @@ int32_t UsbHostManager::UsbDeviceAuthorize( } UsbDev dev = {busNum, devAddr}; - USBConfig config; uint8_t configIndex = 0; - if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1) || - iterDev->second->GetConfig(static_cast(configIndex) - 1, config)) { + if (GetActiveConfig(dev.busNum, dev.devAddr, configIndex) || (configIndex < 1)) { USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); return UEC_SERVICE_INVALID_VALUE; } - for (auto &interface : config.GetInterfaces()) { + uint8_t index = static_cast(configIndex) - 1; + if (index >= iterDev->second->GetConfigs().size()) { + USB_HILOGW(MODULE_USB_SERVICE, "get device config info failed."); + return UEC_SERVICE_INVALID_VALUE; + } + for (auto &interface : iterDev->second->GetConfigs()[index].GetInterfaces()) { interface.SetAuthorizeStatus(authorized); (void)ManageInterface(dev, interface.GetId(), !authorized); std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); @@ -1841,12 +1844,12 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo USB_HILOGW(MODULE_USB_SERVICE, "get device active config failed."); continue; } - USBConfig configs; - if (it->second->GetConfig(static_cast(configIndex) - 1, configs)) { + uint8_t index = static_cast(configIndex) - 1; + if (index >= it->second->GetConfigs().size()) { USB_HILOGW(MODULE_USB_SERVICE, "get device config info failed."); continue; } - for (auto &interface : configs.GetInterfaces()) { + for (auto &interface : it->second->GetConfigs()[index].GetInterfaces()) { int32_t ret = RANDOM_VALUE_INDICATE; if (interface.GetAuthorizeStatus() == !disable) { continue; @@ -1859,7 +1862,7 @@ int32_t UsbHostManager::ManageInterfaceTypeImpl(InterfaceType interfaceType, boo iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", devices_.size(), static_cast(interfaceType), disable); - ret = UsbInterfaceAuthorize(dev, configs.GetId(), interface.GetId(), !disable); + ret = UsbInterfaceAuthorize(dev, it->second->GetConfigs()[index].GetId(), interface.GetId(), !disable); interface.SetAuthorizeStatus(disable ? DISABLED : ENABLED); USB_HILOGI(MODULE_USB_SERVICE, "UsbInterfaceAuthorize ret = %{public}d", ret); } -- Gitee From 005088644580ad7a8f59dd363b3921ae1baca188 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Sat, 23 Aug 2025 14:43:12 +0800 Subject: [PATCH 15/15] =?UTF-8?q?usb=E6=8B=A6=E6=88=AA=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=92=8C=E5=B9=BF=E6=92=AD=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_host_manager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index ca29e617..1c049a89 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -456,8 +456,7 @@ int32_t UsbHostManager::GetDevices(std::vector &deviceList) USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu", devices_.size()); bool isSystemAppOrSa = usbRightManager_->IsSystemAppOrSa(); for (auto it = devices_.begin(); it != devices_.end(); ++it) { - if ((it->second->GetClass() == BASE_CLASS_HUB && !isSystemAppOrSa) || - it->second->GetAuthorizeStatus() != ENABLED) { + if (it->second->GetClass() == BASE_CLASS_HUB && !isSystemAppOrSa) { continue; } auto dev = UsbDevice(*it->second); -- Gitee