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 "dslm_inner_process.h"
17
18 #include "dslm_callback_info.h"
19 #include "dslm_core_process.h"
20 #include "securec.h"
21 #include "utils_dslm_list.h"
22 #include "utils_log.h"
23 #include "utils_mem.h"
24 #include "utils_mutex.h"
25
26 #define DFT_TIMEOUT 45
27 const uint32_t MAX_TIMEOUT = 60;
28 const uint32_t MIN_TIMEOUT = 1;
29 const uint32_t WARNING_GATE = 64;
30 const uint32_t COOKIE_SHIFT = 32;
31 const uint32_t SINGLE_OWNER = 7;
32
GetRemoteStubList(void)33 static DslmRemoteStubList *GetRemoteStubList(void)
34 {
35 static DslmRemoteStubListNode head = {.node = INIT_LIST(head.node), .key = 0, .callback = NULL, .identify = NULL};
36 static Mutex mutex = INITED_MUTEX;
37 static DslmRemoteStubList stubList = {.head = &head, .size = 0, .mutex = &mutex};
38 return &stubList;
39 }
40
SetRemoteStubStatus(DeviceIdentify * identity,DeviceSecurityInfoCallback callback,int32_t status)41 static void SetRemoteStubStatus(DeviceIdentify *identity, DeviceSecurityInfoCallback callback, int32_t status)
42 {
43 if (identity == NULL || callback == NULL) {
44 SECURITY_LOG_ERROR("unexpected input");
45 return;
46 }
47 DeviceSecurityInfo *resultInfo = (DeviceSecurityInfo *)MALLOC(sizeof(DeviceSecurityInfo));
48 if (resultInfo == NULL) {
49 SECURITY_LOG_ERROR("no memory");
50 return;
51 }
52 resultInfo->magicNum = SECURITY_MAGIC;
53 resultInfo->result = status;
54 resultInfo->level = 0;
55 SECURITY_LOG_ERROR("RequestDeviceSecurityLevelSendRequest result value error");
56 SECURITY_LOG_INFO("calling user callback");
57 callback(identity, resultInfo);
58 }
59
DslmPushRemoteStub(uint32_t owner,uint32_t cookie,const DeviceIdentify * identify,DeviceSecurityInfoCallback callback)60 static BOOL DslmPushRemoteStub(uint32_t owner, uint32_t cookie, const DeviceIdentify *identify,
61 DeviceSecurityInfoCallback callback)
62 {
63 if (GetRemoteStubList()->size > WARNING_GATE) {
64 SECURITY_LOG_WARN("remote objects max warning");
65 }
66 uint64_t key = ((uint64_t)owner << COOKIE_SHIFT) | cookie;
67 DslmRemoteStubListNode *item = (DslmRemoteStubListNode *)MALLOC(sizeof(DslmRemoteStubListNode));
68 if (item == NULL) {
69 SECURITY_LOG_ERROR("malloc failed, node is null");
70 return false;
71 }
72 memset_s(item, sizeof(DslmRemoteStubListNode), 0, sizeof(DslmRemoteStubListNode));
73
74 item->key = key;
75 item->callback = callback;
76 item->identify = identify;
77 LockMutex(GetRemoteStubList()->mutex);
78 AddListNode(&item->node, &GetRemoteStubList()->head->node);
79 GetRemoteStubList()->size++;
80 UnlockMutex(GetRemoteStubList()->mutex);
81 return true;
82 }
83
DslmPopRemoteStub(uint32_t owner,uint32_t cookie)84 static DslmRemoteStubListNode *DslmPopRemoteStub(uint32_t owner, uint32_t cookie)
85 {
86 ListNode *node = NULL;
87 ListNode *temp = NULL;
88 DslmRemoteStubListNode *item = NULL;
89
90 LockMutex(GetRemoteStubList()->mutex);
91 uint64_t key = ((uint64_t)owner << COOKIE_SHIFT) | cookie;
92 FOREACH_LIST_NODE_SAFE (node, &GetRemoteStubList()->head->node, temp) {
93 item = LIST_ENTRY(node, DslmRemoteStubListNode, node);
94 if (item->key == key) {
95 SECURITY_LOG_INFO("pop remote stub");
96 RemoveListNode(node);
97 if (GetRemoteStubList()->size > 0) {
98 GetRemoteStubList()->size--;
99 } else {
100 SECURITY_LOG_ERROR("list size is abnormal, size = %u", GetRemoteStubList()->size);
101 }
102 break;
103 }
104 }
105 UnlockMutex(GetRemoteStubList()->mutex);
106 return item;
107 }
108
ProcessCallback(uint32_t owner,uint32_t cookie,uint32_t result,const DslmCallbackInfo * info)109 static void ProcessCallback(uint32_t owner, uint32_t cookie, uint32_t result, const DslmCallbackInfo *info)
110 {
111 if ((cookie == 0) || (info == NULL)) {
112 return;
113 }
114
115 DslmRemoteStubListNode *item = DslmPopRemoteStub(owner, cookie);
116 if (item == NULL || item->callback == NULL) {
117 SECURITY_LOG_ERROR("malformed item");
118 return;
119 }
120
121 DeviceSecurityInfo *resultInfo = (DeviceSecurityInfo *)MALLOC(sizeof(DeviceSecurityInfo));
122 if (resultInfo == NULL) {
123 SECURITY_LOG_ERROR("no memory");
124 return;
125 }
126 resultInfo->magicNum = SECURITY_MAGIC;
127 resultInfo->result = result;
128 resultInfo->level = info->level;
129 SECURITY_LOG_INFO("calling user callback");
130 item->callback(item->identify, resultInfo);
131 FREE(item);
132 SECURITY_LOG_INFO("process callback succ");
133 }
134
DslmProcessGetDeviceSecurityLevel(IUnknown * iUnknown,DslmAsyncCallParams * req,DeviceSecurityInfoCallback callback)135 int32_t DslmProcessGetDeviceSecurityLevel(IUnknown *iUnknown, DslmAsyncCallParams *req,
136 DeviceSecurityInfoCallback callback)
137 {
138 if (req == NULL || callback == NULL) {
139 SECURITY_LOG_ERROR("invalid input");
140 return ERR_INVALID_PARA;
141 }
142
143 uint64_t owner = SINGLE_OWNER;
144 DslmPushRemoteStub(owner, req->cookie, req->identity, callback);
145 int32_t ret = OnRequestDeviceSecLevelInfo(req->identity, req->option, owner, req->cookie, ProcessCallback);
146 if (ret != SUCCESS) {
147 SECURITY_LOG_ERROR("OnRequestDeviceSecLevelInfo failed, ret = %d", ret);
148 SetRemoteStubStatus(req->identity, callback, ret);
149 DslmRemoteStubListNode *item = DslmPopRemoteStub(owner, req->cookie);
150 if (item != NULL) {
151 FREE(item);
152 }
153 return ret;
154 }
155 return SUCCESS;
156 }