From dc7324a2b2fc502098de81a64dd4588b611b1790 Mon Sep 17 00:00:00 2001 From: yuzhiqiang Date: Sat, 20 Apr 2024 02:10:59 +0000 Subject: [PATCH] addright logic recovery Signed-off-by: yuzhiqiang --- services/native/include/usb_right_manager.h | 4 +- services/native/include/usb_service.h | 1 + services/native/src/usb_right_manager.cpp | 49 ++++++++++++++++----- services/native/src/usb_service.cpp | 38 ++++++++++++++-- 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/services/native/include/usb_right_manager.h b/services/native/include/usb_right_manager.h index 7cb407b0..4c14e9ae 100644 --- a/services/native/include/usb_right_manager.h +++ b/services/native/include/usb_right_manager.h @@ -40,7 +40,9 @@ public: /* busDev is in busNum-devAddr format */ int32_t RequestRight(const std::string &busDev, const std::string &deviceName, const std::string &bundleName, const std::string &tokenId, const int32_t &userId); - bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName); + bool AddDeviceRight(const std::string &deviceName, const std::string &tokenIdStr); + bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName, + const std::string &tokenId, const int32_t &userId); bool RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName, const std::string &tokenId, const int32_t &userId); bool RemoveDeviceAllRight(const std::string &deviceName); diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 8c0329d1..3806e720 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -151,6 +151,7 @@ private: bool IsCommonEventServiceAbilityExist(); bool GetBundleName(std::string &bundleName); bool GetCallingInfo(std::string &bundleName, std::string &tokenId, int32_t &userId); + bool GetBundleInfo(std::string &tokenId, int32_t &userId); std::string GetDeviceVidPidSerialNumber(std::string deviceName); int32_t GetDeviceVidPidSerialNumber(std::string deviceName, std::string& strDesc); int32_t FillDevStrings(UsbDevice &dev); diff --git a/services/native/src/usb_right_manager.cpp b/services/native/src/usb_right_manager.cpp index 56c2f07c..f2f8b882 100644 --- a/services/native/src/usb_right_manager.cpp +++ b/services/native/src/usb_right_manager.cpp @@ -147,19 +147,18 @@ int32_t UsbRightManager::RequestRight(const std::string &busDev, const std::stri return UEC_OK; } -bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::string &bundleName) +bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::string &tokenIdStr) { - if (!IsAllDigits(bundleName)) { - USB_HILOGE(MODULE_USB_SERVICE, "bundleName invalid"); + if (!IsAllDigits(tokenIdStr)) { + USB_HILOGE(MODULE_USB_SERVICE, "tokenIdStr invalid"); return false; } /* already checked system app/hap when call */ - uint32_t tokenId = stoul(bundleName); + uint32_t tokenId = stoul(tokenIdStr); HapTokenInfo hapTokenInfoRes; int32_t ret = AccessTokenKit::GetHapTokenInfo((AccessTokenID) tokenId, hapTokenInfoRes); if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_SERVICE, "GetHapTokenInfo failed: tokenId:%{public}d, ret:%{public}d", - tokenId, ret); + USB_HILOGE(MODULE_USB_SERVICE, "GetHapTokenInfo failed:ret:%{public}d", ret); return false; } int32_t uid = hapTokenInfoRes.userID; @@ -170,8 +169,7 @@ bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::s uint64_t installTime = GetCurrentTimestamp(); uint64_t updateTime = GetCurrentTimestamp(); if (!GetBundleInstallAndUpdateTime(uid, hapTokenInfoRes.bundleName, installTime, updateTime)) { - USB_HILOGE(MODULE_USB_SERVICE, "get app install time and update time failed: %{public}s/%{public}d", - bundleName.c_str(), uid); + USB_HILOGE(MODULE_USB_SERVICE, "get app install time and update time failed: %{public}d", uid); } struct UsbRightAppInfo info; info.uid = uid; @@ -181,10 +179,41 @@ bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::s info.validPeriod = USB_RIGHT_VALID_PERIOD_SET; std::shared_ptr helper = UsbRightDbHelper::GetInstance(); - ret = helper->AddOrUpdateRightRecord(uid, deviceName, hapTokenInfoRes.bundleName, bundleName, info); + ret = helper->AddOrUpdateRightRecord(uid, deviceName, hapTokenInfoRes.bundleName, tokenIdStr, info); + if (ret < 0) { + USB_HILOGE(MODULE_USB_SERVICE, "add or update failed: %{public}s/%{public}d, ret=%{public}d", + deviceName.c_str(), uid, ret); + return false; + } + return true; +} + +bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::string &bundleName, + const std::string &tokenId, const int32_t &userId) +{ + /* already checked system app/hap when call */ + if (userId == USB_RIGHT_USERID_CONSOLE) { + USB_HILOGE(MODULE_USB_SERVICE, "console called, bypass"); + return true; + } + uint64_t installTime = GetCurrentTimestamp(); + uint64_t updateTime = GetCurrentTimestamp(); + if (!GetBundleInstallAndUpdateTime(userId, bundleName, installTime, updateTime)) { + USB_HILOGE(MODULE_USB_SERVICE, "get app install time and update time failed: %{public}s/%{public}d", + bundleName.c_str(), userId); + } + struct UsbRightAppInfo info; + info.uid = userId; + info.installTime = installTime; + info.updateTime = updateTime; + info.requestTime = GetCurrentTimestamp(); + info.validPeriod = USB_RIGHT_VALID_PERIOD_SET; + + std::shared_ptr helper = UsbRightDbHelper::GetInstance(); + auto ret = helper->AddOrUpdateRightRecord(userId, deviceName, bundleName, tokenId, info); if (ret < 0) { USB_HILOGE(MODULE_USB_SERVICE, "add or update failed: %{public}s/%{public}s/%{public}d, ret=%{public}d", - deviceName.c_str(), bundleName.c_str(), uid, ret); + deviceName.c_str(), bundleName.c_str(), userId, ret); return false; } return true; diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 91793b47..5de3305c 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -79,7 +79,7 @@ constexpr int32_t PROTOCAL_INDEX = 2; constexpr int32_t GET_EDM_STORAGE_DISABLE_TYPE = 2; constexpr int32_t RANDOM_VALUE_INDICATE = -1; constexpr int32_t USB_RIGHT_USERID_INVALID = -1; - +constexpr const char *USB_DEFAULT_TOKEN = "UsbServiceTokenId"; std::unordered_map> g_typeMap = { {InterfaceType::TYPE_STORAGE, {8, -1, -1}}, {InterfaceType::TYPE_AUDIO, {1, -1, -1}}, @@ -384,7 +384,11 @@ bool UsbService::HasRight(std::string deviceName) } USB_HILOGI(MODULE_USB_SERVICE, "bundle=%{public}s, device=%{public}s", bundleName.c_str(), deviceName.c_str()); - return usbRightManager_->HasRight(deviceVidPidSerialNum, bundleName, tokenId, userId); + if (usbRightManager_->HasRight(deviceVidPidSerialNum, bundleName, tokenId, userId)) { + return true; + } + + return usbRightManager_->HasRight(deviceVidPidSerialNum, bundleName, USB_DEFAULT_TOKEN, userId); } int32_t UsbService::RequestRight(std::string deviceName) @@ -440,7 +444,12 @@ int32_t UsbService::RemoveRight(std::string deviceName) return false; } - if (!usbRightManager_->RemoveDeviceRight(deviceVidPidSerialNum, bundleName, tokenId, userId)) { + if (usbRightManager_->RemoveDeviceRight(deviceVidPidSerialNum, bundleName, tokenId, userId)) { + USB_HILOGI(MODULE_USB_SERVICE, "RemoveDeviceRight done"); + return UEC_OK; + } + + if (!usbRightManager_->RemoveDeviceRight(deviceVidPidSerialNum, bundleName, USB_DEFAULT_TOKEN, userId)) { USB_HILOGE(MODULE_USB_SERVICE, "RemoveDeviceRight failed"); return UEC_SERVICE_INNER_ERR; } @@ -1376,6 +1385,21 @@ bool UsbService::GetCallingInfo(std::string &bundleName, std::string &tokenId, i return true; } +bool UsbService::GetBundleInfo(std::string &tokenId, int32_t &userId) +{ + OHOS::Security::AccessToken::AccessTokenID token = IPCSkeleton::GetCallingTokenID(); + OHOS::Security::AccessToken::HapTokenInfo hapTokenInfoRes; + int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenInfo(token, hapTokenInfoRes); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "failed, ret: %{public}d, id: %{public}u", + ret, (uint32_t)token); + return false; + } + tokenId = USB_DEFAULT_TOKEN; + userId = hapTokenInfoRes.userID; + return true; +} + int32_t UsbService::RegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe, const sptr &cb) { if (cb == nullptr) { @@ -1477,9 +1501,15 @@ int32_t UsbService::AddRight(const std::string &bundleName, const std::string &d USB_HILOGW(MODULE_USB_SERVICE, "is not system app"); return UEC_SERVICE_PERMISSION_DENIED_SYSAPI; } + std::string tokenId; + int32_t userId = USB_RIGHT_USERID_INVALID; + if (!GetBundleInfo(tokenId, userId)) { + USB_HILOGE(MODULE_USB_SERVICE, "GetCallingInfo false"); + return false; + } USB_HILOGI(MODULE_USB_SERVICE, "AddRight bundleName = %{public}s, deviceName = %{public}s", bundleName.c_str(), deviceName.c_str()); - if (!usbRightManager_->AddDeviceRight(deviceVidPidSerialNum, bundleName)) { + if (!usbRightManager_->AddDeviceRight(deviceVidPidSerialNum, bundleName, tokenId, userId)) { USB_HILOGE(MODULE_USB_SERVICE, "AddDeviceRight failed"); return UEC_SERVICE_INNER_ERR; } -- Gitee