1 /*
2 * Copyright (c) 2021-2025 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 "softbus_ddos.h"
16
17 #include <securec.h>
18 #include <time.h>
19
20 #include "anonymizer.h"
21 #include "lnn_event.h"
22 #include "lnn_log.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_error_code.h"
25 #include "softbus_utils.h"
26 #include "legacy/softbus_hidumper_buscenter.h"
27
28 #define TABLE_COLUMNS 3
29 #define SAME_USER_SAME_ID_TIMES 100
30 #define USE_SAME_GET_DEVICE_INFO_ID_TIMES 300
31 #define ALL_USER_SAME_ID_TIMES 800
32 #define SAME_USER_ALL_ID_TIMES 1000
33 #define ALL_USER_ALL_ID_TIMES 2000
34 #define DDOS_HIDUMP_ENABLE "DdosHiDumperEnable"
35 #define DDOS_HIDUMP_DISABLE "DdosHiDumperDisable"
36
37 static SoftBusList* g_callRecord = NULL;
38 static bool g_isEnable = true;
39
SetDdosStateEnable(int fd)40 static int32_t SetDdosStateEnable(int fd)
41 {
42 g_isEnable = true;
43 SOFTBUS_DPRINTF(fd, "%s\n", "ddos already set true");
44 return SOFTBUS_OK;
45 }
46
SetDdosStateDisable(int fd)47 static int32_t SetDdosStateDisable(int fd)
48 {
49 g_isEnable = false;
50 SOFTBUS_DPRINTF(fd, "%s\n", "ddos already set false");
51 return SOFTBUS_OK;
52 }
53
IsEnableDdos()54 static bool IsEnableDdos()
55 {
56 return g_isEnable;
57 }
58
DdosHiDumperRegister()59 static int32_t DdosHiDumperRegister()
60 {
61 int32_t ret = SoftBusRegBusCenterVarDump(DDOS_HIDUMP_ENABLE, &SetDdosStateEnable);
62 LNN_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, LNN_EVENT, "regist ddos enable failed ret=%{public}d", ret);
63 ret = SoftBusRegBusCenterVarDump(DDOS_HIDUMP_DISABLE, &SetDdosStateDisable);
64 LNN_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, LNN_EVENT, "regist ddos disable failed ret=%{public}d", ret);
65 return SOFTBUS_OK;
66 }
67
68 static int32_t callTable[SOFTBUS_FUNC_ID_BUIT][TABLE_COLUMNS] = {
69 [SERVER_JOIN_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
70 [SERVER_LEAVE_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
71 [SERVER_GET_ALL_ONLINE_NODE_INFO] = {USE_SAME_GET_DEVICE_INFO_ID_TIMES, ALL_USER_SAME_ID_TIMES},
72 [SERVER_GET_LOCAL_DEVICE_INFO] = {USE_SAME_GET_DEVICE_INFO_ID_TIMES, ALL_USER_SAME_ID_TIMES},
73 [SERVER_SET_NODE_DATA_CHANGE_FLAG] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
74 [SERVER_GET_NODE_KEY_INFO] = {USE_SAME_GET_DEVICE_INFO_ID_TIMES, ALL_USER_SAME_ID_TIMES},
75 [SERVER_START_TIME_SYNC] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
76 [SERVER_STOP_TIME_SYNC] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
77 [SERVER_PUBLISH_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
78 [SERVER_STOP_PUBLISH_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
79 [SERVER_REFRESH_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
80 [SERVER_STOP_REFRESH_LNN] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
81 [SERVER_ACTIVE_META_NODE] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
82 [SERVER_DEACTIVE_META_NODE] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
83 [SERVER_GET_ALL_META_NODE_INFO] = {USE_SAME_GET_DEVICE_INFO_ID_TIMES, ALL_USER_SAME_ID_TIMES},
84 [SERVER_SHIFT_LNN_GEAR] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
85 [SERVER_SYNC_TRUSTED_RELATION] = {SAME_USER_SAME_ID_TIMES, ALL_USER_SAME_ID_TIMES},
86 };
87
CallRecordLock(void)88 static int32_t CallRecordLock(void)
89 {
90 return SoftBusMutexLock(&g_callRecord->lock);
91 }
92
CallRecordUnlock(void)93 static void CallRecordUnlock(void)
94 {
95 (void)SoftBusMutexUnlock(&g_callRecord->lock);
96 }
97
CreateAndAddCallRecord(const char * pkgName,int interfaceId)98 static CallRecord* CreateAndAddCallRecord(const char* pkgName, int interfaceId)
99 {
100 CallRecord* newRecord = (CallRecord*)SoftBusCalloc(sizeof(CallRecord));
101 if (newRecord == NULL) {
102 LNN_LOGE(LNN_EVENT, "newRecord malloc fail");
103 return NULL;
104 }
105 if (strcpy_s(newRecord->pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
106 LNN_LOGE(LNN_EVENT, "strcpy pkgName fail");
107 SoftBusFree(newRecord);
108 return NULL;
109 }
110 newRecord->interfaceId = interfaceId;
111 newRecord->timestamp = time(NULL);
112 ListAdd(&g_callRecord->list, &newRecord->node);
113 g_callRecord->cnt++;
114 return newRecord;
115 }
116
QueryCallRecord(const char * pkgName,enum SoftBusFuncId interfaceId,DdosInfo * ddosInfo)117 static int32_t QueryCallRecord(const char* pkgName, enum SoftBusFuncId interfaceId, DdosInfo *ddosInfo)
118 {
119 CallRecord *next = NULL;
120 CallRecord *item = NULL;
121 ddosInfo->funcId = interfaceId;
122 ddosInfo->userCount = 1;
123 ddosInfo->idCount = 1;
124 ddosInfo->recordCount = 1;
125 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_callRecord->list, CallRecord, node) {
126 if (strncmp(item->pkgName, pkgName, PKG_NAME_SIZE_MAX) == 0) {
127 ddosInfo->userCount++;
128 }
129 if (item->interfaceId == interfaceId) {
130 ddosInfo->idCount++;
131 if (strncmp(item->pkgName, pkgName, PKG_NAME_SIZE_MAX) == 0) {
132 ddosInfo->recordCount++;
133 }
134 }
135 }
136 if (strcpy_s(ddosInfo->pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
137 LNN_LOGE(LNN_EVENT, "strcpy pkgName fail");
138 return SOFTBUS_STRCPY_ERR;
139 }
140 ddosInfo->totalCount = (int32_t)g_callRecord->cnt;
141 int32_t column = 0;
142 int32_t ret = SOFTBUS_OK;
143 LNN_LOGI(LNN_EVENT, "ddos info, recordCount=%{public}d, idCount=%{public}d, "
144 "userCount=%{public}d, totalCount=%{public}d, interfaceid=%{public}d",
145 ddosInfo->recordCount, ddosInfo->idCount, ddosInfo->userCount, ddosInfo->totalCount, interfaceId);
146 if (ddosInfo->recordCount > callTable[interfaceId][column++]) {
147 ret = SOFTBUS_DDOS_ID_AND_USER_SAME_COUNT_LIMIT;
148 } else if (ddosInfo->idCount > callTable[interfaceId][column]) {
149 ret = SOFTBUS_DDOS_ID_SAME_COUNT_LIMIT;
150 } else if (ddosInfo->userCount > SAME_USER_ALL_ID_TIMES) {
151 ret = SOFTBUS_DDOS_USER_SAME_ID_COUNT_LIMIT;
152 } else if (ddosInfo->totalCount > ALL_USER_ALL_ID_TIMES) {
153 ret = SOFTBUS_DDOS_USER_ID_ALL_COUNT_LIMIT;
154 }
155 return ret;
156 }
157
ClearExpiredRecords(void)158 static void ClearExpiredRecords(void)
159 {
160 if (CallRecordLock() != SOFTBUS_OK) {
161 LNN_LOGE(LNN_EVENT, "CallRecord lock fail");
162 return;
163 }
164 time_t currentTime = time(NULL);
165 CallRecord *next = NULL;
166 CallRecord *item = NULL;
167 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_callRecord->list, CallRecord, node) {
168 if (currentTime - item->timestamp > TIME_THRESHOLD_SIZE) {
169 ListDelete(&item->node);
170 SoftBusFree(item);
171 g_callRecord->cnt--;
172 }
173 }
174 CallRecordUnlock();
175 }
176
IsInterfaceFuncIdValid(enum SoftBusFuncId interfaceId)177 static int32_t IsInterfaceFuncIdValid(enum SoftBusFuncId interfaceId)
178 {
179 if ((interfaceId < 0) || (interfaceId >= SOFTBUS_FUNC_ID_BUIT)) {
180 return false;
181 }
182 return true;
183 }
184
DfxReportDdosInfoResult(int32_t ret,const DdosInfo * info)185 static void DfxReportDdosInfoResult(int32_t ret, const DdosInfo* info)
186 {
187 LnnEventExtra extra = { 0 };
188 LnnEventExtraInit(&extra);
189 extra.errcode = info->errorCode;
190 extra.callerPkg = info->pkgName;
191 extra.recordCnt = info->recordCount;
192 extra.funcId = info->funcId;
193 extra.idCount = info->idCount;
194 extra.userCount = info->userCount;
195 extra.totalCount = info->totalCount;
196 LNN_EVENT(EVENT_SCENE_DDOS, EVENT_STAGE_DDOS_THRESHOLD, extra);
197 }
198
IsOverThreshold(const char * pkgName,enum SoftBusFuncId interfaceId)199 int32_t IsOverThreshold(const char* pkgName, enum SoftBusFuncId interfaceId)
200 {
201 if (!IsEnableDdos()) {
202 LNN_LOGE(LNN_EVENT, "ddos not enable");
203 return SOFTBUS_DDOS_DISABLE;
204 }
205 if (pkgName == NULL || !IsInterfaceFuncIdValid(interfaceId)) {
206 LNN_LOGE(LNN_EVENT, "pkgName or id is invalid, interfaceId=%{public}d", interfaceId);
207 return SOFTBUS_INVALID_PARAM;
208 }
209 ClearExpiredRecords();
210 if (CallRecordLock() != SOFTBUS_OK) {
211 LNN_LOGE(LNN_EVENT, "CallRecord lock fail");
212 return SOFTBUS_LOCK_ERR;
213 }
214 DdosInfo info;
215 int32_t ret = QueryCallRecord(pkgName, interfaceId, &info);
216 if (ret != SOFTBUS_OK) {
217 info.errorCode = ret;
218 DfxReportDdosInfoResult(ret, &info);
219 char *tmpName = NULL;
220 Anonymize(pkgName, &tmpName);
221 LNN_LOGE(LNN_EVENT, "use over limit ret=%{public}d, pkgName=%{public}s, interfaceId=%{public}d",
222 ret, AnonymizeWrapper(tmpName), interfaceId);
223 AnonymizeFree(tmpName);
224 CallRecordUnlock();
225 return ret;
226 }
227 CallRecord* record = CreateAndAddCallRecord(pkgName, interfaceId);
228 if (record == NULL) {
229 LNN_LOGE(LNN_EVENT, "create callrecord failed");
230 CallRecordUnlock();
231 return SOFTBUS_INVALID_PARAM;
232 }
233 CallRecordUnlock();
234 return ret;
235 }
236
RegisterClearRecordsTimer(void)237 static void RegisterClearRecordsTimer(void)
238 {
239 int32_t ret = RegisterTimeoutCallback(SOFTBUS_DDOS_TIMER_FUN, ClearExpiredRecords);
240 if (ret != SOFTBUS_OK) {
241 LNN_LOGE(LNN_EVENT, "regist callback failed ret=%{public}d", ret);
242 }
243 }
244
InitDdos(void)245 int32_t InitDdos(void)
246 {
247 if (g_callRecord != NULL) {
248 return SOFTBUS_OK;
249 }
250 g_callRecord = CreateSoftBusList();
251 if (g_callRecord == NULL) {
252 LNN_LOGE(LNN_EVENT, "create callRecord list fail");
253 return SOFTBUS_CREATE_LIST_ERR;
254 }
255 int32_t ret = DdosHiDumperRegister();
256 if (ret != SOFTBUS_OK) {
257 LNN_LOGE(LNN_EVENT, "register ddos hidumer failed");
258 return ret;
259 }
260 RegisterClearRecordsTimer();
261 g_callRecord->cnt = 0;
262 return SOFTBUS_OK;
263 }
264
DeinitDdos(void)265 void DeinitDdos(void)
266 {
267 if (CallRecordLock() != SOFTBUS_OK) {
268 LNN_LOGE(LNN_EVENT, "CallRecord lock fail");
269 return;
270 }
271 CallRecord *next = NULL;
272 CallRecord *item = NULL;
273 LIST_FOR_EACH_ENTRY_SAFE(item, next, &g_callRecord->list, CallRecord, node) {
274 ListDelete(&item->node);
275 SoftBusFree(item);
276 }
277 CallRecordUnlock();
278 DestroySoftBusList(g_callRecord);
279 g_callRecord = NULL;
280 }