• 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 #include "sec_comp_caller_authorization.h"
16 
17 #include <mutex>
18 #include "sec_comp_log.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace SecurityComponent {
23 namespace {
24 static constexpr int32_t MAX_FUNC_ASM_SIZE = 0x380;
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26     LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompCallerAuthorization"};
27 static constexpr size_t MAX_CALLER_SIZE = 10;
28 static std::mutex g_instanceMutex;
29 }
30 
RegisterSecCompKitCaller(std::vector<uintptr_t> & callerList)31 void SecCompCallerAuthorization::RegisterSecCompKitCaller(std::vector<uintptr_t>& callerList)
32 {
33     if (isInit_) {
34         SC_LOG_ERROR(LABEL, "can not init repeatly");
35         return;
36     }
37 
38     isInit_ = true;
39     if ((callerList.size() == 0) || (callerList.size() > MAX_CALLER_SIZE)) {
40         SC_LOG_ERROR(LABEL, "caller size is invalid");
41         return;
42     }
43 
44     kitCallerList_ = callerList;
45 }
46 
IsKitCaller(uintptr_t callerAddr)47 bool SecCompCallerAuthorization::IsKitCaller(uintptr_t callerAddr)
48 {
49     if (!isInit_) {
50         SC_LOG_INFO(LABEL, "caller authorization has not init");
51         return true;
52     }
53     for (size_t i = 0; i < kitCallerList_.size(); i++) {
54         if ((callerAddr > kitCallerList_[i]) && (callerAddr < kitCallerList_[i] + MAX_FUNC_ASM_SIZE)) {
55             return true;
56         }
57     }
58     return false;
59 }
60 
GetInstance()61 SecCompCallerAuthorization& SecCompCallerAuthorization::GetInstance()
62 {
63     static SecCompCallerAuthorization* instance = nullptr;
64     if (instance == nullptr) {
65         std::lock_guard<std::mutex> lock(g_instanceMutex);
66         if (instance == nullptr) {
67             instance = new SecCompCallerAuthorization();
68         }
69     }
70     return *instance;
71 }
72 }  // namespace SecurityComponent
73 }  // namespace Security
74 }  // namespace OHOS
75 
76