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