• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "ipc_security/rs_ipc_interface_code_access_verifier_base.h"
17 
18 namespace OHOS {
19 namespace Rosen {
IsInterfaceCodeAccessible(CodeUnderlyingType code,const std::string & caller)20 bool RSInterfaceCodeAccessVerifierBase::IsInterfaceCodeAccessible(CodeUnderlyingType code, const std::string& caller)
21 {
22 #ifdef ENABLE_IPC_SECURITY
23     if (!IsCommonVerificationPassed(code)) {
24         ROSEN_LOGE("%s: IsCommonVerificationPassed is false.", caller.c_str());
25         return false;
26     }
27     if (!IsExtraVerificationPassed(code)) {
28         ROSEN_LOGE("%s: IsExtraVerificationPassed is false.", caller.c_str());
29         return false;
30     }
31 #endif
32     return true;
33 }
34 
GetCallingFullTokenID() const35 TokenIdType RSInterfaceCodeAccessVerifierBase::GetCallingFullTokenID() const
36 {
37 #ifdef ENABLE_IPC_SECURITY
38     // next: check the correctness of this part
39     return IPCSkeleton::GetCallingFullTokenID();
40 #endif
41     return 0;
42 }
43 
IsSystemApp() const44 bool RSInterfaceCodeAccessVerifierBase::IsSystemApp() const
45 {
46 #ifdef ENABLE_IPC_SECURITY
47     // next: check the correctness of this part
48     TokenIdType tokenId = GetCallingFullTokenID();
49     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
50 #endif
51     return true;
52 }
53 
IsCommonVerificationPassed(CodeUnderlyingType code,const std::string & caller)54 bool RSInterfaceCodeAccessVerifierBase::IsCommonVerificationPassed(CodeUnderlyingType code, const std::string& caller)
55 {
56     if (accessMap_.count(code) == 0) {
57         ROSEN_LOGE("%s: IPC code is not contained in accessMap.", caller.c_str());
58         return false;
59     }
60     const std::unordered_set<TokenIdType> *const accessSet = &(accessMap_[code]);
61     TokenIdType tokenId = GetCallingFullTokenID();
62     if (accessSet->count(tokenId) == 0) {
63         ROSEN_LOGE("%s: tokenId is not contained in accessSet.", caller.c_str());
64         return false;
65     }
66     return true;
67 }
68 } // namespace Rosen
69 } // namespace OHOS
70