diff --git a/interfaces/innerkits/include/time_service_client.h b/interfaces/innerkits/include/time_service_client.h index a3b4a99..802609b 100644 --- a/interfaces/innerkits/include/time_service_client.h +++ b/interfaces/innerkits/include/time_service_client.h @@ -165,7 +165,7 @@ public: /** * ProxyTimer * @param uid the uid - * @param isProxy true if proxy, false if not proxy + * @param isProxy true if set proxy, false if remove proxy. * @return bool true on success, false on failure. */ bool ProxyTimer(int32_t uid, bool isProxy); diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index 483c777..34818c8 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -582,13 +582,18 @@ void TimeService::NetworkTimeStatusOn() bool TimeService::ProxyTimer(int32_t uid, bool isProxy) { - TIME_HILOGD(TIME_MODULE_SERVICE, "service ProxyTimer start uid: %{public}d, isProxy: %{public}d", + auto hasPerm = DelayedSingleton::GetInstance()->CheckProxyCallingPermission(); + if (!hasPerm) { + TIME_HILOGE(TIME_MODULE_SERVICE, "ProxyTimer permission check failed"); + return E_TIME_NO_PERMISSION; + } + TIME_HILOGD(TIME_MODULE_SERVICE, "ProxyTimer service start uid: %{public}d, isProxy: %{public}d", uid, isProxy); if (timerManagerHandler_ == nullptr) { - TIME_HILOGI(TIME_MODULE_SERVICE, "ProxyTimer Timer manager nullptr."); + TIME_HILOGI(TIME_MODULE_SERVICE, "ProxyTimer manager nullptr."); timerManagerHandler_ = TimerManager::Create(); if (timerManagerHandler_ == nullptr) { - TIME_HILOGE(TIME_MODULE_SERVICE, "ProxyTimer Timer manager Init Failed."); + TIME_HILOGE(TIME_MODULE_SERVICE, "Proxytimer manager init failed."); return false; } } @@ -597,11 +602,16 @@ bool TimeService::ProxyTimer(int32_t uid, bool isProxy) bool TimeService::ResetAllProxy() { - TIME_HILOGD(TIME_MODULE_SERVICE, "service ResetAllProxy"); + auto hasPerm = DelayedSingleton::GetInstance()->CheckProxyCallingPermission(); + if (!hasPerm) { + TIME_HILOGE(TIME_MODULE_SERVICE, "ResetAllProxy permission check failed"); + return E_TIME_NO_PERMISSION; + } + TIME_HILOGD(TIME_MODULE_SERVICE, "ResetAllProxy service"); if (timerManagerHandler_ == nullptr) { timerManagerHandler_ = TimerManager::Create(); if (timerManagerHandler_ == nullptr) { - TIME_HILOGE(TIME_MODULE_SERVICE, "ResetAllProxy Timer manager init failed"); + TIME_HILOGE(TIME_MODULE_SERVICE, "ResetAllProxy timer manager init failed"); return false; } } diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 4e46e8b..30fc86a 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -316,7 +316,7 @@ HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level0) */ HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0) { - int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t uid = 99999; auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true); EXPECT_TRUE(ret); ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false); @@ -331,7 +331,10 @@ HWTEST_F(TimeServiceTest, ProxyTimer001, TestSize.Level0) */ HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0) { - auto ret = TimeServiceClient::GetInstance()->ResetAllProxy(); + int32_t uid = 99999; + auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, true); + EXPECT_TRUE(ret); + ret = TimeServiceClient::GetInstance()->ResetAllProxy(); EXPECT_TRUE(ret); } @@ -343,7 +346,7 @@ HWTEST_F(TimeServiceTest, ProxyTimer002, TestSize.Level0) */ HWTEST_F(TimeServiceTest, ProxyTimer003, TestSize.Level0) { - int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t uid = 99999; auto ret = TimeServiceClient::GetInstance()->ProxyTimer(uid, false); EXPECT_FALSE(ret); } \ No newline at end of file diff --git a/utils/native/include/time_permission.h b/utils/native/include/time_permission.h index c6bacf1..b08be0a 100644 --- a/utils/native/include/time_permission.h +++ b/utils/native/include/time_permission.h @@ -31,6 +31,7 @@ namespace MiscServices { class TimePermission { public: static bool CheckCallingPermission(const std::string &permissionName); + static bool CheckProxyCallingPermission(); }; } // namespace MiscServices } // namespace OHOS diff --git a/utils/native/src/time_permission.cpp b/utils/native/src/time_permission.cpp index 619af9a..38002ea 100644 --- a/utils/native/src/time_permission.cpp +++ b/utils/native/src/time_permission.cpp @@ -45,5 +45,12 @@ bool TimePermission::CheckCallingPermission(const std::string &permissionName) } return true; } + +bool TimePermission::CheckProxyCallingPermission() +{ + auto callerToken = IPCSkeleton::GetCallingTokenID(); + auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken); + return tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE; +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file