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 }
60
GetU32Data(const char * ptr)61 uint32_t GetU32Data(const char* ptr)
62 {
63 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
64 }
65
ParseData(const uint8_t * data,size_t size)66 char* ParseData(const uint8_t* data, size_t size)
67 {
68 if (data == nullptr) {
69 return nullptr;
70 }
71
72 if (size > MAX_MEM_SIZE) {
73 return nullptr;
74 }
75
76 char* ch = (char *)malloc(size + 1);
77 if (ch == nullptr) {
78 return nullptr;
79 }
80
81 (void)memset_s(ch, size + 1, 0x00, size + 1);
82 if (memcpy_s(ch, size, data, size) != EOK) {
83 free(ch);
84 ch = nullptr;
85 return nullptr;
86 }
87 return ch;
88 }
89
90 #ifdef FEATURE_NETWORK_SUPPORT
NetworkCallbackHostFuzzTest(const char * data,size_t size)91 bool NetworkCallbackHostFuzzTest(const char* data, size_t size)
92 {
93 uint32_t code = GetU32Data(data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
94 MessageParcel requestParcel;
95 requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
96 requestParcel.WriteBuffer(data, size);
97 requestParcel.RewindRead(0);
98
99 MessageParcel reply;
100 MessageOption option;
101 auto callback = sptr<NetworkCallbackHost>(new (std::nothrow) NetworkCallbackHost());
102 callback->OnRemoteRequest(code, requestParcel, reply, option);
103 return true;
104 }
105 #endif
106 } // namespace OHOS
107
108 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
110 {
111 OHOS::MockNativePermission();
112 if (size < OHOS::MIN_SIZE_NUM) {
113 return 0;
114 }
115 char* ch = OHOS::ParseData(data, size);
116 if (ch != nullptr) {
117 #ifdef FEATURE_NETWORK_SUPPORT
118 OHOS::NetworkCallbackHostFuzzTest(ch, size);
119 #endif
120 free(ch);
121 ch = nullptr;
122 }
123 return 0;
124 }
125
126