1 /*
2 * Copyright (c) 2022 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 "networkcallbackhost_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locator_ability.h"
27
28 #ifdef FEATURE_NETWORK_SUPPORT
29 #include "network_callback_host.h"
30 #endif
31 #include "permission_manager.h"
32
33 namespace OHOS {
34 using namespace OHOS::Location;
35 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
36 const int32_t CODE_MAX = 50;
37 const int32_t CODE_MIN = 1;
38 const int32_t MIN_SIZE_NUM = 4;
39 const int32_t LOCATION_PERM_NUM = 4;
MockNativePermission()40 void MockNativePermission()
41 {
42 const char *perms[] = {
43 ACCESS_LOCATION.c_str(), ACCESS_APPROXIMATELY_LOCATION.c_str(),
44 ACCESS_BACKGROUND_LOCATION.c_str(), MANAGE_SECURE_SETTINGS.c_str(),
45 };
46 NativeTokenInfoParams infoInstance = {
47 .dcapsNum = 0,
48 .permsNum = LOCATION_PERM_NUM,
49 .aclsNum = 0,
50 .dcaps = nullptr,
51 .perms = perms,
52 .acls = nullptr,
53 .processName = "GnssAbility_FuzzTest",
54 .aplStr = "system_basic",
55 };
56 auto tokenId = GetAccessTokenId(&infoInstance);
57 SetSelfTokenID(tokenId);
58 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59 LocatorAbility::GetInstance()->EnableAbility(true);
60 }
61
GetU32Data(const char * ptr)62 uint32_t GetU32Data(const char* ptr)
63 {
64 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
65 }
66
ParseData(const uint8_t * data,size_t size)67 char* ParseData(const uint8_t* data, size_t size)
68 {
69 if (data == nullptr) {
70 return nullptr;
71 }
72
73 if (size > MAX_MEM_SIZE) {
74 return nullptr;
75 }
76
77 char* ch = (char *)malloc(size + 1);
78 if (ch == nullptr) {
79 return nullptr;
80 }
81
82 (void)memset_s(ch, size + 1, 0x00, size + 1);
83 if (memcpy_s(ch, size, data, size) != EOK) {
84 free(ch);
85 ch = nullptr;
86 return nullptr;
87 }
88 return ch;
89 }
90
91 #ifdef FEATURE_NETWORK_SUPPORT
NetworkCallbackHostFuzzTest(const char * data,size_t size)92 bool NetworkCallbackHostFuzzTest(const char* data, size_t size)
93 {
94 uint32_t code = GetU32Data(data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
95 MessageParcel requestParcel;
96 requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
97 requestParcel.WriteBuffer(data, size);
98 requestParcel.RewindRead(0);
99
100 MessageParcel reply;
101 MessageOption option;
102 auto callback = sptr<NetworkCallbackHost>(new (std::nothrow) NetworkCallbackHost());
103 callback->OnRemoteRequest(code, requestParcel, reply, option);
104 return true;
105 }
106 #endif
107 } // namespace OHOS
108
109 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
111 {
112 OHOS::MockNativePermission();
113 if (size < OHOS::MIN_SIZE_NUM) {
114 return 0;
115 }
116 char* ch = OHOS::ParseData(data, size);
117 if (ch != nullptr) {
118 #ifdef FEATURE_NETWORK_SUPPORT
119 OHOS::NetworkCallbackHostFuzzTest(ch, size);
120 #endif
121 free(ch);
122 ch = nullptr;
123 }
124 return 0;
125 }
126
127