1 /* 2 * Copyright (c) 2023 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 #ifndef SECURITY_COMPONENT_PERMISSION_MANAGER_H 16 #define SECURITY_COMPONENT_PERMISSION_MANAGER_H 17 18 #include <deque> 19 #include <map> 20 #include <set> 21 #include "accesstoken_kit.h" 22 #include "rwlock.h" 23 #include "sec_comp_base.h" 24 #include "sec_event_handler.h" 25 26 namespace OHOS { 27 namespace Security { 28 namespace SecurityComponent { 29 class SecCompPermManager { 30 public: 31 SecCompPermManager() = default; 32 virtual ~SecCompPermManager() = default; 33 static SecCompPermManager& GetInstance(); 34 35 int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, 36 const std::shared_ptr<SecCompBase>& componentInfo); 37 int32_t GrantTempSavePermission(AccessToken::AccessTokenID tokenId); 38 void RevokeTempSavePermission(AccessToken::AccessTokenID tokenId); 39 bool VerifySavePermission(AccessToken::AccessTokenID tokenId); 40 bool VerifyPermission(AccessToken::AccessTokenID tokenId, SecCompType type); 41 42 int32_t GrantAppPermission(AccessToken::AccessTokenID tokenId, const std::string& permissionName); 43 int32_t RevokeAppPermission(AccessToken::AccessTokenID tokenId, const std::string& permissionName); 44 void RevokeAppPermissions(AccessToken::AccessTokenID tokenId); 45 46 void InitEventHandler(const std::shared_ptr<SecEventHandler>& secHandler); 47 std::shared_ptr<SecEventHandler> GetSecEventHandler() const; 48 49 void RevokeAppPermisionsDelayed(AccessToken::AccessTokenID tokenId); 50 void CancelAppRevokingPermisions(AccessToken::AccessTokenID tokenId); 51 52 private: 53 bool DelaySaveRevokePermission(AccessToken::AccessTokenID tokenId, const std::string& taskName); 54 bool RevokeSavePermissionTask(const std::string& taskName); 55 void RevokeTempSavePermissionCount(AccessToken::AccessTokenID tokenId); 56 void RevokeAppPermisionsImmediately(AccessToken::AccessTokenID tokenId); 57 58 void AddAppGrantPermissionRecord(AccessToken::AccessTokenID tokenId, 59 const std::string& permissionName); 60 void RemoveAppGrantPermissionRecord(AccessToken::AccessTokenID tokenId, 61 const std::string& permissionName); 62 63 std::unordered_map<AccessToken::AccessTokenID, int32_t> applySaveCountMap_; 64 std::unordered_map<AccessToken::AccessTokenID, std::deque<std::string>> saveTaskDequeMap_; 65 std::mutex mutex_; 66 std::shared_ptr<SecEventHandler> secHandler_; 67 68 std::mutex grantMtx_; 69 std::unordered_map<int32_t, std::set<std::string>> grantMap_; 70 }; 71 } // namespace SecurityComponent 72 } // namespace Security 73 } // namespace OHOS 74 #endif // SECURITY_COMPONENT_PERMISSION_MANAGER_H 75